@@ -29,120 +29,120 @@ discard block |
||
| 29 | 29 | * @return Geometry |
| 30 | 30 | */ |
| 31 | 31 | public function read($wkb, $is_hex_string = FALSE) {
|
| 32 | - if ($is_hex_string) {
|
|
| 33 | - $wkb = pack('H*',$wkb);
|
|
| 34 | - } |
|
| 32 | + if ($is_hex_string) {
|
|
| 33 | + $wkb = pack('H*',$wkb);
|
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - if (empty($wkb)) {
|
|
| 37 | - throw new Exception('Cannot read empty WKB geometry. Found ' . gettype($wkb));
|
|
| 38 | - } |
|
| 36 | + if (empty($wkb)) {
|
|
| 37 | + throw new Exception('Cannot read empty WKB geometry. Found ' . gettype($wkb));
|
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - $mem = fopen('php://memory', 'r+');
|
|
| 41 | - fwrite($mem, $wkb); |
|
| 42 | - fseek($mem, 0); |
|
| 40 | + $mem = fopen('php://memory', 'r+');
|
|
| 41 | + fwrite($mem, $wkb); |
|
| 42 | + fseek($mem, 0); |
|
| 43 | 43 | |
| 44 | - $geometry = $this->getGeometry($mem); |
|
| 45 | - fclose($mem); |
|
| 46 | - return $geometry; |
|
| 44 | + $geometry = $this->getGeometry($mem); |
|
| 45 | + fclose($mem); |
|
| 46 | + return $geometry; |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | function getGeometry(&$mem) {
|
| 50 | - $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5));
|
|
| 51 | - if ($base_info['order'] !== 1) {
|
|
| 52 | - throw new Exception('Only NDR (little endian) SKB format is supported at the moment');
|
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - if ($base_info['z']) {
|
|
| 56 | - $this->dimension++; |
|
| 57 | - $this->z = TRUE; |
|
| 58 | - } |
|
| 59 | - if ($base_info['m']) {
|
|
| 60 | - $this->dimension++; |
|
| 61 | - $this->m = TRUE; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // If there is SRID information, ignore it - use EWKB Adapter to get SRID support |
|
| 65 | - if ($base_info['s']) {
|
|
| 66 | - fread($mem, 4); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - switch ($base_info['type']) {
|
|
| 70 | - case 1: |
|
| 71 | - return $this->getPoint($mem); |
|
| 72 | - case 2: |
|
| 73 | - return $this->getLinstring($mem); |
|
| 74 | - case 3: |
|
| 75 | - return $this->getPolygon($mem); |
|
| 76 | - case 4: |
|
| 77 | - return $this->getMulti($mem,'point'); |
|
| 78 | - case 5: |
|
| 79 | - return $this->getMulti($mem,'line'); |
|
| 80 | - case 6: |
|
| 81 | - return $this->getMulti($mem,'polygon'); |
|
| 82 | - case 7: |
|
| 83 | - return $this->getMulti($mem,'geometry'); |
|
| 84 | - } |
|
| 50 | + $base_info = unpack("corder/ctype/cz/cm/cs", fread($mem, 5));
|
|
| 51 | + if ($base_info['order'] !== 1) {
|
|
| 52 | + throw new Exception('Only NDR (little endian) SKB format is supported at the moment');
|
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + if ($base_info['z']) {
|
|
| 56 | + $this->dimension++; |
|
| 57 | + $this->z = TRUE; |
|
| 58 | + } |
|
| 59 | + if ($base_info['m']) {
|
|
| 60 | + $this->dimension++; |
|
| 61 | + $this->m = TRUE; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // If there is SRID information, ignore it - use EWKB Adapter to get SRID support |
|
| 65 | + if ($base_info['s']) {
|
|
| 66 | + fread($mem, 4); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + switch ($base_info['type']) {
|
|
| 70 | + case 1: |
|
| 71 | + return $this->getPoint($mem); |
|
| 72 | + case 2: |
|
| 73 | + return $this->getLinstring($mem); |
|
| 74 | + case 3: |
|
| 75 | + return $this->getPolygon($mem); |
|
| 76 | + case 4: |
|
| 77 | + return $this->getMulti($mem,'point'); |
|
| 78 | + case 5: |
|
| 79 | + return $this->getMulti($mem,'line'); |
|
| 80 | + case 6: |
|
| 81 | + return $this->getMulti($mem,'polygon'); |
|
| 82 | + case 7: |
|
| 83 | + return $this->getMulti($mem,'geometry'); |
|
| 84 | + } |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | function getPoint(&$mem) {
|
| 88 | - $point_coords = unpack("d*", fread($mem,$this->dimension*8));
|
|
| 89 | - return new Point($point_coords[1],$point_coords[2]); |
|
| 88 | + $point_coords = unpack("d*", fread($mem,$this->dimension*8));
|
|
| 89 | + return new Point($point_coords[1],$point_coords[2]); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | function getLinstring(&$mem) {
|
| 93 | - // Get the number of points expected in this string out of the first 4 bytes |
|
| 94 | - $line_length = unpack('L',fread($mem,4));
|
|
| 95 | - |
|
| 96 | - // Return an empty linestring if there is no line-length |
|
| 97 | - if (!$line_length[1]) return new LineString(); |
|
| 98 | - |
|
| 99 | - // Read the nubmer of points x2 (each point is two coords) into decimal-floats |
|
| 100 | - $line_coords = unpack('d*', fread($mem,$line_length[1]*$this->dimension*8));
|
|
| 101 | - |
|
| 102 | - // We have our coords, build up the linestring |
|
| 103 | - $components = array(); |
|
| 104 | - $i = 1; |
|
| 105 | - $num_coords = count($line_coords); |
|
| 106 | - while ($i <= $num_coords) {
|
|
| 107 | - $components[] = new Point($line_coords[$i],$line_coords[$i+1]); |
|
| 108 | - $i += 2; |
|
| 109 | - } |
|
| 110 | - return new LineString($components); |
|
| 93 | + // Get the number of points expected in this string out of the first 4 bytes |
|
| 94 | + $line_length = unpack('L',fread($mem,4));
|
|
| 95 | + |
|
| 96 | + // Return an empty linestring if there is no line-length |
|
| 97 | + if (!$line_length[1]) return new LineString(); |
|
| 98 | + |
|
| 99 | + // Read the nubmer of points x2 (each point is two coords) into decimal-floats |
|
| 100 | + $line_coords = unpack('d*', fread($mem,$line_length[1]*$this->dimension*8));
|
|
| 101 | + |
|
| 102 | + // We have our coords, build up the linestring |
|
| 103 | + $components = array(); |
|
| 104 | + $i = 1; |
|
| 105 | + $num_coords = count($line_coords); |
|
| 106 | + while ($i <= $num_coords) {
|
|
| 107 | + $components[] = new Point($line_coords[$i],$line_coords[$i+1]); |
|
| 108 | + $i += 2; |
|
| 109 | + } |
|
| 110 | + return new LineString($components); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | function getPolygon(&$mem) {
|
| 114 | - // Get the number of linestring expected in this poly out of the first 4 bytes |
|
| 115 | - $poly_length = unpack('L',fread($mem,4));
|
|
| 116 | - |
|
| 117 | - $components = array(); |
|
| 118 | - $i = 1; |
|
| 119 | - while ($i <= $poly_length[1]) {
|
|
| 120 | - $components[] = $this->getLinstring($mem); |
|
| 121 | - $i++; |
|
| 122 | - } |
|
| 123 | - return new Polygon($components); |
|
| 114 | + // Get the number of linestring expected in this poly out of the first 4 bytes |
|
| 115 | + $poly_length = unpack('L',fread($mem,4));
|
|
| 116 | + |
|
| 117 | + $components = array(); |
|
| 118 | + $i = 1; |
|
| 119 | + while ($i <= $poly_length[1]) {
|
|
| 120 | + $components[] = $this->getLinstring($mem); |
|
| 121 | + $i++; |
|
| 122 | + } |
|
| 123 | + return new Polygon($components); |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | function getMulti(&$mem, $type) {
|
| 127 | - // Get the number of items expected in this multi out of the first 4 bytes |
|
| 128 | - $multi_length = unpack('L',fread($mem,4));
|
|
| 129 | - |
|
| 130 | - $components = array(); |
|
| 131 | - $i = 1; |
|
| 132 | - while ($i <= $multi_length[1]) {
|
|
| 133 | - $components[] = $this->getGeometry($mem); |
|
| 134 | - $i++; |
|
| 135 | - } |
|
| 136 | - switch ($type) {
|
|
| 137 | - case 'point': |
|
| 138 | - return new MultiPoint($components); |
|
| 139 | - case 'line': |
|
| 140 | - return new MultiLineString($components); |
|
| 141 | - case 'polygon': |
|
| 142 | - return new MultiPolygon($components); |
|
| 143 | - case 'geometry': |
|
| 144 | - return new GeometryCollection($components); |
|
| 145 | - } |
|
| 127 | + // Get the number of items expected in this multi out of the first 4 bytes |
|
| 128 | + $multi_length = unpack('L',fread($mem,4));
|
|
| 129 | + |
|
| 130 | + $components = array(); |
|
| 131 | + $i = 1; |
|
| 132 | + while ($i <= $multi_length[1]) {
|
|
| 133 | + $components[] = $this->getGeometry($mem); |
|
| 134 | + $i++; |
|
| 135 | + } |
|
| 136 | + switch ($type) {
|
|
| 137 | + case 'point': |
|
| 138 | + return new MultiPoint($components); |
|
| 139 | + case 'line': |
|
| 140 | + return new MultiLineString($components); |
|
| 141 | + case 'polygon': |
|
| 142 | + return new MultiPolygon($components); |
|
| 143 | + case 'geometry': |
|
| 144 | + return new GeometryCollection($components); |
|
| 145 | + } |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | /** |
@@ -153,90 +153,90 @@ discard block |
||
| 153 | 153 | * @return string The WKB string representation of the input geometries |
| 154 | 154 | */ |
| 155 | 155 | public function write(Geometry $geometry, $write_as_hex = FALSE) {
|
| 156 | - // We always write into NDR (little endian) |
|
| 157 | - $wkb = pack('c',1);
|
|
| 158 | - |
|
| 159 | - switch ($geometry->getGeomType()) {
|
|
| 160 | - case 'Point'; |
|
| 161 | - $wkb .= pack('L',1);
|
|
| 162 | - $wkb .= $this->writePoint($geometry); |
|
| 163 | - break; |
|
| 164 | - case 'LineString'; |
|
| 165 | - $wkb .= pack('L',2);
|
|
| 166 | - $wkb .= $this->writeLineString($geometry); |
|
| 167 | - break; |
|
| 168 | - case 'Polygon'; |
|
| 169 | - $wkb .= pack('L',3);
|
|
| 170 | - $wkb .= $this->writePolygon($geometry); |
|
| 171 | - break; |
|
| 172 | - case 'MultiPoint'; |
|
| 173 | - $wkb .= pack('L',4);
|
|
| 174 | - $wkb .= $this->writeMulti($geometry); |
|
| 175 | - break; |
|
| 176 | - case 'MultiLineString'; |
|
| 177 | - $wkb .= pack('L',5);
|
|
| 178 | - $wkb .= $this->writeMulti($geometry); |
|
| 179 | - break; |
|
| 180 | - case 'MultiPolygon'; |
|
| 181 | - $wkb .= pack('L',6);
|
|
| 182 | - $wkb .= $this->writeMulti($geometry); |
|
| 183 | - break; |
|
| 184 | - case 'GeometryCollection'; |
|
| 185 | - $wkb .= pack('L',7);
|
|
| 186 | - $wkb .= $this->writeMulti($geometry); |
|
| 187 | - break; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - if ($write_as_hex) {
|
|
| 191 | - $unpacked = unpack('H*',$wkb);
|
|
| 192 | - return $unpacked[1]; |
|
| 193 | - } |
|
| 194 | - else {
|
|
| 195 | - return $wkb; |
|
| 196 | - } |
|
| 156 | + // We always write into NDR (little endian) |
|
| 157 | + $wkb = pack('c',1);
|
|
| 158 | + |
|
| 159 | + switch ($geometry->getGeomType()) {
|
|
| 160 | + case 'Point'; |
|
| 161 | + $wkb .= pack('L',1);
|
|
| 162 | + $wkb .= $this->writePoint($geometry); |
|
| 163 | + break; |
|
| 164 | + case 'LineString'; |
|
| 165 | + $wkb .= pack('L',2);
|
|
| 166 | + $wkb .= $this->writeLineString($geometry); |
|
| 167 | + break; |
|
| 168 | + case 'Polygon'; |
|
| 169 | + $wkb .= pack('L',3);
|
|
| 170 | + $wkb .= $this->writePolygon($geometry); |
|
| 171 | + break; |
|
| 172 | + case 'MultiPoint'; |
|
| 173 | + $wkb .= pack('L',4);
|
|
| 174 | + $wkb .= $this->writeMulti($geometry); |
|
| 175 | + break; |
|
| 176 | + case 'MultiLineString'; |
|
| 177 | + $wkb .= pack('L',5);
|
|
| 178 | + $wkb .= $this->writeMulti($geometry); |
|
| 179 | + break; |
|
| 180 | + case 'MultiPolygon'; |
|
| 181 | + $wkb .= pack('L',6);
|
|
| 182 | + $wkb .= $this->writeMulti($geometry); |
|
| 183 | + break; |
|
| 184 | + case 'GeometryCollection'; |
|
| 185 | + $wkb .= pack('L',7);
|
|
| 186 | + $wkb .= $this->writeMulti($geometry); |
|
| 187 | + break; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + if ($write_as_hex) {
|
|
| 191 | + $unpacked = unpack('H*',$wkb);
|
|
| 192 | + return $unpacked[1]; |
|
| 193 | + } |
|
| 194 | + else {
|
|
| 195 | + return $wkb; |
|
| 196 | + } |
|
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | function writePoint($point) {
|
| 200 | - // Set the coords |
|
| 201 | - $wkb = pack('dd',$point->x(), $point->y());
|
|
| 200 | + // Set the coords |
|
| 201 | + $wkb = pack('dd',$point->x(), $point->y());
|
|
| 202 | 202 | |
| 203 | - return $wkb; |
|
| 203 | + return $wkb; |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | function writeLineString($line) {
|
| 207 | - // Set the number of points in this line |
|
| 208 | - $wkb = pack('L',$line->numPoints());
|
|
| 207 | + // Set the number of points in this line |
|
| 208 | + $wkb = pack('L',$line->numPoints());
|
|
| 209 | 209 | |
| 210 | - // Set the coords |
|
| 211 | - foreach ($line->getComponents() as $point) {
|
|
| 212 | - $wkb .= pack('dd',$point->x(), $point->y());
|
|
| 213 | - } |
|
| 210 | + // Set the coords |
|
| 211 | + foreach ($line->getComponents() as $point) {
|
|
| 212 | + $wkb .= pack('dd',$point->x(), $point->y());
|
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - return $wkb; |
|
| 215 | + return $wkb; |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | function writePolygon($poly) {
|
| 219 | - // Set the number of lines in this poly |
|
| 220 | - $wkb = pack('L',$poly->numGeometries());
|
|
| 219 | + // Set the number of lines in this poly |
|
| 220 | + $wkb = pack('L',$poly->numGeometries());
|
|
| 221 | 221 | |
| 222 | - // Write the lines |
|
| 223 | - foreach ($poly->getComponents() as $line) {
|
|
| 224 | - $wkb .= $this->writeLineString($line); |
|
| 225 | - } |
|
| 222 | + // Write the lines |
|
| 223 | + foreach ($poly->getComponents() as $line) {
|
|
| 224 | + $wkb .= $this->writeLineString($line); |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - return $wkb; |
|
| 227 | + return $wkb; |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | function writeMulti($geometry) {
|
| 231 | - // Set the number of components |
|
| 232 | - $wkb = pack('L',$geometry->numGeometries());
|
|
| 231 | + // Set the number of components |
|
| 232 | + $wkb = pack('L',$geometry->numGeometries());
|
|
| 233 | 233 | |
| 234 | - // Write the components |
|
| 235 | - foreach ($geometry->getComponents() as $component) {
|
|
| 236 | - $wkb .= $this->write($component); |
|
| 237 | - } |
|
| 234 | + // Write the components |
|
| 235 | + foreach ($geometry->getComponents() as $component) {
|
|
| 236 | + $wkb .= $this->write($component); |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - return $wkb; |
|
| 239 | + return $wkb; |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @return Geometry|GeometryCollection |
| 32 | 32 | */ |
| 33 | 33 | public function read($kml) {
|
| 34 | - return $this->geomFromText($kml); |
|
| 34 | + return $this->geomFromText($kml); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -42,218 +42,218 @@ discard block |
||
| 42 | 42 | * @return string The KML string representation of the input geometries |
| 43 | 43 | */ |
| 44 | 44 | public function write(Geometry $geometry, $namespace = FALSE) {
|
| 45 | - if ($namespace) {
|
|
| 46 | - $this->namespace = $namespace; |
|
| 47 | - $this->nss = $namespace.':'; |
|
| 48 | - } |
|
| 49 | - return $this->geometryToKML($geometry); |
|
| 45 | + if ($namespace) {
|
|
| 46 | + $this->namespace = $namespace; |
|
| 47 | + $this->nss = $namespace.':'; |
|
| 48 | + } |
|
| 49 | + return $this->geometryToKML($geometry); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | public function geomFromText($text) {
|
| 53 | 53 | |
| 54 | - // Change to lower-case and strip all CDATA |
|
| 55 | - $text = mb_strtolower($text, mb_detect_encoding($text)); |
|
| 56 | - $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);
|
|
| 57 | - |
|
| 58 | - // Load into DOMDOcument |
|
| 59 | - $xmlobj = new DOMDocument(); |
|
| 60 | - @$xmlobj->loadXML($text); |
|
| 61 | - if ($xmlobj === false) {
|
|
| 62 | - throw new Exception("Invalid KML: ". $text);
|
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - $this->xmlobj = $xmlobj; |
|
| 66 | - try {
|
|
| 67 | - $geom = $this->geomFromXML(); |
|
| 68 | - } catch(InvalidText $e) {
|
|
| 69 | - throw new Exception("Cannot Read Geometry From KML: ". $text);
|
|
| 70 | - } catch(Exception $e) {
|
|
| 71 | - throw $e; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return $geom; |
|
| 54 | + // Change to lower-case and strip all CDATA |
|
| 55 | + $text = mb_strtolower($text, mb_detect_encoding($text)); |
|
| 56 | + $text = preg_replace('/<!\[cdata\[(.*?)\]\]>/s','',$text);
|
|
| 57 | + |
|
| 58 | + // Load into DOMDOcument |
|
| 59 | + $xmlobj = new DOMDocument(); |
|
| 60 | + @$xmlobj->loadXML($text); |
|
| 61 | + if ($xmlobj === false) {
|
|
| 62 | + throw new Exception("Invalid KML: ". $text);
|
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + $this->xmlobj = $xmlobj; |
|
| 66 | + try {
|
|
| 67 | + $geom = $this->geomFromXML(); |
|
| 68 | + } catch(InvalidText $e) {
|
|
| 69 | + throw new Exception("Cannot Read Geometry From KML: ". $text);
|
|
| 70 | + } catch(Exception $e) {
|
|
| 71 | + throw $e; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return $geom; |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | protected function geomFromXML() {
|
| 78 | - $geometries = array(); |
|
| 79 | - $geom_types = geoPHP::geometryList(); |
|
| 80 | - $placemark_elements = $this->xmlobj->getElementsByTagName('placemark');
|
|
| 81 | - if ($placemark_elements->length) {
|
|
| 82 | - foreach ($placemark_elements as $placemark) {
|
|
| 83 | - foreach ($placemark->childNodes as $child) {
|
|
| 84 | - // Node names are all the same, except for MultiGeometry, which maps to GeometryCollection |
|
| 85 | - $node_name = $child->nodeName == 'multigeometry' ? 'geometrycollection' : $child->nodeName; |
|
| 86 | - if (array_key_exists($node_name, $geom_types)) {
|
|
| 87 | - $function = 'parse'.$geom_types[$node_name]; |
|
| 88 | - $geometries[] = $this->$function($child); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - else {
|
|
| 94 | - // The document does not have a placemark, try to create a valid geometry from the root element |
|
| 95 | - $node_name = $this->xmlobj->documentElement->nodeName == 'multigeometry' ? 'geometrycollection' : $this->xmlobj->documentElement->nodeName; |
|
| 96 | - if (array_key_exists($node_name, $geom_types)) {
|
|
| 97 | - $function = 'parse'.$geom_types[$node_name]; |
|
| 98 | - $geometries[] = $this->$function($this->xmlobj->documentElement); |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - return geoPHP::geometryReduce($geometries); |
|
| 78 | + $geometries = array(); |
|
| 79 | + $geom_types = geoPHP::geometryList(); |
|
| 80 | + $placemark_elements = $this->xmlobj->getElementsByTagName('placemark');
|
|
| 81 | + if ($placemark_elements->length) {
|
|
| 82 | + foreach ($placemark_elements as $placemark) {
|
|
| 83 | + foreach ($placemark->childNodes as $child) {
|
|
| 84 | + // Node names are all the same, except for MultiGeometry, which maps to GeometryCollection |
|
| 85 | + $node_name = $child->nodeName == 'multigeometry' ? 'geometrycollection' : $child->nodeName; |
|
| 86 | + if (array_key_exists($node_name, $geom_types)) {
|
|
| 87 | + $function = 'parse'.$geom_types[$node_name]; |
|
| 88 | + $geometries[] = $this->$function($child); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + else {
|
|
| 94 | + // The document does not have a placemark, try to create a valid geometry from the root element |
|
| 95 | + $node_name = $this->xmlobj->documentElement->nodeName == 'multigeometry' ? 'geometrycollection' : $this->xmlobj->documentElement->nodeName; |
|
| 96 | + if (array_key_exists($node_name, $geom_types)) {
|
|
| 97 | + $function = 'parse'.$geom_types[$node_name]; |
|
| 98 | + $geometries[] = $this->$function($this->xmlobj->documentElement); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + return geoPHP::geometryReduce($geometries); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | protected function childElements($xml, $nodename = '') {
|
| 105 | - $children = array(); |
|
| 106 | - if ($xml->childNodes) {
|
|
| 107 | - foreach ($xml->childNodes as $child) {
|
|
| 108 | - if ($child->nodeName == $nodename) {
|
|
| 109 | - $children[] = $child; |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - return $children; |
|
| 105 | + $children = array(); |
|
| 106 | + if ($xml->childNodes) {
|
|
| 107 | + foreach ($xml->childNodes as $child) {
|
|
| 108 | + if ($child->nodeName == $nodename) {
|
|
| 109 | + $children[] = $child; |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + return $children; |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | protected function parsePoint($xml) {
|
| 117 | - $coordinates = $this->_extractCoordinates($xml); |
|
| 118 | - return new Point($coordinates[0][0],$coordinates[0][1]); |
|
| 117 | + $coordinates = $this->_extractCoordinates($xml); |
|
| 118 | + return new Point($coordinates[0][0],$coordinates[0][1]); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | protected function parseLineString($xml) {
|
| 122 | - $coordinates = $this->_extractCoordinates($xml); |
|
| 123 | - $point_array = array(); |
|
| 124 | - foreach ($coordinates as $set) {
|
|
| 125 | - $point_array[] = new Point($set[0],$set[1]); |
|
| 126 | - } |
|
| 127 | - return new LineString($point_array); |
|
| 122 | + $coordinates = $this->_extractCoordinates($xml); |
|
| 123 | + $point_array = array(); |
|
| 124 | + foreach ($coordinates as $set) {
|
|
| 125 | + $point_array[] = new Point($set[0],$set[1]); |
|
| 126 | + } |
|
| 127 | + return new LineString($point_array); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | protected function parsePolygon($xml) {
|
| 131 | - $components = array(); |
|
| 132 | - |
|
| 133 | - $outer_boundary_element_a = $this->childElements($xml, 'outerboundaryis'); |
|
| 134 | - $outer_boundary_element = $outer_boundary_element_a[0]; |
|
| 135 | - $outer_ring_element_a = $this->childElements($outer_boundary_element, 'linearring'); |
|
| 136 | - $outer_ring_element = $outer_ring_element_a[0]; |
|
| 137 | - $components[] = $this->parseLineString($outer_ring_element); |
|
| 138 | - |
|
| 139 | - if (count($components) != 1) {
|
|
| 140 | - throw new Exception("Invalid KML");
|
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $inner_boundary_element_a = $this->childElements($xml, 'innerboundaryis'); |
|
| 144 | - if (count($inner_boundary_element_a)) {
|
|
| 145 | - foreach ($inner_boundary_element_a as $inner_boundary_element) {
|
|
| 146 | - foreach ($this->childElements($inner_boundary_element, 'linearring') as $inner_ring_element) {
|
|
| 147 | - $components[] = $this->parseLineString($inner_ring_element); |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return new Polygon($components); |
|
| 131 | + $components = array(); |
|
| 132 | + |
|
| 133 | + $outer_boundary_element_a = $this->childElements($xml, 'outerboundaryis'); |
|
| 134 | + $outer_boundary_element = $outer_boundary_element_a[0]; |
|
| 135 | + $outer_ring_element_a = $this->childElements($outer_boundary_element, 'linearring'); |
|
| 136 | + $outer_ring_element = $outer_ring_element_a[0]; |
|
| 137 | + $components[] = $this->parseLineString($outer_ring_element); |
|
| 138 | + |
|
| 139 | + if (count($components) != 1) {
|
|
| 140 | + throw new Exception("Invalid KML");
|
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $inner_boundary_element_a = $this->childElements($xml, 'innerboundaryis'); |
|
| 144 | + if (count($inner_boundary_element_a)) {
|
|
| 145 | + foreach ($inner_boundary_element_a as $inner_boundary_element) {
|
|
| 146 | + foreach ($this->childElements($inner_boundary_element, 'linearring') as $inner_ring_element) {
|
|
| 147 | + $components[] = $this->parseLineString($inner_ring_element); |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + return new Polygon($components); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | protected function parseGeometryCollection($xml) {
|
| 156 | - $components = array(); |
|
| 157 | - $geom_types = geoPHP::geometryList(); |
|
| 158 | - foreach ($xml->childNodes as $child) {
|
|
| 159 | - $nodeName = ($child->nodeName == 'linearring') ? 'linestring' : $child->nodeName; |
|
| 160 | - if (array_key_exists($nodeName, $geom_types)) {
|
|
| 161 | - $function = 'parse'.$geom_types[$nodeName]; |
|
| 162 | - $components[] = $this->$function($child); |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - return new GeometryCollection($components); |
|
| 156 | + $components = array(); |
|
| 157 | + $geom_types = geoPHP::geometryList(); |
|
| 158 | + foreach ($xml->childNodes as $child) {
|
|
| 159 | + $nodeName = ($child->nodeName == 'linearring') ? 'linestring' : $child->nodeName; |
|
| 160 | + if (array_key_exists($nodeName, $geom_types)) {
|
|
| 161 | + $function = 'parse'.$geom_types[$nodeName]; |
|
| 162 | + $components[] = $this->$function($child); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + return new GeometryCollection($components); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | protected function _extractCoordinates($xml) {
|
| 169 | - $coord_elements = $this->childElements($xml, 'coordinates'); |
|
| 170 | - $coordinates = array(); |
|
| 171 | - if (count($coord_elements)) {
|
|
| 172 | - $coord_sets = explode(' ', preg_replace('/[\r\n]+/', ' ', $coord_elements[0]->nodeValue));
|
|
| 173 | - foreach ($coord_sets as $set_string) {
|
|
| 174 | - $set_string = trim($set_string); |
|
| 175 | - if ($set_string) {
|
|
| 176 | - $set_array = explode(',',$set_string);
|
|
| 177 | - if (count($set_array) >= 2) {
|
|
| 178 | - $coordinates[] = $set_array; |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - return $coordinates; |
|
| 169 | + $coord_elements = $this->childElements($xml, 'coordinates'); |
|
| 170 | + $coordinates = array(); |
|
| 171 | + if (count($coord_elements)) {
|
|
| 172 | + $coord_sets = explode(' ', preg_replace('/[\r\n]+/', ' ', $coord_elements[0]->nodeValue));
|
|
| 173 | + foreach ($coord_sets as $set_string) {
|
|
| 174 | + $set_string = trim($set_string); |
|
| 175 | + if ($set_string) {
|
|
| 176 | + $set_array = explode(',',$set_string);
|
|
| 177 | + if (count($set_array) >= 2) {
|
|
| 178 | + $coordinates[] = $set_array; |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + return $coordinates; |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | private function geometryToKML($geom) {
|
| 188 | - $type = strtolower($geom->getGeomType()); |
|
| 189 | - switch ($type) {
|
|
| 190 | - case 'point': |
|
| 191 | - return $this->pointToKML($geom); |
|
| 192 | - break; |
|
| 193 | - case 'linestring': |
|
| 194 | - return $this->linestringToKML($geom); |
|
| 195 | - break; |
|
| 196 | - case 'polygon': |
|
| 197 | - return $this->polygonToKML($geom); |
|
| 198 | - break; |
|
| 199 | - case 'multipoint': |
|
| 200 | - case 'multilinestring': |
|
| 201 | - case 'multipolygon': |
|
| 202 | - case 'geometrycollection': |
|
| 203 | - return $this->collectionToKML($geom); |
|
| 204 | - break; |
|
| 205 | - } |
|
| 188 | + $type = strtolower($geom->getGeomType()); |
|
| 189 | + switch ($type) {
|
|
| 190 | + case 'point': |
|
| 191 | + return $this->pointToKML($geom); |
|
| 192 | + break; |
|
| 193 | + case 'linestring': |
|
| 194 | + return $this->linestringToKML($geom); |
|
| 195 | + break; |
|
| 196 | + case 'polygon': |
|
| 197 | + return $this->polygonToKML($geom); |
|
| 198 | + break; |
|
| 199 | + case 'multipoint': |
|
| 200 | + case 'multilinestring': |
|
| 201 | + case 'multipolygon': |
|
| 202 | + case 'geometrycollection': |
|
| 203 | + return $this->collectionToKML($geom); |
|
| 204 | + break; |
|
| 205 | + } |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | private function pointToKML($geom) {
|
| 209 | - return '<'.$this->nss.'Point><'.$this->nss.'coordinates>'.$geom->getX().",".$geom->getY().'</'.$this->nss.'coordinates></'.$this->nss.'Point>'; |
|
| 209 | + return '<'.$this->nss.'Point><'.$this->nss.'coordinates>'.$geom->getX().",".$geom->getY().'</'.$this->nss.'coordinates></'.$this->nss.'Point>'; |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | private function linestringToKML($geom, $type = FALSE) {
|
| 213 | - if (!$type) {
|
|
| 214 | - $type = $geom->getGeomType(); |
|
| 215 | - } |
|
| 213 | + if (!$type) {
|
|
| 214 | + $type = $geom->getGeomType(); |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - $str = '<'.$this->nss . $type .'>'; |
|
| 217 | + $str = '<'.$this->nss . $type .'>'; |
|
| 218 | 218 | |
| 219 | - if (!$geom->isEmpty()) {
|
|
| 220 | - $str .= '<'.$this->nss.'coordinates>'; |
|
| 221 | - $i=0; |
|
| 222 | - foreach ($geom->getComponents() as $comp) {
|
|
| 223 | - if ($i != 0) $str .= ' '; |
|
| 224 | - $str .= $comp->getX() .','. $comp->getY(); |
|
| 225 | - $i++; |
|
| 226 | - } |
|
| 219 | + if (!$geom->isEmpty()) {
|
|
| 220 | + $str .= '<'.$this->nss.'coordinates>'; |
|
| 221 | + $i=0; |
|
| 222 | + foreach ($geom->getComponents() as $comp) {
|
|
| 223 | + if ($i != 0) $str .= ' '; |
|
| 224 | + $str .= $comp->getX() .','. $comp->getY(); |
|
| 225 | + $i++; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - $str .= '</'.$this->nss.'coordinates>'; |
|
| 229 | - } |
|
| 228 | + $str .= '</'.$this->nss.'coordinates>'; |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - $str .= '</'. $this->nss . $type .'>'; |
|
| 231 | + $str .= '</'. $this->nss . $type .'>'; |
|
| 232 | 232 | |
| 233 | - return $str; |
|
| 233 | + return $str; |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | public function polygonToKML($geom) {
|
| 237 | - $components = $geom->getComponents(); |
|
| 238 | - if (!empty($components)) {
|
|
| 239 | - $str = '<'.$this->nss.'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</'.$this->nss.'outerBoundaryIs>'; |
|
| 240 | - foreach (array_slice($components, 1) as $comp) {
|
|
| 241 | - $str .= '<'.$this->nss.'innerBoundaryIs>' . $this->linestringToKML($comp) . '</'.$this->nss.'innerBoundaryIs>'; |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - return '<'.$this->nss.'Polygon>'. $str .'</'.$this->nss.'Polygon>'; |
|
| 237 | + $components = $geom->getComponents(); |
|
| 238 | + if (!empty($components)) {
|
|
| 239 | + $str = '<'.$this->nss.'outerBoundaryIs>' . $this->linestringToKML($components[0], 'LinearRing') . '</'.$this->nss.'outerBoundaryIs>'; |
|
| 240 | + foreach (array_slice($components, 1) as $comp) {
|
|
| 241 | + $str .= '<'.$this->nss.'innerBoundaryIs>' . $this->linestringToKML($comp) . '</'.$this->nss.'innerBoundaryIs>'; |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + return '<'.$this->nss.'Polygon>'. $str .'</'.$this->nss.'Polygon>'; |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | public function collectionToKML($geom) {
|
| 249 | - $components = $geom->getComponents(); |
|
| 250 | - $str = '<'.$this->nss.'MultiGeometry>'; |
|
| 251 | - foreach ($geom->getComponents() as $comp) {
|
|
| 252 | - $sub_adapter = new KML(); |
|
| 253 | - $str .= $sub_adapter->write($comp); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - return $str .'</'.$this->nss.'MultiGeometry>'; |
|
| 249 | + $components = $geom->getComponents(); |
|
| 250 | + $str = '<'.$this->nss.'MultiGeometry>'; |
|
| 251 | + foreach ($geom->getComponents() as $comp) {
|
|
| 252 | + $sub_adapter = new KML(); |
|
| 253 | + $str .= $sub_adapter->write($comp); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + return $str .'</'.$this->nss.'MultiGeometry>'; |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | } |
@@ -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 |
@@ -9,70 +9,70 @@ discard block |
||
| 9 | 9 | protected $geom_type = 'Polygon'; |
| 10 | 10 | |
| 11 | 11 | public function area($exterior_only = FALSE, $signed = FALSE) { |
| 12 | - if ($this->isEmpty()) return 0; |
|
| 12 | + if ($this->isEmpty()) return 0; |
|
| 13 | 13 | |
| 14 | - if ($this->geos() && $exterior_only == FALSE) { |
|
| 15 | - return $this->geos()->area(); |
|
| 16 | - } |
|
| 14 | + if ($this->geos() && $exterior_only == FALSE) { |
|
| 15 | + return $this->geos()->area(); |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - $exterior_ring = $this->components[0]; |
|
| 19 | - $pts = $exterior_ring->getComponents(); |
|
| 18 | + $exterior_ring = $this->components[0]; |
|
| 19 | + $pts = $exterior_ring->getComponents(); |
|
| 20 | 20 | |
| 21 | - $c = count($pts); |
|
| 22 | - if((int)$c == '0') return NULL; |
|
| 23 | - $a = '0'; |
|
| 24 | - foreach($pts as $k => $p){ |
|
| 25 | - $j = ($k + 1) % $c; |
|
| 26 | - $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
|
| 27 | - } |
|
| 21 | + $c = count($pts); |
|
| 22 | + if((int)$c == '0') return NULL; |
|
| 23 | + $a = '0'; |
|
| 24 | + foreach($pts as $k => $p){ |
|
| 25 | + $j = ($k + 1) % $c; |
|
| 26 | + $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - if ($signed) $area = ($a / 2); |
|
| 30 | - else $area = abs(($a / 2)); |
|
| 29 | + if ($signed) $area = ($a / 2); |
|
| 30 | + else $area = abs(($a / 2)); |
|
| 31 | 31 | |
| 32 | - if ($exterior_only == TRUE) { |
|
| 33 | - return $area; |
|
| 34 | - } |
|
| 35 | - foreach ($this->components as $delta => $component) { |
|
| 36 | - if ($delta != 0) { |
|
| 37 | - $inner_poly = new Polygon(array($component)); |
|
| 38 | - $area -= $inner_poly->area(); |
|
| 39 | - } |
|
| 40 | - } |
|
| 41 | - return $area; |
|
| 32 | + if ($exterior_only == TRUE) { |
|
| 33 | + return $area; |
|
| 34 | + } |
|
| 35 | + foreach ($this->components as $delta => $component) { |
|
| 36 | + if ($delta != 0) { |
|
| 37 | + $inner_poly = new Polygon(array($component)); |
|
| 38 | + $area -= $inner_poly->area(); |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | + return $area; |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | public function centroid() { |
| 45 | - if ($this->isEmpty()) return NULL; |
|
| 45 | + if ($this->isEmpty()) return NULL; |
|
| 46 | 46 | |
| 47 | - if ($this->geos()) { |
|
| 48 | - return geoPHP::geosToGeometry($this->geos()->centroid()); |
|
| 49 | - } |
|
| 47 | + if ($this->geos()) { |
|
| 48 | + return geoPHP::geosToGeometry($this->geos()->centroid()); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - $exterior_ring = $this->components[0]; |
|
| 52 | - $pts = $exterior_ring->getComponents(); |
|
| 51 | + $exterior_ring = $this->components[0]; |
|
| 52 | + $pts = $exterior_ring->getComponents(); |
|
| 53 | 53 | |
| 54 | - $c = count($pts); |
|
| 55 | - if((int)$c == '0') return NULL; |
|
| 56 | - $cn = array('x' => '0', 'y' => '0'); |
|
| 57 | - $a = $this->area(TRUE, TRUE); |
|
| 54 | + $c = count($pts); |
|
| 55 | + if((int)$c == '0') return NULL; |
|
| 56 | + $cn = array('x' => '0', 'y' => '0'); |
|
| 57 | + $a = $this->area(TRUE, TRUE); |
|
| 58 | 58 | |
| 59 | - // If this is a polygon with no area. Just return the first point. |
|
| 60 | - if ($a == 0) { |
|
| 61 | - return $this->exteriorRing()->pointN(1); |
|
| 62 | - } |
|
| 59 | + // If this is a polygon with no area. Just return the first point. |
|
| 60 | + if ($a == 0) { |
|
| 61 | + return $this->exteriorRing()->pointN(1); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - foreach($pts as $k => $p){ |
|
| 65 | - $j = ($k + 1) % $c; |
|
| 66 | - $P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
|
| 67 | - $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P; |
|
| 68 | - $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P; |
|
| 69 | - } |
|
| 64 | + foreach($pts as $k => $p){ |
|
| 65 | + $j = ($k + 1) % $c; |
|
| 66 | + $P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX()); |
|
| 67 | + $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P; |
|
| 68 | + $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $cn['x'] = $cn['x'] / ( 6 * $a); |
|
| 72 | - $cn['y'] = $cn['y'] / ( 6 * $a); |
|
| 71 | + $cn['x'] = $cn['x'] / ( 6 * $a); |
|
| 72 | + $cn['y'] = $cn['y'] / ( 6 * $a); |
|
| 73 | 73 | |
| 74 | - $centroid = new Point($cn['x'], $cn['y']); |
|
| 75 | - return $centroid; |
|
| 74 | + $centroid = new Point($cn['x'], $cn['y']); |
|
| 75 | + return $centroid; |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
@@ -98,41 +98,41 @@ discard block |
||
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | public function exteriorRing() { |
| 101 | - if ($this->isEmpty()) return new LineString(); |
|
| 102 | - return $this->components[0]; |
|
| 101 | + if ($this->isEmpty()) return new LineString(); |
|
| 102 | + return $this->components[0]; |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | public function numInteriorRings() { |
| 106 | - if ($this->isEmpty()) return 0; |
|
| 107 | - return $this->numGeometries()-1; |
|
| 106 | + if ($this->isEmpty()) return 0; |
|
| 107 | + return $this->numGeometries()-1; |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | public function interiorRingN($n) { |
| 111 | - return $this->geometryN($n+1); |
|
| 111 | + return $this->geometryN($n+1); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | public function dimension() { |
| 115 | - if ($this->isEmpty()) return 0; |
|
| 116 | - return 2; |
|
| 115 | + if ($this->isEmpty()) return 0; |
|
| 116 | + return 2; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | public function isSimple() { |
| 120 | - if ($this->geos()) { |
|
| 121 | - return $this->geos()->isSimple(); |
|
| 122 | - } |
|
| 120 | + if ($this->geos()) { |
|
| 121 | + return $this->geos()->isSimple(); |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - $segments = $this->explode(); |
|
| 124 | + $segments = $this->explode(); |
|
| 125 | 125 | |
| 126 | - foreach ($segments as $i => $segment) { |
|
| 127 | - foreach ($segments as $j => $check_segment) { |
|
| 128 | - if ($i != $j) { |
|
| 129 | - if ($segment->lineSegmentIntersect($check_segment)) { |
|
| 130 | - return FALSE; |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - return TRUE; |
|
| 126 | + foreach ($segments as $i => $segment) { |
|
| 127 | + foreach ($segments as $j => $check_segment) { |
|
| 128 | + if ($i != $j) { |
|
| 129 | + if ($segment->lineSegmentIntersect($check_segment)) { |
|
| 130 | + return FALSE; |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + return TRUE; |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
@@ -146,59 +146,59 @@ discard block |
||
| 146 | 146 | * @return boolean |
| 147 | 147 | */ |
| 148 | 148 | public function pointInPolygon($point, $pointOnBoundary = true, $pointOnVertex = true) { |
| 149 | - $vertices = $this->getPoints(); |
|
| 149 | + $vertices = $this->getPoints(); |
|
| 150 | 150 | |
| 151 | - // Check if the point sits exactly on a vertex |
|
| 152 | - if ($this->pointOnVertex($point, $vertices)) { |
|
| 153 | - return $pointOnVertex ? TRUE : FALSE; |
|
| 154 | - } |
|
| 151 | + // Check if the point sits exactly on a vertex |
|
| 152 | + if ($this->pointOnVertex($point, $vertices)) { |
|
| 153 | + return $pointOnVertex ? TRUE : FALSE; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - // Check if the point is inside the polygon or on the boundary |
|
| 157 | - $intersections = 0; |
|
| 158 | - $vertices_count = count($vertices); |
|
| 156 | + // Check if the point is inside the polygon or on the boundary |
|
| 157 | + $intersections = 0; |
|
| 158 | + $vertices_count = count($vertices); |
|
| 159 | 159 | |
| 160 | - for ($i=1; $i < $vertices_count; $i++) { |
|
| 161 | - $vertex1 = $vertices[$i-1]; |
|
| 162 | - $vertex2 = $vertices[$i]; |
|
| 163 | - if ($vertex1->y() == $vertex2->y() |
|
| 164 | - && $vertex1->y() == $point->y() |
|
| 165 | - && $point->x() > min($vertex1->x(), $vertex2->x()) |
|
| 166 | - && $point->x() < max($vertex1->x(), $vertex2->x())) { |
|
| 167 | - // Check if point is on an horizontal polygon boundary |
|
| 168 | - return $pointOnBoundary ? TRUE : FALSE; |
|
| 169 | - } |
|
| 170 | - if ($point->y() > min($vertex1->y(), $vertex2->y()) |
|
| 171 | - && $point->y() <= max($vertex1->y(), $vertex2->y()) |
|
| 172 | - && $point->x() <= max($vertex1->x(), $vertex2->x()) |
|
| 173 | - && $vertex1->y() != $vertex2->y()) { |
|
| 174 | - $xinters = |
|
| 175 | - ($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x()) |
|
| 176 | - / ($vertex2->y() - $vertex1->y()) |
|
| 177 | - + $vertex1->x(); |
|
| 178 | - if ($xinters == $point->x()) { |
|
| 179 | - // Check if point is on the polygon boundary (other than horizontal) |
|
| 180 | - return $pointOnBoundary ? TRUE : FALSE; |
|
| 181 | - } |
|
| 182 | - if ($vertex1->x() == $vertex2->x() || $point->x() <= $xinters) { |
|
| 183 | - $intersections++; |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - // If the number of edges we passed through is even, then it's in the polygon. |
|
| 188 | - if ($intersections % 2 != 0) { |
|
| 189 | - return TRUE; |
|
| 190 | - } |
|
| 191 | - else { |
|
| 192 | - return FALSE; |
|
| 193 | - } |
|
| 160 | + for ($i=1; $i < $vertices_count; $i++) { |
|
| 161 | + $vertex1 = $vertices[$i-1]; |
|
| 162 | + $vertex2 = $vertices[$i]; |
|
| 163 | + if ($vertex1->y() == $vertex2->y() |
|
| 164 | + && $vertex1->y() == $point->y() |
|
| 165 | + && $point->x() > min($vertex1->x(), $vertex2->x()) |
|
| 166 | + && $point->x() < max($vertex1->x(), $vertex2->x())) { |
|
| 167 | + // Check if point is on an horizontal polygon boundary |
|
| 168 | + return $pointOnBoundary ? TRUE : FALSE; |
|
| 169 | + } |
|
| 170 | + if ($point->y() > min($vertex1->y(), $vertex2->y()) |
|
| 171 | + && $point->y() <= max($vertex1->y(), $vertex2->y()) |
|
| 172 | + && $point->x() <= max($vertex1->x(), $vertex2->x()) |
|
| 173 | + && $vertex1->y() != $vertex2->y()) { |
|
| 174 | + $xinters = |
|
| 175 | + ($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x()) |
|
| 176 | + / ($vertex2->y() - $vertex1->y()) |
|
| 177 | + + $vertex1->x(); |
|
| 178 | + if ($xinters == $point->x()) { |
|
| 179 | + // Check if point is on the polygon boundary (other than horizontal) |
|
| 180 | + return $pointOnBoundary ? TRUE : FALSE; |
|
| 181 | + } |
|
| 182 | + if ($vertex1->x() == $vertex2->x() || $point->x() <= $xinters) { |
|
| 183 | + $intersections++; |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + // If the number of edges we passed through is even, then it's in the polygon. |
|
| 188 | + if ($intersections % 2 != 0) { |
|
| 189 | + return TRUE; |
|
| 190 | + } |
|
| 191 | + else { |
|
| 192 | + return FALSE; |
|
| 193 | + } |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | public function pointOnVertex($point) { |
| 197 | - foreach($this->getPoints() as $vertex) { |
|
| 198 | - if ($point->equals($vertex)) { |
|
| 199 | - return true; |
|
| 200 | - } |
|
| 201 | - } |
|
| 197 | + foreach($this->getPoints() as $vertex) { |
|
| 198 | + if ($point->equals($vertex)) { |
|
| 199 | + return true; |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | |
@@ -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 |
@@ -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 |
@@ -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 | |