PostGIS  3.0.6dev-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 
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.
Definition: gserialized.c:239
LWCOLLECTION * lwgeom_subdivide(const LWGEOM *geom, uint32_t maxvertices)
Definition: lwgeom.c:2439
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint32_t ngeoms
Definition: liblwgeom.h:566

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

Here is the call graph for this function: