-
Notifications
You must be signed in to change notification settings - Fork 156
/
oracle_gis.c
1516 lines (1310 loc) · 43.5 KB
/
oracle_gis.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
/*-------------------------------------------------------------------------
*
* oracle_gis.c
* routines that convert between Oracle SDO_GEOMETRY and PostGIS EWKB
*
*-------------------------------------------------------------------------
*/
/*
* The code relies heavily on the PostGIS internal data stucture that is explained
* in g_serialized.txt in the PostGIS source code and implemented in liblwgeom.h.
*/
/* Oracle header */
#include <oci.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "oracle_fdw.h"
#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 WKBSRIDFLAG 0x20000000
#define WKBZOFFSET 0x80000000
#define WKBMOFFSET 0x40000000
#define uintToNumber(errhp, intp, numberp) { \
if (checkerr( \
OCINumberFromInt((errhp), (dvoid *)(intp), sizeof(unsigned), OCI_NUMBER_UNSIGNED, (numberp)), \
(errhp)) != OCI_SUCCESS) \
oracleError_d(FDW_ERROR, "OCINumberFromInt failed to convert integer to NUMBER", oraMessage); \
}
#define numberToUint(errhp, numberp, intp) { \
if (checkerr( \
OCINumberToInt((errhp), (numberp), sizeof(unsigned), OCI_NUMBER_UNSIGNED, (dvoid *)(intp)), \
(errhp)) != OCI_SUCCESS) \
oracleError_d(FDW_ERROR, "OCINumberToInt failed to convert NUMBER to integer", oraMessage); \
}
#define doubleToNumber(errhp, doublep, numberp) { \
if (checkerr( \
OCINumberFromReal((errhp), (const dvoid *)(doublep), sizeof(double), (numberp)), \
(errhp)) != OCI_SUCCESS) \
oracleError_d(FDW_ERROR, "OCINumberFromReal failed to convert floating point number to NUMBER", oraMessage); \
}
#define numberToDouble(errhp, numberp, doublep) { \
if (checkerr( \
OCINumberToReal((errhp), (numberp), sizeof(double), (dvoid *)(doublep)), \
(errhp)) != OCI_SUCCESS) \
oracleError_d(FDW_ERROR, "OCINumberToReal failed to convert NUMBER to floating point number", oraMessage); \
}
/* file for mapping SRIDs in the "share" directory */
#define SRID_MAP_FILE "srid.map"
typedef struct
{
unsigned from; /* != 0 for valid ones */
unsigned to;
} mapEntry;
#define mapEntryValid(x) ((x)->from != 0)
/* maps Oracle SRIDs to PostGIS SRIDs */
static mapEntry *srid_map = NULL;
/* contains Oracle error messages, set by checkerr() */
#define ERRBUFSIZE 500
static char oraMessage[ERRBUFSIZE];
static sb4 err_code;
/*
* Structures needed for managing the MDSYS.SDO_GEOMETRY Oracle type.
* Most of these were generated with OTT.
*/
typedef struct
{
OCINumber x;
OCINumber y;
OCINumber z;
} sdo_point_type;
typedef struct
{
OCIInd _atomic;
OCIInd x;
OCIInd y;
OCIInd z;
} sdo_point_type_ind;
typedef struct sdo_geometry
{
OCINumber sdo_gtype;
OCINumber sdo_srid;
sdo_point_type sdo_point;
OCIArray *sdo_elem_info;
OCIArray *sdo_ordinates;
} sdo_geometry;
typedef struct sdo_geometry_ind
{
OCIInd _atomic;
OCIInd sdo_gtype;
OCIInd sdo_srid;
sdo_point_type_ind sdo_point;
OCIInd sdo_elem_info;
OCIInd sdo_ordinates;
} sdo_geometry_ind;
static unsigned ewkbType(oracleSession *session, ora_geometry *geom);
static unsigned sdoDimension(oracleSession *session, ora_geometry *geom);
static unsigned ewkbIsMeasured(oracleSession *session, ora_geometry *geom);
static unsigned ewkbSrid(oracleSession *session, ora_geometry *geom);
static unsigned numCoord(oracleSession *session, ora_geometry *geom);
static double coord(oracleSession *session, ora_geometry *geom, unsigned i);
static unsigned numElemInfo(oracleSession *session, ora_geometry *geom);
static unsigned elemInfo(oracleSession *session, ora_geometry *geom, unsigned i);
static void appendElemInfo(oracleSession *session, ora_geometry *geom, unsigned info );
static void appendCoord(oracleSession *session, ora_geometry *geom, double coord);
static char *doubleFill(double x, char * dest);
static char *unsignedFill(unsigned i, char * dest);
static sword checkerr(sword status, OCIError *handle);
static void initSRIDMap(void);
static unsigned epsgFromOracle(unsigned srid);
static unsigned epsgToOracle(unsigned srid);
static void unpack(oracleSession *session, ora_geometry *geom);
static void freeUnpacked(oracleSession *session, ora_geometry *geom);
/* All ...Fill() functions return a pointer to the end of the written zone
*/
static unsigned ewkbHeaderLen(oracleSession *session, ora_geometry *geom);
static char *ewkbHeaderFill(oracleSession *session, ora_geometry *geom, char * dest);
static unsigned ewkbPointLen(oracleSession *session, ora_geometry *geom);
static char *ewkbPointFill(oracleSession *session, ora_geometry *geom, char *dest);
static unsigned ewkbLineLen(oracleSession *session, ora_geometry *geom);
static char *ewkbLineFill(oracleSession *session, ora_geometry *geom, char * dest);
static unsigned ewkbPolygonLen(oracleSession *session, ora_geometry *geom);
static char *ewkbPolygonFill(oracleSession *session, ora_geometry *geom, char * dest);
static unsigned ewkbMultiPointLen(oracleSession *session, ora_geometry *geom);
static char *ewkbMultiPointFill(oracleSession *session, ora_geometry *geom, char *dest);
static unsigned ewkbMultiLineLen(oracleSession *session, ora_geometry *geom);
static char *ewkbMultiLineFill(oracleSession *session, ora_geometry *geom, char * dest);
static unsigned ewkbMultiPolygonLen(oracleSession *session, ora_geometry *geom);
static char *ewkbMultiPolygonFill(oracleSession *session, ora_geometry *geom, char * dest);
/*
* All the set...() functions return a pointer that points to a position in the
* input buffer right after the added data.
* That way we can call several of them in a row and even nest them,
* like in the case of a multipolygon composed of several polygons.
*/
static const char *setType(oracleSession *session, ora_geometry *geom, const char *data);
static const char *setSridAndFlags(oracleSession *session, ora_geometry *geom, const char *data);
static const char *setPoint(oracleSession *session, ora_geometry *geom, const char *data);
static const char *setLine(oracleSession *session, ora_geometry *geom, const char *data);
static const char *setPolygon(oracleSession *session, ora_geometry *geom, const char *data);
static const char *setMultiPoint(oracleSession *session, ora_geometry *geom, const char *data);
static const char *setMultiLine(oracleSession *session, ora_geometry *geom, const char *data);
static const char *setMultiPolygon(oracleSession *session, ora_geometry *geom, const char *data);
char *
doubleFill(double x, char * dest)
{
memcpy(dest, &x, sizeof(double));
dest += sizeof(double);
return dest;
}
char *
unsignedFill(unsigned i, char * dest)
{
memcpy(dest, &i, sizeof(unsigned));
dest += sizeof(unsigned);
return dest;
}
/*
* initSRIDMap
* Allocates "srid_map" and reads the SRID map file into it.
*/
void
initSRIDMap()
{
char *mapFileName = NULL;
FILE *mapFile;
char line[20];
unsigned long from, to;
int count = 0, i, save_errno, c;
mapFileName = oracleGetShareFileName(SRID_MAP_FILE);
/* initialize "srid_map" with an invalid entry */
srid_map = (mapEntry *)malloc(sizeof(mapEntry));
if (srid_map == NULL)
oracleError_i(FDW_ERROR, "failed to allocate %d bytes of memory", sizeof(mapEntry));
srid_map[0].from = 0;
/* from here on we must make sure that srid_map is reset to NULL if an error occurs */
errno = 0;
if ((mapFile = fopen(mapFileName, "r")) == NULL)
{
/* if the file does not exist, treat it as if it were empty */
if (errno == ENOENT)
return;
/* other errors are reported */
free(srid_map);
srid_map = NULL;
oracleError(FDW_ERROR, "cannot open file \"" SRID_MAP_FILE "\": %m");
}
/* from here on we must make sure that mapFile is closed if an error happens */
oracleFree(mapFileName);
do
{
/* read the next line into "line" */
i = 0;
do
{
c = fgetc(mapFile);
if (c == '\n' || c == EOF)
{
line[i] = '\0';
}
else
{
if (i >= 19)
{
(void)fclose(mapFile);
free(srid_map);
srid_map = NULL;
oracleError(FDW_ERROR,
"syntax error in file \"" SRID_MAP_FILE "\": line too long");
}
line[i++] = c;
}
} while (c != '\n' && c != EOF);
/* ignore empty lines */
if (*line == '\0')
continue;
/* read two unsigned integers */
i = sscanf(line, "%lu %lu", &from, &to);
if (i == EOF)
{
save_errno = errno;
(void)fclose(mapFile);
errno = save_errno;
free(srid_map);
srid_map = NULL;
oracleError(FDW_ERROR, "syntax error in file \"" SRID_MAP_FILE "\": %m");
}
if (i != 2)
{
(void)fclose(mapFile);
free(srid_map);
srid_map = NULL;
oracleError(FDW_ERROR,
"syntax error in file \"" SRID_MAP_FILE "\": line does not contain two numbers");
}
if (from == 0 || to == 0)
{
(void)fclose(mapFile);
free(srid_map);
srid_map = NULL;
oracleError(FDW_ERROR,
"syntax error in file \"" SRID_MAP_FILE "\": SRID cannot be zero");
}
if (from > 0xffffffff || to > 0xffffffff)
{
(void)fclose(mapFile);
free(srid_map);
srid_map = NULL;
oracleError(FDW_ERROR,
"syntax error in file \"" SRID_MAP_FILE "\": number too large");
}
/* add a new mapEntry to srid_map */
srid_map = (mapEntry *)realloc(srid_map, sizeof(mapEntry) * (++count + 1));
if (srid_map == NULL)
{
(void)fclose(mapFile);
free(srid_map);
srid_map = NULL;
oracleError_i(FDW_ERROR, "failed to allocate %d bytes of memory", sizeof(mapEntry) * (count + 1));
}
srid_map[count - 1].from = (unsigned)from;
srid_map[count - 1].to = (unsigned)to;
srid_map[count].from = 0;
} while (c != EOF);
/* check for errors */
save_errno = errno;
(void)fclose(mapFile);
errno = save_errno;
if (errno)
{
free(srid_map);
srid_map = NULL;
oracleError(FDW_ERROR, "error reading from file \"" SRID_MAP_FILE "\": %m");
}
}
unsigned
epsgFromOracle(unsigned srid)
{
mapEntry *entry;
if (srid_map == NULL)
initSRIDMap();
for (entry = srid_map; mapEntryValid(entry); ++entry)
if (entry->from == srid)
return entry->to;
return srid;
}
unsigned
epsgToOracle(unsigned srid)
{
mapEntry *entry;
if (srid_map == NULL)
initSRIDMap();
for (entry = srid_map; mapEntryValid(entry); ++entry)
if (entry->to == srid)
return entry->from;
return srid;
}
/*
* oracleEWKBToGeom
* Creates an Oracle SDO_GEOMETRY from a PostGIS EWKB.
* The result is a partially palloc'ed structure.
* Zero length or a NULL pointer for ewkb_data yield an atomically NULL object.
*/
ora_geometry *
oracleEWKBToGeom(oracleSession *session, unsigned ewkb_length, char *ewkb_data)
{
const char *data = ewkb_data;
unsigned type;
ora_geometry *geom = (ora_geometry *)oracleAlloc(sizeof(ora_geometry));
oracleGeometryAlloc(session, geom);
geom->num_elems = -1;
geom->elem = NULL;
geom->num_coords = -1;
geom->coord = NULL;
/* for NULL data, return an object that is atomically NULL */
if (data == NULL || ewkb_length == 0)
return geom;
else
geom->indicator->_atomic = OCI_IND_NOTNULL;
data = setSridAndFlags(session, geom, data);
/*
* We don't move the data pointer after this call because
* it will be moved after the following setTYPE functions
* and those functions expect the data pointer to be on the
* type and not after (see comment above about g_serialized.txt
* and set... functions).
*/
setType(session, geom, data);
type = ewkbType(session, geom);
/* these will be NULL for points */
geom->indicator->sdo_ordinates = (type == POINTTYPE) ? OCI_IND_NULL : OCI_IND_NOTNULL;
geom->indicator->sdo_elem_info = (type == POINTTYPE) ? OCI_IND_NULL : OCI_IND_NOTNULL;
switch (type)
{
case POINTTYPE:
data = setPoint(session, geom, data);
break;
case LINETYPE:
data = setLine(session, geom, data);
break;
case POLYGONTYPE:
data = setPolygon(session, geom, data);
break;
case MULTIPOINTTYPE:
data = setMultiPoint(session, geom, data);
break;
case MULTILINETYPE:
data = setMultiLine(session, geom, data);
break;
case MULTIPOLYGONTYPE:
data = setMultiPolygon(session, geom, data);
break;
default:
oracleError_i(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: unexpected geometry type %u", type);
}
/* check that we reached the end of input data */
if (data - ewkb_data != ewkb_length)
oracleError_ii(FDW_ERROR, "oracle_fdw internal error: number of bytes read %u is different from length %u", data - ewkb_data, ewkb_length);
return geom;
}
/*
* oracleGetEWKBLen
* Returns the length in bytes needed to store an EWKB conversion of "geom".
*/
unsigned
oracleGetEWKBLen(oracleSession *session, ora_geometry *geom)
{
unsigned type;
/* return zero length for atomically NULL objects */
if (geom->indicator->_atomic == OCI_IND_NULL)
return 0;
/* unpack SDO_ELEM_INFO and SDO_ORDINATES */
if (geom->num_elems == -1)
unpack(session, geom);
/* a first check for supported types is done in ewkbType */
type = ewkbType(session, geom);
/*
* If sdo_elem_info is NOT NULL, we go through and check that the
* type and interpretation are actually supported.
*/
if (geom->indicator->sdo_elem_info == OCI_IND_NOTNULL)
{
const unsigned n = geom->num_elems;
unsigned i;
for (i=0; i<n; i+=3){
const unsigned etype = geom->elem[i+1];
const unsigned interpretation = geom->elem[i+2];
if (!((1 == etype && 1 == interpretation)
||(2 == etype && 1 == interpretation)
||(1003 == etype && 1 == interpretation)
||(2003 == etype && 1 == interpretation)
))
oracleError_ii(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: unsupported etype %u with interpretation %u in elem_info", etype, interpretation);
}
}
switch (type)
{
case POINTTYPE:
return ewkbHeaderLen(session, geom) + ewkbPointLen(session, geom);
case LINETYPE:
return ewkbHeaderLen(session, geom) + ewkbLineLen(session, geom);
case POLYGONTYPE:
return ewkbHeaderLen(session, geom) + ewkbPolygonLen(session, geom);
case MULTIPOINTTYPE:
return ewkbHeaderLen(session, geom) + ewkbMultiPointLen(session, geom);
case MULTILINETYPE:
return ewkbHeaderLen(session, geom) + ewkbMultiLineLen(session, geom);
case MULTIPOLYGONTYPE:
return ewkbHeaderLen(session, geom) + ewkbMultiPolygonLen(session, geom);
default:
oracleError_i(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: unexpected geometry type %u", type);
return 0; /* unreachable, but keeps compiler happy */
}
}
/*
* oracleFillEWKB
* Converts "geom" to an EWKB and stores the result in "dest".
*/
char *
oracleFillEWKB(oracleSession *session, ora_geometry *geom, unsigned size, char *dest)
{
const char *orig = dest;
unsigned type;
dest = ewkbHeaderFill(session, geom, dest);
switch ((type = ewkbType(session, geom)))
{
case POINTTYPE:
dest = ewkbPointFill(session, geom, dest);
break;
case LINETYPE:
dest = ewkbLineFill(session, geom, dest);
break;
case POLYGONTYPE:
dest = ewkbPolygonFill(session, geom, dest);
break;
case MULTIPOINTTYPE:
dest = ewkbMultiPointFill(session, geom, dest);
break;
case MULTILINETYPE:
dest = ewkbMultiLineFill(session, geom, dest);
break;
case MULTIPOLYGONTYPE:
dest = ewkbMultiPolygonFill(session, geom, dest);
break;
default:
oracleError_i(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: unexpected geometry type %u", type);
}
/* check that we have reached the end of the input buffer */
if (dest - orig != size)
oracleError_ii(FDW_ERROR, "oracle_fdw internal error: number of bytes written %u is different from size %u", dest - orig, size);
/* free memory in unpacked geometry */
if (geom->num_elems != -1)
freeUnpacked(session, geom);
return dest;
}
/*
* oracleGeometryFree
* Free the memory allocated with a geometry object.
*/
void
oracleGeometryFree(oracleSession *session, ora_geometry *geom)
{
/*
* From the OCI documentation:
* If there is a top-level object (as with a non-atomically NULL object),
* then the indicator is freed when the top-level object is freed with OCIObjectFree().
* If the object is atomically null, then there is no top-level object,
* so the indicator must be freed separately.
*/
if (geom->geometry != NULL && geom->indicator->_atomic == OCI_IND_NOTNULL)
(void)OCIObjectFree(session->envp->envhp, session->envp->errhp, geom->geometry, 0);
else
(void)OCIObjectFree(session->envp->envhp, session->envp->errhp, geom->indicator, 0);
geom->geometry = NULL;
geom->indicator = NULL;
}
/*
* oracleGeometryAlloc
* Allocate memory for a geometry object in the Oracle object cache.
* The indicator is set to atomic NULL.
*/
void
oracleGeometryAlloc(oracleSession *session, ora_geometry *geom)
{
/* allocate a SDO_GEOMETRY object */
if (checkerr(
OCIObjectNew(session->envp->envhp,
session->envp->errhp,
session->connp->svchp,
OCI_TYPECODE_OBJECT,
oracleGetGeometryType(session),
(dvoid *)NULL,
OCI_DURATION_TRANS,
TRUE,
(dvoid **)&geom->geometry),
session->envp->errhp) != OCI_SUCCESS)
oracleError_d(FDW_ERROR, "cannot allocate SDO_GEOMETRY object", oraMessage);
/* get the NULL indicator */
if (checkerr(
OCIObjectGetInd(session->envp->envhp,
session->envp->errhp,
geom->geometry,
(void **)&geom->indicator),
session->envp->errhp) != OCI_SUCCESS)
oracleError_d(FDW_ERROR, "cannot get indicator for new SDO_GEOMETRY object", oraMessage);
/* initialize as atomic NULL */
geom->indicator->_atomic = OCI_IND_NULL;
}
void
appendElemInfo(oracleSession *session, ora_geometry *geom, unsigned info)
{
OCINumber n;
uintToNumber(session->envp->errhp, &info, &n);
if (checkerr(
OCICollAppend(session->envp->envhp,
session->envp->errhp,
(CONST dvoid*) &n,
NULL,
geom->geometry->sdo_elem_info),
session->envp->errhp) != OCI_SUCCESS)
oracleError_d(FDW_ERROR, "cannot append to element info collection", oraMessage);
}
void
appendCoord(oracleSession *session, ora_geometry *geom, double coord)
{
OCINumber n;
doubleToNumber(session->envp->errhp, &coord, &n);
if (checkerr(
OCICollAppend(session->envp->envhp,
session->envp->errhp,
(CONST dvoid*) &n,
NULL,
geom->geometry->sdo_ordinates),
session->envp->errhp) != OCI_SUCCESS)
oracleError_d(FDW_ERROR, "cannot append to ordinate collection", oraMessage);
}
unsigned
ewkbType(oracleSession *session, ora_geometry *geom)
{
unsigned gtype = 0;
if (geom->indicator->sdo_gtype == OCI_IND_NULL)
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: geometry type cannot be NULL");
numberToUint(session->envp->errhp, &(geom->geometry->sdo_gtype), >ype);
switch (gtype%100)
{
case 1:
return POINTTYPE;
case 2:
return LINETYPE;
case 3:
return POLYGONTYPE;
case 4:
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: geometry type COLLECTION not supported");
break;
case 5:
return MULTIPOINTTYPE;
case 6:
return MULTILINETYPE;
case 7:
return MULTIPOLYGONTYPE;
case 8:
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: geometry type SOLID not supported");
break;
case 9:
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: geometry type MULTISOLID not supported");
break;
default:
oracleError_i(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: unknown geometry type %u", gtype);
}
return 0; /* unreachable, but keeps compiler happy */
}
const char *
setType(oracleSession *session, ora_geometry *geom, const char *data)
{
const ub4 wkbType = *((ub4 *)data);
unsigned gtype;
numberToUint(session->envp->errhp, &(geom->geometry->sdo_gtype), >ype);
data += sizeof(ub4);
switch (wkbType)
{
case POINTTYPE:
gtype += 1;
break;
case LINETYPE:
gtype += 2;
break;
case POLYGONTYPE:
gtype += 3;
break;
case MULTIPOINTTYPE:
gtype += 5;
break;
case MULTILINETYPE:
gtype += 6;
break;
case MULTIPOLYGONTYPE:
gtype += 7;
break;
#define UNSUPPORTED_TYPE( T ) case T ## TYPE: oracleError(FDW_ERROR, "error converting geometry to SDO_GEOMETRY: geometry type "#T" not supported"); break;
UNSUPPORTED_TYPE(COLLECTION)
UNSUPPORTED_TYPE(CIRCSTRING)
UNSUPPORTED_TYPE(COMPOUND)
UNSUPPORTED_TYPE(CURVEPOLY)
UNSUPPORTED_TYPE(MULTICURVE)
UNSUPPORTED_TYPE(MULTISURFACE)
UNSUPPORTED_TYPE(POLYHEDRALSURFACE)
UNSUPPORTED_TYPE(TRIANGLE)
UNSUPPORTED_TYPE(TIN)
#undef UNSUPPORTED_TYPE
default:
oracleError_i(FDW_ERROR, "error converting geometry to SDO_GEOMETRY: unknown geometry type %u", wkbType);
}
geom->indicator->sdo_gtype = OCI_IND_NOTNULL;
uintToNumber(session->envp->errhp, >ype, &(geom->geometry->sdo_gtype));
return data;
}
/*
* Header contains:
* - srid : 3 bytes
* - flags : 1 byte
*/
unsigned
ewkbHeaderLen(oracleSession *session, ora_geometry *geom)
{
return 4;
}
char *
ewkbHeaderFill(oracleSession *session, ora_geometry *geom, char * dest)
{
const unsigned srid = ewkbSrid(session, geom);
const unsigned sdoDim = sdoDimension(session, geom);
ub1 flags;
ub1 s[3];
if (sdoDim > 3)
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: four-dimensional geometries are unsupported");
if (ewkbIsMeasured(session, geom))
/*
* In Oracle, the measure dimension counts as third dimension,
* so "sdoDim" will be 3. This corresponds to a two-dimensional
* PostGIS geometry with the "measure" flag set.
*/
flags = 0x02;
else
flags = ((3 == sdoDim) ? 0x01 : 0x00 );
s[0] = (srid & 0x001F0000) >> 16;
s[1] = (srid & 0x0000FF00) >> 8;
s[2] = (srid & 0x000000FF);
memcpy(dest, s, 3);
dest += 3;
memcpy(dest, &flags, 1);
dest += 1;
return dest;
}
unsigned
ewkbIsMeasured(oracleSession *session, ora_geometry *geom)
{
unsigned gtype = 0;
if (geom->indicator->sdo_gtype == OCI_IND_NOTNULL)
numberToUint(session->envp->errhp, &(geom->geometry->sdo_gtype), >ype);
switch ((gtype % 1000) / 100)
{
case 0:
return 0;
case 3:
return 1;
default:
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: measure dimension must be \"0\" or \"3\"");
return 0; /* keep the compiler happy */
}
}
/*
* sdoDimension
* Returns the dimension as Oracle understands it.
* For a measured geometry with two dimensions and one measure dimension,
* it will return 3 (PostGIS considers such an object two-dimensional).
*/
unsigned
sdoDimension(oracleSession *session, ora_geometry *geom)
{
unsigned gtype = 0;
if (geom->indicator->sdo_gtype == OCI_IND_NOTNULL)
numberToUint(session->envp->errhp, &(geom->geometry->sdo_gtype), >ype);
return gtype / 1000;
}
unsigned
ewkbSrid(oracleSession *session, ora_geometry *geom)
{
unsigned srid = 0;
if (geom->indicator->sdo_srid == OCI_IND_NOTNULL)
numberToUint(session->envp->errhp, &(geom->geometry->sdo_srid), &srid);
/* convert Oracle->PostGIS SRID when needed */
return epsgFromOracle(srid);
}
const char *
setSridAndFlags(oracleSession *session, ora_geometry *geom, const char *data)
{
unsigned srid = 0;
unsigned gtype = 0;
srid |= ((ub1)data[0]) << 16;
srid |= ((ub1)data[1]) << 8;
srid |= ((ub1)data[2]);
/*
* Only the first 21 bits are set. Slide up and back to pull
* the negative bits down, if we need them.
*/
srid = (srid<<11)>>11;
data += 3;
/* convert PostGIS->Oracle SRID when needed */
srid = epsgToOracle(srid);
geom->indicator->sdo_srid = (srid == 0) ? OCI_IND_NULL : OCI_IND_NOTNULL;
if (geom->indicator->sdo_srid == OCI_IND_NOTNULL)
uintToNumber(session->envp->errhp, &srid, &(geom->geometry->sdo_srid));
gtype = (((ub1)data[0]) & 0x01 ) ? 3000 : 2000; /* 3d/2d */
if (data[0] & 0x02)
{
/*
* What PostGIS sees as a two-dimensional geometry with measure
* is a three-dimensional geometry in Oracle, and the measure
* dimension is the third dimension.
*/
gtype += 1300;
if (gtype >= 4000)
oracleError(FDW_ERROR, "error converting geometry to SDO_GEOMETRY: measure dimension only supported for two-dimensional geometries");
}
if (data[0] & 0x08)
oracleError(FDW_ERROR, "error converting geometry to SDO_GEOMETRY: geodetic not supported");
if (((ub1)data[0]) & 0x04) /* has bbox, offsets */
{
data += 1 + 2*(gtype / 1000)*sizeof(float);
}
else
{
data += 1;
}
geom->indicator->sdo_gtype = OCI_IND_NOTNULL;
uintToNumber(session->envp->errhp, >ype, &(geom->geometry->sdo_gtype));
return data;
}
unsigned
ewkbPointLen(oracleSession *session, ora_geometry *geom)
{
return 2*sizeof(unsigned) + sizeof(double)*sdoDimension(session, geom);
}
char *
ewkbPointFill(oracleSession *session, ora_geometry *geom, char *dest)
{
unsigned dim = sdoDimension(session, geom);
dest = unsignedFill(POINTTYPE, dest);
dest = unsignedFill(1, dest);
if (geom->indicator->sdo_point._atomic != OCI_IND_NOTNULL)
{
/* the point must be stored in SDO_ORDINATES */
unsigned offset, etype, interpretation;
double *doubleDest = (double *)dest;
if (geom->indicator->sdo_elem_info != OCI_IND_NOTNULL)
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: SDO_POINT and SDO_ELEM_INFO cannot both be NULL for a point");
if (geom->indicator->sdo_ordinates != OCI_IND_NOTNULL)
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: SDO_POINT and SDO_ORDINATES cannot both be NULL for a point");
if (geom->num_elems < 3)
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: not enough values in SDO_ELEM_INFO");
offset = geom->elem[0];
etype = geom->elem[1];
interpretation = geom->elem[2];
if (etype != 1)
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: point cannot have ETYPE different from 1");
if (interpretation != 1)
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: point with SDO_INTERPRETATION different from 1 is not supported");
*doubleDest = geom->coord[offset - 1];
*(++doubleDest) = geom->coord[offset];
dest += 2 * sizeof(double);
if (dim == 3) {
*(++doubleDest) = geom->coord[offset + 1];
dest += sizeof(double);
}
}
else
{
/* the point must be stored in SDO_POINT */
if (geom->indicator->sdo_point.x != OCI_IND_NOTNULL
|| geom->indicator->sdo_point.y != OCI_IND_NOTNULL
|| (dim == 3
&& geom->indicator->sdo_point.z != OCI_IND_NOTNULL))
{
oracleError(FDW_ERROR, "error converting SDO_GEOMETRY to geometry: null point coordinates not supported");
}
numberToDouble(session->envp->errhp, &(geom->geometry->sdo_point.x), dest);
dest += sizeof(double);
numberToDouble(session->envp->errhp, &(geom->geometry->sdo_point.y), dest);
dest += sizeof(double);
if (3 == dim)
{
numberToDouble(session->envp->errhp, &(geom->geometry->sdo_point.z), dest);
dest += sizeof(double);
}
}
return dest;
}
const char *
setPoint(oracleSession *session, ora_geometry *geom, const char *data)
{
if (*((unsigned *)data) != POINTTYPE)
oracleError_i(FDW_ERROR, "error converting geometry to SDO_GEOMETRY: expected point, got type %u", *((unsigned *)data));
data += sizeof(unsigned);
if (*((unsigned *)data ) != 1)
oracleError(FDW_ERROR, "error converting geometry to SDO_GEOMETRY: empty point is not supported");
data += sizeof(unsigned);
geom->indicator->sdo_point._atomic = OCI_IND_NOTNULL;
geom->indicator->sdo_point.x = OCI_IND_NOTNULL;
doubleToNumber(session->envp->errhp, data, &(geom->geometry->sdo_point.x));
data += sizeof(double);
geom->indicator->sdo_point.y = OCI_IND_NOTNULL;
doubleToNumber(session->envp->errhp, data, &(geom->geometry->sdo_point.y));
data += sizeof(double);
if (3 == sdoDimension(session, geom))
{
geom->indicator->sdo_point.z = OCI_IND_NOTNULL;
doubleToNumber(session->envp->errhp, data, &(geom->geometry->sdo_point.z));
data += sizeof(double);
}
return data;
}
unsigned
ewkbLineLen(oracleSession *session, ora_geometry *geom)
{
return 2 * sizeof(unsigned) + sizeof(double) * geom->num_coords;
}
char *
ewkbLineFill(oracleSession *session, ora_geometry *geom, char * dest)
{
unsigned i;
const unsigned numC = geom->num_coords;
const unsigned numPoints = numC / sdoDimension(session, geom);
dest = unsignedFill(LINETYPE, dest);
dest = unsignedFill(numPoints, dest);
for (i=0; i<numC; i++) dest = doubleFill(geom->coord[i], dest);
return dest;
}
const char *
setLine(oracleSession *session, ora_geometry *geom, const char *data)
{
unsigned i, n;
if (*((unsigned *)data) != LINETYPE)
oracleError_i(FDW_ERROR, "error converting geometry to SDO_GEOMETRY: expected line, got type %u", *((unsigned *)data));
data += sizeof(unsigned);
n = *((unsigned *)data) * sdoDimension(session, geom);
data += sizeof(unsigned);
if (!n)
oracleError(FDW_ERROR, "error converting geometry to SDO_GEOMETRY: empty line is not supported");
appendElemInfo(session, geom, numCoord(session, geom) + 1); /* start index + 1 */
appendElemInfo(session, geom, 2); /* SDO_ETYPE linestring */
appendElemInfo(session, geom, 1); /* SDO_INTERPRETATION straight line segments */
for (i=0; i<n; i++)
{
appendCoord(session, geom, *((double *)data));
data += sizeof(double);
}
return data;
}