简体   繁体   中英

How to check weather a point is inside a polygon or not in PHP mysql

I am trying to get the points those are within the shape file. I have tried the Spatial functions and it's showing an error called

3033 - Binary geometry function st_contains given two geometries of different srids: 4326 and 0, which should have been identical.

My query is

SELECT lightning.lightning_id,lightning.latitude,lightning.longitude,
lightning.flash_type, DATE_FORMAT(lightning.lightning_time, '%D %b %Y %T %p') as lightning_time FROM lightning, districts_s 
WHERE ST_CONTAINS(districts_s.SHAPE, Point(lightning.longitude, lightning.latitude)) AND lightning.height > 50

To rectify the error I got a solution from here and this link but no result. as this also showing an error called

ST_Transform does not exist

I am using PHP 5.6. Any solution is really helpful.

You can try with PHP code.

$point = array(22.765863,75.887488);
$vs = array();
$polygonPoints = '[{"lat":22.765863133410004,"lng":75.88748805487819},{"lat":22.76631821037386,"lng":75.89153282607265},{"lat":22.76462650356429,"lng":75.89126460517116},{"lat":22.764082382883878,"lng":75.89017026389308},{"lat":22.76430992452311,"lng":75.88933341468044},{"lat":22.763815268301787,"lng":75.88875405753322},{"lat":22.764705648209898,"lng":75.8886253115005},{"lat":22.764438534847113,"lng":75.88744513953395},{"lat":22.765229980328275,"lng":75.88824980223842}]';


$boundries = json_decode($polygonPoints);

foreach ($boundries as $key => $val)
{
    $polygon[$key] = array($val->lat,$val->lng);
}

// Check Point Inside Polygone

$check = pointInPolygon($point, $polygon);
if($check == true)
{
    echo "This point inside in polygone";
}
function pointInPolygon($point, $vs)
{
   $x = $point[0];
   $y = $point[1];

   $inside = false;
   for ($i = 0, $j = count($vs) - 1; $i < count($vs); $j = $i++) {
       $xi = $vs[$i][0];
       $yi = $vs[$i][1];
       $xj = $vs[$j][0];
       $yj = $vs[$j][1];

       $intersect = (($yi > $y) != ($yj > $y))
           && ($x < ($xj - $xi) * ($y - $yi) / ($yj - $yi) + $xi);
       if ($intersect) $inside = !$inside;
   }

   return $inside;;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM