PostGIS  3.4.0dev-r@@SVN_REVISION@@
lwgeom_out_flatgeobuf.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) 2021 Björn Harrtell <bjorn@wololo.org>
22  *
23  **********************************************************************/
24 
30 #include "postgres.h"
31 #include "utils/builtins.h"
32 #include "executor/spi.h"
33 
34 #include "../postgis_config.h"
35 #include "lwgeom_pg.h"
36 #include "lwgeom_log.h"
37 #include "liblwgeom.h"
38 #include "flatgeobuf.h"
39 
44 Datum pgis_asflatgeobuf_transfn(PG_FUNCTION_ARGS)
45 {
46  MemoryContext aggcontext, oldcontext;
47  char *geom_name = NULL;
48  bool create_index = false;
49  flatgeobuf_agg_ctx *ctx;
50 
51  POSTGIS_DEBUG(2, "calling pgis_asflatgeobuf_transfn");
52 
53  /* We need to initialize the internal cache to access it later via postgis_oid() */
54  postgis_initialize_cache();
55 
56  if (!AggCheckCallContext(fcinfo, &aggcontext))
57  elog(ERROR, "pgis_asflatgeobuf_transfn: called in non-aggregate context");
58  oldcontext = MemoryContextSwitchTo(aggcontext);
59 
60  if (PG_ARGISNULL(0)) {
61  if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
62  create_index = PG_GETARG_BOOL(2);
63  if (PG_NARGS() > 3 && !PG_ARGISNULL(3))
64  geom_name = text_to_cstring(PG_GETARG_TEXT_P(3));
65  ctx = flatgeobuf_agg_ctx_init(geom_name, create_index);
66  } else {
67  ctx = (flatgeobuf_agg_ctx *) PG_GETARG_POINTER(0);
68  }
69 
70  if (!type_is_rowtype(get_fn_expr_argtype(fcinfo->flinfo, 1)))
71  elog(ERROR, "pgis_asflatgeobuf_transfn: parameter row cannot be other than a rowtype");
72  ctx->row = PG_GETARG_HEAPTUPLEHEADER(1);
73 
75  MemoryContextSwitchTo(oldcontext);
76  PG_RETURN_POINTER(ctx);
77 }
78 
83 Datum pgis_asflatgeobuf_finalfn(PG_FUNCTION_ARGS)
84 {
85  uint8_t *buf;
86  flatgeobuf_agg_ctx *ctx;
87  if (!AggCheckCallContext(fcinfo, NULL))
88  elog(ERROR, "pgis_asflatgeobuf_finalfn called in non-aggregate context");
89 
90  if (PG_ARGISNULL(0))
91  PG_RETURN_NULL();
92 
93  ctx = (flatgeobuf_agg_ctx *) PG_GETARG_POINTER(0);
94  buf = flatgeobuf_agg_finalfn(ctx);
95  PG_RETURN_BYTEA_P(buf);
96 }
uint8_t * flatgeobuf_agg_finalfn(struct flatgeobuf_agg_ctx *ctx)
Finalize aggregation.
Definition: flatgeobuf.c:558
struct flatgeobuf_agg_ctx * flatgeobuf_agg_ctx_init(const char *geom_name, const bool create_index)
Initialize aggregation context.
Definition: flatgeobuf.c:503
void flatgeobuf_agg_transfn(struct flatgeobuf_agg_ctx *ctx)
Aggregation step.
Definition: flatgeobuf.c:527
This library is the generic geometry handling section of PostGIS.
Datum pgis_asflatgeobuf_transfn(PG_FUNCTION_ARGS)
Datum pgis_asflatgeobuf_finalfn(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(pgis_asflatgeobuf_transfn)
Process input parameters and row data into state.
HeapTupleHeader row
Definition: flatgeobuf.h:52