PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ RASTER_asGDALRaster()

Datum RASTER_asGDALRaster ( PG_FUNCTION_ARGS  )

Definition at line 150 of file rtpg_gdal.c.

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

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: