| Total Complexity | 106 |
| Total Lines | 528 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 2 |
Complex classes like GPX often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GPX, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class GPX implements GeoAdapter |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string Name-space string. eg 'georss:' |
||
| 28 | */ |
||
| 29 | private $nss = ''; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var GpxTypes |
||
| 33 | */ |
||
| 34 | protected $gpxTypes; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \DOMXPath |
||
| 38 | */ |
||
| 39 | protected $xpath; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $parseGarminRpt = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Point[] |
||
| 48 | */ |
||
| 49 | protected $trackFromRoute; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var bool add elevation-data to every coordinate |
||
| 53 | */ |
||
| 54 | public $withElevation = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Read GPX string into geometry object |
||
| 58 | * |
||
| 59 | * @param string $gpx A GPX string |
||
| 60 | * @param array<array> $allowedElements Which elements can be read from each GPX type |
||
| 61 | * If not specified, every element defined in the GPX specification can be read |
||
| 62 | * Can be overwritten with an associative array, with type name in keys. |
||
| 63 | * eg.: ['wptType' => ['ele', 'name'], 'trkptType' => ['ele'], 'metadataType' => null] |
||
| 64 | * |
||
| 65 | * @return Geometry|GeometryCollection |
||
| 66 | * @throws \Exception If GPX is not a valid XML |
||
| 67 | */ |
||
| 68 | public function read(string $gpx, array $allowedElements = []): Geometry |
||
| 69 | { |
||
| 70 | // Converts XML tags to lower-case (DOMDocument functions are case sensitive) |
||
| 71 | $gpx = preg_replace_callback("/(<\/?\w+)(.*?>)/", function ($m) { |
||
| 72 | return strtolower($m[1]) . $m[2]; |
||
| 73 | }, $gpx); |
||
| 74 | |||
| 75 | $this->gpxTypes = new GpxTypes($allowedElements); |
||
| 76 | |||
| 77 | //libxml_use_internal_errors(true); // why? |
||
| 78 | // Load into DOMDocument |
||
| 79 | $xmlObject = new \DOMDocument('1.0', 'UTF-8'); |
||
| 80 | $xmlObject->preserveWhiteSpace = false; |
||
| 81 | |||
| 82 | if ($xmlObject->loadXML($gpx) === false) { |
||
| 83 | throw new \Exception("Invalid GPX: " . $gpx); |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->parseGarminRpt = strpos($gpx, 'gpxx:rpt') > 0; |
||
| 87 | |||
| 88 | // Initialize XPath parser if needed (currently only for Garmin extensions) |
||
| 89 | if ($this->parseGarminRpt) { |
||
| 90 | $this->xpath = new \DOMXPath($xmlObject); |
||
| 91 | $this->xpath->registerNamespace('gpx', 'http://www.topografix.com/GPX/1/1'); |
||
| 92 | $this->xpath->registerNamespace('gpxx', 'http://www.garmin.com/xmlschemas/GpxExtensions/v3'); |
||
| 93 | } |
||
| 94 | |||
| 95 | try { |
||
| 96 | $geom = $this->geomFromXML($xmlObject); |
||
| 97 | } catch (\Exception $e) { |
||
| 98 | throw new \Exception("Cannot Read Geometry From GPX: " . $gpx . '<br>' . $e->getMessage()); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $geom; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Parses the GPX XML and returns a geometry |
||
| 106 | * @param \DOMDocument $xmlObject |
||
| 107 | * @return GeometryCollection|Geometry Returns the geometry representation of the GPX (@see geoPHP::buildGeometry) |
||
| 108 | */ |
||
| 109 | protected function geomFromXML($xmlObject): Geometry |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param \DOMNode $xml |
||
| 142 | * @param string $nodeName |
||
| 143 | * @return array<\DOMElement> |
||
| 144 | */ |
||
| 145 | protected function childElements(\DOMNode $xml, string $nodeName = ''): array |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param \DOMElement $node |
||
| 158 | * @return Point |
||
| 159 | */ |
||
| 160 | protected function parsePoint(\DOMElement $node): Point |
||
| 161 | { |
||
| 162 | $lat = null; |
||
| 163 | $lon = null; |
||
| 164 | |||
| 165 | if ($node->attributes !== null) { |
||
| 166 | $lat = $node->attributes->getNamedItem("lat")->nodeValue; |
||
| 167 | $lon = $node->attributes->getNamedItem("lon")->nodeValue; |
||
| 168 | } |
||
| 169 | |||
| 170 | $ele = $node->getElementsByTagName('ele'); |
||
| 171 | |||
| 172 | if ($this->withElevation && $ele->length) { |
||
| 173 | $elevation = $ele->item(0)->nodeValue; |
||
| 174 | $point = new Point($lon, $lat, $elevation <> 0 ? $elevation : null); |
||
| 175 | } else { |
||
| 176 | $point = new Point($lon, $lat); |
||
| 177 | } |
||
| 178 | $point->setData($this->parseNodeProperties($node, $this->gpxTypes->get($node->nodeName . 'Type'))); |
||
| 179 | if ($node->nodeName === 'rtept' && $this->parseGarminRpt) { |
||
| 180 | $rpts = $this->xpath->query('.//gpx:extensions/gpxx:RoutePointExtension/gpxx:rpt', $node); |
||
| 181 | if ($rpts !== false) { |
||
| 182 | foreach ($rpts as $element) { |
||
| 183 | $this->trackFromRoute[] = $this->parsePoint($element); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | return $point; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param \DOMDocument $xmlObject |
||
| 192 | * @return Point[] |
||
| 193 | */ |
||
| 194 | protected function parseWaypoints($xmlObject): array |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param \DOMDocument $xmlObject |
||
| 211 | * @return LineString[]|MultiLineString[] |
||
| 212 | */ |
||
| 213 | protected function parseTracks($xmlObject): array |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param \DOMDocument $xmlObject |
||
| 247 | * @return LineString[] |
||
| 248 | */ |
||
| 249 | protected function parseRoutes($xmlObject): array |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Parses a DOMNode and returns its content in a multidimensional associative array |
||
| 273 | * eg: <wpt><name>Test</name><link href="example.com"><text>Example</text></link></wpt> |
||
| 274 | * to: ['name' => 'Test', 'link' => ['text'] => 'Example', '@attributes' => ['href' => 'example.com']] |
||
| 275 | * |
||
| 276 | * @param \DOMNode $node |
||
| 277 | * @param string[]|null $tagList |
||
| 278 | * @return array<string>|string |
||
| 279 | */ |
||
| 280 | protected function parseNodeProperties(\DOMNode $node, $tagList = null) |
||
| 281 | { |
||
| 282 | if ($node->nodeType === XML_TEXT_NODE) { |
||
| 283 | return $node->nodeValue; |
||
| 284 | } |
||
| 285 | |||
| 286 | $result = []; |
||
| 287 | |||
| 288 | // add/parse properties from childs to result |
||
| 289 | if ($node->hasChildNodes()) { |
||
| 290 | $this->addChildNodeProperties($result, $node, $tagList); |
||
| 291 | } |
||
| 292 | |||
| 293 | // add attributes to result |
||
| 294 | if ($node->hasAttributes()) { |
||
| 295 | $this->addNodeAttributes($result, $node); |
||
| 296 | } |
||
| 297 | |||
| 298 | return $result; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * |
||
| 303 | * @param array<string, mixed>|string $result |
||
| 304 | * @param \DOMNode $node |
||
| 305 | * @param string[]|null $tagList |
||
| 306 | * @return void |
||
| 307 | */ |
||
| 308 | private function addChildNodeProperties(&$result, \DOMNode $node, $tagList) |
||
| 309 | { |
||
| 310 | foreach ($node->childNodes as $childNode) { |
||
| 311 | /** @var \DOMNode $childNode */ |
||
| 312 | if ($childNode->hasChildNodes()) { |
||
| 313 | if ($tagList === null || in_array($childNode->nodeName, $tagList ?: [])) { |
||
| 314 | if ($node->firstChild->nodeName == $node->lastChild->nodeName && $node->childNodes->length > 1) { |
||
| 315 | $result[$childNode->nodeName][] = $this->parseNodeProperties($childNode); |
||
| 316 | } else { |
||
| 317 | $result[$childNode->nodeName] = $this->parseNodeProperties($childNode); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } elseif ($childNode->nodeType === 1 && in_array($childNode->nodeName, $tagList ?: [])) { |
||
| 321 | // node is a DOMElement |
||
| 322 | $result[$childNode->nodeName] = $this->parseNodeProperties($childNode); |
||
| 323 | } elseif ($childNode->nodeType === 3) { |
||
| 324 | // node is a DOMText |
||
| 325 | $result = $childNode->nodeValue; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * |
||
| 332 | * @param array<string, mixed>|string $result |
||
| 333 | * @param \DOMNode $node |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | private function addNodeAttributes(&$result, \DOMNode $node) |
||
| 337 | { |
||
| 338 | if (is_string($result)) { |
||
| 339 | // As of the GPX specification text node cannot have attributes, thus this never happens |
||
| 340 | $result = ['#text' => $result]; |
||
| 341 | } |
||
| 342 | $attributes = []; |
||
| 343 | foreach ($node->attributes as $attribute) { |
||
| 344 | if ($attribute->name !== 'lat' && $attribute->name !== 'lon' && trim($attribute->value) !== '') { |
||
| 345 | $attributes[$attribute->name] = trim($attribute->value); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | if (!empty($attributes)) { |
||
| 349 | $result['@attributes'] = $attributes; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Serialize geometries into a GPX string. |
||
| 355 | * |
||
| 356 | * @param Geometry|GeometryCollection $geometry |
||
| 357 | * @param string $namespace |
||
| 358 | * @param array<array> $allowedElements Which elements can be added to each GPX type |
||
| 359 | * If not specified, every element defined in the GPX specification can be added |
||
| 360 | * Can be overwritten with an associative array, with type name in keys. |
||
| 361 | * eg.: ['wptType' => ['ele', 'name'], 'trkptType' => ['ele'], 'metadataType' => null] |
||
| 362 | * @return string The GPX string representation of the input geometries |
||
| 363 | */ |
||
| 364 | public function write(Geometry $geometry, string $namespace = '', array $allowedElements = []): string |
||
| 365 | { |
||
| 366 | $namespace = trim($namespace); |
||
| 367 | if (!empty($namespace)) { |
||
| 368 | $this->nss = $namespace . ':'; |
||
| 369 | } |
||
| 370 | $this->gpxTypes = new GpxTypes($allowedElements); |
||
| 371 | |||
| 372 | return |
||
| 373 | '<?xml version="1.0" encoding="UTF-8"?> |
||
| 374 | <' . $this->nss . 'gpx creator="geoPHP" version="1.1" |
||
| 375 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||
| 376 | xmlns="http://www.topografix.com/GPX/1/1" |
||
| 377 | xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" > |
||
| 378 | |||
| 379 | ' . $this->geometryToGPX($geometry) . |
||
| 380 | '</' . $this->nss . 'gpx> |
||
| 381 | '; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param Geometry|Collection $geometry |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | protected function geometryToGPX($geometry): string |
||
| 389 | { |
||
| 390 | switch ($geometry->geometryType()) { |
||
| 391 | case Geometry::POINT: |
||
| 392 | /** @var Point $geometry */ |
||
| 393 | return $this->pointToGPX($geometry); |
||
| 394 | case Geometry::LINESTRING: |
||
| 395 | case Geometry::MULTI_LINESTRING: |
||
| 396 | /** @var LineString $geometry */ |
||
| 397 | return $this->linestringToGPX($geometry); |
||
| 398 | case Geometry::POLYGON: |
||
| 399 | case Geometry::MULTI_POINT: |
||
| 400 | case Geometry::MULTI_POLYGON: |
||
| 401 | case Geometry::GEOMETRY_COLLECTION: |
||
| 402 | /** @var GeometryCollection $geometry */ |
||
| 403 | return $this->collectionToGPX($geometry); |
||
| 404 | } |
||
| 405 | return ''; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param Point $geom |
||
| 410 | * @param string $tag Can be "wpt", "trkpt" or "rtept" |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | private function pointToGPX($geom, $tag = 'wpt'): string |
||
| 414 | { |
||
| 415 | if ($geom->isEmpty() || ($tag === 'wpt' && !in_array($tag, $this->gpxTypes->get('gpxType')))) { |
||
| 416 | return ''; |
||
| 417 | } |
||
| 418 | $indent = $tag === 'trkpt' ? "\t\t" : ($tag === 'rtept' ? "\t" : ''); |
||
| 419 | |||
| 420 | if ($geom->hasZ() || $geom->getData() !== null) { |
||
| 421 | $node = $indent . "<" . $this->nss . $tag . " lat=\"" . $geom->getY() . "\" lon=\"" . $geom->getX() . "\">\n"; |
||
| 422 | if ($geom->hasZ()) { |
||
| 423 | $geom->setData('ele', $geom->getZ()); |
||
| 424 | } |
||
| 425 | $node .= self::processGeometryData($geom, $this->gpxTypes->get($tag . 'Type'), $indent . "\t") . |
||
| 426 | $indent . "</" . $this->nss . $tag . ">\n"; |
||
| 427 | if ($geom->hasZ()) { |
||
| 428 | $geom->setData('ele', null); |
||
| 429 | } |
||
| 430 | return $node; |
||
| 431 | } |
||
| 432 | return $indent . "<" . $this->nss . $tag . " lat=\"" . $geom->getY() . "\" lon=\"" . $geom->getX() . "\" />\n"; |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Writes a LineString or MultiLineString to the GPX |
||
| 437 | * |
||
| 438 | * The (Multi)LineString will be included in a <trk></trk> block |
||
| 439 | * The LineString or each LineString of the MultiLineString will be in <trkseg> </trkseg> inside the <trk> |
||
| 440 | * |
||
| 441 | * @param LineString|MultiLineString $geom |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | private function linestringToGPX($geom): string |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param Collection $geometry |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function collectionToGPX($geometry): string |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param Geometry $geometry |
||
| 509 | * @param string[] $tagList Allowed tags |
||
| 510 | * @param string $indent |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | protected static function processGeometryData($geometry, $tagList, $indent = "\t"): string |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param string $tagName |
||
| 528 | * @param string|array<array> $value |
||
| 529 | * @param string $indent |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | protected static function createNodes($tagName, $value, $indent): string |
||
| 551 | } |
||
| 552 | } |
||
| 553 |