|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Maps\Elements\Location; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class to format geographical data to KML. |
|
7
|
|
|
* |
|
8
|
|
|
* @since 0.7.3 |
|
9
|
|
|
* |
|
10
|
|
|
* @licence GNU GPL v2+ |
|
11
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
12
|
|
|
*/ |
|
13
|
|
|
class MapsKMLFormatter { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @since 1.0 |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $params; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @since 0.7.3 |
|
24
|
|
|
* |
|
25
|
|
|
* @var Location[] |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $placemarks; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @since 0.7.3 |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $params |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( array $params = [] ) { |
|
37
|
|
|
$this->params = $params; |
|
38
|
|
|
$this->clearElements(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Builds and returns KML representing the set geographical objects. |
|
43
|
|
|
* |
|
44
|
|
|
* @since 0.7.3 |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getKML() { |
|
49
|
|
|
$elements = $this->getKMLElements(); |
|
50
|
|
|
|
|
51
|
|
|
// http://earth.google.com/kml/2.2 |
|
52
|
|
|
return <<<EOT |
|
53
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
|
54
|
|
|
<kml xmlns="http://www.opengis.net/kml/2.2"> |
|
55
|
|
|
<Document> |
|
56
|
|
|
$elements |
|
57
|
|
|
</Document> |
|
58
|
|
|
</kml> |
|
59
|
|
|
EOT; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Adds a single placemark. |
|
64
|
|
|
* |
|
65
|
|
|
* @since 0.7.3 |
|
66
|
|
|
* |
|
67
|
|
|
* @param Location $placemark |
|
68
|
|
|
*/ |
|
69
|
|
|
public function addPlacemark( Location $placemark ) { |
|
70
|
|
|
$this->placemarks[] = $placemark; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Adds a multiple placemarks. |
|
75
|
|
|
* |
|
76
|
|
|
* @since 0.7.3 |
|
77
|
|
|
* |
|
78
|
|
|
* @param Location[] $placemarks |
|
79
|
|
|
*/ |
|
80
|
|
|
public function addPlacemarks( array $placemarks ) { |
|
81
|
|
|
foreach ( $placemarks as $placemark ) { |
|
82
|
|
|
$this->addPlacemark( $placemark ); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Clears all set geographical objects. |
|
88
|
|
|
* |
|
89
|
|
|
* @since 0.7.3 |
|
90
|
|
|
*/ |
|
91
|
|
|
public function clearElements() { |
|
92
|
|
|
$this->clearPlacemarks(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Clears all set placemarks. |
|
97
|
|
|
* |
|
98
|
|
|
* @since 0.7.3 |
|
99
|
|
|
*/ |
|
100
|
|
|
public function clearPlacemarks() { |
|
101
|
|
|
$this->placemarks = []; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns the KML for all set geographical objects. |
|
106
|
|
|
* |
|
107
|
|
|
* @since 0.7.3 |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getKMLElements() { |
|
112
|
|
|
$elements = []; |
|
113
|
|
|
|
|
114
|
|
|
$elements = array_merge( $elements, $this->getPlacemarks() ); |
|
115
|
|
|
|
|
116
|
|
|
return implode( "\n", $elements ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns KML for all set placemarks in a list, where each element is |
|
121
|
|
|
* a KML node representing a placemark. |
|
122
|
|
|
* |
|
123
|
|
|
* @since 0.7.3 |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function getPlacemarks() { |
|
128
|
|
|
$placemarks = []; |
|
129
|
|
|
|
|
130
|
|
|
foreach ( $this->placemarks as $location ) { |
|
131
|
|
|
$placemarks[] = $this->getKMLForLocation( $location ); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $placemarks; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns the KML representing the provided location. |
|
139
|
|
|
* |
|
140
|
|
|
* @since 0.7.3 |
|
141
|
|
|
* |
|
142
|
|
|
* @param Location $location |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function getKMLForLocation( Location $location ) { |
|
147
|
|
|
$name = '<name><![CDATA[ ' . $location->getTitle() . ']]></name>'; |
|
148
|
|
|
|
|
149
|
|
|
$description = '<description><![CDATA[ ' . $location->getText() . ']]></description>'; |
|
150
|
|
|
|
|
151
|
|
|
$coordinates = $location->getCoordinates(); |
|
152
|
|
|
|
|
153
|
|
|
// lon,lat[,alt] |
|
154
|
|
|
$coordinates = Xml::element( |
|
155
|
|
|
'coordinates', |
|
156
|
|
|
[], |
|
157
|
|
|
$coordinates->getLongitude() . ',' . $coordinates->getLatitude() . ',0' |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
return <<<EOT |
|
161
|
|
|
<Placemark> |
|
162
|
|
|
$name |
|
163
|
|
|
$description |
|
164
|
|
|
<Point> |
|
165
|
|
|
$coordinates |
|
166
|
|
|
</Point> |
|
167
|
|
|
</Placemark> |
|
168
|
|
|
|
|
169
|
|
|
EOT; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|