-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathitem_geofunc.h
1908 lines (1684 loc) · 69.1 KB
/
item_geofunc.h
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef ITEM_GEOFUNC_INCLUDED
#define ITEM_GEOFUNC_INCLUDED
/* Copyright (c) 2000, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <assert.h>
#include <sys/types.h>
#include <cstddef>
#include <vector>
#include "field_types.h" // MYSQL_TYPE_BLOB
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "prealloced_array.h"
#include "sql/enum_query_type.h"
#include "sql/field.h"
#include "sql/gis/buffer_strategies.h" // gis::buffer_strategies
#include "sql/gis/srid.h"
#include "sql/parse_location.h" // POS
/* This file defines all spatial functions */
#include "sql/inplace_vector.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h" // Item_bool_func2
#include "sql/item_func.h"
#include "sql/item_json_func.h" // Item_json_func
#include "sql/item_strfunc.h" // Item_str_func
#include "sql/spatial.h" // gis_wkb_raw_free
#include "sql_string.h"
class Json_array;
class Json_dom;
class Json_object;
class Json_wrapper;
class PT_item_list;
class THD;
struct Parse_context;
struct TABLE;
enum class enum_json_type;
namespace dd {
class Spatial_reference_system;
} // namespace dd
namespace gis {
class Geometry;
class Point;
} // namespace gis
/**
We have to hold result buffers in functions that return a GEOMETRY string,
because such a function's result geometry's buffer is directly used and
set to String result object. We have to release them properly manually
since they won't be released when the String result is destroyed.
*/
class BG_result_buf_mgr {
typedef Prealloced_array<void *, 64> Prealloced_buffers;
public:
BG_result_buf_mgr() : bg_result_buf(nullptr), bg_results(PSI_INSTRUMENT_ME) {}
~BG_result_buf_mgr() {
free_intermediate_result_buffers();
free_result_buffer();
}
void add_buffer(void *buf) { bg_results.insert_unique(buf); }
void forget_buffer(void *buf) {
if (bg_result_buf == buf) bg_result_buf = nullptr;
bg_results.erase_unique(buf);
}
/* Free intermediate result buffers accumulated during GIS calculation. */
void free_intermediate_result_buffers() {
bg_results.erase_unique(bg_result_buf);
for (Prealloced_buffers::iterator itr = bg_results.begin();
itr != bg_results.end(); ++itr)
gis_wkb_raw_free(*itr);
bg_results.clear();
}
// Free the final result buffer, should be called after the result used.
void free_result_buffer() {
gis_wkb_raw_free(bg_result_buf);
bg_result_buf = nullptr;
}
void set_result_buffer(void *buf) {
bg_result_buf = buf;
bg_results.erase_unique(bg_result_buf);
}
private:
/*
Hold data buffer of this set operation's final result geometry which is
freed next time val_str is called since it can be used by upper Item nodes.
*/
void *bg_result_buf;
/*
Result buffers for intermediate set operation results, which are freed
before val_str returns.
*/
Prealloced_buffers bg_results;
};
class Item_func_st_union;
/**
A utility class to flatten any hierarchy of geometry collection into one
with no nested geometry collections. All components are stored separately
and all their data stored in this class, in order to easily manipulate them.
*/
class BG_geometry_collection {
bool comp_no_overlapped;
gis::srid_t m_srid;
size_t m_num_isolated;
std::vector<Geometry *> m_geos;
Inplace_vector<Geometry_buffer> m_geobufs;
Inplace_vector<String> m_geosdata;
public:
typedef std::vector<Geometry *> Geometry_list;
BG_geometry_collection();
bool is_comp_no_overlapped() const { return comp_no_overlapped; }
void set_comp_no_overlapped(bool b) { comp_no_overlapped = b; }
gis::srid_t get_srid() const { return m_srid; }
void set_srid(gis::srid_t srid) { m_srid = srid; }
bool fill(const Geometry *geo, bool break_multi_geom = false) {
return store_geometry(geo, break_multi_geom);
}
const Geometry_list &get_geometries() const { return m_geos; }
Geometry_list &get_geometries() { return m_geos; }
bool all_isolated() const { return m_num_isolated == m_geos.size(); }
size_t num_isolated() const { return m_num_isolated; }
private:
bool store_geometry(const Geometry *geo, bool break_multi_geom);
Geometry *store(const Geometry *geo);
};
class Item_geometry_func : public Item_str_func {
public:
Item_geometry_func() : Item_str_func() {}
Item_geometry_func(Item *a) : Item_str_func(a) {}
Item_geometry_func(const POS &pos, Item *a) : Item_str_func(pos, a) {}
Item_geometry_func(Item *a, Item *b) : Item_str_func(a, b) {}
Item_geometry_func(const POS &pos, Item *a, Item *b)
: Item_str_func(pos, a, b) {}
Item_geometry_func(Item *a, Item *b, Item *c) : Item_str_func(a, b, c) {}
Item_geometry_func(const POS &pos, Item *a, Item *b, Item *c)
: Item_str_func(pos, a, b, c) {}
Item_geometry_func(const POS &pos, PT_item_list *list);
bool resolve_type(THD *) override;
Field *tmp_table_field(TABLE *t_arg) override;
};
class Item_func_geometry_from_text : public Item_geometry_func {
public:
enum class Functype {
GEOMCOLLFROMTEXT,
GEOMCOLLFROMTXT,
GEOMETRYCOLLECTIONFROMTEXT,
GEOMETRYFROMTEXT,
GEOMFROMTEXT,
LINEFROMTEXT,
LINESTRINGFROMTEXT,
MLINEFROMTEXT,
MPOINTFROMTEXT,
MPOLYFROMTEXT,
MULTILINESTRINGFROMTEXT,
MULTIPOINTFROMTEXT,
MULTIPOLYGONFROMTEXT,
POINTFROMTEXT,
POLYFROMTEXT,
POLYGONFROMTEXT
};
private:
typedef Item_geometry_func super;
Functype m_functype;
/**
Get the type of geometry that this Item can return.
@return The geometry type
*/
Geometry::wkbType allowed_wkb_type() const;
/**
Check if a geometry type is a valid return type for this Item.
@param type The type to check
@retval true The geometry type is allowed
@retval false The geometry type is not allowed
*/
bool is_allowed_wkb_type(Geometry::wkbType type) const;
public:
Item_func_geometry_from_text(const POS &pos, Item *a, Functype functype)
: Item_geometry_func(pos, a), m_functype(functype) {}
Item_func_geometry_from_text(const POS &pos, Item *a, Item *srid,
Functype functype)
: Item_geometry_func(pos, a, srid), m_functype(functype) {}
Item_func_geometry_from_text(const POS &pos, Item *a, Item *srid,
Item *option, Functype functype)
: Item_geometry_func(pos, a, srid, option), m_functype(functype) {}
bool do_itemize(Parse_context *pc, Item **res) override;
const char *func_name() const override;
String *val_str(String *) override;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, 1)) return true;
if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_LONGLONG)) return true;
if (param_type_is_default(thd, 2, 3)) return true;
return Item_geometry_func::resolve_type(thd);
}
};
class Item_func_geometry_from_wkb : public Item_geometry_func {
public:
enum class Functype {
GEOMCOLLFROMWKB,
GEOMETRYCOLLECTIONFROMWKB,
GEOMETRYFROMWKB,
GEOMFROMWKB,
LINEFROMWKB,
LINESTRINGFROMWKB,
MLINEFROMWKB,
MPOINTFROMWKB,
MPOLYFROMWKB,
MULTILINESTRINGFROMWKB,
MULTIPOINTFROMWKB,
MULTIPOLYGONFROMWKB,
POINTFROMWKB,
POLYFROMWKB,
POLYGONFROMWKB
};
private:
typedef Item_geometry_func super;
String tmp_value;
Functype m_functype;
/**
Get the type of geometry that this Item can return.
@return The geometry type
*/
Geometry::wkbType allowed_wkb_type() const;
/**
Check if a geometry type is a valid return type for this Item.
@param type The type to check
@retval true The geometry type is allowed
@retval false The geometry type is not allowed
*/
bool is_allowed_wkb_type(Geometry::wkbType type) const;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, 1)) return true;
if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_LONGLONG)) return true;
if (param_type_is_default(thd, 2, 3)) return true;
return Item_geometry_func::resolve_type(thd);
}
public:
Item_func_geometry_from_wkb(const POS &pos, Item *a, Functype functype)
: Item_geometry_func(pos, a), m_functype(functype) {}
Item_func_geometry_from_wkb(const POS &pos, Item *a, Item *srid,
Functype functype)
: Item_geometry_func(pos, a, srid), m_functype(functype) {}
Item_func_geometry_from_wkb(const POS &pos, Item *a, Item *srid, Item *option,
Functype functype)
: Item_geometry_func(pos, a, srid, option), m_functype(functype) {}
bool do_itemize(Parse_context *pc, Item **res) override;
const char *func_name() const override;
String *val_str(String *) override;
};
class Item_func_as_wkt : public Item_str_ascii_func {
public:
Item_func_as_wkt(const POS &pos, Item *a) : Item_str_ascii_func(pos, a) {}
Item_func_as_wkt(const POS &pos, Item *a, Item *b)
: Item_str_ascii_func(pos, a, b) {}
const char *func_name() const override { return "st_astext"; }
String *val_str_ascii(String *) override;
bool resolve_type(THD *) override;
};
class Item_func_as_wkb : public Item_geometry_func {
public:
Item_func_as_wkb(const POS &pos, Item *a) : Item_geometry_func(pos, a) {}
Item_func_as_wkb(const POS &pos, Item *a, Item *b)
: Item_geometry_func(pos, a, b) {}
const char *func_name() const override { return "st_aswkb"; }
String *val_str(String *) override;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_GEOMETRY)) return true;
if (param_type_is_default(thd, 1, 2)) return true;
if (Item_geometry_func::resolve_type(thd)) return true;
set_data_type_blob(MYSQL_TYPE_LONG_BLOB, Field::MAX_LONG_BLOB_WIDTH);
return false;
}
};
class Item_func_geometry_type : public Item_str_ascii_func {
public:
Item_func_geometry_type(const POS &pos, Item *a)
: Item_str_ascii_func(pos, a) {}
String *val_str_ascii(String *) override;
const char *func_name() const override { return "st_geometrytype"; }
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_GEOMETRY)) return true;
// "MultiLinestring" is the longest
set_data_type_string(15, default_charset());
set_nullable(true);
return false;
}
};
/**
This handles one function:
@<geometry@> = ST_GEOMFROMGEOJSON(@<string@>[, @<options@>[, @<srid@>]])
Options is an integer argument which determines how positions with higher
coordinate dimension than MySQL support should be handled. The function will
accept both single objects, geometry collections and feature objects and
collections. All "properties" members of GeoJSON feature objects is ignored.
The implementation conforms with GeoJSON revision 1.0 described at
http://geojson.org/geojson-spec.html.
*/
class Item_func_geomfromgeojson : public Item_geometry_func {
public:
/**
Describing how coordinate dimensions higher than supported in MySQL
should be handled.
*/
enum enum_handle_coordinate_dimension {
reject_document,
strip_now_accept_future,
strip_now_reject_future,
strip_now_strip_future
};
Item_func_geomfromgeojson(const POS &pos, Item *json_string)
: Item_geometry_func(pos, json_string),
m_handle_coordinate_dimension(reject_document),
m_user_provided_srid(false),
m_srid_found_in_document(-1) {}
Item_func_geomfromgeojson(const POS &pos, Item *json_string, Item *options)
: Item_geometry_func(pos, json_string, options),
m_user_provided_srid(false),
m_srid_found_in_document(-1) {}
Item_func_geomfromgeojson(const POS &pos, Item *json_string, Item *options,
Item *srid)
: Item_geometry_func(pos, json_string, options, srid),
m_srid_found_in_document(-1) {}
String *val_str(String *) override;
bool fix_fields(THD *, Item **ref) override;
const char *func_name() const override { return "st_geomfromgeojson"; }
Geometry::wkbType get_wkbtype(const char *typestring);
bool get_positions(const Json_array *coordinates, Gis_point *point);
bool get_linestring(const Json_array *data_array,
Gis_line_string *linestring);
bool get_polygon(const Json_array *data_array, Gis_polygon *polygon);
bool parse_object(const Json_object *object, bool *rollback, String *buffer,
bool is_parent_featurecollection, Geometry **geometry);
bool parse_object_array(const Json_array *points, Geometry::wkbType type,
bool *rollback, String *buffer,
bool is_parent_featurecollection,
Geometry **geometry);
static bool check_argument_valid_integer(Item *argument);
bool parse_crs_object(const Json_object *crs_object);
bool is_member_valid(const Json_dom *member, const char *member_name,
enum_json_type expected_type, bool allow_null,
bool *was_null);
const Json_dom *my_find_member_ncase(const Json_object *object,
const char *member_name);
static const char *TYPE_MEMBER;
static const char *CRS_MEMBER;
static const char *GEOMETRY_MEMBER;
static const char *PROPERTIES_MEMBER;
static const char *FEATURES_MEMBER;
static const char *GEOMETRIES_MEMBER;
static const char *COORDINATES_MEMBER;
static const char *CRS_NAME_MEMBER;
static const char *NAMED_CRS;
static const char *SHORT_EPSG_PREFIX;
static const char *LONG_EPSG_PREFIX;
static const char *CRS84_URN;
static const char *POINT_TYPE;
static const char *MULTIPOINT_TYPE;
static const char *LINESTRING_TYPE;
static const char *MULTILINESTRING_TYPE;
static const char *POLYGON_TYPE;
static const char *MULTIPOLYGON_TYPE;
static const char *GEOMETRYCOLLECTION_TYPE;
static const char *FEATURE_TYPE;
static const char *FEATURECOLLECTION_TYPE;
private:
/**
How higher coordinate dimensions than currently supported should be handled.
*/
enum_handle_coordinate_dimension m_handle_coordinate_dimension;
/// Is set to true if user provided a SRID as an argument.
bool m_user_provided_srid;
/// The SRID user provided as an argument.
gis::srid_t m_user_srid;
/**
The SRID value of the document CRS, if one is found. Otherwise, this value
defaults to -1.
*/
longlong m_srid_found_in_document;
/// The minimum allowed longitude value (non-inclusive).
double m_min_longitude = -180.0;
/// The maximum allowed longitude (inclusive).
double m_max_longitude = 180.0;
/// The minimum allowed latitude value (inclusive).
double m_min_latitude = -90.0;
/// The maximum allowed latitude (inclusive).
double m_max_latitude = 90.0;
/// True if we're currently parsing the top-level object.
bool m_toplevel = true;
};
/// Max width of long CRS URN supported + max width of SRID + '\0'.
static const int MAX_CRS_WIDTH = (22 + MAX_INT_WIDTH + 1);
/**
This class handles the following function:
@<json@> = ST_ASGEOJSON(@<geometry@>[, @<maxdecimaldigits@>[, @<options@>]])
It converts a GEOMETRY into a valid GeoJSON string. If maxdecimaldigits is
specified, the coordinates written are rounded to the number of decimals
specified (e.g with decimaldigits = 3: 10.12399 => 10.124).
Options is a bitmask with the following flags:
0 No options (default values).
1 Add a bounding box to the output.
2 Add a short CRS URN to the output. The default format is a
short format ("EPSG:<srid>").
4 Add a long format CRS URN ("urn:ogc:def:crs:EPSG::<srid>"). This
implies 2. This means that, e.g., bitmask 5 and 7 mean the
same: add a bounding box and a long format CRS URN.
*/
class Item_func_as_geojson : public Item_json_func {
private:
/// Maximum number of decimal digits in printed coordinates.
int m_max_decimal_digits;
/// If true, the output GeoJSON has a bounding box for each GEOMETRY.
bool m_add_bounding_box;
/**
If true, the output GeoJSON has a CRS object in the short
form (e.g "EPSG:4326").
*/
bool m_add_short_crs_urn;
/**
If true, the output GeoJSON has a CRS object in the long
form (e.g "urn:ogc:def:crs:EPSG::4326").
*/
bool m_add_long_crs_urn;
/// The SRID found in the input GEOMETRY.
uint32 m_geometry_srid;
public:
Item_func_as_geojson(THD *thd, const POS &pos, Item *geometry)
: Item_json_func(thd, pos, geometry),
m_add_bounding_box(false),
m_add_short_crs_urn(false),
m_add_long_crs_urn(false) {}
Item_func_as_geojson(THD *thd, const POS &pos, Item *geometry,
Item *maxdecimaldigits)
: Item_json_func(thd, pos, geometry, maxdecimaldigits),
m_add_bounding_box(false),
m_add_short_crs_urn(false),
m_add_long_crs_urn(false) {}
Item_func_as_geojson(THD *thd, const POS &pos, Item *geometry,
Item *maxdecimaldigits, Item *options)
: Item_json_func(thd, pos, geometry, maxdecimaldigits, options),
m_add_bounding_box(false),
m_add_short_crs_urn(false),
m_add_long_crs_urn(false) {}
bool fix_fields(THD *thd, Item **ref) override;
bool val_json(Json_wrapper *wr) override;
const char *func_name() const override { return "st_asgeojson"; }
bool parse_options_argument();
bool parse_maxdecimaldigits_argument();
};
/**
This class handles two forms of the same function:
@<string@> = ST_GEOHASH(@<point@>, @<maxlength@>);
@<string@> = ST_GEOHASH(@<longitude@>, @<latitude@>, @<maxlength@>)
It returns an encoded geohash string, no longer than @<maxlength@> characters
long. Note that it might be shorter than @<maxlength@>.
*/
class Item_func_geohash : public Item_str_ascii_func {
private:
/// The latitude argument supplied by the user (directly or by a POINT).
double latitude;
/// The longitude argument supplied by the user (directly or by a POINT).
double longitude;
/// The maximum output length of the geohash, supplied by the user.
uint geohash_max_output_length;
/**
The maximum input latitude. For now, this is set to 90.0. It can be
changed to support a different range than the normal [90, -90].
*/
const double max_latitude;
/**
The minimum input latitude. For now, this is set to -90.0. It can be
changed to support a different range than the normal [90, -90].
*/
const double min_latitude;
/**
The maximum input longitude. For now, this is set to 180.0. It can be
changed to support a different range than the normal [180, -180].
*/
const double max_longitude;
/**
The minimum input longitude. For now, this is set to -180.0. It can be
changed to support a different range than the normal [180, -180].
*/
const double min_longitude;
/**
The absolute upper limit of geohash output length. User will get an error
if they supply a max geohash length argument greater than this.
*/
const uint upper_limit_output_length;
public:
Item_func_geohash(const POS &pos, Item *point, Item *length)
: Item_str_ascii_func(pos, point, length),
max_latitude(90.0),
min_latitude(-90.0),
max_longitude(180.0),
min_longitude(-180.0),
upper_limit_output_length(100) {}
Item_func_geohash(const POS &pos, Item *longitude, Item *latitude,
Item *length)
: Item_str_ascii_func(pos, longitude, latitude, length),
max_latitude(90.0),
min_latitude(-90.0),
max_longitude(180.0),
min_longitude(-180.0),
upper_limit_output_length(100) {}
String *val_str_ascii(String *) override;
bool resolve_type(THD *) override;
bool fix_fields(THD *thd, Item **ref) override;
const char *func_name() const override { return "st_geohash"; }
char char_to_base32(char char_input);
void encode_bit(double *upper_value, double *lower_value, double target_value,
char *char_value, int bit_number);
bool fill_and_check_fields();
bool check_valid_latlong_type(Item *ref);
};
/**
This is a superclass for Item_func_longfromgeohash and
Item_func_latfromgeohash, since they share almost all code.
*/
class Item_func_latlongfromgeohash : public Item_real_func {
private:
/**
The lower limit for latitude output value. Normally, this will be
set to -90.0.
*/
const double lower_latitude;
/**
The upper limit for latitude output value. Normally, this will be
set to 90.0.
*/
const double upper_latitude;
/**
The lower limit for longitude output value. Normally, this will
be set to -180.0.
*/
const double lower_longitude;
/**
The upper limit for longitude output value. Normally, this will
be set to 180.0.
*/
const double upper_longitude;
/**
If this is set to true the algorithm will start decoding on the first bit,
which decodes a longitude value. If it is false, it will start on the
second bit which decodes a latitude value.
*/
const bool start_on_even_bit;
public:
Item_func_latlongfromgeohash(const POS &pos, Item *a, double lower_latitude,
double upper_latitude, double lower_longitude,
double upper_longitude,
bool start_on_even_bit_arg)
: Item_real_func(pos, a),
lower_latitude(lower_latitude),
upper_latitude(upper_latitude),
lower_longitude(lower_longitude),
upper_longitude(upper_longitude),
start_on_even_bit(start_on_even_bit_arg) {}
double val_real() override;
bool resolve_type(THD *thd) override;
bool fix_fields(THD *thd, Item **ref) override;
static bool decode_geohash(String *geohash, double upper_latitude,
double lower_latitude, double upper_longitude,
double lower_longitude, double *result_latitude,
double *result_longitude);
static double round_latlongitude(double latlongitude, double error_range,
double lower_limit, double upper_limit);
static bool check_geohash_argument_valid_type(Item *item);
};
/**
This handles the @<double@> = ST_LATFROMGEOHASH(@<string@>) function.
It returns the latitude-part of a geohash, in the range of [-90, 90].
*/
class Item_func_latfromgeohash : public Item_func_latlongfromgeohash {
public:
Item_func_latfromgeohash(const POS &pos, Item *a)
: Item_func_latlongfromgeohash(pos, a, -90.0, 90.0, -180.0, 180.0,
false) {}
const char *func_name() const override { return "ST_LATFROMGEOHASH"; }
};
/**
This handles the @<double@> = ST_LONGFROMGEOHASH(@<string@>) function.
It returns the longitude-part of a geohash, in the range of [-180, 180].
*/
class Item_func_longfromgeohash : public Item_func_latlongfromgeohash {
public:
Item_func_longfromgeohash(const POS &pos, Item *a)
: Item_func_latlongfromgeohash(pos, a, -90.0, 90.0, -180.0, 180.0, true) {
}
const char *func_name() const override { return "ST_LONGFROMGEOHASH"; }
};
class Item_func_centroid : public Item_geometry_func {
BG_result_buf_mgr bg_resbuf_mgr;
template <typename Coordsys>
bool bg_centroid(const Geometry *geom, String *ptwkb);
public:
Item_func_centroid(const POS &pos, Item *a) : Item_geometry_func(pos, a) {}
const char *func_name() const override { return "st_centroid"; }
String *val_str(String *) override;
Field::geometry_type get_geometry_type() const override;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_GEOMETRY)) return true;
return Item_geometry_func::resolve_type(thd);
}
};
class Item_func_convex_hull : public Item_geometry_func {
BG_result_buf_mgr bg_resbuf_mgr;
template <typename Coordsys>
bool bg_convex_hull(const Geometry *geom, String *wkb);
public:
Item_func_convex_hull(const POS &pos, Item *a) : Item_geometry_func(pos, a) {}
const char *func_name() const override { return "st_convexhull"; }
String *val_str(String *) override;
Field::geometry_type get_geometry_type() const override;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_GEOMETRY)) return true;
return Item_geometry_func::resolve_type(thd);
}
};
class Item_func_envelope : public Item_geometry_func {
public:
Item_func_envelope(const POS &pos, Item *a) : Item_geometry_func(pos, a) {}
const char *func_name() const override { return "st_envelope"; }
String *val_str(String *) override;
Field::geometry_type get_geometry_type() const override;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_GEOMETRY)) return true;
return Item_geometry_func::resolve_type(thd);
}
};
class Item_func_make_envelope : public Item_geometry_func {
public:
Item_func_make_envelope(const POS &pos, Item *a, Item *b)
: Item_geometry_func(pos, a, b) {}
const char *func_name() const override { return "st_makeenvelope"; }
String *val_str(String *) override;
Field::geometry_type get_geometry_type() const override;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_GEOMETRY)) return true;
return Item_geometry_func::resolve_type(thd);
}
};
class Item_func_validate : public Item_geometry_func {
String arg_val;
public:
Item_func_validate(const POS &pos, Item *a) : Item_geometry_func(pos, a) {}
const char *func_name() const override { return "st_validate"; }
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_GEOMETRY)) return true;
return Item_geometry_func::resolve_type(thd);
}
String *val_str(String *) override;
};
/// Item that implements function ST_Simplify, which simplifies a geometry using
/// the Douglas-Peucker algorithm.
class Item_func_st_simplify : public Item_geometry_func {
public:
Item_func_st_simplify(const POS &pos, Item *a, Item *b)
: Item_geometry_func(pos, a, b) {}
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_GEOMETRY)) return true;
if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_DOUBLE)) return true;
return Item_geometry_func::resolve_type(thd);
}
String *val_str(String *) override;
const char *func_name() const override { return "st_simplify"; }
};
class Item_func_point : public Item_geometry_func {
public:
Item_func_point(const POS &pos, Item *a, Item *b)
: Item_geometry_func(pos, a, b) {}
const char *func_name() const override { return "point"; }
String *val_str(String *) override;
Field::geometry_type get_geometry_type() const override;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_DOUBLE)) return true;
return Item_geometry_func::resolve_type(thd);
}
};
/**
This handles the @<point@> = ST_POINTFROMGEOHASH(@<string@>, @<srid@>)
function.
It returns a point containing the decoded geohash value, where X is the
longitude in the range of [-180, 180] and Y is the latitude in the range
of [-90, 90].
*/
class Item_func_pointfromgeohash : public Item_geometry_func {
private:
/// The maximum output latitude value when decoding the geohash value.
const double upper_latitude;
/// The minimum output latitude value when decoding the geohash value.
const double lower_latitude;
/// The maximum output longitude value when decoding the geohash value.
const double upper_longitude;
/// The minimum output longitude value when decoding the geohash value.
const double lower_longitude;
public:
Item_func_pointfromgeohash(const POS &pos, Item *a, Item *b)
: Item_geometry_func(pos, a, b),
upper_latitude(90.0),
lower_latitude(-90.0),
upper_longitude(180.0),
lower_longitude(-180.0) {}
const char *func_name() const override { return "st_pointfromgeohash"; }
String *val_str(String *) override;
bool fix_fields(THD *thd, Item **ref) override;
bool resolve_type(THD *thd) override;
Field::geometry_type get_geometry_type() const override {
return Field::GEOM_POINT;
}
};
class Item_func_spatial_decomp : public Item_geometry_func {
enum Functype decomp_func;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_GEOMETRY)) return true;
return Item_geometry_func::resolve_type(thd);
}
public:
Item_func_spatial_decomp(const POS &pos, Item *a, Item_func::Functype ft)
: Item_geometry_func(pos, a) {
decomp_func = ft;
}
const char *func_name() const override {
switch (decomp_func) {
case SP_STARTPOINT:
return "st_startpoint";
case SP_ENDPOINT:
return "st_endpoint";
case SP_EXTERIORRING:
return "st_exteriorring";
default:
assert(0); // Should never happened
return "spatial_decomp_unknown";
}
}
String *val_str(String *) override;
};
class Item_func_spatial_decomp_n : public Item_geometry_func {
enum Functype decomp_func_n;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_GEOMETRY)) return true;
if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_LONGLONG)) return true;
return Item_geometry_func::resolve_type(thd);
}
public:
Item_func_spatial_decomp_n(const POS &pos, Item *a, Item *b,
Item_func::Functype ft)
: Item_geometry_func(pos, a, b) {
decomp_func_n = ft;
}
const char *func_name() const override {
switch (decomp_func_n) {
case SP_POINTN:
return "st_pointn";
case SP_GEOMETRYN:
return "st_geometryn";
case SP_INTERIORRINGN:
return "st_interiorringn";
default:
assert(0); // Should never happened
return "spatial_decomp_n_unknown";
}
}
String *val_str(String *) override;
};
class Item_func_spatial_collection : public Item_geometry_func {
String tmp_value;
enum Geometry::wkbType coll_type;
enum Geometry::wkbType item_type;
public:
Item_func_spatial_collection(const POS &pos, PT_item_list *list,
enum Geometry::wkbType ct,
enum Geometry::wkbType it)
: Item_geometry_func(pos, list) {
coll_type = ct;
item_type = it;
}
String *val_str(String *) override;
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_GEOMETRY)) return true;
if (Item_geometry_func::resolve_type(thd)) return true;
for (unsigned int i = 0; i < arg_count; ++i) {
if (args[i]->fixed && args[i]->data_type() != MYSQL_TYPE_GEOMETRY) {
String str;
args[i]->print(thd, &str, QT_NO_DATA_EXPANSION);
str.append('\0');
my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "non geometric", str.ptr());
return true;
}
}
return false;
}
const char *func_name() const override;
};
/*
Spatial relations
*/
class Item_func_spatial_mbr_rel : public Item_bool_func2 {
enum Functype spatial_rel;
public:
Item_func_spatial_mbr_rel(Item *a, Item *b, enum Functype sp_rel)
: Item_bool_func2(a, b) {
spatial_rel = sp_rel;
}
Item_func_spatial_mbr_rel(const POS &pos, Item *a, Item *b,
enum Functype sp_rel)
: Item_bool_func2(pos, a, b) {
spatial_rel = sp_rel;
}
longlong val_int() override;
enum Functype functype() const override { return spatial_rel; }
enum Functype rev_functype() const override {
switch (spatial_rel) {
case SP_CONTAINS_FUNC:
return SP_WITHIN_FUNC;
case SP_WITHIN_FUNC:
return SP_CONTAINS_FUNC;
default:
return spatial_rel;
}
}
const char *func_name() const override;
void print(const THD *thd, String *str,
enum_query_type query_type) const override {
Item_func::print(thd, str, query_type);
}
bool resolve_type(THD *) override {
set_nullable(true);
return false;
}
bool is_null() override {
val_int();
return null_value;
}
};
class Item_func_spatial_relation : public Item_bool_func2 {
public:
Item_func_spatial_relation(const POS &pos, Item *a, Item *b)
: Item_bool_func2(pos, a, b) {}
bool resolve_type(THD *thd) override {
if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_GEOMETRY)) return true;
// Spatial relation functions may return NULL if either parameter is NULL or
// an empty geometry. Since we can't check for empty geometries at resolve
// time, this item is always nullable.
set_nullable(true);
return false;
}
void print(const THD *thd, String *str,
enum_query_type query_type) const override {
Item_func::print(thd, str, query_type);
}
longlong val_int() override;
bool is_null() override {
// The superclass implementation only checks is_null on the item's
// arguments. However, relational functions may return NULL even if the
// arguments are not NULL, e.g., if one or more argument is an empty
// geometry. Therefore, we must evaluate the item to find out if it is NULL
// or not.
val_int();
return null_value;
}
/**
Evaluate the spatial relation function.
@param[in] srs Spatial reference system common to both g1 and g2.