PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum RASTER_fromGDALRaster ( PG_FUNCTION_ARGS  )

Definition at line 11506 of file rt_pg.c.

References clamp_srid(), ovdump::data, FALSE, POSTGIS_RT_DEBUGF, rtrowdump::raster, rt_raster_destroy(), rt_raster_from_gdal_dataset(), rt_raster_serialize(), rt_raster_set_srid(), rt_util_gdal_open(), rt_util_gdal_register_all(), and rt_raster_serialized_t::size.

11507 {
11508  bytea *bytea_data;
11509  uint8_t *data;
11510  int data_len = 0;
11511  VSILFILE *vsifp = NULL;
11512  GDALDatasetH hdsSrc;
11513  int srid = -1; /* -1 for NULL */
11514 
11515  rt_pgraster *pgraster = NULL;
11516  rt_raster raster;
11517 
11518  /* NULL if NULL */
11519  if (PG_ARGISNULL(0))
11520  PG_RETURN_NULL();
11521 
11522  /* get data */
11523  bytea_data = (bytea *) PG_GETARG_BYTEA_P(0);
11524  data = (uint8_t *) VARDATA(bytea_data);
11525  data_len = VARSIZE(bytea_data) - VARHDRSZ;
11526 
11527  /* process srid */
11528  /* NULL srid means try to determine SRID from bytea */
11529  if (!PG_ARGISNULL(1))
11530  srid = clamp_srid(PG_GETARG_INT32(1));
11531 
11532  /* create memory "file" */
11533  vsifp = VSIFileFromMemBuffer("/vsimem/in.dat", data, data_len, FALSE);
11534  if (vsifp == NULL) {
11535  PG_FREE_IF_COPY(bytea_data, 0);
11536  elog(ERROR, "RASTER_fromGDALRaster: Could not load bytea into memory file for use by GDAL");
11537  PG_RETURN_NULL();
11538  }
11539 
11540  /* register all GDAL drivers */
11542 
11543  /* open GDAL raster */
11544  hdsSrc = rt_util_gdal_open("/vsimem/in.dat", GA_ReadOnly, 1);
11545  if (hdsSrc == NULL) {
11546  VSIFCloseL(vsifp);
11547  PG_FREE_IF_COPY(bytea_data, 0);
11548  elog(ERROR, "RASTER_fromGDALRaster: Could not open bytea with GDAL. Check that the bytea is of a GDAL supported format");
11549  PG_RETURN_NULL();
11550  }
11551 
11552 #if POSTGIS_DEBUG_LEVEL > 3
11553  {
11554  GDALDriverH hdrv = GDALGetDatasetDriver(hdsSrc);
11555 
11556  POSTGIS_RT_DEBUGF(4, "Input GDAL Raster info: %s, (%d x %d)",
11557  GDALGetDriverShortName(hdrv),
11558  GDALGetRasterXSize(hdsSrc),
11559  GDALGetRasterYSize(hdsSrc)
11560  );
11561  }
11562 #endif
11563 
11564  /* convert GDAL raster to raster */
11565  raster = rt_raster_from_gdal_dataset(hdsSrc);
11566 
11567  GDALClose(hdsSrc);
11568  VSIFCloseL(vsifp);
11569  PG_FREE_IF_COPY(bytea_data, 0);
11570 
11571  if (raster == NULL) {
11572  elog(ERROR, "RASTER_fromGDALRaster: Could not convert GDAL raster to raster");
11573  PG_RETURN_NULL();
11574  }
11575 
11576  /* apply SRID if set */
11577  if (srid != -1)
11578  rt_raster_set_srid(raster, srid);
11579 
11580  pgraster = rt_raster_serialize(raster);
11581  rt_raster_destroy(raster);
11582  if (!pgraster)
11583  PG_RETURN_NULL();
11584 
11585  SET_VARSIZE(pgraster, pgraster->size);
11586  PG_RETURN_POINTER(pgraster);
11587 }
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
Definition: rt_api.c:9339
int rt_util_gdal_register_all(int force_register_all)
Definition: rt_api.c:445
int clamp_srid(int srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
Definition: lwutil.c:326
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_api.c:8158
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_api.c:5387
tuple data
Definition: ovdump.py:103
tuple raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:123
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Definition: rt_api.c:5668
GDALDatasetH rt_util_gdal_open(const char *fn, GDALAccess fn_access, int shared)
Definition: rt_api.c:487
#define FALSE
Definition: dbfopen.c:169
Struct definitions.
Definition: rt_api.h:2175
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rt_pg.h:62

Here is the call graph for this function: