PostGIS  2.5.0beta2dev-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@kbt.io>
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 // For stat64()
31 #define _LARGEFILE64_SOURCE 1
32 
33 #include <postgres.h>
34 #include <fmgr.h>
35 #include <funcapi.h>
36 #include <utils/builtins.h> /* for text_to_cstring() */
37 #include "utils/lsyscache.h" /* for get_typlenbyvalalign */
38 #include "utils/array.h" /* for ArrayType */
39 #include "catalog/pg_type.h" /* for INT2OID, INT4OID, FLOAT4OID, FLOAT8OID and TEXTOID */
40 
41 #include "../../postgis_config.h"
42 
43 
44 #include "access/htup_details.h" /* for heap_form_tuple() */
45 
46 
47 #include "rtpostgis.h"
48 
49 extern bool enable_outdb_rasters;
50 
51 /* Get all the properties of a raster band */
52 Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS);
53 Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS);
54 Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS);
55 Datum RASTER_getBandPath(PG_FUNCTION_ARGS);
56 Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS);
57 
58 /* get raster band's meta data */
59 Datum RASTER_bandmetadata(PG_FUNCTION_ARGS);
60 
61 /* Set all the properties of a raster band */
62 Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS);
63 Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS);
64 Datum RASTER_setBandPath(PG_FUNCTION_ARGS);
65 Datum RASTER_setBandIndex(PG_FUNCTION_ARGS);
66 
72 Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS)
73 {
74  rt_pgraster *pgraster = NULL;
75  rt_raster raster = NULL;
76  rt_band band = NULL;
77  rt_pixtype pixtype;
78  int32_t bandindex;
79 
80  /* Deserialize raster */
81  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
82  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
83 
84  /* Index is 1-based */
85  bandindex = PG_GETARG_INT32(1);
86  if ( bandindex < 1 ) {
87  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
88  PG_FREE_IF_COPY(pgraster, 0);
89  PG_RETURN_NULL();
90  }
91 
92  raster = rt_raster_deserialize(pgraster, FALSE);
93  if ( ! raster ) {
94  PG_FREE_IF_COPY(pgraster, 0);
95  elog(ERROR, "RASTER_getBandPixelType: Could not deserialize raster");
96  PG_RETURN_NULL();
97  }
98 
99  /* Fetch requested band and its pixel type */
100  band = rt_raster_get_band(raster, bandindex - 1);
101  if ( ! band ) {
102  elog(NOTICE, "Could not find raster band of index %d when getting pixel type. Returning NULL", bandindex);
103  rt_raster_destroy(raster);
104  PG_FREE_IF_COPY(pgraster, 0);
105  PG_RETURN_NULL();
106  }
107 
108  pixtype = rt_band_get_pixtype(band);
109 
110  rt_raster_destroy(raster);
111  PG_FREE_IF_COPY(pgraster, 0);
112 
113  PG_RETURN_INT32(pixtype);
114 }
115 
122 Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
123 {
124  rt_pgraster *pgraster = NULL;
125  rt_raster raster = NULL;
126  rt_band band = NULL;
127  rt_pixtype pixtype;
128  int32_t bandindex;
129  const size_t name_size = 8; /* size of type name */
130  size_t size = 0;
131  char *ptr = NULL;
132  text *result = NULL;
133 
134  /* Deserialize raster */
135  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
136  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
137 
138  /* Index is 1-based */
139  bandindex = PG_GETARG_INT32(1);
140  if ( bandindex < 1 ) {
141  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
142  PG_FREE_IF_COPY(pgraster, 0);
143  PG_RETURN_NULL();
144  }
145 
146  raster = rt_raster_deserialize(pgraster, FALSE);
147  if ( ! raster ) {
148  PG_FREE_IF_COPY(pgraster, 0);
149  elog(ERROR, "RASTER_getBandPixelTypeName: Could not deserialize raster");
150  PG_RETURN_NULL();
151  }
152 
153  /* Fetch requested band and its pixel type */
154  band = rt_raster_get_band(raster, bandindex - 1);
155  if ( ! band ) {
156  elog(NOTICE, "Could not find raster band of index %d when getting pixel type name. Returning NULL", bandindex);
157  rt_raster_destroy(raster);
158  PG_FREE_IF_COPY(pgraster, 0);
159  PG_RETURN_NULL();
160  }
161 
162  pixtype = rt_band_get_pixtype(band);
163 
164  result = palloc(VARHDRSZ + name_size);
165  /* We don't need to check for NULL pointer, because if out of memory, palloc
166  * exit via elog(ERROR). It never returns NULL.
167  */
168 
169  memset(VARDATA(result), 0, name_size);
170  ptr = (char *)result + VARHDRSZ;
171  strcpy(ptr, rt_pixtype_name(pixtype));
172 
173  size = VARHDRSZ + strlen(ptr);
174  SET_VARSIZE(result, size);
175 
176  rt_raster_destroy(raster);
177  PG_FREE_IF_COPY(pgraster, 0);
178 
179  PG_RETURN_TEXT_P(result);
180 }
181 
187 Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS)
188 {
189  rt_pgraster *pgraster = NULL;
190  rt_raster raster = NULL;
191  rt_band band = NULL;
192  int32_t bandindex;
193  double nodata;
194 
195  /* Deserialize raster */
196  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
197  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
198 
199  /* Index is 1-based */
200  bandindex = PG_GETARG_INT32(1);
201  if ( bandindex < 1 ) {
202  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
203  PG_FREE_IF_COPY(pgraster, 0);
204  PG_RETURN_NULL();
205  }
206 
207  raster = rt_raster_deserialize(pgraster, FALSE);
208  if ( ! raster ) {
209  PG_FREE_IF_COPY(pgraster, 0);
210  elog(ERROR, "RASTER_getBandNoDataValue: Could not deserialize raster");
211  PG_RETURN_NULL();
212  }
213 
214  /* Fetch requested band and its nodata value */
215  band = rt_raster_get_band(raster, bandindex - 1);
216  if ( ! band ) {
217  elog(NOTICE, "Could not find raster band of index %d when getting band nodata value. Returning NULL", bandindex);
218  rt_raster_destroy(raster);
219  PG_FREE_IF_COPY(pgraster, 0);
220  PG_RETURN_NULL();
221  }
222 
223  if ( ! rt_band_get_hasnodata_flag(band) ) {
224  /* Raster does not have a nodata value set so we return NULL */
225  rt_raster_destroy(raster);
226  PG_FREE_IF_COPY(pgraster, 0);
227  PG_RETURN_NULL();
228  }
229 
230  rt_band_get_nodata(band, &nodata);
231 
232  rt_raster_destroy(raster);
233  PG_FREE_IF_COPY(pgraster, 0);
234 
235  PG_RETURN_FLOAT8(nodata);
236 }
237 
238 
240 Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS)
241 {
242  rt_pgraster *pgraster = NULL;
243  rt_raster raster = NULL;
244  rt_band band = NULL;
245  int32_t bandindex;
246  bool forcechecking = FALSE;
247  bool bandisnodata = FALSE;
248 
249  /* Index is 1-based */
250  bandindex = PG_GETARG_INT32(1);
251  if ( bandindex < 1 ) {
252  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
253  PG_RETURN_NULL();
254  }
255 
256  /* Deserialize raster */
257  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
258  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
259 
260  raster = rt_raster_deserialize(pgraster, FALSE);
261  if ( ! raster ) {
262  PG_FREE_IF_COPY(pgraster, 0);
263  elog(ERROR, "RASTER_bandIsNoData: Could not deserialize raster");
264  PG_RETURN_NULL();
265  }
266 
267  /* Fetch requested band and its nodata value */
268  band = rt_raster_get_band(raster, bandindex - 1);
269  if ( ! band ) {
270  elog(NOTICE, "Could not find raster band of index %d when determining if band is nodata. Returning NULL", bandindex);
271  rt_raster_destroy(raster);
272  PG_FREE_IF_COPY(pgraster, 0);
273  PG_RETURN_NULL();
274  }
275 
276  forcechecking = PG_GETARG_BOOL(2);
277 
278  bandisnodata = (forcechecking) ?
280 
281  rt_raster_destroy(raster);
282  PG_FREE_IF_COPY(pgraster, 0);
283 
284  PG_RETURN_BOOL(bandisnodata);
285 }
286 
291 Datum RASTER_getBandPath(PG_FUNCTION_ARGS)
292 {
293  rt_pgraster *pgraster = NULL;
294  rt_raster raster = NULL;
295  rt_band band = NULL;
296  int32_t bandindex;
297  const char *bandpath;
298  text *result;
299 
300  /* Index is 1-based */
301  bandindex = PG_GETARG_INT32(1);
302  if ( bandindex < 1 ) {
303  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
304  PG_RETURN_NULL();
305  }
306 
307  /* Deserialize raster */
308  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
309  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
310 
311  raster = rt_raster_deserialize(pgraster, FALSE);
312  if (!raster) {
313  PG_FREE_IF_COPY(pgraster, 0);
314  elog(ERROR, "RASTER_getBandPath: Could not deserialize raster");
315  PG_RETURN_NULL();
316  }
317 
318  /* Fetch requested band */
319  band = rt_raster_get_band(raster, bandindex - 1);
320  if (!band) {
321  elog(
322  NOTICE,
323  "Could not find raster band of index %d when getting band path. Returning NULL",
324  bandindex
325  );
326  rt_raster_destroy(raster);
327  PG_FREE_IF_COPY(pgraster, 0);
328  PG_RETURN_NULL();
329  }
330 
331  bandpath = rt_band_get_ext_path(band);
332  if (!bandpath) {
333  rt_band_destroy(band);
334  rt_raster_destroy(raster);
335  PG_FREE_IF_COPY(pgraster, 0);
336  PG_RETURN_NULL();
337  }
338 
339  result = cstring_to_text(bandpath);
340 
341  rt_band_destroy(band);
342  rt_raster_destroy(raster);
343  PG_FREE_IF_COPY(pgraster, 0);
344 
345  PG_RETURN_TEXT_P(result);
346 }
347 
348 
353 Datum RASTER_getBandFileSize(PG_FUNCTION_ARGS)
354 {
355  rt_pgraster *pgraster;
357  rt_band band = NULL;
358  int64_t fileSize;
359  int32_t bandindex;
360 
361  /* Index is 1-based */
362  bandindex = PG_GETARG_INT32(1);
363  if ( bandindex < 1 ) {
364  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
365  PG_RETURN_NULL();
366  }
367 
368  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
369  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
370 
371  raster = rt_raster_deserialize(pgraster, FALSE);
372  if ( ! raster ) {
373  PG_FREE_IF_COPY(pgraster, 0);
374  elog(ERROR, "RASTER_getFileSize: Could not deserialize raster");
375  PG_RETURN_NULL();
376  }
377 
378  /* Fetch requested band */
379  band = rt_raster_get_band(raster, bandindex - 1);
380  if (!band) {
381  elog(
382  NOTICE,
383  "Could not find raster band of index %d when getting band path. Returning NULL",
384  bandindex
385  );
386  rt_raster_destroy(raster);
387  PG_FREE_IF_COPY(pgraster, 0);
388  PG_RETURN_NULL();
389  }
390 
391  if (!rt_band_is_offline(band)) {
392  elog(NOTICE, "Band of index %d is not out-db.", bandindex);
393  rt_band_destroy(band);
394  rt_raster_destroy(raster);
395  PG_FREE_IF_COPY(pgraster, 0);
396  PG_RETURN_NULL();
397  }
398 
399  fileSize = rt_band_get_file_size(band);
400 
401  rt_band_destroy(band);
402  rt_raster_destroy(raster);
403  PG_FREE_IF_COPY(pgraster, 0);
404 
405  PG_RETURN_INT64(fileSize);
406 }
407 
412 Datum RASTER_getBandFileTimestamp(PG_FUNCTION_ARGS)
413 {
414  rt_pgraster *pgraster;
416  rt_band band = NULL;
417  int64_t fileSize;
418  int32_t bandindex;
419 
420  /* Index is 1-based */
421  bandindex = PG_GETARG_INT32(1);
422  if ( bandindex < 1 ) {
423  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
424  PG_RETURN_NULL();
425  }
426 
427  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
428  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
429 
430  raster = rt_raster_deserialize(pgraster, FALSE);
431  if ( ! raster ) {
432  PG_FREE_IF_COPY(pgraster, 0);
433  elog(ERROR, "RASTER_getBandFileTimestamp: Could not deserialize raster");
434  PG_RETURN_NULL();
435  }
436 
437  /* Fetch requested band */
438  band = rt_raster_get_band(raster, bandindex - 1);
439  if (!band) {
440  elog(
441  NOTICE,
442  "Could not find raster band of index %d when getting band path. Returning NULL",
443  bandindex
444  );
445  rt_raster_destroy(raster);
446  PG_FREE_IF_COPY(pgraster, 0);
447  PG_RETURN_NULL();
448  }
449 
450  if (!rt_band_is_offline(band)) {
451  elog(NOTICE, "Band of index %d is not out-db.", bandindex);
452  rt_band_destroy(band);
453  rt_raster_destroy(raster);
454  PG_FREE_IF_COPY(pgraster, 0);
455  PG_RETURN_NULL();
456  }
457 
458  fileSize = rt_band_get_file_timestamp(band);
459 
460  rt_band_destroy(band);
461  rt_raster_destroy(raster);
462  PG_FREE_IF_COPY(pgraster, 0);
463 
464  PG_RETURN_INT64(fileSize);
465 }
466 
471 Datum RASTER_bandmetadata(PG_FUNCTION_ARGS)
472 {
473  FuncCallContext *funcctx;
474  TupleDesc tupdesc;
475  int call_cntr;
476  int max_calls;
477 
478  struct bandmetadata {
479  uint32_t bandnum;
480  char *pixeltype;
481  bool hasnodata;
482  double nodataval;
483  bool isoutdb;
484  char *bandpath;
485  uint8_t extbandnum;
486  uint64_t filesize;
487  uint64_t timestamp;
488  };
489  struct bandmetadata *bmd = NULL;
490  struct bandmetadata *bmd2 = NULL;
491 
492  HeapTuple tuple;
493  Datum result;
494 
495  if (SRF_IS_FIRSTCALL()) {
496  MemoryContext oldcontext;
497 
498  rt_pgraster *pgraster = NULL;
499  rt_raster raster = NULL;
500  rt_band band = NULL;
501 
502  ArrayType *array;
503  Oid etype;
504  Datum *e;
505  bool *nulls;
506  int16 typlen;
507  bool typbyval;
508  char typalign;
509  int i = 0;
510  int j = 0;
511  int n = 0;
512 
513  uint32_t numBands;
514  uint32_t idx = 1;
515  uint32_t *bandNums = NULL;
516  const char *chartmp = NULL;
517  size_t charlen;
518  uint8_t extbandnum;
519 
520  POSTGIS_RT_DEBUG(3, "RASTER_bandmetadata: Starting");
521 
522  /* create a function context for cross-call persistence */
523  funcctx = SRF_FIRSTCALL_INIT();
524 
525  /* switch to memory context appropriate for multiple function calls */
526  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
527 
528  /* pgraster is null, return null */
529  if (PG_ARGISNULL(0)) {
530  MemoryContextSwitchTo(oldcontext);
531  SRF_RETURN_DONE(funcctx);
532  }
533  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
534 
535  /* raster */
536  raster = rt_raster_deserialize(pgraster, FALSE);
537  if (!raster) {
538  PG_FREE_IF_COPY(pgraster, 0);
539  MemoryContextSwitchTo(oldcontext);
540  elog(ERROR, "RASTER_bandmetadata: Could not deserialize raster");
541  SRF_RETURN_DONE(funcctx);
542  }
543 
544  /* numbands */
545  numBands = rt_raster_get_num_bands(raster);
546  if (numBands < 1) {
547  elog(NOTICE, "Raster provided has no bands");
548  rt_raster_destroy(raster);
549  PG_FREE_IF_COPY(pgraster, 0);
550  MemoryContextSwitchTo(oldcontext);
551  SRF_RETURN_DONE(funcctx);
552  }
553 
554  /* band index */
555  array = PG_GETARG_ARRAYTYPE_P(1);
556  etype = ARR_ELEMTYPE(array);
557  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
558 
559  switch (etype) {
560  case INT2OID:
561  case INT4OID:
562  break;
563  default:
564  rt_raster_destroy(raster);
565  PG_FREE_IF_COPY(pgraster, 0);
566  MemoryContextSwitchTo(oldcontext);
567  elog(ERROR, "RASTER_bandmetadata: Invalid data type for band number(s)");
568  SRF_RETURN_DONE(funcctx);
569  break;
570  }
571 
572  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
573  &nulls, &n);
574 
575  bandNums = palloc(sizeof(uint32_t) * n);
576  for (i = 0, j = 0; i < n; i++) {
577  if (nulls[i]) continue;
578 
579  switch (etype) {
580  case INT2OID:
581  idx = (uint32_t) DatumGetInt16(e[i]);
582  break;
583  case INT4OID:
584  idx = (uint32_t) DatumGetInt32(e[i]);
585  break;
586  }
587 
588  POSTGIS_RT_DEBUGF(3, "band idx (before): %d", idx);
589  if (idx > numBands || idx < 1) {
590  elog(NOTICE, "Invalid band index: %d. Indices must be 1-based. Returning NULL", idx);
591  pfree(bandNums);
592  rt_raster_destroy(raster);
593  PG_FREE_IF_COPY(pgraster, 0);
594  MemoryContextSwitchTo(oldcontext);
595  SRF_RETURN_DONE(funcctx);
596  }
597 
598  bandNums[j] = idx;
599  POSTGIS_RT_DEBUGF(3, "bandNums[%d] = %d", j, bandNums[j]);
600  j++;
601  }
602 
603  if (j < 1) {
604  j = numBands;
605  bandNums = repalloc(bandNums, sizeof(uint32_t) * j);
606  for (i = 0; i < j; i++)
607  bandNums[i] = i + 1;
608  }
609  else if (j < n)
610  bandNums = repalloc(bandNums, sizeof(uint32_t) * j);
611 
612  bmd = (struct bandmetadata *) palloc(sizeof(struct bandmetadata) * j);
613 
614  for (i = 0; i < j; i++) {
615  band = rt_raster_get_band(raster, bandNums[i] - 1);
616  if (NULL == band) {
617  elog(NOTICE, "Could not get raster band at index %d", bandNums[i]);
618  rt_raster_destroy(raster);
619  PG_FREE_IF_COPY(pgraster, 0);
620  MemoryContextSwitchTo(oldcontext);
621  SRF_RETURN_DONE(funcctx);
622  }
623 
624  /* bandnum */
625  bmd[i].bandnum = bandNums[i];
626 
627  /* pixeltype */
628  chartmp = rt_pixtype_name(rt_band_get_pixtype(band));
629  charlen = strlen(chartmp) + 1;
630  bmd[i].pixeltype = palloc(sizeof(char) * charlen);
631  strncpy(bmd[i].pixeltype, chartmp, charlen);
632 
633  /* hasnodatavalue */
634  if (rt_band_get_hasnodata_flag(band))
635  bmd[i].hasnodata = TRUE;
636  else
637  bmd[i].hasnodata = FALSE;
638 
639  /* nodatavalue */
640  if (bmd[i].hasnodata)
641  rt_band_get_nodata(band, &(bmd[i].nodataval));
642  else
643  bmd[i].nodataval = 0;
644 
645  /* out-db path */
646  chartmp = rt_band_get_ext_path(band);
647  if (chartmp) {
648  charlen = strlen(chartmp) + 1;
649  bmd[i].bandpath = palloc(sizeof(char) * charlen);
650  strncpy(bmd[i].bandpath, chartmp, charlen);
651  }
652  else
653  bmd[i].bandpath = NULL;
654 
655  /* isoutdb */
656  bmd[i].isoutdb = bmd[i].bandpath ? TRUE : FALSE;
657 
658  /* out-db bandnum */
659  if (rt_band_get_ext_band_num(band, &extbandnum) == ES_NONE)
660  bmd[i].extbandnum = extbandnum + 1;
661  else
662  bmd[i].extbandnum = 0;
663 
664  bmd[i].filesize = 0;
665  bmd[i].timestamp = 0;
666  if( bmd[i].bandpath && enable_outdb_rasters ) {
667  VSIStatBufL sStat;
668  if( VSIStatL(bmd[i].bandpath, &sStat) == 0 ) {
669  bmd[i].filesize = sStat.st_size;
670  bmd[i].timestamp = sStat.st_mtime;
671  }
672  }
673 
674  rt_band_destroy(band);
675  }
676 
677  rt_raster_destroy(raster);
678  PG_FREE_IF_COPY(pgraster, 0);
679 
680  /* Store needed information */
681  funcctx->user_fctx = bmd;
682 
683  /* total number of tuples to be returned */
684  funcctx->max_calls = j;
685 
686  /* Build a tuple descriptor for our result type */
687  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
688  MemoryContextSwitchTo(oldcontext);
689  ereport(ERROR, (
690  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
691  errmsg(
692  "function returning record called in context "
693  "that cannot accept type record"
694  )
695  ));
696  }
697 
698  BlessTupleDesc(tupdesc);
699  funcctx->tuple_desc = tupdesc;
700 
701  MemoryContextSwitchTo(oldcontext);
702  }
703 
704  /* stuff done on every call of the function */
705  funcctx = SRF_PERCALL_SETUP();
706 
707  call_cntr = funcctx->call_cntr;
708  max_calls = funcctx->max_calls;
709  tupdesc = funcctx->tuple_desc;
710  bmd2 = funcctx->user_fctx;
711 
712  /* do when there is more left to send */
713  if (call_cntr < max_calls) {
714  int values_length = 8;
715  Datum values[values_length];
716  bool nulls[values_length];
717 
718  memset(nulls, FALSE, sizeof(bool) * values_length);
719 
720  values[0] = UInt32GetDatum(bmd2[call_cntr].bandnum);
721  values[1] = CStringGetTextDatum(bmd2[call_cntr].pixeltype);
722 
723  if (bmd2[call_cntr].hasnodata)
724  values[2] = Float8GetDatum(bmd2[call_cntr].nodataval);
725  else
726  nulls[2] = TRUE;
727 
728  values[3] = BoolGetDatum(bmd2[call_cntr].isoutdb);
729  if (bmd2[call_cntr].bandpath && strlen(bmd2[call_cntr].bandpath)) {
730  values[4] = CStringGetTextDatum(bmd2[call_cntr].bandpath);
731  values[5] = UInt32GetDatum(bmd2[call_cntr].extbandnum);
732  }
733  else {
734  nulls[4] = TRUE;
735  nulls[5] = TRUE;
736  }
737 
738  if (bmd2[call_cntr].filesize) {
739 #if POSTGIS_PGSQL_VERSION > 95
740  values[6] = UInt64GetDatum(bmd2[call_cntr].filesize);
741  values[7] = UInt64GetDatum(bmd2[call_cntr].timestamp);
742 #else /* POSTGIS_PGSQL_VERSION <= 95 */
743  values[6] = Int64GetDatum(bmd2[call_cntr].filesize);
744  values[7] = Int64GetDatum(bmd2[call_cntr].timestamp);
745 #endif
746  }
747  else {
748  nulls[6] = TRUE;
749  nulls[7] = TRUE;
750  }
751 
752  /* build a tuple */
753  tuple = heap_form_tuple(tupdesc, values, nulls);
754 
755  /* make the tuple into a datum */
756  result = HeapTupleGetDatum(tuple);
757 
758  /* clean up */
759  pfree(bmd2[call_cntr].pixeltype);
760  if (bmd2[call_cntr].bandpath) pfree(bmd2[call_cntr].bandpath);
761 
762  SRF_RETURN_NEXT(funcctx, result);
763  }
764  /* do when there is no more left */
765  else {
766  pfree(bmd2);
767  SRF_RETURN_DONE(funcctx);
768  }
769 }
770 
775 Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
776 {
777  rt_pgraster *pgraster = NULL;
778  rt_pgraster *pgrtn = NULL;
779  rt_raster raster = NULL;
780  rt_band band = NULL;
781  double nodata;
782  int32_t bandindex;
783  bool forcechecking = FALSE;
784  bool skipset = FALSE;
785 
786  /* Deserialize raster */
787  if (PG_ARGISNULL(0))
788  PG_RETURN_NULL();
789  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
790 
791  /* Check index is not NULL or smaller than 1 */
792  if (PG_ARGISNULL(1))
793  bandindex = -1;
794  else
795  bandindex = PG_GETARG_INT32(1);
796  if (bandindex < 1) {
797  elog(NOTICE, "Invalid band index (must use 1-based). Nodata value not set. Returning original raster");
798  skipset = TRUE;
799  }
800 
801  raster = rt_raster_deserialize(pgraster, FALSE);
802  if (!raster) {
803  PG_FREE_IF_COPY(pgraster, 0);
804  elog(ERROR, "RASTER_setBandNoDataValue: Could not deserialize raster");
805  PG_RETURN_NULL();
806  }
807 
808  if (!skipset) {
809  /* Fetch requested band */
810  band = rt_raster_get_band(raster, bandindex - 1);
811  if (!band) {
812  elog(NOTICE, "Could not find raster band of index %d when setting pixel value. Nodata value not set. Returning original raster", bandindex);
813  }
814  else {
815  if (!PG_ARGISNULL(3))
816  forcechecking = PG_GETARG_BOOL(3);
817 
818  if (PG_ARGISNULL(2)) {
819  /* Set the hasnodata flag to FALSE */
821  POSTGIS_RT_DEBUGF(3, "Raster band %d does not have a nodata value", bandindex);
822  }
823  else {
824  /* Get the nodata value */
825  nodata = PG_GETARG_FLOAT8(2);
826 
827  /* Set the band's nodata value */
828  rt_band_set_nodata(band, nodata, NULL);
829 
830  /* Recheck all pixels if requested */
831  if (forcechecking)
833  }
834  }
835  }
836 
837  pgrtn = rt_raster_serialize(raster);
838  rt_raster_destroy(raster);
839  PG_FREE_IF_COPY(pgraster, 0);
840  if (!pgrtn)
841  PG_RETURN_NULL();
842 
843  SET_VARSIZE(pgrtn, pgrtn->size);
844  PG_RETURN_POINTER(pgrtn);
845 }
846 
851 Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS)
852 {
853  rt_pgraster *pgraster = NULL;
854  rt_pgraster *pgrtn = NULL;
855  rt_raster raster = NULL;
856  rt_band band = NULL;
857  int32_t bandindex;
858 
859  if (PG_ARGISNULL(0))
860  PG_RETURN_NULL();
861  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
862 
863  raster = rt_raster_deserialize(pgraster, FALSE);
864  if (!raster) {
865  PG_FREE_IF_COPY(pgraster, 0);
866  elog(ERROR, "RASTER_setBandIsNoData: Could not deserialize raster");
867  PG_RETURN_NULL();
868  }
869 
870  /* Check index is not NULL or smaller than 1 */
871  if (PG_ARGISNULL(1))
872  bandindex = -1;
873  else
874  bandindex = PG_GETARG_INT32(1);
875 
876  if (bandindex < 1)
877  elog(NOTICE, "Invalid band index (must use 1-based). Isnodata flag not set. Returning original raster");
878  else {
879  /* Fetch requested band */
880  band = rt_raster_get_band(raster, bandindex - 1);
881 
882  if (!band)
883  elog(NOTICE, "Could not find raster band of index %d. Isnodata flag not set. Returning original raster", bandindex);
884  else {
885  if (!rt_band_get_hasnodata_flag(band)) {
886  elog(NOTICE, "Band of index %d has no NODATA so cannot be NODATA. Returning original raster", bandindex);
887  }
888  /* Set the band's nodata value */
889  else {
890  rt_band_set_isnodata_flag(band, 1);
891  }
892  }
893  }
894 
895  /* Serialize raster again */
896  pgrtn = rt_raster_serialize(raster);
897  rt_raster_destroy(raster);
898  PG_FREE_IF_COPY(pgraster, 0);
899  if (!pgrtn) PG_RETURN_NULL();
900 
901  SET_VARSIZE(pgrtn, pgrtn->size);
902  PG_RETURN_POINTER(pgrtn);
903 }
904 
909 Datum RASTER_setBandPath(PG_FUNCTION_ARGS)
910 {
911  rt_pgraster *pgraster = NULL;
912  rt_pgraster *pgrtn = NULL;
913  rt_raster raster = NULL;
914  rt_band band = NULL;
915  int32_t bandindex = 1;
916  const char *outdbpathchar = NULL;
917  int32_t outdbindex = 1;
918  bool forceset = FALSE;
919  rt_band newband = NULL;
920 
921  int hasnodata;
922  double nodataval = 0.;
923 
924  if (PG_ARGISNULL(0))
925  PG_RETURN_NULL();
926  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
927 
928  raster = rt_raster_deserialize(pgraster, FALSE);
929  if (!raster) {
930  PG_FREE_IF_COPY(pgraster, 0);
931  elog(ERROR, "RASTER_setBandPath: Cannot deserialize raster");
932  PG_RETURN_NULL();
933  }
934 
935  /* Check index is not NULL or smaller than 1 */
936  if (!PG_ARGISNULL(1))
937  bandindex = PG_GETARG_INT32(1);
938 
939  if (bandindex < 1)
940  elog(NOTICE, "Invalid band index (must use 1-based). Returning original raster");
941  else {
942  /* Fetch requested band */
943  band = rt_raster_get_band(raster, bandindex - 1);
944 
945  if (!band)
946  elog(NOTICE, "Cannot find raster band of index %d. Returning original raster", bandindex);
947  else if (!rt_band_is_offline(band)) {
948  elog(NOTICE, "Band of index %d is not out-db. Returning original raster", bandindex);
949  }
950  else {
951  /* outdbpath */
952  if (!PG_ARGISNULL(2))
953  outdbpathchar = text_to_cstring(PG_GETARG_TEXT_P(2));
954  else
955  outdbpathchar = rt_band_get_ext_path(band);
956 
957  /* outdbindex, is 1-based */
958  if (!PG_ARGISNULL(3))
959  outdbindex = PG_GETARG_INT32(3);
960 
961  /* force */
962  if (!PG_ARGISNULL(4))
963  forceset = PG_GETARG_BOOL(4);
964 
965  hasnodata = rt_band_get_hasnodata_flag(band);
966  if (hasnodata)
967  rt_band_get_nodata(band, &nodataval);
968 
970  rt_raster_get_width(raster),
971  rt_raster_get_height(raster),
972  hasnodata,
973  nodataval,
974  outdbindex,
975  outdbpathchar,
976  forceset
977  );
978 
979  if (rt_raster_replace_band(raster, newband, bandindex - 1) == NULL)
980  elog(NOTICE, "Cannot change path of band. Returning original raster");
981  else
982  /* old band is in the variable band */
983  rt_band_destroy(band);
984  }
985  }
986 
987  /* Serialize raster again */
988  pgrtn = rt_raster_serialize(raster);
989  rt_raster_destroy(raster);
990  PG_FREE_IF_COPY(pgraster, 0);
991  if (!pgrtn) PG_RETURN_NULL();
992 
993  SET_VARSIZE(pgrtn, pgrtn->size);
994  PG_RETURN_POINTER(pgrtn);
995 }
char * text_to_cstring(const text *textptr)
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
bool enable_outdb_rasters
Definition: rt_band.c:417
uint16_t 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)
int rt_band_is_offline(rt_band band)
Return non-zero if the given band data is on the filesystem.
Definition: rt_band.c:329
rt_band rt_band_new_offline_from_path(uint16_t width, uint16_t height, int hasnodata, double nodataval, uint8_t bandNum, const char *path, int force)
Create an out-db rt_band from path.
Definition: rt_band.c:199
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:733
Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS)
rt_pixtype
Definition: librtcore.h:185
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:340
uint64_t rt_band_get_file_size(rt_band band)
Return file size in bytes.
Definition: rt_band.c:586
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1730
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:363
unsigned int uint32_t
Definition: uthash.h:78
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:1752
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:674
Datum RASTER_setBandPath(PG_FUNCTION_ARGS)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
rt_band rt_raster_replace_band(rt_raster raster, rt_band band, int index)
Replace band at provided index with new band.
Definition: rt_raster.c:1498
Datum RASTER_getBandFileTimestamp(PG_FUNCTION_ARGS)
rt_errorstate rt_band_get_ext_band_num(rt_band band, uint8_t *bandnum)
Return bands&#39; external band number (only valid when rt_band_is_offline returns non-zero).
Definition: rt_band.c:376
void rt_band_set_hasnodata_flag(rt_band band, int flag)
Set hasnodata flag value.
Definition: rt_band.c:681
Datum RASTER_getBandPath(PG_FUNCTION_ARGS)
Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:121
rt_errorstate rt_band_set_isnodata_flag(rt_band band, int flag)
Set isnodata flag value.
Definition: rt_band.c:695
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2250
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:631
uint64_t rt_band_get_file_timestamp(rt_band band)
Return file timestamp.
Definition: rt_band.c:608
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
Definition: rt_band.c:714
Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS)
Datum RASTER_getBandFileSize(PG_FUNCTION_ARGS)
Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS)
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
unsigned char uint8_t
Definition: uthash.h:79
Datum RASTER_setBandIndex(PG_FUNCTION_ARGS)
#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