Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a point that may be inside or outside of a polygon, I need to find the nearest point on the polygon boundary from the point.

Example image

Point(x,y) may be inside or outside of the polygon, shortest distance between polygon ABCDE and point(x,y) is marked as red line, the intersecting point is Point (m,n). I need the value of m and n.

I have ABCDE and Point(x,y)

share|improve this question
I'm afraid your question is not clear. Could you indicate with an image what exactly you need? –  Devdatta Tengshe Jul 4 '14 at 6:28
Sorry .Now I was add an image –  Virender Sehwag Jul 4 '14 at 6:42

3 Answers 3

You can get it with JSTS.

Check DistanceOp.js.

share|improve this answer
but I am using google map –  Virender Sehwag Jul 4 '14 at 10:33
There is not such feature in gmap (AFAIK). Can't you use gmap and JSTS together? –  julien Jul 4 '14 at 13:36

Find the two nearest points to your point; just do this with pythagoras, but don't sqrt, you don't need the distance, only the order.

Work out the gradient between these two points (y1-y2)/(x1-x2) where 1 is one of the points and 2 is the other. This is from solving y=mx+c for m for the two points (simultaneous equation). Then use one of these points to calculate c by substituting back into the equation y=m*x+c for one of the two points, you'll use this later.

Divide -1 by this gradient, called mn (This calculates the gradient of the normal, a line perpendicular to the original line), then calculate cn=y-mn*x, where x and y are your initial point.

You then have two lines so you have to solve the simultaneous equation y=mn*x+cn and y=m*x+c for both x and y. That's your answer!

share|improve this answer
Sorry was in a rush, ask if there's any clarification you want. –  James Jul 4 '14 at 13:01
This is incorrect. You cannot make assumptions based on which segment endpoints are closest. –  jku Jul 4 '14 at 13:02
Please clarify your point. BTW, this goes through some of the theory. math.ucsd.edu/~wgarner/math4c/derivations/distance/… –  James Jul 4 '14 at 13:07
Your link talks about distance from point to line, that part is fine. The problem is that you said you can select the correct line segment based on the distances from the segment endpoints to the reference point. This is not true for a lot of polylines: imagine a trapezoid with one very short segment and one very long segment on the opposite side. The segments are very close to each other. It's trivial to find a reference location where closest segment end points are on the short segment, but closest point is on long segment. –  jku Jul 4 '14 at 13:20
OK, thinking about it, this is a quick and dirty solution. The only way I can think of doing it rigorously is to work out the distance between all the segments and the point, select the shortest and then working out the point on that segment it corresponds to. You'll then have to check that the point is actually between the two point, and not on an extension of the lines between points that isn't part of the segment. I think the choice depends on the complexity of your shapes. –  James Jul 4 '14 at 13:23

The basic algorithm is to check every segment of the polygon and find the closest point for it. This will either be the perpedicular point (if it is on the segment) or one of the endpoints.

After doing this for all segments, pick the point with the smallest total difference.

So in your example it would be the segments:

  • AB, outside, pick B
  • BC, inside, try that point
  • CD, utside, try C
  • DE, outside, try D
  • EA, outside, try A

When you compare the differences, you will see that the perpendicular point from segment BC is the closest match. You can also do this comparison in the same run.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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