199{
200 text *tablenametext = NULL;
201 char *tablename = NULL;
202 text *colnametext = NULL;
203 char *colname = NULL;
204 int32_t bandindex = 1;
205 bool exclude_nodata_value =
TRUE;
206 double sample = 0;
207
208 int len = 0;
210 int spi_result;
211 Portal portal;
212 TupleDesc tupdesc;
213 SPITupleTable *tuptable = NULL;
214 HeapTuple tuple;
215 Datum datum;
217
221 int num_bands = 0;
222 uint64_t cK = 0;
223 double cM = 0;
224 double cQ = 0;
227
230 Datum result;
231
232
233 if (PG_ARGISNULL(0)) {
234 elog(NOTICE, "Table name must be provided");
235 PG_RETURN_NULL();
236 }
237 tablenametext = PG_GETARG_TEXT_P(0);
239 if (!strlen(tablename)) {
240 elog(NOTICE, "Table name must be provided");
241 PG_RETURN_NULL();
242 }
243
244
245 if (PG_ARGISNULL(1)) {
246 elog(NOTICE, "Column name must be provided");
247 PG_RETURN_NULL();
248 }
249 colnametext = PG_GETARG_TEXT_P(1);
251 if (!strlen(colname)) {
252 elog(NOTICE, "Column name must be provided");
253 PG_RETURN_NULL();
254 }
255
256
257 if (!PG_ARGISNULL(2))
258 bandindex = PG_GETARG_INT32(2);
259
260
261 if (!PG_ARGISNULL(3))
262 exclude_nodata_value = PG_GETARG_BOOL(3);
263
264
265 if (!PG_ARGISNULL(4)) {
266 sample = PG_GETARG_FLOAT8(4);
267 if (sample < 0 || sample > 1) {
268 elog(NOTICE, "Invalid sample percentage (must be between 0 and 1). Returning NULL");
270 PG_RETURN_NULL();
271 }
272 else if (
FLT_EQ(sample, 0.0))
273 sample = 1;
274 }
275 else
276 sample = 1;
277
278
279
280 spi_result = SPI_connect();
281 if (spi_result != SPI_OK_CONNECT) {
282 pfree(sql);
283 elog(ERROR, "RASTER_summaryStatsCoverage: Cannot connect to database using SPI");
284 PG_RETURN_NULL();
285 }
286
287
288 len = sizeof(char) * (strlen("SELECT \"\" FROM \"\" WHERE \"\" IS NOT NULL") + (strlen(colname) * 2) + strlen(tablename) + 1);
289 sql = (
char *) palloc(len);
290 if (NULL == sql) {
291 if (SPI_tuptable) SPI_freetuptable(tuptable);
292 SPI_finish();
293 elog(ERROR, "RASTER_summaryStatsCoverage: Cannot allocate memory for sql");
294 PG_RETURN_NULL();
295 }
296
297
298 snprintf(sql, len, "SELECT \"%s\" FROM \"%s\" WHERE \"%s\" IS NOT NULL", colname, tablename, colname);
299 portal = SPI_cursor_open_with_args(
300 "coverage",
301 sql,
302 0, NULL,
303 NULL, NULL,
305 );
306 pfree(sql);
307
308
309 SPI_cursor_fetch(portal,
TRUE, 1);
310 while (SPI_processed == 1 && SPI_tuptable != NULL) {
311 tupdesc = SPI_tuptable->tupdesc;
312 tuple = SPI_tuptable->vals[0];
313
314 datum = SPI_getbinval(tuple, tupdesc, 1, &isNull);
315 if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
316 SPI_freetuptable(SPI_tuptable);
317 SPI_cursor_close(portal);
318 SPI_finish();
319
320 if (NULL != rtn) pfree(rtn);
321 elog(ERROR, "RASTER_summaryStatsCoverage: Cannot get raster of coverage");
322 PG_RETURN_NULL();
323 }
324 else if (isNull) {
325 SPI_cursor_fetch(portal,
TRUE, 1);
326 continue;
327 }
328
329 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(datum);
330
332 if (!raster) {
333 SPI_freetuptable(SPI_tuptable);
334 SPI_cursor_close(portal);
335 SPI_finish();
336
337 if (NULL != rtn) pfree(rtn);
338 elog(ERROR, "RASTER_summaryStatsCoverage: Cannot deserialize raster");
339 PG_RETURN_NULL();
340 }
341
342
344 if (bandindex < 1 || bandindex > num_bands) {
345 elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
346
348
349 SPI_freetuptable(SPI_tuptable);
350 SPI_cursor_close(portal);
351 SPI_finish();
352
353 if (NULL != rtn) pfree(rtn);
354 PG_RETURN_NULL();
355 }
356
357
359 if (!band) {
360 elog(NOTICE, "Cannot find band at index %d. Returning NULL", bandindex);
361
363
364 SPI_freetuptable(SPI_tuptable);
365 SPI_cursor_close(portal);
366 SPI_finish();
367
368 if (NULL != rtn) pfree(rtn);
369 PG_RETURN_NULL();
370 }
371
372
374
377
378 if (NULL == stats) {
379 elog(NOTICE, "Cannot compute summary statistics for band at index %d. Returning NULL", bandindex);
380
381 SPI_freetuptable(SPI_tuptable);
382 SPI_cursor_close(portal);
383 SPI_finish();
384
385 if (NULL != rtn) pfree(rtn);
386 PG_RETURN_NULL();
387 }
388
389
390 if (stats->
count > 0) {
391 if (NULL == rtn) {
393 if (NULL == rtn) {
394 SPI_freetuptable(SPI_tuptable);
395 SPI_cursor_close(portal);
396 SPI_finish();
397
398 elog(ERROR, "RASTER_summaryStatsCoverage: Cannot allocate memory for summary stats of coverage");
399 PG_RETURN_NULL();
400 }
401
409
412 }
413 else {
416
417 if (stats->
min < rtn->
min)
419 if (stats->
max > rtn->
max)
421 }
422 }
423
424 pfree(stats);
425
426
427 SPI_cursor_fetch(portal,
TRUE, 1);
428 }
429
430 if (SPI_tuptable) SPI_freetuptable(SPI_tuptable);
431 SPI_cursor_close(portal);
432 SPI_finish();
433
434 if (NULL == rtn) {
435 elog(ERROR, "RASTER_summaryStatsCoverage: Cannot compute coverage summary stats");
436 PG_RETURN_NULL();
437 }
438
439
441
444
445 else
447
448
449 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
450 ereport(ERROR, (
451 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
452 errmsg(
453 "function returning record called in context "
454 "that cannot accept type record"
455 )
456 ));
457 }
458
459 BlessTupleDesc(tupdesc);
460
462
463 values[0] = Int64GetDatum(rtn->
count);
464 if (rtn->
count > 0) {
465 values[1] = Float8GetDatum(rtn->
sum);
466 values[2] = Float8GetDatum(rtn->
mean);
467 values[3] = Float8GetDatum(rtn->
stddev);
468 values[4] = Float8GetDatum(rtn->
min);
469 values[5] = Float8GetDatum(rtn->
max);
470 }
471 else {
477 }
478
479
480 tuple = heap_form_tuple(tupdesc, values, nulls);
481
482
483 result = HeapTupleGetDatum(tuple);
484
485
486 pfree(rtn);
487
488 PG_RETURN_DATUM(result);
489}
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
struct rt_bandstats_t * rt_bandstats
void rt_band_destroy(rt_band band)
Destroy a raster band.
uint16_t rt_raster_get_num_bands(rt_raster raster)
rt_bandstats rt_band_get_summary_stats(rt_band band, int exclude_nodata_value, double sample, int inc_vals, uint64_t *cK, double *cM, double *cQ)
Compute summary statistics for a band.
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): ...
char * text_to_cstring(const text *textptr)