PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ ST_Subdivide()

Datum ST_Subdivide ( PG_FUNCTION_ARGS  )

Definition at line 328 of file lwgeom_dump.c.

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

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