191 {
192 FuncCallContext *funcctx;
193 TupleDesc tupdesc;
196 int call_cntr;
197 int max_calls;
198
199
200 if (SRF_IS_FIRSTCALL()) {
201 MemoryContext oldcontext;
202 int numbands;
206 bool exclude_nodata_value =
TRUE;
207 int nElements;
208
210
211
212 funcctx = SRF_FIRSTCALL_INIT();
213
214
215 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
216
217
218 if (PG_ARGISNULL(0)) {
219 MemoryContextSwitchTo(oldcontext);
220 SRF_RETURN_DONE(funcctx);
221 }
222 pgraster = (
rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
224 if (!raster) {
225 PG_FREE_IF_COPY(pgraster, 0);
226 ereport(ERROR, (
227 errcode(ERRCODE_OUT_OF_MEMORY),
228 errmsg("Could not deserialize raster")
229 ));
230 MemoryContextSwitchTo(oldcontext);
231 SRF_RETURN_DONE(funcctx);
232 }
233
234 if (!PG_ARGISNULL(1))
235 nband = PG_GETARG_UINT32(1);
236 else
238
241
242 if (nband < 1 || nband > numbands) {
243 elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
245 PG_FREE_IF_COPY(pgraster, 0);
246 MemoryContextSwitchTo(oldcontext);
247 SRF_RETURN_DONE(funcctx);
248 }
249
250 if (!PG_ARGISNULL(2))
251 exclude_nodata_value = PG_GETARG_BOOL(2);
252
253
257 PG_FREE_IF_COPY(pgraster, 0);
258 MemoryContextSwitchTo(oldcontext);
259 SRF_RETURN_DONE(funcctx);
260 }
261
262
263
269 PG_FREE_IF_COPY(pgraster, 0);
270 if (NULL == geomval) {
271 ereport(ERROR, (
272 errcode(ERRCODE_NO_DATA_FOUND),
273 errmsg("Could not polygonize raster")
274 ));
275 MemoryContextSwitchTo(oldcontext);
276 SRF_RETURN_DONE(funcctx);
277 }
278
280
281
282 funcctx->user_fctx = geomval;
283
284
285 funcctx->max_calls = nElements;
286
287
288 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
289 ereport(ERROR, (
290 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
291 errmsg("function returning record called in context that cannot accept type record")
292 ));
293 }
294
295 BlessTupleDesc(tupdesc);
296 funcctx->tuple_desc = tupdesc;
297
298 MemoryContextSwitchTo(oldcontext);
299 }
300
301
302 funcctx = SRF_PERCALL_SETUP();
303
304 call_cntr = funcctx->call_cntr;
305 max_calls = funcctx->max_calls;
306 tupdesc = funcctx->tuple_desc;
307 geomval2 = funcctx->user_fctx;
308
309
310 if (call_cntr < max_calls) {
313 HeapTuple tuple;
314 Datum result;
315
317 size_t gser_size = 0;
318
320
322
323
326
327 values[0] = PointerGetDatum(gser);
328 values[1] = Float8GetDatum(geomval2[call_cntr].val);
329
330
331 tuple = heap_form_tuple(tupdesc, values, nulls);
332
333
334 result = HeapTupleGetDatum(tuple);
335
336 SRF_RETURN_NEXT(funcctx, result);
337 }
338
339 else {
340 pfree(geomval2);
341 SRF_RETURN_DONE(funcctx);
342 }
343}
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,...)