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

Definition at line 314 of file lwgeom_dump.c.

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

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

Here is the call graph for this function: