Reputation: 437
I have array (polygon)
[[-24.837452, -65.452484],[-24.837433, -65.450645],[-24.828865000000004, -65.449997],[-24.828709, -65.451607],[-24.837452, -65.452484]]
and point -24.8330000, -65.452484
how to detect if the point is inside the polygon?
If I use the array of examples works. if you use real coordinates it does not work...
<?php
class pointLocation {
var $pointOnVertex = true; // Checar si el punto se encuentra exactamente en uno de los vértices?
function pointLocation() {
}
function pointInPolygon($point, $polygon, $pointOnVertex = true) {
$this->pointOnVertex = $pointOnVertex;
// Transformar la cadena de coordenadas en matrices con valores "x" e "y"
$point = $this->pointStringToCoordinates($point);
$vertices = array();
foreach ($polygon as $vertex) {
$vertices[] = $this->pointStringToCoordinates($vertex);
}
// Checar si el punto se encuentra exactamente en un vértice
if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
return "vertex";
}
// Checar si el punto está adentro del poligono o en el borde
$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'])) { // Checar si el punto está en un segmento horizontal
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']) { // Checar si el punto está en un segmento (otro que horizontal)
return "boundary";
}
if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
$intersections++;
}
}
}
// Si el número de intersecciones es impar, el punto está dentro del poligono.
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]);
}
}
//original examples is OK
//$points = array("50 70","70 40","-20 30","100 10","-10 -10","40 -20","110 -20");
//$polygon = array("-50 30","50 70","100 50","80 10","110 -10","110 -30","-20 -50","-30 -40","10 -10","-10 10","-30 -20","-50 30");
$json="[[-24.837452, -65.452484],[-24.837433, -65.450645],[-24.828865000000004, -65.449997],[-24.828709, -65.451607],[-24.837452, -65.452484]]";
$resultado = str_replace(",", "", $json);
$resultado = str_replace("[[", "", $resultado);
$resultado = str_replace("]]", "", $resultado);
$polygon=explode("][", $resultado);
$points=array("-24.8330000 -65.452484");
//print_r ($resultado);
$pointLocation = new pointLocation();
// Las últimas coordenadas tienen que ser las mismas que las primeras, para "cerrar el círculo"
foreach($points as $key => $point) {
echo "point " . ($key+1) . " ($point): " . $pointLocation->pointInPolygon($point, $polygon) . "<br>";
}
?>
These data are real and the point is within the polygon
[[-24.837452, -65.452484],[-24.837433, -65.450645],[-24.828865000000004, -65.449997],[-24.828709, -65.451607],[-24.837452, -65.452484]]
and point -24.8330000, -65.452484
Upvotes: 4
Views: 6963
Reputation: 9554
Gave Lavanya E.'s answer a couple of tweaks, because it wasn't running for me.
Since my application requires checking multiple points against multiple polygons (fence areas), I also adapted JSON to work with nested arrays.
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
echo "start decoding JSON</br>";
$pointArray=json_decode('[{"lat": 15.381533561369851, "lng": 73.90972848645542}, {"lat": 12.381533561369851, "lng": 73.90972848645542}]', true);
$fenceAreaArray=json_decode('[{"0":[{"lat":15.20184549675828,"lng":73.97872613764991},{"lat":15.10939000042012,"lng":73.93821405268898},{"lat":15.035131321967592,"lng":74.00619195796241},{"lat":14.991027988389707,"lng":74.05288385249366},{"lat":14.97268353612274,"lng":74.10815881587257},{"lat":15.08885107597101,"lng":74.17785333491554},{"lat":15.179658827735558,"lng":74.16652368403663},{"lat":15.245254746972826,"lng":74.18746637202491},{"lat":15.31261799705796,"lng":74.1534774193882},{"lat":15.35260926883264,"lng":74.06195113314038},{"lat":15.396694714469158,"lng":73.96676107157123},{"lat":15.480490697370463,"lng":73.91443209172701},{"lat":15.485848509162427,"lng":73.84096243101556},{"lat":15.58045246934704,"lng":73.83169271666009},{"lat":15.596325797024097,"lng":73.75410177427727},{"lat":15.406706231349043,"lng":73.79266521010072}], "1":[{"lat":12.20184549675828,"lng":73.97872613764991},{"lat":12.10939000042012,"lng":73.93821405268898},{"lat":12.035131321967592,"lng":74.00619195796241},{"lat":11.991027988389707,"lng":74.05288385249366},{"lat":11.97268353612274,"lng":74.10815881587257},{"lat":12.08885107597101,"lng":74.17785333491554},{"lat":12.179658827735558,"lng":74.16652368403663},{"lat":12.245254746972826,"lng":74.18746637202491},{"lat":12.31261799705796,"lng":74.1534774193882},{"lat":12.35260926883264,"lng":74.06195113314038},{"lat":12.396694714469158,"lng":73.96676107157123},{"lat":12.480490697370463,"lng":73.91443209172701},{"lat":12.485848509162427,"lng":73.84096243101556},{"lat":12.58045246934704,"lng":73.83169271666009},{"lat":12.596325797024097,"lng":73.75410177427727},{"lat":12.406706231349043,"lng":73.79266521010072}]}]', true);
echo "pointArray has [".count($pointArray)."] elements</br>";
echo "fenceAreaArray[0] has [".count($fenceAreaArray[0])."] elements</br>";
// loop thru pointArray
foreach ($pointArray as $k => $pointOut) {
// get the first (and only element) from fenceAreaArray and loop
foreach ($fenceAreaArray[0] as $l => $fenceAreaOut) {
$resultInside = inside($pointOut,$fenceAreaOut);
echo "</br>This is the result for point[$k] and fenceArea[$l]: [$resultInside]";
}
}
function inside($point, $fenceArea) {
//echo "hello from the INSIDE </br>";
$x = $point['lat'];
$y = $point['lng'];
$inside = false;
for ($i = 0, $j = count($fenceArea) - 1; $i < count($fenceArea); $j = $i++) {
$xi = $fenceArea[$i]['lat']; $yi = $fenceArea[$i]['lng'];
$xj = $fenceArea[$j]['lat']; $yj = $fenceArea[$j]['lng'];
$intersect = (($yi > $y) != ($yj > $y))
&& ($x < ($xj - $xi) * ($y - $yi) / ($yj - $yi) + $xi);
if ($intersect) $inside = !$inside;
}
return $inside;
}
?>
Upvotes: 0
Reputation: 216
function inside($point, $fenceArea) {
$x = $point['lat']; $y = $point['lng'];
$inside = false;
for ($i = 0, $j = count($fenceArea) - 1; $i < count($fenceArea); $j = $i++) {
$xi = $fenceArea[$i]['lat']; $yi = $fenceArea[$i]['lng'];
$xj = $fenceArea[$j]['lat']; $yj = $fenceArea[$j]['lng'];
$intersect = (($yi > $y) != ($yj > $y))
&& ($x < ($xj - $xi) * ($y - $yi) / ($yj - $yi) + $xi);
if ($intersect) $inside = !$inside;
}
return $inside;
}
echo inside($point,$fenceArea);
$fenceArea => array of multiple lat lng $fenceArea=json_decode('[{"lat":15.20184549675828,"lng":73.97872613764991},{"lat":15.10939000042012,"lng":73.93821405268898},{"lat":15.035131321967592,"lng":74.00619195796241},{"lat":14.991027988389707,"lng":74.05288385249366},{"lat":14.97268353612274,"lng":74.10815881587257},{"lat":15.08885107597101,"lng":74.17785333491554},{"lat":15.179658827735558,"lng":74.16652368403663},{"lat":15.245254746972826,"lng":74.18746637202491},{"lat":15.31261799705796,"lng":74.1534774193882},{"lat":15.35260926883264,"lng":74.06195113314038},{"lat":15.396694714469158,"lng":73.96676107157123},{"lat":15.480490697370463,"lng":73.91443209172701},{"lat":15.485848509162427,"lng":73.84096243101556},{"lat":15.58045246934704,"lng":73.83169271666009},{"lat":15.596325797024097,"lng":73.75410177427727},{"lat":15.406706231349043,"lng":73.79266521010072}]'); $point => Searching particular lat lng $point=["lat": 15.381533561369851, "lng": 73.90972848645542];
If the point is inside fenceArea return true else it will return false.
Upvotes: 4
Reputation: 129
Here is a working implementation of your problem with the Google Maps API (since you tagged it): jsFiddle
google.maps.geometry.poly.containsLocation(testPointCoords, polygonObject)
returns true if the point is in the polygon or false if the point is outside the polygon.
More information about the geometry library is here: Google Maps Geometry Library
And the containsLocation() function: contains Location()
Upvotes: 1
Reputation: 437
Had already solved more spaces at coordinates
<?php
/*
Descripción: El algoritmo del punto en un polígono permite comprobar mediante
programación si un punto está dentro de un polígono o fuera de ello.
Autor: Michaël Niessen (2009)
Sito web: AssemblySys.com
Si este código le es útil, puede mostrar su
agradecimiento a Michaël ofreciéndole un café ;)
PayPal: [email protected]
Mientras estos comentarios (incluyendo nombre y detalles del autor) estén
incluidos y SIN ALTERAR, este código está distribuido bajo la GNU Licencia
Pública General versión 3: http://www.gnu.org/licenses/gpl.html
*/
class pointLocation {
var $pointOnVertex = true; // Checar si el punto se encuentra exactamente en uno de los vértices?
function pointLocation() {
}
function pointInPolygon($point, $polygon, $pointOnVertex = true) {
$this->pointOnVertex = $pointOnVertex;
// Transformar la cadena de coordenadas en matrices con valores "x" e "y"
$point = $this->pointStringToCoordinates($point);
$vertices = array();
foreach ($polygon as $vertex) {
$vertices[] = $this->pointStringToCoordinates($vertex);
}
// Checar si el punto se encuentra exactamente en un vértice
if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
return "vertex";
}
// Checar si el punto está adentro del poligono o en el borde
$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'])) { // Checar si el punto está en un segmento horizontal
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']) { // Checar si el punto está en un segmento (otro que horizontal)
return "boundary";
}
if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
$intersections++;
}
}
}
// Si el número de intersecciones es impar, el punto está dentro del poligono.
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]);
}
}
$json="[[-24.755444000000004, -65.466476],[-24.755365, -65.466652],[-24.758835, -65.466797],[-24.760336, -65.46637],[-24.761738, -65.465683],[-24.762362, -65.46553000000002],[-24.76733, -65.466927],[-24.767447, -65.464035],[-24.766182, -65.464035],[-24.765907000000002, -65.463577],[-24.765070000000005, -65.4636],[-24.76507, -65.464249],[-24.76392, -65.464119],[-24.763219999999997, -65.464035],[-24.762907, -65.464249],[-24.762402, -65.464378],[-24.761951, -65.464157],[-24.761738, -65.463837],[-24.761465000000005, -65.463455],[-24.757647, -65.46315],[-24.755444000000004, -65.466476]]";
//aca estaba el error (here the error)
$resultado = str_replace(" ", "", $json);
$resultado = str_replace(",", " ", $resultado);
$resultado = str_replace("[[", "", $resultado);
$resultado = str_replace("]]", "", $resultado);
$polygon=explode("] [", $resultado);
$points=array("-24.762426 -65.464812");
print_r ($polygon);
$pointLocation = new pointLocation();
//$points = array("50 70","70 40","-20 30","100 10","-10 -10","40 -20","110 -20");
//$polygon = array("-50 30","50 70","100 50","80 10","110 -10","110 -30","-20 -50","-30 -40","10 -10","-10 10","-30 -20","-50 30");
// print_r($polygon);
// Las últimas coordenadas tienen que ser las mismas que las primeras, para "cerrar el círculo"
foreach($points as $key => $point) {
echo "point " . ($key+1) . " ($point): " . $pointLocation->pointInPolygon($point, $polygon) . "<br>";
}
?>
Upvotes: 0
Reputation: 479
The other answer here is probably your best bet, but if it doesn't work for whatever reason, it looks like you'll want to try a version of the Ray-casting algorithm. A few other users have tried that, and you can look here for reference. Good luck!
Upvotes: 1
Reputation: 1370
If you are just wanting to check if the polygon contains your point using Google maps you can try : Google Maps API
If you want to calculate it in PHP then your question is a duplicate of this: Check if Google Map Point is in polygon from PHP
Upvotes: 1