[Qgis-developer] Adding a WMS layer via python

Nyall Dawson nyall.dawson at gmail.com
Thu Feb 21 14:03:11 PST 2013


Hi all,

I'm wondering if someone can help me out here - I'm trying to add a
WMS layer via a python plugin, but I'm having trouble with two
settings I need:

1: setting a username and password to use for the layer
2: setting a tilesize for the layer

(obviously both these options are available when adding a WMS layer
via the GUI).


My code at the moment looks like this:

uri = QgsDataSourceURI()
uri.setParam("url", "http://images.land.vic.gov.au/ecwp/ecw_wms.dll?")
uri.setParam("layers", "AERIAL_MELBOURNE_2009JAN03_AIR_VIS_35CM_VG94")
uri.setParam("format", "image/jpeg")
uri.setParam("crs", "epsg:3111")
uri.setParam("styles", "")
uri.setParam("width", "512")
uri.setParam("height", "512")
uri.setUsername("x")
uri.setPassword("x")

print uri.encodedUri()
wms_layer = self.iface.addRasterLayer(QString(uri.encodedUri()), layer, "wms")

However, I'm still prompted for a username and password, and the
tilesize width/height parameters are ignored.

One thing I've noticed is that the uri.encodedUri output looks strange:

crs=EPSG:3111&format=image/jpeg&height=512&layers=AERIAL_MELBOURNE_2009JAN03_AIR_VIS_35CM_VG94&styles=&url=http://images.land.vic.gov.au/ecwp/ecw_wms.dll?&width=512

Any ideas what I'm doing wrong on this one?


Kind regards,
Nyall


More information about the Qgis-developer mailing list