[Geowanking] Simple formula for calculating if points are included in an irregularly-shaped polygon Open Layers w/ Open Street Map?

Christopher Schmidt crschmidt at metacarta.com
Wed Sep 16 07:59:44 PDT 2009


On Wed, Sep 16, 2009 at 07:23:47AM -0700, William J. Spat wrote:
> I wanted to pick the collective, dissociated geowanking brain on a problem
> that has me stumped.
> 
> I need to set a region in Open Layers using Open Street Map, and then
> automatically determine if a given point falls within that region.
> 
> I've come up with a simple formula that works with a quadrilateral-shaped
> boxed region, but some possible regions are more complicated than that,
> having more than four sides and not being exactly box shaped.
> 
> Does anyone have any suggestions for a more robust formula that would work
> for a greater range of region shapes?  Or a clever work-around?

var poly = new OpenLayers.Geometry.Polygon();
var test = poly.intersects(new OpenLayers.Geometry.Point(x, y));
if (test == true) { alert("you win!"); }

OpenLayers geometries have built in intersection methods.

-- Chris




More information about the Geowanking mailing list