PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_fromGDALRaster()

Datum RASTER_fromGDALRaster ( PG_FUNCTION_ARGS  )

Definition at line 61 of file rtpg_gdal.c.

References clamp_srid(), ovdump::data, FALSE, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUGF, rtrowdump::raster, RASTER_asGDALRaster(), 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.

62 {
63  bytea *bytea_data;
64  uint8_t *data;
65  int data_len = 0;
66  VSILFILE *vsifp = NULL;
67  GDALDatasetH hdsSrc;
68  int srid = -1; /* -1 for NULL */
69 
70  rt_pgraster *pgraster = NULL;
72 
73  /* NULL if NULL */
74  if (PG_ARGISNULL(0))
75  PG_RETURN_NULL();
76 
77  /* get data */
78  bytea_data = (bytea *) PG_GETARG_BYTEA_P(0);
79  data = (uint8_t *) VARDATA(bytea_data);
80  data_len = VARSIZE(bytea_data) - VARHDRSZ;
81 
82  /* process srid */
83  /* NULL srid means try to determine SRID from bytea */
84  if (!PG_ARGISNULL(1))
85  srid = clamp_srid(PG_GETARG_INT32(1));
86 
87  /* create memory "file" */
88  vsifp = VSIFileFromMemBuffer("/vsimem/in.dat", data, data_len, FALSE);
89  if (vsifp == NULL) {
90  PG_FREE_IF_COPY(bytea_data, 0);
91  elog(ERROR, "RASTER_fromGDALRaster: Could not load bytea into memory file for use by GDAL");
92  PG_RETURN_NULL();
93  }
94 
95  /* register all GDAL drivers */
97 
98  /* open GDAL raster */
99  hdsSrc = rt_util_gdal_open("/vsimem/in.dat", GA_ReadOnly, 1);
100  if (hdsSrc == NULL) {
101  VSIFCloseL(vsifp);
102  PG_FREE_IF_COPY(bytea_data, 0);
103  elog(ERROR, "RASTER_fromGDALRaster: Could not open bytea with GDAL. Check that the bytea is of a GDAL supported format");
104  PG_RETURN_NULL();
105  }
106 
107 #if POSTGIS_DEBUG_LEVEL > 3
108  {
109  GDALDriverH hdrv = GDALGetDatasetDriver(hdsSrc);
110 
111  POSTGIS_RT_DEBUGF(4, "Input GDAL Raster info: %s, (%d x %d)",
112  GDALGetDriverShortName(hdrv),
113  GDALGetRasterXSize(hdsSrc),
114  GDALGetRasterYSize(hdsSrc)
115  );
116  }
117 #endif
118 
119  /* convert GDAL raster to raster */
120  raster = rt_raster_from_gdal_dataset(hdsSrc);
121 
122  GDALClose(hdsSrc);
123  VSIFCloseL(vsifp);
124  PG_FREE_IF_COPY(bytea_data, 0);
125 
126  if (raster == NULL) {
127  elog(ERROR, "RASTER_fromGDALRaster: Could not convert GDAL raster to raster");
128  PG_RETURN_NULL();
129  }
130 
131  /* apply SRID if set */
132  if (srid != -1)
133  rt_raster_set_srid(raster, srid);
134 
135  pgraster = rt_raster_serialize(raster);
136  rt_raster_destroy(raster);
137  if (!pgraster)
138  PG_RETURN_NULL();
139 
140  SET_VARSIZE(pgraster, pgraster->size);
141  PG_RETURN_POINTER(pgraster);
142 }
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:380
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
int rt_util_gdal_register_all(int force_register_all)
Definition: rt_util.c:334
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
data
Definition: ovdump.py:103
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Definition: rt_raster.c:363
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2201
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
Definition: rt_raster.c:2165
unsigned char uint8_t
Definition: uthash.h:79
GDALDatasetH rt_util_gdal_open(const char *fn, GDALAccess fn_access, int shared)
Definition: rt_util.c:379
Here is the call graph for this function: