2

This code imports 'MiniScale' mapping from my WMS

miniScale = 'crs=EPSG:27700&dpiMode=7&featureCount=10&format=image/jpeg&layers=9&styles=&url=http://192.000.0.00:0000/wms_service'
miniScaleLayer = QgsRasterLayer(miniScale, 'MiniScale', 'wms')
QgsMapLayerRegistry.instance().addMapLayer(miniScaleLayer)

Using similar code, I can add WMS feeds for all the other mapping types.

Of course, WMS feeds should appear and disappear as the user zooms in and out, and this is achieved via the scale dependent visibility.

How can I specify the scale dependent visibility for these layers? I can manually set this up via the layer's properties dialog, but it'd be great if these layers could be added in with the properties pre-set.

0

1 Answer 1

7

You could try something like this (in QGIS v.2.8.1):

miniScale = 'crs=EPSG:27700&dpiMode=7&featureCount=10&format=image/jpeg&layers=9&styles=&url=http://192.000.0.00:0000/wms_service'
miniScaleLayer = QgsRasterLayer(miniScale, 'MiniScale', 'wms')        

# Set scale based visibility
miniScaleLayer.setScaleBasedVisibility(True)
miniScaleLayer.setMaximumScale(100000)
miniScaleLayer.setMinimumScale(500000)

QgsMapLayerRegistry.instance().addMapLayer(miniScaleLayer)
3
  • Thanks - I'm now returning QgsRasterLayer' object has no attribute 'setScaleBasedVisibility'
    – Ben Mayo
    Mar 20, 2015 at 9:30
  • If you happen to use QGIS < v.2.8, try with miniScaleLayer.toggleScaleBasedVisibility(True), instead of miniScaleLayer.setScaleBasedVisibility(True), the rest would remain the same. Mar 20, 2015 at 13:13
  • Thanks, works also in PyQGIS3, just the values in the provided example needs to be swapped: miniScaleLayer.setMaximumScale(100000) miniScaleLayer.setMinimumScale(500000)
    – jurajb
    Nov 12, 2020 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.