Select to view content in your preferred language

How to get closest LINE to a point

4440
6
01-05-2013 05:38 PM
WalterEralio
Occasional Contributor
I have a street layer with each street segment and I need to find the closest one to a particular lat-lon (sort of reverse geocoding but without the locator .. I don't have one) - Back on arcims I used to do incremental buffers till I got to a street segment but it sounds inefficient... any idea?
0 Kudos
6 Replies
AnthonyGiles
Honored Contributor
Walter,

You could set up a geoprocessing task that takes the lat-long location as an input, creates a point file from it then performs a near analysis against your street layer to find the nearest street. Have a look at the near analysis tool:

http://resources.arcgis.com/en/help/main/10.1/index.html#//00080000001q000000

Regards

Anthony
0 Kudos
JohnnyPenet
Emerging Contributor
I have a street layer with each street segment and I need to find the closest one to a particular lat-lon (sort of reverse geocoding but without the locator .. I don't have one) - Back on arcims I used to do incremental buffers till I got to a street segment but it sounds inefficient... any idea?


Walter,

You can do it with a geoprocessing task with all processing at the server but this involves some constraints to fullfill. Another way is doing most of the job in javascript requiring no geoprocessing task. This is the way it can be done:

1. Select all the polylines involved, this can be done using a spatial query on the feature layer with the polylines (streets). You must select an extent, this could be the extent of the map showed.

2. Loop through all the polylines and within a polyline loop through all lines (paths)

3. For each path calculate the distance between the point and the line. Maintain the minimum distance found

4. At the end of both loops, you have the resulting polyline

The only difficulty consist of calculating the distance between a point and a line. Below you find code that I retrieved from the internet.
Another item of attention is that you need to convert lat / lon to geometry coordinates. For this you can use esri.geometry.geographicToWebMercator method
The only limitation to this kind of approach is that You must limit the number of polylines involved.

Here is some javascript code reflecting some old school mathematics:

function sqr(x) { return x * x }
function dist2(v, w) { return sqr(v.x - w.x) + sqr(v.y - w.y) }
function distToSegmentSquared(p, v, w) {
  var l2 = dist2(v, w);
  if (l2 == 0) return dist2(p, v);
  var t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2;
  if (t < 0) return dist2(p, v);
  if (t > 1) return dist2(p, w);
  return dist2(p, { x: v.x + t * (w.x - v.x),
                    y: v.y + t * (w.y - v.y) });
}
function distToSegment(p, v, w) { return Math.sqrt(distToSegmentSquared(p, v, w)); }

The issue you raised here is very interesting. I hope one day to add it as a tool to my JavaScript Framework that can be found in my blog. The concept explained above can easy be extended for polygons. (see http://jpenet.blogspot.com)

Johnny
0 Kudos
WalterEralio
Occasional Contributor
Thanks guys.. will try..   was trying to find something like ClosestFacilityTask .. will have to do the old javascript trick .. THANKS!
0 Kudos
MikeSteven
Regular Contributor
Hi, I'd like to do the same thing. Did you ever get a working solution?

Cheers,
Mike
0 Kudos
GaneshSolai_Sambandam
Regular Contributor
Hi, I'd like to do the same thing. Did you ever get a working solution?

Cheers,
Mike

Is there any update on this.
0 Kudos
LisaHolm
New Contributor
Is there any update on this.


Looking for resolution on this as well. Basically I need near functionality for a target point file to point/line/poly files but have only an ArcView license. Not finding any (working) scripts or free utilities. Please help.
0 Kudos