PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ ST_Subdivide()

Datum ST_Subdivide ( PG_FUNCTION_ARGS  )

Definition at line 332 of file lwgeom_dump.c.

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

References lwgeom_from_gserialized(), lwgeom_subdivide_prec(), and LWCOLLECTION::ngeoms.

Here is the call graph for this function: