If I have a linestring of objects (different from the road OSM XML file) and an OSM XML file that contains nodes and ways (extracted down to roads only), how can I detect if my linestring passes through a node?

This is given that I have already made a script that has the list of intersecting nodes from ways, so I just basically want to bring it down to the most significant ones, where my linestring passes through these intersecting nodes.

  • 2
    You tag this question both Leaflet and Python. So, where do you want to do this detection? In python-code? Or using Leaflet (in the browser, using Javascript?). For Python-solutions, take a look at Shapely (toblerity.org/shapely/manual.html)
    – atlefren
    Feb 6, 2017 at 11:17
  • @atlefren thanks for pointing it out it does seem misleading with both tags. Yep, more on pythonic solution on this one I guess.
    – Reiion
    Feb 6, 2017 at 14:03

1 Answer 1


Ok, I will assume you have a list of nodes as a list containing Shapely Points reperesenting your nodes

from shapely.geometry import Point, LineString
nodes = [Point(1, 1), Point(0, 1)]

and your linestring as a Shapely LineString

linestring = LineString([(0, 0), (0, 2)])

then this should just be a matter of finding those nodes with distance == 0.0 to the linestring

on_line = [node for node in nodes if node.distance(linestring) == 0.0]

Note that this will not catch the "near" part, as the distance is set to 0.0. Also note that Shapely uses euclidean calculations, so setting node.distance(linestring) == 1.0 will not yield meters if your coordinates are unprojected.

  • Is there no way to set shapely coordinate projections?
    – Reiion
    Feb 8, 2017 at 12:47
  • 1
    Reiion: As far as I understand the philosophy behind Shapely they've decided to not tackle coordinate transformation. It's agnostic to coordinate systems and does all calculations on euclidean plane. But, you can of course use other tools to transform your data to projected systems before doing anything with shapely. PyProj (pypi.python.org/pypi/pyproj) should work in interop with shapely
    – atlefren
    Feb 8, 2017 at 14:05
  • thanks a lot @atleren! You've been giving me a lot of direction already. I'm really new to this, learning through the ropes as I code. I'm really thankful! :)
    – Reiion
    Feb 8, 2017 at 14:16
  • 1
    @Reiion Happy to help, pay the community back by answering other questions :)
    – atlefren
    Feb 8, 2017 at 15:34

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.