-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapDrawFeatures.php
More file actions
440 lines (386 loc) · 9.01 KB
/
MapDrawFeatures.php
File metadata and controls
440 lines (386 loc) · 9.01 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php namespace ProcessWire;
/**
* MapDrawFeatures Class
*
* @copyright 2023 NB Communication Ltd
* @license Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
*
* @property float $south
* @property float $west
* @property float $north
* @property float $east
* @property string $features
* @property float $zoom
*
* @property string $featureCollection
* @property array $points
* @property array $lineStrings
* @property array $polygons
*
* @method array getFeatures(string|array $type = '', array|string $lnglat = [])
* @method array getPoints(array|string $lnglat = [])
* @method array getLineStrings(array|string $lnglat = [])
* @method array getPolygons(array|string $lnglat = [])
* @method bool inPolygon(array $lnglat, array $polygon)
* @method bool isPoint(array $lnglat, array $point)
* @method bool onLineString(array $lnglat, array $lineString)
*
*/
class MapDrawFeatures extends WireData {
public function __construct() {
$this->set('south', 0.0);
$this->set('west', 0.0);
$this->set('north', 0.0);
$this->set('east', 0.0);
$this->set('features', '');
$this->set('zoom', 12.0);
}
/**
* Get
*
* @param string $key
* @return mixed
*
*/
public function get($key) {
$value = null;
switch($key) {
case 'bounds':
$value = [[$this->west, $this->south], [$this->east, $this->north]];
break;
case 'center':
$value = [($this->west + $this->east) / 2, ($this->south + $this->north) / 2];
break;
case 'featureCollection':
$value = json_encode([
'type' => 'FeatureCollection',
'features' => $this->featuresArray($this->features),
]);
break;
case 'points':
$value = $this->getPoints();
break;
case 'lineStrings':
$value = $this->getLineStrings();
break;
case 'lnglat':
$value = implode(',', $this->center);
break;
case 'polygons':
$value = $this->getPolygons();
break;
default:
$value = parent::get($key);
break;
}
return $value;
}
/**
* Set
*
* @param string $key
* @param mixed $value
* @return $this
*
*/
public function set($key, $value) {
switch($key) {
case 'bounds':
$south = 0;
$west = 0;
$north = 0;
$east = 0;
if(!is_array($value)) {
$value = json_decode($value, 1);
}
if(is_array($value)) {
$start = $value[0] ?? null;
if(is_array($start)) {
list($west, $south) = $value[0];
list($east, $north) = $value[1];
} else if(is_float($start)) {
list($west, $south, $east, $north) = $value;
}
}
if($south && $west && $north && $east) {
$this->set('south', $south);
$this->set('west', $west);
$this->set('north', $north);
$this->set('east', $east);
}
break;
case 'features':
$value = json_encode($this->featuresArray($value));
break;
case 'south':
case 'west':
case 'north':
case 'east':
case 'zoom':
$value = (float) $value;
break;
}
return parent::set($key, $value);
}
/**
* Get features, optionally by type
*
* @param string|array $type Point|LineString|Polygon|MultiPoint|MultiLineString|MultiPolygon
* @param array|string $lnglat
* @return array
*
*/
public function getFeatures($type = '', $lnglat = []) {
if(is_array($type) && !is_string($type[0] ?? '')) {
$lnglat = $type;
$type = '';
}
$featuresArray = $this->featuresArray($this->features);
$types = [];
foreach(is_array($type) ? $type : explode('|', $type) as $t) {
if(in_array($t, [
'Point',
'LineString',
'Polygon',
'MultiPoint',
'MultiLineString',
'MultiPolygon',
])) {
$types[] = strtolower($t);
}
}
if(!count($types) && empty($lnglat)) {
return $featuresArray;
}
$features = [];
foreach($featuresArray as $feature) {
$geometry = $feature['geometry'] ?? [];
if(empty($geometry)) continue;
// Is it the requested type?
if(!count($types) || in_array(strtolower($geometry['type'] ?? ''), $types)) {
// Attempt to match a given LngLat
if(!empty($lnglat)) {
if(is_string($lnglat)) {
if(strpos($lnglat, 'lng') !== false) {
$lnglat = json_decode($lnglat, 1);
$lnglat = [$lnglat['lng'], $lnglat['lat']];
} else {
$lnglat = explode(',', str_replace(' ', '', $lnglat));
}
}
if(is_array($lnglat)) {
list($lng, $lat) = $lnglat;
if($lng && $lat) {
$lnglat = [$lng, $lat];
$coordinates = $geometry['coordinates'];
$matches = false;
foreach($types as $t) {
switch($t) {
case 'point':
case 'multipoint':
if($this->isPoint($lnglat, $coordinates)) {
$matches = true;
}
break;
case 'linestring':
case 'multilinestring':
if($this->onLineString($lnglat, $coordinates)) {
$matches = true;
}
break;
case 'polygon':
case 'multipolygon':
if($this->inPolygon($lnglat, $coordinates)) {
$matches = true;
}
break;
}
}
if($matches) {
$features[] = $feature;
}
}
}
} else {
$features[] = $feature;
}
}
}
return $features;
}
/**
* Get Points
*
* @param string|array $lnglat
* @return array
*
*/
public function getPoints($lnglat = []) {
return $this->getFeatures('Point|MultiPoint', $lnglat);
}
/**
* Get LineStrings
*
* @param string|array $lnglat
* @return array
*
*/
public function getLineStrings($lnglat = []) {
return $this->getFeatures('LineString|MultiLineString', $lnglat);
}
/**
* Get Polygons
*
* @param string|array $lnglat
* @return array
*
*/
public function getPolygons($lnglat = []) {
return $this->getFeatures('Polygon|MultiPolygon', $lnglat);
}
/**
* Does the item has bounds set?
*
* @return bool
*
*/
public function hasBounds() {
return $this->south && $this->west && $this->north && $this->east;
}
/**
* Is the LngLat in the Polygon?
*
* #pw-advanced
*
* @param array $lnglat
* @param array $polygon
* @return bool
*
*/
public function inPolygon(array $lnglat, array $polygon) {
$in = false;
if(isset($polygon[0][0]) && is_float($polygon[0][0])) {
$x = $lnglat[1];
$y = $lnglat[0];
$len = count($polygon);
for ($i = 0, $j = $len - 1; $i < $len; $j = $i++) {
$xi = $polygon[$i][1];
$yi = $polygon[$i][0];
$xj = $polygon[$j][1];
$yj = $polygon[$j][0];
if ((($yi > $y) !== ($yj > $y)) && ($x < ($xj - $xi) * ($y - $yi) / ($yj - $yi) + $xi)) {
$in = !$in;
}
}
} else if(
isset($polygon[0][0][0]) && is_float($polygon[0][0][0]) || // Polygon
isset($polygon[0][0][0][0]) && is_float($polygon[0][0][0][0]) // MultiPolygon
) {
foreach($polygon as $p) {
if($this->inPolygon($lnglat, $p)) {
$in = true;
}
}
} else {
$this->log($this->_('Invalid polygon') . ': ' . json_encode($polygon, 1));
}
return $in;
}
/**
* Is the LngLat the Point?
*
* #pw-advanced
*
* @param array $lnglat
* @param array $point
* @return bool
*
*/
public function isPoint(array $lnglat, array $point) {
$_round = function($n) {
return (string) round((float) $n, 6);
};
$is = false;
if(is_array($point[0])) {
// MultiPoint
foreach($point as $p) {
if($this->isPoint($lnglat, $p)) {
$is = true;
}
}
} else {
$is = count($point) === 2 &&
count($lnglat) === 2 &&
$_round($point[0]) === $_round($lnglat[0]) &&
$_round($point[1]) === $_round($lnglat[1]);
}
return $is;
}
/**
* Is the LngLat on the LineString?
*
* #pw-advanced
*
* @param array $lnglat
* @param array $lineString
* @return bool
* @todo correct implementation that evaluates if the lnglat appears
* anywhere on the LineString, not just if it has the coordinate
*
*/
public function onLineString(array $lnglat, array $lineString) {
$on = false;
if(is_array($lineString[0][0])) {
// MultiLineString
foreach($lineString as $ls) {
foreach($ls as $l) {
if($this->isPoint($lnglat, $l)) {
$on = true;
}
}
}
} else {
foreach($lineString as $ls) {
if($this->isPoint($lnglat, $ls)) {
$on = true;
}
}
}
return $on;
}
/**
* Return the features correctly as an array
*
* Make empty arrays objects where required for correct JSON encoding
*
* @param string $features
* @return array
*
*/
private function featuresArray($features) {
$features = json_decode($features, 1);
if(is_array($features)) {
$keys = ['properties', 'geometry'];
foreach($features as $index => $feature) {
foreach($keys as $key) {
if(isset($feature[$key]) && is_array($feature[$key]) && empty($feature[$key])) {
$feature[$key] = (object) $feature[$key];
}
}
$features[$index] = $feature;
}
}
return $features ?? [];
}
/**
* If accessed as a string output as JSON
*
*/
public function __toString() {
return json_encode([
'bounds' => $this->bounds,
'features' => $this->featuresArray($this->features),
'zoom' => $this->zoom,
]);
}
}