PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ RASTER_asGDALRaster()

Datum RASTER_asGDALRaster ( PG_FUNCTION_ARGS  )

Definition at line 147 of file rtpg_gdal.c.

148 {
149  rt_pgraster *pgraster = NULL;
151 
152  text *formattext = NULL;
153  char *format = NULL;
154  char **options = NULL;
155  text *optiontext = NULL;
156  char *option = NULL;
157  int32_t srid = SRID_UNKNOWN;
158  char *srs = NULL;
159 
160  ArrayType *array;
161  Oid etype;
162  Datum *e;
163  bool *nulls;
164  int16 typlen;
165  bool typbyval;
166  char typalign;
167  int n = 0;
168  int i = 0;
169  int j = 0;
170 
171  uint8_t *gdal = NULL;
172  uint64_t gdal_size = 0;
173  bytea *result = NULL;
174  uint64_t result_size = 0;
175 
176  POSTGIS_RT_DEBUG(3, "RASTER_asGDALRaster: Starting");
177 
178  /* pgraster is null, return null */
179  if (PG_ARGISNULL(0)) PG_RETURN_NULL();
180  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
181 
182  raster = rt_raster_deserialize(pgraster, FALSE);
183  if (!raster) {
184  PG_FREE_IF_COPY(pgraster, 0);
185  elog(ERROR, "RASTER_asGDALRaster: Could not deserialize raster");
186  PG_RETURN_NULL();
187  }
188 
189  /* format is required */
190  if (PG_ARGISNULL(1)) {
191  elog(NOTICE, "Format must be provided");
193  PG_FREE_IF_COPY(pgraster, 0);
194  PG_RETURN_NULL();
195  }
196  else {
197  formattext = PG_GETARG_TEXT_P(1);
198  format = text_to_cstring(formattext);
199  }
200 
201  POSTGIS_RT_DEBUGF(3, "RASTER_asGDALRaster: Arg 1 (format) is %s", format);
202 
203  /* process options */
204  if (!PG_ARGISNULL(2)) {
205  POSTGIS_RT_DEBUG(3, "RASTER_asGDALRaster: Processing Arg 2 (options)");
206  array = PG_GETARG_ARRAYTYPE_P(2);
207  etype = ARR_ELEMTYPE(array);
208  get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
209 
210  switch (etype) {
211  case TEXTOID:
212  break;
213  default:
215  PG_FREE_IF_COPY(pgraster, 0);
216  elog(ERROR, "RASTER_asGDALRaster: Invalid data type for options");
217  PG_RETURN_NULL();
218  break;
219  }
220 
221  deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
222  &nulls, &n);
223 
224  if (n) {
225  options = (char **) palloc(sizeof(char *) * (n + 1));
226  if (options == NULL) {
228  PG_FREE_IF_COPY(pgraster, 0);
229  elog(ERROR, "RASTER_asGDALRaster: Could not allocate memory for options");
230  PG_RETURN_NULL();
231  }
232 
233  /* clean each option */
234  for (i = 0, j = 0; i < n; i++) {
235  if (nulls[i]) continue;
236 
237  option = NULL;
238  switch (etype) {
239  case TEXTOID:
240  optiontext = (text *) DatumGetPointer(e[i]);
241  if (NULL == optiontext) break;
242  option = text_to_cstring(optiontext);
243 
244  /* trim string */
245  option = rtpg_trim(option);
246  POSTGIS_RT_DEBUGF(3, "RASTER_asGDALRaster: option is '%s'", option);
247  break;
248  }
249 
250  if (strlen(option)) {
251  options[j] = (char *) palloc(sizeof(char) * (strlen(option) + 1));
252  strcpy(options[j], option);
253  j++;
254  }
255  }
256 
257  if (j > 0) {
258  /* trim allocation */
259  options = repalloc(options, (j + 1) * sizeof(char *));
260 
261  /* add NULL to end */
262  options[j] = NULL;
263 
264  }
265  else {
266  pfree(options);
267  options = NULL;
268  }
269  }
270  }
271 
272  /* process srid */
273  /* NULL srid means use raster's srid */
274  if (PG_ARGISNULL(3))
275  srid = rt_raster_get_srid(raster);
276  else
277  srid = PG_GETARG_INT32(3);
278 
279  /* get srs from srid */
280  if (clamp_srid(srid) != SRID_UNKNOWN) {
281  srs = rtpg_getSR(srid);
282  if (NULL == srs) {
283  if (NULL != options) {
284  for (i = j - 1; i >= 0; i--) pfree(options[i]);
285  pfree(options);
286  }
288  PG_FREE_IF_COPY(pgraster, 0);
289  elog(ERROR, "RASTER_asGDALRaster: Could not find srtext for SRID (%d)", srid);
290  PG_RETURN_NULL();
291  }
292  POSTGIS_RT_DEBUGF(3, "RASTER_asGDALRaster: Arg 3 (srs) is %s", srs);
293  }
294  else
295  srs = NULL;
296 
297  POSTGIS_RT_DEBUG(3, "RASTER_asGDALRaster: Generating GDAL raster");
298  gdal = rt_raster_to_gdal(raster, srs, format, options, &gdal_size);
299 
300  /* free memory */
301  if (NULL != options) {
302  for (i = j - 1; i >= 0; i--) pfree(options[i]);
303  pfree(options);
304  }
305  if (NULL != srs) pfree(srs);
307  PG_FREE_IF_COPY(pgraster, 0);
308 
309  if (!gdal) {
310  elog(ERROR, "RASTER_asGDALRaster: Could not allocate and generate GDAL raster");
311  PG_RETURN_NULL();
312  }
313  POSTGIS_RT_DEBUGF(3, "RASTER_asGDALRaster: GDAL raster generated with %d bytes", (int) gdal_size);
314 
315  /* result is a varlena */
316  result_size = gdal_size + VARHDRSZ;
317  result = (bytea *) palloc(result_size);
318  if (NULL == result) {
319  elog(ERROR, "RASTER_asGDALRaster: Insufficient virtual memory for GDAL raster");
320  PG_RETURN_NULL();
321  }
322  SET_VARSIZE(result, result_size);
323  memcpy(VARDATA(result), gdal, VARSIZE_ANY_EXHDR(result));
324 
325  /* free gdal mem buffer */
326  CPLFree(gdal);
327 
328  POSTGIS_RT_DEBUG(3, "RASTER_asGDALRaster: Returning pointer to GDAL raster");
329  PG_RETURN_POINTER(result);
330 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
#define FALSE
Definition: dbfopen.c:72
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:230
int32_t clamp_srid(int32_t srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
Definition: lwutil.c:333
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_raster.c:360
uint8_t * rt_raster_to_gdal(rt_raster raster, const char *srs, char *format, char **options, uint64_t *gdalsize)
Return formatted GDAL raster from raster.
Definition: rt_raster.c:1720
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:86
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
char * rtpg_getSR(int32_t srid)
char * rtpg_trim(const char *input)
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:69
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:73
Struct definitions.
Definition: librtcore.h:2396

References clamp_srid(), FALSE, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtrowdump::raster, result, rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_srid(), rt_raster_to_gdal(), rtpg_getSR(), rtpg_trim(), and SRID_UNKNOWN.

Here is the call graph for this function: