forked from postgis/postgis
/
lwgeom_out_mvt.c
268 lines (235 loc) · 7.37 KB
/
lwgeom_out_mvt.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
/**********************************************************************
*
* 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) 2016-2017 Björn Harrtell <bjorn@wololo.org>
*
**********************************************************************/
#include "postgres.h"
#include "utils/builtins.h"
#include "executor/spi.h"
#include "../postgis_config.h"
#include "lwgeom_pg.h"
#include "lwgeom_log.h"
#include "liblwgeom.h"
#include "mvt.h"
#ifdef HAVE_LIBPROTOBUF
#include "vector_tile.pb-c.h"
#endif /* HAVE_LIBPROTOBUF */
/**
* Process input parameters to mvt_geom and returned serialized geometry
*/
PG_FUNCTION_INFO_V1(ST_AsMVTGeom);
Datum ST_AsMVTGeom(PG_FUNCTION_ARGS)
{
#ifndef HAVE_LIBPROTOBUF
elog(ERROR, "Missing libprotobuf-c");
PG_RETURN_NULL();
#else
GBOX *bounds = NULL;
int32_t extent = 0;
int32_t buffer = 0;
bool clip_geom = true;
GSERIALIZED *geom_in, *geom_out;
LWGEOM *lwgeom_in, *lwgeom_out;
uint8_t type = 0;
if (PG_ARGISNULL(0))
{
PG_RETURN_NULL();
}
if (PG_ARGISNULL(1))
{
elog(ERROR, "%s: parameter bounds cannot be null", __func__);
PG_RETURN_NULL();
}
bounds = (GBOX *)PG_GETARG_POINTER(1);
if (bounds->xmax - bounds->xmin <= 0 || bounds->ymax - bounds->ymin <= 0)
{
elog(ERROR, "%s: bounds width or height cannot be 0", __func__);
PG_RETURN_NULL();
}
extent = PG_ARGISNULL(2) ? 4096 : PG_GETARG_INT32(2);
if (extent <= 0)
{
elog(ERROR, "%s: extent cannot be 0", __func__);
PG_RETURN_NULL();
}
buffer = PG_ARGISNULL(3) ? 256 : PG_GETARG_INT32(3);
clip_geom = PG_ARGISNULL(4) ? true : PG_GETARG_BOOL(4);
geom_in = PG_GETARG_GSERIALIZED_P_COPY(0);
type = gserialized_get_type(geom_in);
/* If possible, peak into the bounding box before deserializating it to discard small geometries
* We don't check COLLECTIONTYPE since that might be a collection of points */
if (type == LINETYPE || type == POLYGONTYPE || type == MULTILINETYPE || type == MULTIPOLYGONTYPE)
{
GBOX gserialized_box;
/* We only apply the optimization if the bounding box is available */
if (gserialized_read_gbox_p(geom_in, &gserialized_box) == LW_SUCCESS)
{
/* Shortcut to drop geometries smaller than the resolution */
double geom_width = gserialized_box.xmax - gserialized_box.xmin;
double geom_height = gserialized_box.ymax - gserialized_box.ymin;
double geom_area = geom_width * geom_height;
double bounds_width = (bounds->xmax - bounds->xmin) / extent;
double bounds_height = (bounds->ymax - bounds->ymin) / extent;
/* We use half the area of the grid square as minimun resolution */
double min_resolution_area = bounds_width * bounds_height / 2.0;
if (geom_area < min_resolution_area)
{
PG_RETURN_NULL();
}
}
}
lwgeom_in = lwgeom_from_gserialized(geom_in);
lwgeom_out = mvt_geom(lwgeom_in, bounds, extent, buffer, clip_geom);
if (lwgeom_out == NULL)
PG_RETURN_NULL();
geom_out = geometry_serialize(lwgeom_out);
lwgeom_free(lwgeom_out);
PG_FREE_IF_COPY(geom_in, 0);
PG_RETURN_POINTER(geom_out);
#endif
}
/**
* Process input parameters and row data into state
*/
PG_FUNCTION_INFO_V1(pgis_asmvt_transfn);
Datum pgis_asmvt_transfn(PG_FUNCTION_ARGS)
{
#ifndef HAVE_LIBPROTOBUF
elog(ERROR, "Missing libprotobuf-c");
PG_RETURN_NULL();
#else
MemoryContext aggcontext;
mvt_agg_context *ctx;
if (!AggCheckCallContext(fcinfo, &aggcontext))
elog(ERROR, "%s called in non-aggregate context", __func__);
MemoryContextSwitchTo(aggcontext);
if (PG_ARGISNULL(0)) {
ctx = palloc(sizeof(*ctx));
ctx->name = "default";
if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
ctx->name = text_to_cstring(PG_GETARG_TEXT_P(2));
ctx->extent = 4096;
if (PG_NARGS() > 3 && !PG_ARGISNULL(3))
ctx->extent = PG_GETARG_INT32(3);
ctx->geom_name = NULL;
if (PG_NARGS() > 4 && !PG_ARGISNULL(4))
ctx->geom_name = text_to_cstring(PG_GETARG_TEXT_P(4));
if (PG_NARGS() > 5 && !PG_ARGISNULL(5))
ctx->id_name = text_to_cstring(PG_GETARG_TEXT_P(5));
else
ctx->id_name = NULL;
mvt_agg_init_context(ctx);
} else {
ctx = (mvt_agg_context *) PG_GETARG_POINTER(0);
}
if (!type_is_rowtype(get_fn_expr_argtype(fcinfo->flinfo, 1)))
elog(ERROR, "%s: parameter row cannot be other than a rowtype", __func__);
ctx->row = PG_GETARG_HEAPTUPLEHEADER(1);
mvt_agg_transfn(ctx);
PG_FREE_IF_COPY(ctx->row, 1);
PG_RETURN_POINTER(ctx);
#endif
}
/**
* Encode final state to Mapbox Vector Tile
*/
PG_FUNCTION_INFO_V1(pgis_asmvt_finalfn);
Datum pgis_asmvt_finalfn(PG_FUNCTION_ARGS)
{
#ifndef HAVE_LIBPROTOBUF
elog(ERROR, "Missing libprotobuf-c");
PG_RETURN_NULL();
#else
mvt_agg_context *ctx;
bytea *buf;
elog(DEBUG2, "%s called", __func__);
if (!AggCheckCallContext(fcinfo, NULL))
elog(ERROR, "%s called in non-aggregate context", __func__);
if (PG_ARGISNULL(0))
{
bytea *emptybuf = palloc(VARHDRSZ);
SET_VARSIZE(emptybuf, VARHDRSZ);
PG_RETURN_BYTEA_P(emptybuf);
}
ctx = (mvt_agg_context *) PG_GETARG_POINTER(0);
buf = mvt_agg_finalfn(ctx);
PG_RETURN_BYTEA_P(buf);
#endif
}
PG_FUNCTION_INFO_V1(pgis_asmvt_serialfn);
Datum pgis_asmvt_serialfn(PG_FUNCTION_ARGS)
{
#ifndef HAVE_LIBPROTOBUF
elog(ERROR, "Missing libprotobuf-c");
PG_RETURN_NULL();
#else
mvt_agg_context *ctx;
elog(DEBUG2, "%s called", __func__);
if (!AggCheckCallContext(fcinfo, NULL))
elog(ERROR, "%s called in non-aggregate context", __func__);
if (PG_ARGISNULL(0))
{
bytea *emptybuf = palloc(VARHDRSZ);
SET_VARSIZE(emptybuf, VARHDRSZ);
PG_RETURN_BYTEA_P(emptybuf);
}
ctx = (mvt_agg_context *) PG_GETARG_POINTER(0);
PG_RETURN_BYTEA_P(mvt_ctx_serialize(ctx));
#endif
}
PG_FUNCTION_INFO_V1(pgis_asmvt_deserialfn);
Datum pgis_asmvt_deserialfn(PG_FUNCTION_ARGS)
{
#ifndef HAVE_LIBPROTOBUF
elog(ERROR, "Missing libprotobuf-c");
PG_RETURN_NULL();
#else
MemoryContext aggcontext, oldcontext;
mvt_agg_context *ctx;
elog(DEBUG2, "%s called", __func__);
if (!AggCheckCallContext(fcinfo, &aggcontext))
elog(ERROR, "%s called in non-aggregate context", __func__);
oldcontext = MemoryContextSwitchTo(aggcontext);
ctx = mvt_ctx_deserialize(PG_GETARG_BYTEA_P(0));
MemoryContextSwitchTo(oldcontext);
PG_RETURN_POINTER(ctx);
#endif
}
PG_FUNCTION_INFO_V1(pgis_asmvt_combinefn);
Datum pgis_asmvt_combinefn(PG_FUNCTION_ARGS)
{
#ifndef HAVE_LIBPROTOBUF
elog(ERROR, "Missing libprotobuf-c");
PG_RETURN_NULL();
#else
MemoryContext aggcontext, oldcontext;
mvt_agg_context *ctx, *ctx1, *ctx2;
elog(DEBUG2, "%s called", __func__);
if (!AggCheckCallContext(fcinfo, &aggcontext))
elog(ERROR, "%s called in non-aggregate context", __func__);
ctx1 = (mvt_agg_context*)PG_GETARG_POINTER(0);
ctx2 = (mvt_agg_context*)PG_GETARG_POINTER(1);
oldcontext = MemoryContextSwitchTo(aggcontext);
ctx = mvt_ctx_combine(ctx1, ctx2);
MemoryContextSwitchTo(oldcontext);
PG_RETURN_POINTER(ctx);
#endif
}