PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ST_Subdivide()

Datum ST_Subdivide ( PG_FUNCTION_ARGS  )

Definition at line 329 of file lwgeom_dump.c.

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

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

Here is the call graph for this function: