[Qgis-user] Add a grid to a map frame
Mayo Rubiano
mayo.rubianoh at gmail.com
Mon Apr 12 11:10:48 PDT 2021
Hello everyone. I was trying to layout with pyQGIS, and I already have the
map frame with the layers, but now I need to add a grid, I tried with the
code written down, but the grid didn't work. I'm new pyQGIS and maybe there
are errors in the code.
project = QgsProject().instance()
layer_name = "Curvas de nivel"
layout_name = "MyLayout"
layout_page = ["Letter", 0]
layers = QgsProject.instance().mapLayersByName(layer_name)[0]
for layout in project.layoutManager().printLayouts():
if layout.name() == layout_name:
project.layoutManager().removeLayout(layout)
layout = QgsPrintLayout(project)
page = QgsLayoutItemPage(layout)
page.setPageSize(layout_page[0], layout_page[1])
layout.pageCollection().addPage(page)
map = QgsLayoutItemMap(layout)
map.attemptMove(QgsLayoutPoint(5,5,0))
map.attemptResize(QgsLayoutSize(200,200,0))
map.setExtent(layers.extent())
map_grid = QgsLayoutItemMapGrid("Grid", map)
map_grid.setEnabled(True)
map_grid.setIntervalX(layers.extent().width() / 3)
map_grid.setIntervalY(layers.extent().width() / 3)
map_grids = QgsLayoutItemMapGridStack(map).addGrid(map_grid)
layout.addLayoutItem(map)
layout.setName(layout_name)
project.layoutManager().addLayout(layout)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210412/c369d7c4/attachment.html>
More information about the Qgis-user
mailing list