-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGisGeometryCollection.php
301 lines (258 loc) · 9.25 KB
/
GisGeometryCollection.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* Handles actions related to GIS GEOMETRYCOLLECTION objects
*/
declare(strict_types=1);
namespace PhpMyAdmin\Gis;
use ErrorException;
use PhpMyAdmin\Gis\Ds\Extent;
use PhpMyAdmin\Gis\Ds\ScaleData;
use PhpMyAdmin\Image\ImageWrapper;
use TCPDF;
use function count;
use function implode;
use function mb_substr;
use function str_split;
/**
* Handles actions related to GIS GEOMETRYCOLLECTION objects
*/
class GisGeometryCollection extends GisGeometry
{
private static self $instance;
/**
* A private constructor; prevents direct creation of object.
*/
private function __construct()
{
}
/**
* Returns the singleton.
*
* @return GisGeometryCollection the singleton
*/
public static function singleton(): GisGeometryCollection
{
if (! isset(self::$instance)) {
self::$instance = new GisGeometryCollection();
}
return self::$instance;
}
/**
* Get coordinate extent for this wkt.
*
* @param string $wkt Well Known Text represenatation of the geometry
*
* @return Extent the min, max values for x and y coordinates
*/
public function getExtent(string $wkt): Extent
{
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$geomCol = mb_substr($wkt, 19, -1);
// Split the geometry collection object to get its constituents.
$subParts = $this->explodeGeomCol($geomCol);
$extent = Extent::empty();
foreach ($subParts as $subPart) {
$gisObj = GisFactory::fromWkt($subPart);
if ($gisObj === null) {
continue;
}
$extent = $extent->merge($gisObj->getExtent($subPart));
}
return $extent;
}
/**
* Adds to the PNG image object, the data related to a row in the GIS dataset.
*
* @param string $spatial GIS POLYGON object
* @param string $label Label for the GIS POLYGON object
* @param int[] $color Color for the GIS POLYGON object
* @param ScaleData $scaleData Array containing data related to scaling
*/
public function prepareRowAsPng(
string $spatial,
string $label,
array $color,
ScaleData $scaleData,
ImageWrapper $image,
): void {
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$geomCol = mb_substr($spatial, 19, -1);
// Split the geometry collection object to get its constituents.
$subParts = $this->explodeGeomCol($geomCol);
foreach ($subParts as $subPart) {
$gisObj = GisFactory::fromWkt($subPart);
if ($gisObj === null) {
continue;
}
$gisObj->prepareRowAsPng($subPart, $label, $color, $scaleData, $image);
}
}
/**
* Adds to the TCPDF instance, the data related to a row in the GIS dataset.
*
* @param string $spatial GIS GEOMETRYCOLLECTION object
* @param string $label label for the GIS GEOMETRYCOLLECTION object
* @param int[] $color color for the GIS GEOMETRYCOLLECTION object
* @param ScaleData $scaleData array containing data related to scaling
*/
public function prepareRowAsPdf(
string $spatial,
string $label,
array $color,
ScaleData $scaleData,
TCPDF $pdf,
): void {
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$geomCol = mb_substr($spatial, 19, -1);
// Split the geometry collection object to get its constituents.
$subParts = $this->explodeGeomCol($geomCol);
foreach ($subParts as $subPart) {
$gisObj = GisFactory::fromWkt($subPart);
if ($gisObj === null) {
continue;
}
$gisObj->prepareRowAsPdf($subPart, $label, $color, $scaleData, $pdf);
}
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*
* @param string $spatial GIS GEOMETRYCOLLECTION object
* @param string $label label for the GIS GEOMETRYCOLLECTION object
* @param int[] $color color for the GIS GEOMETRYCOLLECTION object
* @param ScaleData $scaleData array containing data related to scaling
*
* @return string the code related to a row in the GIS dataset
*/
public function prepareRowAsSvg(string $spatial, string $label, array $color, ScaleData $scaleData): string
{
$row = '';
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$geomCol = mb_substr($spatial, 19, -1);
// Split the geometry collection object to get its constituents.
$subParts = $this->explodeGeomCol($geomCol);
foreach ($subParts as $subPart) {
$gisObj = GisFactory::fromWkt($subPart);
if ($gisObj === null) {
continue;
}
$row .= $gisObj->prepareRowAsSvg($subPart, $label, $color, $scaleData);
}
return $row;
}
/**
* Prepares data related to a row in the GIS dataset to visualize it with OpenLayers.
*
* @param string $spatial GIS GEOMETRYCOLLECTION object
* @param int $srid spatial reference ID
* @param string $label label for the GIS GEOMETRYCOLLECTION object
* @param int[] $color color for the GIS GEOMETRYCOLLECTION object
*
* @return mixed[]
*/
public function prepareRowAsOl(string $spatial, int $srid, string $label, array $color): array
{
$row = ['isCollection' => true, 'geometries' => []];
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$geomCol = mb_substr($spatial, 19, -1);
// Split the geometry collection object to get its constituents.
$subParts = $this->explodeGeomCol($geomCol);
foreach ($subParts as $subPart) {
$gisObj = GisFactory::fromWkt($subPart);
if ($gisObj === null) {
continue;
}
$row['geometries'][] = $gisObj->prepareRowAsOl($subPart, $srid, $label, $color);
}
return $row;
}
/**
* Splits the GEOMETRYCOLLECTION object and get its constituents.
*
* @param string $geomCol geometry collection string
*
* @return string[] the constituents of the geometry collection object
*/
private function explodeGeomCol(string $geomCol): array
{
$subParts = [];
$brCount = 0;
$start = 0;
$count = 0;
foreach (str_split($geomCol) as $char) {
if ($char === '(') {
$brCount++;
} elseif ($char === ')') {
$brCount--;
if ($brCount === 0) {
$subParts[] = mb_substr($geomCol, $start, $count + 1 - $start);
$start = $count + 2;
}
}
$count++;
}
return $subParts;
}
/**
* Generates the WKT with the set of parameters passed by the GIS editor.
*
* @param mixed[] $gisData GIS data
* @param int $index index into the parameter object
* @param string $empty value for empty points
*
* @return string WKT with the set of parameters passed by the GIS editor
*/
public function generateWkt(array $gisData, int $index, string $empty = ''): string
{
$geomCount = $gisData['GEOMETRYCOLLECTION']['data_length'] ?? 1;
$wktGeoms = [];
/** @infection-ignore-all */
for ($i = 0; $i < $geomCount; $i++) {
if (! isset($gisData[$i]['gis_type'])) {
continue;
}
$type = $gisData[$i]['gis_type'];
$gisObj = GisFactory::fromType($type);
if ($gisObj === null) {
continue;
}
$wktGeoms[] = $gisObj->generateWkt($gisData, $i, $empty);
}
return 'GEOMETRYCOLLECTION(' . implode(',', $wktGeoms) . ')';
}
/** @inheritDoc */
protected function getCoordinateParams(string $wkt): array
{
// GeometryCollection does not have coordinates of its own
throw new ErrorException('Has no own coordinates');
}
/**
* Generates parameters for the GIS data editor from the value of the GIS column.
*
* @param string $value of the GIS column
*
* @return mixed[] parameters for the GIS editor from the value of the GIS column
*/
public function generateParams(string $value): array
{
$data = $this->parseWktAndSrid($value);
$wkt = $data['wkt'];
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$geomCol = mb_substr($wkt, 19, -1);
$wktGeometries = $this->explodeGeomCol($geomCol);
$params = ['srid' => $data['srid'], 'GEOMETRYCOLLECTION' => ['data_length' => count($wktGeometries)]];
foreach ($wktGeometries as $wktGeometry) {
$gisObj = GisFactory::fromWkt($wktGeometry);
if ($gisObj === null) {
continue;
}
$wktType = $gisObj->getType();
$params[] = ['gis_type' => $wktType, $wktType => $gisObj->getCoordinateParams($wktGeometry)];
}
return $params;
}
protected function getType(): string
{
return 'GEOMETRYCOLLECTION';
}
}