PostGIS  3.4.0dev-r@@SVN_REVISION@@
lwgeom_dumppoints.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^
22  *
23  **********************************************************************/
24 
25 #include "postgres.h"
26 #include "fmgr.h"
27 #include "utils/elog.h"
28 #include "utils/array.h"
29 #include "utils/geo_decls.h"
30 #include "utils/lsyscache.h"
31 #include "catalog/pg_type.h"
32 #include "funcapi.h"
33 
34 #include "../postgis_config.h"
35 #include "lwgeom_pg.h"
36 
37 #include "access/htup_details.h"
38 
39 
40 #include "liblwgeom.h"
41 
42 /* ST_DumpPoints for postgis.
43  * By Nathan Wagner, copyright disclaimed,
44  * this entire file is in the public domain
45  */
46 
47 Datum LWGEOM_dumppoints(PG_FUNCTION_ARGS);
48 Datum LWGEOM_dumpsegments(PG_FUNCTION_ARGS);
49 
50 struct dumpnode {
52  uint32_t idx; /* which member geom we're working on */
53 } ;
54 
55 /* 32 is the max depth for st_dump, so it seems reasonable
56  * to use the same here
57  */
58 #define MAXDEPTH 32
59 struct dumpstate {
61  int stacklen; /* collections/geoms on stack */
62  int pathlen; /* polygon rings and such need extra path info */
63  struct dumpnode stack[MAXDEPTH];
64  Datum path[34]; /* two more than max depth, for ring and point */
65 
66  /* used to cache the type attributes for integer arrays */
67  int16 typlen;
68  bool byval;
69  char align;
70 
71  uint32_t ring; /* ring of top polygon */
72  uint32_t pt; /* point of top geom or current ring */
73 };
74 
76 Datum LWGEOM_dumppoints(PG_FUNCTION_ARGS) {
77  FuncCallContext *funcctx;
78  MemoryContext oldcontext, newcontext;
79 
80  GSERIALIZED *pglwgeom;
81  LWCOLLECTION *lwcoll;
82  LWGEOM *lwgeom;
83  struct dumpstate *state;
84  struct dumpnode *node;
85 
86  HeapTuple tuple;
87  Datum pathpt[2]; /* used to construct the composite return value */
88  bool isnull[2] = {0,0}; /* needed to say neither value is null */
89  Datum result; /* the actual composite return value */
90 
91  if (SRF_IS_FIRSTCALL()) {
92  funcctx = SRF_FIRSTCALL_INIT();
93 
94  newcontext = funcctx->multi_call_memory_ctx;
95  oldcontext = MemoryContextSwitchTo(newcontext);
96 
97  /* get a local copy of what we're doing a dump points on */
98  pglwgeom = PG_GETARG_GSERIALIZED_P_COPY(0);
99  lwgeom = lwgeom_from_gserialized(pglwgeom);
100 
101  /* return early if nothing to do */
102  if (!lwgeom || lwgeom_is_empty(lwgeom)) {
103  MemoryContextSwitchTo(oldcontext);
104  funcctx = SRF_PERCALL_SETUP();
105  SRF_RETURN_DONE(funcctx);
106  }
107 
108  /* Create function state */
109  state = lwalloc(sizeof *state);
110  state->root = lwgeom;
111  state->stacklen = 0;
112  state->pathlen = 0;
113  state->pt = 0;
114  state->ring = 0;
115 
116  funcctx->user_fctx = state;
117 
118  /*
119  * Push a struct dumpnode on the state stack
120  */
121 
122  state->stack[0].idx = 0;
123  state->stack[0].geom = lwgeom;
124  state->stacklen++;
125 
126  /*
127  * get tuple description for return type
128  */
129  if (get_call_result_type(fcinfo, 0, &funcctx->tuple_desc) != TYPEFUNC_COMPOSITE) {
130  ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
131  errmsg("set-valued function called in context that cannot accept a set")));
132  }
133 
134  BlessTupleDesc(funcctx->tuple_desc);
135 
136  /* get and cache data for constructing int4 arrays */
137  get_typlenbyvalalign(INT4OID, &state->typlen, &state->byval, &state->align);
138 
139  MemoryContextSwitchTo(oldcontext);
140  }
141 
142  /* stuff done on every call of the function */
143  funcctx = SRF_PERCALL_SETUP();
144  newcontext = funcctx->multi_call_memory_ctx;
145 
146  /* get state */
147  state = funcctx->user_fctx;
148 
149  while (1) {
150  node = &state->stack[state->stacklen-1];
151  lwgeom = node->geom;
152 
153  /* need to return a point from this geometry */
154  if (!lwgeom_is_collection(lwgeom) ) {
155  /* either return a point, or pop the stack */
156  /* TODO use a union? would save a tiny amount of stack space.
157  * probably not worth the bother
158  */
159  LWLINE *line;
160  LWCIRCSTRING *circ;
161  LWPOLY *poly;
162  LWTRIANGLE *tri;
163  LWPOINT *lwpoint = NULL;
164  POINT4D pt;
165 
166  if ( !lwgeom_is_empty(lwgeom) ) {
167  /*
168  * net result of switch should be to set lwpoint to the
169  * next point to return, or leave at NULL if there
170  * are no more points in the geometry
171  */
172  switch(lwgeom->type) {
173  case TRIANGLETYPE:
174  tri = lwgeom_as_lwtriangle(lwgeom);
175  if (state->pt == 0) {
176  state->path[state->pathlen++] = Int32GetDatum(state->ring+1);
177  }
178  if (state->pt <= 3) {
179  getPoint4d_p(tri->points, state->pt, &pt);
180  lwpoint = lwpoint_make(tri->srid,
181  lwgeom_has_z(lwgeom),
182  lwgeom_has_m(lwgeom),
183  &pt);
184  }
185  if (state->pt > 3) {
186  state->pathlen--;
187  }
188  break;
189  case POLYGONTYPE:
190  poly = lwgeom_as_lwpoly(lwgeom);
191  if (state->pt == poly->rings[state->ring]->npoints) {
192  state->pt = 0;
193  state->ring++;
194  state->pathlen--;
195  }
196  if (state->pt == 0 && state->ring < poly->nrings) {
197  /* handle new ring */
198  state->path[state->pathlen] = Int32GetDatum(state->ring+1);
199  state->pathlen++;
200  }
201  if (state->ring == poly->nrings) {
202  } else {
203  /* TODO should be able to directly get the point
204  * into the point array of a fixed lwpoint
205  */
206  /* can't get the point directly from the ptarray because
207  * it might be aligned wrong, so at least one memcpy
208  * seems unavoidable
209  * It might be possible to pass it directly to gserialized
210  * depending how that works, it might effectively be gserialized
211  * though a brief look at the code indicates not
212  */
213  getPoint4d_p(poly->rings[state->ring], state->pt, &pt);
214  lwpoint = lwpoint_make(poly->srid,
215  lwgeom_has_z(lwgeom),
216  lwgeom_has_m(lwgeom),
217  &pt);
218  }
219  break;
220  case POINTTYPE:
221  if (state->pt == 0) lwpoint = lwgeom_as_lwpoint(lwgeom);
222  break;
223  case LINETYPE:
224  line = lwgeom_as_lwline(lwgeom);
225  if (line->points && state->pt <= line->points->npoints) {
226  lwpoint = lwline_get_lwpoint((LWLINE*)lwgeom, state->pt);
227  }
228  break;
229  case CIRCSTRINGTYPE:
230  circ = lwgeom_as_lwcircstring(lwgeom);
231  if (circ->points && state->pt <= circ->points->npoints) {
232  lwpoint = lwcircstring_get_lwpoint((LWCIRCSTRING*)lwgeom, state->pt);
233  }
234  break;
235  default:
236  ereport(ERROR, (errcode(ERRCODE_DATA_EXCEPTION),
237  errmsg("Invalid Geometry type %d passed to ST_DumpPoints()", lwgeom->type)));
238  }
239  }
240 
241  /*
242  * At this point, lwpoint is either NULL, in which case
243  * we need to pop the geometry stack and get the next
244  * geometry, if any, or lwpoint is set and we construct
245  * a record type with the integer array of geometry
246  * indexes and the point number, and the actual point
247  * geometry itself
248  */
249 
250  if (!lwpoint) {
251  /* no point, so pop the geom and look for more */
252  if (--state->stacklen == 0) SRF_RETURN_DONE(funcctx);
253  state->pathlen--;
254  continue;
255  } else {
256  /* write address of current geom/pt */
257  state->pt++;
258 
259  state->path[state->pathlen] = Int32GetDatum(state->pt);
260  pathpt[0] = PointerGetDatum(construct_array(state->path, state->pathlen+1,
261  INT4OID, state->typlen, state->byval, state->align));
262 
263  pathpt[1] = PointerGetDatum(geometry_serialize((LWGEOM*)lwpoint));
264 
265  tuple = heap_form_tuple(funcctx->tuple_desc, pathpt, isnull);
266  result = HeapTupleGetDatum(tuple);
267  SRF_RETURN_NEXT(funcctx, result);
268  }
269  }
270 
271  lwcoll = (LWCOLLECTION*)node->geom;
272 
273  /* if a collection and we have more geoms */
274  if (node->idx < lwcoll->ngeoms) {
275  /* push the next geom on the path and the stack */
276  lwgeom = lwcoll->geoms[node->idx++];
277  state->path[state->pathlen++] = Int32GetDatum(node->idx);
278 
279  node = &state->stack[state->stacklen++];
280  node->idx = 0;
281  node->geom = lwgeom;
282 
283  state->pt = 0;
284  state->ring = 0;
285 
286  /* loop back to beginning, which will then check whatever node we just pushed */
287  continue;
288  }
289 
290  /* no more geometries in the current collection */
291  if (--state->stacklen == 0) SRF_RETURN_DONE(funcctx);
292  state->pathlen--;
293  }
294 }
295 
297 Datum LWGEOM_dumpsegments(PG_FUNCTION_ARGS)
298 {
299  FuncCallContext *funcctx;
300  MemoryContext oldcontext, newcontext;
301 
302  GSERIALIZED *pglwgeom;
303  LWCOLLECTION *lwcoll;
304  LWGEOM *lwgeom;
305  struct dumpstate *state;
306  struct dumpnode *node;
307 
308  HeapTuple tuple;
309  Datum pathpt[2]; /* used to construct the composite return value */
310  bool isnull[2] = {0, 0}; /* needed to say neither value is null */
311  Datum result; /* the actual composite return value */
312 
313  if (SRF_IS_FIRSTCALL())
314  {
315  funcctx = SRF_FIRSTCALL_INIT();
316 
317  newcontext = funcctx->multi_call_memory_ctx;
318  oldcontext = MemoryContextSwitchTo(newcontext);
319 
320  pglwgeom = PG_GETARG_GSERIALIZED_P_COPY(0);
321  lwgeom = lwgeom_from_gserialized(pglwgeom);
322 
323  /* return early if nothing to do */
324  if (!lwgeom || lwgeom_is_empty(lwgeom))
325  {
326  MemoryContextSwitchTo(oldcontext);
327  funcctx = SRF_PERCALL_SETUP();
328  SRF_RETURN_DONE(funcctx);
329  }
330 
331  /* Create function state */
332  state = lwalloc(sizeof *state);
333  state->root = lwgeom;
334  state->stacklen = 0;
335  state->pathlen = 0;
336  state->pt = 0;
337  state->ring = 0;
338 
339  funcctx->user_fctx = state;
340 
341  /*
342  * Push a struct dumpnode on the state stack
343  */
344  state->stack[0].idx = 0;
345  state->stack[0].geom = lwgeom;
346  state->stacklen++;
347 
348  /*
349  * get tuple description for return type
350  */
351  if (get_call_result_type(fcinfo, 0, &funcctx->tuple_desc) != TYPEFUNC_COMPOSITE)
352  {
353  ereport(ERROR,
354  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
355  errmsg("set-valued function called in context that cannot accept a set")));
356  }
357 
358  BlessTupleDesc(funcctx->tuple_desc);
359 
360  /* get and cache data for constructing int4 arrays */
361  get_typlenbyvalalign(INT4OID, &state->typlen, &state->byval, &state->align);
362 
363  MemoryContextSwitchTo(oldcontext);
364  }
365 
366  /* stuff done on every call of the function */
367  funcctx = SRF_PERCALL_SETUP();
368  newcontext = funcctx->multi_call_memory_ctx;
369 
370  /* get state */
371  state = funcctx->user_fctx;
372 
373  while (1)
374  {
375  POINTARRAY *points = NULL;
376  LWLINE *line;
377  LWTRIANGLE *tri;
378  LWPOLY *poly;
379  POINT4D pt_start, pt_end;
380  POINTARRAY *segment_pa;
381  LWLINE *segment;
382 
383  node = &state->stack[state->stacklen - 1];
384  lwgeom = node->geom;
385 
386  if ( !lwgeom_is_empty(lwgeom) && (lwgeom->type == LINETYPE || lwgeom->type == TRIANGLETYPE || lwgeom->type == POLYGONTYPE) )
387  {
388  if (lwgeom->type == LINETYPE)
389  {
390  line = (LWLINE *)lwgeom;
391 
392  if (state->pt < line->points->npoints - 1)
393  {
394  points = line->points;
395  }
396  }
397  if (lwgeom->type == TRIANGLETYPE)
398  {
399  tri = (LWTRIANGLE *)lwgeom;
400 
401  if (state->pt == 0)
402  {
403  state->path[state->pathlen++] = Int32GetDatum(state->ring + 1);
404  }
405 
406  if (state->pt < 3)
407  {
408  points = tri->points;
409  }
410  else
411  {
412  state->pathlen--;
413  }
414  }
415  if (lwgeom->type == POLYGONTYPE)
416  {
417  poly = (LWPOLY *)lwgeom;
418 
419  if (state->pt == poly->rings[state->ring]->npoints)
420  {
421  state->pt = 0;
422  state->ring++;
423  state->pathlen--;
424  }
425 
426  if (state->ring < poly->nrings)
427  {
428  if (state->pt == 0)
429  {
430  state->path[state->pathlen] = Int32GetDatum(state->ring + 1);
431  state->pathlen++;
432  }
433 
434  if (state->pt < poly->rings[state->ring]->npoints - 1)
435  {
436  points = poly->rings[state->ring];
437  }
438  else
439  {
440  state->pt++;
441  continue;
442  }
443  }
444  }
445 
446  if (points)
447  {
448  getPoint4d_p(points, state->pt, &pt_start);
449  getPoint4d_p(points, state->pt + 1, &pt_end);
450 
451  segment_pa = ptarray_construct(lwgeom_has_z(lwgeom), lwgeom_has_m(lwgeom), 2);
452  ptarray_set_point4d(segment_pa, 0, &pt_start);
453  ptarray_set_point4d(segment_pa, 1, &pt_end);
454 
455  segment = lwline_construct(lwgeom->srid, NULL, segment_pa);
456 
457  state->pt++;
458 
459  state->path[state->pathlen] = Int32GetDatum(state->pt);
460  pathpt[0] = PointerGetDatum(construct_array(state->path,
461  state->pathlen + 1,
462  INT4OID,
463  state->typlen,
464  state->byval,
465  state->align));
466  pathpt[1] = PointerGetDatum(geometry_serialize((LWGEOM *)segment));
467 
468  tuple = heap_form_tuple(funcctx->tuple_desc, pathpt, isnull);
469  result = HeapTupleGetDatum(tuple);
470  SRF_RETURN_NEXT(funcctx, result);
471  }
472  else
473  {
474  if (--state->stacklen == 0)
475  SRF_RETURN_DONE(funcctx);
476  state->pathlen--;
477  continue;
478  }
479  }
480 
481  if (lwgeom->type == COLLECTIONTYPE || lwgeom->type == MULTILINETYPE ||
482  lwgeom->type == MULTIPOLYGONTYPE || lwgeom->type == TINTYPE)
483  {
484  lwcoll = (LWCOLLECTION *)node->geom;
485 
486  /* if a collection and we have more geoms */
487  if (node->idx < lwcoll->ngeoms)
488  {
489  /* push the next geom on the path and the stack */
490  lwgeom = lwcoll->geoms[node->idx++];
491  state->path[state->pathlen++] = Int32GetDatum(node->idx);
492 
493  node = &state->stack[state->stacklen++];
494  node->idx = 0;
495  node->geom = lwgeom;
496 
497  state->pt = 0;
498  state->ring = 0;
499 
500  /* loop back to beginning, which will then check whatever node we just pushed */
501  continue;
502  }
503  }
504 
505  /* no more geometries in the current collection */
506  if (--state->stacklen == 0)
507  SRF_RETURN_DONE(funcctx);
508  state->pathlen--;
509  }
510 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:179
LWPOINT * lwcircstring_get_lwpoint(const LWCIRCSTRING *circ, uint32_t where)
Definition: lwcircstring.c:286
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
#define MULTILINETYPE
Definition: liblwgeom.h:106
#define LINETYPE
Definition: liblwgeom.h:103
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
Definition: ptarray.c:51
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:102
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWTRIANGLE * lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
Definition: lwgeom.c:224
#define TINTYPE
Definition: liblwgeom.h:116
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:107
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1097
#define POLYGONTYPE
Definition: liblwgeom.h:104
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:109
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
#define TRIANGLETYPE
Definition: liblwgeom.h:115
void * lwalloc(size_t size)
Definition: lwutil.c:227
LWCIRCSTRING * lwgeom_as_lwcircstring(const LWGEOM *lwgeom)
Definition: lwgeom.c:188
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:215
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:941
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:369
LWPOINT * lwpoint_make(int32_t srid, int hasz, int hasm, const POINT4D *p)
Definition: lwpoint.c:206
LWPOINT * lwline_get_lwpoint(const LWLINE *line, uint32_t where)
Returns freshly allocated LWPOINT that corresponds to the index where.
Definition: lwline.c:309
This library is the generic geometry handling section of PostGIS.
#define MAXDEPTH
Datum LWGEOM_dumpsegments(PG_FUNCTION_ARGS)
Datum LWGEOM_dumppoints(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(LWGEOM_dumppoints)
if(!(yy_init))
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:131
POINTARRAY * points
Definition: liblwgeom.h:507
uint32_t ngeoms
Definition: liblwgeom.h:580
LWGEOM ** geoms
Definition: liblwgeom.h:575
uint8_t type
Definition: liblwgeom.h:462
int32_t srid
Definition: liblwgeom.h:460
POINTARRAY * points
Definition: liblwgeom.h:483
POINTARRAY ** rings
Definition: liblwgeom.h:519
uint32_t nrings
Definition: liblwgeom.h:524
int32_t srid
Definition: liblwgeom.h:520
int32_t srid
Definition: liblwgeom.h:496
POINTARRAY * points
Definition: liblwgeom.h:495
uint32_t npoints
Definition: liblwgeom.h:427
uint32_t idx
LWGEOM * geom
Datum path[34]
LWGEOM * root
struct dumpnode stack[MAXDEPTH]
uint32_t ring