PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
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
46Datum pgis_asgeobuf_transfn(PG_FUNCTION_ARGS)
47{
48#if !(defined HAVE_LIBPROTOBUF)
49 elog(ERROR, "ST_AsGeobuf: Compiled without protobuf-c support");
50 PG_RETURN_NULL();
51#else
52 MemoryContext aggcontext, oldcontext;
53 struct geobuf_agg_context *ctx;
54
55 /* We need to initialize the internal cache to access it later via postgis_oid() */
56 postgis_initialize_cache();
57
58 if (!AggCheckCallContext(fcinfo, &aggcontext))
59 elog(ERROR, "pgis_asgeobuf_transfn: called in non-aggregate context");
60 oldcontext = MemoryContextSwitchTo(aggcontext);
61
62 if (PG_ARGISNULL(0)) {
63 ctx = palloc(sizeof(*ctx));
64
65 ctx->geom_name = NULL;
66 if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
67 ctx->geom_name = text_to_cstring(PG_GETARG_TEXT_P(2));
69 } else {
70 ctx = (struct geobuf_agg_context *) PG_GETARG_POINTER(0);
71 }
72
73 if (!type_is_rowtype(get_fn_expr_argtype(fcinfo->flinfo, 1)))
74 elog(ERROR, "pgis_asgeobuf_transfn: parameter row cannot be other than a rowtype");
75
76 /* Null input tuple => null result */
77 if (PG_ARGISNULL(1)) {
78 PG_RETURN_NULL();
79 }
80
81 ctx->row = PG_GETARG_HEAPTUPLEHEADER(1);
82
84 MemoryContextSwitchTo(oldcontext);
85 PG_RETURN_POINTER(ctx);
86#endif
87}
88
93Datum pgis_asgeobuf_finalfn(PG_FUNCTION_ARGS)
94{
95#if !(defined HAVE_LIBPROTOBUF)
96 elog(ERROR, "ST_AsGeobuf: Compiled without protobuf-c support");
97 PG_RETURN_NULL();
98#else
99 uint8_t *buf;
100 struct geobuf_agg_context *ctx;
101 if (!AggCheckCallContext(fcinfo, NULL))
102 elog(ERROR, "pgis_asmvt_finalfn called in non-aggregate context");
103
104 if (PG_ARGISNULL(0))
105 PG_RETURN_NULL();
106
107 ctx = (struct geobuf_agg_context *) PG_GETARG_POINTER(0);
108 buf = geobuf_agg_finalfn(ctx);
109 PG_RETURN_BYTEA_P(buf);
110#endif
111}
void geobuf_agg_init_context(struct geobuf_agg_context *ctx)
Initialize aggregation context.
Definition geobuf.c:540
void geobuf_agg_transfn(struct geobuf_agg_context *ctx)
Aggregation step.
Definition geobuf.c:577
uint8_t * geobuf_agg_finalfn(struct geobuf_agg_context *ctx)
Finalize aggregation.
Definition geobuf.c:623
This library is the generic geometry handling section of PostGIS.
Datum pgis_asgeobuf_transfn(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(pgis_asgeobuf_transfn)
Process input parameters and row data into state.
Datum pgis_asgeobuf_finalfn(PG_FUNCTION_ARGS)
HeapTupleHeader row
Definition geobuf.h:52
char * geom_name
Definition geobuf.h:50