1

Inside my .shp there are two simple layer:

  • "Points_Example" (Type: Point, EPSG:25832)
  • "Lines_Example" (Type: Line, EPSG:25832)

Some of the points are located exactly on the line and I'm trying to filter out these points via Python, but for some reason QGIS is always saying there are no points on my line!

layerLines = QgsProject.instance().mapLayersByName('Lines_Example')
layerLine = layerLines[0]

layerPoints = QgsProject.instance().mapLayersByName('Points_Example')
layerPoint = layerPoints[0]

list = []
for line in layerLine.getFeatures():
    geom = item.geometry()

    for point in layerPoint.getFeatures():
        if point.geometry().touches(geom):
            list.append(point['id'])

I've also tried some other methods from "QgsGeometry", but nothing seems to work! How can I solve this?

2
  • Should geom = item.geometry() not be geom = line.geometry() in your sample?
    – ThomasG77
    Jul 6 '20 at 22:40
  • Also I don't think touches will work between points e.g the definition in PostGIS (that use Geos behind the scene) "Returns TRUE if the only points in common between g1 and g2 lie in the union of the boundaries of g1 and g2. The ST_Touches relation applies to all Area/Area, Line/Line, Line/Area, Point/Area and Point/Line pairs of relationships, but not to the Point/Point pair."
    – ThomasG77
    Jul 6 '20 at 22:44
1

I've choose to calculate the distance between lines and points. If difference is really small, the point is part of the line (I supposed due to rounding there is a small difference)

layerLines = QgsProject.instance().mapLayersByName('Lines_Example')
layerLine = layerLines[0]

layerPoints = QgsProject.instance().mapLayersByName('Points_Example')
layerPoint = layerPoints[0]


list1 = []
for line in layerLine.getFeatures():
    geom = line.geometry()
    print(geom)
    for point in layerPoint.getFeatures():
        print(geom.distance(point.geometry()))
        if geom.distance(point.geometry()) < 10E-16:
            list1.append(point['id'])

print(list1)

You can take demo files from http://labs.webgeodatavore.com/partage/qgis-rounding.zip (Using EPSG 4326 in these files but projected coordinates will not change the logic)

1
  • Thanks, works perfectly!
    – finethen
    Jul 7 '20 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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