I am trying to find the closest road segment to a point that I have and return an attribute from the closest segment to the given points. How should I do that? Any suggestions?

Can i use a marker and then show attribute-data from another layer in the markers popup?

Any suggestions?


Not a trivial thing to do just with Leaflet+Javascript.

The quickest thing that comes to my mind is using TurfJS to compute the distance between the point and a point on that line, for all your line geometries (iterating over them), and keep a reference to the feature using the closest geometry.

To put it all together like in this gist:

for(i = 0; i < featureCollection.features.length; i++){
    var closestPoint = pointOnLine(linestring, featureCollection.features[i]);
    var distance = distanceCalc(featureCollection.features[i], closestPoint, units);

    if(distance < closestDistance || closestDistance === 0){
        closestFeature = closestPoint;
        closestDistance = distance;

This might be computationally expensive specially in those cases where you have lots of line segments around. If that's the case, use rbush to index the bboxes of your lines, and use a k-nearest-neighbour query to cull the amount of lines to check.

Another approach would be to generate a Voronoi diagram based on line segments beforehand (which is computationally expensive), index the bboxes of the areas, and do point-in-polygon queries (which are computationally cheaper than finding the closest point in a line segment).


I have done this recently for returning the closest street address to my points of interest within ESRI ArcMap The process i used was to use the 'Near' tool (Analysis Tools/Proximity/Near). ESRI help here - http://pro.arcgis.com/en/pro-app/tool-reference/analysis/near.htm

Once you run this tool, it will add new fields to your input feature including a field called 'NEAR_FID". This is the ObjectID of the nearest attribute in the near feature you selected (in your case, the objectID of the closest road segment).

You can then do a table join to your roads database based on NEAR_FID in your points layer and OBJECTID in your roads layer. The joined table will display the nearest road name, and you can export this join in to a new feature class, or use the field calculator to update existing fields in your attribute table with road names

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.