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

2 Answers 2

7

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())
2
  • 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
    Commented Dec 3, 2020 at 9:06
  • See my updates in the answer
    – Zoltan
    Commented Dec 3, 2020 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

3
  • 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
    Commented Dec 3, 2020 at 9: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
    Commented Dec 3, 2020 at 9:15
  • But I want the coordinates separately.
    – xor
    Commented Dec 3, 2020 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.