PostGIS  2.2.8dev-r@@SVN_REVISION@@
rtpg_band_properties.c
Go to the documentation of this file.
1 /*
2  *
3  * WKTRaster - Raster Types for PostGIS
4  * http://trac.osgeo.org/postgis/wiki/WKTRaster
5  *
6  * Copyright (C) 2011-2013 Regents of the University of California
7  * <bkpark@ucdavis.edu>
8  * Copyright (C) 2010-2011 Jorge Arevalo <jorge.arevalo@deimos-space.com>
9  * Copyright (C) 2010-2011 David Zwarg <dzwarg@azavea.com>
10  * Copyright (C) 2009-2011 Pierre Racine <pierre.racine@sbf.ulaval.ca>
11  * Copyright (C) 2009-2011 Mateusz Loskot <mateusz@loskot.net>
12  * Copyright (C) 2008-2009 Sandro Santilli <strk@keybit.net>
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27  *
28  */
29 
30 #include <postgres.h>
31 #include <fmgr.h>
32 #include <funcapi.h>
33 #include <utils/builtins.h>
34 #include "utils/lsyscache.h" /* for get_typlenbyvalalign */
35 #include "utils/array.h" /* for ArrayType */
36 #include "catalog/pg_type.h" /* for INT2OID, INT4OID, FLOAT4OID, FLOAT8OID and TEXTOID */
37 
38 #include "../../postgis_config.h"
39 
40 #if POSTGIS_PGSQL_VERSION > 92
41 #include "access/htup_details.h" /* for heap_form_tuple() */
42 #endif
43 
44 #include "rtpostgis.h"
45 
46 /* Get all the properties of a raster band */
47 Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS);
48 Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS);
49 Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS);
50 Datum RASTER_getBandPath(PG_FUNCTION_ARGS);
51 Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS);
52 
53 /* get raster band's meta data */
54 Datum RASTER_bandmetadata(PG_FUNCTION_ARGS);
55 
56 /* Set all the properties of a raster band */
57 Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS);
58 Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS);
59 
65 Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS)
66 {
67  rt_pgraster *pgraster = NULL;
68  rt_raster raster = NULL;
69  rt_band band = NULL;
70  rt_pixtype pixtype;
71  int32_t bandindex;
72 
73  /* Deserialize raster */
74  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
75  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
76 
77  /* Index is 1-based */
78  bandindex = PG_GETARG_INT32(1);
79  if ( bandindex < 1 ) {
80  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
81  PG_FREE_IF_COPY(pgraster, 0);
82  PG_RETURN_NULL();
83  }
84 
85  raster = rt_raster_deserialize(pgraster, FALSE);
86  if ( ! raster ) {
87  PG_FREE_IF_COPY(pgraster, 0);
88  elog(ERROR, "RASTER_getBandPixelType: Could not deserialize raster");
89  PG_RETURN_NULL();
90  }
91 
92  /* Fetch requested band and its pixel type */
93  band = rt_raster_get_band(raster, bandindex - 1);
94  if ( ! band ) {
95  elog(NOTICE, "Could not find raster band of index %d when getting pixel type. Returning NULL", bandindex);
96  rt_raster_destroy(raster);
97  PG_FREE_IF_COPY(pgraster, 0);
98  PG_RETURN_NULL();
99  }
100 
101  pixtype = rt_band_get_pixtype(band);
102 
103  rt_raster_destroy(raster);
104  PG_FREE_IF_COPY(pgraster, 0);
105 
106  PG_RETURN_INT32(pixtype);
107 }
108 
115 Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
116 {
117  rt_pgraster *pgraster = NULL;
118  rt_raster raster = NULL;
119  rt_band band = NULL;
120  rt_pixtype pixtype;
121  int32_t bandindex;
122  const size_t name_size = 8; /* size of type name */
123  size_t size = 0;
124  char *ptr = NULL;
125  text *result = NULL;
126 
127  /* Deserialize raster */
128  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
129  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
130 
131  /* Index is 1-based */
132  bandindex = PG_GETARG_INT32(1);
133  if ( bandindex < 1 ) {
134  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
135  PG_FREE_IF_COPY(pgraster, 0);
136  PG_RETURN_NULL();
137  }
138 
139  raster = rt_raster_deserialize(pgraster, FALSE);
140  if ( ! raster ) {
141  PG_FREE_IF_COPY(pgraster, 0);
142  elog(ERROR, "RASTER_getBandPixelTypeName: Could not deserialize raster");
143  PG_RETURN_NULL();
144  }
145 
146  /* Fetch requested band and its pixel type */
147  band = rt_raster_get_band(raster, bandindex - 1);
148  if ( ! band ) {
149  elog(NOTICE, "Could not find raster band of index %d when getting pixel type name. Returning NULL", bandindex);
150  rt_raster_destroy(raster);
151  PG_FREE_IF_COPY(pgraster, 0);
152  PG_RETURN_NULL();
153  }
154 
155  pixtype = rt_band_get_pixtype(band);
156 
157  result = palloc(VARHDRSZ + name_size);
158  /* We don't need to check for NULL pointer, because if out of memory, palloc
159  * exit via elog(ERROR). It never returns NULL.
160  */
161 
162  memset(VARDATA(result), 0, name_size);
163  ptr = (char *)result + VARHDRSZ;
164  strcpy(ptr, rt_pixtype_name(pixtype));
165 
166  size = VARHDRSZ + strlen(ptr);
167  SET_VARSIZE(result, size);
168 
169  rt_raster_destroy(raster);
170  PG_FREE_IF_COPY(pgraster, 0);
171 
172  PG_RETURN_TEXT_P(result);
173 }
174 
180 Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS)
181 {
182  rt_pgraster *pgraster = NULL;
183  rt_raster raster = NULL;
184  rt_band band = NULL;
185  int32_t bandindex;
186  double nodata;
187 
188  /* Deserialize raster */
189  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
190  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
191 
192  /* Index is 1-based */
193  bandindex = PG_GETARG_INT32(1);
194  if ( bandindex < 1 ) {
195  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
196  PG_FREE_IF_COPY(pgraster, 0);
197  PG_RETURN_NULL();
198  }
199 
200  raster = rt_raster_deserialize(pgraster, FALSE);
201  if ( ! raster ) {
202  PG_FREE_IF_COPY(pgraster, 0);
203  elog(ERROR, "RASTER_getBandNoDataValue: Could not deserialize raster");
204  PG_RETURN_NULL();
205  }
206 
207  /* Fetch requested band and its nodata value */
208  band = rt_raster_get_band(raster, bandindex - 1);
209  if ( ! band ) {
210  elog(NOTICE, "Could not find raster band of index %d when getting band nodata value. Returning NULL", bandindex);
211  rt_raster_destroy(raster);
212  PG_FREE_IF_COPY(pgraster, 0);
213  PG_RETURN_NULL();
214  }
215 
216  if ( ! rt_band_get_hasnodata_flag(band) ) {
217  /* Raster does not have a nodata value set so we return NULL */
218  rt_raster_destroy(raster);
219  PG_FREE_IF_COPY(pgraster, 0);
220  PG_RETURN_NULL();
221  }
222 
223  rt_band_get_nodata(band, &nodata);
224 
225  rt_raster_destroy(raster);
226  PG_FREE_IF_COPY(pgraster, 0);
227 
228  PG_RETURN_FLOAT8(nodata);
229 }
230 
231 
233 Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS)
234 {
235  rt_pgraster *pgraster = NULL;
236  rt_raster raster = NULL;
237  rt_band band = NULL;
238  int32_t bandindex;
239  bool forcechecking = FALSE;
240  bool bandisnodata = FALSE;
241 
242  /* Index is 1-based */
243  bandindex = PG_GETARG_INT32(1);
244  if ( bandindex < 1 ) {
245  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
246  PG_RETURN_NULL();
247  }
248 
249  /* Deserialize raster */
250  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
251  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
252 
253  raster = rt_raster_deserialize(pgraster, FALSE);
254  if ( ! raster ) {
255  PG_FREE_IF_COPY(pgraster, 0);
256  elog(ERROR, "RASTER_bandIsNoData: Could not deserialize raster");
257  PG_RETURN_NULL();
258  }
259 
260  /* Fetch requested band and its nodata value */
261  band = rt_raster_get_band(raster, bandindex - 1);
262  if ( ! band ) {
263  elog(NOTICE, "Could not find raster band of index %d when determining if band is nodata. Returning NULL", bandindex);
264  rt_raster_destroy(raster);
265  PG_FREE_IF_COPY(pgraster, 0);
266  PG_RETURN_NULL();
267  }
268 
269  forcechecking = PG_GETARG_BOOL(2);
270 
271  bandisnodata = (forcechecking) ?
273 
274  rt_raster_destroy(raster);
275  PG_FREE_IF_COPY(pgraster, 0);
276 
277  PG_RETURN_BOOL(bandisnodata);
278 }
279 
284 Datum RASTER_getBandPath(PG_FUNCTION_ARGS)
285 {
286  rt_pgraster *pgraster = NULL;
287  rt_raster raster = NULL;
288  rt_band band = NULL;
289  int32_t bandindex;
290  const char *bandpath;
291  text *result;
292 
293  /* Index is 1-based */
294  bandindex = PG_GETARG_INT32(1);
295  if ( bandindex < 1 ) {
296  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
297  PG_RETURN_NULL();
298  }
299 
300  /* Deserialize raster */
301  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
302  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
303 
304  raster = rt_raster_deserialize(pgraster, FALSE);
305  if (!raster) {
306  PG_FREE_IF_COPY(pgraster, 0);
307  elog(ERROR, "RASTER_getBandPath: Could not deserialize raster");
308  PG_RETURN_NULL();
309  }
310 
311  /* Fetch requested band */
312  band = rt_raster_get_band(raster, bandindex - 1);
313  if (!band) {
314  elog(
315  NOTICE,
316  "Could not find raster band of index %d when getting band path. Returning NULL",
317  bandindex
318  );
319  rt_raster_destroy(raster);
320  PG_FREE_IF_COPY(pgraster, 0);
321  PG_RETURN_NULL();
322  }
323 
324  bandpath = rt_band_get_ext_path(band);
325  if (!bandpath) {
326  rt_band_destroy(band);
327  rt_raster_destroy(raster);
328  PG_FREE_IF_COPY(pgraster, 0);
329  PG_RETURN_NULL();
330  }
331 
332  result = (text *) palloc(VARHDRSZ + strlen(bandpath) + 1);
333  SET_VARSIZE(result, VARHDRSZ + strlen(bandpath) + 1);
334  strcpy((char *) VARDATA(result), bandpath);
335 
336  rt_band_destroy(band);
337  rt_raster_destroy(raster);
338  PG_FREE_IF_COPY(pgraster, 0);
339 
340  PG_RETURN_TEXT_P(result);
341 }
342 
347 Datum RASTER_bandmetadata(PG_FUNCTION_ARGS)
348 {
349  FuncCallContext *funcctx;
350  TupleDesc tupdesc;
351  int call_cntr;
352  int max_calls;
353 
354  struct bandmetadata {
355  uint32_t bandnum;
356  char *pixeltype;
357  bool hasnodata;
358  double nodataval;
359  bool isoutdb;
360  char *bandpath;
361  };
362  struct bandmetadata *bmd = NULL;
363  struct bandmetadata *bmd2 = NULL;
364 
365  HeapTuple tuple;
366  Datum result;
367 
368  if (SRF_IS_FIRSTCALL()) {
369  MemoryContext oldcontext;
370 
371  rt_pgraster *pgraster = NULL;
372  rt_raster raster = NULL;
373  rt_band band = NULL;
374 
375  ArrayType *array;
376  Oid etype;
377  Datum *e;
378  bool *nulls;
379  int16 typlen;
380  bool typbyval;
381  char typalign;
382  int i = 0;
383  int j = 0;
384  int n = 0;
385 
386  uint32_t numBands;
387  uint32_t idx = 1;
388  uint32_t *bandNums = NULL;
389  const char *tmp = NULL;
390 
391  POSTGIS_RT_DEBUG(3, "RASTER_bandmetadata: Starting");
392 
393  /* create a function context for cross-call persistence */
394  funcctx = SRF_FIRSTCALL_INIT();
395 
396  /* switch to memory context appropriate for multiple function calls */
397  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
398 
399  /* pgraster is null, return null */
400  if (PG_ARGISNULL(0)) {
401  MemoryContextSwitchTo(oldcontext);
402  SRF_RETURN_DONE(funcctx);
403  }
404  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
405 
406  /* raster */
407  raster = rt_raster_deserialize(pgraster, FALSE);
408  if (!raster) {
409  PG_FREE_IF_COPY(pgraster, 0);
410  MemoryContextSwitchTo(oldcontext);
411  elog(ERROR, "RASTER_bandmetadata: Could not deserialize raster");
412  SRF_RETURN_DONE(funcctx);
413  }
414 
415  /* numbands */
416  numBands = rt_raster_get_num_bands(raster);
417  if (numBands < 1) {
418  elog(NOTICE, "Raster provided has no bands");
419  rt_raster_destroy(raster);
420  PG_FREE_IF_COPY(pgraster, 0);
421  MemoryContextSwitchTo(oldcontext);
422  SRF_RETURN_DONE(funcctx);
423  }
424 
425  /* band index */
426  array = PG_GETARG_ARRAYTYPE_P(1);
427  etype = ARR_ELEMTYPE(array);
428  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
429 
430  switch (etype) {
431  case INT2OID:
432  case INT4OID:
433  break;
434  default:
435  rt_raster_destroy(raster);
436  PG_FREE_IF_COPY(pgraster, 0);
437  MemoryContextSwitchTo(oldcontext);
438  elog(ERROR, "RASTER_bandmetadata: Invalid data type for band number(s)");
439  SRF_RETURN_DONE(funcctx);
440  break;
441  }
442 
443  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
444  &nulls, &n);
445 
446  bandNums = palloc(sizeof(uint32_t) * n);
447  for (i = 0, j = 0; i < n; i++) {
448  if (nulls[i]) continue;
449 
450  switch (etype) {
451  case INT2OID:
452  idx = (uint32_t) DatumGetInt16(e[i]);
453  break;
454  case INT4OID:
455  idx = (uint32_t) DatumGetInt32(e[i]);
456  break;
457  }
458 
459  POSTGIS_RT_DEBUGF(3, "band idx (before): %d", idx);
460  if (idx > numBands || idx < 1) {
461  elog(NOTICE, "Invalid band index: %d. Indices must be 1-based. Returning NULL", idx);
462  pfree(bandNums);
463  rt_raster_destroy(raster);
464  PG_FREE_IF_COPY(pgraster, 0);
465  MemoryContextSwitchTo(oldcontext);
466  SRF_RETURN_DONE(funcctx);
467  }
468 
469  bandNums[j] = idx;
470  POSTGIS_RT_DEBUGF(3, "bandNums[%d] = %d", j, bandNums[j]);
471  j++;
472  }
473 
474  if (j < 1) {
475  j = numBands;
476  bandNums = repalloc(bandNums, sizeof(uint32_t) * j);
477  for (i = 0; i < j; i++)
478  bandNums[i] = i + 1;
479  }
480  else if (j < n)
481  bandNums = repalloc(bandNums, sizeof(uint32_t) * j);
482 
483  bmd = (struct bandmetadata *) palloc(sizeof(struct bandmetadata) * j);
484 
485  for (i = 0; i < j; i++) {
486  band = rt_raster_get_band(raster, bandNums[i] - 1);
487  if (NULL == band) {
488  elog(NOTICE, "Could not get raster band at index %d", bandNums[i]);
489  rt_raster_destroy(raster);
490  PG_FREE_IF_COPY(pgraster, 0);
491  MemoryContextSwitchTo(oldcontext);
492  SRF_RETURN_DONE(funcctx);
493  }
494 
495  /* bandnum */
496  bmd[i].bandnum = bandNums[i];
497 
498  /* pixeltype */
500  bmd[i].pixeltype = palloc(sizeof(char) * (strlen(tmp) + 1));
501  strncpy(bmd[i].pixeltype, tmp, strlen(tmp) + 1);
502 
503  /* hasnodatavalue */
504  if (rt_band_get_hasnodata_flag(band))
505  bmd[i].hasnodata = TRUE;
506  else
507  bmd[i].hasnodata = FALSE;
508 
509  /* nodatavalue */
510  if (bmd[i].hasnodata)
511  rt_band_get_nodata(band, &(bmd[i].nodataval));
512  else
513  bmd[i].nodataval = 0;
514 
515  /* path */
516  tmp = rt_band_get_ext_path(band);
517  if (tmp) {
518  bmd[i].bandpath = palloc(sizeof(char) * (strlen(tmp) + 1));
519  strncpy(bmd[i].bandpath, tmp, strlen(tmp) + 1);
520  }
521  else
522  bmd[i].bandpath = NULL;
523 
524  /* isoutdb */
525  bmd[i].isoutdb = bmd[i].bandpath ? TRUE : FALSE;
526 
527  rt_band_destroy(band);
528  }
529 
530  rt_raster_destroy(raster);
531  PG_FREE_IF_COPY(pgraster, 0);
532 
533  /* Store needed information */
534  funcctx->user_fctx = bmd;
535 
536  /* total number of tuples to be returned */
537  funcctx->max_calls = j;
538 
539  /* Build a tuple descriptor for our result type */
540  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
541  MemoryContextSwitchTo(oldcontext);
542  ereport(ERROR, (
543  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
544  errmsg(
545  "function returning record called in context "
546  "that cannot accept type record"
547  )
548  ));
549  }
550 
551  BlessTupleDesc(tupdesc);
552  funcctx->tuple_desc = tupdesc;
553 
554  MemoryContextSwitchTo(oldcontext);
555  }
556 
557  /* stuff done on every call of the function */
558  funcctx = SRF_PERCALL_SETUP();
559 
560  call_cntr = funcctx->call_cntr;
561  max_calls = funcctx->max_calls;
562  tupdesc = funcctx->tuple_desc;
563  bmd2 = funcctx->user_fctx;
564 
565  /* do when there is more left to send */
566  if (call_cntr < max_calls) {
567  int values_length = 5;
568  Datum values[values_length];
569  bool nulls[values_length];
570 
571  memset(nulls, FALSE, sizeof(bool) * values_length);
572 
573  values[0] = UInt32GetDatum(bmd2[call_cntr].bandnum);
574  values[1] = CStringGetTextDatum(bmd2[call_cntr].pixeltype);
575 
576  if (bmd2[call_cntr].hasnodata)
577  values[2] = Float8GetDatum(bmd2[call_cntr].nodataval);
578  else
579  nulls[2] = TRUE;
580 
581  values[3] = BoolGetDatum(bmd2[call_cntr].isoutdb);
582  if (bmd2[call_cntr].bandpath && strlen(bmd2[call_cntr].bandpath))
583  values[4] = CStringGetTextDatum(bmd2[call_cntr].bandpath);
584  else
585  nulls[4] = TRUE;
586 
587  /* build a tuple */
588  tuple = heap_form_tuple(tupdesc, values, nulls);
589 
590  /* make the tuple into a datum */
591  result = HeapTupleGetDatum(tuple);
592 
593  /* clean up */
594  pfree(bmd2[call_cntr].pixeltype);
595  if (bmd2[call_cntr].bandpath) pfree(bmd2[call_cntr].bandpath);
596 
597  SRF_RETURN_NEXT(funcctx, result);
598  }
599  /* do when there is no more left */
600  else {
601  pfree(bmd2);
602  SRF_RETURN_DONE(funcctx);
603  }
604 }
605 
610 Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
611 {
612  rt_pgraster *pgraster = NULL;
613  rt_pgraster *pgrtn = NULL;
614  rt_raster raster = NULL;
615  rt_band band = NULL;
616  double nodata;
617  int32_t bandindex;
618  bool forcechecking = FALSE;
619  bool skipset = FALSE;
620 
621  /* Deserialize raster */
622  if (PG_ARGISNULL(0))
623  PG_RETURN_NULL();
624  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
625 
626  /* Check index is not NULL or smaller than 1 */
627  if (PG_ARGISNULL(1))
628  bandindex = -1;
629  else
630  bandindex = PG_GETARG_INT32(1);
631  if (bandindex < 1) {
632  elog(NOTICE, "Invalid band index (must use 1-based). Nodata value not set. Returning original raster");
633  skipset = TRUE;
634  }
635 
636  raster = rt_raster_deserialize(pgraster, FALSE);
637  if (!raster) {
638  PG_FREE_IF_COPY(pgraster, 0);
639  elog(ERROR, "RASTER_setBandNoDataValue: Could not deserialize raster");
640  PG_RETURN_NULL();
641  }
642 
643  if (!skipset) {
644  /* Fetch requested band */
645  band = rt_raster_get_band(raster, bandindex - 1);
646  if (!band) {
647  elog(NOTICE, "Could not find raster band of index %d when setting pixel value. Nodata value not set. Returning original raster", bandindex);
648  }
649  else {
650  if (!PG_ARGISNULL(3))
651  forcechecking = PG_GETARG_BOOL(3);
652 
653  if (PG_ARGISNULL(2)) {
654  /* Set the hasnodata flag to FALSE */
656  POSTGIS_RT_DEBUGF(3, "Raster band %d does not have a nodata value", bandindex);
657  }
658  else {
659  /* Get the nodata value */
660  nodata = PG_GETARG_FLOAT8(2);
661 
662  /* Set the band's nodata value */
663  rt_band_set_nodata(band, nodata, NULL);
664 
665  /* Recheck all pixels if requested */
666  if (forcechecking)
668  }
669  }
670  }
671 
672  pgrtn = rt_raster_serialize(raster);
673  rt_raster_destroy(raster);
674  PG_FREE_IF_COPY(pgraster, 0);
675  if (!pgrtn)
676  PG_RETURN_NULL();
677 
678  SET_VARSIZE(pgrtn, pgrtn->size);
679  PG_RETURN_POINTER(pgrtn);
680 }
681 
683 Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS)
684 {
685  rt_pgraster *pgraster = NULL;
686  rt_pgraster *pgrtn = NULL;
687  rt_raster raster = NULL;
688  rt_band band = NULL;
689  int32_t bandindex;
690 
691  if (PG_ARGISNULL(0))
692  PG_RETURN_NULL();
693  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
694 
695  raster = rt_raster_deserialize(pgraster, FALSE);
696  if (!raster) {
697  PG_FREE_IF_COPY(pgraster, 0);
698  elog(ERROR, "RASTER_setBandIsNoData: Could not deserialize raster");
699  PG_RETURN_NULL();
700  }
701 
702  /* Check index is not NULL or smaller than 1 */
703  if (PG_ARGISNULL(1))
704  bandindex = -1;
705  else
706  bandindex = PG_GETARG_INT32(1);
707 
708  if (bandindex < 1)
709  elog(NOTICE, "Invalid band index (must use 1-based). Isnodata flag not set. Returning original raster");
710  else {
711  /* Fetch requested band */
712  band = rt_raster_get_band(raster, bandindex - 1);
713 
714  if (!band)
715  elog(NOTICE, "Could not find raster band of index %d. Isnodata flag not set. Returning original raster", bandindex);
716  else {
717  if (!rt_band_get_hasnodata_flag(band)) {
718  elog(NOTICE, "Band of index %d has no NODATA so cannot be NODATA. Returning original raster", bandindex);
719  }
720  /* Set the band's nodata value */
721  else {
722  rt_band_set_isnodata_flag(band, 1);
723  }
724  }
725  }
726 
727  /* Serialize raster again */
728  pgrtn = rt_raster_serialize(raster);
729  rt_raster_destroy(raster);
730  PG_FREE_IF_COPY(pgraster, 0);
731  if (!pgrtn) PG_RETURN_NULL();
732 
733  SET_VARSIZE(pgrtn, pgrtn->size);
734  PG_RETURN_POINTER(pgrtn);
735 }
736 
PG_FUNCTION_INFO_V1(RASTER_getBandPixelType)
Return pixel type of the specified band of raster.
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
band
Definition: ovdump.py:57
Datum RASTER_bandmetadata(PG_FUNCTION_ARGS)
Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
rt_errorstate rt_band_set_nodata(rt_band band, double val, int *converted)
Set nodata value.
Definition: rt_band.c:600
Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS)
rt_pixtype
Definition: librtcore.h:197
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:242
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:57
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1597
const char * rt_band_get_ext_path(rt_band band)
Return band&#39;s external path (only valid when rt_band_is_offline returns non-zero).
Definition: rt_band.c:265
Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS)
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
int rt_band_check_is_nodata(rt_band band)
Returns TRUE if the band is only nodata values.
Definition: rt_band.c:1619
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:541
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
void rt_band_set_hasnodata_flag(rt_band band, int flag)
Set hasnodata flag value.
Definition: rt_band.c:548
Datum RASTER_getBandPath(PG_FUNCTION_ARGS)
Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
rt_errorstate rt_band_set_isnodata_flag(rt_band band, int flag)
Set isnodata flag value.
Definition: rt_band.c:562
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2213
const char * rt_pixtype_name(rt_pixtype pixtype)
Definition: rt_pixel.c:110
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition: rt_band.c:498
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
Definition: rt_band.c:581
Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS)
Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS)
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:53
#define TRUE
Definition: dbfopen.c:169
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717