PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ ST_Subdivide()

Datum ST_Subdivide ( PG_FUNCTION_ARGS  )

Definition at line 330 of file lwgeom_dump.c.

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

331 {
332 #if POSTGIS_GEOS_VERSION < 35
333 
334  elog(ERROR, "The GEOS version this PostGIS binary "
335  "was compiled against (%d) doesn't support "
336  "'%s' function (3.5.0+ required)",
337  POSTGIS_GEOS_VERSION, __func__);
338  PG_RETURN_NULL();
339 
340 #else /* POSTGIS_GEOS_VERSION >= 35 */
341 
342  typedef struct
343  {
344  int nextgeom;
345  int numgeoms;
346  LWCOLLECTION *col;
347  } collection_fctx;
348 
349  FuncCallContext *funcctx;
350  collection_fctx *fctx;
351  MemoryContext oldcontext;
352 
353  /* stuff done only on the first call of the function */
354  if (SRF_IS_FIRSTCALL())
355  {
356  GSERIALIZED *gser;
357  LWGEOM *geom;
358  LWCOLLECTION *col;
359  int maxvertices = 256;
360 
361  /* create a function context for cross-call persistence */
362  funcctx = SRF_FIRSTCALL_INIT();
363 
364  /*
365  * switch to memory context appropriate for multiple function calls
366  */
367  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
368 
369  /*
370  * Get the geometry value
371  */
372  gser = PG_GETARG_GSERIALIZED_P(0);
373  geom = lwgeom_from_gserialized(gser);
374 
375  /*
376  * Get the max vertices value
377  */
378  if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
379  maxvertices = PG_GETARG_INT32(1);
380 
381  /*
382  * Compute the subdivision of the geometry
383  */
384  col = lwgeom_subdivide(geom, maxvertices);
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 
418 #endif /* POSTGIS_GEOS_VERSION >= 35 */
419 }
#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:2054
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
Here is the call graph for this function: