PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ ST_Subdivide()

Datum ST_Subdivide ( PG_FUNCTION_ARGS  )

Definition at line 322 of file lwgeom_dump.c.

323{
324 typedef struct
325 {
326 int nextgeom;
327 int numgeoms;
328 LWCOLLECTION *col;
329 } collection_fctx;
330
331 FuncCallContext *funcctx;
332 collection_fctx *fctx;
333 MemoryContext oldcontext;
334
335 /* stuff done only on the first call of the function */
336 if (SRF_IS_FIRSTCALL())
337 {
338 GSERIALIZED *gser;
339 LWGEOM *geom;
340 LWCOLLECTION *col;
341 /* default to maxvertices < page size */
342 int maxvertices = 128;
343 double gridSize = -1;
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 * Get the gridSize value
367 */
368 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
369 gridSize = PG_GETARG_FLOAT8(2);
370
371 /*
372 * Compute the subdivision of the geometry
373 */
374 col = lwgeom_subdivide_prec(geom, maxvertices, gridSize);
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.
LWCOLLECTION * lwgeom_subdivide_prec(const LWGEOM *geom, uint32_t maxvertices, double gridSize)
Definition lwgeom.c:2619
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: