I am currently blocked by a quite weird issue I have no idea how to solve.

On Windows 10, with QGIS Desktop 3.22.11. The simple process of doing a difference between a linestring and a polygon breaks up my linestring in a multilinestring but a recuperable one since all parts are in order. So its as simple as concatenating all the points of all parts to rebuild the clipped linestring. enter image description here However running the same difference operation inside a Debian Docker container having installed the same version of QGIS as the windows desktop version gives the following result enter image description here which is barelly usable since I need to rebuild the linestring from the multiple parts.

I am unsure of what exactly causes the change in behavior except that it is consistent and repeatable. And that it is probably caused by how the (self-intersecting) geometry that result from the QgsGeometry.difference() operation is built

test code used to generate the geometry:

from qgis.core import QgsVectorLayer

if __name__ == "__main__":
    poly_layer = QgsVectorLayer("poly.gpkg|layername=poly", "poly", "ogr")
    linestring_layer = QgsVectorLayer("linestring.gpkg|layername=linestring", "linestring", "ogr")
    poly_layer = QgsProject.instance().mapLayersByName("poly")[0]
    linestring_layer = QgsProject.instance().mapLayersByName("linestring")[0]

linestring = next(linestring_layer.getFeatures())
poly = next(poly_layer.getFeatures())

diff = linestring.geometry().difference(poly.geometry())


for geom in diff.constParts():
# # or
#for geom in diff.asGeometryCollection():
#    print(geom.asWkt())
  • are both of your input geometries valid? If all you want to do is stitch them together then gis.stackexchange.com/questions/198317/… might help
    – Ian Turton
    Commented Jan 18, 2023 at 15:55
  • @IanTurton The problem is that I have more than two geometries that need to be stitch back together in order. And that would be ok except that I need to account for self-intersection as shown in the pictures (exaggerated for the example but still present) making the choice of which two geometries to stitch together at a time non trivial if the geometries aren't in order out of the difference function.
    – Kalak
    Commented Jan 18, 2023 at 19:30

1 Answer 1


By the simple features standard, the order of members in a multigeometry does not matter. The following query in PostGIS returns true. Notice also the reversed order of vertices in one part.

ST_GeomFromText('MULTILINESTRING ((0 0, 20 20),(30 30, 100 100))'),
ST_GeomFromText('MULTILINESTRING ((30 30, 100 100),(20 20, 0 0))'));

You cannot trust that the geometry functions are returning the members always in the same and predictable order. Unfortunately I cannot suggest how to get the result that you wish.

  • I get what you are saying but I find this quite bothersome. I understand the utility of multilinestring and them becoming default type. However A function that breaks the order of the vertices in the geometry makes it a lot harder to do what I want since I will have to split my linestring into individual multilinestrings (hopeful not loosing the order there) do the difference on each then rebuild from all the results hoping the multilinestring haven't split further with the difference operations .... that's quite the nightmare
    – Kalak
    Commented Jan 18, 2023 at 14:58
  • Especially since I can confidently say (that process and others using that same principle have been working perfectly for years) that on windows the result is always ordered
    – Kalak
    Commented Jan 18, 2023 at 15:05
  • 1
    I do not know if the QGIS function is made to sort the parts or if you have been lucky for all these years. With PostGIS I have been using postgis.net/docs/ST_Normalize.html but even that would not probably do what you want.
    – user30184
    Commented Jan 18, 2023 at 15:21

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.