@@ -13,33 +13,33 @@ discard block |
||
13 | 13 | * @return Geometry |
14 | 14 | */ |
15 | 15 | public function read($wkb, $is_hex_string = FALSE) { |
16 | - if ($is_hex_string) { |
|
17 | - $wkb = pack('H*',$wkb); |
|
18 | - } |
|
16 | + if ($is_hex_string) { |
|
17 | + $wkb = pack('H*',$wkb); |
|
18 | + } |
|
19 | 19 | |
20 | - // Open the wkb up in memory so we can examine the SRID |
|
21 | - $mem = fopen('php://memory', 'r+'); |
|
22 | - fwrite($mem, $wkb); |
|
23 | - fseek($mem, 0); |
|
24 | - $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5)); |
|
25 | - if ($base_info['s']) { |
|
26 | - $srid = current(unpack("Lsrid", fread($mem, 4))); |
|
27 | - } |
|
28 | - else { |
|
29 | - $srid = NULL; |
|
30 | - } |
|
31 | - fclose($mem); |
|
20 | + // Open the wkb up in memory so we can examine the SRID |
|
21 | + $mem = fopen('php://memory', 'r+'); |
|
22 | + fwrite($mem, $wkb); |
|
23 | + fseek($mem, 0); |
|
24 | + $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5)); |
|
25 | + if ($base_info['s']) { |
|
26 | + $srid = current(unpack("Lsrid", fread($mem, 4))); |
|
27 | + } |
|
28 | + else { |
|
29 | + $srid = NULL; |
|
30 | + } |
|
31 | + fclose($mem); |
|
32 | 32 | |
33 | - // Run the wkb through the normal WKB reader to get the geometry |
|
34 | - $wkb_reader = new WKB(); |
|
35 | - $geom = $wkb_reader->read($wkb); |
|
33 | + // Run the wkb through the normal WKB reader to get the geometry |
|
34 | + $wkb_reader = new WKB(); |
|
35 | + $geom = $wkb_reader->read($wkb); |
|
36 | 36 | |
37 | - // If there is an SRID, add it to the geometry |
|
38 | - if ($srid) { |
|
39 | - $geom->setSRID($srid); |
|
40 | - } |
|
37 | + // If there is an SRID, add it to the geometry |
|
38 | + if ($srid) { |
|
39 | + $geom->setSRID($srid); |
|
40 | + } |
|
41 | 41 | |
42 | - return $geom; |
|
42 | + return $geom; |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
@@ -50,47 +50,47 @@ discard block |
||
50 | 50 | * @return string The Extended-WKB binary string representation of the input geometries |
51 | 51 | */ |
52 | 52 | public function write(Geometry $geometry, $write_as_hex = FALSE) { |
53 | - // We always write into NDR (little endian) |
|
54 | - $wkb = pack('c',1); |
|
53 | + // We always write into NDR (little endian) |
|
54 | + $wkb = pack('c',1); |
|
55 | 55 | |
56 | - switch ($geometry->getGeomType()) { |
|
57 | - case 'Point'; |
|
58 | - $wkb .= pack('L',1); |
|
59 | - $wkb .= $this->writePoint($geometry); |
|
60 | - break; |
|
61 | - case 'LineString'; |
|
62 | - $wkb .= pack('L',2); |
|
63 | - $wkb .= $this->writeLineString($geometry); |
|
64 | - break; |
|
65 | - case 'Polygon'; |
|
66 | - $wkb .= pack('L',3); |
|
67 | - $wkb .= $this->writePolygon($geometry); |
|
68 | - break; |
|
69 | - case 'MultiPoint'; |
|
70 | - $wkb .= pack('L',4); |
|
71 | - $wkb .= $this->writeMulti($geometry); |
|
72 | - break; |
|
73 | - case 'MultiLineString'; |
|
74 | - $wkb .= pack('L',5); |
|
75 | - $wkb .= $this->writeMulti($geometry); |
|
76 | - break; |
|
77 | - case 'MultiPolygon'; |
|
78 | - $wkb .= pack('L',6); |
|
79 | - $wkb .= $this->writeMulti($geometry); |
|
80 | - break; |
|
81 | - case 'GeometryCollection'; |
|
82 | - $wkb .= pack('L',7); |
|
83 | - $wkb .= $this->writeMulti($geometry); |
|
84 | - break; |
|
85 | - } |
|
56 | + switch ($geometry->getGeomType()) { |
|
57 | + case 'Point'; |
|
58 | + $wkb .= pack('L',1); |
|
59 | + $wkb .= $this->writePoint($geometry); |
|
60 | + break; |
|
61 | + case 'LineString'; |
|
62 | + $wkb .= pack('L',2); |
|
63 | + $wkb .= $this->writeLineString($geometry); |
|
64 | + break; |
|
65 | + case 'Polygon'; |
|
66 | + $wkb .= pack('L',3); |
|
67 | + $wkb .= $this->writePolygon($geometry); |
|
68 | + break; |
|
69 | + case 'MultiPoint'; |
|
70 | + $wkb .= pack('L',4); |
|
71 | + $wkb .= $this->writeMulti($geometry); |
|
72 | + break; |
|
73 | + case 'MultiLineString'; |
|
74 | + $wkb .= pack('L',5); |
|
75 | + $wkb .= $this->writeMulti($geometry); |
|
76 | + break; |
|
77 | + case 'MultiPolygon'; |
|
78 | + $wkb .= pack('L',6); |
|
79 | + $wkb .= $this->writeMulti($geometry); |
|
80 | + break; |
|
81 | + case 'GeometryCollection'; |
|
82 | + $wkb .= pack('L',7); |
|
83 | + $wkb .= $this->writeMulti($geometry); |
|
84 | + break; |
|
85 | + } |
|
86 | 86 | |
87 | - if ($write_as_hex) { |
|
88 | - $unpacked = unpack('H*',$wkb); |
|
89 | - return $unpacked[1]; |
|
90 | - } |
|
91 | - else { |
|
92 | - return $wkb; |
|
93 | - } |
|
87 | + if ($write_as_hex) { |
|
88 | + $unpacked = unpack('H*',$wkb); |
|
89 | + return $unpacked[1]; |
|
90 | + } |
|
91 | + else { |
|
92 | + return $wkb; |
|
93 | + } |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | } |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | */ |
15 | 15 | public function read($wkb, $is_hex_string = FALSE) { |
16 | 16 | if ($is_hex_string) { |
17 | - $wkb = pack('H*',$wkb); |
|
17 | + $wkb = pack('H*', $wkb); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | // Open the wkb up in memory so we can examine the SRID |
@@ -51,41 +51,41 @@ discard block |
||
51 | 51 | */ |
52 | 52 | public function write(Geometry $geometry, $write_as_hex = FALSE) { |
53 | 53 | // We always write into NDR (little endian) |
54 | - $wkb = pack('c',1); |
|
54 | + $wkb = pack('c', 1); |
|
55 | 55 | |
56 | 56 | switch ($geometry->getGeomType()) { |
57 | 57 | case 'Point'; |
58 | - $wkb .= pack('L',1); |
|
58 | + $wkb .= pack('L', 1); |
|
59 | 59 | $wkb .= $this->writePoint($geometry); |
60 | 60 | break; |
61 | 61 | case 'LineString'; |
62 | - $wkb .= pack('L',2); |
|
62 | + $wkb .= pack('L', 2); |
|
63 | 63 | $wkb .= $this->writeLineString($geometry); |
64 | 64 | break; |
65 | 65 | case 'Polygon'; |
66 | - $wkb .= pack('L',3); |
|
66 | + $wkb .= pack('L', 3); |
|
67 | 67 | $wkb .= $this->writePolygon($geometry); |
68 | 68 | break; |
69 | 69 | case 'MultiPoint'; |
70 | - $wkb .= pack('L',4); |
|
70 | + $wkb .= pack('L', 4); |
|
71 | 71 | $wkb .= $this->writeMulti($geometry); |
72 | 72 | break; |
73 | 73 | case 'MultiLineString'; |
74 | - $wkb .= pack('L',5); |
|
74 | + $wkb .= pack('L', 5); |
|
75 | 75 | $wkb .= $this->writeMulti($geometry); |
76 | 76 | break; |
77 | 77 | case 'MultiPolygon'; |
78 | - $wkb .= pack('L',6); |
|
78 | + $wkb .= pack('L', 6); |
|
79 | 79 | $wkb .= $this->writeMulti($geometry); |
80 | 80 | break; |
81 | 81 | case 'GeometryCollection'; |
82 | - $wkb .= pack('L',7); |
|
82 | + $wkb .= pack('L', 7); |
|
83 | 83 | $wkb .= $this->writeMulti($geometry); |
84 | 84 | break; |
85 | 85 | } |
86 | 86 | |
87 | 87 | if ($write_as_hex) { |
88 | - $unpacked = unpack('H*',$wkb); |
|
88 | + $unpacked = unpack('H*', $wkb); |
|
89 | 89 | return $unpacked[1]; |
90 | 90 | } |
91 | 91 | else { |
@@ -24,8 +24,7 @@ discard block |
||
24 | 24 | $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5)); |
25 | 25 | if ($base_info['s']) { |
26 | 26 | $srid = current(unpack("Lsrid", fread($mem, 4))); |
27 | - } |
|
28 | - else { |
|
27 | + } else { |
|
29 | 28 | $srid = NULL; |
30 | 29 | } |
31 | 30 | fclose($mem); |
@@ -87,8 +86,7 @@ discard block |
||
87 | 86 | if ($write_as_hex) { |
88 | 87 | $unpacked = unpack('H*',$wkb); |
89 | 88 | return $unpacked[1]; |
90 | - } |
|
91 | - else { |
|
89 | + } else { |
|
92 | 90 | return $wkb; |
93 | 91 | } |
94 | 92 | } |
@@ -16,21 +16,21 @@ discard block |
||
16 | 16 | * @see GeoAdapter::read() |
17 | 17 | */ |
18 | 18 | public function read($hash, $as_grid = FALSE) { |
19 | - $ll = $this->decode($hash); |
|
20 | - if (!$as_grid) { |
|
21 | - return new Point($ll['medlon'], $ll['medlat']); |
|
22 | - } |
|
23 | - else { |
|
24 | - return new Polygon(array( |
|
25 | - new LineString(array( |
|
26 | - new Point($ll['minlon'], $ll['maxlat']), |
|
27 | - new Point($ll['maxlon'], $ll['maxlat']), |
|
28 | - new Point($ll['maxlon'], $ll['minlat']), |
|
29 | - new Point($ll['minlon'], $ll['minlat']), |
|
30 | - new Point($ll['minlon'], $ll['maxlat']), |
|
31 | - )) |
|
32 | - )); |
|
33 | - } |
|
19 | + $ll = $this->decode($hash); |
|
20 | + if (!$as_grid) { |
|
21 | + return new Point($ll['medlon'], $ll['medlat']); |
|
22 | + } |
|
23 | + else { |
|
24 | + return new Polygon(array( |
|
25 | + new LineString(array( |
|
26 | + new Point($ll['minlon'], $ll['maxlat']), |
|
27 | + new Point($ll['maxlon'], $ll['maxlat']), |
|
28 | + new Point($ll['maxlon'], $ll['minlat']), |
|
29 | + new Point($ll['minlon'], $ll['minlat']), |
|
30 | + new Point($ll['minlon'], $ll['maxlat']), |
|
31 | + )) |
|
32 | + )); |
|
33 | + } |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | /** |
@@ -40,32 +40,32 @@ discard block |
||
40 | 40 | * @see GeoAdapter::write() |
41 | 41 | */ |
42 | 42 | public function write(Geometry $geometry, $precision = NULL){ |
43 | - if ($geometry->isEmpty()) return ''; |
|
43 | + if ($geometry->isEmpty()) return ''; |
|
44 | 44 | |
45 | - if($geometry->geometryType() === 'Point'){ |
|
46 | - return $this->encodePoint($geometry, $precision); |
|
47 | - } |
|
48 | - else { |
|
49 | - // The geohash is the hash grid ID that fits the envelope |
|
50 | - $envelope = $geometry->envelope(); |
|
51 | - $geohashes = array(); |
|
52 | - $geohash = ''; |
|
53 | - foreach ($envelope->getPoints() as $point) { |
|
54 | - $geohashes[] = $this->encodePoint($point, 0.0000001); |
|
55 | - } |
|
56 | - $i = 0; |
|
57 | - while ($i < strlen($geohashes[0])) { |
|
58 | - $char = $geohashes[0][$i]; |
|
59 | - foreach ($geohashes as $hash) { |
|
60 | - if ($hash[$i] != $char) { |
|
61 | - return $geohash; |
|
62 | - } |
|
63 | - } |
|
64 | - $geohash .= $char; |
|
65 | - $i++; |
|
66 | - } |
|
67 | - return $geohash; |
|
68 | - } |
|
45 | + if($geometry->geometryType() === 'Point'){ |
|
46 | + return $this->encodePoint($geometry, $precision); |
|
47 | + } |
|
48 | + else { |
|
49 | + // The geohash is the hash grid ID that fits the envelope |
|
50 | + $envelope = $geometry->envelope(); |
|
51 | + $geohashes = array(); |
|
52 | + $geohash = ''; |
|
53 | + foreach ($envelope->getPoints() as $point) { |
|
54 | + $geohashes[] = $this->encodePoint($point, 0.0000001); |
|
55 | + } |
|
56 | + $i = 0; |
|
57 | + while ($i < strlen($geohashes[0])) { |
|
58 | + $char = $geohashes[0][$i]; |
|
59 | + foreach ($geohashes as $hash) { |
|
60 | + if ($hash[$i] != $char) { |
|
61 | + return $geohash; |
|
62 | + } |
|
63 | + } |
|
64 | + $geohash .= $char; |
|
65 | + $i++; |
|
66 | + } |
|
67 | + return $geohash; |
|
68 | + } |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -75,51 +75,51 @@ discard block |
||
75 | 75 | * @see https://github.com/asonge/php-geohash/issues/1 |
76 | 76 | */ |
77 | 77 | private function encodePoint($point, $precision = NULL){ |
78 | - if ($precision === NULL) { |
|
79 | - $lap = strlen($point->y())-strpos($point->y(),"."); |
|
80 | - $lop = strlen($point->x())-strpos($point->x(),"."); |
|
81 | - $precision = pow(10,-max($lap-1,$lop-1,0))/2; |
|
82 | - } |
|
78 | + if ($precision === NULL) { |
|
79 | + $lap = strlen($point->y())-strpos($point->y(),"."); |
|
80 | + $lop = strlen($point->x())-strpos($point->x(),"."); |
|
81 | + $precision = pow(10,-max($lap-1,$lop-1,0))/2; |
|
82 | + } |
|
83 | 83 | |
84 | - $minlat = -90; |
|
85 | - $maxlat = 90; |
|
86 | - $minlon = -180; |
|
87 | - $maxlon = 180; |
|
88 | - $latE = 90; |
|
89 | - $lonE = 180; |
|
90 | - $i = 0; |
|
91 | - $error = 180; |
|
92 | - $hash=''; |
|
93 | - while($error>=$precision) { |
|
94 | - $chr = 0; |
|
95 | - for($b=4;$b>=0;--$b) { |
|
96 | - if((1&$b) == (1&$i)) { |
|
97 | - // even char, even bit OR odd char, odd bit...a lon |
|
98 | - $next = ($minlon+$maxlon)/2; |
|
99 | - if($point->x()>$next) { |
|
100 | - $chr |= pow(2,$b); |
|
101 | - $minlon = $next; |
|
102 | - } else { |
|
103 | - $maxlon = $next; |
|
104 | - } |
|
105 | - $lonE /= 2; |
|
106 | - } else { |
|
107 | - // odd char, even bit OR even char, odd bit...a lat |
|
108 | - $next = ($minlat+$maxlat)/2; |
|
109 | - if($point->y()>$next) { |
|
110 | - $chr |= pow(2,$b); |
|
111 | - $minlat = $next; |
|
112 | - } else { |
|
113 | - $maxlat = $next; |
|
114 | - } |
|
115 | - $latE /= 2; |
|
116 | - } |
|
117 | - } |
|
118 | - $hash .= $this->table[$chr]; |
|
119 | - $i++; |
|
120 | - $error = min($latE,$lonE); |
|
121 | - } |
|
122 | - return $hash; |
|
84 | + $minlat = -90; |
|
85 | + $maxlat = 90; |
|
86 | + $minlon = -180; |
|
87 | + $maxlon = 180; |
|
88 | + $latE = 90; |
|
89 | + $lonE = 180; |
|
90 | + $i = 0; |
|
91 | + $error = 180; |
|
92 | + $hash=''; |
|
93 | + while($error>=$precision) { |
|
94 | + $chr = 0; |
|
95 | + for($b=4;$b>=0;--$b) { |
|
96 | + if((1&$b) == (1&$i)) { |
|
97 | + // even char, even bit OR odd char, odd bit...a lon |
|
98 | + $next = ($minlon+$maxlon)/2; |
|
99 | + if($point->x()>$next) { |
|
100 | + $chr |= pow(2,$b); |
|
101 | + $minlon = $next; |
|
102 | + } else { |
|
103 | + $maxlon = $next; |
|
104 | + } |
|
105 | + $lonE /= 2; |
|
106 | + } else { |
|
107 | + // odd char, even bit OR even char, odd bit...a lat |
|
108 | + $next = ($minlat+$maxlat)/2; |
|
109 | + if($point->y()>$next) { |
|
110 | + $chr |= pow(2,$b); |
|
111 | + $minlat = $next; |
|
112 | + } else { |
|
113 | + $maxlat = $next; |
|
114 | + } |
|
115 | + $latE /= 2; |
|
116 | + } |
|
117 | + } |
|
118 | + $hash .= $this->table[$chr]; |
|
119 | + $i++; |
|
120 | + $error = min($latE,$lonE); |
|
121 | + } |
|
122 | + return $hash; |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
@@ -128,39 +128,39 @@ discard block |
||
128 | 128 | * @see https://github.com/asonge/php-geohash/issues/1 |
129 | 129 | */ |
130 | 130 | private function decode($hash){ |
131 | - $ll = array(); |
|
132 | - $minlat = -90; |
|
133 | - $maxlat = 90; |
|
134 | - $minlon = -180; |
|
135 | - $maxlon = 180; |
|
136 | - $latE = 90; |
|
137 | - $lonE = 180; |
|
138 | - for($i=0,$c=strlen($hash);$i<$c;$i++) { |
|
139 | - $v = strpos($this->table,$hash[$i]); |
|
140 | - if(1&$i) { |
|
141 | - if(16&$v)$minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
142 | - if(8&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
143 | - if(4&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
144 | - if(2&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
145 | - if(1&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
146 | - $latE /= 8; |
|
147 | - $lonE /= 4; |
|
148 | - } else { |
|
149 | - if(16&$v)$minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
150 | - if(8&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
151 | - if(4&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
152 | - if(2&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
153 | - if(1&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
154 | - $latE /= 4; |
|
155 | - $lonE /= 8; |
|
156 | - } |
|
157 | - } |
|
158 | - $ll['minlat'] = $minlat; |
|
159 | - $ll['minlon'] = $minlon; |
|
160 | - $ll['maxlat'] = $maxlat; |
|
161 | - $ll['maxlon'] = $maxlon; |
|
162 | - $ll['medlat'] = round(($minlat+$maxlat)/2, max(1, -round(log10($latE)))-1); |
|
163 | - $ll['medlon'] = round(($minlon+$maxlon)/2, max(1, -round(log10($lonE)))-1); |
|
164 | - return $ll; |
|
131 | + $ll = array(); |
|
132 | + $minlat = -90; |
|
133 | + $maxlat = 90; |
|
134 | + $minlon = -180; |
|
135 | + $maxlon = 180; |
|
136 | + $latE = 90; |
|
137 | + $lonE = 180; |
|
138 | + for($i=0,$c=strlen($hash);$i<$c;$i++) { |
|
139 | + $v = strpos($this->table,$hash[$i]); |
|
140 | + if(1&$i) { |
|
141 | + if(16&$v)$minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
142 | + if(8&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
143 | + if(4&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
144 | + if(2&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
145 | + if(1&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
146 | + $latE /= 8; |
|
147 | + $lonE /= 4; |
|
148 | + } else { |
|
149 | + if(16&$v)$minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
150 | + if(8&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
151 | + if(4&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
152 | + if(2&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
153 | + if(1&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
154 | + $latE /= 4; |
|
155 | + $lonE /= 8; |
|
156 | + } |
|
157 | + } |
|
158 | + $ll['minlat'] = $minlat; |
|
159 | + $ll['minlon'] = $minlon; |
|
160 | + $ll['maxlat'] = $maxlat; |
|
161 | + $ll['maxlon'] = $maxlon; |
|
162 | + $ll['medlat'] = round(($minlat+$maxlat)/2, max(1, -round(log10($latE)))-1); |
|
163 | + $ll['medlon'] = round(($minlon+$maxlon)/2, max(1, -round(log10($lonE)))-1); |
|
164 | + return $ll; |
|
165 | 165 | } |
166 | 166 | } |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | * @see http://en.wikipedia.org/wiki/Geohash |
7 | 7 | * |
8 | 8 | */ |
9 | -class GeoHash extends GeoAdapter{ |
|
9 | +class GeoHash extends GeoAdapter { |
|
10 | 10 | private $table = "0123456789bcdefghjkmnpqrstuvwxyz"; |
11 | 11 | |
12 | 12 | /** |
@@ -39,10 +39,10 @@ discard block |
||
39 | 39 | * @param Point $geometry |
40 | 40 | * @see GeoAdapter::write() |
41 | 41 | */ |
42 | - public function write(Geometry $geometry, $precision = NULL){ |
|
42 | + public function write(Geometry $geometry, $precision = NULL) { |
|
43 | 43 | if ($geometry->isEmpty()) return ''; |
44 | 44 | |
45 | - if($geometry->geometryType() === 'Point'){ |
|
45 | + if ($geometry->geometryType() === 'Point') { |
|
46 | 46 | return $this->encodePoint($geometry, $precision); |
47 | 47 | } |
48 | 48 | else { |
@@ -74,30 +74,30 @@ discard block |
||
74 | 74 | * @author algorithm based on code by Alexander Songe <[email protected]> |
75 | 75 | * @see https://github.com/asonge/php-geohash/issues/1 |
76 | 76 | */ |
77 | - private function encodePoint($point, $precision = NULL){ |
|
77 | + private function encodePoint($point, $precision = NULL) { |
|
78 | 78 | if ($precision === NULL) { |
79 | - $lap = strlen($point->y())-strpos($point->y(),"."); |
|
80 | - $lop = strlen($point->x())-strpos($point->x(),"."); |
|
81 | - $precision = pow(10,-max($lap-1,$lop-1,0))/2; |
|
79 | + $lap = strlen($point->y()) - strpos($point->y(), "."); |
|
80 | + $lop = strlen($point->x()) - strpos($point->x(), "."); |
|
81 | + $precision = pow(10, -max($lap - 1, $lop - 1, 0))/2; |
|
82 | 82 | } |
83 | 83 | |
84 | - $minlat = -90; |
|
85 | - $maxlat = 90; |
|
84 | + $minlat = -90; |
|
85 | + $maxlat = 90; |
|
86 | 86 | $minlon = -180; |
87 | - $maxlon = 180; |
|
88 | - $latE = 90; |
|
89 | - $lonE = 180; |
|
87 | + $maxlon = 180; |
|
88 | + $latE = 90; |
|
89 | + $lonE = 180; |
|
90 | 90 | $i = 0; |
91 | 91 | $error = 180; |
92 | - $hash=''; |
|
93 | - while($error>=$precision) { |
|
92 | + $hash = ''; |
|
93 | + while ($error >= $precision) { |
|
94 | 94 | $chr = 0; |
95 | - for($b=4;$b>=0;--$b) { |
|
96 | - if((1&$b) == (1&$i)) { |
|
95 | + for ($b = 4; $b >= 0; --$b) { |
|
96 | + if ((1&$b) == (1&$i)) { |
|
97 | 97 | // even char, even bit OR odd char, odd bit...a lon |
98 | - $next = ($minlon+$maxlon)/2; |
|
99 | - if($point->x()>$next) { |
|
100 | - $chr |= pow(2,$b); |
|
98 | + $next = ($minlon + $maxlon)/2; |
|
99 | + if ($point->x() > $next) { |
|
100 | + $chr |= pow(2, $b); |
|
101 | 101 | $minlon = $next; |
102 | 102 | } else { |
103 | 103 | $maxlon = $next; |
@@ -105,9 +105,9 @@ discard block |
||
105 | 105 | $lonE /= 2; |
106 | 106 | } else { |
107 | 107 | // odd char, even bit OR even char, odd bit...a lat |
108 | - $next = ($minlat+$maxlat)/2; |
|
109 | - if($point->y()>$next) { |
|
110 | - $chr |= pow(2,$b); |
|
108 | + $next = ($minlat + $maxlat)/2; |
|
109 | + if ($point->y() > $next) { |
|
110 | + $chr |= pow(2, $b); |
|
111 | 111 | $minlat = $next; |
112 | 112 | } else { |
113 | 113 | $maxlat = $next; |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | } |
118 | 118 | $hash .= $this->table[$chr]; |
119 | 119 | $i++; |
120 | - $error = min($latE,$lonE); |
|
120 | + $error = min($latE, $lonE); |
|
121 | 121 | } |
122 | 122 | return $hash; |
123 | 123 | } |
@@ -127,30 +127,30 @@ discard block |
||
127 | 127 | * @author algorithm based on code by Alexander Songe <[email protected]> |
128 | 128 | * @see https://github.com/asonge/php-geohash/issues/1 |
129 | 129 | */ |
130 | - private function decode($hash){ |
|
130 | + private function decode($hash) { |
|
131 | 131 | $ll = array(); |
132 | - $minlat = -90; |
|
133 | - $maxlat = 90; |
|
132 | + $minlat = -90; |
|
133 | + $maxlat = 90; |
|
134 | 134 | $minlon = -180; |
135 | - $maxlon = 180; |
|
136 | - $latE = 90; |
|
137 | - $lonE = 180; |
|
138 | - for($i=0,$c=strlen($hash);$i<$c;$i++) { |
|
139 | - $v = strpos($this->table,$hash[$i]); |
|
140 | - if(1&$i) { |
|
141 | - if(16&$v)$minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
142 | - if(8&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
143 | - if(4&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
144 | - if(2&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
145 | - if(1&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
135 | + $maxlon = 180; |
|
136 | + $latE = 90; |
|
137 | + $lonE = 180; |
|
138 | + for ($i = 0, $c = strlen($hash); $i < $c; $i++) { |
|
139 | + $v = strpos($this->table, $hash[$i]); |
|
140 | + if (1&$i) { |
|
141 | + if (16&$v)$minlat = ($minlat + $maxlat)/2; else $maxlat = ($minlat + $maxlat)/2; |
|
142 | + if (8&$v) $minlon = ($minlon + $maxlon)/2; else $maxlon = ($minlon + $maxlon)/2; |
|
143 | + if (4&$v) $minlat = ($minlat + $maxlat)/2; else $maxlat = ($minlat + $maxlat)/2; |
|
144 | + if (2&$v) $minlon = ($minlon + $maxlon)/2; else $maxlon = ($minlon + $maxlon)/2; |
|
145 | + if (1&$v) $minlat = ($minlat + $maxlat)/2; else $maxlat = ($minlat + $maxlat)/2; |
|
146 | 146 | $latE /= 8; |
147 | 147 | $lonE /= 4; |
148 | 148 | } else { |
149 | - if(16&$v)$minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
150 | - if(8&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
151 | - if(4&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
152 | - if(2&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
153 | - if(1&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
149 | + if (16&$v)$minlon = ($minlon + $maxlon)/2; else $maxlon = ($minlon + $maxlon)/2; |
|
150 | + if (8&$v) $minlat = ($minlat + $maxlat)/2; else $maxlat = ($minlat + $maxlat)/2; |
|
151 | + if (4&$v) $minlon = ($minlon + $maxlon)/2; else $maxlon = ($minlon + $maxlon)/2; |
|
152 | + if (2&$v) $minlat = ($minlat + $maxlat)/2; else $maxlat = ($minlat + $maxlat)/2; |
|
153 | + if (1&$v) $minlon = ($minlon + $maxlon)/2; else $maxlon = ($minlon + $maxlon)/2; |
|
154 | 154 | $latE /= 4; |
155 | 155 | $lonE /= 8; |
156 | 156 | } |
@@ -159,8 +159,8 @@ discard block |
||
159 | 159 | $ll['minlon'] = $minlon; |
160 | 160 | $ll['maxlat'] = $maxlat; |
161 | 161 | $ll['maxlon'] = $maxlon; |
162 | - $ll['medlat'] = round(($minlat+$maxlat)/2, max(1, -round(log10($latE)))-1); |
|
163 | - $ll['medlon'] = round(($minlon+$maxlon)/2, max(1, -round(log10($lonE)))-1); |
|
162 | + $ll['medlat'] = round(($minlat + $maxlat)/2, max(1, -round(log10($latE))) - 1); |
|
163 | + $ll['medlon'] = round(($minlon + $maxlon)/2, max(1, -round(log10($lonE))) - 1); |
|
164 | 164 | return $ll; |
165 | 165 | } |
166 | 166 | } |
@@ -19,8 +19,7 @@ discard block |
||
19 | 19 | $ll = $this->decode($hash); |
20 | 20 | if (!$as_grid) { |
21 | 21 | return new Point($ll['medlon'], $ll['medlat']); |
22 | - } |
|
23 | - else { |
|
22 | + } else { |
|
24 | 23 | return new Polygon(array( |
25 | 24 | new LineString(array( |
26 | 25 | new Point($ll['minlon'], $ll['maxlat']), |
@@ -40,12 +39,13 @@ discard block |
||
40 | 39 | * @see GeoAdapter::write() |
41 | 40 | */ |
42 | 41 | public function write(Geometry $geometry, $precision = NULL){ |
43 | - if ($geometry->isEmpty()) return ''; |
|
42 | + if ($geometry->isEmpty()) { |
|
43 | + return ''; |
|
44 | + } |
|
44 | 45 | |
45 | 46 | if($geometry->geometryType() === 'Point'){ |
46 | 47 | return $this->encodePoint($geometry, $precision); |
47 | - } |
|
48 | - else { |
|
48 | + } else { |
|
49 | 49 | // The geohash is the hash grid ID that fits the envelope |
50 | 50 | $envelope = $geometry->envelope(); |
51 | 51 | $geohashes = array(); |
@@ -138,19 +138,59 @@ discard block |
||
138 | 138 | for($i=0,$c=strlen($hash);$i<$c;$i++) { |
139 | 139 | $v = strpos($this->table,$hash[$i]); |
140 | 140 | if(1&$i) { |
141 | - if(16&$v)$minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
142 | - if(8&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
143 | - if(4&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
144 | - if(2&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
145 | - if(1&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
141 | + if(16&$v) { |
|
142 | + $minlat = ($minlat+$maxlat)/2; |
|
143 | + } else { |
|
144 | + $maxlat = ($minlat+$maxlat)/2; |
|
145 | + } |
|
146 | + if(8&$v) { |
|
147 | + $minlon = ($minlon+$maxlon)/2; |
|
148 | + } else { |
|
149 | + $maxlon = ($minlon+$maxlon)/2; |
|
150 | + } |
|
151 | + if(4&$v) { |
|
152 | + $minlat = ($minlat+$maxlat)/2; |
|
153 | + } else { |
|
154 | + $maxlat = ($minlat+$maxlat)/2; |
|
155 | + } |
|
156 | + if(2&$v) { |
|
157 | + $minlon = ($minlon+$maxlon)/2; |
|
158 | + } else { |
|
159 | + $maxlon = ($minlon+$maxlon)/2; |
|
160 | + } |
|
161 | + if(1&$v) { |
|
162 | + $minlat = ($minlat+$maxlat)/2; |
|
163 | + } else { |
|
164 | + $maxlat = ($minlat+$maxlat)/2; |
|
165 | + } |
|
146 | 166 | $latE /= 8; |
147 | 167 | $lonE /= 4; |
148 | 168 | } else { |
149 | - if(16&$v)$minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
150 | - if(8&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
151 | - if(4&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
152 | - if(2&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
153 | - if(1&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
169 | + if(16&$v) { |
|
170 | + $minlon = ($minlon+$maxlon)/2; |
|
171 | + } else { |
|
172 | + $maxlon = ($minlon+$maxlon)/2; |
|
173 | + } |
|
174 | + if(8&$v) { |
|
175 | + $minlat = ($minlat+$maxlat)/2; |
|
176 | + } else { |
|
177 | + $maxlat = ($minlat+$maxlat)/2; |
|
178 | + } |
|
179 | + if(4&$v) { |
|
180 | + $minlon = ($minlon+$maxlon)/2; |
|
181 | + } else { |
|
182 | + $maxlon = ($minlon+$maxlon)/2; |
|
183 | + } |
|
184 | + if(2&$v) { |
|
185 | + $minlat = ($minlat+$maxlat)/2; |
|
186 | + } else { |
|
187 | + $maxlat = ($minlat+$maxlat)/2; |
|
188 | + } |
|
189 | + if(1&$v) { |
|
190 | + $minlon = ($minlon+$maxlon)/2; |
|
191 | + } else { |
|
192 | + $maxlon = ($minlon+$maxlon)/2; |
|
193 | + } |
|
154 | 194 | $latE /= 4; |
155 | 195 | $lonE /= 8; |
156 | 196 | } |
@@ -12,14 +12,14 @@ |
||
12 | 12 | // MultiPoint or a LineString, since they share the same structure (collection |
13 | 13 | // of points). So we need to call out the type explicitly. |
14 | 14 | public function asArray() { |
15 | - $array = array(); |
|
16 | - foreach ($this->components as $component) { |
|
17 | - $array[] = array( |
|
18 | - 'type' => $component->geometryType(), |
|
19 | - 'components' => $component->asArray(), |
|
20 | - ); |
|
21 | - } |
|
22 | - return $array; |
|
15 | + $array = array(); |
|
16 | + foreach ($this->components as $component) { |
|
17 | + $array[] = array( |
|
18 | + 'type' => $component->geometryType(), |
|
19 | + 'components' => $component->asArray(), |
|
20 | + ); |
|
21 | + } |
|
22 | + return $array; |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | // Not valid for this geomettry |
@@ -18,31 +18,31 @@ discard block |
||
18 | 18 | * @param numeric $z The z coordinate (or altitude) - optional |
19 | 19 | */ |
20 | 20 | public function __construct($x, $y, $z = NULL) { |
21 | - // Basic validation on x and y |
|
22 | - if (!is_numeric($x) || !is_numeric($y)) { |
|
23 | - throw new Exception("Cannot construct Point. x and y should be numeric"); |
|
24 | - } |
|
25 | - |
|
26 | - // Check to see if this is a 3D point |
|
27 | - if ($z !== NULL) { |
|
28 | - if (!is_numeric($z)) { |
|
29 | - throw new Exception("Cannot construct Point. z should be numeric"); |
|
30 | - } |
|
31 | - $this->dimention = 3; |
|
32 | - } |
|
33 | - |
|
34 | - // Convert to floatval in case they are passed in as a string or integer etc. |
|
35 | - $x = floatval($x); |
|
36 | - $y = floatval($y); |
|
37 | - $z = floatval($z); |
|
38 | - |
|
39 | - // Add poitional elements |
|
40 | - if ($this->dimention == 2) { |
|
41 | - $this->coords = array($x, $y); |
|
42 | - } |
|
43 | - if ($this->dimention == 3) { |
|
44 | - $this->coords = array($x, $y, $z); |
|
45 | - } |
|
21 | + // Basic validation on x and y |
|
22 | + if (!is_numeric($x) || !is_numeric($y)) { |
|
23 | + throw new Exception("Cannot construct Point. x and y should be numeric"); |
|
24 | + } |
|
25 | + |
|
26 | + // Check to see if this is a 3D point |
|
27 | + if ($z !== NULL) { |
|
28 | + if (!is_numeric($z)) { |
|
29 | + throw new Exception("Cannot construct Point. z should be numeric"); |
|
30 | + } |
|
31 | + $this->dimention = 3; |
|
32 | + } |
|
33 | + |
|
34 | + // Convert to floatval in case they are passed in as a string or integer etc. |
|
35 | + $x = floatval($x); |
|
36 | + $y = floatval($y); |
|
37 | + $z = floatval($z); |
|
38 | + |
|
39 | + // Add poitional elements |
|
40 | + if ($this->dimention == 2) { |
|
41 | + $this->coords = array($x, $y); |
|
42 | + } |
|
43 | + if ($this->dimention == 3) { |
|
44 | + $this->coords = array($x, $y, $z); |
|
45 | + } |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | /** |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | * @return float The X coordinate |
52 | 52 | */ |
53 | 53 | public function x() { |
54 | - return $this->coords[0]; |
|
54 | + return $this->coords[0]; |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | * @return float The Y coordinate |
61 | 61 | */ |
62 | 62 | public function y() { |
63 | - return $this->coords[1]; |
|
63 | + return $this->coords[1]; |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | /** |
@@ -69,73 +69,73 @@ discard block |
||
69 | 69 | * @return float The Z coordinate or NULL is not a 3D point |
70 | 70 | */ |
71 | 71 | public function z() { |
72 | - if ($this->dimention == 3) { |
|
73 | - return $this->coords[2]; |
|
74 | - } |
|
75 | - else return NULL; |
|
72 | + if ($this->dimention == 3) { |
|
73 | + return $this->coords[2]; |
|
74 | + } |
|
75 | + else return NULL; |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | // A point's centroid is itself |
79 | 79 | public function centroid() { |
80 | - return $this; |
|
80 | + return $this; |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | public function getBBox() { |
84 | - return array( |
|
85 | - 'maxy' => $this->getY(), |
|
86 | - 'miny' => $this->getY(), |
|
87 | - 'maxx' => $this->getX(), |
|
88 | - 'minx' => $this->getX(), |
|
89 | - ); |
|
84 | + return array( |
|
85 | + 'maxy' => $this->getY(), |
|
86 | + 'miny' => $this->getY(), |
|
87 | + 'maxx' => $this->getX(), |
|
88 | + 'minx' => $this->getX(), |
|
89 | + ); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | public function asArray($assoc = FALSE) { |
93 | - return $this->coords; |
|
93 | + return $this->coords; |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | public function area() { |
97 | - return 0; |
|
97 | + return 0; |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | public function length() { |
101 | - return 0; |
|
101 | + return 0; |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | public function greatCircleLength() { |
105 | - return 0; |
|
105 | + return 0; |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | public function haversineLength() { |
109 | - return 0; |
|
109 | + return 0; |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | // The boundary of a point is itself |
113 | 113 | public function boundary() { |
114 | - return $this; |
|
114 | + return $this; |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | public function dimension() { |
118 | - return 0; |
|
118 | + return 0; |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | public function isEmpty() { |
122 | - return FALSE; |
|
122 | + return FALSE; |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | public function numPoints() { |
126 | - return 1; |
|
126 | + return 1; |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | public function getPoints() { |
130 | - return array($this); |
|
130 | + return array($this); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | public function equals($geometry) { |
134 | - return ($this->x() == $geometry->x() && $this->y() == $geometry->y()); |
|
134 | + return ($this->x() == $geometry->x() && $this->y() == $geometry->y()); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | public function isSimple() { |
138 | - return TRUE; |
|
138 | + return TRUE; |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | // Not valid for this geometry type |
@@ -139,17 +139,17 @@ |
||
139 | 139 | } |
140 | 140 | |
141 | 141 | // Not valid for this geometry type |
142 | - public function numGeometries() { return NULL; } |
|
143 | - public function geometryN($n) { return NULL; } |
|
144 | - public function startPoint() { return NULL; } |
|
145 | - public function endPoint() { return NULL; } |
|
146 | - public function isRing() { return NULL; } |
|
147 | - public function isClosed() { return NULL; } |
|
148 | - public function pointN($n) { return NULL; } |
|
149 | - public function exteriorRing() { return NULL; } |
|
142 | + public function numGeometries() { return NULL; } |
|
143 | + public function geometryN($n) { return NULL; } |
|
144 | + public function startPoint() { return NULL; } |
|
145 | + public function endPoint() { return NULL; } |
|
146 | + public function isRing() { return NULL; } |
|
147 | + public function isClosed() { return NULL; } |
|
148 | + public function pointN($n) { return NULL; } |
|
149 | + public function exteriorRing() { return NULL; } |
|
150 | 150 | public function numInteriorRings() { return NULL; } |
151 | - public function interiorRingN($n) { return NULL; } |
|
152 | - public function pointOnSurface() { return NULL; } |
|
153 | - public function explode() { return NULL; } |
|
151 | + public function interiorRingN($n) { return NULL; } |
|
152 | + public function pointOnSurface() { return NULL; } |
|
153 | + public function explode() { return NULL; } |
|
154 | 154 | } |
155 | 155 |
@@ -71,8 +71,9 @@ |
||
71 | 71 | public function z() { |
72 | 72 | if ($this->dimention == 3) { |
73 | 73 | return $this->coords[2]; |
74 | + } else { |
|
75 | + return NULL; |
|
74 | 76 | } |
75 | - else return NULL; |
|
76 | 77 | } |
77 | 78 | |
78 | 79 | // A point's centroid is itself |
@@ -7,11 +7,11 @@ |
||
7 | 7 | protected $geom_type = 'MultiPoint'; |
8 | 8 | |
9 | 9 | public function numPoints() { |
10 | - return $this->numGeometries(); |
|
10 | + return $this->numGeometries(); |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | public function isSimple() { |
14 | - return TRUE; |
|
14 | + return TRUE; |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | // Not valid for this geometry type |
@@ -8,12 +8,12 @@ |
||
8 | 8 | |
9 | 9 | // MultiLineString is closed if all it's components are closed |
10 | 10 | public function isClosed() { |
11 | - foreach ($this->components as $line) { |
|
12 | - if (!$line->isClosed()) { |
|
13 | - return FALSE; |
|
14 | - } |
|
15 | - } |
|
16 | - return TRUE; |
|
11 | + foreach ($this->components as $line) { |
|
12 | + if (!$line->isClosed()) { |
|
13 | + return FALSE; |
|
14 | + } |
|
15 | + } |
|
16 | + return TRUE; |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | } |
@@ -18,17 +18,17 @@ discard block |
||
18 | 18 | * @param array $components array of geometries |
19 | 19 | */ |
20 | 20 | public function __construct($components = array()) { |
21 | - if (!is_array($components)) { |
|
22 | - throw new Exception("Component geometries must be passed as an array"); |
|
23 | - } |
|
24 | - foreach ($components as $component) { |
|
25 | - if ($component instanceof Geometry) { |
|
26 | - $this->components[] = $component; |
|
27 | - } |
|
28 | - else { |
|
29 | - throw new Exception("Cannot create a collection with non-geometries"); |
|
30 | - } |
|
31 | - } |
|
21 | + if (!is_array($components)) { |
|
22 | + throw new Exception("Component geometries must be passed as an array"); |
|
23 | + } |
|
24 | + foreach ($components as $component) { |
|
25 | + if ($component instanceof Geometry) { |
|
26 | + $this->components[] = $component; |
|
27 | + } |
|
28 | + else { |
|
29 | + throw new Exception("Cannot create a collection with non-geometries"); |
|
30 | + } |
|
31 | + } |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** |
@@ -37,244 +37,244 @@ discard block |
||
37 | 37 | * @return array |
38 | 38 | */ |
39 | 39 | public function getComponents() { |
40 | - return $this->components; |
|
40 | + return $this->components; |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | public function centroid() { |
44 | - if ($this->isEmpty()) return NULL; |
|
44 | + if ($this->isEmpty()) return NULL; |
|
45 | 45 | |
46 | - if ($this->geos()) { |
|
47 | - $geos_centroid = $this->geos()->centroid(); |
|
48 | - if ($geos_centroid->typeName() == 'Point') { |
|
49 | - return geoPHP::geosToGeometry($this->geos()->centroid()); |
|
50 | - } |
|
51 | - } |
|
46 | + if ($this->geos()) { |
|
47 | + $geos_centroid = $this->geos()->centroid(); |
|
48 | + if ($geos_centroid->typeName() == 'Point') { |
|
49 | + return geoPHP::geosToGeometry($this->geos()->centroid()); |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | - // As a rough estimate, we say that the centroid of a colletion is the centroid of it's envelope |
|
54 | - // @@TODO: Make this the centroid of the convexHull |
|
55 | - // Note: Outside of polygons, geometryCollections and the trivial case of points, there is no standard on what a "centroid" is |
|
56 | - $centroid = $this->envelope()->centroid(); |
|
53 | + // As a rough estimate, we say that the centroid of a colletion is the centroid of it's envelope |
|
54 | + // @@TODO: Make this the centroid of the convexHull |
|
55 | + // Note: Outside of polygons, geometryCollections and the trivial case of points, there is no standard on what a "centroid" is |
|
56 | + $centroid = $this->envelope()->centroid(); |
|
57 | 57 | |
58 | - return $centroid; |
|
58 | + return $centroid; |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | public function getBBox() { |
62 | - if ($this->isEmpty()) return NULL; |
|
63 | - |
|
64 | - if ($this->geos()) { |
|
65 | - $envelope = $this->geos()->envelope(); |
|
66 | - if ($envelope->typeName() == 'Point') { |
|
67 | - return geoPHP::geosToGeometry($envelope)->getBBOX(); |
|
68 | - } |
|
69 | - |
|
70 | - $geos_ring = $envelope->exteriorRing(); |
|
71 | - return array( |
|
72 | - 'maxy' => $geos_ring->pointN(3)->getY(), |
|
73 | - 'miny' => $geos_ring->pointN(1)->getY(), |
|
74 | - 'maxx' => $geos_ring->pointN(1)->getX(), |
|
75 | - 'minx' => $geos_ring->pointN(3)->getX(), |
|
76 | - ); |
|
77 | - } |
|
78 | - |
|
79 | - // Go through each component and get the max and min x and y |
|
80 | - $i = 0; |
|
81 | - foreach ($this->components as $component) { |
|
82 | - $component_bbox = $component->getBBox(); |
|
83 | - |
|
84 | - // On the first run through, set the bbox to the component bbox |
|
85 | - if ($i == 0) { |
|
86 | - $maxx = $component_bbox['maxx']; |
|
87 | - $maxy = $component_bbox['maxy']; |
|
88 | - $minx = $component_bbox['minx']; |
|
89 | - $miny = $component_bbox['miny']; |
|
90 | - } |
|
91 | - |
|
92 | - // Do a check and replace on each boundary, slowly growing the bbox |
|
93 | - $maxx = $component_bbox['maxx'] > $maxx ? $component_bbox['maxx'] : $maxx; |
|
94 | - $maxy = $component_bbox['maxy'] > $maxy ? $component_bbox['maxy'] : $maxy; |
|
95 | - $minx = $component_bbox['minx'] < $minx ? $component_bbox['minx'] : $minx; |
|
96 | - $miny = $component_bbox['miny'] < $miny ? $component_bbox['miny'] : $miny; |
|
97 | - $i++; |
|
98 | - } |
|
99 | - |
|
100 | - return array( |
|
101 | - 'maxy' => $maxy, |
|
102 | - 'miny' => $miny, |
|
103 | - 'maxx' => $maxx, |
|
104 | - 'minx' => $minx, |
|
105 | - ); |
|
62 | + if ($this->isEmpty()) return NULL; |
|
63 | + |
|
64 | + if ($this->geos()) { |
|
65 | + $envelope = $this->geos()->envelope(); |
|
66 | + if ($envelope->typeName() == 'Point') { |
|
67 | + return geoPHP::geosToGeometry($envelope)->getBBOX(); |
|
68 | + } |
|
69 | + |
|
70 | + $geos_ring = $envelope->exteriorRing(); |
|
71 | + return array( |
|
72 | + 'maxy' => $geos_ring->pointN(3)->getY(), |
|
73 | + 'miny' => $geos_ring->pointN(1)->getY(), |
|
74 | + 'maxx' => $geos_ring->pointN(1)->getX(), |
|
75 | + 'minx' => $geos_ring->pointN(3)->getX(), |
|
76 | + ); |
|
77 | + } |
|
78 | + |
|
79 | + // Go through each component and get the max and min x and y |
|
80 | + $i = 0; |
|
81 | + foreach ($this->components as $component) { |
|
82 | + $component_bbox = $component->getBBox(); |
|
83 | + |
|
84 | + // On the first run through, set the bbox to the component bbox |
|
85 | + if ($i == 0) { |
|
86 | + $maxx = $component_bbox['maxx']; |
|
87 | + $maxy = $component_bbox['maxy']; |
|
88 | + $minx = $component_bbox['minx']; |
|
89 | + $miny = $component_bbox['miny']; |
|
90 | + } |
|
91 | + |
|
92 | + // Do a check and replace on each boundary, slowly growing the bbox |
|
93 | + $maxx = $component_bbox['maxx'] > $maxx ? $component_bbox['maxx'] : $maxx; |
|
94 | + $maxy = $component_bbox['maxy'] > $maxy ? $component_bbox['maxy'] : $maxy; |
|
95 | + $minx = $component_bbox['minx'] < $minx ? $component_bbox['minx'] : $minx; |
|
96 | + $miny = $component_bbox['miny'] < $miny ? $component_bbox['miny'] : $miny; |
|
97 | + $i++; |
|
98 | + } |
|
99 | + |
|
100 | + return array( |
|
101 | + 'maxy' => $maxy, |
|
102 | + 'miny' => $miny, |
|
103 | + 'maxx' => $maxx, |
|
104 | + 'minx' => $minx, |
|
105 | + ); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | public function asArray() { |
109 | - $array = array(); |
|
110 | - foreach ($this->components as $component) { |
|
111 | - $array[] = $component->asArray(); |
|
112 | - } |
|
113 | - return $array; |
|
109 | + $array = array(); |
|
110 | + foreach ($this->components as $component) { |
|
111 | + $array[] = $component->asArray(); |
|
112 | + } |
|
113 | + return $array; |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | public function area() { |
117 | - if ($this->geos()) { |
|
118 | - return $this->geos()->area(); |
|
119 | - } |
|
120 | - |
|
121 | - $area = 0; |
|
122 | - foreach ($this->components as $component) { |
|
123 | - $area += $component->area(); |
|
124 | - } |
|
125 | - return $area; |
|
117 | + if ($this->geos()) { |
|
118 | + return $this->geos()->area(); |
|
119 | + } |
|
120 | + |
|
121 | + $area = 0; |
|
122 | + foreach ($this->components as $component) { |
|
123 | + $area += $component->area(); |
|
124 | + } |
|
125 | + return $area; |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | // By default, the boundary of a collection is the boundary of it's components |
129 | 129 | public function boundary() { |
130 | - if ($this->isEmpty()) return new LineString(); |
|
130 | + if ($this->isEmpty()) return new LineString(); |
|
131 | 131 | |
132 | - if ($this->geos()) { |
|
133 | - return $this->geos()->boundary(); |
|
134 | - } |
|
132 | + if ($this->geos()) { |
|
133 | + return $this->geos()->boundary(); |
|
134 | + } |
|
135 | 135 | |
136 | - $components_boundaries = array(); |
|
137 | - foreach ($this->components as $component) { |
|
138 | - $components_boundaries[] = $component->boundary(); |
|
139 | - } |
|
140 | - return geoPHP::geometryReduce($components_boundaries); |
|
136 | + $components_boundaries = array(); |
|
137 | + foreach ($this->components as $component) { |
|
138 | + $components_boundaries[] = $component->boundary(); |
|
139 | + } |
|
140 | + return geoPHP::geometryReduce($components_boundaries); |
|
141 | 141 | } |
142 | 142 | |
143 | 143 | public function numGeometries() { |
144 | - return count($this->components); |
|
144 | + return count($this->components); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | // Note that the standard is 1 based indexing |
148 | 148 | public function geometryN($n) { |
149 | - $n = intval($n); |
|
150 | - if (array_key_exists($n-1, $this->components)) { |
|
151 | - return $this->components[$n-1]; |
|
152 | - } |
|
153 | - else { |
|
154 | - return NULL; |
|
155 | - } |
|
149 | + $n = intval($n); |
|
150 | + if (array_key_exists($n-1, $this->components)) { |
|
151 | + return $this->components[$n-1]; |
|
152 | + } |
|
153 | + else { |
|
154 | + return NULL; |
|
155 | + } |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | public function length() { |
159 | - $length = 0; |
|
160 | - foreach ($this->components as $delta => $component) { |
|
161 | - $length += $component->length(); |
|
162 | - } |
|
163 | - return $length; |
|
159 | + $length = 0; |
|
160 | + foreach ($this->components as $delta => $component) { |
|
161 | + $length += $component->length(); |
|
162 | + } |
|
163 | + return $length; |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | public function greatCircleLength($radius = 6378137) { |
167 | - $length = 0; |
|
168 | - foreach ($this->components as $component) { |
|
169 | - $length += $component->greatCircleLength($radius); |
|
170 | - } |
|
171 | - return $length; |
|
167 | + $length = 0; |
|
168 | + foreach ($this->components as $component) { |
|
169 | + $length += $component->greatCircleLength($radius); |
|
170 | + } |
|
171 | + return $length; |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | public function haversineLength() { |
175 | - $length = 0; |
|
176 | - foreach ($this->components as $component) { |
|
177 | - $length += $component->haversineLength(); |
|
178 | - } |
|
179 | - return $length; |
|
175 | + $length = 0; |
|
176 | + foreach ($this->components as $component) { |
|
177 | + $length += $component->haversineLength(); |
|
178 | + } |
|
179 | + return $length; |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | public function dimension() { |
183 | - $dimension = 0; |
|
184 | - foreach ($this->components as $component) { |
|
185 | - if ($component->dimension() > $dimension) { |
|
186 | - $dimension = $component->dimension(); |
|
187 | - } |
|
188 | - } |
|
189 | - return $dimension; |
|
183 | + $dimension = 0; |
|
184 | + foreach ($this->components as $component) { |
|
185 | + if ($component->dimension() > $dimension) { |
|
186 | + $dimension = $component->dimension(); |
|
187 | + } |
|
188 | + } |
|
189 | + return $dimension; |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | // A collection is empty if it has no components OR all it's components are empty |
193 | 193 | public function isEmpty() { |
194 | - if (!count($this->components)) { |
|
195 | - return TRUE; |
|
196 | - } |
|
197 | - else { |
|
198 | - foreach ($this->components as $component) { |
|
199 | - if (!$component->isEmpty()) return FALSE; |
|
200 | - } |
|
201 | - return TRUE; |
|
202 | - } |
|
194 | + if (!count($this->components)) { |
|
195 | + return TRUE; |
|
196 | + } |
|
197 | + else { |
|
198 | + foreach ($this->components as $component) { |
|
199 | + if (!$component->isEmpty()) return FALSE; |
|
200 | + } |
|
201 | + return TRUE; |
|
202 | + } |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | public function numPoints() { |
206 | - $num = 0; |
|
207 | - foreach ($this->components as $component) { |
|
208 | - $num += $component->numPoints(); |
|
209 | - } |
|
210 | - return $num; |
|
206 | + $num = 0; |
|
207 | + foreach ($this->components as $component) { |
|
208 | + $num += $component->numPoints(); |
|
209 | + } |
|
210 | + return $num; |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | public function getPoints() { |
214 | - $points = array(); |
|
215 | - foreach ($this->components as $component) { |
|
216 | - $points = array_merge($points, $component->getPoints()); |
|
217 | - } |
|
218 | - return $points; |
|
214 | + $points = array(); |
|
215 | + foreach ($this->components as $component) { |
|
216 | + $points = array_merge($points, $component->getPoints()); |
|
217 | + } |
|
218 | + return $points; |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | public function equals($geometry) { |
222 | - if ($this->geos()) { |
|
223 | - return $this->geos()->equals($geometry->geos()); |
|
224 | - } |
|
225 | - |
|
226 | - // To test for equality we check to make sure that there is a matching point |
|
227 | - // in the other geometry for every point in this geometry. |
|
228 | - // This is slightly more strict than the standard, which |
|
229 | - // uses Within(A,B) = true and Within(B,A) = true |
|
230 | - // @@TODO: Eventually we could fix this by using some sort of simplification |
|
231 | - // method that strips redundant vertices (that are all in a row) |
|
232 | - |
|
233 | - $this_points = $this->getPoints(); |
|
234 | - $other_points = $geometry->getPoints(); |
|
235 | - |
|
236 | - // First do a check to make sure they have the same number of vertices |
|
237 | - if (count($this_points) != count($other_points)) { |
|
238 | - return FALSE; |
|
239 | - } |
|
240 | - |
|
241 | - foreach ($this_points as $point) { |
|
242 | - $found_match = FALSE; |
|
243 | - foreach ($other_points as $key => $test_point) { |
|
244 | - if ($point->equals($test_point)) { |
|
245 | - $found_match = TRUE; |
|
246 | - unset($other_points[$key]); |
|
247 | - break; |
|
248 | - } |
|
249 | - } |
|
250 | - if (!$found_match) { |
|
251 | - return FALSE; |
|
252 | - } |
|
253 | - } |
|
254 | - |
|
255 | - // All points match, return TRUE |
|
256 | - return TRUE; |
|
222 | + if ($this->geos()) { |
|
223 | + return $this->geos()->equals($geometry->geos()); |
|
224 | + } |
|
225 | + |
|
226 | + // To test for equality we check to make sure that there is a matching point |
|
227 | + // in the other geometry for every point in this geometry. |
|
228 | + // This is slightly more strict than the standard, which |
|
229 | + // uses Within(A,B) = true and Within(B,A) = true |
|
230 | + // @@TODO: Eventually we could fix this by using some sort of simplification |
|
231 | + // method that strips redundant vertices (that are all in a row) |
|
232 | + |
|
233 | + $this_points = $this->getPoints(); |
|
234 | + $other_points = $geometry->getPoints(); |
|
235 | + |
|
236 | + // First do a check to make sure they have the same number of vertices |
|
237 | + if (count($this_points) != count($other_points)) { |
|
238 | + return FALSE; |
|
239 | + } |
|
240 | + |
|
241 | + foreach ($this_points as $point) { |
|
242 | + $found_match = FALSE; |
|
243 | + foreach ($other_points as $key => $test_point) { |
|
244 | + if ($point->equals($test_point)) { |
|
245 | + $found_match = TRUE; |
|
246 | + unset($other_points[$key]); |
|
247 | + break; |
|
248 | + } |
|
249 | + } |
|
250 | + if (!$found_match) { |
|
251 | + return FALSE; |
|
252 | + } |
|
253 | + } |
|
254 | + |
|
255 | + // All points match, return TRUE |
|
256 | + return TRUE; |
|
257 | 257 | } |
258 | 258 | |
259 | 259 | public function isSimple() { |
260 | - if ($this->geos()) { |
|
261 | - return $this->geos()->isSimple(); |
|
262 | - } |
|
260 | + if ($this->geos()) { |
|
261 | + return $this->geos()->isSimple(); |
|
262 | + } |
|
263 | 263 | |
264 | - // A collection is simple if all it's components are simple |
|
265 | - foreach ($this->components as $component) { |
|
266 | - if (!$component->isSimple()) return FALSE; |
|
267 | - } |
|
264 | + // A collection is simple if all it's components are simple |
|
265 | + foreach ($this->components as $component) { |
|
266 | + if (!$component->isSimple()) return FALSE; |
|
267 | + } |
|
268 | 268 | |
269 | - return TRUE; |
|
269 | + return TRUE; |
|
270 | 270 | } |
271 | 271 | |
272 | 272 | public function explode() { |
273 | - $parts = array(); |
|
274 | - foreach ($this->components as $component) { |
|
275 | - $parts = array_merge($parts, $component->explode()); |
|
276 | - } |
|
277 | - return $parts; |
|
273 | + $parts = array(); |
|
274 | + foreach ($this->components as $component) { |
|
275 | + $parts = array_merge($parts, $component->explode()); |
|
276 | + } |
|
277 | + return $parts; |
|
278 | 278 | } |
279 | 279 | |
280 | 280 | // Not valid for this geometry type |
@@ -147,8 +147,8 @@ discard block |
||
147 | 147 | // Note that the standard is 1 based indexing |
148 | 148 | public function geometryN($n) { |
149 | 149 | $n = intval($n); |
150 | - if (array_key_exists($n-1, $this->components)) { |
|
151 | - return $this->components[$n-1]; |
|
150 | + if (array_key_exists($n - 1, $this->components)) { |
|
151 | + return $this->components[$n - 1]; |
|
152 | 152 | } |
153 | 153 | else { |
154 | 154 | return NULL; |
@@ -279,16 +279,16 @@ discard block |
||
279 | 279 | |
280 | 280 | // Not valid for this geometry type |
281 | 281 | // -------------------------------- |
282 | - public function x() { return NULL; } |
|
283 | - public function y() { return NULL; } |
|
284 | - public function startPoint() { return NULL; } |
|
285 | - public function endPoint() { return NULL; } |
|
286 | - public function isRing() { return NULL; } |
|
287 | - public function isClosed() { return NULL; } |
|
288 | - public function pointN($n) { return NULL; } |
|
289 | - public function exteriorRing() { return NULL; } |
|
282 | + public function x() { return NULL; } |
|
283 | + public function y() { return NULL; } |
|
284 | + public function startPoint() { return NULL; } |
|
285 | + public function endPoint() { return NULL; } |
|
286 | + public function isRing() { return NULL; } |
|
287 | + public function isClosed() { return NULL; } |
|
288 | + public function pointN($n) { return NULL; } |
|
289 | + public function exteriorRing() { return NULL; } |
|
290 | 290 | public function numInteriorRings() { return NULL; } |
291 | - public function interiorRingN($n) { return NULL; } |
|
292 | - public function pointOnSurface() { return NULL; } |
|
291 | + public function interiorRingN($n) { return NULL; } |
|
292 | + public function pointOnSurface() { return NULL; } |
|
293 | 293 | } |
294 | 294 |
@@ -24,8 +24,7 @@ discard block |
||
24 | 24 | foreach ($components as $component) { |
25 | 25 | if ($component instanceof Geometry) { |
26 | 26 | $this->components[] = $component; |
27 | - } |
|
28 | - else { |
|
27 | + } else { |
|
29 | 28 | throw new Exception("Cannot create a collection with non-geometries"); |
30 | 29 | } |
31 | 30 | } |
@@ -41,7 +40,9 @@ discard block |
||
41 | 40 | } |
42 | 41 | |
43 | 42 | public function centroid() { |
44 | - if ($this->isEmpty()) return NULL; |
|
43 | + if ($this->isEmpty()) { |
|
44 | + return NULL; |
|
45 | + } |
|
45 | 46 | |
46 | 47 | if ($this->geos()) { |
47 | 48 | $geos_centroid = $this->geos()->centroid(); |
@@ -59,7 +60,9 @@ discard block |
||
59 | 60 | } |
60 | 61 | |
61 | 62 | public function getBBox() { |
62 | - if ($this->isEmpty()) return NULL; |
|
63 | + if ($this->isEmpty()) { |
|
64 | + return NULL; |
|
65 | + } |
|
63 | 66 | |
64 | 67 | if ($this->geos()) { |
65 | 68 | $envelope = $this->geos()->envelope(); |
@@ -127,7 +130,9 @@ discard block |
||
127 | 130 | |
128 | 131 | // By default, the boundary of a collection is the boundary of it's components |
129 | 132 | public function boundary() { |
130 | - if ($this->isEmpty()) return new LineString(); |
|
133 | + if ($this->isEmpty()) { |
|
134 | + return new LineString(); |
|
135 | + } |
|
131 | 136 | |
132 | 137 | if ($this->geos()) { |
133 | 138 | return $this->geos()->boundary(); |
@@ -149,8 +154,7 @@ discard block |
||
149 | 154 | $n = intval($n); |
150 | 155 | if (array_key_exists($n-1, $this->components)) { |
151 | 156 | return $this->components[$n-1]; |
152 | - } |
|
153 | - else { |
|
157 | + } else { |
|
154 | 158 | return NULL; |
155 | 159 | } |
156 | 160 | } |
@@ -193,10 +197,11 @@ discard block |
||
193 | 197 | public function isEmpty() { |
194 | 198 | if (!count($this->components)) { |
195 | 199 | return TRUE; |
196 | - } |
|
197 | - else { |
|
200 | + } else { |
|
198 | 201 | foreach ($this->components as $component) { |
199 | - if (!$component->isEmpty()) return FALSE; |
|
202 | + if (!$component->isEmpty()) { |
|
203 | + return FALSE; |
|
204 | + } |
|
200 | 205 | } |
201 | 206 | return TRUE; |
202 | 207 | } |
@@ -263,7 +268,9 @@ discard block |
||
263 | 268 | |
264 | 269 | // A collection is simple if all it's components are simple |
265 | 270 | foreach ($this->components as $component) { |
266 | - if (!$component->isSimple()) return FALSE; |
|
271 | + if (!$component->isSimple()) { |
|
272 | + return FALSE; |
|
273 | + } |
|
267 | 274 | } |
268 | 275 | |
269 | 276 | return TRUE; |
@@ -19,173 +19,173 @@ |
||
19 | 19 | throw new Exception("Cannot construct a LineString with a single point"); |
20 | 20 | } |
21 | 21 | */ |
22 | - // Call the Collection constructor to build the LineString |
|
23 | - parent::__construct($points); |
|
22 | + // Call the Collection constructor to build the LineString |
|
23 | + parent::__construct($points); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | // The boundary of a linestring is itself |
27 | 27 | public function boundary() { |
28 | - return $this; |
|
28 | + return $this; |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | public function startPoint() { |
32 | - return $this->pointN(1); |
|
32 | + return $this->pointN(1); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | public function endPoint() { |
36 | - $last_n = $this->numPoints(); |
|
37 | - return $this->pointN($last_n); |
|
36 | + $last_n = $this->numPoints(); |
|
37 | + return $this->pointN($last_n); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | public function isClosed() { |
41 | - return ($this->startPoint()->equals($this->endPoint())); |
|
41 | + return ($this->startPoint()->equals($this->endPoint())); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | public function isRing() { |
45 | - return ($this->isClosed() && $this->isSimple()); |
|
45 | + return ($this->isClosed() && $this->isSimple()); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | public function numPoints() { |
49 | - return $this->numGeometries(); |
|
49 | + return $this->numGeometries(); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | public function pointN($n) { |
53 | - return $this->geometryN($n); |
|
53 | + return $this->geometryN($n); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | public function dimension() { |
57 | - if ($this->isEmpty()) return 0; |
|
58 | - return 1; |
|
57 | + if ($this->isEmpty()) return 0; |
|
58 | + return 1; |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | public function area() { |
62 | - return 0; |
|
62 | + return 0; |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | public function length() { |
66 | - if ($this->geos()) { |
|
67 | - return $this->geos()->length(); |
|
68 | - } |
|
69 | - $length = 0; |
|
70 | - foreach ($this->getPoints() as $delta => $point) { |
|
71 | - $previous_point = $this->geometryN($delta); |
|
72 | - if ($previous_point) { |
|
73 | - $length += sqrt(pow(($previous_point->getX() - $point->getX()), 2) + pow(($previous_point->getY()- $point->getY()), 2)); |
|
74 | - } |
|
75 | - } |
|
76 | - return $length; |
|
66 | + if ($this->geos()) { |
|
67 | + return $this->geos()->length(); |
|
68 | + } |
|
69 | + $length = 0; |
|
70 | + foreach ($this->getPoints() as $delta => $point) { |
|
71 | + $previous_point = $this->geometryN($delta); |
|
72 | + if ($previous_point) { |
|
73 | + $length += sqrt(pow(($previous_point->getX() - $point->getX()), 2) + pow(($previous_point->getY()- $point->getY()), 2)); |
|
74 | + } |
|
75 | + } |
|
76 | + return $length; |
|
77 | 77 | } |
78 | 78 | |
79 | 79 | public function greatCircleLength($radius = 6378137) { |
80 | - $length = 0; |
|
81 | - $points = $this->getPoints(); |
|
82 | - for($i=0; $i<$this->numPoints()-1; $i++) { |
|
83 | - $point = $points[$i]; |
|
84 | - $next_point = $points[$i+1]; |
|
85 | - if (!is_object($next_point)) {continue;} |
|
86 | - // Great circle method |
|
87 | - $lat1 = deg2rad($point->getY()); |
|
88 | - $lat2 = deg2rad($next_point->getY()); |
|
89 | - $lon1 = deg2rad($point->getX()); |
|
90 | - $lon2 = deg2rad($next_point->getX()); |
|
91 | - $dlon = $lon2 - $lon1; |
|
92 | - $length += |
|
93 | - $radius * |
|
94 | - atan2( |
|
95 | - sqrt( |
|
96 | - pow(cos($lat2) * sin($dlon), 2) + |
|
97 | - pow(cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dlon), 2) |
|
98 | - ) |
|
99 | - , |
|
100 | - sin($lat1) * sin($lat2) + |
|
101 | - cos($lat1) * cos($lat2) * cos($dlon) |
|
102 | - ); |
|
103 | - } |
|
104 | - // Returns length in meters. |
|
105 | - return $length; |
|
80 | + $length = 0; |
|
81 | + $points = $this->getPoints(); |
|
82 | + for($i=0; $i<$this->numPoints()-1; $i++) { |
|
83 | + $point = $points[$i]; |
|
84 | + $next_point = $points[$i+1]; |
|
85 | + if (!is_object($next_point)) {continue;} |
|
86 | + // Great circle method |
|
87 | + $lat1 = deg2rad($point->getY()); |
|
88 | + $lat2 = deg2rad($next_point->getY()); |
|
89 | + $lon1 = deg2rad($point->getX()); |
|
90 | + $lon2 = deg2rad($next_point->getX()); |
|
91 | + $dlon = $lon2 - $lon1; |
|
92 | + $length += |
|
93 | + $radius * |
|
94 | + atan2( |
|
95 | + sqrt( |
|
96 | + pow(cos($lat2) * sin($dlon), 2) + |
|
97 | + pow(cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dlon), 2) |
|
98 | + ) |
|
99 | + , |
|
100 | + sin($lat1) * sin($lat2) + |
|
101 | + cos($lat1) * cos($lat2) * cos($dlon) |
|
102 | + ); |
|
103 | + } |
|
104 | + // Returns length in meters. |
|
105 | + return $length; |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | public function haversineLength() { |
109 | - $degrees = 0; |
|
110 | - $points = $this->getPoints(); |
|
111 | - for($i=0; $i<$this->numPoints()-1; $i++) { |
|
112 | - $point = $points[$i]; |
|
113 | - $next_point = $points[$i+1]; |
|
114 | - if (!is_object($next_point)) {continue;} |
|
115 | - $degree = rad2deg( |
|
116 | - acos( |
|
117 | - sin(deg2rad($point->getY())) * sin(deg2rad($next_point->getY())) + |
|
118 | - cos(deg2rad($point->getY())) * cos(deg2rad($next_point->getY())) * |
|
119 | - cos(deg2rad(abs($point->getX() - $next_point->getX()))) |
|
120 | - ) |
|
121 | - ); |
|
122 | - $degrees += $degree; |
|
123 | - } |
|
124 | - // Returns degrees |
|
125 | - return $degrees; |
|
109 | + $degrees = 0; |
|
110 | + $points = $this->getPoints(); |
|
111 | + for($i=0; $i<$this->numPoints()-1; $i++) { |
|
112 | + $point = $points[$i]; |
|
113 | + $next_point = $points[$i+1]; |
|
114 | + if (!is_object($next_point)) {continue;} |
|
115 | + $degree = rad2deg( |
|
116 | + acos( |
|
117 | + sin(deg2rad($point->getY())) * sin(deg2rad($next_point->getY())) + |
|
118 | + cos(deg2rad($point->getY())) * cos(deg2rad($next_point->getY())) * |
|
119 | + cos(deg2rad(abs($point->getX() - $next_point->getX()))) |
|
120 | + ) |
|
121 | + ); |
|
122 | + $degrees += $degree; |
|
123 | + } |
|
124 | + // Returns degrees |
|
125 | + return $degrees; |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | public function explode() { |
129 | - $parts = array(); |
|
130 | - $points = $this->getPoints(); |
|
129 | + $parts = array(); |
|
130 | + $points = $this->getPoints(); |
|
131 | 131 | |
132 | - foreach ($points as $i => $point) { |
|
133 | - if (isset($points[$i+1])) { |
|
134 | - $parts[] = new LineString(array($point, $points[$i+1])); |
|
135 | - } |
|
136 | - } |
|
137 | - return $parts; |
|
132 | + foreach ($points as $i => $point) { |
|
133 | + if (isset($points[$i+1])) { |
|
134 | + $parts[] = new LineString(array($point, $points[$i+1])); |
|
135 | + } |
|
136 | + } |
|
137 | + return $parts; |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | public function isSimple() { |
141 | - if ($this->geos()) { |
|
142 | - return $this->geos()->isSimple(); |
|
143 | - } |
|
141 | + if ($this->geos()) { |
|
142 | + return $this->geos()->isSimple(); |
|
143 | + } |
|
144 | 144 | |
145 | - $segments = $this->explode(); |
|
145 | + $segments = $this->explode(); |
|
146 | 146 | |
147 | - foreach ($segments as $i => $segment) { |
|
148 | - foreach ($segments as $j => $check_segment) { |
|
149 | - if ($i != $j) { |
|
150 | - if ($segment->lineSegmentIntersect($check_segment)) { |
|
151 | - return FALSE; |
|
152 | - } |
|
153 | - } |
|
154 | - } |
|
155 | - } |
|
156 | - return TRUE; |
|
147 | + foreach ($segments as $i => $segment) { |
|
148 | + foreach ($segments as $j => $check_segment) { |
|
149 | + if ($i != $j) { |
|
150 | + if ($segment->lineSegmentIntersect($check_segment)) { |
|
151 | + return FALSE; |
|
152 | + } |
|
153 | + } |
|
154 | + } |
|
155 | + } |
|
156 | + return TRUE; |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | // Utility function to check if any line sigments intersect |
160 | 160 | // Derived from http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect |
161 | 161 | public function lineSegmentIntersect($segment) { |
162 | - $p0_x = $this->startPoint()->x(); |
|
163 | - $p0_y = $this->startPoint()->y(); |
|
164 | - $p1_x = $this->endPoint()->x(); |
|
165 | - $p1_y = $this->endPoint()->y(); |
|
166 | - $p2_x = $segment->startPoint()->x(); |
|
167 | - $p2_y = $segment->startPoint()->y(); |
|
168 | - $p3_x = $segment->endPoint()->x(); |
|
169 | - $p3_y = $segment->endPoint()->y(); |
|
170 | - |
|
171 | - $s1_x = $p1_x - $p0_x; $s1_y = $p1_y - $p0_y; |
|
172 | - $s2_x = $p3_x - $p2_x; $s2_y = $p3_y - $p2_y; |
|
173 | - |
|
174 | - $fps = (-$s2_x * $s1_y) + ($s1_x * $s2_y); |
|
175 | - $fpt = (-$s2_x * $s1_y) + ($s1_x * $s2_y); |
|
176 | - |
|
177 | - if ($fps == 0 || $fpt == 0) { |
|
178 | - return FALSE; |
|
179 | - } |
|
180 | - |
|
181 | - $s = (-$s1_y * ($p0_x - $p2_x) + $s1_x * ($p0_y - $p2_y)) / $fps; |
|
182 | - $t = ( $s2_x * ($p0_y - $p2_y) - $s2_y * ($p0_x - $p2_x)) / $fpt; |
|
183 | - |
|
184 | - if ($s > 0 && $s < 1 && $t > 0 && $t < 1) { |
|
185 | - // Collision detected |
|
186 | - return TRUE; |
|
187 | - } |
|
188 | - return FALSE; |
|
162 | + $p0_x = $this->startPoint()->x(); |
|
163 | + $p0_y = $this->startPoint()->y(); |
|
164 | + $p1_x = $this->endPoint()->x(); |
|
165 | + $p1_y = $this->endPoint()->y(); |
|
166 | + $p2_x = $segment->startPoint()->x(); |
|
167 | + $p2_y = $segment->startPoint()->y(); |
|
168 | + $p3_x = $segment->endPoint()->x(); |
|
169 | + $p3_y = $segment->endPoint()->y(); |
|
170 | + |
|
171 | + $s1_x = $p1_x - $p0_x; $s1_y = $p1_y - $p0_y; |
|
172 | + $s2_x = $p3_x - $p2_x; $s2_y = $p3_y - $p2_y; |
|
173 | + |
|
174 | + $fps = (-$s2_x * $s1_y) + ($s1_x * $s2_y); |
|
175 | + $fpt = (-$s2_x * $s1_y) + ($s1_x * $s2_y); |
|
176 | + |
|
177 | + if ($fps == 0 || $fpt == 0) { |
|
178 | + return FALSE; |
|
179 | + } |
|
180 | + |
|
181 | + $s = (-$s1_y * ($p0_x - $p2_x) + $s1_x * ($p0_y - $p2_y)) / $fps; |
|
182 | + $t = ( $s2_x * ($p0_y - $p2_y) - $s2_y * ($p0_x - $p2_x)) / $fpt; |
|
183 | + |
|
184 | + if ($s > 0 && $s < 1 && $t > 0 && $t < 1) { |
|
185 | + // Collision detected |
|
186 | + return TRUE; |
|
187 | + } |
|
188 | + return FALSE; |
|
189 | 189 | } |
190 | 190 | } |
191 | 191 |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | foreach ($this->getPoints() as $delta => $point) { |
71 | 71 | $previous_point = $this->geometryN($delta); |
72 | 72 | if ($previous_point) { |
73 | - $length += sqrt(pow(($previous_point->getX() - $point->getX()), 2) + pow(($previous_point->getY()- $point->getY()), 2)); |
|
73 | + $length += sqrt(pow(($previous_point->getX() - $point->getX()), 2) + pow(($previous_point->getY() - $point->getY()), 2)); |
|
74 | 74 | } |
75 | 75 | } |
76 | 76 | return $length; |
@@ -79,10 +79,10 @@ discard block |
||
79 | 79 | public function greatCircleLength($radius = 6378137) { |
80 | 80 | $length = 0; |
81 | 81 | $points = $this->getPoints(); |
82 | - for($i=0; $i<$this->numPoints()-1; $i++) { |
|
82 | + for ($i = 0; $i < $this->numPoints() - 1; $i++) { |
|
83 | 83 | $point = $points[$i]; |
84 | - $next_point = $points[$i+1]; |
|
85 | - if (!is_object($next_point)) {continue;} |
|
84 | + $next_point = $points[$i + 1]; |
|
85 | + if (!is_object($next_point)) {continue; } |
|
86 | 86 | // Great circle method |
87 | 87 | $lat1 = deg2rad($point->getY()); |
88 | 88 | $lat2 = deg2rad($next_point->getY()); |
@@ -90,15 +90,15 @@ discard block |
||
90 | 90 | $lon2 = deg2rad($next_point->getX()); |
91 | 91 | $dlon = $lon2 - $lon1; |
92 | 92 | $length += |
93 | - $radius * |
|
93 | + $radius* |
|
94 | 94 | atan2( |
95 | 95 | sqrt( |
96 | - pow(cos($lat2) * sin($dlon), 2) + |
|
97 | - pow(cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dlon), 2) |
|
96 | + pow(cos($lat2)*sin($dlon), 2) + |
|
97 | + pow(cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dlon), 2) |
|
98 | 98 | ) |
99 | 99 | , |
100 | - sin($lat1) * sin($lat2) + |
|
101 | - cos($lat1) * cos($lat2) * cos($dlon) |
|
100 | + sin($lat1)*sin($lat2) + |
|
101 | + cos($lat1)*cos($lat2)*cos($dlon) |
|
102 | 102 | ); |
103 | 103 | } |
104 | 104 | // Returns length in meters. |
@@ -108,14 +108,14 @@ discard block |
||
108 | 108 | public function haversineLength() { |
109 | 109 | $degrees = 0; |
110 | 110 | $points = $this->getPoints(); |
111 | - for($i=0; $i<$this->numPoints()-1; $i++) { |
|
111 | + for ($i = 0; $i < $this->numPoints() - 1; $i++) { |
|
112 | 112 | $point = $points[$i]; |
113 | - $next_point = $points[$i+1]; |
|
114 | - if (!is_object($next_point)) {continue;} |
|
113 | + $next_point = $points[$i + 1]; |
|
114 | + if (!is_object($next_point)) {continue; } |
|
115 | 115 | $degree = rad2deg( |
116 | 116 | acos( |
117 | - sin(deg2rad($point->getY())) * sin(deg2rad($next_point->getY())) + |
|
118 | - cos(deg2rad($point->getY())) * cos(deg2rad($next_point->getY())) * |
|
117 | + sin(deg2rad($point->getY()))*sin(deg2rad($next_point->getY())) + |
|
118 | + cos(deg2rad($point->getY()))*cos(deg2rad($next_point->getY()))* |
|
119 | 119 | cos(deg2rad(abs($point->getX() - $next_point->getX()))) |
120 | 120 | ) |
121 | 121 | ); |
@@ -130,8 +130,8 @@ discard block |
||
130 | 130 | $points = $this->getPoints(); |
131 | 131 | |
132 | 132 | foreach ($points as $i => $point) { |
133 | - if (isset($points[$i+1])) { |
|
134 | - $parts[] = new LineString(array($point, $points[$i+1])); |
|
133 | + if (isset($points[$i + 1])) { |
|
134 | + $parts[] = new LineString(array($point, $points[$i + 1])); |
|
135 | 135 | } |
136 | 136 | } |
137 | 137 | return $parts; |
@@ -168,18 +168,18 @@ discard block |
||
168 | 168 | $p3_x = $segment->endPoint()->x(); |
169 | 169 | $p3_y = $segment->endPoint()->y(); |
170 | 170 | |
171 | - $s1_x = $p1_x - $p0_x; $s1_y = $p1_y - $p0_y; |
|
172 | - $s2_x = $p3_x - $p2_x; $s2_y = $p3_y - $p2_y; |
|
171 | + $s1_x = $p1_x - $p0_x; $s1_y = $p1_y - $p0_y; |
|
172 | + $s2_x = $p3_x - $p2_x; $s2_y = $p3_y - $p2_y; |
|
173 | 173 | |
174 | - $fps = (-$s2_x * $s1_y) + ($s1_x * $s2_y); |
|
175 | - $fpt = (-$s2_x * $s1_y) + ($s1_x * $s2_y); |
|
174 | + $fps = (-$s2_x*$s1_y) + ($s1_x*$s2_y); |
|
175 | + $fpt = (-$s2_x*$s1_y) + ($s1_x*$s2_y); |
|
176 | 176 | |
177 | 177 | if ($fps == 0 || $fpt == 0) { |
178 | 178 | return FALSE; |
179 | 179 | } |
180 | 180 | |
181 | - $s = (-$s1_y * ($p0_x - $p2_x) + $s1_x * ($p0_y - $p2_y)) / $fps; |
|
182 | - $t = ( $s2_x * ($p0_y - $p2_y) - $s2_y * ($p0_x - $p2_x)) / $fpt; |
|
181 | + $s = (-$s1_y*($p0_x - $p2_x) + $s1_x*($p0_y - $p2_y))/$fps; |
|
182 | + $t = ($s2_x*($p0_y - $p2_y) - $s2_y*($p0_x - $p2_x))/$fpt; |
|
183 | 183 | |
184 | 184 | if ($s > 0 && $s < 1 && $t > 0 && $t < 1) { |
185 | 185 | // Collision detected |
@@ -54,7 +54,9 @@ |
||
54 | 54 | } |
55 | 55 | |
56 | 56 | public function dimension() { |
57 | - if ($this->isEmpty()) return 0; |
|
57 | + if ($this->isEmpty()) { |
|
58 | + return 0; |
|
59 | + } |
|
58 | 60 | return 1; |
59 | 61 | } |
60 | 62 |
@@ -46,38 +46,38 @@ discard block |
||
46 | 46 | // Public: Standard -- Common to all geometries |
47 | 47 | // -------------------------------------------- |
48 | 48 | public function SRID() { |
49 | - return $this->srid; |
|
49 | + return $this->srid; |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | public function setSRID($srid) { |
53 | - if ($this->geos()) { |
|
54 | - $this->geos()->setSRID($srid); |
|
55 | - } |
|
56 | - $this->srid = $srid; |
|
53 | + if ($this->geos()) { |
|
54 | + $this->geos()->setSRID($srid); |
|
55 | + } |
|
56 | + $this->srid = $srid; |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | public function envelope() { |
60 | - if ($this->isEmpty()) return new Polygon(); |
|
60 | + if ($this->isEmpty()) return new Polygon(); |
|
61 | 61 | |
62 | - if ($this->geos()) { |
|
63 | - return geoPHP::geosToGeometry($this->geos()->envelope()); |
|
64 | - } |
|
62 | + if ($this->geos()) { |
|
63 | + return geoPHP::geosToGeometry($this->geos()->envelope()); |
|
64 | + } |
|
65 | 65 | |
66 | - $bbox = $this->getBBox(); |
|
67 | - $points = array ( |
|
68 | - new Point($bbox['maxx'],$bbox['miny']), |
|
69 | - new Point($bbox['maxx'],$bbox['maxy']), |
|
70 | - new Point($bbox['minx'],$bbox['maxy']), |
|
71 | - new Point($bbox['minx'],$bbox['miny']), |
|
72 | - new Point($bbox['maxx'],$bbox['miny']), |
|
73 | - ); |
|
66 | + $bbox = $this->getBBox(); |
|
67 | + $points = array ( |
|
68 | + new Point($bbox['maxx'],$bbox['miny']), |
|
69 | + new Point($bbox['maxx'],$bbox['maxy']), |
|
70 | + new Point($bbox['minx'],$bbox['maxy']), |
|
71 | + new Point($bbox['minx'],$bbox['miny']), |
|
72 | + new Point($bbox['maxx'],$bbox['miny']), |
|
73 | + ); |
|
74 | 74 | |
75 | - $outer_boundary = new LineString($points); |
|
76 | - return new Polygon(array($outer_boundary)); |
|
75 | + $outer_boundary = new LineString($points); |
|
76 | + return new Polygon(array($outer_boundary)); |
|
77 | 77 | } |
78 | 78 | |
79 | 79 | public function geometryType() { |
80 | - return $this->geom_type; |
|
80 | + return $this->geom_type; |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | // Public: Non-Standard -- Common to all geometries |
@@ -85,263 +85,263 @@ discard block |
||
85 | 85 | |
86 | 86 | // $this->out($format, $other_args); |
87 | 87 | public function out() { |
88 | - $args = func_get_args(); |
|
88 | + $args = func_get_args(); |
|
89 | 89 | |
90 | - $format = array_shift($args); |
|
91 | - $type_map = geoPHP::getAdapterMap(); |
|
92 | - $processor_type = $type_map[$format]; |
|
93 | - $processor = new $processor_type(); |
|
90 | + $format = array_shift($args); |
|
91 | + $type_map = geoPHP::getAdapterMap(); |
|
92 | + $processor_type = $type_map[$format]; |
|
93 | + $processor = new $processor_type(); |
|
94 | 94 | |
95 | - array_unshift($args, $this); |
|
96 | - $result = call_user_func_array(array($processor, 'write'), $args); |
|
95 | + array_unshift($args, $this); |
|
96 | + $result = call_user_func_array(array($processor, 'write'), $args); |
|
97 | 97 | |
98 | - return $result; |
|
98 | + return $result; |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | |
102 | 102 | // Public: Aliases |
103 | 103 | // --------------- |
104 | 104 | public function getCentroid() { |
105 | - return $this->centroid(); |
|
105 | + return $this->centroid(); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | public function getArea() { |
109 | - return $this->area(); |
|
109 | + return $this->area(); |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | public function getX() { |
113 | - return $this->x(); |
|
113 | + return $this->x(); |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | public function getY() { |
117 | - return $this->y(); |
|
117 | + return $this->y(); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | public function getGeos() { |
121 | - return $this->geos(); |
|
121 | + return $this->geos(); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | public function getGeomType() { |
125 | - return $this->geometryType(); |
|
125 | + return $this->geometryType(); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | public function getSRID() { |
129 | - return $this->SRID(); |
|
129 | + return $this->SRID(); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | public function asText() { |
133 | - return $this->out('wkt'); |
|
133 | + return $this->out('wkt'); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | public function asBinary() { |
137 | - return $this->out('wkb'); |
|
137 | + return $this->out('wkb'); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | // Public: GEOS Only Functions |
141 | 141 | // --------------------------- |
142 | 142 | public function geos() { |
143 | - // If it's already been set, just return it |
|
144 | - if ($this->geos && geoPHP::geosInstalled()) { |
|
145 | - return $this->geos; |
|
146 | - } |
|
147 | - // It hasn't been set yet, generate it |
|
148 | - if (geoPHP::geosInstalled()) { |
|
149 | - $reader = new GEOSWKBReader(); |
|
150 | - $this->geos = $reader->readHEX($this->out('wkb',TRUE)); |
|
151 | - } |
|
152 | - else { |
|
153 | - $this->geos = FALSE; |
|
154 | - } |
|
155 | - return $this->geos; |
|
143 | + // If it's already been set, just return it |
|
144 | + if ($this->geos && geoPHP::geosInstalled()) { |
|
145 | + return $this->geos; |
|
146 | + } |
|
147 | + // It hasn't been set yet, generate it |
|
148 | + if (geoPHP::geosInstalled()) { |
|
149 | + $reader = new GEOSWKBReader(); |
|
150 | + $this->geos = $reader->readHEX($this->out('wkb',TRUE)); |
|
151 | + } |
|
152 | + else { |
|
153 | + $this->geos = FALSE; |
|
154 | + } |
|
155 | + return $this->geos; |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | public function setGeos($geos) { |
159 | - $this->geos = $geos; |
|
159 | + $this->geos = $geos; |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | public function pointOnSurface() { |
163 | - if ($this->geos()) { |
|
164 | - return geoPHP::geosToGeometry($this->geos()->pointOnSurface()); |
|
165 | - } |
|
163 | + if ($this->geos()) { |
|
164 | + return geoPHP::geosToGeometry($this->geos()->pointOnSurface()); |
|
165 | + } |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | public function equalsExact(Geometry $geometry) { |
169 | - if ($this->geos()) { |
|
170 | - return $this->geos()->equalsExact($geometry->geos()); |
|
171 | - } |
|
169 | + if ($this->geos()) { |
|
170 | + return $this->geos()->equalsExact($geometry->geos()); |
|
171 | + } |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | public function relate(Geometry $geometry, $pattern = NULL) { |
175 | - if ($this->geos()) { |
|
176 | - if ($pattern) { |
|
177 | - return $this->geos()->relate($geometry->geos(), $pattern); |
|
178 | - } |
|
179 | - else { |
|
180 | - return $this->geos()->relate($geometry->geos()); |
|
181 | - } |
|
182 | - } |
|
175 | + if ($this->geos()) { |
|
176 | + if ($pattern) { |
|
177 | + return $this->geos()->relate($geometry->geos(), $pattern); |
|
178 | + } |
|
179 | + else { |
|
180 | + return $this->geos()->relate($geometry->geos()); |
|
181 | + } |
|
182 | + } |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | public function checkValidity() { |
186 | - if ($this->geos()) { |
|
187 | - return $this->geos()->checkValidity(); |
|
188 | - } |
|
186 | + if ($this->geos()) { |
|
187 | + return $this->geos()->checkValidity(); |
|
188 | + } |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | public function buffer($distance) { |
192 | - if ($this->geos()) { |
|
193 | - return geoPHP::geosToGeometry($this->geos()->buffer($distance)); |
|
194 | - } |
|
192 | + if ($this->geos()) { |
|
193 | + return geoPHP::geosToGeometry($this->geos()->buffer($distance)); |
|
194 | + } |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | public function intersection(Geometry $geometry) { |
198 | - if ($this->geos()) { |
|
199 | - return geoPHP::geosToGeometry($this->geos()->intersection($geometry->geos())); |
|
200 | - } |
|
198 | + if ($this->geos()) { |
|
199 | + return geoPHP::geosToGeometry($this->geos()->intersection($geometry->geos())); |
|
200 | + } |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | public function convexHull() { |
204 | - if ($this->geos()) { |
|
205 | - return geoPHP::geosToGeometry($this->geos()->convexHull()); |
|
206 | - } |
|
204 | + if ($this->geos()) { |
|
205 | + return geoPHP::geosToGeometry($this->geos()->convexHull()); |
|
206 | + } |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | public function difference(Geometry $geometry) { |
210 | - if ($this->geos()) { |
|
211 | - return geoPHP::geosToGeometry($this->geos()->difference($geometry->geos())); |
|
212 | - } |
|
210 | + if ($this->geos()) { |
|
211 | + return geoPHP::geosToGeometry($this->geos()->difference($geometry->geos())); |
|
212 | + } |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | public function symDifference(Geometry $geometry) { |
216 | - if ($this->geos()) { |
|
217 | - return geoPHP::geosToGeometry($this->geos()->symDifference($geometry->geos())); |
|
218 | - } |
|
216 | + if ($this->geos()) { |
|
217 | + return geoPHP::geosToGeometry($this->geos()->symDifference($geometry->geos())); |
|
218 | + } |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | // Can pass in a geometry or an array of geometries |
222 | 222 | public function union(Geometry $geometry) { |
223 | - if ($this->geos()) { |
|
224 | - if (is_array($geometry)) { |
|
225 | - $geom = $this->geos(); |
|
226 | - foreach ($geometry as $item) { |
|
227 | - $geom = $geom->union($item->geos()); |
|
228 | - } |
|
229 | - return geoPHP::geosToGeometry($geom); |
|
230 | - } |
|
231 | - else { |
|
232 | - return geoPHP::geosToGeometry($this->geos()->union($geometry->geos())); |
|
233 | - } |
|
234 | - } |
|
223 | + if ($this->geos()) { |
|
224 | + if (is_array($geometry)) { |
|
225 | + $geom = $this->geos(); |
|
226 | + foreach ($geometry as $item) { |
|
227 | + $geom = $geom->union($item->geos()); |
|
228 | + } |
|
229 | + return geoPHP::geosToGeometry($geom); |
|
230 | + } |
|
231 | + else { |
|
232 | + return geoPHP::geosToGeometry($this->geos()->union($geometry->geos())); |
|
233 | + } |
|
234 | + } |
|
235 | 235 | } |
236 | 236 | |
237 | 237 | public function simplify($tolerance, $preserveTopology = FALSE) { |
238 | - if ($this->geos()) { |
|
239 | - return geoPHP::geosToGeometry($this->geos()->simplify($tolerance, $preserveTopology)); |
|
240 | - } |
|
238 | + if ($this->geos()) { |
|
239 | + return geoPHP::geosToGeometry($this->geos()->simplify($tolerance, $preserveTopology)); |
|
240 | + } |
|
241 | 241 | } |
242 | 242 | |
243 | 243 | public function disjoint(Geometry $geometry) { |
244 | - if ($this->geos()) { |
|
245 | - return $this->geos()->disjoint($geometry->geos()); |
|
246 | - } |
|
244 | + if ($this->geos()) { |
|
245 | + return $this->geos()->disjoint($geometry->geos()); |
|
246 | + } |
|
247 | 247 | } |
248 | 248 | |
249 | 249 | public function touches(Geometry $geometry) { |
250 | - if ($this->geos()) { |
|
251 | - return $this->geos()->touches($geometry->geos()); |
|
252 | - } |
|
250 | + if ($this->geos()) { |
|
251 | + return $this->geos()->touches($geometry->geos()); |
|
252 | + } |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | public function intersects(Geometry $geometry) { |
256 | - if ($this->geos()) { |
|
257 | - return $this->geos()->intersects($geometry->geos()); |
|
258 | - } |
|
256 | + if ($this->geos()) { |
|
257 | + return $this->geos()->intersects($geometry->geos()); |
|
258 | + } |
|
259 | 259 | } |
260 | 260 | |
261 | 261 | public function crosses(Geometry $geometry) { |
262 | - if ($this->geos()) { |
|
263 | - return $this->geos()->crosses($geometry->geos()); |
|
264 | - } |
|
262 | + if ($this->geos()) { |
|
263 | + return $this->geos()->crosses($geometry->geos()); |
|
264 | + } |
|
265 | 265 | } |
266 | 266 | |
267 | 267 | public function within(Geometry $geometry) { |
268 | - if ($this->geos()) { |
|
269 | - return $this->geos()->within($geometry->geos()); |
|
270 | - } |
|
268 | + if ($this->geos()) { |
|
269 | + return $this->geos()->within($geometry->geos()); |
|
270 | + } |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | public function contains(Geometry $geometry) { |
274 | - if ($this->geos()) { |
|
275 | - return $this->geos()->contains($geometry->geos()); |
|
276 | - } |
|
274 | + if ($this->geos()) { |
|
275 | + return $this->geos()->contains($geometry->geos()); |
|
276 | + } |
|
277 | 277 | } |
278 | 278 | |
279 | 279 | public function overlaps(Geometry $geometry) { |
280 | - if ($this->geos()) { |
|
281 | - return $this->geos()->overlaps($geometry->geos()); |
|
282 | - } |
|
280 | + if ($this->geos()) { |
|
281 | + return $this->geos()->overlaps($geometry->geos()); |
|
282 | + } |
|
283 | 283 | } |
284 | 284 | |
285 | 285 | public function covers(Geometry $geometry) { |
286 | - if ($this->geos()) { |
|
287 | - return $this->geos()->covers($geometry->geos()); |
|
288 | - } |
|
286 | + if ($this->geos()) { |
|
287 | + return $this->geos()->covers($geometry->geos()); |
|
288 | + } |
|
289 | 289 | } |
290 | 290 | |
291 | 291 | public function coveredBy(Geometry $geometry) { |
292 | - if ($this->geos()) { |
|
293 | - return $this->geos()->coveredBy($geometry->geos()); |
|
294 | - } |
|
292 | + if ($this->geos()) { |
|
293 | + return $this->geos()->coveredBy($geometry->geos()); |
|
294 | + } |
|
295 | 295 | } |
296 | 296 | |
297 | 297 | public function distance(Geometry $geometry) { |
298 | - if ($this->geos()) { |
|
299 | - return $this->geos()->distance($geometry->geos()); |
|
300 | - } |
|
298 | + if ($this->geos()) { |
|
299 | + return $this->geos()->distance($geometry->geos()); |
|
300 | + } |
|
301 | 301 | } |
302 | 302 | |
303 | 303 | public function hausdorffDistance(Geometry $geometry) { |
304 | - if ($this->geos()) { |
|
305 | - return $this->geos()->hausdorffDistance($geometry->geos()); |
|
306 | - } |
|
304 | + if ($this->geos()) { |
|
305 | + return $this->geos()->hausdorffDistance($geometry->geos()); |
|
306 | + } |
|
307 | 307 | } |
308 | 308 | |
309 | 309 | public function project(Geometry $point, $normalized = NULL) { |
310 | - if ($this->geos()) { |
|
311 | - return $this->geos()->project($point->geos(), $normalized); |
|
312 | - } |
|
310 | + if ($this->geos()) { |
|
311 | + return $this->geos()->project($point->geos(), $normalized); |
|
312 | + } |
|
313 | 313 | } |
314 | 314 | |
315 | 315 | // Public - Placeholders |
316 | 316 | // --------------------- |
317 | 317 | public function hasZ() { |
318 | - // geoPHP does not support Z values at the moment |
|
319 | - return FALSE; |
|
318 | + // geoPHP does not support Z values at the moment |
|
319 | + return FALSE; |
|
320 | 320 | } |
321 | 321 | |
322 | 322 | public function is3D() { |
323 | - // geoPHP does not support 3D geometries at the moment |
|
324 | - return FALSE; |
|
323 | + // geoPHP does not support 3D geometries at the moment |
|
324 | + return FALSE; |
|
325 | 325 | } |
326 | 326 | |
327 | 327 | public function isMeasured() { |
328 | - // geoPHP does not yet support M values |
|
329 | - return FALSE; |
|
328 | + // geoPHP does not yet support M values |
|
329 | + return FALSE; |
|
330 | 330 | } |
331 | 331 | |
332 | 332 | public function coordinateDimension() { |
333 | - // geoPHP only supports 2-dimentional space |
|
334 | - return 2; |
|
333 | + // geoPHP only supports 2-dimentional space |
|
334 | + return 2; |
|
335 | 335 | } |
336 | 336 | |
337 | 337 | public function z() { |
338 | - // geoPHP only supports 2-dimentional space |
|
339 | - return NULL; |
|
338 | + // geoPHP only supports 2-dimentional space |
|
339 | + return NULL; |
|
340 | 340 | } |
341 | 341 | |
342 | 342 | public function m() { |
343 | - // geoPHP only supports 2-dimentional space |
|
344 | - return NULL; |
|
343 | + // geoPHP only supports 2-dimentional space |
|
344 | + return NULL; |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | } |
@@ -21,8 +21,8 @@ discard block |
||
21 | 21 | abstract public function geometryN($n); |
22 | 22 | abstract public function startPoint(); |
23 | 23 | abstract public function endPoint(); |
24 | - abstract public function isRing(); // Mssing dependancy |
|
25 | - abstract public function isClosed(); // Missing dependancy |
|
24 | + abstract public function isRing(); // Mssing dependancy |
|
25 | + abstract public function isClosed(); // Missing dependancy |
|
26 | 26 | abstract public function numPoints(); |
27 | 27 | abstract public function pointN($n); |
28 | 28 | abstract public function exteriorRing(); |
@@ -64,12 +64,12 @@ discard block |
||
64 | 64 | } |
65 | 65 | |
66 | 66 | $bbox = $this->getBBox(); |
67 | - $points = array ( |
|
68 | - new Point($bbox['maxx'],$bbox['miny']), |
|
69 | - new Point($bbox['maxx'],$bbox['maxy']), |
|
70 | - new Point($bbox['minx'],$bbox['maxy']), |
|
71 | - new Point($bbox['minx'],$bbox['miny']), |
|
72 | - new Point($bbox['maxx'],$bbox['miny']), |
|
67 | + $points = array( |
|
68 | + new Point($bbox['maxx'], $bbox['miny']), |
|
69 | + new Point($bbox['maxx'], $bbox['maxy']), |
|
70 | + new Point($bbox['minx'], $bbox['maxy']), |
|
71 | + new Point($bbox['minx'], $bbox['miny']), |
|
72 | + new Point($bbox['maxx'], $bbox['miny']), |
|
73 | 73 | ); |
74 | 74 | |
75 | 75 | $outer_boundary = new LineString($points); |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | // It hasn't been set yet, generate it |
148 | 148 | if (geoPHP::geosInstalled()) { |
149 | 149 | $reader = new GEOSWKBReader(); |
150 | - $this->geos = $reader->readHEX($this->out('wkb',TRUE)); |
|
150 | + $this->geos = $reader->readHEX($this->out('wkb', TRUE)); |
|
151 | 151 | } |
152 | 152 | else { |
153 | 153 | $this->geos = FALSE; |
@@ -57,7 +57,9 @@ discard block |
||
57 | 57 | } |
58 | 58 | |
59 | 59 | public function envelope() { |
60 | - if ($this->isEmpty()) return new Polygon(); |
|
60 | + if ($this->isEmpty()) { |
|
61 | + return new Polygon(); |
|
62 | + } |
|
61 | 63 | |
62 | 64 | if ($this->geos()) { |
63 | 65 | return geoPHP::geosToGeometry($this->geos()->envelope()); |
@@ -148,8 +150,7 @@ discard block |
||
148 | 150 | if (geoPHP::geosInstalled()) { |
149 | 151 | $reader = new GEOSWKBReader(); |
150 | 152 | $this->geos = $reader->readHEX($this->out('wkb',TRUE)); |
151 | - } |
|
152 | - else { |
|
153 | + } else { |
|
153 | 154 | $this->geos = FALSE; |
154 | 155 | } |
155 | 156 | return $this->geos; |
@@ -175,8 +176,7 @@ discard block |
||
175 | 176 | if ($this->geos()) { |
176 | 177 | if ($pattern) { |
177 | 178 | return $this->geos()->relate($geometry->geos(), $pattern); |
178 | - } |
|
179 | - else { |
|
179 | + } else { |
|
180 | 180 | return $this->geos()->relate($geometry->geos()); |
181 | 181 | } |
182 | 182 | } |
@@ -227,8 +227,7 @@ discard block |
||
227 | 227 | $geom = $geom->union($item->geos()); |
228 | 228 | } |
229 | 229 | return geoPHP::geosToGeometry($geom); |
230 | - } |
|
231 | - else { |
|
230 | + } else { |
|
232 | 231 | return geoPHP::geosToGeometry($this->geos()->union($geometry->geos())); |
233 | 232 | } |
234 | 233 | } |