Geographic Information Systems Asked by xor on January 13, 2021
We can get point layer coordinates through the following code, but I want to know the coordinates of line or multiline. How to get these coordinates?
layer = QgsProject.instance().mapLayersByName("layerName")[0]
iter = layer.getFeatures()
for feature in iter:
geom=feature.geometry()
print(geom.asPoint().x(),geom.asPoint().y())
For multipart linestring you can use the following code (I tested it in 3.16.1)
layer = QgsProject.instance().mapLayersByName("folyo")[0]
for feature in layer.getFeatures():
for part in feature.geometry().asMultiPolyline():
print("part")
for pnt in part:
print(pnt.x(), pnt.y())
For single part linestring:
layer = QgsProject.instance().mapLayersByName("Single parts")[0]
for feature in layer.getFeatures():
for pnt in feature.geometry().asPolyline():
print(pnt.x(), pnt.y())
asMultiPolyline and asPolyline returns list of Points, so you can use indexes.
UPDATE to check layer geometry type use geometryType method of layer. To separate single and multi types use isSingleType function. Here is the code for it:
layer = QgsProject.instance().mapLayersByName("your_layer")[0]
if layer.geometryType() == QgsWkbTypes.LineGeometry:
for feature in layer.getFeatures():
geom = feature.geometry()
if QgsWkbTypes.isSingleType(geom.wkbType()):
# single
for pnt in geom.asPolyline():
print(pnt.x(), pnt.y())
else:
# multipart
for part in geom.asMultiPolyline():
print("part")
for pnt in part:
print(pnt.x(), pnt.y())
Answered by Zoltan on January 13, 2021
If you want to extract the xy of vertices of a MultiPolyline geometry you can use the following code:
layer = QgsProject.instance().mapLayersByName("layerName")[0]
iter = layer.getFeatures()
for feature in iter:
geom=feature.geometry()
verts = geom.asMultiPolyline() # use geom.asPolyline() for line geometry
for vert in verts:
for i in range(len(vert)):
print(vert[i].x(),vert[i].y())
The output will be like this
-3487202.783144876 10010395.30478035
-3428476.505393841 10049595.900699263
-3428476.505393841 10049595.900699263
-3374593.995628879 9923727.261801671
-3428476.505393841 10049595.900699263
-3359626.0983730364 10059037.052994486
Answered by ahmadhanb on January 13, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP