forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGpx.php
More file actions
90 lines (76 loc) · 2.07 KB
/
Copy pathGpx.php
File metadata and controls
90 lines (76 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Dumper;
use Geocoder\Geocoder;
use Geocoder\Model\Address;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class Gpx implements Dumper
{
/**
* @param Address $address
*
* @return string
*/
public function dump(Address $address)
{
$gpx = sprintf(<<<GPX
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx
version="1.0"
creator="Geocoder" version="%s"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
GPX
, Geocoder::VERSION);
if ($address->getBounds()->isDefined()) {
$bounds = $address->getBounds();
$gpx .= sprintf(<<<GPX
<bounds minlat="%f" minlon="%f" maxlat="%f" maxlon="%f"/>
GPX
, $bounds->getWest(), $bounds->getSouth(), $bounds->getEast(), $bounds->getNorth());
}
$gpx .= sprintf(<<<GPX
<wpt lat="%.7f" lon="%.7f">
<name><![CDATA[%s]]></name>
<type><![CDATA[Address]]></type>
</wpt>
GPX
, $address->getLatitude(), $address->getLongitude(), $this->formatName($address));
$gpx .= <<<GPX
</gpx>
GPX;
return $gpx;
}
/**
* @param Address $address
*
* @return string
*/
protected function formatName(Address $address)
{
$name = [];
$array = $address->toArray();
$attrs = [
['streetNumber'],
['streetName'],
['postalCode'],
['locality'],
['adminLevels', 2, 'name'],
['adminLevels', 1, 'name'],
['country'],
];
foreach ($attrs as $attr) {
$name[] = \igorw\get_in($array, $attr);
}
return implode(', ', array_filter($name));
}
}