PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum ST_Subdivide ( PG_FUNCTION_ARGS  )

Definition at line 328 of file lwgeom_dump.c.

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

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

Here is the call graph for this function: