[Proj] Coordinate within an area ?

Karl Swartz karl at kls2.com
Tue Mar 30 11:49:39 EST 2010


> > My next questions would be: (1) how accurate does it need to be? and (2)
> > If it is done everywhere in the world, what projection to use to reduce
> > error?

> 2/ We use global data, so it is everywhere in the world. I thought
> about using Lambert azimuthal equal-area projection.

If you're working with lat/lon coordinates why would you be using a
projection at all?

For a point and a simple polygon that's not enormous, I'd calculate
paths and compare angles.  Consider triangle ABC as the polygon and
you want to determine if point P is inside the triangle.  Compute the
initial azimuth paths AB, AC, and AP.  Then, determine if P lies within
angle A:

    if azim(AB) < azim(AC)
        within(B) = azim(AP) > azim(AB) && azim(AP) < azim(AC)
    else
        within(B) = azim(AP) > azim(AC) && azim(AP) < azim(AB)

Repeat from angles B and C and if P is within all three angles then P
is within the triangle, otherwise it is not.

Note that the comparisons I used treat the boundaries of the triangle
as "not within" -- change the "within" comparisons to >= and <= to
include the boundaries of the polygon.

This algorithm can be trivially generalized to any convex n-sided
polygon.

For the azimuth calculations, you can use Vincenty or similar if you
want high accuracy, or but you said small errors are acceptable so
basic spherical geometry may suffice.

 -- Karl


More information about the Proj mailing list