1

I have project in QGIS 3.2 with different shapefiles and WMS layer. When I start the projectfile (.qgs) I want the extent of the map to be the same as one specific WMS layer (same as the option "zoom to layer" in QGIS).

I created the following macro, where I get the right scale of the layer but the extent is not adjusted to the specific layer.

def openProject():
    from qgis.utils import iface
    iface.mapCanvas().zoomScale(4096000)

Is there an macro or even an option in QGIS to accomplish this?

  • Basic option: before saving the project, zoom to that layers extent & location. – Erik Aug 1 '18 at 9:41
  • Thanks, but I don't want to change the extent manually everytime before closing the project. – Blabsl Aug 1 '18 at 10:27
2

You can use macros. Just edit openProject method in " Project > Project Properties > Macros" like that:

def openProject():
    from qgis.core import QgsProject
    from qgis.utils import iface
    canvas = iface.mapCanvas()

    # change LAYERNAME into your WMS layer name
    # I assume WMS layer has unique name, no more layers with same name
    layer = QgsProject.instance().mapLayersByName('LAYERNAME')[0]
    canvas.setExtent(layer.extent())
    canvas.refresh()

You also have to change "Enable Macros" option into "Always (not recommended)" or "For this session only" in "Settings > Options > General". Or you can just click "Enable Macros" text shown on starting a project which contains macros.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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