194 {
195 FuncCallContext *funcctx;
196 TupleDesc tupdesc;
199 int call_cntr;
200 int max_calls;
201
202
203 if (SRF_IS_FIRSTCALL()) {
204 MemoryContext oldcontext;
205 int numbands;
209 bool exclude_nodata_value =
TRUE;
210 int nElements;
211
213
214
215 funcctx = SRF_FIRSTCALL_INIT();
216
217
218 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
219
220
221 if (PG_ARGISNULL(0)) {
222 MemoryContextSwitchTo(oldcontext);
223 SRF_RETURN_DONE(funcctx);
224 }
225 pgraster = (
rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
227 if (!raster) {
228 PG_FREE_IF_COPY(pgraster, 0);
229 ereport(ERROR, (
230 errcode(ERRCODE_OUT_OF_MEMORY),
231 errmsg("Could not deserialize raster")
232 ));
233 MemoryContextSwitchTo(oldcontext);
234 SRF_RETURN_DONE(funcctx);
235 }
236
237 if (!PG_ARGISNULL(1))
238 nband = PG_GETARG_UINT32(1);
239 else
241
244
245 if (nband < 1 || nband > numbands) {
246 elog(NOTICE, "Invalid band index (must use 1-based). Returning empty set");
248 PG_FREE_IF_COPY(pgraster, 0);
249 MemoryContextSwitchTo(oldcontext);
250 SRF_RETURN_DONE(funcctx);
251 }
252
253 if (!PG_ARGISNULL(2))
254 exclude_nodata_value = PG_GETARG_BOOL(2);
255
256
260 PG_FREE_IF_COPY(pgraster, 0);
261 MemoryContextSwitchTo(oldcontext);
262 SRF_RETURN_DONE(funcctx);
263 }
264
265
266
272 PG_FREE_IF_COPY(pgraster, 0);
273 if (NULL == geomval) {
274 ereport(ERROR, (
275 errcode(ERRCODE_NO_DATA_FOUND),
276 errmsg("Could not polygonize raster")
277 ));
278 MemoryContextSwitchTo(oldcontext);
279 SRF_RETURN_DONE(funcctx);
280 }
281
283
284
285 funcctx->user_fctx = geomval;
286
287
288 funcctx->max_calls = nElements;
289
290
291 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
292 ereport(ERROR, (
293 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
294 errmsg("function returning record called in context that cannot accept type record")
295 ));
296 }
297
298 BlessTupleDesc(tupdesc);
299 funcctx->tuple_desc = tupdesc;
300
301 MemoryContextSwitchTo(oldcontext);
302 }
303
304
305 funcctx = SRF_PERCALL_SETUP();
306
307 call_cntr = funcctx->call_cntr;
308 max_calls = funcctx->max_calls;
309 tupdesc = funcctx->tuple_desc;
310 geomval2 = funcctx->user_fctx;
311
312
313 if (call_cntr < max_calls) {
316 HeapTuple tuple;
318
320 size_t gser_size = 0;
321
323
325
326
329
330 values[0] = PointerGetDatum(gser);
331 values[1] = Float8GetDatum(geomval2[call_cntr].val);
332
333
334 tuple = heap_form_tuple(tupdesc, values, nulls);
335
336
337 result = HeapTupleGetDatum(tuple);
338
339 SRF_RETURN_NEXT(funcctx,
result);
340 }
341
342 else {
343 pfree(geomval2);
344 SRF_RETURN_DONE(funcctx);
345 }
346}
char result[OUT_DOUBLE_BUFFER_SIZE]
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
void lwgeom_free(LWGEOM *geom)
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
rt_geomval rt_raster_gdal_polygonize(rt_raster raster, int nband, int exclude_nodata_value, int *pnElements)
Returns a set of "geomval" value, one for each group of pixel sharing the same value for the provided...
uint16_t rt_raster_get_num_bands(rt_raster raster)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)