Pregunta

I'm trying to generate some geolocations within the borders of an area right now I'm trying to generate geo locations within these boundaries

$bounds = array(
    array(55.4024447, 13.1656691),
    array(56.1575776, 15.8350261),
    array(68.0410163, 17.4600359),
    array(58.9380148, 11.3501468),
    array(67.6820048, 16.1964251)
);

and right now my checking for this is as follows

if (
    ($bounds[0][0] < $lat && $bounds[0][1] < $lng) &&
    ($bounds[1][0] < $lat && $bounds[1][1] > $lng) &&
    ($bounds[2][0] > $lat && $bounds[2][1] > $lng) &&
    ($bounds[3][0] > $lat && $bounds[3][1] < $lng) &&
    ($bounds[4][0] > $lat && $bounds[4][1] > $lng)
) {

however this only gets locations within a square in the middle not in the entire area do you have any idea of how to check within the full area instead?

¿Fue útil?

Solución

The following PHP code uses point in polygon algorithm described at the wikipedia.

In Fig 1 point is in polygon and the line drawn from this crosses the perimeter of the polygon an odd number of times(3). In Fig 2 the line crosses the perimeter an even number of times(4) and the point is outside. enter image description here

<?php

function pointInPolygon($polySides,$polyX,$polyY,$x,$y) {
  $j = $polySides-1 ;
  $oddNodes = 0;
  for ($i=0; $i<$polySides; $i++) {
    if ($polyY[$i]<$y && $polyY[$j]>=$y  ||  $polyY[$j]<$y && $polyY[$i]>=$y) {
        if ($polyX[$i]+($y-$polyY[$i])/($polyY[$j]-$polyY[$i])*($polyX[$j]-$polyX[$i])<$x)  {
            $oddNodes=!$oddNodes; 
        }
    }
   $j=$i; }

  return $oddNodes;
}
$polySides = 3;
$x =60;
$y =15;
$polyX = array(55.4024447,56.1575776,68.0410163,67.6820048,58.9380148,55.4024447);// First point repeated to close polygon coordinates 
$polyY = array(13.1656691,15.8350261,17.4600359,16.1964251,11.3501468,13.1656691); 
if (pointInPolygon($polySides,$polyX,$polyY,$x,$y)){
    echo "Point In Polygon";
    }else{
    echo "Point Not In Polygon";
    }
?>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top