Python one-liners for adding wms, wmts, wfs layers

In the Netherlands, currently there is a movement to make more and more data available for the general public. This is done via different services: WMS, WMTS and WFS services via a service called PDOK (Publieke Data Op de Kaart).

While creating a plugin to list and load all those layers in QGIS I had to dive into the details of the different api-possibilities to add raster and vector layers to the map via the Python inteface.

Because I think this is generally helpfull, here is some code to try.

NOTE: it’s best to try the code examples below in master/development/nightly build version of QGIS. The stable 1.8 version for example has a slightly different api version, AND is not able to handle WMTS layers at the moment. To learn how to install a ‘1.9’ version, see the post about a osgeo4w installation in this blog.

You can get the Python console via the menu ‘Plugins/Python console’. Note that they look very different in master and 1.8. In master the console will be more intelligent, in the way that it will try to do code-completion etc.

WFS

A WFS layer can be used with the same interface for both 1.8 and master. The ‘addVectorLayer’ function to use has three parameters: a data uri, a name for the layer in the legend, and a provider type. For WFS the provider type is ‘WFS”. One thing ot note is that the WFS provider needs uppercase ‘WFS’ while other providers use lowercase ‘wms’.

So the WFS layer example:

qgis.utils.iface.addVectorLayer(
"http://geodata.nationaalgeoregister.nl/bestuurlijkegrenzen/wfs?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=bestuurlijkegrenzen:provincies_2012&SRSNAME=EPSG:28992",
"wfs example",
"WFS")

WMS

The addRasterLayer layer interface changed. In 1.8 the addRasterLayer interface used a bunch of parameters. See below for an example:

qgis.utils.iface.addRasterLayer(
"http://geodata.nationaalgeoregister.nl/natura2000/ows?", # service uri
"wms 1.8 example", # name for layer (as seen in QGIS)
"wms", # dataprovider key
["natura2000:natura2000"], # array of layername(s) for provider (id's)
[""], # array of stylename(s)
"image/png", # image format string
"EPSG:28992") # crs code string

While in current master you use the same three parameters as for the WFS example (actually the extra information is put into the uri):

qgis.utils.iface.addRasterLayer(
"crs=EPSG:28992&layers=natura2000:natura2000&styles=&format=image/png&url=http://geodata.nationaalgeoregister.nl/natura2000/ows?", # uri
"wms master example", # name for layer (as seen in QGIS)
"wms" # dataprovider key
)

WMTS

Currently it is only possible in the Development version of QGIS to load WMTS layers. A WMTS layer needs some more information then a WMS layer. WMTS uses the same data provider key as WMS.

# NOT working in 1.8 !

qgis.utils.iface.addRasterLayer(
"tileMatrixSet=EPSG:28992&crs=EPSG:28992&layers=brtachtergrondkaart&styles=&format=image/png&url=http://geodata.nationaalgeoregister.nl/tiles/service/wmts/brtachtergrondkaart", # uri
"wmts master example", # name for layer (as seen in QGIS)
"wms" # dataprovider key
)

WMS-C

And last but not least, an example of the WMS-C interface. In QGIS version 1.8 this can be seen as an alternative for the non existent WMTS interface for that QGIS version. If an wmts service for example is provided via the MapProxy software, you can also expose the wmsc interface.

WMS and WMS-C share the same data provider in QGIS. Meaning that here also there is a difference between the master and 1.8 version of QGIS. To be complete I show the same layer in both versions:

In QGIS 1.8 the following is working

iface.addRasterLayer(
"tiled=256;256;3440.64;1720.32;860.16;430.08;215.04;107.52;53.76;26.88;13.44;6.72;3.36;1.68;0.84;0.42;0.21,url=http://geodata.nationaalgeoregister.nl/wmsc?tiled=true&request=getcapabilities", # service uri for capabilities
"wmsc example in 1.8", # name for layer (as seen in QGIS)
"wms", # dataprovider key
["brtachtergrondkaart"], # array of layername(s) for provider (id's)
[""], # array of stylename(s)
"image/png", # image format string
"EPSG:28992") # crs code string

OK, it needs some fiddling with uri’s and probably you will need to have a look into the xml of the getCapabilities of some services sometimes… but it is worth it isn’t it ;-)

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *

De volgende HTML tags en attributen zijn toegestaan: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>