PostGIS  2.5.0beta1dev-r@@SVN_REVISION@@
lwgeom_out_geobuf.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 "geobuf.h"
26 
32 #include "postgres.h"
33 #include "utils/builtins.h"
34 #include "executor/spi.h"
35 
36 #include "../postgis_config.h"
37 #include "lwgeom_pg.h"
38 #include "lwgeom_log.h"
39 #include "liblwgeom.h"
40 #include "geobuf.h"
41 
46 Datum pgis_asgeobuf_transfn(PG_FUNCTION_ARGS)
47 {
48 #ifndef HAVE_LIBPROTOBUF
49  elog(ERROR, "Missing libprotobuf-c");
50  PG_RETURN_NULL();
51 #else
52  MemoryContext aggcontext;
53  struct geobuf_agg_context *ctx;
54 
55  if (!AggCheckCallContext(fcinfo, &aggcontext))
56  elog(ERROR, "pgis_asmvt_transfn: called in non-aggregate context");
57  MemoryContextSwitchTo(aggcontext);
58 
59  if (PG_ARGISNULL(0)) {
60  ctx = palloc(sizeof(*ctx));
61 
62  ctx->geom_name = NULL;
63  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
64  ctx->geom_name = text_to_cstring(PG_GETARG_TEXT_P(2));
66  } else {
67  ctx = (struct geobuf_agg_context *) PG_GETARG_POINTER(0);
68  }
69 
70  if (!type_is_rowtype(get_fn_expr_argtype(fcinfo->flinfo, 1)))
71  elog(ERROR, "pgis_asgeobuf_transfn: parameter row cannot be other than a rowtype");
72  ctx->row = PG_GETARG_HEAPTUPLEHEADER(1);
73 
74  geobuf_agg_transfn(ctx);
75  PG_RETURN_POINTER(ctx);
76 #endif
77 }
78 
83 Datum pgis_asgeobuf_finalfn(PG_FUNCTION_ARGS)
84 {
85 #ifndef HAVE_LIBPROTOBUF
86  elog(ERROR, "Missing libprotobuf-c");
87  PG_RETURN_NULL();
88 #else
89  uint8_t *buf;
90  struct geobuf_agg_context *ctx;
91  if (!AggCheckCallContext(fcinfo, NULL))
92  elog(ERROR, "pgis_asmvt_finalfn called in non-aggregate context");
93 
94  if (PG_ARGISNULL(0))
95  PG_RETURN_NULL();
96 
97  ctx = (struct geobuf_agg_context *) PG_GETARG_POINTER(0);
98  buf = geobuf_agg_finalfn(ctx);
99  PG_RETURN_BYTEA_P(buf);
100 #endif
101 }
char * text_to_cstring(const text *textptr)
PG_FUNCTION_INFO_V1(pgis_asgeobuf_transfn)
Process input parameters and row data into state.
HeapTupleHeader row
Definition: geobuf.h:52
Datum pgis_asgeobuf_finalfn(PG_FUNCTION_ARGS)
void geobuf_agg_transfn(struct geobuf_agg_context *ctx)
Aggregation step.
Definition: geobuf.c:561
void geobuf_agg_init_context(struct geobuf_agg_context *ctx)
Initialize aggregation context.
Definition: geobuf.c:524
uint8_t * geobuf_agg_finalfn(struct geobuf_agg_context *ctx)
Finalize aggregation.
Definition: geobuf.c:607
Datum pgis_asgeobuf_transfn(PG_FUNCTION_ARGS)
unsigned char uint8_t
Definition: uthash.h:79
char * geom_name
Definition: geobuf.h:50
This library is the generic geometry handling section of PostGIS.