Geographic Information Systems Asked on May 5, 2021
I am new to Python and I am using the Python console in QGIS 3.10 to make the coordinate grid on a vector layer.
I had lots of vector files which are basically square shaped of 500 meters but all are of different location. I want to export this vector file but after making grid on top of that.
I want to do it in Python as I can loop it and do all the process at once.
My workflows goes like this:
The sample image of what i want is like this:
Initial Vector layer:
Here is my code and in output it is nit creating the grids
`
fn = r"C:UsersAsusOneDriveDocumentsmap secien360mparent gridgrid_no_A1.shp"
layer = iface.addVectorLayer(fn, 'grid_no_A1', 'ogr')
from qgis.PyQt import QtGui
layers = QgsProject.instance().mapLayersByName('grid_no_A1')
layer = layers[0]
project = QgsProject.instance()
manager = project.layoutManager()
layoutName = 'Layout1'
layouts_list = manager.printLayouts()
# remove any duplicate layouts
for layout in layouts_list:
if layout.name() == layoutName:
manager.removeLayout(layout)
layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName(layoutName)
manager.addLayout(layout)
# create map item in the layout
map = QgsLayoutItemMap(layout)
map.setRect(20, 20, 20, 20)
# set the map extent
ms = QgsMapSettings()
ms.setLayers([layer])
# set layers to be mapped
rect = QgsRectangle(ms.fullExtent())
rect.scale(1.0)
ms.setExtent(rect)
map.setExtent(rect)
map.setBackgroundColor(QColor(255, 255, 255, 0))
layout.addLayoutItem(map)
map.attemptMove(QgsLayoutPoint(5, 20, QgsUnitTypes.LayoutMillimeters))
map.attemptResize(QgsLayoutSize(180, 180, QgsUnitTypes.LayoutMillimeters))
grid = QgsLayoutItemMapGrid(layout)
out = QgsLayoutItemMap(layout)
grid = QgsLayoutItemMapGrid("hr", out)
grid.annotationEnabled()
grid.setIntervalX(0.1)
grid.setIntervalY(0.1)
grid.setAnnotationEnabled(True)
layout.addLayoutItem(out)
layout = manager.layoutByName(layoutName)
exporter = QgsLayoutExporter(layout)
fn = r'C:UsersAsusOneDriveDocumentsmap secienlayout_export.jpeg'
exporter.exportToImage(fn, QgsLayoutExporter.ImageExportSettings())
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP