forked from fprochazka/GeoJson
/
WKT.php
248 lines (203 loc) · 5.74 KB
/
WKT.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
<?php
/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008, 2012 Filip Procházka (filip.prochazka@kdyby.org)
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/
namespace Kdyby\Extension\GeoJson;
use Nette;
/**
* PHP Geometry/WKT encoder/decoder
*
* Mainly inspired/adapted from OpenLayers( http://www.openlayers.org )
* Openlayers/format/WKT.js
*
* @copyright Camptocamp <info@camptocamp.com>
* @author Filip Procházka <filip.prochazka@kdyby.org>
*/
class WKT extends Nette\Object
{
const POINT = 'point';
const MULTIPOINT = 'multipoint';
const LINESTRING = 'linestring';
const MULTILINESTRING = 'multilinestring';
const LINEARRING = 'linearring';
const POLYGON = 'polygon';
const MULTIPOLYGON = 'multipolygon';
const GEOMETRYCOLLECTION = 'geometrycollection';
/**
* @var array
*/
private $regExes = array(
'typeStr' => '/^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$/',
'spaces' => '/\s+/',
'parenComma' => '/\)\s*,\s*\(/',
'doubleParenComma' => '/\)\s*\)\s*,\s*\(\s*\(/',
'trimParens' => '/^\s*\(?(.*?)\)?\s*$/'
);
/**
* Read WKT string into geometry objects
*
* @param string $WKT A WKT string
*
* @return Geometry\Geometry|Geometry\GeometryCollection
*/
public function read($WKT)
{
if (!preg_match($this->regExes['typeStr'], $WKT, $matches)) {
return NULL;
}
return $this->parse(strtolower($matches[1]), $matches[2]);
}
/**
* Parse WKT string into geometry objects
*
* @param $type
* @param $str
*
* @return Geometry\Geometry|Geometry\GeometryCollection
*/
public function parse($type, $str)
{
$components = array();
switch ($type) {
case self::POINT:
$coords = $this->pregExplode('spaces', $str);
return new Geometry\Point($coords[0], $coords[1]);
case self::MULTIPOINT:
foreach (explode(',', trim($str)) as $point) {
$components[] = $this->parse(self::POINT, $point);
}
return new Geometry\MultiPoint($components);
case self::LINESTRING:
foreach (explode(',', trim($str)) as $point) {
$components[] = $this->parse(self::POINT, $point);
}
return new Geometry\LineString($components);
case self::MULTILINESTRING:
$lines = $this->pregExplode('parenComma', $str);
foreach ($lines as $l) {
$line = preg_replace($this->regExes['trimParens'], '$1', $l);
$components[] = $this->parse(self::LINESTRING, $line);
}
return new Geometry\MultiLineString($components);
case self::POLYGON:
$rings = $this->pregExplode('parenComma', $str);
foreach ($rings as $r) {
$ring = preg_replace($this->regExes['trimParens'], '$1', $r);
$linestring = $this->parse(self::LINESTRING, $ring);
$components[] = new Geometry\LinearRing($linestring->getComponents());
}
return new Geometry\Polygon($components);
case self::MULTIPOLYGON:
$polygons = $this->pregExplode('doubleParenComma', $str);
foreach ($polygons as $p) {
$polygon = preg_replace($this->regExes['trimParens'], '$1', $p);
$components[] = $this->parse(self::POLYGON, $polygon);
}
return new Geometry\MultiPolygon($components);
case self::GEOMETRYCOLLECTION:
$str = preg_replace('/,\s*([A-Za-z])/', '|$1', $str);
$wktArray = explode('|', trim($str));
foreach ($wktArray as $wkt) {
$components[] = $this->read($wkt);
}
return new Geometry\GeometryCollection($components);
default:
return null;
}
}
/**
* Split string according to first match of passed regEx index of $regExes
*
* @param string $regEx
* @param string $str
*
* @return array
*/
protected function pregExplode($regEx, $str)
{
$matches = array();
preg_match($this->regExes[$regEx], $str, $matches);
return empty($matches) ? array(trim($str)) : explode($matches[0], trim($str));
}
/**
* Serialize geometries into a WKT string.
*
* @param Geometry\Geometry $geometry
*
* @return string The WKT string representation of the input geometries
*/
public function write(Geometry\Geometry $geometry)
{
$type = strtolower(get_class($geometry));
if (is_null($data = $this->extract($geometry))) {
return null;
}
return strtoupper($type) . '(' . $data . ')';
}
/**
* Extract geometry to a WKT string
*
* @param Geometry\Geometry $geometry A Geometry object
*
* @return string
*/
public function extract(Geometry\Geometry $geometry)
{
$array = array();
switch (strtolower(get_class($geometry))) {
case self::POINT:
/** @var Geometry\Point $geometry */
return number_format($geometry->getX(), 1, '.', '') . ' ' .
number_format($geometry->getY(), 1, '.', '');
case self::MULTIPOINT:
case self::LINESTRING:
case self::LINEARRING:
foreach ($geometry as $geom) {
$array[] = $this->extract($geom);
}
return implode(',', $array);
case self::MULTILINESTRING:
case self::POLYGON:
case self::MULTIPOLYGON:
foreach ($geometry as $geom) {
$array[] = '(' . $this->extract($geom) . ')';
}
return implode(',', $array);
case self::GEOMETRYCOLLECTION:
foreach ($geometry as $geom) {
$array[] = strtoupper(get_class($geom)) . '(' . $this->extract($geom) . ')';
}
return implode(',', $array);
default:
return null;
}
}
/**
* Loads a WKT string into a Geometry Object
*
* @param string $WKT
*
* @return Geometry\Geometry
*/
static public function load($WKT)
{
$instance = new self;
return $instance->read($WKT);
}
/**
* Dumps a Geometry Object into a WKT string
*
* @param Geometry\Geometry $geometry
*
* @return String A WKT string corresponding to passed object
*/
static public function dump(Geometry\Geometry $geometry)
{
$instance = new self;
return $instance->write($geometry);
}
}