forked from postgis/postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwin_wkt.c
917 lines (784 loc) · 21.4 KB
/
lwin_wkt.c
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
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright (C) 2010 Paul Ramsey <pramsey@cleverelephant.ca>
*
**********************************************************************/
#include <stdlib.h>
#include <ctype.h> /* for isspace */
#include "lwin_wkt.h"
#include "lwin_wkt_parse.h"
#include "lwgeom_log.h"
/*
* Error messages for failures in the parser.
*/
const char *parser_error_messages[] =
{
"",
"geometry requires more points",
"geometry must have an odd number of points",
"geometry contains non-closed rings",
"can not mix dimensionality in a geometry",
"parse error - invalid geometry",
"invalid WKB type",
"incontinuous compound curve",
"triangle must have exactly 4 points",
"geometry has too many points",
"parse error - invalid geometry"
};
#define SET_PARSER_ERROR(errno) { \
global_parser_result.message = parser_error_messages[(errno)]; \
global_parser_result.errcode = (errno); \
global_parser_result.errlocation = wkt_yylloc.last_column; \
}
/**
* Read the SRID number from an SRID=<> string
*/
int wkt_lexer_read_srid(char *str)
{
char *c = str;
long i = 0;
int srid;
if( ! str ) return SRID_UNKNOWN;
c += 5; /* Advance past "SRID=" */
i = strtol(c, NULL, 10);
srid = clamp_srid((int)i);
/* TODO: warn on explicit UNKNOWN srid ? */
return srid;
}
static uint8_t wkt_dimensionality(char *dimensionality)
{
size_t i = 0;
uint8_t flags = 0;
if( ! dimensionality )
return flags;
/* If there's an explicit dimensionality, we use that */
for( i = 0; i < strlen(dimensionality); i++ )
{
if( (dimensionality[i] == 'Z') || (dimensionality[i] == 'z') )
FLAGS_SET_Z(flags,1);
else if( (dimensionality[i] == 'M') || (dimensionality[i] == 'm') )
FLAGS_SET_M(flags,1);
/* only a space is accepted in between */
else if( ! isspace(dimensionality[i]) ) break;
}
return flags;
}
/**
* Force the dimensionality of a geometry to match the dimensionality
* of a set of flags (usually derived from a ZM WKT tag).
*/
static int wkt_parser_set_dims(LWGEOM *geom, uint8_t flags)
{
int hasz = FLAGS_GET_Z(flags);
int hasm = FLAGS_GET_M(flags);
uint32_t i = 0;
/* Error on junk */
if( ! geom )
return LW_FAILURE;
FLAGS_SET_Z(geom->flags, hasz);
FLAGS_SET_M(geom->flags, hasm);
switch( geom->type )
{
case POINTTYPE:
{
LWPOINT *pt = (LWPOINT*)geom;
if ( pt->point )
{
FLAGS_SET_Z(pt->point->flags, hasz);
FLAGS_SET_M(pt->point->flags, hasm);
}
break;
}
case TRIANGLETYPE:
case CIRCSTRINGTYPE:
case LINETYPE:
{
LWLINE *ln = (LWLINE*)geom;
if ( ln->points )
{
FLAGS_SET_Z(ln->points->flags, hasz);
FLAGS_SET_M(ln->points->flags, hasm);
}
break;
}
case POLYGONTYPE:
{
LWPOLY *poly = (LWPOLY*)geom;
for ( i = 0; i < poly->nrings; i++ )
{
if( poly->rings[i] )
{
FLAGS_SET_Z(poly->rings[i]->flags, hasz);
FLAGS_SET_M(poly->rings[i]->flags, hasm);
}
}
break;
}
case CURVEPOLYTYPE:
{
LWCURVEPOLY *poly = (LWCURVEPOLY*)geom;
for ( i = 0; i < poly->nrings; i++ )
wkt_parser_set_dims(poly->rings[i], flags);
break;
}
default:
{
if ( lwtype_is_collection(geom->type) )
{
LWCOLLECTION *col = (LWCOLLECTION*)geom;
for ( i = 0; i < col->ngeoms; i++ )
wkt_parser_set_dims(col->geoms[i], flags);
return LW_SUCCESS;
}
else
{
LWDEBUGF(2,"Unknown geometry type: %d", geom->type);
return LW_FAILURE;
}
}
}
return LW_SUCCESS;
}
/**
* Read the dimensionality from a flag, if provided. Then check that the
* dimensionality matches that of the pointarray. If the dimension counts
* match, ensure the pointarray is using the right "Z" or "M".
*/
static int wkt_pointarray_dimensionality(POINTARRAY *pa, uint8_t flags)
{
int hasz = FLAGS_GET_Z(flags);
int hasm = FLAGS_GET_M(flags);
int ndims = 2 + hasz + hasm;
/* No dimensionality or array means we go with what we have */
if( ! (flags && pa) )
return LW_TRUE;
LWDEBUGF(5,"dimensionality ndims == %d", ndims);
LWDEBUGF(5,"FLAGS_NDIMS(pa->flags) == %d", FLAGS_NDIMS(pa->flags));
/*
* ndims > 2 implies that the flags have something useful to add,
* that there is a 'Z' or an 'M' or both.
*/
if( ndims > 2 )
{
/* Mismatch implies a problem */
if ( FLAGS_NDIMS(pa->flags) != ndims )
return LW_FALSE;
/* Match means use the explicit dimensionality */
else
{
FLAGS_SET_Z(pa->flags, hasz);
FLAGS_SET_M(pa->flags, hasm);
}
}
return LW_TRUE;
}
/**
* Build a 2d coordinate.
*/
POINT wkt_parser_coord_2(double c1, double c2)
{
POINT p;
p.flags = 0;
p.x = c1;
p.y = c2;
p.z = p.m = 0.0;
FLAGS_SET_Z(p.flags, 0);
FLAGS_SET_M(p.flags, 0);
return p;
}
/**
* Note, if this is an XYM coordinate we'll have to fix it later when we build
* the object itself and have access to the dimensionality token.
*/
POINT wkt_parser_coord_3(double c1, double c2, double c3)
{
POINT p;
p.flags = 0;
p.x = c1;
p.y = c2;
p.z = c3;
p.m = 0;
FLAGS_SET_Z(p.flags, 1);
FLAGS_SET_M(p.flags, 0);
return p;
}
/**
*/
POINT wkt_parser_coord_4(double c1, double c2, double c3, double c4)
{
POINT p;
p.flags = 0;
p.x = c1;
p.y = c2;
p.z = c3;
p.m = c4;
FLAGS_SET_Z(p.flags, 1);
FLAGS_SET_M(p.flags, 1);
return p;
}
POINTARRAY* wkt_parser_ptarray_add_coord(POINTARRAY *pa, POINT p)
{
POINT4D pt;
LWDEBUG(4,"entered");
/* Error on trouble */
if( ! pa )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
/* Check that the coordinate has the same dimesionality as the array */
if( FLAGS_NDIMS(p.flags) != FLAGS_NDIMS(pa->flags) )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
/* While parsing the point arrays, XYM and XMZ points are both treated as XYZ */
pt.x = p.x;
pt.y = p.y;
if( FLAGS_GET_Z(pa->flags) )
pt.z = p.z;
if( FLAGS_GET_M(pa->flags) )
pt.m = p.m;
/* If the destination is XYM, we'll write the third coordinate to m */
if( FLAGS_GET_M(pa->flags) && ! FLAGS_GET_Z(pa->flags) )
pt.m = p.z;
ptarray_append_point(pa, &pt, LW_TRUE); /* Allow duplicate points in array */
return pa;
}
/**
* Start a point array from the first coordinate.
*/
POINTARRAY* wkt_parser_ptarray_new(POINT p)
{
int ndims = FLAGS_NDIMS(p.flags);
POINTARRAY *pa = ptarray_construct_empty((ndims>2), (ndims>3), 4);
LWDEBUG(4,"entered");
if ( ! pa )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
return wkt_parser_ptarray_add_coord(pa, p);
}
/**
* Create a new point. Null point array implies empty. Null dimensionality
* implies no specified dimensionality in the WKT.
*/
LWGEOM* wkt_parser_point_new(POINTARRAY *pa, char *dimensionality)
{
uint8_t flags = wkt_dimensionality(dimensionality);
LWDEBUG(4,"entered");
/* No pointarray means it is empty */
if( ! pa )
return lwpoint_as_lwgeom(lwpoint_construct_empty(SRID_UNKNOWN, FLAGS_GET_Z(flags), FLAGS_GET_M(flags)));
/* If the number of dimensions is not consistent, we have a problem. */
if( wkt_pointarray_dimensionality(pa, flags) == LW_FALSE )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
/* Only one point allowed in our point array! */
if( pa->npoints != 1 )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_LESSPOINTS);
return NULL;
}
return lwpoint_as_lwgeom(lwpoint_construct(SRID_UNKNOWN, NULL, pa));
}
/**
* Create a new linestring. Null point array implies empty. Null dimensionality
* implies no specified dimensionality in the WKT. Check for numpoints >= 2 if
* requested.
*/
LWGEOM* wkt_parser_linestring_new(POINTARRAY *pa, char *dimensionality)
{
uint8_t flags = wkt_dimensionality(dimensionality);
LWDEBUG(4,"entered");
/* No pointarray means it is empty */
if( ! pa )
return lwline_as_lwgeom(lwline_construct_empty(SRID_UNKNOWN, FLAGS_GET_Z(flags), FLAGS_GET_M(flags)));
/* If the number of dimensions is not consistent, we have a problem. */
if( wkt_pointarray_dimensionality(pa, flags) == LW_FALSE )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
/* Apply check for not enough points, if requested. */
if( (global_parser_result.parser_check_flags & LW_PARSER_CHECK_MINPOINTS) && (pa->npoints < 2) )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_MOREPOINTS);
return NULL;
}
return lwline_as_lwgeom(lwline_construct(SRID_UNKNOWN, NULL, pa));
}
/**
* Create a new circularstring. Null point array implies empty. Null dimensionality
* implies no specified dimensionality in the WKT.
* Circular strings are just like linestrings, except with slighty different
* validity rules (minpoint == 3, numpoints % 2 == 1).
*/
LWGEOM* wkt_parser_circularstring_new(POINTARRAY *pa, char *dimensionality)
{
uint8_t flags = wkt_dimensionality(dimensionality);
LWDEBUG(4,"entered");
/* No pointarray means it is empty */
if( ! pa )
return lwcircstring_as_lwgeom(lwcircstring_construct_empty(SRID_UNKNOWN, FLAGS_GET_Z(flags), FLAGS_GET_M(flags)));
/* If the number of dimensions is not consistent, we have a problem. */
if( wkt_pointarray_dimensionality(pa, flags) == LW_FALSE )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
/* Apply check for not enough points, if requested. */
if( (global_parser_result.parser_check_flags & LW_PARSER_CHECK_MINPOINTS) && (pa->npoints < 3) )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_MOREPOINTS);
return NULL;
}
/* Apply check for odd number of points, if requested. */
if( (global_parser_result.parser_check_flags & LW_PARSER_CHECK_ODD) && ((pa->npoints % 2) == 0) )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_ODDPOINTS);
return NULL;
}
return lwcircstring_as_lwgeom(lwcircstring_construct(SRID_UNKNOWN, NULL, pa));
}
LWGEOM* wkt_parser_triangle_new(POINTARRAY *pa, char *dimensionality)
{
uint8_t flags = wkt_dimensionality(dimensionality);
LWDEBUG(4,"entered");
/* No pointarray means it is empty */
if( ! pa )
return lwtriangle_as_lwgeom(lwtriangle_construct_empty(SRID_UNKNOWN, FLAGS_GET_Z(flags), FLAGS_GET_M(flags)));
/* If the number of dimensions is not consistent, we have a problem. */
if( wkt_pointarray_dimensionality(pa, flags) == LW_FALSE )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
/* Triangles need four points. */
if( (pa->npoints != 4) )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_TRIANGLEPOINTS);
return NULL;
}
/* Triangles need closure. */
if( ! ptarray_is_closed_z(pa) )
{
ptarray_free(pa);
SET_PARSER_ERROR(PARSER_ERROR_UNCLOSED);
return NULL;
}
return lwtriangle_as_lwgeom(lwtriangle_construct(SRID_UNKNOWN, NULL, pa));
}
LWGEOM* wkt_parser_polygon_new(POINTARRAY *pa, char dimcheck)
{
LWPOLY *poly = NULL;
LWDEBUG(4,"entered");
/* No pointarray is a problem */
if( ! pa )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
poly = lwpoly_construct_empty(SRID_UNKNOWN, FLAGS_GET_Z(pa->flags), FLAGS_GET_M(pa->flags));
/* Error out if we can't build this polygon. */
if( ! poly )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
wkt_parser_polygon_add_ring(lwpoly_as_lwgeom(poly), pa, dimcheck);
return lwpoly_as_lwgeom(poly);
}
LWGEOM* wkt_parser_polygon_add_ring(LWGEOM *poly, POINTARRAY *pa, char dimcheck)
{
LWDEBUG(4,"entered");
/* Bad inputs are a problem */
if( ! (pa && poly) )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
/* Rings must agree on dimensionality */
if( FLAGS_NDIMS(poly->flags) != FLAGS_NDIMS(pa->flags) )
{
ptarray_free(pa);
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
/* Apply check for minimum number of points, if requested. */
if( (global_parser_result.parser_check_flags & LW_PARSER_CHECK_MINPOINTS) && (pa->npoints < 4) )
{
ptarray_free(pa);
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_MOREPOINTS);
return NULL;
}
/* Apply check for not closed rings, if requested. */
if( (global_parser_result.parser_check_flags & LW_PARSER_CHECK_CLOSURE) &&
! (dimcheck == 'Z' ? ptarray_is_closed_z(pa) : ptarray_is_closed_2d(pa)) )
{
ptarray_free(pa);
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_UNCLOSED);
return NULL;
}
/* If something goes wrong adding a ring, error out. */
if ( LW_FAILURE == lwpoly_add_ring(lwgeom_as_lwpoly(poly), pa) )
{
ptarray_free(pa);
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
return poly;
}
LWGEOM* wkt_parser_polygon_finalize(LWGEOM *poly, char *dimensionality)
{
uint8_t flags = wkt_dimensionality(dimensionality);
int flagdims = FLAGS_NDIMS(flags);
LWDEBUG(4,"entered");
/* Null input implies empty return */
if( ! poly )
return lwpoly_as_lwgeom(lwpoly_construct_empty(SRID_UNKNOWN, FLAGS_GET_Z(flags), FLAGS_GET_M(flags)));
/* If the number of dimensions are not consistent, we have a problem. */
if( flagdims > 2 )
{
if ( flagdims != FLAGS_NDIMS(poly->flags) )
{
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
/* Harmonize the flags in the sub-components with the wkt flags */
if( LW_FAILURE == wkt_parser_set_dims(poly, flags) )
{
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
}
return poly;
}
LWGEOM* wkt_parser_curvepolygon_new(LWGEOM *ring)
{
LWGEOM *poly;
LWDEBUG(4,"entered");
/* Toss error on null geometry input */
if( ! ring )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
/* Construct poly and add the ring. */
poly = lwcurvepoly_as_lwgeom(lwcurvepoly_construct_empty(SRID_UNKNOWN, FLAGS_GET_Z(ring->flags), FLAGS_GET_M(ring->flags)));
/* Return the result. */
return wkt_parser_curvepolygon_add_ring(poly,ring);
}
LWGEOM* wkt_parser_curvepolygon_add_ring(LWGEOM *poly, LWGEOM *ring)
{
LWDEBUG(4,"entered");
/* Toss error on null input */
if( ! (ring && poly) )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
LWDEBUG(4,"inputs are null");
return NULL;
}
/* All the elements must agree on dimensionality */
if( FLAGS_NDIMS(poly->flags) != FLAGS_NDIMS(ring->flags) )
{
LWDEBUG(4,"dimensionality does not match");
lwgeom_free(ring);
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
/* Apply check for minimum number of points, if requested. */
if( (global_parser_result.parser_check_flags & LW_PARSER_CHECK_MINPOINTS) )
{
uint32_t vertices_needed = 3;
if ( ring->type == LINETYPE )
vertices_needed = 4;
if (lwgeom_count_vertices(ring) < vertices_needed)
{
LWDEBUG(4,"number of points is incorrect");
lwgeom_free(ring);
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_MOREPOINTS);
return NULL;
}
}
/* Apply check for not closed rings, if requested. */
if( (global_parser_result.parser_check_flags & LW_PARSER_CHECK_CLOSURE) )
{
int is_closed = 1;
LWDEBUG(4,"checking ring closure");
switch ( ring->type )
{
case LINETYPE:
is_closed = lwline_is_closed(lwgeom_as_lwline(ring));
break;
case CIRCSTRINGTYPE:
is_closed = lwcircstring_is_closed(lwgeom_as_lwcircstring(ring));
break;
case COMPOUNDTYPE:
is_closed = lwcompound_is_closed(lwgeom_as_lwcompound(ring));
break;
}
if ( ! is_closed )
{
LWDEBUG(4,"ring is not closed");
lwgeom_free(ring);
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_UNCLOSED);
return NULL;
}
}
if( LW_FAILURE == lwcurvepoly_add_ring(lwgeom_as_lwcurvepoly(poly), ring) )
{
LWDEBUG(4,"failed to add ring");
lwgeom_free(ring);
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
return poly;
}
LWGEOM* wkt_parser_curvepolygon_finalize(LWGEOM *poly, char *dimensionality)
{
uint8_t flags = wkt_dimensionality(dimensionality);
int flagdims = FLAGS_NDIMS(flags);
LWDEBUG(4,"entered");
/* Null input implies empty return */
if( ! poly )
return lwcurvepoly_as_lwgeom(lwcurvepoly_construct_empty(SRID_UNKNOWN, FLAGS_GET_Z(flags), FLAGS_GET_M(flags)));
if ( flagdims > 2 )
{
/* If the number of dimensions are not consistent, we have a problem. */
if( flagdims != FLAGS_NDIMS(poly->flags) )
{
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
/* Harmonize the flags in the sub-components with the wkt flags */
if( LW_FAILURE == wkt_parser_set_dims(poly, flags) )
{
lwgeom_free(poly);
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
}
return poly;
}
LWGEOM* wkt_parser_collection_new(LWGEOM *geom)
{
LWCOLLECTION *col;
LWGEOM **geoms;
static int ngeoms = 1;
LWDEBUG(4,"entered");
/* Toss error on null geometry input */
if( ! geom )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
/* Create our geometry array */
geoms = lwalloc(sizeof(LWGEOM*) * ngeoms);
geoms[0] = geom;
/* Make a new collection */
col = lwcollection_construct(COLLECTIONTYPE, SRID_UNKNOWN, NULL, ngeoms, geoms);
/* Return the result. */
return lwcollection_as_lwgeom(col);
}
LWGEOM* wkt_parser_compound_new(LWGEOM *geom)
{
LWCOLLECTION *col;
LWGEOM **geoms;
static int ngeoms = 1;
LWDEBUG(4,"entered");
/* Toss error on null geometry input */
if( ! geom )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
/* Elements of a compoundcurve cannot be empty, because */
/* empty things can't join up and form a ring */
if ( lwgeom_is_empty(geom) )
{
lwgeom_free(geom);
SET_PARSER_ERROR(PARSER_ERROR_INCONTINUOUS);
return NULL;
}
/* Create our geometry array */
geoms = lwalloc(sizeof(LWGEOM*) * ngeoms);
geoms[0] = geom;
/* Make a new collection */
col = lwcollection_construct(COLLECTIONTYPE, SRID_UNKNOWN, NULL, ngeoms, geoms);
/* Return the result. */
return lwcollection_as_lwgeom(col);
}
LWGEOM* wkt_parser_compound_add_geom(LWGEOM *col, LWGEOM *geom)
{
LWDEBUG(4,"entered");
/* Toss error on null geometry input */
if( ! (geom && col) )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
/* All the elements must agree on dimensionality */
if( FLAGS_NDIMS(col->flags) != FLAGS_NDIMS(geom->flags) )
{
lwgeom_free(col);
lwgeom_free(geom);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
if( LW_FAILURE == lwcompound_add_lwgeom((LWCOMPOUND*)col, geom) )
{
lwgeom_free(col);
lwgeom_free(geom);
SET_PARSER_ERROR(PARSER_ERROR_INCONTINUOUS);
return NULL;
}
return col;
}
LWGEOM* wkt_parser_collection_add_geom(LWGEOM *col, LWGEOM *geom)
{
LWDEBUG(4,"entered");
/* Toss error on null geometry input */
if( ! (geom && col) )
{
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
return lwcollection_as_lwgeom(lwcollection_add_lwgeom(lwgeom_as_lwcollection(col), geom));
}
LWGEOM* wkt_parser_collection_finalize(int lwtype, LWGEOM *geom, char *dimensionality)
{
uint8_t flags = wkt_dimensionality(dimensionality);
int flagdims = FLAGS_NDIMS(flags);
/* No geometry means it is empty */
if( ! geom )
{
return lwcollection_as_lwgeom(lwcollection_construct_empty(lwtype, SRID_UNKNOWN, FLAGS_GET_Z(flags), FLAGS_GET_M(flags)));
}
/* There are 'Z' or 'M' tokens in the signature */
if ( flagdims > 2 )
{
LWCOLLECTION *col = lwgeom_as_lwcollection(geom);
uint32_t i;
for ( i = 0 ; i < col->ngeoms; i++ )
{
LWGEOM *subgeom = col->geoms[i];
if ( FLAGS_NDIMS(flags) != FLAGS_NDIMS(subgeom->flags) &&
! lwgeom_is_empty(subgeom) )
{
lwgeom_free(geom);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
if ( lwtype == COLLECTIONTYPE &&
( (FLAGS_GET_Z(flags) != FLAGS_GET_Z(subgeom->flags)) ||
(FLAGS_GET_M(flags) != FLAGS_GET_M(subgeom->flags)) ) &&
! lwgeom_is_empty(subgeom) )
{
lwgeom_free(geom);
SET_PARSER_ERROR(PARSER_ERROR_MIXDIMS);
return NULL;
}
}
/* Harmonize the collection dimensionality */
if( LW_FAILURE == wkt_parser_set_dims(geom, flags) )
{
lwgeom_free(geom);
SET_PARSER_ERROR(PARSER_ERROR_OTHER);
return NULL;
}
}
/* Set the collection type */
geom->type = lwtype;
return geom;
}
void wkt_parser_geometry_new(LWGEOM *geom, int srid)
{
LWDEBUG(4,"entered");
LWDEBUGF(4,"geom %p",geom);
LWDEBUGF(4,"srid %d",srid);
if ( geom == NULL )
{
lwerror("Parsed geometry is null!");
return;
}
if ( srid != SRID_UNKNOWN && srid < SRID_MAXIMUM )
lwgeom_set_srid(geom, srid);
else
lwgeom_set_srid(geom, SRID_UNKNOWN);
global_parser_result.geom = geom;
}
void lwgeom_parser_result_init(LWGEOM_PARSER_RESULT *parser_result)
{
memset(parser_result, 0, sizeof(LWGEOM_PARSER_RESULT));
}
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
{
if ( parser_result->geom )
{
lwgeom_free(parser_result->geom);
parser_result->geom = 0;
}
if ( parser_result->serialized_lwgeom )
{
lwfree(parser_result->serialized_lwgeom );
parser_result->serialized_lwgeom = 0;
}
/* We don't free parser_result->message because
it is a const *char */
}
/*
* Public function used for easy access to the parser.
*/
LWGEOM *lwgeom_from_wkt(const char *wkt, const char check)
{
LWGEOM_PARSER_RESULT r;
if( LW_FAILURE == lwgeom_parse_wkt(&r, (char*)wkt, check) )
{
lwerror(r.message);
return NULL;
}
return r.geom;
}