PostGIS  2.4.9dev-r@@SVN_REVISION@@
lwgeom_out_mvt.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright (C) 2016-2017 Björn Harrtell <bjorn@wololo.org>
22  *
23  **********************************************************************/
24 
25 #include "postgres.h"
26 #include "utils/builtins.h"
27 #include "executor/spi.h"
28 #include "../postgis_config.h"
29 #include "lwgeom_pg.h"
30 #include "lwgeom_log.h"
31 #include "liblwgeom.h"
32 #include "mvt.h"
33 
34 #ifdef HAVE_LIBPROTOBUF
35 #include "vector_tile.pb-c.h"
36 #endif /* HAVE_LIBPROTOBUF */
37 
42 Datum ST_AsMVTGeom(PG_FUNCTION_ARGS)
43 {
44 #ifndef HAVE_LIBPROTOBUF
45  elog(ERROR, "Missing libprotobuf-c");
46  PG_RETURN_NULL();
47 #else
48  LWGEOM *lwgeom_in, *lwgeom_out;
49  GSERIALIZED *geom_in, *geom_out;
50  GBOX *bounds;
51  int extent, buffer;
52  bool clip_geom;
53  if (PG_ARGISNULL(0))
54  PG_RETURN_NULL();
55  geom_in = PG_GETARG_GSERIALIZED_P(0);
56  lwgeom_in = lwgeom_from_gserialized(geom_in);
57  if (PG_ARGISNULL(1))
58  elog(ERROR, "ST_AsMVTGeom: parameter bounds cannot be null");
59  bounds = (GBOX *) PG_GETARG_POINTER(1);
60  extent = PG_ARGISNULL(2) ? 4096 : PG_GETARG_INT32(2);
61  buffer = PG_ARGISNULL(3) ? 256 : PG_GETARG_INT32(3);
62  clip_geom = PG_ARGISNULL(4) ? true : PG_GETARG_BOOL(4);
63  lwgeom_out = mvt_geom(lwgeom_in, bounds, extent, buffer, clip_geom);
64  lwgeom_free(lwgeom_in);
65  if (lwgeom_out == NULL)
66  PG_RETURN_NULL();
67  geom_out = geometry_serialize(lwgeom_out);
68  lwgeom_free(lwgeom_out);
69  PG_FREE_IF_COPY(geom_in, 0);
70  PG_RETURN_POINTER(geom_out);
71 #endif
72 }
73 
78 Datum pgis_asmvt_transfn(PG_FUNCTION_ARGS)
79 {
80 #ifndef HAVE_LIBPROTOBUF
81  elog(ERROR, "Missing libprotobuf-c");
82  PG_RETURN_NULL();
83 #else
84  MemoryContext aggcontext;
85  struct mvt_agg_context *ctx;
86 
87  if (!AggCheckCallContext(fcinfo, &aggcontext))
88  elog(ERROR, "pgis_asmvt_transfn: called in non-aggregate context");
89  MemoryContextSwitchTo(aggcontext);
90 
91  if (PG_ARGISNULL(0)) {
92  ctx = palloc(sizeof(*ctx));
93  ctx->name = "default";
94  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
95  ctx->name = text_to_cstring(PG_GETARG_TEXT_P(2));
96  ctx->extent = 4096;
97  if (PG_NARGS() > 3 && !PG_ARGISNULL(3))
98  ctx->extent = PG_GETARG_INT32(3);
99  ctx->geom_name = NULL;
100  if (PG_NARGS() > 4 && !PG_ARGISNULL(4))
101  ctx->geom_name = text_to_cstring(PG_GETARG_TEXT_P(4));
103  } else {
104  ctx = (struct mvt_agg_context *) PG_GETARG_POINTER(0);
105  }
106 
107  if (!type_is_rowtype(get_fn_expr_argtype(fcinfo->flinfo, 1)))
108  elog(ERROR, "pgis_asmvt_transfn: parameter row cannot be other than a rowtype");
109  ctx->row = PG_GETARG_HEAPTUPLEHEADER(1);
110 
111  mvt_agg_transfn(ctx);
112  PG_FREE_IF_COPY(ctx->row, 1);
113  PG_RETURN_POINTER(ctx);
114 #endif
115 }
116 
121 Datum pgis_asmvt_finalfn(PG_FUNCTION_ARGS)
122 {
123 #ifndef HAVE_LIBPROTOBUF
124  elog(ERROR, "Missing libprotobuf-c");
125  PG_RETURN_NULL();
126 #else
127  struct mvt_agg_context *ctx;
128  uint8_t *buf;
129  if (!AggCheckCallContext(fcinfo, NULL))
130  elog(ERROR, "pgis_asmvt_finalfn called in non-aggregate context");
131 
132  if (PG_ARGISNULL(0))
133  {
134  bytea *emptybuf = palloc(VARHDRSZ);
135  SET_VARSIZE(emptybuf, VARHDRSZ);
136  PG_RETURN_BYTEA_P(emptybuf);
137  }
138 
139  ctx = (struct mvt_agg_context *) PG_GETARG_POINTER(0);
140  buf = mvt_agg_finalfn(ctx);
141  PG_RETURN_BYTEA_P(buf);
142 #endif
143 }
HeapTupleHeader row
Definition: mvt.h:54
char * name
Definition: mvt.h:50
uint8_t * mvt_agg_finalfn(struct mvt_agg_context *ctx)
Finalize aggregation.
Definition: mvt.c:942
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
char * geom_name
Definition: mvt.h:52
Datum ST_AsMVTGeom(PG_FUNCTION_ARGS)
LWGEOM * mvt_geom(const LWGEOM *lwgeom, const GBOX *gbox, uint32_t extent, uint32_t buffer, bool clip_geom)
Transform a geometry into vector tile coordinate space.
Definition: mvt.c:710
Datum buffer(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(ST_AsMVTGeom)
Process input parameters to mvt_geom and returned serialized geometry.
void mvt_agg_init_context(struct mvt_agg_context *ctx)
Initialize aggregation context.
Definition: mvt.c:848
Datum pgis_asmvt_finalfn(PG_FUNCTION_ARGS)
uint32_t extent
Definition: mvt.h:51
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
void mvt_agg_transfn(struct mvt_agg_context *ctx)
Aggregation step.
Definition: mvt.c:888
unsigned char uint8_t
Definition: uthash.h:79
This library is the generic geometry handling section of PostGIS.
Datum pgis_asmvt_transfn(PG_FUNCTION_ARGS)