Skip to main content
Tweeted twitter.com/StackGIS/status/1615906898208776194
Became Hot Network Question
edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
clarity of the type of input in code
Source Link
Kalak
  • 4k
  • 12
  • 28

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")
    multiline_layerlinestring_layer = QgsVectorLayer("multi_line"linestring.gpkg|layername=multi_line"gpkg|layername=linestring", "multiline""linestring", "ogr")
else:
    poly_layer = QgsProject.instance().mapLayersByName("poly")[0]
    multiline_layerlinestring_layer = QgsProject.instance().mapLayersByName("multi_line""linestring")[0]

multilinelinestring = next(multiline_layerlinestring_layer.getFeatures())
poly = next(poly_layer.getFeatures())

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

print(diff.asWkt())

for geom in diff.constParts():
    print(geom.asWkt())
# # or
#for geom in diff.asGeometryCollection():
#    print(geom.asWkt())

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")
    multiline_layer = QgsVectorLayer("multi_line.gpkg|layername=multi_line", "multiline", "ogr")
else:
    poly_layer = QgsProject.instance().mapLayersByName("poly")[0]
    multiline_layer = QgsProject.instance().mapLayersByName("multi_line")[0]

multiline = next(multiline_layer.getFeatures())
poly = next(poly_layer.getFeatures())

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

print(diff.asWkt())

for geom in diff.constParts():
    print(geom.asWkt())
# # or
#for geom in diff.asGeometryCollection():
#    print(geom.asWkt())

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")
else:
    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())

print(diff.asWkt())

for geom in diff.constParts():
    print(geom.asWkt())
# # or
#for geom in diff.asGeometryCollection():
#    print(geom.asWkt())
Source Link
Kalak
  • 4k
  • 12
  • 28

Wrong order of the geometry parts in the result of QgsGeometry.difference()

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")
    multiline_layer = QgsVectorLayer("multi_line.gpkg|layername=multi_line", "multiline", "ogr")
else:
    poly_layer = QgsProject.instance().mapLayersByName("poly")[0]
    multiline_layer = QgsProject.instance().mapLayersByName("multi_line")[0]

multiline = next(multiline_layer.getFeatures())
poly = next(poly_layer.getFeatures())

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

print(diff.asWkt())

for geom in diff.constParts():
    print(geom.asWkt())
# # or
#for geom in diff.asGeometryCollection():
#    print(geom.asWkt())