forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_geofunc.cc
5148 lines (4477 loc) · 144 KB
/
item_geofunc.cc
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) 2003, 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 */
/**
@file
@brief
This file defines all spatial functions
*/
#include "item_geofunc.h"
#include "gstream.h" // Gis_read_stream
#include "sql_class.h" // THD
#include "gis_bg_traits.h"
#include "parse_tree_helpers.h"
#include "item_geofunc_internal.h"
#include <stack>
static int check_geometry_valid(Geometry *geom);
/**
Check whether all points in the sequence container are colinear.
@param ls a sequence of points with no duplicates.
@return true if the points are colinear, false otherwise.
*/
template <typename Point_range>
bool is_colinear(const Point_range &ls)
{
if (ls.size() < 3)
return true;
double x1, x2, x3, y1, y2, y3, X1, X2, Y1, Y2;
for (size_t i= 0; i < ls.size() - 2; i++)
{
x1= ls[i].template get<0>();
x2= ls[i + 1].template get<0>();
x3= ls[i + 2].template get<0>();
y1= ls[i].template get<1>();
y2= ls[i + 1].template get<1>();
y3= ls[i + 2].template get<1>();
X1= x2 - x1;
X2= x3 - x2;
Y1= y2 - y1;
Y2= y3 - y2;
if (X1 * Y2 - X2 * Y1 != 0)
return false;
}
return true;
}
Item_geometry_func::Item_geometry_func(const POS &pos, PT_item_list *list)
:Item_str_func(pos, list)
{}
Field *Item_geometry_func::tmp_table_field(TABLE *t_arg)
{
Field *result;
if ((result= new Field_geom(max_length, maybe_null, item_name.ptr(), t_arg->s,
get_geometry_type())))
result->init(t_arg);
return result;
}
void Item_geometry_func::fix_length_and_dec()
{
collation.set(&my_charset_bin);
decimals=0;
max_length= 0xFFFFFFFFU;
maybe_null= 1;
}
bool Item_func_geometry_from_text::itemize(Parse_context *pc, Item **res)
{
if (skip_itemize(res))
return false;
if (super::itemize(pc, res))
return true;
assert(arg_count == 1 || arg_count == 2);
if (arg_count == 1)
pc->thd->lex->set_uncacheable(pc->select, UNCACHEABLE_RAND);
return false;
}
/**
Parses a WKT string to produce a geometry encoded with an SRID prepending
its WKB bytes, namely a byte string of GEOMETRY format.
@param str buffer to hold result, may not be filled.
@return the buffer that hold the GEOMETRY byte string result, may or may
not be the same as 'str' parameter.
*/
String *Item_func_geometry_from_text::val_str(String *str)
{
assert(fixed == 1);
Geometry_buffer buffer;
String arg_val;
String *wkt= args[0]->val_str_ascii(&arg_val);
if ((null_value= (!wkt || args[0]->null_value)))
return 0;
Gis_read_stream trs(wkt->charset(), wkt->ptr(), wkt->length());
uint32 srid= 0;
if (arg_count == 2)
{
if ((null_value= args[1]->null_value))
{
assert(maybe_null);
return NULL;
}
else
srid= (uint32)args[1]->val_int();
}
str->set_charset(&my_charset_bin);
if ((null_value= str->reserve(GEOM_HEADER_SIZE, 512)))
return 0;
str->length(0);
str->q_append(srid);
if (!Geometry::create_from_wkt(&buffer, &trs, str, 0))
{
my_error(ER_GIS_INVALID_DATA, MYF(0), func_name());
return error_str();
}
return str;
}
bool Item_func_geometry_from_wkb::itemize(Parse_context *pc, Item **res)
{
if (skip_itemize(res))
return false;
if (super::itemize(pc, res))
return true;
assert(arg_count == 1 || arg_count == 2);
if (arg_count == 1)
pc->thd->lex->set_uncacheable(pc->select, UNCACHEABLE_RAND);
return false;
}
/**
Parses a WKT string to produce a geometry encoded with an SRID prepending
its WKB bytes, namely a byte string of GEOMETRY format.
@param str buffer to hold result, may not be filled.
@return the buffer that hold the GEOMETRY byte string result, may or may
not be the same as 'str' parameter.
*/
String *Item_func_geometry_from_wkb::val_str(String *str)
{
assert(fixed == 1);
String *wkb= NULL;
uint32 srid= 0;
if (arg_count == 2)
{
srid= static_cast<uint32>(args[1]->val_int());
if ((null_value= args[1]->null_value))
return NULL;
}
wkb= args[0]->val_str(&tmp_value);
if ((null_value= (!wkb || args[0]->null_value)))
return NULL;
/*
GeometryFromWKB(wkb [,srid]) understands both WKB (without SRID) and
Geometry (with SRID) values in the "wkb" argument.
In case if a Geometry type value is passed, we assume that the value
is well-formed and can directly return it without going through
Geometry::create_from_wkb(), and consequently such WKB data must be
MySQL standard (little) endian. Note that users can pass via client
any WKB/Geometry byte string, including those of big endianess.
*/
if (args[0]->field_type() == MYSQL_TYPE_GEOMETRY)
{
if (arg_count == 1)
{
push_warning_printf(current_thd,
Sql_condition::SL_WARNING,
ER_WARN_USING_GEOMFROMWKB_TO_SET_SRID_ZERO,
ER_THD(current_thd, ER_WARN_USING_GEOMFROMWKB_TO_SET_SRID_ZERO),
func_name(), func_name());
}
else if (arg_count == 2)
{
push_warning_printf(current_thd,
Sql_condition::SL_WARNING,
ER_WARN_USING_GEOMFROMWKB_TO_SET_SRID,
ER_THD(current_thd, ER_WARN_USING_GEOMFROMWKB_TO_SET_SRID),
func_name(), func_name());
}
Geometry_buffer buff;
if (Geometry::construct(&buff, wkb->ptr(), wkb->length()) == NULL)
{
my_error(ER_GIS_INVALID_DATA, MYF(0), func_name());
return error_str();
}
/*
Check if SRID embedded into the Geometry value differs
from the SRID value passed in the second argument.
*/
if (srid == uint4korr(wkb->ptr()))
return wkb; // Do not differ
/*
Replace SRID to the one passed in the second argument.
Note, we cannot replace SRID directly in wkb->ptr(),
because wkb can point to some value that we should not touch,
e.g. to a SP variable value. So we need to copy to "str".
*/
if ((null_value= str->copy(*wkb)))
return NULL;
str->write_at_position(0, srid);
return str;
}
str->set_charset(&my_charset_bin);
if (str->reserve(GEOM_HEADER_SIZE, 512))
{
null_value= true; /* purecov: inspected */
return NULL; /* purecov: inspected */
}
str->length(0);
str->q_append(srid);
Geometry_buffer buffer;
if (!Geometry::create_from_wkb(&buffer, wkb->ptr(), wkb->length(), str,
false/* Don't init stream. */))
{
my_error(ER_GIS_INVALID_DATA, MYF(0), func_name());
return error_str();
}
return str;
}
/**
Definition of various string constants used for writing and reading
GeoJSON data.
*/
const char *Item_func_geomfromgeojson::TYPE_MEMBER= "type";
const char *Item_func_geomfromgeojson::CRS_MEMBER= "crs";
const char *Item_func_geomfromgeojson::GEOMETRY_MEMBER= "geometry";
const char *Item_func_geomfromgeojson::PROPERTIES_MEMBER= "properties";
const char *Item_func_geomfromgeojson::FEATURES_MEMBER= "features";
const char *Item_func_geomfromgeojson::GEOMETRIES_MEMBER= "geometries";
const char *Item_func_geomfromgeojson::COORDINATES_MEMBER= "coordinates";
const char *Item_func_geomfromgeojson::CRS_NAME_MEMBER= "name";
const char *Item_func_geomfromgeojson::NAMED_CRS= "name";
const char *Item_func_geomfromgeojson::SHORT_EPSG_PREFIX= "EPSG:";
const char *Item_func_geomfromgeojson::POINT_TYPE= "Point";
const char *Item_func_geomfromgeojson::MULTIPOINT_TYPE= "MultiPoint";
const char *Item_func_geomfromgeojson::LINESTRING_TYPE= "LineString";
const char *Item_func_geomfromgeojson::MULTILINESTRING_TYPE= "MultiLineString";
const char *Item_func_geomfromgeojson::POLYGON_TYPE= "Polygon";
const char *Item_func_geomfromgeojson::MULTIPOLYGON_TYPE= "MultiPolygon";
const char *Item_func_geomfromgeojson::FEATURE_TYPE= "Feature";
const char *Item_func_geomfromgeojson::
FEATURECOLLECTION_TYPE= "FeatureCollection";
const char *Item_func_geomfromgeojson::
LONG_EPSG_PREFIX= "urn:ogc:def:crs:EPSG::";
const char *Item_func_geomfromgeojson::
CRS84_URN= "urn:ogc:def:crs:OGC:1.3:CRS84";
const char *Item_func_geomfromgeojson::
GEOMETRYCOLLECTION_TYPE= "GeometryCollection";
/**
<geometry> = ST_GEOMFROMGEOJSON(<string>[, <options>[, <srid>]])
Takes a GeoJSON input string and outputs a GEOMETRY.
This function supports both single GeoJSON objects and geometry collections.
In addition, feature objects and feature collections are supported (feature
collections are translated into GEOMETRYCOLLECTION).
It follows the standard described at http://geojson.org/geojson-spec.html
(revision 1.0).
*/
String *Item_func_geomfromgeojson::val_str(String *buf)
{
if (arg_count > 1)
{
// Check and parse the OPTIONS parameter.
longlong dimension_argument= args[1]->val_int();
if ((null_value= args[1]->null_value))
return NULL;
if (dimension_argument == 1)
{
m_handle_coordinate_dimension= Item_func_geomfromgeojson::reject_document;
}
else if (dimension_argument == 2)
{
m_handle_coordinate_dimension=
Item_func_geomfromgeojson::strip_now_accept_future;
}
else if (dimension_argument == 3)
{
m_handle_coordinate_dimension=
Item_func_geomfromgeojson::strip_now_reject_future;
}
else if (dimension_argument == 4)
{
m_handle_coordinate_dimension=
Item_func_geomfromgeojson::strip_now_strip_future;
}
else
{
char option_string[MAX_BIGINT_WIDTH + 1];
if (args[1]->unsigned_flag)
ullstr(dimension_argument, option_string);
else
llstr(dimension_argument, option_string);
my_error(ER_WRONG_VALUE_FOR_TYPE, MYF(0), "option", option_string,
func_name());
return error_str();
}
}
if (arg_count > 2)
{
/*
Check and parse the SRID parameter. If this is set to a valid value,
any CRS member in the GeoJSON document will be ignored.
*/
longlong srid_argument= args[2]->val_int();
if ((null_value= args[2]->null_value))
return NULL;
// Only allow unsigned 32 bits integer as SRID.
if (srid_argument < 0 || srid_argument > UINT_MAX32)
{
char srid_string[MAX_BIGINT_WIDTH + 1];
if (args[2]->unsigned_flag)
ullstr(srid_argument, srid_string);
else
llstr(srid_argument, srid_string);
my_error(ER_WRONG_VALUE_FOR_TYPE, MYF(0), "SRID", srid_string,
func_name());
return error_str();
}
else
{
m_user_srid= static_cast<Geometry::srid_t>(srid_argument);
m_user_provided_srid= true;
}
}
Json_wrapper wr;
if (get_json_wrapper(args, 0, buf, func_name(), &wr, true))
return error_str();
/*
We will handle JSON NULL the same way as we handle SQL NULL. The reason
behind this is that we want the following SQL to return SQL NULL:
SELECT ST_GeomFromGeoJSON(
JSON_EXTRACT(
'{ "type": "Feature",
"geometry": null,
"properties": { "name": "Foo" }
}',
'$.geometry')
);
The function JSON_EXTRACT will return a JSON NULL, so if we don't handle
JSON NULL as SQL NULL the above SQL will raise an error since we would
expect a SQL NULL or a JSON object.
*/
null_value= (args[0]->null_value || wr.type() == Json_dom::J_NULL);
if (null_value)
{
assert(maybe_null);
return NULL;
}
if (wr.type() != Json_dom::J_OBJECT)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return error_str();
}
const Json_object *root_obj= down_cast<const Json_object*>(wr.to_dom());
/*
Set the default SRID to 4326. This will be overwritten if a valid CRS is
found in the GeoJSON input, or if the user has specified a SRID as an
argument.
It would probably be smart to allocate a percentage of the length of the
input string (something like buf->realloc(json_string->length() * 0.2)).
This would save a lot of reallocations and boost performance, especially for
large inputs. But it is difficult to predict how much of the json input that
will be parsed into output data.
*/
if (buf->reserve(GEOM_HEADER_SIZE, 512))
{
my_error(ER_OUTOFMEMORY, GEOM_HEADER_SIZE);
return error_str();
}
buf->set_charset(&my_charset_bin);
buf->length(0);
buf->q_append(static_cast<uint32>(4326));
/*
The rollback variable is used for detecting/accepting NULL objects inside
collections (a feature with NULL geometry is allowed, and thus we can have
a geometry collection with a NULL geometry translated into following WKT:
GEOMETRYCOLLECTION()).
parse_object() does a recursive parsing of the GeoJSON document.
*/
String collection_buffer;
bool rollback= false;
Geometry *result_geometry= NULL;
m_srid_found_in_document = -1;
if (parse_object(root_obj, &rollback, &collection_buffer, false,
&result_geometry))
{
// Do a delete here, to be sure that we have no memory leaks.
delete result_geometry;
result_geometry= NULL;
if (rollback)
{
assert(maybe_null);
null_value= true;
return NULL;
}
return error_str();
}
// Set the correct SRID for the geometry data.
if (m_user_provided_srid)
buf->write_at_position(0, m_user_srid);
else if (m_srid_found_in_document > -1)
buf->write_at_position(0, static_cast<uint32>(m_srid_found_in_document));
bool return_result= result_geometry->as_wkb(buf, false);
delete result_geometry;
result_geometry= NULL;
if (return_result)
{
my_error(ER_GIS_INVALID_DATA, MYF(0), func_name());
return error_str();
}
return buf;
}
/**
Case insensitive lookup of a member in a JSON object.
This is needed since the get()-method of the JSON object is case
sensitive.
@param v The object to look for the member in.
@param member_name Name of the member to look after
@return The member if one was found, NULL otherwise.
*/
const Json_dom *Item_func_geomfromgeojson::
my_find_member_ncase(const Json_object *object, const char *member_name)
{
Json_object::const_iterator itr;
for (itr= object->begin(); itr != object->end(); ++itr)
{
if (native_strcasecmp(member_name, itr->first.c_str()) == 0)
return itr->second;
}
return NULL;
}
/**
Takes a JSON object as input, and parses the data to a Geometry object.
The call stack will be no larger than the maximum depth of the GeoJSON
document, which is more or less equivalent to the number of nested
collections in the document.
@param object A JSON object object to parse.
@param rollback Pointer to a boolean indicating if parsed data should
be reverted/rolled back.
@param buffer A string buffer to be used by GeometryCollection
@param is_parent_featurecollection Indicating if the current geometry is a
child of a FeatureCollection.
@param[out] geometry A pointer to the parsed geometry.
@return true if the parsing failed, false otherwise. Note that if rollback is
set to true and true is returned, the parsing succeeded, but no
Geometry data could be parsed.
*/
bool Item_func_geomfromgeojson::
parse_object(const Json_object *object, bool *rollback, String *buffer,
bool is_parent_featurecollection, Geometry **geometry)
{
/*
A GeoJSON object MUST have a type member, which MUST
be of string type.
*/
const Json_dom *type_member= my_find_member_ncase(object, TYPE_MEMBER);
if (!is_member_valid(type_member, TYPE_MEMBER, Json_dom::J_STRING, false,
NULL))
{
return true;
}
/*
Check if this object has a CRS member.
We allow the CRS member to be JSON NULL.
*/
const Json_dom *crs_member= my_find_member_ncase(object, CRS_MEMBER);
if (crs_member != NULL)
{
if (crs_member->json_type() == Json_dom::J_OBJECT)
{
const Json_object *crs_obj= down_cast<const Json_object *>(crs_member);
if (parse_crs_object(crs_obj))
return true;
}
else if (crs_member->json_type() != Json_dom::J_NULL)
{
my_error(ER_INVALID_GEOJSON_WRONG_TYPE, MYF(0), func_name(),
CRS_MEMBER, "object");
return true;
}
}
// Handle feature objects and feature collection objects.
const Json_string *type_member_str=
down_cast<const Json_string*>(type_member);
if (strcmp(type_member_str->value().c_str(), FEATURE_TYPE) == 0)
{
/*
Check if this feature object has the required "geometry" and "properties"
member. Note that we do not use the member "properties" for anything else
than checking for valid GeoJSON document.
*/
bool dummy;
const Json_dom *geometry_member= my_find_member_ncase(object,
GEOMETRY_MEMBER);
const Json_dom *properties_member= my_find_member_ncase(object,
PROPERTIES_MEMBER);
if (!is_member_valid(geometry_member, GEOMETRY_MEMBER, Json_dom::J_OBJECT,
true, rollback) ||
!is_member_valid(properties_member, PROPERTIES_MEMBER,
Json_dom::J_OBJECT, true, &dummy) || *rollback)
{
return true;
}
const Json_object *geometry_member_obj=
down_cast<const Json_object*>(geometry_member);
return parse_object(geometry_member_obj, rollback, buffer, false, geometry);
}
else if (strcmp(type_member_str->value().c_str(), FEATURECOLLECTION_TYPE) == 0)
{
// FeatureCollections cannot be nested according to GeoJSON spec.
if (is_parent_featurecollection)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
// We will handle a FeatureCollection as a GeometryCollection.
const Json_dom *features= my_find_member_ncase(object, FEATURES_MEMBER);
if (!is_member_valid(features, FEATURES_MEMBER, Json_dom::J_ARRAY, false,
NULL))
{
return true;
}
const Json_array *features_array= down_cast<const Json_array*>(features);
return parse_object_array(features_array, Geometry::wkb_geometrycollection,
rollback, buffer, true, geometry);
}
else
{
Geometry::wkbType wkbtype= get_wkbtype(type_member_str->value().c_str());
if (wkbtype == Geometry::wkb_invalid_type)
{
// An invalid GeoJSON type was found.
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
else
{
/*
All objects except GeometryCollection MUST have a member "coordinates"
of type array. GeometryCollection MUST have a member "geometries" of
type array.
*/
const char *member_name;
if (wkbtype == Geometry::wkb_geometrycollection)
member_name= GEOMETRIES_MEMBER;
else
member_name= COORDINATES_MEMBER;
const Json_dom *array_member= my_find_member_ncase(object, member_name);
if (!is_member_valid(array_member, member_name, Json_dom::J_ARRAY, false,
NULL))
{
return true;
}
const Json_array *array_member_array=
down_cast<const Json_array*>(array_member);
return parse_object_array(array_member_array, wkbtype, rollback, buffer,
false, geometry);
}
}
// Defensive code. This should never be reached.
/* purecov: begin inspected */
assert(false);
return true;
/* purecov: end inspected */
}
/**
Parse an array of coordinates to a Gis_point.
Parses an array of coordinates to a Gis_point. This function must handle
according to the handle_dimension parameter on how non 2D objects should be
handled.
According to the specification, a position array must have at least two
elements, but there is no upper limit.
@param coordinates A JSON array of coordinates.
@param[out] point A pointer to the parsed Gis_point.
@return true if the parsing failed, false otherwise.
*/
bool Item_func_geomfromgeojson::
get_positions(const Json_array *coordinates, Gis_point *point)
{
/*
According to GeoJSON specification, a position array must have at least
two positions.
*/
if (coordinates->size() < 2)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
switch (m_handle_coordinate_dimension)
{
case Item_func_geomfromgeojson::reject_document:
if (coordinates->size() > GEOM_DIM)
{
my_error(ER_DIMENSION_UNSUPPORTED, MYF(0), func_name(),
coordinates->size(), GEOM_DIM);
return true;
}
break;
case Item_func_geomfromgeojson::strip_now_reject_future:
/*
The version in development as of writing, only supports 2 dimensions.
When dimension count is increased beyond 2, we want the function to fail.
*/
if (GEOM_DIM > 2 && coordinates->size() > 2)
{
my_error(ER_DIMENSION_UNSUPPORTED, MYF(0), func_name(),
coordinates->size(), GEOM_DIM);
return true;
}
break;
case Item_func_geomfromgeojson::strip_now_strip_future:
case Item_func_geomfromgeojson::strip_now_accept_future:
if (GEOM_DIM > 2)
assert(false);
break;
default:
// Unspecified behaviour.
assert(false);
return true;
}
// Check if all array members are numbers.
for (size_t i= 0; i < coordinates->size(); ++i)
{
if (!(*coordinates)[i]->is_number())
{
my_error(ER_INVALID_GEOJSON_WRONG_TYPE, MYF(0), func_name(),
"array coordinate", "number");
return true;
}
/*
Even though we only need the two first coordinates, we check the rest of
them to ensure that the GeoJSON is valid.
Remember to call set_alias(), so that this wrapper does not take ownership
of the data.
*/
Json_wrapper coord((*coordinates)[i]);
coord.set_alias();
if (i == 0)
point->set<0>(coord.coerce_real(""));
else if (i == 1)
point->set<1>(coord.coerce_real(""));
}
return false;
}
/**
Takes a JSON array as input, does a recursive parsing and returns a
Geometry object.
This function differs from parse_object() in that it takes an array as input
instead of a object. This is one of the members "coordinates" or "geometries"
of a GeoJSON object.
@param data_array A JSON array to parse.
@param type The type of the GeoJSON object this array belongs to.
@param rollback Pointer to a boolean indicating if parsed data should
be reverted/rolled back.
@param buffer A String buffer to be used by GeometryCollection.
@param[out] geometry A pointer to the parsed Geometry.
@return true on failure, false otherwise.
*/
bool Item_func_geomfromgeojson::
parse_object_array(const Json_array *data_array, Geometry::wkbType type,
bool *rollback, String *buffer,
bool is_parent_featurecollection, Geometry **geometry)
{
switch (type)
{
case Geometry::wkb_geometrycollection:
{
/*
Ensure that the provided buffer is empty, and then create a empty
GeometryCollection using this buffer.
*/
buffer->set_charset(&my_charset_bin);
buffer->length(0);
buffer->reserve(GEOM_HEADER_SIZE + SIZEOF_INT);
write_geometry_header(buffer, 0, Geometry::wkb_geometrycollection, 0);
Gis_geometry_collection *collection= new Gis_geometry_collection();
*geometry= collection;
collection->set_data_ptr(buffer->ptr() + GEOM_HEADER_SIZE, 4);
collection->has_geom_header_space(true);
for (size_t i= 0; i < data_array->size(); ++i)
{
if ((*data_array)[i]->json_type() != Json_dom::J_OBJECT)
{
my_error(ER_INVALID_GEOJSON_WRONG_TYPE, MYF(0), func_name(),
GEOMETRIES_MEMBER, "object array");
return true;
}
const Json_object *object=
down_cast<const Json_object*>((*data_array)[i]);
String geo_buffer;
Geometry *parsed_geometry= NULL;
if (parse_object(object, rollback, &geo_buffer,
is_parent_featurecollection, &parsed_geometry))
{
/*
This will happen if a feature object contains a NULL geometry
object (which is a perfectly valid GeoJSON object).
*/
if (*rollback)
{
*rollback= false;
}
else
{
delete parsed_geometry;
parsed_geometry= NULL;
return true;
}
}
else
{
if (parsed_geometry->get_geotype() == Geometry::wkb_polygon)
{
// Make the Gis_polygon suitable for MySQL GIS code.
Gis_polygon *polygon= static_cast<Gis_polygon*>(parsed_geometry);
polygon->to_wkb_unparsed();
}
collection->append_geometry(parsed_geometry, buffer);
}
delete parsed_geometry;
parsed_geometry= NULL;
}
return false;
}
case Geometry::wkb_point:
{
Gis_point *point= new Gis_point(false);
*geometry= point;
return get_positions(data_array, point);
}
case Geometry::wkb_linestring:
{
Gis_line_string *linestring= new Gis_line_string(false);
*geometry= linestring;
if (get_linestring(data_array, linestring))
return true;
return false;
}
case Geometry::wkb_multipoint:
{
// Ensure that the MultiPoint has at least one Point.
if (data_array->size() == 0)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
Gis_multi_point *multipoint= new Gis_multi_point(false);
*geometry= multipoint;
for (size_t i= 0; i < data_array->size(); ++i)
{
if ((*data_array)[i]->json_type() != Json_dom::J_ARRAY)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
else
{
const Json_array *coords=
down_cast<const Json_array*>((*data_array)[i]);
Gis_point point;
if (get_positions(coords, &point))
return true;
multipoint->push_back(point);
}
}
return false;
}
case Geometry::wkb_multilinestring:
{
// Ensure that the MultiLineString has at least one LineString.
if (data_array->size() == 0)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
Gis_multi_line_string *multilinestring= new Gis_multi_line_string(false);
*geometry= multilinestring;
for (size_t i= 0; i < data_array->size(); ++i)
{
if ((*data_array)[i]->json_type() != Json_dom::J_ARRAY)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
const Json_array *coords=
down_cast<const Json_array*>((*data_array)[i]);
Gis_line_string linestring;
if (get_linestring(coords, &linestring))
return true;
multilinestring->push_back(linestring);
}
return false;
}
case Geometry::wkb_polygon:
{
Gis_polygon *polygon= new Gis_polygon(false);
*geometry= polygon;
return get_polygon(data_array, polygon);
}
case Geometry::wkb_multipolygon:
{
// Ensure that the MultiPolygon has at least one Polygon.
if (data_array->size() == 0)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
Gis_multi_polygon *multipolygon= new Gis_multi_polygon(false);
*geometry= multipolygon;
for (size_t i= 0; i < data_array->size(); ++i)
{
if ((*data_array)[i]->json_type() != Json_dom::J_ARRAY)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
const Json_array *coords=
down_cast<const Json_array*>((*data_array)[i]);
Gis_polygon polygon;
if (get_polygon(coords, &polygon))
return true;
multipolygon->push_back(polygon);
}
return false;
}
default:
{
assert(false);
return false;
}
}
}
/**
Create a Gis_line_string from a JSON array.
@param data_array A JSON array containing the coordinates.
@param linestring Pointer to a linestring to be filled with data.
@return true on failure, false otherwise.
*/
bool
Item_func_geomfromgeojson::get_linestring(const Json_array *data_array,
Gis_line_string *linestring)
{
// Ensure that the linestring has at least one point.
if (data_array->size() < 2)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
for (size_t i= 0; i < data_array->size(); ++i)
{
if ((*data_array)[i]->json_type() != Json_dom::J_ARRAY)
{
my_error(ER_INVALID_GEOJSON_UNSPECIFIED, MYF(0), func_name());
return true;
}
else
{
Gis_point point;
const Json_array *coords= down_cast<const Json_array*>((*data_array)[i]);
if (get_positions(coords, &point))
return true;
linestring->push_back(point);
}
}
return false;
}