Gpx.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Geocoder package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. namespace Geocoder\Dumper;
  11. use Geocoder\Geocoder;
  12. use Geocoder\Location;
  13. /**
  14. * @author William Durand <william.durand1@gmail.com>
  15. */
  16. final class Gpx extends AbstractDumper implements Dumper
  17. {
  18. /**
  19. * @param Location $location
  20. *
  21. * @return string
  22. */
  23. public function dump(Location $location): string
  24. {
  25. $gpx = sprintf(<<<'GPX'
  26. <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
  27. <gpx
  28. version="1.0"
  29. creator="Geocoder" version="%s"
  30. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31. xmlns="http://www.topografix.com/GPX/1/0"
  32. xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
  33. GPX
  34. , Geocoder::VERSION);
  35. if (null !== $bounds = $location->getBounds()) {
  36. $gpx .= sprintf(<<<'GPX'
  37. <bounds minlat="%f" minlon="%f" maxlat="%f" maxlon="%f"/>
  38. GPX
  39. , $bounds->getWest(), $bounds->getSouth(), $bounds->getEast(), $bounds->getNorth());
  40. }
  41. $lat = null;
  42. $lon = null;
  43. if (null !== $coordinates = $location->getCoordinates()) {
  44. $lat = $coordinates->getLatitude();
  45. $lon = $coordinates->getLongitude();
  46. }
  47. $gpx .= sprintf(<<<'GPX'
  48. <wpt lat="%.7f" lon="%.7f">
  49. <name><![CDATA[%s]]></name>
  50. <type><![CDATA[Address]]></type>
  51. </wpt>
  52. GPX
  53. , $lat, $lon, $this->formatName($location));
  54. $gpx .= <<<'GPX'
  55. </gpx>
  56. GPX;
  57. return $gpx;
  58. }
  59. }