forked from postgis/postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwin_wkb.c
806 lines (688 loc) · 20.3 KB
/
lwin_wkb.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
/**********************************************************************
*
* 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) 2009 Paul Ramsey <pramsey@cleverelephant.ca>
*
**********************************************************************/
#include "../postgis_config.h"
/*#define POSTGIS_DEBUG_LEVEL 4*/
#include "liblwgeom_internal.h" /* NOTE: includes lwgeom_log.h */
#include "lwgeom_log.h"
#include <math.h>
#include <limits.h>
/**
* Used for passing the parse state between the parsing functions.
*/
typedef struct
{
const uint8_t *wkb; /* Points to start of WKB */
size_t wkb_size; /* Expected size of WKB */
int swap_bytes; /* Do an endian flip? */
int check; /* Simple validity checks on geometries */
uint32_t lwtype; /* Current type we are handling */
uint32_t srid; /* Current SRID we are handling */
int has_z; /* Z? */
int has_m; /* M? */
int has_srid; /* SRID? */
const uint8_t *pos; /* Current parse position */
} wkb_parse_state;
/**
* Internal function declarations.
*/
LWGEOM* lwgeom_from_wkb_state(wkb_parse_state *s);
/**********************************************************************/
/* Our static character->number map. Anything > 15 is invalid */
static uint8_t hex2char[256] = {
/* not Hex characters */
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
/* 0-9 */
0,1,2,3,4,5,6,7,8,9,20,20,20,20,20,20,
/* A-F */
20,10,11,12,13,14,15,20,20,20,20,20,20,20,20,20,
/* not Hex characters */
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
/* a-f */
20,10,11,12,13,14,15,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
/* not Hex characters (upper 128 characters) */
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20
};
uint8_t* bytes_from_hexbytes(const char *hexbuf, size_t hexsize)
{
uint8_t *buf = NULL;
register uint8_t h1, h2;
uint32_t i;
if( hexsize % 2 )
lwerror("Invalid hex string, length (%d) has to be a multiple of two!", hexsize);
buf = lwalloc(hexsize/2);
if( ! buf )
lwerror("Unable to allocate memory buffer.");
for( i = 0; i < hexsize/2; i++ )
{
h1 = hex2char[(int)hexbuf[2*i]];
h2 = hex2char[(int)hexbuf[2*i+1]];
if( h1 > 15 )
lwerror("Invalid hex character (%c) encountered", hexbuf[2*i]);
if( h2 > 15 )
lwerror("Invalid hex character (%c) encountered", hexbuf[2*i+1]);
/* First character is high bits, second is low bits */
buf[i] = ((h1 & 0x0F) << 4) | (h2 & 0x0F);
}
return buf;
}
/**********************************************************************/
/**
* Check that we are not about to read off the end of the WKB
* array.
*/
static inline void wkb_parse_state_check(wkb_parse_state *s, size_t next)
{
if( (s->pos + next) > (s->wkb + s->wkb_size) )
{
lwerror("WKB structure does not match expected size!");
return;
}
}
/**
* Take in an unknown kind of wkb type number and ensure it comes out
* as an extended WKB type number (with Z/M/SRID flags masked onto the
* high bits).
*/
static void lwtype_from_wkb_state(wkb_parse_state *s, uint32_t wkb_type)
{
uint32_t wkb_simple_type;
LWDEBUG(4, "Entered function");
s->has_z = LW_FALSE;
s->has_m = LW_FALSE;
s->has_srid = LW_FALSE;
/* If any of the higher bits are set, this is probably an extended type. */
if( wkb_type & 0xF0000000 )
{
if( wkb_type & WKBZOFFSET ) s->has_z = LW_TRUE;
if( wkb_type & WKBMOFFSET ) s->has_m = LW_TRUE;
if( wkb_type & WKBSRIDFLAG ) s->has_srid = LW_TRUE;
LWDEBUGF(4, "Extended type: has_z=%d has_m=%d has_srid=%d", s->has_z, s->has_m, s->has_srid);
}
/* Mask off the flags */
wkb_type = wkb_type & 0x0FFFFFFF;
/* Strip out just the type number (1-12) from the ISO number (eg 3001-3012) */
wkb_simple_type = wkb_type % 1000;
/* Extract the Z/M information from ISO style numbers */
if( wkb_type >= 3000 && wkb_type < 4000 )
{
s->has_z = LW_TRUE;
s->has_m = LW_TRUE;
}
else if ( wkb_type >= 2000 && wkb_type < 3000 )
{
s->has_m = LW_TRUE;
}
else if ( wkb_type >= 1000 && wkb_type < 2000 )
{
s->has_z = LW_TRUE;
}
switch (wkb_simple_type)
{
case WKB_POINT_TYPE:
s->lwtype = POINTTYPE;
break;
case WKB_LINESTRING_TYPE:
s->lwtype = LINETYPE;
break;
case WKB_POLYGON_TYPE:
s->lwtype = POLYGONTYPE;
break;
case WKB_MULTIPOINT_TYPE:
s->lwtype = MULTIPOINTTYPE;
break;
case WKB_MULTILINESTRING_TYPE:
s->lwtype = MULTILINETYPE;
break;
case WKB_MULTIPOLYGON_TYPE:
s->lwtype = MULTIPOLYGONTYPE;
break;
case WKB_GEOMETRYCOLLECTION_TYPE:
s->lwtype = COLLECTIONTYPE;
break;
case WKB_CIRCULARSTRING_TYPE:
s->lwtype = CIRCSTRINGTYPE;
break;
case WKB_COMPOUNDCURVE_TYPE:
s->lwtype = COMPOUNDTYPE;
break;
case WKB_CURVEPOLYGON_TYPE:
s->lwtype = CURVEPOLYTYPE;
break;
case WKB_MULTICURVE_TYPE:
s->lwtype = MULTICURVETYPE;
break;
case WKB_MULTISURFACE_TYPE:
s->lwtype = MULTISURFACETYPE;
break;
case WKB_POLYHEDRALSURFACE_TYPE:
s->lwtype = POLYHEDRALSURFACETYPE;
break;
case WKB_TIN_TYPE:
s->lwtype = TINTYPE;
break;
case WKB_TRIANGLE_TYPE:
s->lwtype = TRIANGLETYPE;
break;
/* PostGIS 1.5 emits 13, 14 for CurvePolygon, MultiCurve */
/* These numbers aren't SQL/MM (numbers currently only */
/* go up to 12. We can handle the old data here (for now??) */
/* converting them into the lwtypes that are intended. */
case WKB_CURVE_TYPE:
s->lwtype = CURVEPOLYTYPE;
break;
case WKB_SURFACE_TYPE:
s->lwtype = MULTICURVETYPE;
break;
default: /* Error! */
lwerror("Unknown WKB type (%d)! Full WKB type number was (%d).", wkb_simple_type, wkb_type);
break;
}
LWDEBUGF(4,"Got lwtype %s (%u)", lwtype_name(s->lwtype), s->lwtype);
return;
}
/**
* Byte
* Read a byte and advance the parse state forward.
*/
static char byte_from_wkb_state(wkb_parse_state *s)
{
char char_value = 0;
LWDEBUG(4, "Entered function");
wkb_parse_state_check(s, WKB_BYTE_SIZE);
LWDEBUG(4, "Passed state check");
char_value = s->pos[0];
LWDEBUGF(4, "Read byte value: %x", char_value);
s->pos += WKB_BYTE_SIZE;
return char_value;
}
/**
* Int32
* Read 4-byte integer and advance the parse state forward.
*/
static uint32_t integer_from_wkb_state(wkb_parse_state *s)
{
uint32_t i = 0;
wkb_parse_state_check(s, WKB_INT_SIZE);
memcpy(&i, s->pos, WKB_INT_SIZE);
/* Swap? Copy into a stack-allocated integer. */
if( s->swap_bytes )
{
int j = 0;
uint8_t tmp;
for( j = 0; j < WKB_INT_SIZE/2; j++ )
{
tmp = ((uint8_t*)(&i))[j];
((uint8_t*)(&i))[j] = ((uint8_t*)(&i))[WKB_INT_SIZE - j - 1];
((uint8_t*)(&i))[WKB_INT_SIZE - j - 1] = tmp;
}
}
s->pos += WKB_INT_SIZE;
return i;
}
/**
* Double
* Read an 8-byte double and advance the parse state forward.
*/
static double double_from_wkb_state(wkb_parse_state *s)
{
double d = 0;
wkb_parse_state_check(s, WKB_DOUBLE_SIZE);
memcpy(&d, s->pos, WKB_DOUBLE_SIZE);
/* Swap? Copy into a stack-allocated integer. */
if( s->swap_bytes )
{
int i = 0;
uint8_t tmp;
for( i = 0; i < WKB_DOUBLE_SIZE/2; i++ )
{
tmp = ((uint8_t*)(&d))[i];
((uint8_t*)(&d))[i] = ((uint8_t*)(&d))[WKB_DOUBLE_SIZE - i - 1];
((uint8_t*)(&d))[WKB_DOUBLE_SIZE - i - 1] = tmp;
}
}
s->pos += WKB_DOUBLE_SIZE;
return d;
}
/**
* POINTARRAY
* Read a dynamically sized point array and advance the parse state forward.
* First read the number of points, then read the points.
*/
static POINTARRAY* ptarray_from_wkb_state(wkb_parse_state *s)
{
POINTARRAY *pa = NULL;
size_t pa_size;
uint32_t ndims = 2;
uint32_t npoints = 0;
static uint32_t maxpoints = UINT_MAX / WKB_DOUBLE_SIZE / 4;
/* Calculate the size of this point array. */
npoints = integer_from_wkb_state(s);
if (npoints > maxpoints)
{
lwerror("Pointarray length (%d) is too large");
return NULL;
}
LWDEBUGF(4,"Pointarray has %d points", npoints);
if( s->has_z ) ndims++;
if( s->has_m ) ndims++;
pa_size = npoints * ndims * WKB_DOUBLE_SIZE;
/* Empty! */
if( npoints == 0 )
return ptarray_construct(s->has_z, s->has_m, npoints);
/* Does the data we want to read exist? */
wkb_parse_state_check(s, pa_size);
/* If we're in a native endianness, we can just copy the data directly! */
if( ! s->swap_bytes )
{
pa = ptarray_construct_copy_data(s->has_z, s->has_m, npoints, (uint8_t*)s->pos);
s->pos += pa_size;
}
/* Otherwise we have to read each double, separately. */
else
{
uint32_t i = 0;
double *dlist;
pa = ptarray_construct(s->has_z, s->has_m, npoints);
dlist = (double*)(pa->serialized_pointlist);
for( i = 0; i < npoints * ndims; i++ )
{
dlist[i] = double_from_wkb_state(s);
}
}
return pa;
}
/**
* POINT
* Read a WKB point, starting just after the endian byte,
* type number and optional srid number.
* Advance the parse state forward appropriately.
* WKB point has just a set of doubles, with the quantity depending on the
* dimension of the point, so this looks like a special case of the above
* with only one point.
*/
static LWPOINT* lwpoint_from_wkb_state(wkb_parse_state *s)
{
static uint32_t npoints = 1;
POINTARRAY *pa = NULL;
size_t pa_size;
uint32_t ndims = 2;
const POINT2D *pt;
/* Count the dimensions. */
if( s->has_z ) ndims++;
if( s->has_m ) ndims++;
pa_size = ndims * WKB_DOUBLE_SIZE;
/* Does the data we want to read exist? */
wkb_parse_state_check(s, pa_size);
/* If we're in a native endianness, we can just copy the data directly! */
if( ! s->swap_bytes )
{
pa = ptarray_construct_copy_data(s->has_z, s->has_m, npoints, (uint8_t*)s->pos);
s->pos += pa_size;
}
/* Otherwise we have to read each double, separately */
else
{
uint32_t i = 0;
double *dlist;
pa = ptarray_construct(s->has_z, s->has_m, npoints);
dlist = (double*)(pa->serialized_pointlist);
for( i = 0; i < ndims; i++ )
{
dlist[i] = double_from_wkb_state(s);
}
}
/* Check for POINT(NaN NaN) ==> POINT EMPTY */
pt = getPoint2d_cp(pa, 0);
if ( isnan(pt->x) && isnan(pt->y) )
{
ptarray_free(pa);
return lwpoint_construct_empty(s->srid, s->has_z, s->has_m);
}
else
{
return lwpoint_construct(s->srid, NULL, pa);
}
}
/**
* LINESTRING
* Read a WKB linestring, starting just after the endian byte,
* type number and optional srid number. Advance the parse state
* forward appropriately.
* There is only one pointarray in a linestring. Optionally
* check for minimal following of rules (two point minimum).
*/
static LWLINE* lwline_from_wkb_state(wkb_parse_state *s)
{
POINTARRAY *pa = ptarray_from_wkb_state(s);
if( pa == NULL || pa->npoints == 0 )
{
ptarray_free(pa);
return lwline_construct_empty(s->srid, s->has_z, s->has_m);
}
if( s->check & LW_PARSER_CHECK_MINPOINTS && pa->npoints < 2 )
{
lwerror("%s must have at least two points", lwtype_name(s->lwtype));
return NULL;
}
return lwline_construct(s->srid, NULL, pa);
}
/**
* CIRCULARSTRING
* Read a WKB circularstring, starting just after the endian byte,
* type number and optional srid number. Advance the parse state
* forward appropriately.
* There is only one pointarray in a linestring. Optionally
* check for minimal following of rules (three point minimum,
* odd number of points).
*/
static LWCIRCSTRING* lwcircstring_from_wkb_state(wkb_parse_state *s)
{
POINTARRAY *pa = ptarray_from_wkb_state(s);
if( pa == NULL || pa->npoints == 0 )
return lwcircstring_construct_empty(s->srid, s->has_z, s->has_m);
if( s->check & LW_PARSER_CHECK_MINPOINTS && pa->npoints < 3 )
{
lwerror("%s must have at least three points", lwtype_name(s->lwtype));
return NULL;
}
if( s->check & LW_PARSER_CHECK_ODD && ! (pa->npoints % 2) )
{
lwerror("%s must have an odd number of points", lwtype_name(s->lwtype));
return NULL;
}
return lwcircstring_construct(s->srid, NULL, pa);
}
/**
* POLYGON
* Read a WKB polygon, starting just after the endian byte,
* type number and optional srid number. Advance the parse state
* forward appropriately.
* First read the number of rings, then read each ring
* (which are structured as point arrays)
*/
static LWPOLY* lwpoly_from_wkb_state(wkb_parse_state *s)
{
uint32_t nrings = integer_from_wkb_state(s);
uint32_t i = 0;
LWPOLY *poly = lwpoly_construct_empty(s->srid, s->has_z, s->has_m);
LWDEBUGF(4,"Polygon has %d rings", nrings);
/* Empty polygon? */
if( nrings == 0 )
return poly;
for( i = 0; i < nrings; i++ )
{
POINTARRAY *pa = ptarray_from_wkb_state(s);
if( pa == NULL )
continue;
/* Check for at least four points. */
if( s->check & LW_PARSER_CHECK_MINPOINTS && pa->npoints < 4 )
{
LWDEBUGF(2, "%s must have at least four points in each ring", lwtype_name(s->lwtype));
lwerror("%s must have at least four points in each ring", lwtype_name(s->lwtype));
return NULL;
}
/* Check that first and last points are the same. */
if( s->check & LW_PARSER_CHECK_CLOSURE && ! ptarray_is_closed_2d(pa) )
{
LWDEBUGF(2, "%s must have closed rings", lwtype_name(s->lwtype));
lwerror("%s must have closed rings", lwtype_name(s->lwtype));
return NULL;
}
/* Add ring to polygon */
if ( lwpoly_add_ring(poly, pa) == LW_FAILURE )
{
LWDEBUG(2, "Unable to add ring to polygon");
lwerror("Unable to add ring to polygon");
}
}
return poly;
}
/**
* TRIANGLE
* Read a WKB triangle, starting just after the endian byte,
* type number and optional srid number. Advance the parse state
* forward appropriately.
* Triangles are encoded like polygons in WKB, but more like linestrings
* as lwgeometries.
*/
static LWTRIANGLE* lwtriangle_from_wkb_state(wkb_parse_state *s)
{
uint32_t nrings = integer_from_wkb_state(s);
LWTRIANGLE *tri = lwtriangle_construct_empty(s->srid, s->has_z, s->has_m);
POINTARRAY *pa = NULL;
/* Empty triangle? */
if( nrings == 0 )
return tri;
/* Should be only one ring. */
if ( nrings != 1 )
lwerror("Triangle has wrong number of rings: %d", nrings);
/* There's only one ring, we hope? */
pa = ptarray_from_wkb_state(s);
/* If there's no points, return an empty triangle. */
if( pa == NULL )
return tri;
/* Check for at least four points. */
if( s->check & LW_PARSER_CHECK_MINPOINTS && pa->npoints < 4 )
{
LWDEBUGF(2, "%s must have at least four points", lwtype_name(s->lwtype));
lwerror("%s must have at least four points", lwtype_name(s->lwtype));
return NULL;
}
if( s->check & LW_PARSER_CHECK_ZCLOSURE && ! ptarray_is_closed_z(pa) )
{
lwerror("%s must have closed rings", lwtype_name(s->lwtype));
return NULL;
}
/* Empty TRIANGLE starts w/ empty POINTARRAY, free it first */
if (tri->points)
ptarray_free(tri->points);
tri->points = pa;
return tri;
}
/**
* CURVEPOLYTYPE
*/
static LWCURVEPOLY* lwcurvepoly_from_wkb_state(wkb_parse_state *s)
{
uint32_t ngeoms = integer_from_wkb_state(s);
LWCURVEPOLY *cp = lwcurvepoly_construct_empty(s->srid, s->has_z, s->has_m);
LWGEOM *geom = NULL;
uint32_t i;
/* Empty collection? */
if ( ngeoms == 0 )
return cp;
for ( i = 0; i < ngeoms; i++ )
{
geom = lwgeom_from_wkb_state(s);
if ( lwcurvepoly_add_ring(cp, geom) == LW_FAILURE )
lwerror("Unable to add geometry (%p) to curvepoly (%p)", geom, cp);
}
return cp;
}
/**
* POLYHEDRALSURFACETYPE
*/
/**
* COLLECTION, MULTIPOINTTYPE, MULTILINETYPE, MULTIPOLYGONTYPE, COMPOUNDTYPE,
* MULTICURVETYPE, MULTISURFACETYPE,
* TINTYPE
*/
static LWCOLLECTION* lwcollection_from_wkb_state(wkb_parse_state *s)
{
uint32_t ngeoms = integer_from_wkb_state(s);
LWCOLLECTION *col = lwcollection_construct_empty(s->lwtype, s->srid, s->has_z, s->has_m);
LWGEOM *geom = NULL;
uint32_t i;
LWDEBUGF(4,"Collection has %d components", ngeoms);
/* Empty collection? */
if ( ngeoms == 0 )
return col;
/* Be strict in polyhedral surface closures */
if ( s->lwtype == POLYHEDRALSURFACETYPE )
s->check |= LW_PARSER_CHECK_ZCLOSURE;
for ( i = 0; i < ngeoms; i++ )
{
geom = lwgeom_from_wkb_state(s);
if ( lwcollection_add_lwgeom(col, geom) == NULL )
{
lwerror("Unable to add geometry (%p) to collection (%p)", geom, col);
return NULL;
}
}
return col;
}
/**
* GEOMETRY
* Generic handling for WKB geometries. The front of every WKB geometry
* (including those embedded in collections) is an endian byte, a type
* number and an optional srid number. We handle all those here, then pass
* to the appropriate handler for the specific type.
*/
LWGEOM* lwgeom_from_wkb_state(wkb_parse_state *s)
{
char wkb_little_endian;
uint32_t wkb_type;
LWDEBUG(4,"Entered function");
/* Fail when handed incorrect starting byte */
wkb_little_endian = byte_from_wkb_state(s);
if( wkb_little_endian != 1 && wkb_little_endian != 0 )
{
LWDEBUG(4,"Leaving due to bad first byte!");
lwerror("Invalid endian flag value encountered.");
return NULL;
}
/* Check the endianness of our input */
s->swap_bytes = LW_FALSE;
if( getMachineEndian() == NDR ) /* Machine arch is little */
{
if ( ! wkb_little_endian ) /* Data is big! */
s->swap_bytes = LW_TRUE;
}
else /* Machine arch is big */
{
if ( wkb_little_endian ) /* Data is little! */
s->swap_bytes = LW_TRUE;
}
/* Read the type number */
wkb_type = integer_from_wkb_state(s);
LWDEBUGF(4,"Got WKB type number: 0x%X", wkb_type);
lwtype_from_wkb_state(s, wkb_type);
/* Read the SRID, if necessary */
if( s->has_srid )
{
s->srid = clamp_srid(integer_from_wkb_state(s));
/* TODO: warn on explicit UNKNOWN srid ? */
LWDEBUGF(4,"Got SRID: %u", s->srid);
}
/* Do the right thing */
switch( s->lwtype )
{
case POINTTYPE:
return (LWGEOM*)lwpoint_from_wkb_state(s);
break;
case LINETYPE:
return (LWGEOM*)lwline_from_wkb_state(s);
break;
case CIRCSTRINGTYPE:
return (LWGEOM*)lwcircstring_from_wkb_state(s);
break;
case POLYGONTYPE:
return (LWGEOM*)lwpoly_from_wkb_state(s);
break;
case TRIANGLETYPE:
return (LWGEOM*)lwtriangle_from_wkb_state(s);
break;
case CURVEPOLYTYPE:
return (LWGEOM*)lwcurvepoly_from_wkb_state(s);
break;
case MULTIPOINTTYPE:
case MULTILINETYPE:
case MULTIPOLYGONTYPE:
case COMPOUNDTYPE:
case MULTICURVETYPE:
case MULTISURFACETYPE:
case POLYHEDRALSURFACETYPE:
case TINTYPE:
case COLLECTIONTYPE:
return (LWGEOM*)lwcollection_from_wkb_state(s);
break;
/* Unknown type! */
default:
lwerror("Unsupported geometry type: %s [%d]", lwtype_name(s->lwtype), s->lwtype);
}
/* Return value to keep compiler happy. */
return NULL;
}
/* TODO add check for SRID consistency */
/**
* WKB inputs *must* have a declared size, to prevent malformed WKB from reading
* off the end of the memory segment (this stops a malevolent user from declaring
* a one-ring polygon to have 10 rings, causing the WKB reader to walk off the
* end of the memory).
*
* Check is a bitmask of: LW_PARSER_CHECK_MINPOINTS, LW_PARSER_CHECK_ODD,
* LW_PARSER_CHECK_CLOSURE, LW_PARSER_CHECK_NONE, LW_PARSER_CHECK_ALL
*/
LWGEOM* lwgeom_from_wkb(const uint8_t *wkb, const size_t wkb_size, const char check)
{
wkb_parse_state s;
/* Initialize the state appropriately */
s.wkb = wkb;
s.wkb_size = wkb_size;
s.swap_bytes = LW_FALSE;
s.check = check;
s.lwtype = 0;
s.srid = SRID_UNKNOWN;
s.has_z = LW_FALSE;
s.has_m = LW_FALSE;
s.has_srid = LW_FALSE;
s.pos = wkb;
return lwgeom_from_wkb_state(&s);
}
LWGEOM* lwgeom_from_hexwkb(const char *hexwkb, const char check)
{
int hexwkb_len;
uint8_t *wkb;
LWGEOM *lwgeom;
if ( ! hexwkb )
{
lwerror("lwgeom_from_hexwkb: null input");
return NULL;
}
hexwkb_len = strlen(hexwkb);
wkb = bytes_from_hexbytes(hexwkb, hexwkb_len);
lwgeom = lwgeom_from_wkb(wkb, hexwkb_len/2, check);
lwfree(wkb);
return lwgeom;
}