-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpostgis_gml.c
1933 lines (1595 loc) · 50.7 KB
/
postgis_gml.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
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
/**********************************************************************
*
* VagueGeometry - Vague Spatial Objects for PostgreSQL
* http://gbd.dc.ufscar.br/vaguegeometry/
*
* Copyright 2013-2015 Anderson Chaves Carniel <[email protected]>
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU General Public Licence. See the COPYING file.
*
* Fully developed by Anderson Chaves Carniel
*
**********************************************************************/
/**********************************************************************
*
* Part this file was copied from lwgeom_in_gml.c from PostGIS Project.
*
**********************************************************************/
#include "postgres.h"
#include "executor/spi.h"
#include "libvgeom.h"
#include "lwgeom_transform.h"
#include <string.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
static LWGEOM* parse_gml(xmlNodePtr xnode, bool *hasz, int *root_srid);
typedef struct struct_gmlSrs
{
int srid;
bool reverse_axis;
}
gmlSrs;
#define XLINK_NS ((char *) "http://www.w3.org/1999/xlink")
#define GML_NS ((char *) "http://www.opengis.net/gml")
#define GML32_NS ((char *) "http://www.opengis.net/gml/3.2")
static void gml_lwerror(char *msg, int error_code) {
// POSTGIS_DEBUGF(3, "ST_GeomFromGML ERROR %i", error_code);
lwerror("%s", msg);
}
/**
* Return false if current element namespace is not a GML one
* Return true otherwise.
*/
static bool is_gml_namespace(xmlNodePtr xnode, bool is_strict)
{
xmlNsPtr *ns, *p;
ns = xmlGetNsList(xnode->doc, xnode);
/*
* If no namespace is available we could return true anyway
* (because we work only on GML fragment, we don't want to
* 'oblige' to add namespace on the geometry root node)
*/
if (ns == NULL) { return !is_strict; }
/*
* Handle namespaces:
* - http://www.opengis.net/gml (GML 3.1.1 and priors)
* - http://www.opengis.net/gml/3.2 (GML 3.2.1)
*/
for (p=ns ; *p ; p++)
{
if ((*p)->href == NULL || (*p)->prefix == NULL ||
xnode->ns == NULL || xnode->ns->prefix == NULL) continue;
if (!xmlStrcmp(xnode->ns->prefix, (*p)->prefix))
{
if ( !strcmp((char *) (*p)->href, GML_NS)
|| !strcmp((char *) (*p)->href, GML32_NS))
{
xmlFree(ns);
return true;
} else {
xmlFree(ns);
return false;
}
}
}
xmlFree(ns);
return !is_strict; /* Same reason here to not return false */
}
/**
* Retrieve a GML propertie from a node or NULL otherwise
* Respect namespaces if presents in the node element
*/
static xmlChar *gmlGetProp(xmlNodePtr xnode, xmlChar *prop)
{
xmlChar *value;
if (!is_gml_namespace(xnode, true))
return xmlGetProp(xnode, prop);
/*
* Handle namespaces:
* - http://www.opengis.net/gml (GML 3.1.1 and priors)
* - http://www.opengis.net/gml/3.2 (GML 3.2.1)
*/
value = xmlGetNsProp(xnode, prop, (xmlChar *) GML_NS);
if (value == NULL) value = xmlGetNsProp(xnode, prop, (xmlChar *) GML32_NS);
/* In last case try without explicit namespace */
if (value == NULL) value = xmlGetNoNsProp(xnode, prop);
return value;
}
/**
* Return true if current node contains a simple XLink
* Return false otherwise.
*/
static bool is_xlink(xmlNodePtr node)
{
xmlChar *prop;
prop = xmlGetNsProp(node, (xmlChar *)"type", (xmlChar *) XLINK_NS);
if (prop == NULL) return false;
if (strcmp((char *) prop, "simple"))
{
xmlFree(prop);
return false;
}
prop = xmlGetNsProp(node, (xmlChar *)"href", (xmlChar *) XLINK_NS);
if (prop == NULL) return false;
if (prop[0] != '#')
{
xmlFree(prop);
return false;
}
xmlFree(prop);
return true;
}
/**
* Return a xmlNodePtr on a node referenced by a XLink or NULL otherwise
*/
static xmlNodePtr get_xlink_node(xmlNodePtr xnode) {
char *id;
xmlNsPtr *ns, *n;
xmlXPathContext *ctx;
xmlXPathObject *xpath;
xmlNodePtr node, ret_node;
xmlChar *href, *p, *node_id;
href = xmlGetNsProp(xnode, (xmlChar *)"href", (xmlChar *) XLINK_NS);
id = lwalloc((xmlStrlen(xnode->ns->prefix) * 2 + xmlStrlen(xnode->name)
+ xmlStrlen(href) + sizeof("//:[@:id='']") + 1));
p = href;
p++; /* ignore '#' first char */
/* XPath pattern look like: //gml:point[@gml:id='p1'] */
sprintf(id, "//%s:%s[@%s:id='%s']", (char *) xnode->ns->prefix,
(char *) xnode->name,
(char *) xnode->ns->prefix,
(char *) p);
ctx = xmlXPathNewContext(xnode->doc);
if (ctx == NULL)
{
xmlFree(href);
lwfree(id);
return NULL;
}
/* Handle namespaces */
ns = xmlGetNsList(xnode->doc, xnode);
for (n=ns ; *n; n++) xmlXPathRegisterNs(ctx, (*n)->prefix, (*n)->href);
xmlFree(ns);
/* Execute XPath expression */
xpath = xmlXPathEvalExpression((xmlChar *) id, ctx);
lwfree(id);
if (xpath == NULL || xpath->nodesetval == NULL || xpath->nodesetval->nodeNr != 1)
{
xmlFree(href);
xmlXPathFreeObject(xpath);
xmlXPathFreeContext(ctx);
return NULL;
}
ret_node = xpath->nodesetval->nodeTab[0];
xmlXPathFreeObject(xpath);
xmlXPathFreeContext(ctx);
/* Protection against circular calls */
for (node = xnode ; node != NULL ; node = node->parent)
{
if (node->type != XML_ELEMENT_NODE) continue;
node_id = gmlGetProp(node, (xmlChar *) "id");
if (node_id != NULL)
{
if (!xmlStrcmp(node_id, p))
gml_lwerror("invalid GML representation", 2);
xmlFree(node_id);
}
}
xmlFree(href);
return ret_node;
}
/**
* Use Proj4 to reproject a given POINTARRAY
*/
static POINTARRAY* gml_reproject_pa(POINTARRAY *pa, int srid_in, int srid_out)
{
int i;
POINT4D p;
projPJ in_pj, out_pj;
char *text_in, *text_out;
if (srid_in == SRID_UNKNOWN) return pa; /* nothing to do */
if (srid_out == SRID_UNKNOWN) gml_lwerror("invalid GML representation", 3);
text_in = GetProj4StringSPI(srid_in);
text_out = GetProj4StringSPI(srid_out);
in_pj = lwproj_from_string(text_in);
out_pj = lwproj_from_string(text_out);
lwfree(text_in);
lwfree(text_out);
for (i=0 ; i < pa->npoints ; i++)
{
getPoint4d_p(pa, i, &p);
point4d_transform(&p, in_pj, out_pj);
ptarray_set_point4d(pa, i, &p);
}
pj_free(in_pj);
pj_free(out_pj);
return pa;
}
/**
* Return 1 if given srid is planar (0 otherwise, i.e geocentric srid)
* Return -1 if srid is not in spatial_ref_sys
*/
static int gml_is_srid_planar(int srid)
{
char *result;
char query[256];
int is_planar, err;
if (SPI_OK_CONNECT != SPI_connect ())
lwerror("gml_is_srid_planar: could not connect to SPI manager");
/* A way to find if this projection is planar or geocentric */
sprintf(query, "SELECT position('+units=m ' in proj4text) \
FROM spatial_ref_sys WHERE srid='%d'", srid);
err = SPI_exec(query, 1);
if (err < 0) lwerror("gml_is_srid_planar: error executing query %d", err);
/* No entry in spatial_ref_sys */
if (SPI_processed <= 0)
{
SPI_finish();
return -1;
}
result = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
is_planar = atoi(result);
SPI_finish();
return is_planar;
}
/**
* Parse gml srsName attribute
*/
static void parse_gml_srs(xmlNodePtr xnode, gmlSrs *srs)
{
char *p;
int is_planar;
xmlNodePtr node;
xmlChar *srsname;
bool latlon = false;
char sep = ':';
node = xnode;
srsname = gmlGetProp(node, (xmlChar *) "srsName");
/*printf("srsname %s\n",srsname);*/
if (!srsname)
{
if (node->parent == NULL)
{
srs->srid = SRID_UNKNOWN;
srs->reverse_axis = false;
return;
}
parse_gml_srs(node->parent, srs);
}
else
{
/* Severals srsName formats are available...
* cf WFS 1.1.0 -> 9.2 (p36)
* cf ISO 19142:2009 -> 7.9.2.4.4 (p34)
* cf RFC 5165 <http://tools.ietf.org/html/rfc5165>
* cf CITE WFS-1.1 (GetFeature-tc17.2)
*/
/* SRS pattern like: EPSG:4326
urn:EPSG:geographicCRS:4326
urn:ogc:def:crs:EPSG:4326
urn:ogc:def:crs:EPSG::4326
urn:ogc:def:crs:EPSG:6.6:4326
urn:x-ogc:def:crs:EPSG:6.6:4326
http://www.opengis.net/gml/srs/epsg.xml#4326
http://www.epsg.org/6.11.2/4326
*/
if (!strncmp((char *) srsname, "EPSG:", 5))
{
sep = ':';
latlon = false;
}
else if (!strncmp((char *) srsname, "urn:ogc:def:crs:EPSG:", 21)
|| !strncmp((char *) srsname, "urn:x-ogc:def:crs:EPSG:", 23)
|| !strncmp((char *) srsname, "urn:EPSG:geographicCRS:", 23))
{
sep = ':';
latlon = true;
}
else if (!strncmp((char *) srsname,
"http://www.opengis.net/gml/srs/epsg.xml#", 40))
{
sep = '#';
latlon = false;
}
else gml_lwerror("unknown spatial reference system", 4);
/* retrieve the last ':' or '#' char */
for (p = (char *) srsname ; *p ; p++);
for (--p ; *p != sep ; p--)
if (!isdigit(*p)) gml_lwerror("unknown spatial reference system", 5);
srs->srid = atoi(++p);
/* Check into spatial_ref_sys that this SRID really exist */
is_planar = gml_is_srid_planar(srs->srid);
if (srs->srid == SRID_UNKNOWN || is_planar == -1)
gml_lwerror("unknown spatial reference system", 6);
/* About lat/lon issue, Cf: http://tinyurl.com/yjpr55z */
srs->reverse_axis = !is_planar && latlon;
xmlFree(srsname);
return;
}
}
/**
* Parse a string supposed to be a double
*/
static double parse_gml_double(char *d, bool space_before, bool space_after)
{
char *p;
int st;
enum states
{
INIT = 0,
NEED_DIG = 1,
DIG = 2,
NEED_DIG_DEC = 3,
DIG_DEC = 4,
EXP = 5,
NEED_DIG_EXP = 6,
DIG_EXP = 7,
END = 8
};
/*
* Double pattern
* [-|\+]?[0-9]+(\.)?([0-9]+)?([Ee](\+|-)?[0-9]+)?
* We could also meet spaces before and/or after
* this pattern upon parameters
*/
if (space_before) while (isspace(*d)) d++;
for (st = INIT, p = d ; *p ; p++)
{
if (isdigit(*p))
{
if (st == INIT || st == NEED_DIG) st = DIG;
else if (st == NEED_DIG_DEC) st = DIG_DEC;
else if (st == NEED_DIG_EXP || st == EXP) st = DIG_EXP;
else if (st == DIG || st == DIG_DEC || st == DIG_EXP);
else gml_lwerror("invalid GML representation", 7);
}
else if (*p == '.')
{
if (st == DIG) st = NEED_DIG_DEC;
else gml_lwerror("invalid GML representation", 8);
}
else if (*p == '-' || *p == '+')
{
if (st == INIT) st = NEED_DIG;
else if (st == EXP) st = NEED_DIG_EXP;
else gml_lwerror("invalid GML representation", 9);
}
else if (*p == 'e' || *p == 'E')
{
if (st == DIG || st == DIG_DEC) st = EXP;
else gml_lwerror("invalid GML representation", 10);
}
else if (isspace(*p))
{
if (!space_after) gml_lwerror("invalid GML representation", 11);
if (st == DIG || st == DIG_DEC || st == DIG_EXP)st = END;
else if (st == NEED_DIG_DEC) st = END;
else if (st == END);
else gml_lwerror("invalid GML representation", 12);
}
else gml_lwerror("invalid GML representation", 13);
}
if (st != DIG && st != NEED_DIG_DEC && st != DIG_DEC && st != DIG_EXP && st != END)
gml_lwerror("invalid GML representation", 14);
return atof(d);
}
/**
* Parse gml:coordinates
*/
static POINTARRAY* parse_gml_coordinates(xmlNodePtr xnode, bool *hasz)
{
xmlChar *gml_coord, *gml_ts, *gml_cs, *gml_dec;
char cs, ts, dec;
POINTARRAY *dpa;
int gml_dims;
char *p, *q;
bool digit;
POINT4D pt;
/* We begin to retrieve coordinates string */
gml_coord = xmlNodeGetContent(xnode);
p = (char *) gml_coord;
/* Default GML coordinates pattern: x1,y1 x2,y2
* x1,y1,z1 x2,y2,z2
*
* Cf GML 2.1.2 -> 4.3.1 (p18)
*/
/* Retrieve separator between coordinates tuples */
gml_ts = gmlGetProp(xnode, (xmlChar *) "ts");
if (gml_ts == NULL) ts = ' ';
else
{
if (xmlStrlen(gml_ts) > 1 || isdigit(gml_ts[0]))
gml_lwerror("invalid GML representation", 15);
ts = gml_ts[0];
xmlFree(gml_ts);
}
/* Retrieve separator between each coordinate */
gml_cs = gmlGetProp(xnode, (xmlChar *) "cs");
if (gml_cs == NULL) cs = ',';
else
{
if (xmlStrlen(gml_cs) > 1 || isdigit(gml_cs[0]))
gml_lwerror("invalid GML representation", 16);
cs = gml_cs[0];
xmlFree(gml_cs);
}
/* Retrieve decimal separator */
gml_dec = gmlGetProp(xnode, (xmlChar *) "decimal");
if (gml_dec == NULL) dec = '.';
else
{
if (xmlStrlen(gml_dec) > 1 || isdigit(gml_dec[0]))
gml_lwerror("invalid GML representation", 17);
dec = gml_dec[0];
xmlFree(gml_dec);
}
if (cs == ts || cs == dec || ts == dec)
gml_lwerror("invalid GML representation", 18);
/* HasZ, !HasM, 1 Point */
dpa = ptarray_construct_empty(1, 0, 1);
while (isspace(*p)) p++; /* Eat extra whitespaces if any */
for (q = p, gml_dims=0, digit = false ; *p ; p++)
{
if (isdigit(*p)) digit = true; /* One state parser */
/* Coordinate Separator */
if (*p == cs)
{
*p = '\0';
gml_dims++;
if (*(p+1) == '\0') gml_lwerror("invalid GML representation", 19);
if (gml_dims == 1) pt.x = parse_gml_double(q, false, true);
else if (gml_dims == 2) pt.y = parse_gml_double(q, false, true);
q = p+1;
/* Tuple Separator (or end string) */
}
else if (digit && (*p == ts || *(p+1) == '\0'))
{
if (*p == ts) *p = '\0';
gml_dims++;
if (gml_dims < 2 || gml_dims > 3)
gml_lwerror("invalid GML representation", 20);
if (gml_dims == 3)
pt.z = parse_gml_double(q, false, true);
else
{
pt.y = parse_gml_double(q, false, true);
*hasz = false;
}
ptarray_append_point(dpa, &pt, LW_FALSE);
digit = false;
q = p+1;
gml_dims = 0;
/* Need to put standard decimal separator to atof handle */
}
else if (*p == dec && dec != '.') *p = '.';
}
xmlFree(gml_coord);
return dpa; /* ptarray_clone_deep(dpa); */
}
/**
* Parse gml:coord
*/
static POINTARRAY* parse_gml_coord(xmlNodePtr xnode, bool *hasz)
{
xmlNodePtr xyz;
POINTARRAY *dpa;
bool x,y,z;
xmlChar *c;
POINT4D p;
/* HasZ?, !HasM, 1 Point */
dpa = ptarray_construct_empty(1, 0, 1);
x = y = z = false;
for (xyz = xnode->children ; xyz != NULL ; xyz = xyz->next)
{
if (xyz->type != XML_ELEMENT_NODE) continue;
if (!is_gml_namespace(xyz, false)) continue;
if (!strcmp((char *) xyz->name, "X"))
{
if (x) gml_lwerror("invalid GML representation", 21);
c = xmlNodeGetContent(xyz);
p.x = parse_gml_double((char *) c, true, true);
x = true;
xmlFree(c);
}
else if (!strcmp((char *) xyz->name, "Y"))
{
if (y) gml_lwerror("invalid GML representation", 22);
c = xmlNodeGetContent(xyz);
p.y = parse_gml_double((char *) c, true, true);
y = true;
xmlFree(c);
}
else if (!strcmp((char *) xyz->name, "Z"))
{
if (z) gml_lwerror("invalid GML representation", 23);
c = xmlNodeGetContent(xyz);
p.z = parse_gml_double((char *) c, true, true);
z = true;
xmlFree(c);
}
}
/* Check dimension consistancy */
if (!x || !y) gml_lwerror("invalid GML representation", 24);
if (!z) *hasz = false;
ptarray_append_point(dpa, &p, LW_FALSE);
x = y = z = false;
return ptarray_clone_deep(dpa);
}
/**
* Parse gml:pos
*/
static POINTARRAY* parse_gml_pos(xmlNodePtr xnode, bool *hasz)
{
xmlChar *dimension, *gmlpos;
xmlNodePtr posnode;
int dim, gml_dim;
POINTARRAY *dpa;
char *pos, *p;
bool digit;
POINT4D pt;
/* HasZ, !HasM, 1 Point */
dpa = ptarray_construct_empty(1, 0, 1);
for (posnode = xnode ; posnode != NULL ; posnode = posnode->next)
{
/* We only care about gml:pos element */
if (posnode->type != XML_ELEMENT_NODE) continue;
if (!is_gml_namespace(posnode, false)) continue;
if (strcmp((char *) posnode->name, "pos")) continue;
dimension = gmlGetProp(xnode, (xmlChar *) "srsDimension");
if (dimension == NULL) /* in GML 3.0.0 it was dimension */
dimension = gmlGetProp(xnode, (xmlChar *) "dimension");
if (dimension == NULL) dim = 2; /* We assume that we are in 2D */
else
{
dim = atoi((char *) dimension);
xmlFree(dimension);
if (dim < 2 || dim > 3)
gml_lwerror("invalid GML representation", 25);
}
if (dim == 2) *hasz = false;
/* We retrieve gml:pos string */
gmlpos = xmlNodeGetContent(posnode);
pos = (char *) gmlpos;
while (isspace(*pos)) pos++; /* Eat extra whitespaces if any */
/* gml:pos pattern: x1 y1
* x1 y1 z1
*/
for (p=pos, gml_dim=0, digit=false ; *pos ; pos++)
{
if (isdigit(*pos)) digit = true;
if (digit && (*pos == ' ' || *(pos+1) == '\0'))
{
if (*pos == ' ') *pos = '\0';
gml_dim++;
if (gml_dim == 1)
pt.x = parse_gml_double(p, true, true);
else if (gml_dim == 2)
pt.y = parse_gml_double(p, true, true);
else if (gml_dim == 3)
pt.z = parse_gml_double(p, true, true);
p = pos+1;
digit = false;
}
}
xmlFree(gmlpos);
/* Test again coherent dimensions on each coord */
if (gml_dim == 2) *hasz = false;
if (gml_dim < 2 || gml_dim > 3 || gml_dim != dim)
gml_lwerror("invalid GML representation", 26);
ptarray_append_point(dpa, &pt, LW_FALSE);
}
return ptarray_clone_deep(dpa);
}
/**
* Parse gml:posList
*/
static POINTARRAY* parse_gml_poslist(xmlNodePtr xnode, bool *hasz)
{
xmlChar *dimension, *gmlposlist;
char *poslist, *p;
int dim, gml_dim;
POINTARRAY *dpa;
POINT4D pt;
bool digit;
/* Retrieve gml:srsDimension attribute if any */
dimension = gmlGetProp(xnode, (xmlChar *) "srsDimension");
if (dimension == NULL) /* in GML 3.0.0 it was dimension */
dimension = gmlGetProp(xnode, (xmlChar *) "dimension");
if (dimension == NULL) dim = 2; /* We assume that we are in common 2D */
else
{
dim = atoi((char *) dimension);
xmlFree(dimension);
if (dim < 2 || dim > 3) gml_lwerror("invalid GML representation", 27);
}
if (dim == 2) *hasz = false;
/* Retrieve gml:posList string */
gmlposlist = xmlNodeGetContent(xnode);
poslist = (char *) gmlposlist;
/* HasZ?, !HasM, 1 point */
dpa = ptarray_construct_empty(1, 0, 1);
/* gml:posList pattern: x1 y1 x2 y2
* x1 y1 z1 x2 y2 z2
*/
while (isspace(*poslist)) poslist++; /* Eat extra whitespaces if any */
for (p=poslist, gml_dim=0, digit=false ; *poslist ; poslist++)
{
if (isdigit(*poslist)) digit = true;
if (digit && (*poslist == ' ' || *(poslist+1) == '\0'))
{
if (*poslist == ' ') *poslist = '\0';
gml_dim++;
if (gml_dim == 1) pt.x = parse_gml_double(p, true, true);
else if (gml_dim == 2) pt.y = parse_gml_double(p, true, true);
else if (gml_dim == 3) pt.z = parse_gml_double(p, true, true);
if (gml_dim == dim)
{
ptarray_append_point(dpa, &pt, LW_FALSE);
gml_dim = 0;
}
else if (*(poslist+1) == '\0')
gml_lwerror("invalid GML representation", 28);
p = poslist+1;
digit = false;
}
}
xmlFree(gmlposlist);
return ptarray_clone_deep(dpa);
}
/**
* Parse data coordinates
*
* There's several ways to encode data coordinates, who could be mixed
* inside a single geometrie:
* - gml:pos element
* - gml:posList element
* - gml:pointProperty
* - gml:pointRep (deprecated in 3.1.0)
* - gml:coordinate element with tuples string inside (deprecated in 3.1.0)
* - gml:coord elements with X,Y(,Z) nested elements (deprecated in 3.0.0)
*/
static POINTARRAY* parse_gml_data(xmlNodePtr xnode, bool *hasz, int *root_srid)
{
POINTARRAY *pa = 0, *tmp_pa = 0;
xmlNodePtr xa, xb;
gmlSrs srs;
bool found;
pa = NULL;
for (xa = xnode ; xa != NULL ; xa = xa->next)
{
if (xa->type != XML_ELEMENT_NODE) continue;
if (!is_gml_namespace(xa, false)) continue;
if (xa->name == NULL) continue;
if (!strcmp((char *) xa->name, "pos"))
{
tmp_pa = parse_gml_pos(xa, hasz);
if (pa == NULL) pa = tmp_pa;
else pa = ptarray_merge(pa, tmp_pa);
}
else if (!strcmp((char *) xa->name, "posList"))
{
tmp_pa = parse_gml_poslist(xa, hasz);
if (pa == NULL) pa = tmp_pa;
else pa = ptarray_merge(pa, tmp_pa);
}
else if (!strcmp((char *) xa->name, "coordinates"))
{
tmp_pa = parse_gml_coordinates(xa, hasz);
if (pa == NULL) pa = tmp_pa;
else pa = ptarray_merge(pa, tmp_pa);
}
else if (!strcmp((char *) xa->name, "coord"))
{
tmp_pa = parse_gml_coord(xa, hasz);
if (pa == NULL) pa = tmp_pa;
else pa = ptarray_merge(pa, tmp_pa);
}
else if (!strcmp((char *) xa->name, "pointRep") ||
!strcmp((char *) xa->name, "pointProperty"))
{
found = false;
for (xb = xa->children ; xb != NULL ; xb = xb->next)
{
if (xb->type != XML_ELEMENT_NODE) continue;
if (!is_gml_namespace(xb, false)) continue;
if (!strcmp((char *) xb->name, "Point"))
{
found = true;
break;
}
}
if (!found || xb == NULL)
gml_lwerror("invalid GML representation", 29);
if (is_xlink(xb)) xb = get_xlink_node(xb);
if (xb == NULL || xb->children == NULL)
gml_lwerror("invalid GML representation", 30);
tmp_pa = parse_gml_data(xb->children, hasz, root_srid);
if (tmp_pa->npoints != 1)
gml_lwerror("invalid GML representation", 31);
parse_gml_srs(xb, &srs);
if (srs.reverse_axis) tmp_pa = ptarray_flip_coordinates(tmp_pa);
if (*root_srid == SRID_UNKNOWN) *root_srid = srs.srid;
else if (srs.srid != *root_srid)
gml_reproject_pa(tmp_pa, srs.srid, *root_srid);
if (pa == NULL) pa = tmp_pa;
else pa = ptarray_merge(pa, tmp_pa);
}
}
if (pa == NULL) gml_lwerror("invalid GML representation", 32);
return pa;
}
/**
* Parse GML point (2.1.2, 3.1.1)
*/
static LWGEOM* parse_gml_point(xmlNodePtr xnode, bool *hasz, int *root_srid)
{
gmlSrs srs;
LWGEOM *geom;
POINTARRAY *pa;
if (is_xlink(xnode)) xnode = get_xlink_node(xnode);
if (xnode->children == NULL)
return lwpoint_as_lwgeom(lwpoint_construct_empty(*root_srid, 0, 0));
pa = parse_gml_data(xnode->children, hasz, root_srid);
if (pa->npoints != 1) gml_lwerror("invalid GML representation", 34);
parse_gml_srs(xnode, &srs);
if (srs.reverse_axis) pa = ptarray_flip_coordinates(pa);
if (!*root_srid)
{
*root_srid = srs.srid;
geom = (LWGEOM *) lwpoint_construct(*root_srid, NULL, pa);
}
else
{
if (srs.srid != *root_srid)
gml_reproject_pa(pa, srs.srid, *root_srid);
geom = (LWGEOM *) lwpoint_construct(SRID_UNKNOWN, NULL, pa);
}
return geom;
}
/**
* Parse GML lineString (2.1.2, 3.1.1)
*/
static LWGEOM* parse_gml_line(xmlNodePtr xnode, bool *hasz, int *root_srid)
{
gmlSrs srs;
LWGEOM *geom;
POINTARRAY *pa;
if (is_xlink(xnode)) xnode = get_xlink_node(xnode);
if (xnode->children == NULL)
return lwline_as_lwgeom(lwline_construct_empty(*root_srid, 0, 0));
pa = parse_gml_data(xnode->children, hasz, root_srid);
if (pa->npoints < 2) gml_lwerror("invalid GML representation", 36);
parse_gml_srs(xnode, &srs);
if (srs.reverse_axis) pa = ptarray_flip_coordinates(pa);
if (!*root_srid)
{
*root_srid = srs.srid;
geom = (LWGEOM *) lwline_construct(*root_srid, NULL, pa);
}
else
{
if (srs.srid != *root_srid)
gml_reproject_pa(pa, srs.srid, *root_srid);
geom = (LWGEOM *) lwline_construct(SRID_UNKNOWN, NULL, pa);
}
return geom;
}
/**
* Parse GML Curve (3.1.1)
*/
static LWGEOM* parse_gml_curve(xmlNodePtr xnode, bool *hasz, int *root_srid)
{
xmlNodePtr xa;
int lss, last, i;
bool found=false;
gmlSrs srs;
LWGEOM *geom=NULL;
POINTARRAY *pa=NULL;
POINTARRAY **ppa=NULL;
uint32 npoints=0;
xmlChar *interpolation=NULL;
if (is_xlink(xnode)) xnode = get_xlink_node(xnode);
/* Looking for gml:segments */
for (xa = xnode->children ; xa != NULL ; xa = xa->next)
{
if (xa->type != XML_ELEMENT_NODE) continue;
if (!is_gml_namespace(xa, false)) continue;
if (!strcmp((char *) xa->name, "segments"))
{
found = true;
break;
}
}
if (!found) gml_lwerror("invalid GML representation", 37);
ppa = (POINTARRAY**) lwalloc(sizeof(POINTARRAY*));
/* Processing each gml:LineStringSegment */
for (xa = xa->children, lss=0; xa != NULL ; xa = xa->next)
{
if (xa->type != XML_ELEMENT_NODE) continue;
if (!is_gml_namespace(xa, false)) continue;
if (strcmp((char *) xa->name, "LineStringSegment")) continue;
/* GML SF is resticted to linear interpolation */
interpolation = gmlGetProp(xa, (xmlChar *) "interpolation");
if (interpolation != NULL)
{
if (strcmp((char *) interpolation, "linear"))
gml_lwerror("invalid GML representation", 38);
xmlFree(interpolation);
}
if (lss > 0) ppa = (POINTARRAY**) lwrealloc((POINTARRAY *) ppa,
sizeof(POINTARRAY*) * (lss + 1));
ppa[lss] = parse_gml_data(xa->children, hasz, root_srid);
npoints += ppa[lss]->npoints;
if (ppa[lss]->npoints < 2)
gml_lwerror("invalid GML representation", 39);
lss++;
}
if (lss == 0) gml_lwerror("invalid GML representation", 40);
/* Most common case, a single segment */
if (lss == 1) pa = ppa[0];
/*
* "The curve segments are connected to one another, with the end point
* of each segment except the last being the start point of the next
* segment" from ISO 19107:2003 -> 6.3.16.1 (p43)
*
* So we must aggregate all the segments into a single one and avoid
* to copy the redundants points
*/
if (lss > 1)
{