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")

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

extent = iface.mapCanvas().extent()

map1 = QgsLayoutItemMap(layout)
layers =  QgsProject.instance().mapLayers()
zoning = layers.values(map_layer1)
map1.setRect(QRectF(20, 20, 20, 100))

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.

  • 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 May 30 '19 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 May 30 '19 at 22:55
  • It appears that QgsMapLayerREgistry however is no longer for QGIS3. – BrendanP May 30 '19 at 22:59
  • QgsMapLayerRegistry. Its functionality has been moved to QgsProject. – Fran Raga May 31 '19 at 7:10

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, privacy policy and cookie policy

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