PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ RASTER_fromGDALRaster()

Datum RASTER_fromGDALRaster ( PG_FUNCTION_ARGS  )

Definition at line 63 of file rtpg_gdal.c.

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

References clamp_srid(), FALSE, POSTGIS_RT_DEBUGF, 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: