PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ RASTER_fromGDALRaster()

Datum RASTER_fromGDALRaster ( PG_FUNCTION_ARGS  )

Definition at line 60 of file rtpg_gdal.c.

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

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.

Here is the call graph for this function: