/
WKT.class.php
258 lines (221 loc) · 7.33 KB
/
WKT.class.php
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* WKT (Well Known Text) Adapter
*/
class WKT extends GeoAdapter
{
/**
* Read WKT string into geometry objects
*
* @param string $WKT A WKT string
*
* @return Geometry
*/
public function read($wkt) {
$wkt = trim($wkt);
// If it contains a ';', then it contains additional SRID data
if (strpos($wkt,';')) {
$parts = explode(';', $wkt);
$wkt = $parts[1];
$eparts = explode('=',$parts[0]);
$srid = $eparts[1];
}
else {
$srid = NULL;
}
// If geos is installed, then we take a shortcut and let it parse the WKT
if (geoPHP::geosInstalled()) {
$reader = new GEOSWKTReader();
if ($srid) {
$geom = geoPHP::geosToGeometry($reader->read($wkt));
$geom->setSRID($srid);
return $geom;
}
else {
return geoPHP::geosToGeometry($reader->read($wkt));
}
}
$wkt = str_replace(', ', ',', $wkt);
// For each geometry type, check to see if we have a match at the
// beginning of the string. If we do, then parse using that type
foreach (geoPHP::geometryList() as $geom_type) {
$wkt_geom = strtoupper($geom_type);
if (strtoupper(substr($wkt, 0, strlen($wkt_geom))) == $wkt_geom) {
$data_string = $this->getDataString($wkt);
$method = 'parse'.$geom_type;
if ($srid) {
$geom = $this->$method($data_string);
$geom->setSRID($srid);
return $geom;
}
else {
return $this->$method($data_string);
}
}
}
}
private function parsePoint($data_string) {
$data_string = $this->trimParens($data_string);
// If it's marked as empty, then return an empty point
if ($data_string == 'EMPTY') return new Point();
$parts = explode(' ',$data_string);
return new Point($parts[0], $parts[1]);
}
private function parseLineString($data_string) {
$data_string = $this->trimParens($data_string);
// If it's marked as empty, then return an empty line
if ($data_string == 'EMPTY') return new LineString();
$parts = explode(',',$data_string);
$points = array();
foreach ($parts as $part) {
$points[] = $this->parsePoint($part);
}
return new LineString($points);
}
private function parsePolygon($data_string) {
$data_string = $this->trimParens($data_string);
// If it's marked as empty, then return an empty polygon
if ($data_string == 'EMPTY') return new Polygon();
$parts = explode('),(',$data_string);
$lines = array();
foreach ($parts as $part) {
if (!$this->beginsWith($part,'(')) $part = '(' . $part;
if (!$this->endsWith($part,')')) $part = $part . ')';
$lines[] = $this->parseLineString($part);
}
return new Polygon($lines);
}
private function parseMultiPoint($data_string) {
$data_string = $this->trimParens($data_string);
// If it's marked as empty, then return an empty MutiPoint
if ($data_string == 'EMPTY') return new MultiPoint();
$parts = explode(',',$data_string);
$points = array();
foreach ($parts as $part) {
$points[] = $this->parsePoint($part);
}
return new MultiPoint($points);
}
private function parseMultiLineString($data_string) {
$data_string = $this->trimParens($data_string);
// If it's marked as empty, then return an empty multi-linestring
if ($data_string == 'EMPTY') return new MultiLineString();
$parts = explode('),(',$data_string);
$lines = array();
foreach ($parts as $part) {
// Repair the string if the explode broke it
if (!$this->beginsWith($part,'(')) $part = '(' . $part;
if (!$this->endsWith($part,')')) $part = $part . ')';
$lines[] = $this->parseLineString($part);
}
return new MultiLineString($lines);
}
private function parseMultiPolygon($data_string) {
$data_string = $this->trimParens($data_string);
// If it's marked as empty, then return an empty multi-polygon
if ($data_string == 'EMPTY') return new MultiPolygon();
$parts = explode(')),((',$data_string);
$polys = array();
foreach ($parts as $part) {
// Repair the string if the explode broke it
if (!$this->beginsWith($part,'((')) $part = '((' . $part;
if (!$this->endsWith($part,'))')) $part = $part . '))';
$polys[] = $this->parsePolygon($part);
}
return new MultiPolygon($polys);
}
private function parseGeometryCollection($data_string) {
$data_string = $this->trimParens($data_string);
// If it's marked as empty, then return an empty geom-collection
if ($data_string == 'EMPTY') return new GeometryCollection();
$geometries = array();
$matches = array();
$str = preg_replace('/,\s*([A-Za-z])/', '|$1', $data_string);
$components = explode('|', trim($str));
foreach ($components as $component) {
$geometries[] = $this->read($component);
}
return new GeometryCollection($geometries);
}
protected function getDataString($wkt) {
$first_paren = strpos($wkt, '(');
if ($first_paren !== FALSE) {
return substr($wkt, $first_paren);
} elseif (strstr($wkt,'EMPTY')) {
return 'EMPTY';
} else
return FALSE;
}
/**
* Trim the parenthesis and spaces
*/
protected function trimParens($str) {
$str = trim($str);
// We want to only strip off one set of parenthesis
if ($this->beginsWith($str, '(')) {
return substr($str,1,-1);
}
else return $str;
}
protected function beginsWith($str, $char) {
if (substr($str,0,strlen($char)) == $char) return TRUE;
else return FALSE;
}
protected function endsWith($str, $char) {
if (substr($str,(0 - strlen($char))) == $char) return TRUE;
else return FALSE;
}
/**
* Serialize geometries into a WKT string.
*
* @param Geometry $geometry
*
* @return string The WKT string representation of the input geometries
*/
public function write(Geometry $geometry) {
// If geos is installed, then we take a shortcut and let it write the WKT
if (geoPHP::geosInstalled()) {
$writer = new GEOSWKTWriter();
$writer->setTrim(TRUE);
return $writer->write($geometry->geos());
}
if ($geometry->isEmpty()) {
return strtoupper($geometry->geometryType()).' EMPTY';
}
else if ($data = $this->extractData($geometry)) {
return strtoupper($geometry->geometryType()).' ('.$data.')';
}
}
/**
* Extract geometry to a WKT string
*
* @param Geometry $geometry A Geometry object
*
* @return string
*/
public function extractData($geometry) {
$parts = array();
switch ($geometry->geometryType()) {
case 'Point':
return $geometry->getX().' '.$geometry->getY();
case 'LineString':
foreach ($geometry->getComponents() as $component) {
$parts[] = $this->extractData($component);
}
return implode(', ', $parts);
case 'Polygon':
case 'MultiPoint':
case 'MultiLineString':
case 'MultiPolygon':
foreach ($geometry->getComponents() as $component) {
$parts[] = '('.$this->extractData($component).')';
}
return implode(', ', $parts);
case 'GeometryCollection':
foreach ($geometry->getComponents() as $component) {
$parts[] = strtoupper($component->geometryType()).' ('.$this->extractData($component).')';
}
return implode(', ', $parts);
}
}
}