3

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]

features = layer.getFeatures()

for feature in features: geom = feature.geometry() print(geom.asPoint().x(), geom.asPoint().y())

Taras
  • 32,823
  • 4
  • 66
  • 137
xor
  • 141
  • 4
  • 2
    What do you exactly mean by "coordinates of the line/multiline", all vertex coordinates or just coordinates of end and start points or coordinates of line centroid/midpoin/center? Check these https://gis.stackexchange.com/questions/304755/first-and-last-point-of-multilinestring-objects-in-qgis-3 and https://gis.stackexchange.com/questions/116211/how-to-get-coordinates-of-line. What do you want as an output, e.g. a list, or dict as well as when it is a multiline would like to have a list with lists or just a flat list or just a normal print()? – Taras Dec 03 '20 at 07:00
  • 1

2 Answers2

6

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

For multipart LineString:

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

Tested in QGIS 3.16.1

asMultiPolyline() and asPolyline() return a list of points, so you can use indexes.

To check the layer geometry type use the geometryType() method of a layer.

To separate single and multi types use the isSingleType() method.

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

Taras
  • 32,823
  • 4
  • 66
  • 137
Zoltan
  • 7,325
  • 17
  • 27
  • yeah it's correct, but if i don't know the layer type then how can I mention geom.asMultiPolyline(), it can be geom.asPolyline() or geom.asPoint(). "verts = geom.asMultiPolyline() # use geom.asPolyline() for line geometry" instead of hard coding to geom.asMultiPolyline(), can I use some run time API(). which will decide whether to use geom.asMultiPolyline()/ geom.asPolyline() or geom.asPoint() – xor Dec 03 '20 at 09:06
  • See my updates in the answer – Zoltan Dec 03 '20 at 11:13
3

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]

features = layer.getFeatures() for feature in features: 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

enter image description here

Taras
  • 32,823
  • 4
  • 66
  • 137
ahmadhanb
  • 40,826
  • 5
  • 51
  • 105
  • yeah it's correct, but if i don't know the layer type then how can I mention geom.asMultiPolyline(), it can be geom.asPolyline() or geom.asPoint(). "verts = geom.asMultiPolyline() # use geom.asPolyline() for line geometry" – xor Dec 03 '20 at 09:00
  • Add print(geom) to your code right after the geom variable, it will print the type of the geometry plus the coordinate of the vertices. – ahmadhanb Dec 03 '20 at 09:15
  • But I want the coordinates separately. – xor Dec 03 '20 at 10:45