2

I have a layout with four maps over four pages in QGIS 3.4. I am building a python plugin to simplify map production for non GIS users. I am having difficulty defining the layers I want in each map item, as each map item will have slightly different layers. So far I have the code (I have left out some code above that loads a layout template and yes I have no doubt my code has some ineffeciencies :p ):

project = QgsProject.instance()
projectLayoutManager = project.layoutManager()
layout = projectLayoutManager.layoutByName("test")
iface.openLayoutDesigner(layout)

layerList = QgsProject.instance().layerTreeRoot().findLayers()

extent = iface.mapCanvas().extent()
extent.grow(0.7)

map1 = QgsLayoutItemMap(layout)
layers =  QgsProject.instance().mapLayers()
zoning = layers.values(map_layer1)
map1.setLayers(zoning)
map1.setFrameEnabled(True)
map1.setRect(QRectF(20, 20, 20, 100))
map1.setExtent(extent)
layout.addLayoutItem(map1)

I have tried a few options but keep getting the same error:

NameError: name 'map_layer1' is not defined

So, I'm not really sure where to go from here. I am just testing with two layers in the map canvas at this point, map layer 1 and map layer 2.

4
  • The error is clear, map_layer1 is not defined, add where you define it. If you want set all layer in this map need this map1.setLayers(layers)
    – Fran Raga
    Commented May 30, 2019 at 8:01
  • Yes. I think I was not clear enough with my question. Defining the layer is where I am having problems. I've not done this before and the methods I've tried have failed. My last attempt was to use: layer =QgsVectorLayer('path', 'name', 'ogr') QgsMapLayerRegistry.instance().addMapLayer(layer)
    – BrendanP
    Commented May 30, 2019 at 22:55
  • It appears that QgsMapLayerREgistry however is no longer for QGIS3.
    – BrendanP
    Commented May 30, 2019 at 22:59
  • QgsMapLayerRegistry. Its functionality has been moved to QgsProject.
    – Fran Raga
    Commented May 31, 2019 at 7:10

1 Answer 1

1

Solved by adding this:

vlayer = QgsProject.instance().mapLayersByName("map_layer1")[0]

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.