[Qgis-user] Add a wms layer from startu macro

G. Allegri giohappy at gmail.com
Thu Apr 24 05:29:47 PDT 2014


To run python code embedded in a project file you have to use project
macros.

Have a look at this answer:
http://gis.stackexchange.com/questions/89903/is-there-a-way-to-run-a-python-script-on-opening-a-qgis-project

Giovanni

THX Giovanni forma tour precious help.
I cannot use a file in .qgis2 folder. Because this mean to have a  setup
phase before to open the project. But i hope to apply the code directly
inside the qgis project. Your code is really interesting.
Many thx,
Andrea
Il 24/apr/2014 13:55 "G. Allegri" <giohappy at gmail.com> ha scritto:

> You can write a startup.py file and put it into the <user>/.qgis2/python
> folder.
> It will be loaded and executed, so you could check for internet
> availability with something as simple as:
>
> ------------------------------------------------------
> import urllib2
> url = <your_wms_server_getcapabilities_service>
> try:
>    urllib2.urlopen(url)
> except urllib2.URLError, e:
>    print "Could not connect"
> -----------------------------------------------------
>
> Last time I needed it I used the following (with various adaptations):
>
> -----------------------------------------------------
> import sys
> import socket
> import time
>
> host = <my_url>
> port = <my_port_number>
> # type => ["tcp" or "udp"]
> type = sys.argv[3]
> test = ""
> if len(sys.argv) > 4 :
>  test = sys.argv[4]
>
> while 1 :
>    if type == "udp":
>     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>   else:
>     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>   s.settimeout(5)
>   try:
>     if type == "udp":
>       s.sendto("--TEST LINE--", (host, port))
>       recv, svr = s.recvfrom(255)
>       s.shutdown(2)
>       print "Success connecting to " + host + " on UDP port: " + str(port)
>     else:
>       s.connect((host, port))
>       s.shutdown(2)
>       print "Success connecting to " + host + " on TCP port: " + str(port)
>   except Exception, e:
>     try:
>       errno, errtxt = e
>     except ValueError:
>       print "Cannot connect to " + host + " on port: " + str(port)
>     else:
>       if errno == 107:
>         print "Success connecting to " + host + " on UDP port: " +
> str(port)
>       else:
>         print "Cannot connect to " + host + " on port: " + str(port)
>         print e
>   if test != "C" :
>     sys.exit(0)
>
>   s.close
>   time.sleep(1)
> --------------------------------------------------------------
>
> Giovanni
>
> --
> Giovanni Allegri
> http://about.me/giovanniallegri
> Twitter: https://twitter.com/_giohappy_
> blog: http://blog.spaziogis.it
> GEO+ geomatica in Italia http://bit.ly/GEOplus
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20140424/5c9a5c43/attachment.html>


More information about the Qgis-user mailing list