PostGIS  2.5.0dev-r@@SVN_REVISION@@
lwgeom_dump.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 2001-2009 Refractions Research Inc.
22  *
23  **********************************************************************/
24 
25 
26 #include <math.h>
27 #include <float.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <assert.h>
32 
33 #include "postgres.h"
34 #include "fmgr.h"
35 #include "utils/elog.h"
36 #include "utils/array.h"
37 #include "utils/geo_decls.h"
38 #include "funcapi.h"
39 
40 #include "../postgis_config.h"
41 #include "liblwgeom.h"
42 #include "lwgeom_pg.h"
43 
44 
45 Datum LWGEOM_dump(PG_FUNCTION_ARGS);
46 Datum LWGEOM_dump_rings(PG_FUNCTION_ARGS);
47 Datum ST_Subdivide(PG_FUNCTION_ARGS);
48 
49 typedef struct GEOMDUMPNODE_T
50 {
53 }
55 
56 #define MAXDEPTH 32
57 typedef struct GEOMDUMPSTATE
58 {
59  int stacklen;
62 }
64 
65 #define PUSH(x,y) ((x)->stack[(x)->stacklen++]=(y))
66 #define LAST(x) ((x)->stack[(x)->stacklen-1])
67 #define POP(x) (--((x)->stacklen))
68 
69 
71 Datum LWGEOM_dump(PG_FUNCTION_ARGS)
72 {
73  GSERIALIZED *pglwgeom;
74  LWCOLLECTION *lwcoll;
75  LWGEOM *lwgeom;
76  FuncCallContext *funcctx;
77  GEOMDUMPSTATE *state;
78  GEOMDUMPNODE *node;
79  TupleDesc tupdesc;
80  HeapTuple tuple;
81  AttInMetadata *attinmeta;
82  MemoryContext oldcontext, newcontext;
83  Datum result;
84  char address[256];
85  char *ptr;
86  int i;
87  char *values[2];
88 
89  if (SRF_IS_FIRSTCALL())
90  {
91  funcctx = SRF_FIRSTCALL_INIT();
92  newcontext = funcctx->multi_call_memory_ctx;
93 
94  oldcontext = MemoryContextSwitchTo(newcontext);
95 
96  pglwgeom = PG_GETARG_GSERIALIZED_P_COPY(0);
97  lwgeom = lwgeom_from_gserialized(pglwgeom);
98 
99  /* Create function state */
100  state = lwalloc(sizeof(GEOMDUMPSTATE));
101  state->root = lwgeom;
102  state->stacklen=0;
103 
104  if ( lwgeom_is_collection(lwgeom) )
105  {
106  /*
107  * Push a GEOMDUMPNODE on the state stack
108  */
109  node = lwalloc(sizeof(GEOMDUMPNODE));
110  node->idx=0;
111  node->geom = lwgeom;
112  PUSH(state, node);
113  }
114 
115  funcctx->user_fctx = state;
116 
117  /*
118  * Build a tuple description for an
119  * geometry_dump tuple
120  */
121  tupdesc = RelationNameGetTupleDesc("geometry_dump");
122 
123  /*
124  * generate attribute metadata needed later to produce
125  * tuples from raw C strings
126  */
127  attinmeta = TupleDescGetAttInMetadata(tupdesc);
128  funcctx->attinmeta = attinmeta;
129 
130  MemoryContextSwitchTo(oldcontext);
131  }
132 
133  /* stuff done on every call of the function */
134  funcctx = SRF_PERCALL_SETUP();
135  newcontext = funcctx->multi_call_memory_ctx;
136 
137  /* get state */
138  state = funcctx->user_fctx;
139 
140  /* Handled simple geometries */
141  if ( ! state->root ) SRF_RETURN_DONE(funcctx);
142  /* Return nothing for empties */
143  if ( lwgeom_is_empty(state->root) ) SRF_RETURN_DONE(funcctx);
144  if ( ! lwgeom_is_collection(state->root) )
145  {
146  values[0] = "{}";
147  values[1] = lwgeom_to_hexwkb(state->root, WKB_EXTENDED, 0);
148  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
149  result = HeapTupleGetDatum(tuple);
150 
151  state->root = NULL;
152  SRF_RETURN_NEXT(funcctx, result);
153  }
154 
155  while (1)
156  {
157  node = LAST(state);
158  lwcoll = (LWCOLLECTION*)node->geom;
159 
160  if ( node->idx < lwcoll->ngeoms )
161  {
162  lwgeom = lwcoll->geoms[node->idx];
163  if ( ! lwgeom_is_collection(lwgeom) )
164  {
165  /* write address of current geom */
166  ptr=address;
167  *ptr++='{';
168  for (i=0; i<state->stacklen; i++)
169  {
170  if ( i ) ptr += sprintf(ptr, ",");
171  ptr += sprintf(ptr, "%d", state->stack[i]->idx+1);
172  }
173  *ptr++='}';
174  *ptr='\0';
175 
176  break;
177  }
178 
179  /*
180  * It's a collection, increment index
181  * of current node, push a new one on the
182  * stack
183  */
184 
185  oldcontext = MemoryContextSwitchTo(newcontext);
186 
187  node = lwalloc(sizeof(GEOMDUMPNODE));
188  node->idx=0;
189  node->geom = lwgeom;
190  PUSH(state, node);
191 
192  MemoryContextSwitchTo(oldcontext);
193 
194  continue;
195  }
196 
197  if ( ! POP(state) ) SRF_RETURN_DONE(funcctx);
198  LAST(state)->idx++;
199  }
200 
201  lwgeom->srid = state->root->srid;
202 
203  values[0] = address;
204  values[1] = lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, 0);
205  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
206  result = TupleGetDatum(funcctx->slot, tuple);
207  node->idx++;
208  SRF_RETURN_NEXT(funcctx, result);
209 }
210 
212 {
215 };
216 
218 Datum LWGEOM_dump_rings(PG_FUNCTION_ARGS)
219 {
220  GSERIALIZED *pglwgeom;
221  LWGEOM *lwgeom;
222  FuncCallContext *funcctx;
223  struct POLYDUMPSTATE *state;
224  TupleDesc tupdesc;
225  HeapTuple tuple;
226  AttInMetadata *attinmeta;
227  MemoryContext oldcontext, newcontext;
228  Datum result;
229  char address[256];
230  char *values[2];
231 
232  if (SRF_IS_FIRSTCALL())
233  {
234  funcctx = SRF_FIRSTCALL_INIT();
235  newcontext = funcctx->multi_call_memory_ctx;
236 
237  oldcontext = MemoryContextSwitchTo(newcontext);
238 
239  pglwgeom = PG_GETARG_GSERIALIZED_P_COPY(0);
240  if ( gserialized_get_type(pglwgeom) != POLYGONTYPE )
241  {
242  elog(ERROR, "Input is not a polygon");
243  }
244 
245  lwgeom = lwgeom_from_gserialized(pglwgeom);
246 
247  /* Create function state */
248  state = lwalloc(sizeof(struct POLYDUMPSTATE));
249  state->poly = lwgeom_as_lwpoly(lwgeom);
250  assert (state->poly);
251  state->ringnum=0;
252 
253  funcctx->user_fctx = state;
254 
255  /*
256  * Build a tuple description for an
257  * geometry_dump tuple
258  */
259  tupdesc = RelationNameGetTupleDesc("geometry_dump");
260 
261  /*
262  * generate attribute metadata needed later to produce
263  * tuples from raw C strings
264  */
265  attinmeta = TupleDescGetAttInMetadata(tupdesc);
266  funcctx->attinmeta = attinmeta;
267 
268  MemoryContextSwitchTo(oldcontext);
269  }
270 
271  /* stuff done on every call of the function */
272  funcctx = SRF_PERCALL_SETUP();
273  newcontext = funcctx->multi_call_memory_ctx;
274 
275  /* get state */
276  state = funcctx->user_fctx;
277 
278  /* Loop trough polygon rings */
279  while (state->ringnum < state->poly->nrings )
280  {
281  LWPOLY* poly = state->poly;
282  POINTARRAY *ring;
283  LWGEOM* ringgeom;
284 
285  /* Switch to an appropriate memory context for POINTARRAY
286  * cloning and hexwkb allocation */
287  oldcontext = MemoryContextSwitchTo(newcontext);
288 
289  /* We need a copy of input ring here */
290  ring = ptarray_clone_deep(poly->rings[state->ringnum]);
291 
292  /* Construct another polygon with shell only */
293  ringgeom = (LWGEOM*)lwpoly_construct(
294  poly->srid,
295  NULL, /* TODO: could use input bounding box here */
296  1, /* one ring */
297  &ring);
298 
299  /* Write path as ``{ <ringnum> }'' */
300  sprintf(address, "{%d}", state->ringnum);
301 
302  values[0] = address;
303  values[1] = lwgeom_to_hexwkb(ringgeom, WKB_EXTENDED, 0);
304 
305  MemoryContextSwitchTo(oldcontext);
306 
307  tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
308  result = HeapTupleGetDatum(tuple);
309  ++state->ringnum;
310  SRF_RETURN_NEXT(funcctx, result);
311  }
312 
313  SRF_RETURN_DONE(funcctx);
314 
315 }
316 
317 
319 {
320  int geomnum;
322 };
323 
324 /*
325 * Break an object up into smaller objects of no more than N vertices
326 */
328 Datum ST_Subdivide(PG_FUNCTION_ARGS)
329 {
330 #if POSTGIS_GEOS_VERSION < 35
331 
332  elog(ERROR, "The GEOS version this PostGIS binary "
333  "was compiled against (%d) doesn't support "
334  "'%s' function (3.5.0+ required)",
335  POSTGIS_GEOS_VERSION, __func__);
336  PG_RETURN_NULL();
337 
338 #else /* POSTGIS_GEOS_VERSION >= 35 */
339 
340  typedef struct
341  {
342  int nextgeom;
343  int numgeoms;
344  LWCOLLECTION *col;
345  } collection_fctx;
346 
347  FuncCallContext *funcctx;
348  collection_fctx *fctx;
349  MemoryContext oldcontext;
350 
351  /* stuff done only on the first call of the function */
352  if (SRF_IS_FIRSTCALL())
353  {
354  GSERIALIZED *gser;
355  LWGEOM *geom;
356  LWCOLLECTION *col;
357  int maxvertices = 256;
358 
359  /* create a function context for cross-call persistence */
360  funcctx = SRF_FIRSTCALL_INIT();
361 
362  /*
363  * switch to memory context appropriate for multiple function calls
364  */
365  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
366 
367  /*
368  * Get the geometry value
369  */
370  gser = PG_GETARG_GSERIALIZED_P(0);
371  geom = lwgeom_from_gserialized(gser);
372 
373  /*
374  * Get the max vertices value
375  */
376  if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
377  maxvertices = PG_GETARG_INT32(1);
378 
379  /*
380  * Compute the subdivision of the geometry
381  */
382  col = lwgeom_subdivide(geom, maxvertices);
383 
384  if ( ! col )
385  SRF_RETURN_DONE(funcctx);
386 
387  /* allocate memory for user context */
388  fctx = (collection_fctx *) palloc(sizeof(collection_fctx));
389 
390  /* initialize state */
391  fctx->nextgeom = 0;
392  fctx->numgeoms = col->ngeoms;
393  fctx->col = col;
394 
395  /* save user context, switch back to function context */
396  funcctx->user_fctx = fctx;
397  MemoryContextSwitchTo(oldcontext);
398  }
399 
400  /* stuff done on every call of the function */
401  funcctx = SRF_PERCALL_SETUP();
402  fctx = funcctx->user_fctx;
403 
404  if (fctx->nextgeom < fctx->numgeoms)
405  {
406  GSERIALIZED *gpart = geometry_serialize(fctx->col->geoms[fctx->nextgeom]);
407  fctx->nextgeom++;
408  SRF_RETURN_NEXT(funcctx, PointerGetDatum(gpart));
409  }
410  else
411  {
412  /* do when there is no more left */
413  SRF_RETURN_DONE(funcctx);
414  }
415 
416 #endif /* POSTGIS_GEOS_VERSION >= 35 */
417 }
418 
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:86
char * lwgeom_to_hexwkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Definition: lwout_wkb.c:848
Datum LWGEOM_dump(PG_FUNCTION_ARGS)
Definition: lwgeom_dump.c:71
#define POSTGIS_GEOS_VERSION
Definition: sqldefines.h:10
#define MAXDEPTH
Definition: lwgeom_dump.c:56
#define POP(x)
Definition: lwgeom_dump.c:67
#define PUSH(x, y)
Definition: lwgeom_dump.c:65
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1085
LWGEOM * root
Definition: lwgeom_dump.c:61
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:86
LWCOLLECTION * col
Definition: lwgeom_dump.c:321
PG_FUNCTION_INFO_V1(LWGEOM_dump)
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:206
uint32_t ngeoms
Definition: liblwgeom.h:509
int32_t srid
Definition: liblwgeom.h:401
uint32_t nrings
Definition: liblwgeom.h:457
unsigned int uint32_t
Definition: uthash.h:78
struct GEOMDUMPSTATE GEOMDUMPSTATE
LWCOLLECTION * lwgeom_subdivide(const LWGEOM *geom, uint32_t maxvertices)
Definition: lwgeom.c:2412
Datum ST_Subdivide(PG_FUNCTION_ARGS)
Definition: lwgeom_dump.c:328
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
LWGEOM ** geoms
Definition: liblwgeom.h:511
Datum LWGEOM_dump_rings(PG_FUNCTION_ARGS)
Definition: lwgeom_dump.c:218
POINTARRAY ** rings
Definition: liblwgeom.h:459
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:628
GEOMDUMPNODE * stack[MAXDEPTH]
Definition: lwgeom_dump.c:60
#define LAST(x)
Definition: lwgeom_dump.c:66
#define WKB_EXTENDED
Definition: liblwgeom.h:2067
LWGEOM * geom
Definition: lwgeom_dump.c:52
LWPOLY * poly
Definition: lwgeom_dump.c:214
int32_t srid
Definition: liblwgeom.h:456
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint32_t ringnum
Definition: lwgeom_dump.c:213
void * lwalloc(size_t size)
Definition: lwutil.c:229
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1393
struct GEOMDUMPNODE_T GEOMDUMPNODE
uint32_t idx
Definition: lwgeom_dump.c:51
if(!(yy_init))
This library is the generic geometry handling section of PostGIS.