PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ LWGEOM_dump_rings()

Datum LWGEOM_dump_rings ( PG_FUNCTION_ARGS  )

Definition at line 219 of file lwgeom_dump.c.

220 {
221  GSERIALIZED *pglwgeom;
222  LWGEOM *lwgeom;
223  FuncCallContext *funcctx;
224  struct POLYDUMPSTATE *state;
225  TupleDesc tupdesc;
226  HeapTuple tuple;
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  get_call_result_type(fcinfo, 0, &tupdesc);
260  BlessTupleDesc(tupdesc);
261 
262  /*
263  * generate attribute metadata needed later to produce
264  * tuples from raw C strings
265  */
266  funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);;
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 through 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_buffer(ringgeom, WKB_EXTENDED);
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 }
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:268
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:118
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:643
char * lwgeom_to_hexwkb_buffer(const LWGEOM *geom, uint8_t variant)
Definition: lwout_wkb.c:845
#define POLYGONTYPE
Definition: liblwgeom.h:104
#define WKB_EXTENDED
Definition: liblwgeom.h:2209
void * lwalloc(size_t size)
Definition: lwutil.c:227
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:215
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
POINTARRAY ** rings
Definition: liblwgeom.h:519
uint32_t nrings
Definition: liblwgeom.h:524
int32_t srid
Definition: liblwgeom.h:520
uint32_t ringnum
Definition: lwgeom_dump.c:214
LWPOLY * poly
Definition: lwgeom_dump.c:215

References gserialized_get_type(), lwalloc(), lwgeom_as_lwpoly(), lwgeom_from_gserialized(), lwgeom_to_hexwkb_buffer(), lwpoly_construct(), LWPOLY::nrings, POLYDUMPSTATE::poly, POLYGONTYPE, ptarray_clone_deep(), result, POLYDUMPSTATE::ringnum, LWPOLY::rings, LWPOLY::srid, and WKB_EXTENDED.

Here is the call graph for this function: