PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ RASTER_fromGDALRaster()

Datum RASTER_fromGDALRaster ( PG_FUNCTION_ARGS  )

Definition at line 62 of file rtpg_gdal.c.

63 {
64  bytea *bytea_data;
65  uint8_t *data;
66  int data_len = 0;
67  VSILFILE *vsifp = NULL;
68  GDALDatasetH hdsSrc;
69  int32_t srid = -1; /* -1 for NULL */
70 
71  rt_pgraster *pgraster = NULL;
73 
74  /* NULL if NULL */
75  if (PG_ARGISNULL(0))
76  PG_RETURN_NULL();
77 
78  /* get data */
79  bytea_data = (bytea *) PG_GETARG_BYTEA_P(0);
80  data = (uint8_t *) VARDATA(bytea_data);
81  data_len = VARSIZE_ANY_EXHDR(bytea_data);
82 
83  /* process srid */
84  /* NULL srid means try to determine SRID from bytea */
85  if (!PG_ARGISNULL(1))
86  srid = clamp_srid(PG_GETARG_INT32(1));
87 
88  /* create memory "file" */
89  vsifp = VSIFileFromMemBuffer("/vsimem/in.dat", data, data_len, FALSE);
90  if (vsifp == NULL) {
91  PG_FREE_IF_COPY(bytea_data, 0);
92  elog(ERROR, "RASTER_fromGDALRaster: Could not load bytea into memory file for use by GDAL");
93  PG_RETURN_NULL();
94  }
95 
96  /* register all GDAL drivers */
98 
99  /* open GDAL raster */
100  hdsSrc = rt_util_gdal_open("/vsimem/in.dat", GA_ReadOnly, 1);
101  if (hdsSrc == NULL) {
102  VSIFCloseL(vsifp);
103  PG_FREE_IF_COPY(bytea_data, 0);
104  elog(ERROR, "RASTER_fromGDALRaster: Could not open bytea with GDAL. Check that the bytea is of a GDAL supported format");
105  PG_RETURN_NULL();
106  }
107 
108 #if POSTGIS_DEBUG_LEVEL > 3
109  {
110  GDALDriverH hdrv = GDALGetDatasetDriver(hdsSrc);
111 
112  POSTGIS_RT_DEBUGF(4, "Input GDAL Raster info: %s, (%d x %d)",
113  GDALGetDriverShortName(hdrv),
114  GDALGetRasterXSize(hdsSrc),
115  GDALGetRasterYSize(hdsSrc)
116  );
117  }
118 #endif
119 
120  /* convert GDAL raster to raster */
122 
123  GDALClose(hdsSrc);
124  VSIFCloseL(vsifp);
125  PG_FREE_IF_COPY(bytea_data, 0);
126 
127  if (raster == NULL) {
128  elog(ERROR, "RASTER_fromGDALRaster: Could not convert GDAL raster to raster");
129  PG_RETURN_NULL();
130  }
131 
132  /* apply SRID if set */
133  if (srid != -1)
134  rt_raster_set_srid(raster, srid);
135 
136  pgraster = rt_raster_serialize(raster);
138  if (!pgraster)
139  PG_RETURN_NULL();
140 
141  SET_VARSIZE(pgraster, pgraster->size);
142  PG_RETURN_POINTER(pgraster);
143 }
#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:342
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:389
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:69
Struct definitions.
Definition: librtcore.h:2403

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: