[QGIS-Developer] How do I add a QgsLayoutItemMapGrid to a QgsLayoutItemMap with PyQGIS 3.x?

Nyall Dawson nyall.dawson at gmail.com
Mon May 13 16:14:20 PDT 2019


On Tue, 14 May 2019 at 01:13, Damián Ortega Terol <dortegat at gmail.com> wrote:
>
> I'm trying to add a grid to a map but I don't get any results. My code:
>
> def add_map_layout_item(self,
>                         list_maplayers,
>                         qgs_rectangle,
>                         origin_x_mm,
>                         origin_y_mm,
>                         width_mm,
>                         height_mm,
>                         grid_interval):
>
>     map = QgsLayoutItemMap(self.layout)
>     map.updateBoundingRect()
>     map.setRect(QRectF(origin_x_mm, origin_y_mm, width_mm, height_mm))
>     map.setPos(origin_x_mm, origin_y_mm)
>     map.setFrameEnabled(True)
>
>     map.setLayers(list_maplayers)
>     map.setKeepLayerSet(True)
>     map.setExtent(qgs_rectangle)
>     self.iface.mapCanvas().freeze(True)
>
>     map.attemptMove(QgsLayoutPoint(origin_x_mm, origin_y_mm))
>     map.attemptResize(QgsLayoutSize(width_mm, height_mm))
>     map.zoomToExtent(qgs_rectangle)
>
>     qgs_layout_item_map_grid = QgsLayoutItemMapGrid("New grid",
>                                                         map)
>     qgs_layout_item_map_grid.setAnnotationPrecision(0)
>     qgs_layout_item_map_grid.setAnnotationFrameDistance(1)
>     qgs_layout_item_map_grid.setAnnotationFontColor(QColor(0, 0, 0))
>     qgs_layout_item_map_grid.setCrossLength(1.0)
>     qgs_layout_item_map_grid.setIntervalX(grid_interval)
>     qgs_layout_item_map_grid.setIntervalY(grid_interval)

You need to add your grid to the map item:

map.grids().addGrid(qgs_layout_item_map_grid)

Nyall


>
>     self.layout.addLayoutItem(map)
>
> What am I missing?
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list