-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsqlite_gis.c
279 lines (252 loc) · 8.06 KB
/
sqlite_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
/*-------------------------------------------------------------------------
*
* SQLite Foreign Data Wrapper for PostgreSQL
*
* Portions Copyright (c) 2018, TOSHIBA CORPORATION
*
* IDENTIFICATION
* sqlite_gis.c
*
* Routines that convert between SpatiaLite BLOB storage form and PostGIS EWKB
* and constants for detecting data type names from PostGIS set
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "sqlite_fdw.h"
#ifdef SQLITE_FDW_GIS_ENABLE
#include <spatialite.h>
#endif
#include <sqlite3.h>
#include "nodes/makefuncs.h"
#include "parser/parse_type.h"
#include "utils/lsyscache.h"
#ifdef SQLITE_FDW_GIS_ENABLE
static void
common_EWKB_error (Form_pg_attribute att, int len, const char* data, bool direction_to_pg);
static char*
getHexFormOfBlob(blobOutput b);
#endif
/*
* Byte length of PostGIS/GEOS copyed image
* of geoobject initial data for testing or
* extracting SRID data
*/
#define EWKB_SRID_TEST_PREFIX_LEN 12
/*
* This is data types from PostGIS 3.4. For a newer version use the following query
* select pt.typname, pt."oid", pd.description,
* pt.typinput, pt.typoutput
* from pg_catalog.pg_type pt
* inner join pg_catalog.pg_description pd
* on pt."oid" = pd.objoid
* where pd.description like 'postgis %'
*/
const char *postGisSpecificTypes[] =
{
"addbandarg", "box2d", "box3d", "geometry_dump", "geomval",
"getfaceedges_returntype", "rastbandarg", "raster", "reclassarg",
"summarystats", "topoelement", "topoelementarray", "topogeometry",
"unionarg", "validatetopology_returntype", NULL
};
const char *postGisSQLiteCompatibleTypes[] = { "geometry", "geography", NULL };
#ifdef SQLITE_FDW_GIS_ENABLE
/*
* SpatiaLiteAsPostGISgeom
* Converts SpatiaLite BLOB to hex value string for PostGIS/GEOS input function
*/
char *
SpatiaLiteAsPostGISgeom (blobOutput spatiaLiteBlob, Form_pg_attribute att)
{
gaiaOutBuffer out_buf;
gaiaGeomCollPtr geo = NULL;
int gpkg_amphibious = 0;
int gpkg_mode = 0;
int res_len = 0;
char *res = NULL;
geo = gaiaFromSpatiaLiteBlobWkbEx ((unsigned char *)spatiaLiteBlob.dat,
spatiaLiteBlob.len,
gpkg_mode,
gpkg_amphibious);
if (geo == NULL)
{
common_EWKB_error (att,
spatiaLiteBlob.len,
getHexFormOfBlob(spatiaLiteBlob),
true);
}
else
{
gaiaOutBufferInitialize (&out_buf);
gaiaToEWKB (&out_buf, geo);
gaiaFreeGeomColl (geo);
if (out_buf.Error || out_buf.Buffer == NULL)
{
gaiaOutBufferReset (&out_buf);
common_EWKB_error (att,
spatiaLiteBlob.len,
getHexFormOfBlob(spatiaLiteBlob),
true);
}
else
{
res_len = out_buf.WriteOffset + 1; /* Include \0 on the end */
res = (char*) palloc(res_len*sizeof(char));
strcpy(res, out_buf.Buffer);
gaiaOutBufferReset (&out_buf);
return res;
}
}
return NULL;
}
/*
* getHexFormOfBlob
* Return normal ASCII hex string for a bytes of input data BLOB
*/
static char*
getHexFormOfBlob(blobOutput b)
{
const char *bstr = b.dat;
char* hstr = (char*)palloc(b.len * 2 + 1);
unsigned char *phstr = (unsigned char *)hstr;
for (int i = 0; i < b.len; i++)
{
if (bstr[i] == -128)
{
*phstr++ = '0';
*phstr++ = '0';
} else {
*phstr++ = hex_dig[((bstr[i] >> 4) & 0x0F)];
*phstr++ = hex_dig[((bstr[i]) & 0x0F)];
}
}
*phstr++ = '\0';
return hstr;
}
/*
* hasSRID
* Returns true if there is any SRID data in hex input of EWKB
* in other cases, including damaged input data, returns false
*/
static inline bool hasSRID (char *hexEWKB)
{
unsigned char *blob = NULL;
int blob_size;
const int wkbSRID = 0x20000000; /* See PostGIS file doc/ZMSgeoms.txt */
int endian;
int endian_arch = gaiaEndianArch ();
int srid;
char hexPrefix[EWKB_SRID_TEST_PREFIX_LEN + 1]; /* also reserved for \0 */
int i;
/* Copy only some initial hex byte images to get SRID flag and SRID value */
for (i = 0; i < EWKB_SRID_TEST_PREFIX_LEN && hexEWKB[i] != '\0'; i++) {
hexPrefix[i] = hexEWKB[i];
}
hexPrefix[i] = '\0'; /* Null-terminate the substring */
blob = gaiaParseHexEWKB ((const unsigned char *)hexPrefix, &blob_size);
if (blob == NULL)
return false;
endian = (*(blob + 0) == 0x01);
/* wkbSRID flag bytes 1-5. PostGIS doc/ZMSgeoms.txt */
srid = gaiaImport32 (blob + 1, endian, endian_arch);
free(blob);
return (srid & wkbSRID) == wkbSRID;
}
/*
* EWKB2SpatiaLiteBlobImage
* Uses char* and len struncture for SQLite BLOB binding from
* input hex string with possible EWKB presentation
*/
static inline blobOutput
EWKB2SpatiaLiteBlobImage (char *hexEWKB, Form_pg_attribute att)
{
gaiaGeomCollPtr geo = NULL;
int gpkg_mode = 0;
int tiny_point = 0;
unsigned char *spatialite_blob = NULL;
int len = strlen(hexEWKB);
char *src = NULL;
int shift = 0;
/* Ignore leading '\x' in hex data */
if (hexEWKB[0] == '\\' && hexEWKB[1] == 'x')
shift = 2;
src = hexEWKB + shift;
if (!hasSRID(src))
{
if (att != NULL)
{
char *pg_dataTypeName = TypeNameToString(makeTypeNameFromOid(att->atttypid, att->atttypmod));
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("SpatiaLite doesn't accept GIS data without SRID"),
errhint("In column \"%.*s\" with data type \"%s\" there is incorrect value in %d bytes", (int)sizeof(att->attname.data), att->attname.data, pg_dataTypeName, len),
errcontext("Hex data: %s", hexEWKB)));
}
else
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("SpatiaLite doesn't accept GIS data without SRID"),
errhint("Not deparsable value for SpatiaLite in %d bytes", len),
errcontext("Hex data: %s", hexEWKB)));
pfree(hexEWKB);
return (struct blobOutput){NULL, 0};
}
geo = gaiaFromEWKB ((const unsigned char *)src);
if (NULL == geo)
{
common_EWKB_error(att, len/2, hexEWKB, false);
}
elog(DEBUG4, "sqlite_fdw : PostGIS -> SpatiaLite %s", hexEWKB);
gaiaToSpatiaLiteBlobWkbEx2 (geo, &spatialite_blob, &len, gpkg_mode, tiny_point);
gaiaFreeGeomColl (geo);
pfree(hexEWKB);
return (struct blobOutput){(char *)spatialite_blob, len};
}
/*
* PostGISgeomAsSpatiaLite
* Converts PostGIS/GEOS BLOB as Datum + attribute metadata to SpatiaLite BLOB
*/
blobOutput
PostGISgeomAsSpatiaLite (Datum d, Form_pg_attribute att)
{
char *pgHexOutput = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
getTypeOutputInfo(att->atttypid, &outputFunctionId, &typeVarLength);
pgHexOutput = OidOutputFunctionCall(outputFunctionId, d);
return EWKB2SpatiaLiteBlobImage (pgHexOutput, att);
}
/*
* common_EWKB_error
* Message about error inside of both PostGIS/GEOS<->SpatiaLite transformations
*/
static void
common_EWKB_error (Form_pg_attribute att, int len, const char* data, bool direction_to_pg)
{
Oid pgtyp = att->atttypid;
int32 pgtypmod = att->atttypmod;
NameData pgColND = att->attname;
char *pg_dataTypeName = TypeNameToString(makeTypeNameFromOid(pgtyp, pgtypmod));
if (direction_to_pg)
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("GIS data transformation error SpatiaLite->GEOS/PostGIS"),
errhint("In column \"%.*s\" with data type \"%s\" there is incorrect value in %d bytes", (int)sizeof(pgColND.data), pgColND.data, pg_dataTypeName, len),
errcontext("Hex data: %s", data)));
else
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("GIS data transformation error GEOS/PostGIS->SpatiaLite"),
((att != NULL) ? (errhint("In column \"%.*s\" with data type \"%s\" there is incorrect value in %d bytes", (int)sizeof(pgColND.data), pgColND.data, pg_dataTypeName, len)): (errhint("Not deparsable value for SpatiaLite in %d bytes", len))),
errcontext("Hex data: %s", data)));
}
/*
* sqlite_deparse_PostGIS_value
* Transform PostGIS/GEOS value from extval to SpatiaLite hex constant
* for a SQLite query. SQLite constant format is EWKB.
*/
void
sqlite_deparse_PostGIS_value(StringInfo buf, char *extval)
{
blobOutput bO = EWKB2SpatiaLiteBlobImage (extval, NULL);
char* hexform = getHexFormOfBlob(bO);
appendStringInfo(buf, "X\'%s\'", hexform);
elog(DEBUG4, "sqlite_fdw : SpatiaLiteData %s", hexform);
pfree(hexform);
}
#endif