3

I have a LineString that I modify by extending the line. After extending it I want to check if it crosses itself. I am using QgsGeometry().crosses() to test this. For example:

wkt_new = 'MultiLineString ((688017.94950945943128318 5331570.45111508388072252, 684852.62947463185992092 5328735.23067484982311726, 686420.68171600450295955 5329768.71965211816132069, 690305.1747684960719198 5330196.37026340141892433, 691766.31435704790055752 5330873.48373126704245806, 696755.57148868835065514 5329234.15638801362365484, 699285.83760544890537858 5327915.56700322311371565, 701352.81555998569820076 5329661.80699929688125849, 697483.55642176070250571 5328310.3356888284906745))'
wkt_old = 'MultiLineString ((684852.62947463185992092 5328735.23067484982311726, 686420.68171600450295955 5329768.71965211816132069, 690305.1747684960719198 5330196.37026340141892433, 691766.31435704790055752 5330873.48373126704245806, 696755.57148868835065514 5329234.15638801362365484, 699285.83760544890537858 5327915.56700322311371565, 701352.81555998569820076 5329661.80699929688125849))'

geom_new = QgsGeometry.fromWkt(wkt_new)
geom_old = QgsGeometry.fromWkt(wkt_old)

print(geom_new.crosses(geom_old))

unexpectedly returns False.

How could I check if a line crosses itself (or more general: another geometry/line)?

enter image description here

1
  • 1
    In crosses predicate between lines, the intersection between their interiors (all the line except start and end points) must return points (dim(II) = 0). Maybe you need to compare only the extension segments to know if they cross the original line. Jan 14 at 3:31

1 Answer 1

2

Finding line is self intersected or not using PyQGIS

isSimple()

Determines whether the geometry is simple (according to OGC definition), i.e. it has no anomalous geometric points, such as self-intersection or self-tangency. Uses GEOS library for the test.

This is useful mainly for linestrings and linear rings. Polygons are simple by definition, for checking anomalies in polygon geometries one can use isGeosValid().

wkt_new = 'MultiLineString ((688017.94950945943128318 5331570.45111508388072252, 684852.62947463185992092 5328735.23067484982311726, 686420.68171600450295955 5329768.71965211816132069, 690305.1747684960719198 5330196.37026340141892433, 691766.31435704790055752 5330873.48373126704245806, 696755.57148868835065514 5329234.15638801362365484, 699285.83760544890537858 5327915.56700322311371565, 701352.81555998569820076 5329661.80699929688125849, 697483.55642176070250571 5328310.3356888284906745))'
wkt_old = 'MultiLineString ((684852.62947463185992092 5328735.23067484982311726, 686420.68171600450295955 5329768.71965211816132069, 690305.1747684960719198 5330196.37026340141892433, 691766.31435704790055752 5330873.48373126704245806, 696755.57148868835065514 5329234.15638801362365484, 699285.83760544890537858 5327915.56700322311371565, 701352.81555998569820076 5329661.80699929688125849))'

geom_new = QgsGeometry.fromWkt(wkt_new)
geom_old = QgsGeometry.fromWkt(wkt_old)

new_layer = QgsVectorLayer("MultiLineString?crs=epsg:31255", "New", "Memory")
QgsProject.instance().addMapLayer(new_layer)

feat = QgsFeature()
feat.setGeometry(geom_new)
new_layer.dataProvider().addFeatures([feat])
new_layer.updateFields()

for ft in new_layer.getFeatures():
    if not ft.geometry().isSimple():
        print ("Not simple - self-intersect")
    else:
        print("Simple - no intersect")
2
  • So does crosses() always return false if one of the two geometries is not simple?
    – MrXsquared
    Jan 15 at 11:54
  • isSimple() just tests one geometry (self) according to ogc.org/standard/sfa --> Basic methods on geometric objects
    – Xeppit
    Jan 15 at 15:04

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.