forked from FreifunkFranken/VPNkeyXchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointLocation.class.php
78 lines (66 loc) · 2.4 KB
/
pointLocation.class.php
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
<?php
class pointLocation {
// Original version: https://gist.github.com/jeremejazz/5219848
// Modified by Adrian Schmutzler, 2018.
function excludePolygon($point, $minlon, $maxlon, $minlat, $maxlat) {
// exclude polygon if LAT/LNG of point is smaller than minimum lat/lng of all vertices
// or bigger than maximum ...
// returning TRUE means exclusion, so polygon should NOT be used
return ($point[0] < $minlon or $point[0] > $maxlon or $point[1] < $minlat or $point[1] > $maxlat);
}
function pointInPolygon($point, $polygon, $pointOnVertex = true) {
// Support both string version "lng lat" and array(lng,lat)
if(!is_array($point)) {
$point = $this->pointStringToCoordinates($point);
}
$vertices = array();
foreach ($polygon as $vertex) {
if(is_array($vertex)) {
$vertices[] = $vertex;
} else {
$vertices[] = $this->pointStringToCoordinates($vertex);
}
}
// Check if the point sits exactly on a vertex
if ($pointOnVertex and $this->pointOnVertex($point, $vertices)) {
return false;
}
// Check if the point is inside the polygon or on the boundary
$intersections = 0;
for ($i=1; $i < count($vertices); $i++) {
$vertex1 = $vertices[$i-1];
$vertex2 = $vertices[$i];
if ($vertex1[1] == $vertex2[1] and $vertex1[1] == $point[1]
and $point[0] > min($vertex1[0], $vertex2[0]) and $point[0] < max($vertex1[0], $vertex2[0]))
{ // Check if point is on an horizontal polygon boundary
return false;
}
if ($point[1] > min($vertex1[1], $vertex2[1]) and $point[1] <= max($vertex1[1], $vertex2[1])
and $point[0] <= max($vertex1[0], $vertex2[0]) and $vertex1[1] != $vertex2[1])
{
$xinters = ($point[1] - $vertex1[1]) * ($vertex2[0] - $vertex1[0]) / ($vertex2[1] - $vertex1[1]) + $vertex1[0];
if ($xinters == $point[0]) { // Check if point is on the polygon boundary (other than horizontal)
return false;
}
if ($vertex1[0] == $vertex2[0] || $point[0] <= $xinters) {
$intersections++;
}
}
}
// If the number of edges we passed through is odd, then it's in the polygon.
return ($intersections % 2 != 0);
}
function pointOnVertex($point, $vertices) {
foreach($vertices as $vertex) {
if ($point == $vertex) { // works for arrays
return true;
}
}
return false;
}
function pointStringToCoordinates($pointString) {
$coordinates = explode(" ", $pointString);
return array($coordinates[0],$coordinates[1]);
}
}
?>