point in polygon problem

0 views
Skip to first unread message

Tarun... 【ツ】 Learning is not compulsory. Neither is survival.

unread,
Dec 25, 2009, 5:15:03 AM12/25/09
to php...@googlegroups.com
Hello Everyone..

Merry Christmas ...

well i'm stuct with a prolem that is exactly this one ... I have two MySQL data tables. One has point locations of  x,y data in the form of Lon and Lat.  The other table holds x,y data Lon and Lat of twenty vertices that make up one polygon.  I need to find which points from the point table fall inside the polygon.  I have tried to use spatial extension in mysql but it will not handle the polygon with more than four points (a rectangle).  My polygon has many vertices.  I have also tried to use many point in polygon scripts that are available on the web, such as the one http://www.assemblysys.com/dataServices/php_pointinpolygon.php
but I can't get it to work either.  I have been trying to use the following arrarys in this example.

$polygon = array("-81.512017186594051 28.462547669074866", "-81.511992815261863 28.462541107562458", "-81.511965163173500 28.462533140011544", "-81.511963757135106 28.462524235101682", "-81.511968443929788 28.462510174717693", "-81.511975942801200 28.462497989051712", "-81.511975942801200 28.462485803385562", "-81.511974068083305 28.462471274322297", "-81.511972662044911 28.462458619976644", "-81.511976880160091 28.462441278836536", "-81.511988597146683 28.462422062978476", "-81.511995158659261 28.462400035043629", "-81.511998908094938 28.462388786736426", "-81.512014843196710 28.462368633519588", "-81.512031715657486 28.462361603327679", "-81.512056555669062 28.462361603327679", "-81.512083270398477 28.462356447853438", "-81.512103423615486 28.462355510494604", "-81.512136699857422 28.462359728609727", "-81.512155447035980 28.462363009366015", "-81.512182630444897 28.462367227481252", "-81.512191066675200 28.462373320314157", "-81.512205595738692 28.462382693903521", "-81.512217312725227 28.462393473531222", "-81.512221530840463 28.462408471273989", "-81.512223405558245 28.462427687132049", "-81.512227154994036 28.462439872798029", "-81.512226217635032 28.462460026015094", "-81.512220593481516 28.462479710552657", "-81.512210751212763 28.462493302256973", "-81.512205127059190 28.462502675846338", "-81.512194347431489 28.462508768679413", "-81.512177006291324 28.462515330191820", "-81.512159196471657 28.462524703781185", "-81.512141386651990 28.462525172460687", "-81.512122170794044 28.462525172460687", "-81.512105767012827 28.462528921896308", "-81.512095456064571 28.462525641140019", "-81.512080927001193 28.462523766422180", "-81.512061711143133 28.462535014729383", "-81.512045776041361 28.462536420767663", "-81.512030778298481 28.462541576241790", "-81.512017186594051 28.462547669074866");
 
$points = array("-81.5098026 28.466316", "-81.5096105 28.4665349", "-81.5097445 28.4665184");

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


I have found this code, but I can not get it to work!  I am sure these points lie inside the polygon, but the output says that it is outside.  THis may be way over my head
<?php
class pointLocation {
    var $pointOnVertex = true; // Check if the point sits exactly on one of the vertices

    function pointLocation() {
    }
   
   
        function pointInPolygon($point, $polygon, $pointOnVertex = true) {
        $this->pointOnVertex = $pointOnVertex;
       
        // Transform string coordinates into arrays with x and y values
        $point = $this->pointStringToCoordi
nates($point);
        $vertices = array();
        foreach ($polygon as $vertex) {
            $vertices[] = $this->pointStringToCoordinates($vertex);
        }
       
        // Check if the point sits exactly on a vertex
        if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
            return "vertex";
        }
       
        // Check if the point is inside the polygon or on the boundary
        $intersections = 0;
        $vertices_count = count($vertices);
   
        for ($i=1; $i < $vertices_count; $i++) {
            $vertex1 = $vertices[$i-1];
            $vertex2 = $vertices[$i];
            if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x'])) { // Check if point is on an horizontal polygon boundary
                return "boundary";
            }
            if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) {
                $xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x'];
                if ($xinters == $point['x']) { // Check if point is on the polygon boundary (other than horizontal)
                    return "boundary";
                }
                if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
                    $intersections++;
                }
            }
        }
        // If the number of edges we passed through is even, then it's in the polygon.
        if ($intersections % 2 != 0) {
            return "inside";
        } else {
            return "outside";
        }
    }

   
   
    function pointOnVertex($point, $vertices) {
        foreach($vertices as $vertex) {
            if ($point == $vertex) {
                return true;
            }
        }
   
    }
       
   
    function pointStringToCoordinates($pointString) {
        $coordinates = explode(" ", $pointString);
        return array("x" => $coordinates[0], "y" => $coordinates[1]);
    }
   
   
}

/*** Example ***/
$pointLocation = new pointLocation();
polygon = array("-81.512017186594051 28.462547669074866", "-81.511992815261863 28.462541107562458", "-81.511965163173500 28.462533140011544", "-81.511963757135106 28.462524235101682", "-81.511968443929788 28.462510174717693", "-81.511975942801200 28.462497989051712", "-81.511975942801200 28.462485803385562", "-81.511974068083305 28.462471274322297", "-81.511972662044911 28.462458619976644", "-81.511976880160091 28.462441278836536", "-81.511988597146683 28.462422062978476", "-81.511995158659261 28.462400035043629", "-81.511998908094938 28.462388786736426", "-81.512014843196710 28.462368633519588", "-81.512031715657486 28.462361603327679", "-81.512056555669062 28.462361603327679", "-81.512083270398477 28.462356447853438", "-81.512103423615486 28.462355510494604", "-81.512136699857422 28.462359728609727", "-81.512155447035980 28.462363009366015", "-81.512182630444897 28.462367227481252", "-81.512191066675200 28.462373320314157", "-81.512205595738692 28.462382693903521", "-81.512217312725227 28.462393473531222", "-81.512221530840463 28.462408471273989", "-81.512223405558245 28.462427687132049", "-81.512227154994036 28.462439872798029", "-81.512226217635032 28.462460026015094", "-81.512220593481516 28.462479710552657", "-81.512210751212763 28.462493302256973", "-81.512205127059190 28.462502675846338", "-81.512194347431489 28.462508768679413", "-81.512177006291324 28.462515330191820", "-81.512159196471657 28.462524703781185", "-81.512141386651990 28.462525172460687", "-81.512122170794044 28.462525172460687", "-81.512105767012827 28.462528921896308", "-81.512095456064571 28.462525641140019", "-81.512080927001193 28.462523766422180", "-81.512061711143133 28.462535014729383", "-81.512045776041361 28.462536420767663", "-81.512030778298481 28.462541576241790", "-81.512017186594051 28.462547669074866");
 
$points = array("-81.5098026 28.466316", "-81.5096105 28.4665349", "-81.5097445 28.4665184");

foreach($points as $key => $point) {
    echo "$key ($point) is " . $pointLocation->pointInPolygon($point, $polygon) . "<br>";
}
?>

CAN ANYONE PLEASE HELP ME OUT OF THIS ASAP ...

Warm Regards
tarun...@gmail.com
(Tarun Aggarwal)

Reply all
Reply to author
Forward
0 new messages