forked from BrilliantPlugins/wp-geometa-lib
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-geoutil.php
818 lines (707 loc) · 21.1 KB
/
wp-geoutil.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
<?php
/**
* This class has geo utils that users and WP_Geo* classes might need.
*
* @package wp-geometa
* @link https://github.com/BrilliantPlugins/wp-geometa
* @author Michael Moore / michael.moore@luminfire.com / https://profiles.wordpress.org/stuporglue/
* @copyright LuminFire, 2016
* @license GNU GPL v2
*/
defined( 'ABSPATH' ) or die( 'No direct access' );
if ( !class_exists( 'WP_GeoUtil', false ) ) {
/**
* Some spatial utilities that are used by both WP_GeoQuery and WP_GeoMeta
* and which may be available to developers at some point.
*
* For now, their use is not recommended since things are still in flux.
*/
class WP_GeoUtil {
/**
* A GeoJSON reader (GeoPHP classes);.
*
* @var $geojson
*/
private static $geojson;
/**
* A WKT writer (GeoPHP classes);.
*
* @var $geowkt
*/
private static $geowkt;
/**
* EPSG:4326 is the web mercator project, such as is used by Google Maps
*
* @see https://en.wikipedia.org/wiki/World_Geodetic_System !
*
* @var $srid
*/
private static $srid;
/**
* This is a list of all known spatial functions in MySQL 5.4.2 to 5.7.6 and MariaDB 5.1 - 10.1.2
* We will test for capabilities by checking if the function exists instead of
* checking function names.
*
* @var $all_funcs
*/
public static $all_funcs = array(
'Area',
'AsBinary',
'AsText',
'AsWKB',
'AsWKT',
'Boundary',
'Buffer',
'Centroid',
'Contains',
'ConvexHull',
'Crosses',
'Dimension',
'Disjoint',
'Distance',
'EndPoint',
'Envelope',
'Equals',
'ExteriorRing',
'GeomCollFromText',
'GeomCollFromWKB',
'GeometryCollection',
'GeometryCollectionFromText',
'GeometryCollectionFromWKB',
'GeometryFromText',
'GeometryFromWKB',
'GeometryN',
'GeometryType',
'GeomFromText',
'GeomFromWKB',
'GLength',
'InteriorRingN',
'Intersects',
'IsClosed',
'IsEmpty',
'IsRing',
'IsSimple',
'LineFromText',
'LineFromWKB',
'LineString',
'LineStringFromText',
'LineStringFromWKB',
'MBRContains',
'MBRCoveredBy',
'MBRDisjoint',
'MBREqual',
'MBREquals',
'MBRIntersects',
'MBROverlaps',
'MBRTouches',
'MBRWithin',
'MLineFromText',
'MLineFromWKB',
'MPointFromText',
'MPointFromWKB',
'MPolyFromText',
'MPolyFromWKB',
'MultiLineString',
'MultiLineStringFromText',
'MultiLineStringFromWKB',
'MultiPoint',
'MultiPointFromText',
'MultiPointFromWKB',
'MultiPolygon',
'MultiPolygonFromText',
'MultiPolygonFromWKB',
'NumGeometries',
'NumInteriorRings',
'NumPoints',
'Overlaps',
'Point',
'PointFromText',
'PointFromWKB',
'PointOnSurface',
'PointN',
'PolyFromText',
'PolyFromWKB',
'Polygon',
'PolygonFromText',
'PolygonFromWKB',
'SRID',
'ST_Area',
'ST_AsBinary',
'ST_AsGeoJSON',
'ST_AsText',
'ST_AsWKB',
'ST_AsWKT',
'ST_Boundary',
'ST_Buffer',
'ST_Buffer_Strategy',
'ST_Centroid',
'ST_Contains',
'ST_ConvexHull',
'ST_Crosses',
'ST_Difference',
'ST_Dimension',
'ST_Disjoint',
'ST_Distance',
'ST_Distance_Sphere',
'ST_EndPoint',
'ST_Envelope',
'ST_Equals',
'ST_ExteriorRing',
'ST_GeoHash',
'ST_GeomCollFromText',
'ST_GeomCollFromWKB',
'ST_GeometryCollectionFromText',
'ST_GeometryCollectionFromWKB',
'ST_GeometryFromText',
'ST_GeometryFromWKB',
'ST_GeometryN',
'ST_GeometryType',
'ST_GeomFromGeoJSON',
'ST_GeomFromText',
'ST_GeomFromWKB',
'ST_InteriorRingN',
'ST_Intersection',
'ST_Intersects',
'ST_IsClosed',
'ST_IsEmpty',
'ST_IsRing',
'ST_IsSimple',
'ST_IsValid',
'ST_LatFromGeoHash',
'ST_Length',
'ST_LineFromText',
'ST_LineFromWKB',
'ST_LineStringFromText',
'ST_LineStringFromWKB',
'ST_LongFromGeoHash',
'ST_MakeEnvelope',
'ST_NumGeometries',
'ST_NumInteriorRings',
'ST_NumPoints',
'ST_Overlaps',
'ST_PointFromGeoHash',
'ST_PointFromText',
'ST_PointFromWKB',
'ST_PointOnSurface',
'ST_PointN',
'ST_PolyFromText',
'ST_PolyFromWKB',
'ST_PolygonFromText',
'ST_PolygonFromWKB',
'ST_Relate',
'ST_Simplify',
'ST_SRID',
'ST_StartPoint',
'ST_SymDifference',
'ST_Touches',
'ST_Union',
'ST_Validate',
'ST_Within',
'ST_X',
'ST_Y',
'StartPoint',
'Touches',
'Within',
'X',
'Y',
'WP_Bearing',
'WP_Bevier_CtrlPts',
'WP_Buffer_Point_M',
'WP_Buffer_Point_Mi',
'WP_Buffer_Point_Real',
'WP_Distance_Point_M',
'WP_Distance_Point_Mi',
'WP_Distance_Point_Real',
'WP_First_Geom',
'WP_Point_Bearing_Distance_To_Line',
'WP_Point_Bearing_Distance_To_Line_M',
'WP_Point_Bearing_Distance_To_Line_Mi',
'WP_Point_Bearing_Distance_Coord_Pair',
);
/**
* All the functions we detect as available in MySQL
*
* @var $found_funcs
*/
private static $found_funcs = array();
/**
* Has plugins loaded been run yet?
*
* @var $plugins_loaded_run
*/
public static $plugins_loaded_run = false;
/**
* The instance variable
*
* @var $_instance
*/
private static $_instance = null;
/**
* Get the singleton instance.
*/
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
global $wp_actions;
self::$_instance = new self;
self::$srid = 4326;
if ( isset( $wp_actions['plugins_loaded'] ) ) {
WP_GeoUtil::plugins_loaded();
} else {
add_action( 'plugins_loaded', array( 'WP_GeoUtil', 'plugins_loaded' ) );
}
spl_autoload_register( array( 'WP_GeoUtil', 'spl_autoload_register' ) );
}
return self::$_instance;
}
/**
* Handle autoloading when looking for the geoPHP class.
*
* @param string $class_name The class name that PHP is looking for.
*/
public static function spl_autoload_register( $class_name ) {
if ( in_array( $class_name, array( 'WKT', 'GeoJSON' ), true ) ) {
/**
* Include geoPHP for this function
*/
require_once( dirname( __FILE__ ) . '/geoPHP/geoPHP.inc' );
}
}
/**
* Load in the SRID after plugins are loaded.
* Check for any additional capabilities after plugins are loaded.
*/
public static function plugins_loaded() {
WP_GeoUtil::$srid = apply_filters( 'wpgm_geoquery_srid', 4326 );
/* This filter has been deprecated and will be removed in a future version. */
WP_GeoUtil::$srid = apply_filters( 'wp_geoquery_srid', WP_GeoUtil::$srid );
$orig_funcs = array_map( 'strtolower',WP_GeoUtil::$all_funcs );
WP_GeoUtil::get_all_funcs();
WP_GeoUtil::$plugins_loaded_run = true;
$new_funcs = array_map( 'strtolower',WP_GeoUtil::$all_funcs );
$diff = array_diff( $new_funcs, $orig_funcs );
if ( count( $diff ) > 0 ) {
WP_GeoUtil::get_capabilities( true, false );
}
}
/**
* This function loads all known functions, including applying filters to load from other plugins.
*/
public static function get_all_funcs() {
WP_GeoUtil::$all_funcs = apply_filters( 'wpgm_known_capabilities', WP_GeoUtil::$all_funcs );
/* This filter has been deprecated and will be removed in a future version. */
WP_GeoUtil::$all_funcs = apply_filters( 'wpgq_known_capabilities', WP_GeoUtil::$all_funcs );
WP_GeoUtil::$all_funcs = array_unique( WP_GeoUtil::$all_funcs );
return WP_GeoUtil::$all_funcs;
}
/**
* Merge one or more pieces of geojson together. Each item could be a FeatureCollection
* or an individual feature.
*
* All pieces will be combined to make a single FeatureCollection
*
* If only one piece is sent, then it will be converted into a FeatureCollection if
* it isn't already.
*
* @note This function takes as many geojson or geojson fragments as you want to pass in.
*
* @return A FeatureCollection GeoJSON array
*/
public static function merge_geojson() {
$fragments = func_get_args();
// Check if we've been given an array of fragments and act accordingly.
// If we don't have 'type' in our keys, then there's a good chance we might have an array of geojsons as our first arg.
if ( 1 === count( $fragments ) && is_array( $fragments[0] ) && ! array_key_exists( 'type', $fragments[0] ) ) {
$fragments = $fragments[0];
}
$ret = array(
'type' => 'FeatureCollection',
'features' => array(),
);
foreach ( $fragments as $fragment ) {
$fragment = maybe_unserialize( $fragment );
if ( is_object( $fragment ) ) {
$fragment = (array) $fragment;
} else if ( is_string( $fragment ) ) {
$utf8_fragment = @iconv( $encoding, "UTF-8//IGNORE", $fragment );
$fragment = json_decode( $utf8_fragment, true );
}
if ( ! is_array( $fragment ) ) {
return false;
}
$fragment = array_change_key_case( $fragment );
if ( ! array_key_exists( 'type',$fragment ) ) {
continue;
}
if ( 0 === strcasecmp( 'featurecollection',$fragment['type'] ) && is_array( $fragment['features'] ) ) {
$ret['features'] = array_merge( $ret['features'], $fragment['features'] );
} else if ( 0 === strcasecmp( 'feature', $fragment['type'] ) ) {
$ret['features'][] = $fragment;
}
}
if ( empty( $ret['features'] ) ) {
return false;
}
return wp_json_encode( $ret );
}
/**
* Convert a metaval to GeoJSON
*
* @param anything $metaval The value to turn into GeoJSON.
* @param string $return_type The supported types are 'string' and 'array'.
*/
public static function metaval_to_geojson( $metaval, $return_type = 'string' ) {
$metaval = maybe_unserialize( $metaval );
if ( self::is_geojson( $metaval ) ) {
return $metaval;
}
if ( empty( $metaval ) ) {
return false;
}
// Exit early if we're a non-GeoJSON string.
if ( is_string( $metaval ) ) {
if ( strpos( $metaval,'{' ) === false || strpos( $metaval,'Feature' ) === false || strpos( $metaval,'geometry' ) === false ) {
return false;
} else {
$metaval = json_decode( $metaval,true );
}
}
// If it's an object, cast it to an array for consistancy.
if ( is_object( $metaval ) ) {
$metaval = (array) $metaval;
}
// Last check!
$string_metaval = wp_json_encode( $metaval );
if ( ! self::is_geojson( $metaval ) ) {
return false;
}
if ( 'string' === $return_type ) {
return $string_metaval;
} else if ( 'array' === $return_type ) {
return $metaval;
}
}
/**
* We're going to support single GeoJSON features and FeatureCollections in either string, object or array format.
*
* @param mixed $metaval The meta value to try to convert to WKT.
* @param bool $force_multi Should the value be turned into a MULTI type geometry? Default is false. This is set to true by WP-GeoMeta before storing values in the database.
*
* @return A WKT geometry string.
*/
public static function metaval_to_geom( $metaval = false, $force_multi = false ) {
$maybe_geom = apply_filters( 'wpgm_metaval_to_geom', $metaval );
/* This filter has been deprecated and will be removed in a future version. */
$maybe_geom = apply_filters( 'wpgq_metaval_to_geom', $maybe_geom );
if ( self::is_geom( $maybe_geom ) ) {
return $maybe_geom;
}
// Everything becomes GeoJSON so that the rest of this function will be simpler.
$make_string = ( $force_multi ? 'string' : 'array' );
$metaval = self::metaval_to_geojson( $metaval, $make_string );
if ( false === $metaval ) {
return $metaval;
}
if ( $force_multi ) {
$metaval = self::merge_geojson( $metaval );
}
if ( false === $metaval ) {
return false;
}
// Stringify any arrays.
if ( is_array( $metaval ) ) {
$metaval = wp_json_encode( $metaval );
}
// Convert GeoJSON to WKT.
try {
$geom = self::get_geojson()->read( (string) $metaval );
if ( is_null( $geom ) ) {
return false;
}
} catch (Exception $e) {
return false;
}
try {
$wkt = self::get_wkt()->write( $geom );
/*
* MUTLI all the things because MySQL 5.7 (at least, maybe others) doesn't
* like Geometry in GeometryCollection columns.
*/
if ( $force_multi && false === strpos( $wkt, 'MULTI' ) ) {
if ( 0 === strpos( $wkt, 'POINT' ) ) {
$wkt = preg_replace( '@^POINT@','MULTIPOINT', $wkt );
} else if ( 0 === strpos( $wkt, 'LINE' ) || 0 === strpos( $wkt, 'POLYGON' ) ) {
$wkt = preg_replace( '@^(LINE|POLYGON)(\s*)(\(.*?\)[^,])@','MULTI$1$2($3)', $wkt );
}
}
return $wkt;
} catch (Exception $e) {
return false;
}
}
/**
* Convert WKT to GeoJSON
*
* @param geometry $wkt Convert a geometry of some sort to GeoJSON.
*
* @return A GeoJSON string.
*/
public static function geom_to_geojson( $wkt ) {
$maybe_geojson = apply_filters( 'wpgm_geom_to_geojson', $wkt );
/* This filter has been deprecated and will be removed in a future version. */
$maybe_geojson = apply_filters( 'wpgq_geom_to_geojson', $maybe_geojson );
if ( self::is_geojson( $maybe_geojson ) ) {
return $maybe_geojson;
}
// Don't know what to do non-strings.
if ( ! is_string( $maybe_geojson ) ) {
return false;
}
// WKT needs to start with one of these things.
$maybe_geojson = trim( $maybe_geojson );
if ( stripos( $maybe_geojson, 'POINT' ) !== 0 &&
stripos( $maybe_geojson, 'LINESTRING' ) !== 0 &&
stripos( $maybe_geojson, 'POLYGON' ) !== 0 &&
stripos( $maybe_geojson, 'MULTIPOINT' ) !== 0 &&
stripos( $maybe_geojson, 'MULTILINESTRING' ) !== 0 &&
stripos( $maybe_geojson, 'MULTIPOLYGON' ) !== 0 &&
stripos( $maybe_geojson, 'GEOMETRYCOLLECTION' ) !== 0
) {
return false;
}
try {
$geom = self::get_wkt()->read( $maybe_geojson );
$geojson = self::get_geojson()->write( $geom );
// Do we need to wrap it?
if ( ! empty( $geojson ) && strpos( $geojson, '"type":"Feature"' ) === false ) {
$geojson = '{"type":"Feature","geometry":' . $geojson . ',"properties":{}}';
}
return $geojson;
} catch ( Exception $e ) {
return false;
}
}
/**
* Check if a value is in WKT, which is our DB-ready format.
*
* @param string $maybe_geom Something which we want to check if it's WKT or not.
*
* @return bool
*/
public static function is_geom( $maybe_geom ) {
try {
if ( ! is_string( $maybe_geom ) ) {
return false;
}
if ( stripos( $maybe_geom, 'POINT' ) !== 0 &&
stripos( $maybe_geom, 'LINESTRING' ) !== 0 &&
stripos( $maybe_geom, 'POLYGON' ) !== 0 &&
stripos( $maybe_geom, 'MULTIPOINT' ) !== 0 &&
stripos( $maybe_geom, 'MULTILINESTRING' ) !== 0 &&
stripos( $maybe_geom, 'MULTIPOLYGON' ) !== 0 &&
stripos( $maybe_geom, 'GEOMETRYCOLLECTION' ) !== 0
) {
return false;
}
$what = self::get_wkt()->read( (string) $maybe_geom );
if ( null !== $what ) {
return true;
} else {
return false;
}
} catch (Exception $e) {
return false;
}
}
/**
* Check if a value is in GeoJSON, which is our code-ready forma.
*
* @param anything $maybe_geojson Check if a value is GeoJSON or not.
* @param bool $string_only Should GeoJSON compatible strings and objects be counted as GeoJSON? Default is false (allow arrays/objects).
*
* @return boolean
*/
public static function is_geojson( $maybe_geojson, $string_only = false ) {
try {
if ( ! is_string( $maybe_geojson ) && $string_only ) {
return false;
}
if ( is_array( $maybe_geojson ) || is_object( $maybe_geojson ) ) {
$maybe_geojson = wp_json_encode( $maybe_geojson );
}
$maybe_geojson = (string) $maybe_geojson;
if ( strpos( $maybe_geojson, '{' ) === false || strpos( $maybe_geojson, 'Feature' ) === false || strpos( $maybe_geojson, 'geometry' ) === false ) {
return false;
}
$what = self::get_geojson()->read( $maybe_geojson );
if ( null !== $what ) {
return true;
} else {
return false;
}
} catch (Exception $e) {
return false;
}
}
/**
* Fetch the found capabilities from the database
*
* If no capabilites are found, then generate them by running
* queries with each SQL function and seeing what the error
* message says
*
* @param bool $retest Should we re-check and re-store our capabilities.
* @param bool $lower Should all functions be lower-cased before returning.
* @param bool $cache_results Should our known functions be cached once they're generated.
*/
public static function get_capabilities( $retest = false, $lower = true, $cache_results = true ) {
global $wpdb;
if ( true !== WP_GeoUtil::$plugins_loaded_run ) {
WP_GeoUtil::plugins_loaded();
}
if ( ! $retest ) {
if ( empty( self::$found_funcs ) ) {
self::$found_funcs = get_option( 'wp_geometa_capabilities',array() );
}
if ( ! empty( self::$found_funcs ) ) {
if ( $lower ) {
return array_map( 'strtolower', self::$found_funcs );
} else {
return self::$found_funcs;
}
}
}
$suppress = $wpdb->suppress_errors( true );
$errors = $wpdb->show_errors( false );
// Reset it before adding stuff!
self::$found_funcs = array();
foreach ( WP_GeoUtil::$all_funcs as $func ) {
// First, check to see if a custom function exists.
$q = "SELECT IF( COUNT(*) = 0, 'F' , 'T' ) AS ProcedureExists FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '{$wpdb->dbname}' AND ROUTINE_TYPE = 'FUNCTION' AND UCASE(ROUTINE_NAME) = UCASE('$func');";
$custom_func = $wpdb->get_var( $q ); // @codingStandardsIgnoreLine
if ( 'T' === $custom_func ) {
self::$found_funcs[] = $func;
continue;
}
// Otherwise check if it's a built-in.
$q = "SELECT $func() AS worked";
$wpdb->query( $q ); // @codingStandardsIgnoreLine
if ( strpos( $wpdb->last_error,'Incorrect parameter count' ) !== false || strpos( $wpdb->last_error,'You have an error in your SQL syntax' ) !== false ) {
self::$found_funcs[] = $func;
}
}
// Re-set the error settings.
$wpdb->suppress_errors( $suppress );
$wpdb->show_errors( $errors );
if ( $cache_results ) {
update_option( 'wp_geometa_capabilities',self::$found_funcs, false );
}
return self::get_capabilities( false, $lower, $cache_results );
}
/**
* Get the srid
*/
public static function get_srid() {
return self::$srid;
}
/**
* Static magic method to support calling any geometry function.
*
* Eg. WP_GeoUtil::Buffer( $geometry, $distance);
*
* @param string $name The name of the function that is being called.
* @param array $arguments The arguments for the function.
*/
public static function __callStatic( $name, $arguments ) {
if ( in_array( strtolower( $name ),array_merge( self::get_capabilities(),array("linestring","point")), true ) ) {
return self::run_spatial_query( $name, $arguments );
}
}
/**
* Run the actual spatial query.
*
* Any geometries should be GeoJSON compatible.
*
* Geometry responses will be returned as GeoJSON.
*
* Other responses will be returned as is.
*
* @param string $name The name of the function that is being called.
* @param array $arguments The arguments for the function.
*/
private static function run_spatial_query( $name, $arguments = array() ) {
global $wpdb;
if ( empty( $arguments ) ) {
return false;
}
$q = 'SELECT ' . $name . '(';
foreach ( $arguments as $idx => $arg ) {
if ( $idx > 0 ) {
$q .= ',';
}
$maybe_geom = self::metaval_to_geom( $arg );
if ( false !== $maybe_geom ) {
$arguments[ $idx ] = $maybe_geom;
$q .= 'ST_GeomCollFromText(%s)';
} else {
$q .= '%s';
}
}
$q .= ')';
/*
-- Detect geometry.
-- Data is WKB, with a 4 byte leading SRID (which may be 00 00 00 00)
-- In big endian it will look like this:
-- E6 10 00 00 01 02 00
-- srid endianness WKB integer code
-- In little endian it should look like this:
-- 10 E6 01 01 00 00 02
-- srid endianness WKB integer code
-- Note: MySQL appears to use the Z codes, though I can't find actual support for Z values
*/
$real_q = 'SELECT IF(
COALESCE( SUBSTR(HEX(retval),5,10) IN (
-- big endian
\'0000010000\', -- geometry
\'0000010100\', -- point
\'0000010200\', -- line
\'0000010300\', -- polygon
\'0000010400\', -- multipoint
\'0000010500\', -- multiline
\'0000010600\', -- multipolygon
-- little endian
\'0001000000\', -- geometry
\'0001000001\', -- point
\'0001000002\', -- line
\'0001000003\', -- polygon
\'0001000004\', -- multipoint
\'0001000005\', -- multiline
\'0001000006\' -- multipolygon
), false) , ST_AsText( retval ), retval ) AS res FROM ( ' . $q . ' AS retval ) rq';
$sql = $wpdb->prepare( $real_q, $arguments ); // @codingStandardsIgnoreLine
$res = $wpdb->get_var( $sql ); // @codingStandardsIgnoreLine
$maybe_geojson = self::geom_to_geojson( $res );
if ( false !== $maybe_geojson ) {
return $maybe_geojson;
}
return $res;
}
/**
* Get the GeoJSON object, creating it if needed.
*/
private static function get_geojson() {
if ( ! isset( self::$geojson ) ) {
self::$geojson = new GeoJSON();
}
return self::$geojson;
}
/**
* Get the WKT object, creating it if needed.
*/
private static function get_wkt() {
if ( ! isset( self::$geowkt ) ) {
self::$geowkt = new WKT();
}
return self::$geowkt;
}
}
}