Link to home
Start Free TrialLog in
Avatar of jgoodale
jgoodaleFlag for United States of America

asked on

Test whether or not a coord exisits in polygon defined by geocodes

I have a list of geocodes that define a polygon on a map and I want to find if a particular geocode lands within the values. The polygon shape in this case is like a 7 but changes in nearly all cases. We are using SQL 2008 and I can define the table any way needed to take a single geocode and return true or false.

-117.218499,33.099865,0
-117.216804,33.09991,0
-117.216182,33.099919,0
-117.215549,33.099811,0
-117.21498,33.099658,0
-117.215602,33.098624,0
-117.21557,33.097519,0
-117.215291,33.096692,0
-117.215055,33.095532,0
-117.215173,33.095317,0
-117.215184,33.095146,0
-117.21616,33.095038,0
-117.21689,33.097267,0
-117.216825,33.098786,0
-117.219089,33.098858,0
-117.218553,33.099847,0
-117.218499,33.099865,0
Avatar of Daniel Junges
Daniel Junges
Flag of Brazil image

first transform you coords in one 2d coord system like utm, then you can use the follow function:

public bool isPointInside( Ponto ponto )
{
    int i, j;
    bool res = false;
    for ( i = 0, j = Count - 1; i < Count; j = i++ ) {
        if ( (((polygono[i].Y <= ponto.Y) && (ponto.Y < polygono[j].Y)) ||
        ((polygono[j].Y <= ponto.Y) && (ponto.Y < polygono[i].Y))) &&
        (ponto.X < (polygono[j].X - polygono[i].X) * (ponto.Y - polygono[i].Y) / (polygono[j].Y - polygono[i].Y) + polygono[i].X) )
            res = !res;
    }
    return res;
}
Avatar of jgoodale

ASKER

Can you clarify you answer more? I'm not sure what a 2d coord system like utm is..

Thanks!

You cannot use this function if you coord are in a form of degrees
ASKER CERTIFIED SOLUTION
Avatar of jgoodale
jgoodale
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Solved