PostGIS  3.0.6dev-r@@SVN_REVISION@@
rtpg_utility.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 #include <postgres.h> /* for palloc */
31 #include <fmgr.h>
32 #include <utils/builtins.h>
33 
34 #include "../../postgis_config.h"
35 #include "lwgeom_pg.h"
36 
37 #define xstr(s) str(s)
38 #define str(s) #s
39 
40 #include "rtpostgis.h"
41 
42 Datum RASTER_lib_version(PG_FUNCTION_ARGS);
43 Datum RASTER_lib_build_date(PG_FUNCTION_ARGS);
44 Datum RASTER_gdal_version(PG_FUNCTION_ARGS);
45 Datum RASTER_minPossibleValue(PG_FUNCTION_ARGS);
46 
48 Datum RASTER_lib_version(PG_FUNCTION_ARGS)
49 {
50  char ver[64];
51  text *result;
52 
53  snprintf(ver, 64, "%s %s", POSTGIS_LIB_VERSION, xstr(POSTGIS_REVISION));
54  ver[63] = '\0';
55 
56  result = cstring_to_text(ver);
57  PG_RETURN_TEXT_P(result);
58 }
59 
61 Datum RASTER_lib_build_date(PG_FUNCTION_ARGS)
62 {
63  char *ver = POSTGIS_BUILD_DATE;
64  text *result;
65  result = palloc(VARHDRSZ + strlen(ver));
66  SET_VARSIZE(result, VARHDRSZ + strlen(ver));
67  memcpy(VARDATA(result), ver, strlen(ver));
68  PG_RETURN_POINTER(result);
69 }
70 
72 Datum RASTER_gdal_version(PG_FUNCTION_ARGS)
73 {
74  const char *ver = rt_util_gdal_version("--version");
75  text *result;
76 
77  /* add indicator if GDAL isn't configured right */
78  if (!rt_util_gdal_configured()) {
79  char *rtn = NULL;
80  size_t sz = strlen(ver) + strlen(" GDAL_DATA not found") + 1;
81  rtn = palloc(sz);
82  if (!rtn)
83  result = cstring_to_text(ver);
84  else {
85  snprintf(rtn, sz, "%s GDAL_DATA not found", ver);
86  result = cstring_to_text(rtn);
87  pfree(rtn);
88  }
89  }
90  else
91  result = cstring_to_text(ver);
92 
93  PG_RETURN_POINTER(result);
94 }
95 
97 Datum RASTER_minPossibleValue(PG_FUNCTION_ARGS)
98 {
99  text *pixeltypetext = NULL;
100  char *pixeltypechar = NULL;
101  rt_pixtype pixtype = PT_END;
102  double pixsize = 0;
103 
104  if (PG_ARGISNULL(0))
105  PG_RETURN_NULL();
106 
107  pixeltypetext = PG_GETARG_TEXT_P(0);
108  pixeltypechar = text_to_cstring(pixeltypetext);
109 
110  pixtype = rt_pixtype_index_from_name(pixeltypechar);
111  if (pixtype == PT_END) {
112  elog(ERROR, "RASTER_minPossibleValue: Invalid pixel type: %s", pixeltypechar);
113  PG_RETURN_NULL();
114  }
115 
116  pixsize = rt_pixtype_get_min_value(pixtype);
117 
118  /*
119  correct pixsize of unsigned pixel types
120  example: for PT_8BUI, the value is CHAR_MIN but if char is signed,
121  the value returned is -127 instead of 0.
122  */
123  switch (pixtype) {
124  case PT_1BB:
125  case PT_2BUI:
126  case PT_4BUI:
127  case PT_8BUI:
128  case PT_16BUI:
129  case PT_32BUI:
130  pixsize = 0;
131  break;
132  default:
133  break;
134  }
135 
136  PG_RETURN_FLOAT8(pixsize);
137 }
138 
140 Datum RASTER_memsize(PG_FUNCTION_ARGS);
142 Datum RASTER_memsize(PG_FUNCTION_ARGS)
143 {
144  void *detoasted = PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
145  size_t size = VARSIZE(detoasted);
146  PG_FREE_IF_COPY(detoasted,0);
147  PG_RETURN_INT32(size);
148 }
149 
150 
int rt_util_gdal_configured(void)
Definition: rt_util.c:317
rt_pixtype rt_pixtype_index_from_name(const char *pixname)
Definition: rt_pixel.c:80
rt_pixtype
Definition: librtcore.h:185
@ PT_32BUI
Definition: librtcore.h:194
@ PT_2BUI
Definition: librtcore.h:187
@ PT_END
Definition: librtcore.h:197
@ PT_4BUI
Definition: librtcore.h:188
@ PT_1BB
Definition: librtcore.h:186
@ PT_16BUI
Definition: librtcore.h:192
@ PT_8BUI
Definition: librtcore.h:190
const char * rt_util_gdal_version(const char *request)
Definition: rt_util.c:182
double rt_pixtype_get_min_value(rt_pixtype pixtype)
Return minimum value possible for pixel type.
Definition: rt_pixel.c:148
char * text_to_cstring(const text *textptr)
Datum RASTER_lib_version(PG_FUNCTION_ARGS)
Definition: rtpg_utility.c:48
Datum RASTER_lib_build_date(PG_FUNCTION_ARGS)
Definition: rtpg_utility.c:61
PG_FUNCTION_INFO_V1(RASTER_lib_version)
Datum RASTER_minPossibleValue(PG_FUNCTION_ARGS)
Definition: rtpg_utility.c:97
Datum RASTER_gdal_version(PG_FUNCTION_ARGS)
Definition: rtpg_utility.c:72
Datum RASTER_memsize(PG_FUNCTION_ARGS)
find the detoasted size of a value
Definition: rtpg_utility.c:142
#define xstr(s)
Definition: rtpg_utility.c:37
#define POSTGIS_LIB_VERSION
Definition: sqldefines.h:13