1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
// fonctions
class pointLocation {
var $pointOnVertex = true; // Vérifier si le point est exactement sur un sommet ?
function pointLocation() {
}
function pointInPolygon($point, $polygon, $pointOnVertex = true) {
$this->pointOnVertex = $pointOnVertex;
// Transformer chaque couple de coordonnées en un tableau de 2 valeurs (x et y)
$point = $this->pointStringToCoordinates($point);
$vertices = array();
foreach ($polygon as $vertex) {
$vertices[] = $this->pointStringToCoordinates($vertex);
}
// Vérfier si le point est exactement sur un sommet
if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
return "vertex";
}
// Vérifier si le point est dans le polygone ou sur le bord
$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'])) {
// Vérifier si le point est sur un bord 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']) {
// Vérifier si le point est sur un bord (autre qu'horizontal)
return "boundary";
}
if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
$intersections++;
}
}
}
// Si le nombre de bords par lesquels on passe est impair, le point est dans le polygone.
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]);
}
}
// mise en forme du polygone pour Ambérieu
// les coordonnées du polygone sont stockées dans la base MySQL sous un format que je ne peux pas changer, il est utilisé dans de nombreuses autres pages
// d'où les 3 lignes ci-dessous qui reformatent les coordonnées au format correspondant à l'attente de la fonction
$req_kml = mysql_query("SELECT kml from communes WHERE insee='01004'",$conn);
$kml_commune=str_replace(" ",";",mysql_result($req_kml,0,"kml"));
$kml_commune=str_replace(","," ",$kml_commune);
$kml_commune='"'.str_replace(';','","',$kml_commune).'"';
//echo "polygone Amberieu formaté : ".$kml_commune."<br>";
// appel de la fonction
$pointLocation = new pointLocation();
$points = array("6.7 45.4","6.56 45.39","6.77 45.48","6.74 45.47","5.34 45.96");
$polygon=explode(",",$kml_commune);
foreach($points as $key => $point) {
echo "point " . ($key+1) . " ($point): " . $pointLocation->pointInPolygon($point, $polygon) . "<br>";
} |