-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathspatial.h
2490 lines (2119 loc) · 82.6 KB
/
spatial.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
/* Copyright (c) 2002, 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 */
#ifndef SPATIAL_INCLUDED
#define SPATIAL_INCLUDED
#include <assert.h>
#include <float.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <cstddef>
#include <iterator>
#include "lex_string.h"
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_inttypes.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/service_mysql_alloc.h"
#include "sql/gis/srid.h"
#include "sql/inplace_vector.h"
#include "sql_string.h" // String
#include "unsafe_string_append.h"
class Gis_read_stream;
class THD;
const uint GEOM_DIM = 2;
const uint SRID_SIZE = 4;
const uint SIZEOF_STORED_DOUBLE = 8;
const uint POINT_DATA_SIZE = (SIZEOF_STORED_DOUBLE * 2);
const uint WKB_HEADER_SIZE = (1 + 4);
const uint GEOM_HEADER_SIZE = (SRID_SIZE + WKB_HEADER_SIZE);
const uint32 GET_SIZE_ERROR = 0xFFFFFFFFU;
/**
Point with coordinates X and Y.
*/
class point_xy {
public:
double x;
double y;
point_xy() = default;
point_xy(double x_arg, double y_arg) : x(x_arg), y(y_arg) {}
double distance(const point_xy &p) const;
/**
Compare to another point.
Return true if equal, false if not equal.
*/
bool eq(point_xy p) const { return (x == p.x) && (y == p.y); }
};
typedef struct wkb_header_st {
uchar byte_order;
uint32 wkb_type;
} wkb_header;
/***************************** MBR *******************************/
struct MBR {
double xmin, ymin, xmax, ymax;
MBR() {
xmin = ymin = DBL_MAX;
xmax = ymax = -DBL_MAX;
}
MBR(const double xmin_arg, const double ymin_arg, const double xmax_arg,
const double ymax_arg)
: xmin(xmin_arg), ymin(ymin_arg), xmax(xmax_arg), ymax(ymax_arg) {}
MBR(const point_xy &min, const point_xy &max)
: xmin(min.x), ymin(min.y), xmax(max.x), ymax(max.y) {}
void add_xy(double x, double y) {
/* Not using "else" for proper one point MBR calculation */
if (x < xmin) xmin = x;
if (x > xmax) xmax = x;
if (y < ymin) ymin = y;
if (y > ymax) ymax = y;
}
void add_xy(point_xy p) { add_xy(p.x, p.y); }
void add_xy(const char *px, const char *py) {
const double x = float8get(px);
const double y = float8get(py);
add_xy(x, y);
}
void add_mbr(const MBR *mbr) {
if (mbr->xmin < xmin) xmin = mbr->xmin;
if (mbr->xmax > xmax) xmax = mbr->xmax;
if (mbr->ymin < ymin) ymin = mbr->ymin;
if (mbr->ymax > ymax) ymax = mbr->ymax;
}
int equals(const MBR *mbr) const {
/* The following should be safe, even if we compare doubles */
return ((mbr->xmin == xmin) && (mbr->ymin == ymin) && (mbr->xmax == xmax) &&
(mbr->ymax == ymax));
}
int disjoint(const MBR *mbr) const {
/* The following should be safe, even if we compare doubles */
return ((mbr->xmin > xmax) || (mbr->ymin > ymax) || (mbr->xmax < xmin) ||
(mbr->ymax < ymin));
}
int intersects(const MBR *mbr) const { return !disjoint(mbr); }
int within(const MBR *mbr) const;
int contains(const MBR *mbr) const { return mbr->within(this); }
int covered_by(const MBR *mbr) const {
/* The following should be safe, even if we compare doubles */
return ((mbr->xmin <= xmin) && (mbr->ymin <= ymin) && (mbr->xmax >= xmax) &&
(mbr->ymax >= ymax));
}
int covers(const MBR *mbr) const { return mbr->covered_by(this); }
bool inner_point(double x, double y) const {
/* The following should be safe, even if we compare doubles */
return (xmin < x) && (xmax > x) && (ymin < y) && (ymax > y);
}
/**
The dimension maps to an integer as:
- Polygon -> 2
- Horizontal or vertical line -> 1
- Point -> 0
- Invalid MBR -> -1
*/
int dimension() const {
int d = 0;
if (xmin > xmax)
return -1;
else if (xmin < xmax)
d++;
if (ymin > ymax)
return -1;
else if (ymin < ymax)
d++;
return d;
}
int overlaps(const MBR *mbr) const {
/*
overlaps() requires that some point inside *this is also inside
*mbr, and that both geometries and their intersection are of the
same dimension.
*/
const int d = dimension();
assert(d >= 0 && d <= 2);
if (d != mbr->dimension() || d == 0 || contains(mbr) || within(mbr))
return 0;
MBR intersection(std::max(xmin, mbr->xmin), std::max(ymin, mbr->ymin),
std::min(xmax, mbr->xmax), std::min(ymax, mbr->ymax));
return (d == intersection.dimension());
}
};
/***************************** Geometry *******************************/
struct Geometry_buffer;
/*
Memory management functions for BG adapter code. Allocate extra space for
GEOMETRY header so that we can later prefix the header if needed.
*/
void *gis_wkb_alloc(size_t sz);
inline void *gis_wkb_fixed_alloc(size_t sz) { return gis_wkb_alloc(sz); }
void *gis_wkb_realloc(void *p, size_t sz);
inline void gis_wkb_free(void *p) {
if (p == nullptr) return;
char *cp = static_cast<char *>(p);
my_free(cp - GEOM_HEADER_SIZE);
}
inline void gis_wkb_raw_free(void *p) { my_free(p); }
class Geometry {
friend void parse_wkb_data(Geometry *geom, const char *p, size_t num_geoms);
protected:
// Flag bits for m_flags.props.
/*
Whether the linestring is a polygon's outer ring, or inner ring.
*/
const static int POLYGON_OUTER_RING = 0x1;
const static int POLYGON_INNER_RING = 0x2;
/*
Whether the Geometry object is created to be used by Boost Geometry or
only by MySQL. There are some operations that only work for one type and
can or must be skipped otherwise. This state is transient and mutable, we
set it even to a const geometry object.
*/
const static int IS_BOOST_GEOMETRY_ADAPTER = 0x4;
/*
Whether the geometry length is verified, so that we can return the stored
length without having to parse the WKB again.
*/
const static int GEOM_LENGTH_VERIFIED = 0x8;
/*
Whether the geometry has components stored out of line, see
Gis_wkb_vector<>::resize for details.
*/
const static int HAS_OUT_OF_LINE_COMPONENTS = 0x10;
/*
Whether the polygon's data is in WKB form, as is so in MySQL, or it's in
BG form, where the m_ptr points to an outer ring object, and m_inn_rings
points to the inner rings. See Gis_polygon for more information.
*/
const static int POLYGON_IN_WKB_FORM = 0x20;
/*
whether the geometry's data buffer has space for a GEOMETRY header.
BG adapter code use gis_wkb_alloc to allocate WKB buffer for Geometry
objects, they always has such space. Gis_geometry_collection created
from a single geometry and then appended with more geometries also have
such space. Those with such space we can simply prefix the GEOMETRY header
into its buffer without copying its WKB data.
*/
const static int HAS_GEOM_HEADER_SPACE = 0x40;
/*
Whether the multi geometry has overlapped components, if false(the bit set)
this geometry will be skipped from merge-component operation.
Effective only for multipolygons, multilinestrings and geometry collections.
Such geometries returned by BG always has this bit set, i.e. their
components don't overlap.
*/
const static int MULTIPOLYGON_NO_OVERLAPPED_COMPS = 0x80;
public:
// Check user's transmitted data against these limits.
const static uint32 MAX_GEOM_WKB_LENGTH = 0x3fffffff;
const static gis::srid_t default_srid = 0;
virtual ~Geometry();
/*
We have to define a wkb_first and wkb_invalid_type and set them to 0
because Geometry objects stored in m_geo_vect vector can be constructed
using the default constructur Geometry() which sets geotype to 0, and
there are asserts in BG adapter code which asserts geotype is in valid
range [wkb_first, wkb_last]. Neither items will be treated as valid
geometry types.
wkb_first and wkb_last are only intended to be used to express a valid
range of wkbType values, other items are to be used as real type values.
*/
enum wkbType {
wkb_invalid_type = 0,
wkb_first = 1,
wkb_point = 1,
wkb_linestring = 2,
wkb_polygon = 3,
wkb_multipoint = 4,
wkb_multilinestring = 5,
wkb_multipolygon = 6,
wkb_geometrycollection = 7,
/*
OGC defines 10 more basic geometry types for values 8 to 17, we don't
support them now so don't define them. And there may be more of
them defined in the future. Since we will need 5 bits anyway, we grow
from 31 down to 18 for our extra private types instead of from 18 to 31,
to avoid potential data file format binary compatibility issues, which
would occur if OGC defined more basic types and we would support them.
*/
wkb_polygon_inner_rings = 31,
wkb_last = 31
};
enum wkbByteOrder {
wkb_xdr = 0, /* Big Endian */
wkb_ndr = 1, /* Little Endian */
wkb_invalid
};
enum enum_coordinate_reference_system {
coord_first = 1,
cartesian = 1,
coord_last = 1
};
static String bad_geometry_data;
/**
Constant storage for WKB.
Encapsulation and the available methods make it impossible
to update the members of wkb_container once it is initialized.
The only allowed modification method is set(),
which fully replaces the previous buffer.
*/
class wkb_container {
protected:
const char *m_data;
const char *m_data_end;
public:
wkb_container() = default;
wkb_container(const char *data, const char *data_end) {
set(data, data_end);
}
void set(const char *data, const char *data_end) {
m_data = data;
m_data_end = data_end;
}
const char *data() const { return m_data; }
const char *data_end() const { return m_data_end; }
uint32 length() const { return (uint32)(m_data_end - m_data); }
/**
Check if there's enough data remaining as requested.
@arg data_amount data requested
@return true if not enough data
*/
bool no_data(size_t data_amount) const {
return (m_data + data_amount > m_data_end);
}
/**
Check if there're enough points remaining as requested.
Need to perform the calculation in logical units, since multiplication
can overflow the size data type.
@arg expected_points number of points expected
@arg extra_point_space extra space for each point element in the array
@return true if there are not enough points
*/
bool not_enough_points(uint32 expected_points,
uint32 extra_point_space = 0) const {
return (m_data_end < m_data ||
expected_points > ((m_data_end - m_data) /
(POINT_DATA_SIZE + extra_point_space)));
}
};
/**
WKB parser, designed to traverse through WKB data from
beginning of the buffer towards the end using a set
of scan_xxx(), get_xxx() and skip_xxx() routines,
with safety tests to avoid going beyond the buffer end.
*/
class wkb_parser : public wkb_container {
/* Low level routines to get data of various types */
void get_uint4(uint32 *number) {
*number = uint4korr(m_data); // GIS-TODO: byte order
}
void get_float8(double *x) {
*x = float8get(m_data); // GIS-TODO: byte order
}
public:
wkb_parser(const char *data, const char *data_end)
: wkb_container(data, data_end) {}
/* Routines to skip non-interesting data */
void skip_unsafe(size_t nbytes) {
assert(!no_data(nbytes));
m_data += nbytes;
}
bool skip(size_t nbytes) {
if (no_data(nbytes)) return true;
m_data += nbytes;
return false;
}
bool skip_wkb_header() { return skip(WKB_HEADER_SIZE); }
bool skip_coord() { return skip(SIZEOF_STORED_DOUBLE); }
/* Routines to scan wkb header information */
bool scan_wkb_header(wkb_header *header) {
if (no_data(WKB_HEADER_SIZE)) return true;
header->byte_order = (uchar)(*m_data);
m_data++;
get_uint4(&header->wkb_type);
m_data += 4;
return false;
}
/* Routines to scan uint4 information */
bool scan_uint4(uint32 *number) {
if (no_data(4)) return true;
get_uint4(number);
m_data += 4;
return false;
}
bool scan_non_zero_uint4(uint32 *number) {
return (scan_uint4(number) || 0 == *number);
}
bool scan_n_points_and_check_data(uint32 *n_points,
uint32 extra_point_space = 0) {
return scan_non_zero_uint4(n_points) ||
not_enough_points(*n_points, extra_point_space);
}
/* Routines to scan coordinate information */
void scan_xy_unsafe(point_xy *p) {
assert(!no_data(POINT_DATA_SIZE));
get_float8(&p->x);
m_data += SIZEOF_STORED_DOUBLE;
get_float8(&p->y);
m_data += SIZEOF_STORED_DOUBLE;
}
bool scan_xy(point_xy *p) {
if (no_data(SIZEOF_STORED_DOUBLE * 2)) return true;
scan_xy_unsafe(p);
return false;
}
bool scan_coord(double *x) {
if (no_data(SIZEOF_STORED_DOUBLE)) return true;
get_float8(x);
m_data += SIZEOF_STORED_DOUBLE;
return false;
}
};
/** Callback which creates Geometry objects on top of a given placement. */
typedef Geometry *(*create_geom_t)(char *);
class Class_info {
public:
const LEX_CSTRING m_name;
int m_type_id;
create_geom_t m_create_func;
Class_info(const char *name, int type_id, create_geom_t create_func);
};
// LCOV_EXCL_START
virtual const Class_info *get_class_info() const { return nullptr; }
virtual uint32 get_data_size() const { return ~0U; }
/* read from trs the wkt string and write into wkb as wkb encoded data. */
virtual bool init_from_wkt(Gis_read_stream *trs [[maybe_unused]],
String *wkb [[maybe_unused]]) {
return true;
}
/* read from wkb the wkb data and write into res as wkb encoded data. */
/* returns the length of the wkb that was read */
virtual uint init_from_wkb(THD *thd [[maybe_unused]],
const char *wkb [[maybe_unused]],
uint len [[maybe_unused]],
wkbByteOrder bo [[maybe_unused]],
String *res [[maybe_unused]]) {
return 0;
}
virtual bool get_data_as_wkt(String *txt [[maybe_unused]],
wkb_parser *wkb [[maybe_unused]]) const {
return true;
}
// LCOV_EXCL_STOP
virtual bool get_mbr(MBR *mbr [[maybe_unused]],
wkb_parser *wkb [[maybe_unused]]) const {
return true;
}
bool get_mbr(MBR *mbr) {
wkb_parser wkb(get_cptr(), get_cptr() + get_nbytes());
return get_mbr(mbr, &wkb);
}
virtual bool dimension(uint32 *dim, wkb_parser *wkb) const {
*dim = feature_dimension();
uint32 length;
if ((length = get_data_size()) == GET_SIZE_ERROR) return true;
wkb->skip(length);
return false;
}
bool dimension(uint32 *dim) const {
wkb_parser wkb(get_cptr(), get_cptr() + get_nbytes());
return dimension(dim, &wkb);
}
wkbType get_type() const {
return static_cast<Geometry::wkbType>(get_class_info()->m_type_id);
}
enum_coordinate_reference_system get_coordsys() const { return cartesian; }
virtual uint32 feature_dimension() const {
assert(false);
return 0;
}
virtual int get_x(double *) const { return -1; } // LCOV_EXCL_LINE
virtual int get_y(double *) const { return -1; } // LCOV_EXCL_LINE
virtual int geom_length(double *) const { return -1; } // LCOV_EXCL_LINE
virtual int is_closed(int *) const { return -1; } // LCOV_EXCL_LINE
virtual int num_interior_ring(uint32 *) const { // LCOV_EXCL_LINE
return -1; // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE
virtual int num_points(uint32 *) const { return -1; } // LCOV_EXCL_LINE
virtual int num_geometries(uint32 *) const { return -1; } // LCOV_EXCL_LINE
virtual int copy_points(String *) const { return -1; } // LCOV_EXCL_LINE
/* The following 7 functions return geometries in wkb format. */
virtual int start_point(String *) const { return -1; } // LCOV_EXCL_LINE
virtual int end_point(String *) const { return -1; } // LCOV_EXCL_LINE
virtual int exterior_ring(String *) const { return -1; } // LCOV_EXCL_LINE
// LCOV_EXCL_START
virtual int point_n(uint32 num [[maybe_unused]],
String *result [[maybe_unused]]) const {
return -1;
}
virtual int interior_ring_n(uint32 num [[maybe_unused]],
String *result [[maybe_unused]]) const {
return -1;
}
virtual int geometry_n(uint32 num [[maybe_unused]],
String *result [[maybe_unused]]) const {
return -1;
}
// LCOV_EXCL_STOP
/**
Reverses the coordinates of a geometry.
Switches the coordinates of the wkb string pointed to by the Geometry.
Ex: Used on a POINT(5,2), the result would be POINT(2, 5).
@retval false coordinate reversal was successful
@retval true coordinate reversal was unsuccessful
*/
virtual bool reverse_coordinates() = 0;
/**
Check that the coordinates of a geometry is within the valid range.
Checks if the coordinates in a geometry are within allowed range of a
geographic spatial reference system. Valid range for longitude and latitude
coordinates in geographic spatial reference systems are (-180, 180) and
[-90, 90] degrees, respectively.
@param[in] srs_angular_unit Unit to radians conversion factor.
@param[out] long_out_of_range Longitude is out of range.
@param[out] lat_out_of_range Latitude is out of range.
@param[out] out_of_range_value The value that is out of range.
@retval false Coordinates are within allowed range.
@retval true Coordinates are not within allowed range, or an error occurred
during range checking.
*/
virtual bool validate_coordinate_range(double srs_angular_unit,
bool *long_out_of_range,
bool *lat_out_of_range,
double *out_of_range_value) = 0;
public:
static Geometry *create_by_typeid(Geometry_buffer *buffer, int type_id);
static Geometry *construct(Geometry_buffer *buffer, const char *data,
uint32 data_len, bool has_srid = true);
static Geometry *construct(Geometry_buffer *buffer, const String *str,
bool has_srid = true) {
return construct(buffer, str->ptr(), static_cast<uint32>(str->length()),
has_srid);
}
static Geometry *create_from_wkt(Geometry_buffer *buffer,
Gis_read_stream *trs, String *wkt,
bool init_stream = true,
bool check_trailing = true);
static Geometry *create_from_wkb(THD *thd, Geometry_buffer *buffer,
const char *wkb, uint32 len, String *res,
bool init);
bool as_wkt(String *wkt, wkb_parser *wkb) const {
const uint32 len = (uint)get_class_info()->m_name.length;
if (wkt->reserve(len + 2, 512)) return true;
if (get_type() == wkb_geometrycollection)
wkt->append("GEOMETRYCOLLECTION");
else
qs_append(get_class_info()->m_name.str, len, wkt);
if (get_data_as_wkt(wkt, wkb)) return true;
return false;
}
bool as_wkt(String *wkt) const {
wkb_parser wkb(get_cptr(), get_cptr() + get_nbytes());
return as_wkt(wkt, &wkb);
}
bool as_wkb(String *wkb, bool shallow_copy) const;
bool as_geometry(String *wkb, bool shallow_copy) const;
void set_data_ptr(const void *data, size_t data_len) {
m_ptr = const_cast<void *>(data);
set_nbytes(data_len);
}
void set_data_ptr(const wkb_container *c) {
m_ptr = const_cast<void *>(static_cast<const void *>(c->data()));
set_nbytes(c->length());
}
void *get_data_ptr() const { return m_ptr; }
bool envelope(String *result) const;
bool envelope(MBR *mbr) const;
static Class_info *ci_collection[wkb_last + 1];
bool is_polygon_ring() const {
return m_flags.props & (POLYGON_OUTER_RING | POLYGON_INNER_RING);
}
bool is_polygon_outer_ring() const {
return m_flags.props & POLYGON_OUTER_RING;
}
bool is_polygon_inner_ring() const {
return m_flags.props & POLYGON_INNER_RING;
}
bool has_geom_header_space() const {
return (m_flags.props & HAS_GEOM_HEADER_SPACE) ||
(m_flags.props & IS_BOOST_GEOMETRY_ADAPTER);
}
void has_geom_header_space(bool b) {
if (b)
m_flags.props |= HAS_GEOM_HEADER_SPACE;
else
m_flags.props &= ~HAS_GEOM_HEADER_SPACE;
}
bool is_components_no_overlapped() const {
return (m_flags.props & MULTIPOLYGON_NO_OVERLAPPED_COMPS);
}
void set_components_no_overlapped(bool b) {
assert(get_type() == wkb_multilinestring ||
get_type() == wkb_multipolygon ||
get_type() == wkb_geometrycollection);
if (b)
m_flags.props |= MULTIPOLYGON_NO_OVERLAPPED_COMPS;
else
m_flags.props &= ~MULTIPOLYGON_NO_OVERLAPPED_COMPS;
}
void set_props(uint16 flag) {
assert(0xfff >= flag);
m_flags.props |= flag;
}
uint16 get_props() const { return (uint16)m_flags.props; }
void set_srid(gis::srid_t id) { m_srid = id; }
gis::srid_t get_srid() const { return m_srid; }
const void *normalize_ring_order();
protected:
static Class_info *find_class(int type_id) {
return ((type_id < wkb_first) || (type_id > wkb_last))
? nullptr
: ci_collection[type_id];
}
static Class_info *find_class(const char *name, size_t len);
void append_points(String *txt, uint32 n_points, wkb_parser *wkb,
uint32 offset, bool bracket_pt = false) const;
bool create_point(String *result, wkb_parser *wkb) const;
bool get_mbr_for_points(MBR *mbr, wkb_parser *wkb, uint offset) const;
bool is_length_verified() const {
return m_flags.props & GEOM_LENGTH_VERIFIED;
}
// Have to make this const because it's called in a const member function.
void set_length_verified(bool b) const {
if (b)
m_flags.props |= GEOM_LENGTH_VERIFIED;
else
m_flags.props &= ~GEOM_LENGTH_VERIFIED;
}
/***************************** Boost Geometry Adapter Interface ************/
public:
/**
Highest byte is stores byte order, dimension, nomem and geotype as follows:
bo: byte order, 1 for little endian(ndr), 0 for big endian(xdr); Currently
it must be always wkb_ndr since it is MySQL's portable geometry format.
dimension: 0~3 for 1~4 dimensions;
nomem: indicating whether this object has its own memory.
If so, the memory is released when the object is destroyed. Some
objects may refer to an existing WKB buffer and access it read only.
geotype: stores the wkbType enum numbers, at most 32 values, valid range
so far: [0, 7] and 31.
nybytes: takes the following 30 bits, stores number of effective and valid
data bytes of current object's wkb data.
props: bits OR'ed for various other runtime properties of the geometry
object. Bits are defined above. No properties are stored
permanently, all properties here are specified/used at runtime
while the Geometry object is alive.
zm: not used now, always be 0, i.e. always 2D geometries. In future,
they represent Z and/or M settings, 1: Z, 2: M, 3: ZM.
unused: reserved for future use, it's unused now.
*/
class Flags_t {
public:
Flags_t() : Flags_t(wkb_invalid_type, /*len*/ 0) {}
Flags_t(wkbType type, size_t len)
: bo(wkb_ndr),
dim(GEOM_DIM - 1),
nomem(1),
geotype(type),
nbytes(len),
props(0),
zm(0) {}
uint64 bo : 1;
uint64 dim : 2;
uint64 nomem : 1;
uint64 geotype : 5;
uint64 nbytes : 30;
uint64 props : 12;
uint64 zm : 2;
uint64 unused : 11;
};
static_assert(sizeof(Flags_t) == sizeof(uint64),
"Flags are expected to line up exactly with an uint64.");
Geometry() {
m_ptr = nullptr;
m_owner = nullptr;
set_ownmem(false);
set_byte_order(Geometry::wkb_ndr);
set_srid(default_srid);
}
/**
Constructor used as BG adapter or by default constructors of children
classes.
@param ptr WKB buffer address, or NULL for an empty object.
@param len WKB buffer length in bytes.
@param flags the flags to set, no field is used for now except geotype.
@param srid srid of the geometry.
*/
Geometry(const void *ptr, size_t len, const Flags_t &flags,
gis::srid_t srid) {
m_ptr = const_cast<void *>(ptr);
m_flags.nbytes = len;
set_srid(srid);
m_flags.geotype = flags.geotype;
m_owner = nullptr;
set_ownmem(false);
}
Geometry(const Geometry &geo);
Geometry &operator=(const Geometry &rhs);
/* Getters and setters. */
void *get_ptr() const { return m_ptr; }
char *get_cptr() const { return static_cast<char *>(m_ptr); }
uchar *get_ucptr() const { return static_cast<uchar *>(m_ptr); }
Geometry *get_owner() const { return m_owner; }
void set_owner(Geometry *o) { m_owner = o; }
void set_byte_order(Geometry::wkbByteOrder bo) {
assert(bo == Geometry::wkb_ndr);
m_flags.bo = static_cast<char>(bo);
}
void set_dimension(char dim) {
// Valid dim is one of [1, 2, 3, 4].
assert(dim > 0 && dim < 5);
m_flags.dim = dim - 1;
}
/**
Check if a given geometry type is a valid geometry type according
to OpenGIS.
Internal geometry types of MySQL are regarded as invalid.
@param gtype geometry type to check
@retval true valid geometry type
@retval false invalid geometry type
*/
static bool is_valid_opengis_geotype(uint32 gtype) {
return gtype >= wkb_first && gtype <= wkb_geometrycollection;
}
/**
Check if a given geometry type is a valid internal geometry type.
Both OpenGIS standard geometry types and internal geometry types
of MySQL are regarded as valid.
@param gtype geometry type to check
@retval true valid geometry type
@retval false invalid geometry type
*/
static bool is_valid_geotype(uint32 gtype) {
/*
Stricter check, outside only checks for [wkb_first, wkb_last],
they don't have to know about the details.
*/
return ((gtype >= wkb_first && gtype <= wkb_geometrycollection) ||
gtype == wkb_polygon_inner_rings);
}
/**
Check if a given geometry type is a valid internal geometry type.
Both OpenGIS standard geometry types and internal geometry types
of MySQL are regarded as valid.
@param gt geometry type to check
@retval true valid geometry type
@retval false invalid geometry type
*/
static bool is_valid_geotype(Geometry::wkbType gt) {
/*
Stricter check, outside only checks for [wkb_first, wkb_last],
they don't have to know about the details.
*/
return ((gt >= wkb_first && gt <= wkb_geometrycollection) ||
gt == wkb_polygon_inner_rings);
}
/**
Verify that a string is a well-formed GEOMETRY string.
This does not check if the geometry is geometrically valid.
@see Geometry_well_formed_checker
@param from String to check
@param length Length of string
@param type Expected type of geometry, or
Geometry::wkb_invalid_type if any type is allowed
@param bo byte order
@return True if the string is a well-formed GEOMETRY string,
false otherwise
*/
static bool is_well_formed(const char *from, size_t length, wkbType type,
wkbByteOrder bo);
void set_geotype(Geometry::wkbType gt) {
is_valid_geotype(gt);
m_flags.geotype = static_cast<char>(gt);
}
// Have to make this const because it's called in a const member function.
void set_nbytes(size_t n) const {
if (get_nbytes() != n) {
set_length_verified(false);
m_flags.nbytes = n;
}
}
/**
Set whether this object has its own memory. If so, the memory is released
when this object is destroyed.
@param b true if this object has its own memory, false otherwise.
*/
void set_ownmem(bool b) { m_flags.nomem = (b ? 0 : 1); }
/**
Returns whether this object has its own memory. If so, the memory is
released when this object is destroyed.
*/
bool get_ownmem() const { return !m_flags.nomem; }
Geometry::wkbByteOrder get_byte_order() const {
assert(m_flags.bo == 1);
return Geometry::wkb_ndr;
}
char get_dimension() const { return static_cast<char>(m_flags.dim) + 1; }
Geometry::wkbType get_geotype() const {
const char gt = static_cast<char>(m_flags.geotype);
return static_cast<Geometry::wkbType>(gt);
}
/**
Build an OGC standard type value from m_flags.zm and m_flags.geotype. For
now m_flags.zm is always 0 so simply call get_geotype(). We don't
directly store the OGC defined values in order to save more bits
of m_flags for other purposes; and also separating zm settings from basic
geometry types is easier for coding and geometry type identification.
When we start to support Z/M settings we need to modify all code which call
write_wkb_header and write_geometry_header to pass to them an OGC standard
type value returned by this function or built similarly. And by doing so
our internal runtime geometry type encoding will work consistently with
OGC defined standard geometry type values in byte strings of WKB format.
@return OGC standard geometry type value.
*/
uint32 get_ogc_geotype() const { return static_cast<uint32>(get_geotype()); }
size_t get_nbytes() const { return static_cast<size_t>(m_flags.nbytes); }
/*
Only sets m_ptr, different from the overloaded one in Gis_wkb_vector<>
which also does WKB parsing.
*/
void set_ptr(const void *ptr) { m_ptr = const_cast<void *>(ptr); }
/**
Whether the Geometry object is created to be used by Boost Geometry or
only by MySQL. There are some operations that only work for one type and
can or must be skipped otherwise.
@return true if it's a BG adapter, false otherwise.
*/
bool is_bg_adapter() const {
return m_flags.props & IS_BOOST_GEOMETRY_ADAPTER;
}
/**
Set whether this object is a BG adapter.
@param b true if it's a BG adapter, false otherwise.
Have to declare this as const because even when a Geometry object's const
adapter member function is called, it's regarded as a BG adapter object.
*/
void set_bg_adapter(bool b) const {
if (b)
m_flags.props |= IS_BOOST_GEOMETRY_ADAPTER;
else
m_flags.props &= ~IS_BOOST_GEOMETRY_ADAPTER;
}
/*
Give up ownership of m_ptr, so as not to release them when
this object is destroyed, to be called when the two member is shallow
assigned to another geometry object.
*/
virtual void donate_data() {
set_ownmem(false);
set_nbytes(0);
m_ptr = nullptr;
}
protected:
/**
In a polygon usable by boost geometry, the m_ptr points to the outer ring
object, and m_inn_rings points to the inner rings, thus the polygon's data
isn't stored in a single WKB. Users should call
@c Gis_polygon::to_wkb_unparsed() before getting the polygon's wkb data,
@c Gis_polygon::to_wkb_unparsed() will form a single WKB for the polygon
and refer to it with m_ptr, and release the outer ring object
and the inner rings objects, and such an polygon isn't usable by BG any
more, it's exactly what we got with
@c Geometry::create_from_wkt / @c Geometry::create_from_wkt.
*/
bool polygon_is_wkb_form() const {