PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ ST_Subdivide()

Datum ST_Subdivide ( PG_FUNCTION_ARGS  )

Definition at line 330 of file lwgeom_dump.c.

331 {
332  typedef struct
333  {
334  int nextgeom;
335  int numgeoms;
336  LWCOLLECTION *col;
337  } collection_fctx;
338 
339  FuncCallContext *funcctx;
340  collection_fctx *fctx;
341  MemoryContext oldcontext;
342 
343  /* stuff done only on the first call of the function */
344  if (SRF_IS_FIRSTCALL())
345  {
346  GSERIALIZED *gser;
347  LWGEOM *geom;
348  LWCOLLECTION *col;
349  int maxvertices = 256;
350 
351  /* create a function context for cross-call persistence */
352  funcctx = SRF_FIRSTCALL_INIT();
353 
354  /*
355  * switch to memory context appropriate for multiple function calls
356  */
357  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
358 
359  /*
360  * Get the geometry value
361  */
362  gser = PG_GETARG_GSERIALIZED_P(0);
363  geom = lwgeom_from_gserialized(gser);
364 
365  /*
366  * Get the max vertices value
367  */
368  if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
369  maxvertices = PG_GETARG_INT32(1);
370 
371  /*
372  * Compute the subdivision of the geometry
373  */
374  col = lwgeom_subdivide(geom, maxvertices);
375 
376  if ( ! col )
377  SRF_RETURN_DONE(funcctx);
378 
379  /* allocate memory for user context */
380  fctx = (collection_fctx *) palloc(sizeof(collection_fctx));
381 
382  /* initialize state */
383  fctx->nextgeom = 0;
384  fctx->numgeoms = col->ngeoms;
385  fctx->col = col;
386 
387  /* save user context, switch back to function context */
388  funcctx->user_fctx = fctx;
389  MemoryContextSwitchTo(oldcontext);
390  }
391 
392  /* stuff done on every call of the function */
393  funcctx = SRF_PERCALL_SETUP();
394  fctx = funcctx->user_fctx;
395 
396  if (fctx->nextgeom < fctx->numgeoms)
397  {
398  GSERIALIZED *gpart = geometry_serialize(fctx->col->geoms[fctx->nextgeom]);
399  fctx->nextgeom++;
400  SRF_RETURN_NEXT(funcctx, PointerGetDatum(gpart));
401  }
402  else
403  {
404  /* do when there is no more left */
405  SRF_RETURN_DONE(funcctx);
406  }
407 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
LWCOLLECTION * lwgeom_subdivide(const LWGEOM *geom, uint32_t maxvertices)
Definition: lwgeom.c:2448
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint32_t ngeoms
Definition: liblwgeom.h:510

References geometry_serialize(), lwgeom_from_gserialized(), lwgeom_subdivide(), and LWCOLLECTION::ngeoms.

Here is the call graph for this function: