Search code examples
algorithmgeometrykml

Find nearest points on KML polygon


In the graphic:

  • Red is GEO point (from GEO location)
  • Blue is KML polygon
  • Green is radius to check for nearest points
  • Brown dots are the nearest points on a KML inside the radius

How to find these nearest points mathematically?

Graphic


Solution

  • result = an empty list of points
    
    for each edge of the KML polygon:
        if the edge is completely inside the green circle:
            append to result the point on the edge that is closest to the green circle center (see https://math.stackexchange.com/questions/2193720/find-a-point-on-a-line-segment-which-is-the-closest-to-other-point-not-on-the-li) 
        else:
            if the edge has two different intersections with the green circle:
                find the two intersection P1 and P2
                append to result the point (P1+P2)/2
            else:
                if the edge has one intersection with the green circle:
                    append to result the intersection
                endif
            endif
        endif
    endfor