PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ LWGEOM_dump_rings()

Datum LWGEOM_dump_rings ( PG_FUNCTION_ARGS  )

Definition at line 218 of file lwgeom_dump.c.

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

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 }
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:69
char * lwgeom_to_hexwkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Definition: lwout_wkb.c:848
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:86
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:138
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
POINTARRAY ** rings
Definition: liblwgeom.h:456
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:634
int nrings
Definition: liblwgeom.h:454
#define WKB_EXTENDED
Definition: liblwgeom.h:2048
LWPOLY * poly
Definition: lwgeom_dump.c:214
int32_t srid
Definition: liblwgeom.h:453
void * lwalloc(size_t size)
Definition: lwutil.c:227
Here is the call graph for this function: