forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_geofunc.h
1164 lines (1015 loc) · 34.5 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, 2023, 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 also distributed 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 included with MySQL.
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,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
/* This file defines all spatial functions */
#include "inplace_vector.h"
#include "item_cmpfunc.h" // Item_bool_func2
#include "prealloced_array.h"
#include "spatial.h" // gis_wkb_raw_free
#include "item_strfunc.h" // Item_str_func
#include "item_json_func.h" // Item_json_func
#include <vector>
/**
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(NULL), 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= NULL;
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= NULL;
}
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_spatial_operation;
/**
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;
Geometry::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()
:comp_no_overlapped(false), m_srid(0), m_num_isolated(0),
m_geobufs(key_memory_Geometry_objects_data),
m_geosdata(key_memory_Geometry_objects_data)
{}
bool is_comp_no_overlapped() const
{
return comp_no_overlapped;
}
void set_comp_no_overlapped(bool b)
{
comp_no_overlapped= b;
}
Geometry::srid_t get_srid() const
{
return m_srid;
}
void set_srid(Geometry::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;
}
Gis_geometry_collection *as_geometry_collection(String *geodata) const;
template<typename Coordsys>
void merge_components(my_bool *pnull_value);
private:
template<typename Coordsys>
bool merge_one_run(Item_func_spatial_operation *ifso,
my_bool *pnull_value);
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);
void fix_length_and_dec();
enum_field_types field_type() const { return MYSQL_TYPE_GEOMETRY; }
Field *tmp_table_field(TABLE *t_arg);
bool is_null() { (void) val_int(); return null_value; }
};
class Item_func_geometry_from_text: public Item_geometry_func
{
typedef Item_geometry_func super;
public:
Item_func_geometry_from_text(const POS &pos, Item *a)
:Item_geometry_func(pos, a)
{}
Item_func_geometry_from_text(const POS &pos, Item *a, Item *srid)
:Item_geometry_func(pos, a, srid)
{}
virtual bool itemize(Parse_context *pc, Item **res);
const char *func_name() const { return "st_geometryfromtext"; }
String *val_str(String *);
};
class Item_func_geometry_from_wkb: public Item_geometry_func
{
typedef Item_geometry_func super;
String tmp_value;
public:
Item_func_geometry_from_wkb(const POS &pos, Item *a)
: Item_geometry_func(pos, a)
{}
Item_func_geometry_from_wkb(const POS &pos, Item *a, Item *srid):
Item_geometry_func(pos, a, srid)
{}
virtual bool itemize(Parse_context *pc, Item **res);
const char *func_name() const { return "st_geometryfromwkb"; }
String *val_str(String *);
};
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) {}
const char *func_name() const { return "st_astext"; }
String *val_str_ascii(String *);
void fix_length_and_dec();
};
class Item_func_as_wkb: public Item_geometry_func
{
public:
Item_func_as_wkb(const POS &pos, Item *a): Item_geometry_func(pos, a) {}
const char *func_name() const { return "st_aswkb"; }
String *val_str(String *);
enum_field_types field_type() const { return MYSQL_TYPE_BLOB; }
};
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 *);
const char *func_name() const { return "st_geometrytype"; }
void fix_length_and_dec()
{
// "GeometryCollection" is the longest
fix_length_and_charset(20, default_charset());
maybe_null= 1;
};
};
/**
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 *);
void fix_length_and_dec();
bool fix_fields(THD *, Item **ref);
const char *func_name() const { 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,
Json_dom::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.
Geometry::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;
};
/// 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);
bool val_json(Json_wrapper *wr);
const char *func_name() const { return "st_asgeojson"; }
bool parse_options_argument();
bool parse_maxdecimaldigits_argument();
};
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 { return "st_centroid"; }
String *val_str(String *);
Field::geometry_type get_geometry_type() const;
};
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 { return "st_convexhull"; }
String *val_str(String *);
Field::geometry_type get_geometry_type() const;
};
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 { return "st_envelope"; }
String *val_str(String *);
Field::geometry_type get_geometry_type() const;
};
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 { return "st_makeenvelope"; }
String *val_str(String *);
Field::geometry_type get_geometry_type() const;
};
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 { return "st_validate"; }
String *val_str(String *);
};
class Item_func_simplify: public Item_geometry_func
{
BG_result_buf_mgr bg_resbuf_mgr;
String arg_val;
template <typename Coordsys>
int simplify_basic(Geometry *geom, double max_dist, String *str,
Gis_geometry_collection *gc= NULL,
String *gcbuf= NULL);
public:
Item_func_simplify(const POS &pos, Item *a, Item *b)
: Item_geometry_func(pos, a, b) {}
const char *func_name() const { return "st_simplify"; }
String *val_str(String *);
};
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 { return "point"; }
String *val_str(String *);
Field::geometry_type get_geometry_type() const;
};
/**
This handles the <point> = ST_POINTFROMGEOHASH(<string>, <srid>) funtion.
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].
At the moment, SRID can be any 32 bit unsigned integer.
*/
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 { return "st_pointfromgeohash"; }
String *val_str(String *);
bool fix_fields(THD *thd, Item **ref);
Field::geometry_type get_geometry_type() const
{
return Field::GEOM_POINT;
};
};
class Item_func_spatial_decomp: public Item_geometry_func
{
enum Functype decomp_func;
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
{
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 *);
};
class Item_func_spatial_decomp_n: public Item_geometry_func
{
enum Functype decomp_func_n;
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
{
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 *);
};
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 *);
void fix_length_and_dec()
{
Item_geometry_func::fix_length_and_dec();
for (unsigned int i= 0; i < arg_count; ++i)
{
if (args[i]->fixed && args[i]->field_type() != MYSQL_TYPE_GEOMETRY)
{
String str;
args[i]->print(&str, QT_NO_DATA_EXPANSION);
str.append('\0');
my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "non geometric",
str.ptr());
}
}
}
const char *func_name() const;
};
/*
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();
enum Functype functype() const
{
return spatial_rel;
}
enum Functype rev_functype() const
{
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;
virtual inline void print(String *str, enum_query_type query_type)
{
Item_func::print(str, query_type);
}
void fix_length_and_dec() { maybe_null= 1; }
bool is_null() { (void) val_int(); return null_value; }
};
class Item_func_spatial_rel: public Item_bool_func2
{
enum Functype spatial_rel;
String tmp_value1,tmp_value2;
public:
Item_func_spatial_rel(const POS &pos, Item *a,Item *b, enum Functype sp_rel);
virtual ~Item_func_spatial_rel();
longlong val_int();
enum Functype functype() const
{
return spatial_rel;
}
enum Functype rev_functype() const
{
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;
virtual inline void print(String *str, enum_query_type query_type)
{
Item_func::print(str, query_type);
}
void fix_length_and_dec() { maybe_null= 1; }
bool is_null() { (void) val_int(); return null_value; }
template<typename CoordinateSystemType>
static int bg_geo_relation_check(Geometry *g1, Geometry *g2,
Functype relchk_type, my_bool *);
protected:
template<typename Geom_types>
friend class BG_wrap;
template<typename Geotypes>
static int within_check(Geometry *g1, Geometry *g2,
my_bool *pnull_value);
template<typename Geotypes>
static int equals_check(Geometry *g1, Geometry *g2,
my_bool *pnull_value);
template<typename Geotypes>
static int disjoint_check(Geometry *g1, Geometry *g2,
my_bool *pnull_value);
template<typename Geotypes>
static int intersects_check(Geometry *g1, Geometry *g2,
my_bool *pnull_value);
template<typename Geotypes>
static int overlaps_check(Geometry *g1, Geometry *g2,
my_bool *pnull_value);
template<typename Geotypes>
static int touches_check(Geometry *g1, Geometry *g2,
my_bool *pnull_value);
template<typename Geotypes>
static int crosses_check(Geometry *g1, Geometry *g2,
my_bool *pnull_value);
template<typename Coordsys>
int multipoint_within_geometry_collection(Gis_multi_point *mpts,
const typename
BG_geometry_collection::
Geometry_list *gv2,
const void *prtree);
template<typename Coordsys>
int geocol_relation_check(Geometry *g1, Geometry *g2);
template<typename Coordsys>
int geocol_relcheck_intersect_disjoint(const typename BG_geometry_collection::
Geometry_list *gv1,
const typename BG_geometry_collection::
Geometry_list *gv2);
template<typename Coordsys>
int geocol_relcheck_within(const typename BG_geometry_collection::
Geometry_list *gv1,
const typename BG_geometry_collection::
Geometry_list *gv2);
template<typename Coordsys>
int geocol_equals_check(const typename BG_geometry_collection::
Geometry_list *gv1,
const typename BG_geometry_collection::
Geometry_list *gv2);
};
/*
Spatial operations
*/
class Item_func_spatial_operation: public Item_geometry_func
{
protected:
// It will call the protected member functions in this class,
// no data member accessed directly.
template<typename Geotypes>
friend class BG_setop_wrapper;
// Calls bg_geo_set_op.
friend class BG_geometry_collection;
template<typename Coordsys>
Geometry *bg_geo_set_op(Geometry *g1, Geometry *g2, String *result);
template<typename Coordsys>
Geometry *combine_sub_results(Geometry *g1, Geometry *g2, String *result);
Geometry *simplify_multilinestring(Gis_multi_line_string *mls,
String *result);
template<typename Coordsys>
Geometry *geometry_collection_set_operation(Geometry *g1, Geometry *g2,
String *result);
Geometry *empty_result(String *str, uint32 srid);
String tmp_value1,tmp_value2;
BG_result_buf_mgr bg_resbuf_mgr;
bool assign_result(Geometry *geo, String *result);
template <typename Geotypes>
Geometry *intersection_operation(Geometry *g1, Geometry *g2, String *result);
template <typename Geotypes>
Geometry *union_operation(Geometry *g1, Geometry *g2, String *result);
template <typename Geotypes>
Geometry *difference_operation(Geometry *g1, Geometry *g2, String *result);
template <typename Geotypes>
Geometry *symdifference_operation(Geometry *g1, Geometry *g2, String *result);
template<typename Coordsys>
Geometry *geocol_symdifference(const BG_geometry_collection &bggc1,
const BG_geometry_collection &bggc2,
String *result);
template<typename Coordsys>
Geometry *geocol_difference(const BG_geometry_collection &bggc1,
const BG_geometry_collection &bggc2,
String *result);
template<typename Coordsys>
Geometry *geocol_intersection(const BG_geometry_collection &bggc1,
const BG_geometry_collection &bggc2,
String *result);
template<typename Coordsys>
Geometry *geocol_union(const BG_geometry_collection &bggc1,
const BG_geometry_collection &bggc2,
String *result);
public:
enum op_type
{
op_shape= 0,
op_not= 0x80000000,
op_union= 0x10000000,
op_intersection= 0x20000000,
op_symdifference= 0x30000000,
op_difference= 0x40000000,
op_backdifference= 0x50000000,
op_any= 0x70000000
};
Item_func_spatial_operation(const POS &pos, Item *a, Item *b,
Item_func_spatial_operation::op_type sp_op) :
Item_geometry_func(pos, a, b), spatial_op(sp_op)
{
}
virtual ~Item_func_spatial_operation();
String *val_str(String *);
const char *func_name() const;
virtual inline void print(String *str, enum_query_type query_type)
{
Item_func::print(str, query_type);
}
private:
op_type spatial_op;
String m_result_buffer;
};
class Item_func_buffer: public Item_geometry_func
{
public:
/*
There are five types of buffer strategies, this is an enumeration of them.
*/
enum enum_buffer_strategy_types
{
invalid_strategy_type= 0,
end_strategy,
join_strategy,
point_strategy,
// The two below are not parameterized.
distance_strategy,
side_strategy
};
/*
For each type of strategy listed above, there are several options/values
for it, this is an enumeration of all such options/values for all types of
strategies.
*/
enum enum_buffer_strategies
{
invalid_strategy= 0,
end_round,
end_flat,
join_round,
join_miter,
point_circle,
point_square,
max_strategy= point_square
// Distance and side strategies are fixed, so no need to implement
// parameterization for them.
};
/*
A piece of strategy setting. User can specify 0 to 3 different strategy
settings in any order to ST_Buffer(), which must be of different
strategy types. Default strategies are used if not explicitly specified.
*/
struct Strategy_setting
{
enum_buffer_strategies strategy;
// This field is only effective for end_round, join_round, join_mit,
// and point_circle.
double value;
};
private:
BG_result_buf_mgr bg_resbuf_mgr;
int num_strats;
String *strategies[side_strategy + 1];
/*
end_xxx stored in settings[end_strategy];
join_xxx stored in settings[join_strategy];
point_xxx stored in settings[point_strategy].
*/
Strategy_setting settings[side_strategy + 1];
String tmp_value; // Stores current buffer result.
String m_tmp_geombuf;
void set_strategies();
public:
Item_func_buffer(const POS &pos, PT_item_list *ilist);
const char *func_name() const { return "st_buffer"; }
String *val_str(String *);
};
class Item_func_buffer_strategy: public Item_str_func
{
private:
friend class Item_func_buffer;
String tmp_value;
char tmp_buffer[16]; // The buffer for tmp_value.
public:
Item_func_buffer_strategy(const POS &pos, PT_item_list *ilist);
const char *func_name() const { return "st_buffer_strategy"; }
String *val_str(String *);
void fix_length_and_dec();
};
class Item_func_isempty: public Item_bool_func
{
public:
Item_func_isempty(const POS &pos, Item *a): Item_bool_func(pos, a) {}
longlong val_int();
optimize_type select_optimize() const { return OPTIMIZE_NONE; }
const char *func_name() const { return "st_isempty"; }
void fix_length_and_dec() { maybe_null= 1; }
};
class Item_func_issimple: public Item_bool_func
{
String tmp;
public:
Item_func_issimple(const POS &pos, Item *a): Item_bool_func(pos, a) {}
longlong val_int();
bool issimple(Geometry *g);
optimize_type select_optimize() const { return OPTIMIZE_NONE; }
const char *func_name() const { return "st_issimple"; }
void fix_length_and_dec() { maybe_null= 1; }
};
class Item_func_isclosed: public Item_bool_func
{
public:
Item_func_isclosed(const POS &pos, Item *a): Item_bool_func(pos, a) {}
longlong val_int();
optimize_type select_optimize() const { return OPTIMIZE_NONE; }
const char *func_name() const { return "st_isclosed"; }
void fix_length_and_dec() { maybe_null= 1; }
};
class Item_func_isvalid: public Item_bool_func
{
public:
Item_func_isvalid(const POS &pos, Item *a): Item_bool_func(pos, a) {}
longlong val_int();
optimize_type select_optimize() const { return OPTIMIZE_NONE; }
const char *func_name() const { return "st_isvalid"; }
};
class Item_func_dimension: public Item_int_func
{