-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_twkb.c
334 lines (258 loc) · 7.79 KB
/
pg_twkb.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
/**********************************************************************
*
* pg_twkb - Spatial Types for PostgreSQL
*
* Copyright (C) 2016 Nicklas Avén
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU General Public Licence. See the LICENCE file.
*
**********************************************************************/
#include <pg_twkb.h>
#include <math.h>
#include "pg_twkb.h"
#include "lwout_twkb.h"
#include "lwin_twkb.h"
#include "utils/builtins.h"
#include "executor/spi.h"
#include <sqlite3.h>
/*
* This is required for builds against pgsql
*/
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(TWKBFromTWKBArray);
Datum TWKBFromTWKBArray(PG_FUNCTION_ARGS)
{
ArrayType *arr_twkbs = NULL;
ArrayType *arr_ids = NULL;
int num_twkbs, num_ids, i = 0;
ArrayIterator iter_twkbs, iter_ids;
bool null_twkb, null_id;
Datum val_twkb, val_id;
uint8_t **col = NULL;
size_t *sizes = NULL;
int64_t *idlist = NULL;
uint8_t *twkb;
size_t twkb_size;
size_t out_size;
bytea *result;
bytea *bytea_twkb;
/* The first two arguments are required */
if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
PG_RETURN_NULL();
arr_twkbs = PG_GETARG_ARRAYTYPE_P(0);
arr_ids = PG_GETARG_ARRAYTYPE_P(1);
num_twkbs = ArrayGetNItems(ARR_NDIM(arr_twkbs), ARR_DIMS(arr_twkbs));
num_ids = ArrayGetNItems(ARR_NDIM(arr_ids), ARR_DIMS(arr_ids));
if ( num_twkbs != num_ids )
{
elog(ERROR, "size of geometry[] and integer[] arrays must match");
PG_RETURN_NULL();
}
/* Loop through array and build a collection of geometry and */
/* a simple array of ids. If either side is NULL, skip it */
#if POSTGIS_PGSQL_VERSION >= 95
iter_twkbs = array_create_iterator(arr_twkbs, 0, NULL);
iter_ids = array_create_iterator(arr_ids, 0, NULL);
#else
iter_twkbs = array_create_iterator(arr_twkbs, 0);
iter_ids = array_create_iterator(arr_ids, 0);
#endif
/* Construct collection/idlist first time through */
col = palloc0(num_twkbs * sizeof(void*));
sizes = palloc0(num_twkbs * sizeof(size_t));
idlist = palloc0(num_twkbs * sizeof(int64_t));
while( array_iterate(iter_twkbs, &val_twkb, &null_twkb) &&
array_iterate(iter_ids, &val_id, &null_id) )
{
int32_t uid;
if ( null_twkb || null_id )
{
elog(NOTICE, "ST_CollectTWKB skipping NULL entry at position %d", i);
continue;
}
bytea_twkb =(bytea*) DatumGetPointer(val_twkb);
uid = DatumGetInt64(val_id);
twkb_size=VARSIZE_ANY_EXHDR(bytea_twkb);
/* Store the values */
col[i] = (uint8_t*)VARDATA(bytea_twkb);
sizes[i] = twkb_size;
idlist[i] = uid;
i++;
}
array_free_iterator(iter_twkbs);
array_free_iterator(iter_ids);
if(i==0)
{
elog(NOTICE, "No valid geometry - id pairs found");
PG_FREE_IF_COPY(arr_twkbs, 0);
PG_FREE_IF_COPY(arr_ids, 1);
PG_RETURN_NULL();
}
/* Write out the TWKB */
twkb = twkb_to_twkbcoll(col, sizes,&out_size, idlist, i);
/* Convert to a bytea return type */
result = palloc(out_size + VARHDRSZ);
memcpy(VARDATA(result), twkb,out_size);
SET_VARSIZE(result, out_size + VARHDRSZ);
/* Clean up */
//~ pfree(twkb);
pfree(idlist);
PG_FREE_IF_COPY(arr_twkbs, 0);
PG_FREE_IF_COPY(arr_ids, 1);
PG_RETURN_BYTEA_P(result);
}
PG_FUNCTION_INFO_V1(TWKB2file);
Datum TWKB2file(PG_FUNCTION_ARGS)
{
text *filename;
bytea *bytea_twkb;
uint8_t *twkb;
FILE * file_p;
if( PG_NARGS() < 1 || PG_ARGISNULL(0))
{
lwnotice("No buffer to write");
PG_RETURN_NULL();
}
bytea_twkb = (bytea*)PG_GETARG_BYTEA_P(0);
if ( PG_NARGS() < 2 || PG_ARGISNULL(1) )
{
lwerror("No filename to write to");
PG_RETURN_NULL();
}
filename = PG_GETARG_TEXT_P(1);
twkb = (uint8_t*)VARDATA(bytea_twkb);
file_p = fopen(text_to_cstring(filename), "ab");
if (file_p == NULL)
lwerror("Couldn't open the file for writing");
fwrite(twkb, VARSIZE(bytea_twkb)-VARHDRSZ, 1, file_p);
fclose(file_p);
PG_FREE_IF_COPY(bytea_twkb, 0);
PG_RETURN_INT32(1);
}
PG_FUNCTION_INFO_V1(TWKB_Write2SQLite);
Datum TWKB_Write2SQLite(PG_FUNCTION_ARGS)
{
char *sqlitedb_name,*dataset_name, *sql_string, *twkb_name, *id_name,*idx_tbl, *idx_geom, *idx_id;
int create;
/*Name of sqlite-database to write to*/
if ( PG_NARGS() < 1 || PG_ARGISNULL(0) )
{
lwerror("No sqlitedb to write to");
PG_RETURN_NULL();
}
sqlitedb_name = text_to_cstring(PG_GETARG_TEXT_P(0));
/*Name of dataset in sqlite*/
if ( PG_NARGS() < 2 || PG_ARGISNULL(1) )
{
lwerror("No name of dataset");
PG_RETURN_NULL();
}
dataset_name = text_to_cstring(PG_GETARG_TEXT_P(1));
/*SQL-query to fetch data*/
if( PG_NARGS() < 3 || PG_ARGISNULL(2))
{
lwnotice("No sql query to use");
PG_RETURN_NULL();
}
sql_string = text_to_cstring(PG_GETARG_TEXT_P(2));
/*Name of twkb-column in sql-query above*/
if( PG_NARGS() < 4|| PG_ARGISNULL(3))
{
lwerror("No twkb-column name");
PG_RETURN_NULL();
}
twkb_name = text_to_cstring(PG_GETARG_TEXT_P(3));
/*Name of id-column in sql-query above*/
if( PG_NARGS() < 5 || PG_ARGISNULL(4))
{
id_name = "";
}
id_name = text_to_cstring(PG_GETARG_TEXT_P(4));
/*Name of table to create spatial index from with corresponding id*/
if( PG_NARGS() < 6|| PG_ARGISNULL(5))
{
idx_tbl = "";
}
idx_tbl = text_to_cstring(PG_GETARG_TEXT_P(5));
/*Geometry column to create spatial index from*/
if( PG_NARGS() < 7|| PG_ARGISNULL(6))
{
idx_geom = "";
}
idx_geom = text_to_cstring(PG_GETARG_TEXT_P(6));
/*id in spatial index that points to right twkb*/
if( PG_NARGS() < 8|| PG_ARGISNULL(7))
{
idx_id = "";
}
idx_id = text_to_cstring(PG_GETARG_TEXT_P(7));
/*if the table shall be created*/
if( PG_NARGS() < 9|| PG_ARGISNULL(8))
{
create = 1;
}
create = PG_GETARG_INT32(8);
// PG_FREE_IF_COPY(bytea_twkb, 0);
write2sqlite(sqlitedb_name,dataset_name, sql_string, twkb_name,id_name,idx_geom,idx_tbl, idx_id, create);
pfree(sqlitedb_name);
pfree(sql_string);
pfree(dataset_name);
pfree(twkb_name);
pfree(id_name);
pfree(idx_tbl);
pfree(idx_geom);
pfree(idx_id);
PG_RETURN_INT32(1);
}
PG_FUNCTION_INFO_V1(text2file);
Datum text2file(PG_FUNCTION_ARGS)
{
text *filename;
text *the_text;
char *text_data;
FILE * file_p;
if( PG_NARGS() < 1 || PG_ARGISNULL(0))
{
lwnotice("No text to write");
PG_RETURN_NULL();
}
the_text = PG_GETARG_TEXT_P(0);
if ( PG_NARGS() < 2 || PG_ARGISNULL(1) )
{
lwerror("No filename to write to");
PG_RETURN_NULL();
}
filename = PG_GETARG_TEXT_P(1);
text_data = (char*)VARDATA(the_text);
file_p = fopen(text_to_cstring(filename), "w");
if (file_p == NULL)
lwerror("Couldn't open the file for writing");
fwrite(text_data, VARSIZE(the_text)-VARHDRSZ, 1, file_p);
fclose(file_p);
PG_FREE_IF_COPY(the_text, 0);
PG_RETURN_INT32(1);
}
PG_FUNCTION_INFO_V1(get_tileid);
Datum get_tileid(PG_FUNCTION_ARGS)
{
int x, y,i, res=0;
if( PG_NARGS() < 1 || PG_ARGISNULL(0))
{
lwnotice("No text to write");
PG_RETURN_NULL();
}
x = PG_GETARG_INT32(0);
if ( PG_NARGS() < 2 || PG_ARGISNULL(1) )
{
lwerror("No filename to write to");
PG_RETURN_NULL();
}
y = PG_GETARG_INT32(1);
for (i=15; i>=0; i--)
{
res=res | ((1&(x>>i))<<2*i);
res=res | ((1&(y>>i))<<(2*i+1));
}
PG_RETURN_INT32(res);
}