PostGIS  2.5.7dev-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 #include "rtpostgis.h"
38 
39 Datum RASTER_lib_version(PG_FUNCTION_ARGS);
40 Datum RASTER_lib_build_date(PG_FUNCTION_ARGS);
41 Datum RASTER_gdal_version(PG_FUNCTION_ARGS);
42 Datum RASTER_minPossibleValue(PG_FUNCTION_ARGS);
43 
45 Datum RASTER_lib_version(PG_FUNCTION_ARGS)
46 {
47  char ver[64];
48  text *result;
49 
50  snprintf(ver, 64, "%s r%d", POSTGIS_LIB_VERSION, POSTGIS_SVN_REVISION);
51  ver[63] = '\0';
52 
53  result = cstring_to_text(ver);
54  PG_RETURN_TEXT_P(result);
55 }
56 
58 Datum RASTER_lib_build_date(PG_FUNCTION_ARGS)
59 {
60  char *ver = POSTGIS_BUILD_DATE;
61  text *result;
62  result = palloc(VARHDRSZ + strlen(ver));
63  SET_VARSIZE(result, VARHDRSZ + strlen(ver));
64  memcpy(VARDATA(result), ver, strlen(ver));
65  PG_RETURN_POINTER(result);
66 }
67 
69 Datum RASTER_gdal_version(PG_FUNCTION_ARGS)
70 {
71  const char *ver = rt_util_gdal_version("--version");
72  text *result;
73 
74  /* add indicator if GDAL isn't configured right */
75  if (!rt_util_gdal_configured()) {
76  char *rtn = NULL;
77  rtn = palloc(strlen(ver) + strlen(" GDAL_DATA not found") + 1);
78  if (!rtn)
79  result = cstring_to_text(ver);
80  else {
81  sprintf(rtn, "%s GDAL_DATA not found", ver);
82  result = cstring_to_text(rtn);
83  pfree(rtn);
84  }
85  }
86  else
87  result = cstring_to_text(ver);
88 
89  PG_RETURN_POINTER(result);
90 }
91 
93 Datum RASTER_minPossibleValue(PG_FUNCTION_ARGS)
94 {
95  text *pixeltypetext = NULL;
96  char *pixeltypechar = NULL;
97  rt_pixtype pixtype = PT_END;
98  double pixsize = 0;
99 
100  if (PG_ARGISNULL(0))
101  PG_RETURN_NULL();
102 
103  pixeltypetext = PG_GETARG_TEXT_P(0);
104  pixeltypechar = text_to_cstring(pixeltypetext);
105 
106  pixtype = rt_pixtype_index_from_name(pixeltypechar);
107  if (pixtype == PT_END) {
108  elog(ERROR, "RASTER_minPossibleValue: Invalid pixel type: %s", pixeltypechar);
109  PG_RETURN_NULL();
110  }
111 
112  pixsize = rt_pixtype_get_min_value(pixtype);
113 
114  /*
115  correct pixsize of unsigned pixel types
116  example: for PT_8BUI, the value is CHAR_MIN but if char is signed,
117  the value returned is -127 instead of 0.
118  */
119  switch (pixtype) {
120  case PT_1BB:
121  case PT_2BUI:
122  case PT_4BUI:
123  case PT_8BUI:
124  case PT_16BUI:
125  case PT_32BUI:
126  pixsize = 0;
127  break;
128  default:
129  break;
130  }
131 
132  PG_RETURN_FLOAT8(pixsize);
133 }
134 
136 Datum RASTER_memsize(PG_FUNCTION_ARGS);
138 Datum RASTER_memsize(PG_FUNCTION_ARGS)
139 {
140  void *detoasted = PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
141  size_t size = VARSIZE(detoasted);
142  PG_FREE_IF_COPY(detoasted,0);
143  PG_RETURN_INT32(size);
144 }
145 
146 
int rt_util_gdal_configured(void)
Definition: rt_util.c:315
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:180
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:45
Datum RASTER_lib_build_date(PG_FUNCTION_ARGS)
Definition: rtpg_utility.c:58
PG_FUNCTION_INFO_V1(RASTER_lib_version)
Datum RASTER_minPossibleValue(PG_FUNCTION_ARGS)
Definition: rtpg_utility.c:93
Datum RASTER_gdal_version(PG_FUNCTION_ARGS)
Definition: rtpg_utility.c:69
Datum RASTER_memsize(PG_FUNCTION_ARGS)
find the detoasted size of a value
Definition: rtpg_utility.c:138
#define POSTGIS_LIB_VERSION
Definition: sqldefines.h:12