PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ LWGEOM_dumpsegments()

Datum LWGEOM_dumpsegments ( PG_FUNCTION_ARGS  )

Definition at line 301 of file lwgeom_dumppoints.c.

302 {
303  FuncCallContext *funcctx;
304  MemoryContext oldcontext, newcontext;
305 
306  GSERIALIZED *pglwgeom;
307  LWCOLLECTION *lwcoll;
308  LWGEOM *lwgeom;
309  struct dumpstate *state;
310  struct dumpnode *node;
311 
312  HeapTuple tuple;
313  Datum pathpt[2]; /* used to construct the composite return value */
314  bool isnull[2] = {0, 0}; /* needed to say neither value is null */
315  Datum result; /* the actual composite return value */
316 
317  if (SRF_IS_FIRSTCALL())
318  {
319  funcctx = SRF_FIRSTCALL_INIT();
320 
321  newcontext = funcctx->multi_call_memory_ctx;
322  oldcontext = MemoryContextSwitchTo(newcontext);
323 
324  pglwgeom = PG_GETARG_GSERIALIZED_P_COPY(0);
325  lwgeom = lwgeom_from_gserialized(pglwgeom);
326 
327  /* return early if nothing to do */
328  if (!lwgeom || lwgeom_is_empty(lwgeom))
329  {
330  MemoryContextSwitchTo(oldcontext);
331  funcctx = SRF_PERCALL_SETUP();
332  SRF_RETURN_DONE(funcctx);
333  }
334 
335  /* Create function state */
336  state = lwalloc(sizeof *state);
337  state->root = lwgeom;
338  state->stacklen = 0;
339  state->pathlen = 0;
340  state->pt = 0;
341  state->ring = 0;
342 
343  funcctx->user_fctx = state;
344 
345  /*
346  * Push a struct dumpnode on the state stack
347  */
348  state->stack[0].idx = 0;
349  state->stack[0].geom = lwgeom;
350  state->stacklen++;
351 
352  /*
353  * get tuple description for return type
354  */
355  if (get_call_result_type(fcinfo, 0, &funcctx->tuple_desc) != TYPEFUNC_COMPOSITE)
356  {
357  ereport(ERROR,
358  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
359  errmsg("set-valued function called in context that cannot accept a set")));
360  }
361 
362  BlessTupleDesc(funcctx->tuple_desc);
363 
364  /* get and cache data for constructing int4 arrays */
365  get_typlenbyvalalign(INT4OID, &state->typlen, &state->byval, &state->align);
366 
367  MemoryContextSwitchTo(oldcontext);
368  }
369 
370  /* stuff done on every call of the function */
371  funcctx = SRF_PERCALL_SETUP();
372  newcontext = funcctx->multi_call_memory_ctx;
373 
374  /* get state */
375  state = funcctx->user_fctx;
376 
377  while (1)
378  {
379  POINTARRAY *points = NULL;
380  LWLINE *line;
381  LWTRIANGLE *tri;
382  LWPOLY *poly;
383  POINT4D pt_start, pt_end;
384  POINTARRAY *segment_pa;
385  LWLINE *segment;
386 
387  node = &state->stack[state->stacklen - 1];
388  lwgeom = node->geom;
389 
390  if ( !lwgeom_is_empty(lwgeom) && (lwgeom->type == LINETYPE || lwgeom->type == TRIANGLETYPE || lwgeom->type == POLYGONTYPE) )
391  {
392  if (lwgeom->type == LINETYPE)
393  {
394  line = (LWLINE *)lwgeom;
395 
396  if (state->pt < line->points->npoints - 1)
397  {
398  points = line->points;
399  }
400  }
401  if (lwgeom->type == TRIANGLETYPE)
402  {
403  tri = (LWTRIANGLE *)lwgeom;
404 
405  if (state->pt == 0)
406  {
407  state->path[state->pathlen++] = Int32GetDatum(state->ring + 1);
408  }
409 
410  if (state->pt < 3)
411  {
412  points = tri->points;
413  }
414  else
415  {
416  state->pathlen--;
417  }
418  }
419  if (lwgeom->type == POLYGONTYPE)
420  {
421  poly = (LWPOLY *)lwgeom;
422 
423  if (state->pt == poly->rings[state->ring]->npoints)
424  {
425  state->pt = 0;
426  state->ring++;
427  state->pathlen--;
428  }
429 
430  if (state->ring < poly->nrings)
431  {
432  if (state->pt == 0)
433  {
434  state->path[state->pathlen] = Int32GetDatum(state->ring + 1);
435  state->pathlen++;
436  }
437 
438  if (state->pt < poly->rings[state->ring]->npoints - 1)
439  {
440  points = poly->rings[state->ring];
441  }
442  else
443  {
444  state->pt++;
445  continue;
446  }
447  }
448  }
449 
450  if (points)
451  {
452  getPoint4d_p(points, state->pt, &pt_start);
453  getPoint4d_p(points, state->pt + 1, &pt_end);
454 
455  segment_pa = ptarray_construct(lwgeom_has_z(lwgeom), lwgeom_has_m(lwgeom), 2);
456  ptarray_set_point4d(segment_pa, 0, &pt_start);
457  ptarray_set_point4d(segment_pa, 1, &pt_end);
458 
459  segment = lwline_construct(lwgeom->srid, NULL, segment_pa);
460 
461  state->pt++;
462 
463  state->path[state->pathlen] = Int32GetDatum(state->pt);
464  pathpt[0] = PointerGetDatum(construct_array(state->path,
465  state->pathlen + 1,
466  INT4OID,
467  state->typlen,
468  state->byval,
469  state->align));
470  pathpt[1] = PointerGetDatum(geometry_serialize((LWGEOM *)segment));
471 
472  tuple = heap_form_tuple(funcctx->tuple_desc, pathpt, isnull);
473  result = HeapTupleGetDatum(tuple);
474  SRF_RETURN_NEXT(funcctx, result);
475  }
476  else
477  {
478  if (--state->stacklen == 0)
479  SRF_RETURN_DONE(funcctx);
480  state->pathlen--;
481  continue;
482  }
483  }
484 
485  if (lwgeom->type == COLLECTIONTYPE || lwgeom->type == MULTILINETYPE ||
486  lwgeom->type == MULTIPOLYGONTYPE || lwgeom->type == TINTYPE)
487  {
488  lwcoll = (LWCOLLECTION *)node->geom;
489 
490  /* if a collection and we have more geoms */
491  if (node->idx < lwcoll->ngeoms)
492  {
493  if (state->stacklen > MAXDEPTH)
494  elog(ERROR, "Unable to dump overly nested collection");
495 
496  /* push the next geom on the path and the stack */
497  lwgeom = lwcoll->geoms[node->idx++];
498  state->path[state->pathlen++] = Int32GetDatum(node->idx);
499 
500  node = &state->stack[state->stacklen++];
501  node->idx = 0;
502  node->geom = lwgeom;
503 
504  state->pt = 0;
505  state->ring = 0;
506 
507  /* loop back to beginning, which will then check whatever node we just pushed */
508  continue;
509  }
510  }
511 
512  /* no more geometries in the current collection */
513  if (--state->stacklen == 0)
514  SRF_RETURN_DONE(funcctx);
515  state->pathlen--;
516  }
517 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
#define COLLECTIONTYPE
Definition: liblwgeom.h:123
#define MULTILINETYPE
Definition: liblwgeom.h:121
#define LINETYPE
Definition: liblwgeom.h:118
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
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
#define TINTYPE
Definition: liblwgeom.h:131
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:122
#define POLYGONTYPE
Definition: liblwgeom.h:119
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:126
#define TRIANGLETYPE
Definition: liblwgeom.h:130
void * lwalloc(size_t size)
Definition: lwutil.c:227
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:370
#define MAXDEPTH
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
uint32_t ngeoms
Definition: liblwgeom.h:595
LWGEOM ** geoms
Definition: liblwgeom.h:590
uint8_t type
Definition: liblwgeom.h:477
int32_t srid
Definition: liblwgeom.h:475
POINTARRAY * points
Definition: liblwgeom.h:498
POINTARRAY ** rings
Definition: liblwgeom.h:534
uint32_t nrings
Definition: liblwgeom.h:539
POINTARRAY * points
Definition: liblwgeom.h:510
uint32_t npoints
Definition: liblwgeom.h:442
uint32_t idx
LWGEOM * geom
Datum path[34]
LWGEOM * root
struct dumpnode stack[MAXDEPTH]
uint32_t ring

References dumpstate::align, dumpstate::byval, COLLECTIONTYPE, dumpnode::geom, LWCOLLECTION::geoms, getPoint4d_p(), dumpnode::idx, if(), LINETYPE, lwalloc(), lwgeom_from_gserialized(), lwgeom_has_m(), lwgeom_has_z(), lwgeom_is_empty(), lwline_construct(), MAXDEPTH, MULTILINETYPE, MULTIPOLYGONTYPE, LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, dumpstate::path, dumpstate::pathlen, LWLINE::points, LWTRIANGLE::points, POLYGONTYPE, dumpstate::pt, ptarray_construct(), ptarray_set_point4d(), result, dumpstate::ring, LWPOLY::rings, dumpstate::root, LWGEOM::srid, dumpstate::stack, dumpstate::stacklen, TINTYPE, TRIANGLETYPE, LWGEOM::type, and dumpstate::typlen.

Here is the call graph for this function: