forked from postgis/postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliblwgeom.h.in
2406 lines (2044 loc) · 77.7 KB
/
liblwgeom.h.in
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
/**********************************************************************
*
* 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 2011 Sandro Santilli <strk@kbt.io>
* Copyright 2011 Paul Ramsey <pramsey@cleverelephant.ca>
* Copyright 2007-2008 Mark Cave-Ayland
* Copyright 2001-2006 Refractions Research Inc.
*
**********************************************************************/
#ifndef _LIBLWGEOM_H
#define _LIBLWGEOM_H 1
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include "proj_api.h"
#if defined(PJ_VERSION) && PJ_VERSION >= 490
/* Enable new geodesic functions */
#define PROJ_GEODESIC 1
#else
/* Use the old (pre-2.2) geodesic functions */
#define PROJ_GEODESIC 0
#endif
/**
* @file liblwgeom.h
*
* This library is the generic geometry handling section of PostGIS. The geometry
* objects, constructors, destructors, and a set of spatial processing functions,
* are implemented here.
*
* The library is designed for use in non-PostGIS applications if necessary. The
* units tests at cunit/cu_tester.c and the loader/dumper programs at
* ../loader/shp2pgsql.c are examples of non-PostGIS applications using liblwgeom.
*
* Programs using this library can install their custom memory managers and error
* handlers by calling the lwgeom_set_handlers() function, otherwise the default
* ones will be used.
*/
/**
* liblwgeom versions
*/
#define LIBLWGEOM_VERSION "@POSTGIS_LIB_VERSION@"
#define LIBLWGEOM_VERSION_MAJOR "@POSTGIS_MAJOR_VERSION@"
#define LIBLWGEOM_VERSION_MINOR "@POSTGIS_MINOR_VERSION@"
#define LIBLWGEOM_GEOS_VERSION "@POSTGIS_GEOS_VERSION@"
/** Return lwgeom version string (not to be freed) */
const char* lwgeom_version(void);
/**
* Return types for functions with status returns.
*/
#define LW_TRUE 1
#define LW_FALSE 0
#define LW_UNKNOWN 2
#define LW_FAILURE 0
#define LW_SUCCESS 1
/**
* LWTYPE numbers, used internally by PostGIS
*/
#define POINTTYPE 1
#define LINETYPE 2
#define POLYGONTYPE 3
#define MULTIPOINTTYPE 4
#define MULTILINETYPE 5
#define MULTIPOLYGONTYPE 6
#define COLLECTIONTYPE 7
#define CIRCSTRINGTYPE 8
#define COMPOUNDTYPE 9
#define CURVEPOLYTYPE 10
#define MULTICURVETYPE 11
#define MULTISURFACETYPE 12
#define POLYHEDRALSURFACETYPE 13
#define TRIANGLETYPE 14
#define TINTYPE 15
#define NUMTYPES 16
/**
* Flags applied in EWKB to indicate Z/M dimensions and
* presence/absence of SRID and bounding boxes
*/
#define WKBZOFFSET 0x80000000
#define WKBMOFFSET 0x40000000
#define WKBSRIDFLAG 0x20000000
#define WKBBBOXFLAG 0x10000000
/** Ordinate names */
typedef enum LWORD_T {
LWORD_X = 0,
LWORD_Y = 1,
LWORD_Z = 2,
LWORD_M = 3
} LWORD;
/**********************************************************************
** Spherical radius.
** Moritz, H. (1980). Geodetic Reference System 1980, by resolution of
** the XVII General Assembly of the IUGG in Canberra.
** http://en.wikipedia.org/wiki/Earth_radius
** http://en.wikipedia.org/wiki/World_Geodetic_System
*/
#define WGS84_MAJOR_AXIS 6378137.0
#define WGS84_INVERSE_FLATTENING 298.257223563
#define WGS84_MINOR_AXIS (WGS84_MAJOR_AXIS - WGS84_MAJOR_AXIS / WGS84_INVERSE_FLATTENING)
#define WGS84_RADIUS ((2.0 * WGS84_MAJOR_AXIS + WGS84_MINOR_AXIS ) / 3.0)
/**
* Macros for manipulating the 'flags' byte. A uint8_t used as follows:
* VVSRGBMZ
* Version bit, followed by
* Validty, Solid, ReadOnly, Geodetic, HasBBox, HasM and HasZ flags.
*/
#define FLAGS_GET_Z(flags) ((flags) & 0x01)
#define FLAGS_GET_M(flags) (((flags) & 0x02)>>1)
#define FLAGS_GET_BBOX(flags) (((flags) & 0x04)>>2)
#define FLAGS_GET_GEODETIC(flags) (((flags) & 0x08)>>3)
#define FLAGS_GET_READONLY(flags) (((flags) & 0x10)>>4)
#define FLAGS_GET_SOLID(flags) (((flags) & 0x20)>>5)
#define FLAGS_SET_Z(flags, value) ((flags) = (value) ? ((flags) | 0x01) : ((flags) & 0xFE))
#define FLAGS_SET_M(flags, value) ((flags) = (value) ? ((flags) | 0x02) : ((flags) & 0xFD))
#define FLAGS_SET_BBOX(flags, value) ((flags) = (value) ? ((flags) | 0x04) : ((flags) & 0xFB))
#define FLAGS_SET_GEODETIC(flags, value) ((flags) = (value) ? ((flags) | 0x08) : ((flags) & 0xF7))
#define FLAGS_SET_READONLY(flags, value) ((flags) = (value) ? ((flags) | 0x10) : ((flags) & 0xEF))
#define FLAGS_SET_SOLID(flags, value) ((flags) = (value) ? ((flags) | 0x20) : ((flags) & 0xDF))
#define FLAGS_NDIMS(flags) (2 + FLAGS_GET_Z(flags) + FLAGS_GET_M(flags))
#define FLAGS_GET_ZM(flags) (FLAGS_GET_M(flags) + FLAGS_GET_Z(flags) * 2)
#define FLAGS_NDIMS_BOX(flags) (FLAGS_GET_GEODETIC(flags) ? 3 : FLAGS_NDIMS(flags))
/**
* Macros for manipulating the 'typemod' int. An int32_t used as follows:
* Plus/minus = Top bit.
* Spare bits = Next 2 bits.
* SRID = Next 21 bits.
* TYPE = Next 6 bits.
* ZM Flags = Bottom 2 bits.
*/
#define TYPMOD_GET_SRID(typmod) ((((typmod) & 0x0FFFFF00) - ((typmod) & 0x10000000)) >> 8)
#define TYPMOD_SET_SRID(typmod, srid) ((typmod) = (((typmod) & 0xE00000FF) | ((srid & 0x001FFFFF)<<8)))
#define TYPMOD_GET_TYPE(typmod) ((typmod & 0x000000FC)>>2)
#define TYPMOD_SET_TYPE(typmod, type) ((typmod) = (typmod & 0xFFFFFF03) | ((type & 0x0000003F)<<2))
#define TYPMOD_GET_Z(typmod) ((typmod & 0x00000002)>>1)
#define TYPMOD_SET_Z(typmod) ((typmod) = typmod | 0x00000002)
#define TYPMOD_GET_M(typmod) (typmod & 0x00000001)
#define TYPMOD_SET_M(typmod) ((typmod) = typmod | 0x00000001)
#define TYPMOD_GET_NDIMS(typmod) (2+TYPMOD_GET_Z(typmod)+TYPMOD_GET_M(typmod))
/**
* Maximum allowed SRID value in serialized geometry.
* Currently we are using 21 bits (2097152) of storage for SRID.
*/
#define SRID_MAXIMUM @SRID_MAX@
/**
* Maximum valid SRID value for the user
* We reserve 1000 values for internal use
*/
#define SRID_USER_MAXIMUM @SRID_USR_MAX@
/** Unknown SRID value */
#define SRID_UNKNOWN 0
#define SRID_IS_UNKNOWN(x) ((int)x<=0)
/*
** EPSG WGS84 geographics, OGC standard default SRS, better be in
** the SPATIAL_REF_SYS table!
*/
#define SRID_DEFAULT 4326
#ifndef __GNUC__
# define __attribute__(x)
#endif
/**
* Return a valid SRID from an arbitrary integer
* Raises a notice if what comes out is different from
* what went in.
* Raises an error if SRID value is out of bounds.
*/
extern int clamp_srid(int srid);
/* Raise an lwerror if srids do not match */
void error_if_srid_mismatch(int srid1, int srid2);
/**
* Global functions for memory/logging handlers.
*/
typedef void* (*lwallocator)(size_t size);
typedef void* (*lwreallocator)(void *mem, size_t size);
typedef void (*lwfreeor)(void* mem);
typedef void (*lwreporter)(const char* fmt, va_list ap)
__attribute__ (( format(printf, 1, 0) ));
typedef void (*lwdebuglogger)(int level, const char* fmt, va_list ap)
__attribute__ (( format(printf, 2,0) ));
/**
* Install custom memory management and error handling functions you want your
* application to use.
* @ingroup system
* @todo take a structure ?
*/
extern void lwgeom_set_handlers(lwallocator allocator,
lwreallocator reallocator, lwfreeor freeor, lwreporter errorreporter,
lwreporter noticereporter);
extern void lwgeom_set_debuglogger(lwdebuglogger debuglogger);
/**
* Request interruption of any running code
*
* Safe for use from signal handlers
*
* Interrupted code will (as soon as it finds out
* to be interrupted) cleanup and return as soon as possible.
*
* The return value from interrupted code is undefined,
* it is the caller responsibility to not take it in consideration.
*
*/
extern void lwgeom_request_interrupt(void);
/**
* Cancel any interruption request
*/
extern void lwgeom_cancel_interrupt(void);
/**
* Install a callback to be called periodically during
* algorithm execution. Mostly only needed on WIN32 to
* dispatch queued signals.
*
* The callback is invoked before checking for interrupt
* being requested, so you can request interruption from
* the callback, if you want (see lwgeom_request_interrupt).
*
*/
typedef void (lwinterrupt_callback)();
extern lwinterrupt_callback *lwgeom_register_interrupt_callback(lwinterrupt_callback *);
/******************************************************************/
typedef struct {
double afac, bfac, cfac, dfac, efac, ffac, gfac, hfac, ifac, xoff, yoff, zoff;
} AFFINE;
/******************************************************************/
typedef struct
{
double xmin, ymin, zmin;
double xmax, ymax, zmax;
int32_t srid;
}
BOX3D;
/******************************************************************
* GBOX structure.
* We include the flags (information about dimensinality),
* so we don't have to constantly pass them
* into functions that use the GBOX.
*/
typedef struct
{
uint8_t flags;
double xmin;
double xmax;
double ymin;
double ymax;
double zmin;
double zmax;
double mmin;
double mmax;
} GBOX;
/******************************************************************
* SPHEROID
*
* Standard definition of an ellipsoid (what wkt calls a spheroid)
* f = (a-b)/a
* e_sq = (a*a - b*b)/(a*a)
* b = a - fa
*/
typedef struct
{
double a; /* semimajor axis */
double b; /* semiminor axis b = (a - fa) */
double f; /* flattening f = (a-b)/a */
double e; /* eccentricity (first) */
double e_sq; /* eccentricity squared (first) e_sq = (a*a-b*b)/(a*a) */
double radius; /* spherical average radius = (2*a+b)/3 */
char name[20]; /* name of ellipse */
}
SPHEROID;
/******************************************************************
* POINT2D, POINT3D, POINT3DM, POINT4D
*/
typedef struct
{
double x, y;
}
POINT2D;
typedef struct
{
double x, y, z;
}
POINT3DZ;
typedef struct
{
double x, y, z;
}
POINT3D;
typedef struct
{
double x, y, m;
}
POINT3DM;
typedef struct
{
double x, y, z, m;
}
POINT4D;
/******************************************************************
* POINTARRAY
* Point array abstracts a lot of the complexity of points and point lists.
* It handles 2d/3d translation
* (2d points converted to 3d will have z=0 or NaN)
* DO NOT MIX 2D and 3D POINTS! EVERYTHING* is either one or the other
*/
typedef struct
{
/* Array of POINT 2D, 3D or 4D, possibly missaligned. */
uint8_t *serialized_pointlist;
/* Use FLAGS_* macros to handle */
uint8_t flags;
uint32_t npoints; /* how many points we are currently storing */
uint32_t maxpoints; /* how many points we have space for in serialized_pointlist */
}
POINTARRAY;
/******************************************************************
* GSERIALIZED
*/
typedef struct
{
uint32_t size; /* For PgSQL use only, use VAR* macros to manipulate. */
uint8_t srid[3]; /* 24 bits of SRID */
uint8_t flags; /* HasZ, HasM, HasBBox, IsGeodetic, IsReadOnly */
uint8_t data[1]; /* See gserialized.txt */
} GSERIALIZED;
/******************************************************************
* LWGEOM (any geometry type)
*
* Abstract type, note that 'type', 'bbox' and 'srid' are available in
* all geometry variants.
*/
typedef struct
{
uint8_t type;
uint8_t flags;
GBOX *bbox;
int32_t srid;
void *data;
}
LWGEOM;
/* POINTYPE */
typedef struct
{
uint8_t type; /* POINTTYPE */
uint8_t flags;
GBOX *bbox;
int32_t srid;
POINTARRAY *point; /* hide 2d/3d (this will be an array of 1 point) */
}
LWPOINT; /* "light-weight point" */
/* LINETYPE */
typedef struct
{
uint8_t type; /* LINETYPE */
uint8_t flags;
GBOX *bbox;
int32_t srid;
POINTARRAY *points; /* array of POINT3D */
}
LWLINE; /* "light-weight line" */
/* TRIANGLE */
typedef struct
{
uint8_t type;
uint8_t flags;
GBOX *bbox;
int32_t srid;
POINTARRAY *points;
}
LWTRIANGLE;
/* CIRCSTRINGTYPE */
typedef struct
{
uint8_t type; /* CIRCSTRINGTYPE */
uint8_t flags;
GBOX *bbox;
int32_t srid;
POINTARRAY *points; /* array of POINT(3D/3DM) */
}
LWCIRCSTRING; /* "light-weight circularstring" */
/* POLYGONTYPE */
typedef struct
{
uint8_t type; /* POLYGONTYPE */
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t nrings; /* how many rings we are currently storing */
uint32_t maxrings; /* how many rings we have space for in **rings */
POINTARRAY **rings; /* list of rings (list of points) */
}
LWPOLY; /* "light-weight polygon" */
/* MULTIPOINTTYPE */
typedef struct
{
uint8_t type;
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t ngeoms; /* how many geometries we are currently storing */
uint32_t maxgeoms; /* how many geometries we have space for in **geoms */
LWPOINT **geoms;
}
LWMPOINT;
/* MULTILINETYPE */
typedef struct
{
uint8_t type;
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t ngeoms; /* how many geometries we are currently storing */
uint32_t maxgeoms; /* how many geometries we have space for in **geoms */
LWLINE **geoms;
}
LWMLINE;
/* MULTIPOLYGONTYPE */
typedef struct
{
uint8_t type;
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t ngeoms; /* how many geometries we are currently storing */
uint32_t maxgeoms; /* how many geometries we have space for in **geoms */
LWPOLY **geoms;
}
LWMPOLY;
/* COLLECTIONTYPE */
typedef struct
{
uint8_t type;
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t ngeoms; /* how many geometries we are currently storing */
uint32_t maxgeoms; /* how many geometries we have space for in **geoms */
LWGEOM **geoms;
}
LWCOLLECTION;
/* COMPOUNDTYPE */
typedef struct
{
uint8_t type; /* COMPOUNDTYPE */
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t ngeoms; /* how many geometries we are currently storing */
uint32_t maxgeoms; /* how many geometries we have space for in **geoms */
LWGEOM **geoms;
}
LWCOMPOUND; /* "light-weight compound line" */
/* CURVEPOLYTYPE */
typedef struct
{
uint8_t type; /* CURVEPOLYTYPE */
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t nrings; /* how many rings we are currently storing */
uint32_t maxrings; /* how many rings we have space for in **rings */
LWGEOM **rings; /* list of rings (list of points) */
}
LWCURVEPOLY; /* "light-weight polygon" */
/* MULTICURVE */
typedef struct
{
uint8_t type;
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t ngeoms; /* how many geometries we are currently storing */
uint32_t maxgeoms; /* how many geometries we have space for in **geoms */
LWGEOM **geoms;
}
LWMCURVE;
/* MULTISURFACETYPE */
typedef struct
{
uint8_t type;
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t ngeoms; /* how many geometries we are currently storing */
uint32_t maxgeoms; /* how many geometries we have space for in **geoms */
LWGEOM **geoms;
}
LWMSURFACE;
/* POLYHEDRALSURFACETYPE */
typedef struct
{
uint8_t type;
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t ngeoms; /* how many geometries we are currently storing */
uint32_t maxgeoms; /* how many geometries we have space for in **geoms */
LWPOLY **geoms;
}
LWPSURFACE;
/* TINTYPE */
typedef struct
{
uint8_t type;
uint8_t flags;
GBOX *bbox;
int32_t srid;
uint32_t ngeoms; /* how many geometries we are currently storing */
uint32_t maxgeoms; /* how many geometries we have space for in **geoms */
LWTRIANGLE **geoms;
}
LWTIN;
/* Casts LWGEOM->LW* (return NULL if cast is illegal) */
extern LWMPOLY *lwgeom_as_lwmpoly(const LWGEOM *lwgeom);
extern LWMLINE *lwgeom_as_lwmline(const LWGEOM *lwgeom);
extern LWMPOINT *lwgeom_as_lwmpoint(const LWGEOM *lwgeom);
extern LWCOLLECTION *lwgeom_as_lwcollection(const LWGEOM *lwgeom);
extern LWPOLY *lwgeom_as_lwpoly(const LWGEOM *lwgeom);
extern LWLINE *lwgeom_as_lwline(const LWGEOM *lwgeom);
extern LWPOINT *lwgeom_as_lwpoint(const LWGEOM *lwgeom);
extern LWCIRCSTRING *lwgeom_as_lwcircstring(const LWGEOM *lwgeom);
extern LWCURVEPOLY *lwgeom_as_lwcurvepoly(const LWGEOM *lwgeom);
extern LWCOMPOUND *lwgeom_as_lwcompound(const LWGEOM *lwgeom);
extern LWPSURFACE *lwgeom_as_lwpsurface(const LWGEOM *lwgeom);
extern LWTRIANGLE *lwgeom_as_lwtriangle(const LWGEOM *lwgeom);
extern LWTIN *lwgeom_as_lwtin(const LWGEOM *lwgeom);
extern LWGEOM *lwgeom_as_multi(const LWGEOM *lwgeom);
extern LWGEOM *lwgeom_as_curve(const LWGEOM *lwgeom);
/* Casts LW*->LWGEOM (always cast) */
extern LWGEOM *lwtin_as_lwgeom(const LWTIN *obj);
extern LWGEOM *lwtriangle_as_lwgeom(const LWTRIANGLE *obj);
extern LWGEOM *lwpsurface_as_lwgeom(const LWPSURFACE *obj);
extern LWGEOM *lwmpoly_as_lwgeom(const LWMPOLY *obj);
extern LWGEOM *lwmline_as_lwgeom(const LWMLINE *obj);
extern LWGEOM *lwmpoint_as_lwgeom(const LWMPOINT *obj);
extern LWGEOM *lwcollection_as_lwgeom(const LWCOLLECTION *obj);
extern LWGEOM *lwcircstring_as_lwgeom(const LWCIRCSTRING *obj);
extern LWGEOM *lwcompound_as_lwgeom(const LWCOMPOUND *obj);
extern LWGEOM *lwcurvepoly_as_lwgeom(const LWCURVEPOLY *obj);
extern LWGEOM *lwpoly_as_lwgeom(const LWPOLY *obj);
extern LWGEOM *lwline_as_lwgeom(const LWLINE *obj);
extern LWGEOM *lwpoint_as_lwgeom(const LWPOINT *obj);
extern LWCOLLECTION* lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom);
extern LWMPOINT* lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj);
extern LWMLINE* lwmline_add_lwline(LWMLINE *mobj, const LWLINE *obj);
extern LWMPOLY* lwmpoly_add_lwpoly(LWMPOLY *mobj, const LWPOLY *obj);
extern LWPSURFACE* lwpsurface_add_lwpoly(LWPSURFACE *mobj, const LWPOLY *obj);
extern LWTIN* lwtin_add_lwtriangle(LWTIN *mobj, const LWTRIANGLE *obj);
/***********************************************************************
** Utility functions for flag byte and srid_flag integer.
*/
/**
* Construct a new flags char.
*/
extern uint8_t gflags(int hasz, int hasm, int geodetic);
/**
* Extract the geometry type from the serialized form (it hides in
* the anonymous data area, so this is a handy function).
*/
extern uint32_t gserialized_get_type(const GSERIALIZED *g);
/**
* Returns the size in bytes to read from toast to get the basic
* information from a geometry: GSERIALIZED struct, bbox and type
*/
extern uint32_t gserialized_max_header_size(void);
/**
* Returns the size in bytes of the header, from the start of the
* object up to the type number.
*/
extern uint32_t gserialized_header_size(const GSERIALIZED *gser);
/**
* Extract the SRID from the serialized form (it is packed into
* three bytes so this is a handy function).
*/
extern int32_t gserialized_get_srid(const GSERIALIZED *g);
/**
* Write the SRID into the serialized form (it is packed into
* three bytes so this is a handy function).
*/
extern void gserialized_set_srid(GSERIALIZED *g, int32_t srid);
/**
* Check if a #GSERIALIZED is empty without deserializing first.
* Only checks if the number of elements of the parent geometry
* is zero, will not catch collections of empty, eg:
* GEOMETRYCOLLECTION(POINT EMPTY)
*/
extern int gserialized_is_empty(const GSERIALIZED *g);
/**
* Check if a #GSERIALIZED has a bounding box without deserializing first.
*/
extern int gserialized_has_bbox(const GSERIALIZED *gser);
/**
* Check if a #GSERIALIZED has a Z ordinate.
*/
extern int gserialized_has_z(const GSERIALIZED *gser);
/**
* Check if a #GSERIALIZED has an M ordinate.
*/
extern int gserialized_has_m(const GSERIALIZED *gser);
/**
* Check if a #GSERIALIZED is a geography.
*/
extern int gserialized_is_geodetic(const GSERIALIZED *gser);
/**
* Return a number indicating presence of Z and M coordinates.
* 0 = None, 1 = M, 2 = Z, 3 = ZM
*/
extern int gserialized_get_zm(const GSERIALIZED *gser);
/**
* Return the number of dimensions (2, 3, 4) in a geometry
*/
extern int gserialized_ndims(const GSERIALIZED *gser);
/**
* Return -1 if g1 is "less than" g2, 1 if g1 is "greater than"
* g2 and 0 if g1 and g2 are the "same". Equality is evaluated
* with a memcmp and size check. So it is possible that two
* identical objects where one lacks a bounding box could be
* evaluated as non-equal initially. Greater and less than
* are evaluated by calculating a sortable key from the center
* point of the object bounds.
*/
extern int gserialized_cmp(const GSERIALIZED *g1, const GSERIALIZED *g2);
/**
* Call this function to drop BBOX and SRID
* from LWGEOM. If LWGEOM type is *not* flagged
* with the HASBBOX flag and has a bbox, it
* will be released.
*/
extern void lwgeom_drop_bbox(LWGEOM *lwgeom);
extern void lwgeom_drop_srid(LWGEOM *lwgeom);
/**
* Compute a bbox if not already computed
*
* After calling this function lwgeom->bbox is only
* NULL if the geometry is empty.
*/
extern void lwgeom_add_bbox(LWGEOM *lwgeom);
/**
* Drop current bbox and calculate a fresh one.
*/
extern void lwgeom_refresh_bbox(LWGEOM *lwgeom);
/**
* Compute a box for geom and all sub-geometries, if not already computed
*/
extern void lwgeom_add_bbox_deep(LWGEOM *lwgeom, GBOX *gbox);
/**
* Get a non-empty geometry bounding box, computing and
* caching it if not already there
*
* NOTE: empty geometries don't have a bounding box so
* you'd still get a NULL for them.
*/
extern const GBOX *lwgeom_get_bbox(const LWGEOM *lwgeom);
/**
* Determine whether a LWGEOM can contain sub-geometries or not
*/
extern int lwgeom_is_collection(const LWGEOM *lwgeom);
/******************************************************************/
/* Functions that work on type numbers */
/**
* Determine whether a type number is a collection or not
*/
extern int lwtype_is_collection(uint8_t type);
/**
* Given an lwtype number, what homogeneous collection can hold it?
*/
extern uint32_t lwtype_get_collectiontype(uint8_t type);
/**
* Return the type name string associated with a type number
* (e.g. Point, LineString, Polygon)
*/
extern const char *lwtype_name(uint8_t type);
/******************************************************************/
/*
* copies a point from the point array into the parameter point
* will set point's z=0 (or NaN) if pa is 2d
* will set point's m=0 (or NaN) if pa is 3d or 2d
* NOTE: point is a real POINT3D *not* a pointer
*/
extern POINT4D getPoint4d(const POINTARRAY *pa, uint32_t n);
/*
* copies a point from the point array into the parameter point
* will set point's z=0 (or NaN) if pa is 2d
* will set point's m=0 (or NaN) if pa is 3d or 2d
* NOTE: this will modify the point4d pointed to by 'point'.
*/
extern int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point);
/*
* copies a point from the point array into the parameter point
* will set point's z=0 (or NaN) if pa is 2d
* NOTE: point is a real POINT3D *not* a pointer
*/
extern POINT3DZ getPoint3dz(const POINTARRAY *pa, uint32_t n);
extern POINT3DM getPoint3dm(const POINTARRAY *pa, uint32_t n);
/*
* copies a point from the point array into the parameter point
* will set point's z=0 (or NaN) if pa is 2d
* NOTE: this will modify the point3d pointed to by 'point'.
*/
extern int getPoint3dz_p(const POINTARRAY *pa, uint32_t n, POINT3DZ *point);
extern int getPoint3dm_p(const POINTARRAY *pa, uint32_t n, POINT3DM *point);
/*
* copies a point from the point array into the parameter point
* z value (if present is not returned)
* NOTE: point is a real POINT3D *not* a pointer
*/
extern POINT2D getPoint2d(const POINTARRAY *pa, uint32_t n);
/*
* copies a point from the point array into the parameter point
* z value (if present is not returned)
* NOTE: this will modify the point2d pointed to by 'point'.
*/
extern int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point);
/**
* Returns a POINT2D pointer into the POINTARRAY serialized_ptlist,
* suitable for reading from. This is very high performance
* and declared const because you aren't allowed to muck with the
* values, only read them.
*/
extern const POINT2D* getPoint2d_cp(const POINTARRAY *pa, uint32_t n);
/**
* Returns a POINT3DZ pointer into the POINTARRAY serialized_ptlist,
* suitable for reading from. This is very high performance
* and declared const because you aren't allowed to muck with the
* values, only read them.
*/
extern const POINT3DZ* getPoint3dz_cp(const POINTARRAY *pa, uint32_t n);
/**
* Returns a POINT4D pointer into the POINTARRAY serialized_ptlist,
* suitable for reading from. This is very high performance
* and declared const because you aren't allowed to muck with the
* values, only read them.
*/
extern const POINT4D* getPoint4d_cp(const POINTARRAY *pa, uint32_t n);
/*
* set point N to the given value
* NOTE that the pointarray can be of any
* dimension, the appropriate ordinate values
* will be extracted from it
*
* N must be a valid point index
*/
extern void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d);
/*
* get a pointer to nth point of a POINTARRAY
* You'll need to cast it to appropriate dimensioned point.
* Note that if you cast to a higher dimensional point you'll
* possibly corrupt the POINTARRAY.
*
* WARNING: Don't cast this to a POINT !
* it would not be reliable due to memory alignment constraints
*/
extern uint8_t *getPoint_internal(const POINTARRAY *pa, uint32_t n);
/*
* size of point represeneted in the POINTARRAY
* 16 for 2d, 24 for 3d, 32 for 4d
*/
extern size_t ptarray_point_size(const POINTARRAY *pa);
/**
* Construct an empty pointarray, allocating storage and setting
* the npoints, but not filling in any information. Should be used in conjunction
* with ptarray_set_point4d to fill in the information in the array.
*/
extern POINTARRAY* ptarray_construct(char hasz, char hasm, uint32_t npoints);
/**
* Construct a new #POINTARRAY, <em>copying</em> in the data from ptlist
*/
extern POINTARRAY* ptarray_construct_copy_data(char hasz, char hasm, uint32_t npoints, const uint8_t *ptlist);
/**
* Construct a new #POINTARRAY, <em>referencing</em> to the data from ptlist
*/
extern POINTARRAY* ptarray_construct_reference_data(char hasz, char hasm, uint32_t npoints, uint8_t *ptlist);
/**
* Create a new #POINTARRAY with no points. Allocate enough storage
* to hold maxpoints vertices before having to reallocate the storage
* area.
*/
extern POINTARRAY* ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints);
/**
* Append a point to the end of an existing #POINTARRAY
* If allow_duplicate is LW_FALSE, then a duplicate point will
* not be added.
*/
extern int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates);
/**
* Append a #POINTARRAY, pa2 to the end of an existing #POINTARRAY, pa1.
*
* If gap_tolerance is >= 0 then the end point of pa1 will be checked for
* being within gap_tolerance 2d distance from start point of pa2 or an
* error will be raised and LW_FAILURE returned.
* A gap_tolerance < 0 disables the check.
*
* If end point of pa1 and start point of pa2 are 2d-equal, then pa2 first
* point will not be appended.
*/
extern int ptarray_append_ptarray(POINTARRAY *pa1, POINTARRAY *pa2, double gap_tolerance);
/**
* Insert a point into an existing #POINTARRAY. Zero
* is the index of the start of the array.
*/
extern int ptarray_insert_point(POINTARRAY *pa, const POINT4D *p, uint32_t where);
/**
* Remove a point from an existing #POINTARRAY. Zero
* is the index of the start of the array.
*/
extern int ptarray_remove_point(POINTARRAY *pa, uint32_t where);
/**
* @brief Add a point in a pointarray.
*
* @param pa the source POINTARRAY
* @param p the point to add
* @param pdims number of ordinates in p (2..4)
* @param where to insert the point. 0 prepends, pa->npoints appends
*
* @returns a newly constructed POINTARRAY using a newly allocated buffer
* for the actual points, or NULL on error.
*/
extern POINTARRAY *ptarray_addPoint(const POINTARRAY *pa, uint8_t *p, size_t pdims, uint32_t where);
/**
* @brief Remove a point from a pointarray.
* @param which - is the offset (starting at 0)
* @return #POINTARRAY is newly allocated
*/
extern POINTARRAY *ptarray_removePoint(POINTARRAY *pa, uint32_t where);
/**
* @brief Merge two given POINTARRAY and returns a pointer
* on the new aggregate one.
* Warning: this function free the two inputs POINTARRAY
* @return #POINTARRAY is newly allocated
*/
extern POINTARRAY *ptarray_merge(POINTARRAY *pa1, POINTARRAY *pa2);
extern int ptarray_is_closed(const POINTARRAY *pa);
extern int ptarray_is_closed_2d(const POINTARRAY *pa);
extern int ptarray_is_closed_3d(const POINTARRAY *pa);
extern int ptarray_is_closed_z(const POINTARRAY *pa);
extern POINTARRAY* ptarray_flip_coordinates(POINTARRAY *pa);
/**
* @d1 start location (distance from start / total distance)
* @d2 end location (distance from start / total distance)
* @param tolerance snap to vertices at locations < tolerance away from given ones
*/
extern POINTARRAY *ptarray_substring(POINTARRAY *pa, double d1, double d2,
double tolerance);
/**
* Strip out the Z/M components of an #LWGEOM
*/
extern LWGEOM* lwgeom_force_2d(const LWGEOM *geom);
extern LWGEOM* lwgeom_force_3dz(const LWGEOM *geom);
extern LWGEOM* lwgeom_force_3dm(const LWGEOM *geom);
extern LWGEOM* lwgeom_force_4d(const LWGEOM *geom);
extern LWGEOM* lwgeom_set_effective_area(const LWGEOM *igeom, int set_area, double area);
/*
* Force to use SFS 1.1 geometry type
* (rather than SFS 1.2 and/or SQL/MM)
*/
extern LWGEOM* lwgeom_force_sfs(LWGEOM *geom, int version);
/*--------------------------------------------------------
* all the base types (point/line/polygon) will have a
* basic constructor, basic de-serializer, basic serializer,
* bounding box finder and (TODO) serialized form size finder.