33 #include <utils/builtins.h> 34 #include "utils/lsyscache.h" 35 #include "utils/array.h" 36 #include "catalog/pg_type.h" 38 #include "../../postgis_config.h" 41 #include "access/htup_details.h" 66 VSILFILE *vsifp = NULL;
78 bytea_data = (bytea *) PG_GETARG_BYTEA_P(0);
79 data = (
uint8_t *) VARDATA(bytea_data);
80 data_len = VARSIZE(bytea_data) - VARHDRSZ;
88 vsifp = VSIFileFromMemBuffer(
"/vsimem/in.dat", data, data_len,
FALSE);
90 PG_FREE_IF_COPY(bytea_data, 0);
91 elog(ERROR,
"RASTER_fromGDALRaster: Could not load bytea into memory file for use by GDAL");
100 if (hdsSrc == NULL) {
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");
107 #if POSTGIS_DEBUG_LEVEL > 3 109 GDALDriverH hdrv = GDALGetDatasetDriver(hdsSrc);
112 GDALGetDriverShortName(hdrv),
113 GDALGetRasterXSize(hdsSrc),
114 GDALGetRasterYSize(hdsSrc)
124 PG_FREE_IF_COPY(bytea_data, 0);
126 if (raster == NULL) {
127 elog(ERROR,
"RASTER_fromGDALRaster: Could not convert GDAL raster to raster");
140 SET_VARSIZE(pgraster, pgraster->
size);
141 PG_RETURN_POINTER(pgraster);
153 text *formattext = NULL;
155 char **options = NULL;
156 text *optiontext = NULL;
173 uint64_t gdal_size = 0;
174 bytea *result = NULL;
175 uint64_t result_size = 0;
180 if (PG_ARGISNULL(0)) PG_RETURN_NULL();
181 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
185 PG_FREE_IF_COPY(pgraster, 0);
186 elog(ERROR,
"RASTER_asGDALRaster: Could not deserialize raster");
191 if (PG_ARGISNULL(1)) {
192 elog(NOTICE,
"Format must be provided");
194 PG_FREE_IF_COPY(pgraster, 0);
198 formattext = PG_GETARG_TEXT_P(1);
199 format = text_to_cstring(formattext);
205 if (!PG_ARGISNULL(2)) {
207 array = PG_GETARG_ARRAYTYPE_P(2);
208 etype = ARR_ELEMTYPE(array);
209 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
216 PG_FREE_IF_COPY(pgraster, 0);
217 elog(ERROR,
"RASTER_asGDALRaster: Invalid data type for options");
222 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
226 options = (
char **) palloc(
sizeof(
char *) * (n + 1));
227 if (options == NULL) {
229 PG_FREE_IF_COPY(pgraster, 0);
230 elog(ERROR,
"RASTER_asGDALRaster: Could not allocate memory for options");
235 for (i = 0, j = 0; i < n; i++) {
236 if (nulls[i])
continue;
241 optiontext = (text *) DatumGetPointer(e[i]);
242 if (NULL == optiontext)
break;
243 option = text_to_cstring(optiontext);
251 if (strlen(option)) {
252 options[j] = (
char *) palloc(
sizeof(
char) * (strlen(option) + 1));
260 options = repalloc(options, (j + 1) *
sizeof(
char *));
278 srid = PG_GETARG_INT32(3);
284 if (NULL != options) {
285 for (i = j - 1; i >= 0; i--) pfree(options[i]);
289 PG_FREE_IF_COPY(pgraster, 0);
290 elog(ERROR,
"RASTER_asGDALRaster: Could not find srtext for SRID (%d)", srid);
302 if (NULL != options) {
303 for (i = j - 1; i >= 0; i--) pfree(options[i]);
306 if (NULL != srs) pfree(srs);
308 PG_FREE_IF_COPY(pgraster, 0);
311 elog(ERROR,
"RASTER_asGDALRaster: Could not allocate and generate GDAL raster");
314 POSTGIS_RT_DEBUGF(3,
"RASTER_asGDALRaster: GDAL raster generated with %d bytes", (
int) gdal_size);
317 result_size = gdal_size + VARHDRSZ;
318 result = (bytea *) palloc(result_size);
319 if (NULL == result) {
320 elog(ERROR,
"RASTER_asGDALRaster: Insufficient virtual memory for GDAL raster");
323 SET_VARSIZE(result, result_size);
324 memcpy(VARDATA(result), gdal, VARSIZE(result) - VARHDRSZ);
334 if (gdal) CPLFree(gdal);
336 POSTGIS_RT_DEBUG(3,
"RASTER_asGDALRaster: Returning pointer to GDAL raster");
337 PG_RETURN_POINTER(result);
340 #define VALUES_LENGTH 6 348 FuncCallContext *funcctx;
358 if (SRF_IS_FIRSTCALL()) {
359 MemoryContext oldcontext;
362 funcctx = SRF_FIRSTCALL_INIT();
365 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
368 if (NULL == drv_set || !drv_count) {
369 elog(NOTICE,
"No GDAL drivers found");
370 MemoryContextSwitchTo(oldcontext);
371 SRF_RETURN_DONE(funcctx);
377 funcctx->user_fctx = drv_set;
380 funcctx->max_calls = drv_count;
383 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) {
385 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
387 "function returning record called in context " 388 "that cannot accept type record" 393 BlessTupleDesc(tupdesc);
394 funcctx->tuple_desc = tupdesc;
395 MemoryContextSwitchTo(oldcontext);
399 funcctx = SRF_PERCALL_SETUP();
401 call_cntr = funcctx->call_cntr;
402 max_calls = funcctx->max_calls;
403 tupdesc = funcctx->tuple_desc;
404 drv_set2 = funcctx->user_fctx;
407 if (call_cntr < max_calls) {
417 values[0] = Int32GetDatum(drv_set2[call_cntr].idx);
418 values[1] = CStringGetTextDatum(drv_set2[call_cntr].short_name);
419 values[2] = CStringGetTextDatum(drv_set2[call_cntr].long_name);
420 values[3] = CStringGetTextDatum(drv_set2[call_cntr].create_options);
423 POSTGIS_RT_DEBUGF(4,
"Result %d, Short Name %s", call_cntr, drv_set2[call_cntr].short_name);
424 POSTGIS_RT_DEBUGF(4,
"Result %d, Full Name %s", call_cntr, drv_set2[call_cntr].long_name);
425 POSTGIS_RT_DEBUGF(5,
"Result %d, Create Options %s", call_cntr, drv_set2[call_cntr].create_options);
428 tuple = heap_form_tuple(tupdesc, values, nulls);
431 result = HeapTupleGetDatum(tuple);
434 pfree(drv_set2[call_cntr].short_name);
435 pfree(drv_set2[call_cntr].long_name);
436 pfree(drv_set2[call_cntr].create_options);
438 SRF_RETURN_NEXT(funcctx, result);
443 SRF_RETURN_DONE(funcctx);
458 text *algtext = NULL;
459 char *algchar = NULL;
460 GDALResampleAlg alg = GRA_NearestNeighbour;
461 double max_err = 0.125;
464 char *src_srs = NULL;
466 char *dst_srs = NULL;
469 double scale[2] = {0};
470 double *scale_x = NULL;
471 double *scale_y = NULL;
473 double gridw[2] = {0};
474 double *grid_xw = NULL;
475 double *grid_yw = NULL;
477 double skew[2] = {0};
478 double *skew_x = NULL;
479 double *skew_y = NULL;
490 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
495 PG_FREE_IF_COPY(pgraster, 0);
496 elog(ERROR,
"RASTER_GDALWarp: Could not deserialize raster");
501 if (!PG_ARGISNULL(1)) {
502 algtext = PG_GETARG_TEXT_P(1);
509 if (!PG_ARGISNULL(2)) {
510 max_err = PG_GETARG_FLOAT8(2);
511 if (max_err < 0.) max_err = 0.;
520 if (!PG_ARGISNULL(3)) {
524 PG_FREE_IF_COPY(pgraster, 0);
525 elog(ERROR,
"RASTER_GDALWarp: %d is an invalid target SRID", dst_srid);
536 PG_FREE_IF_COPY(pgraster, 0);
537 elog(ERROR,
"RASTER_GDALWarp: Input raster has unknown (%d) SRID", src_srid);
541 else if (dst_srid == src_srid) {
546 if (!PG_ARGISNULL(4)) {
547 scale[0] = PG_GETARG_FLOAT8(4);
548 if (
FLT_NEQ(scale[0], 0)) scale_x = &scale[0];
552 if (!PG_ARGISNULL(5)) {
553 scale[1] = PG_GETARG_FLOAT8(5);
554 if (
FLT_NEQ(scale[1], 0)) scale_y = &scale[1];
558 if (!PG_ARGISNULL(6)) {
559 gridw[0] = PG_GETARG_FLOAT8(6);
564 if (!PG_ARGISNULL(7)) {
565 gridw[1] = PG_GETARG_FLOAT8(7);
570 if (!PG_ARGISNULL(8)) {
571 skew[0] = PG_GETARG_FLOAT8(8);
572 if (
FLT_NEQ(skew[0], 0)) skew_x = &skew[0];
576 if (!PG_ARGISNULL(9)) {
577 skew[1] = PG_GETARG_FLOAT8(9);
578 if (
FLT_NEQ(skew[1], 0)) skew_y = &skew[1];
582 if (!PG_ARGISNULL(10)) {
583 dim[0] = PG_GETARG_INT32(10);
584 if (dim[0] < 0) dim[0] = 0;
585 if (dim[0] > 0) dim_x = &dim[0];
589 if (!PG_ARGISNULL(11)) {
590 dim[1] = PG_GETARG_INT32(11);
591 if (dim[1] < 0) dim[1] = 0;
592 if (dim[1] > 0) dim_y = &dim[1];
598 (scale_x == NULL) && (scale_y == NULL) &&
599 (grid_xw == NULL) && (grid_yw == NULL) &&
600 (skew_x == NULL) && (skew_y == NULL) &&
601 (dim_x == NULL) && (dim_y == NULL)
603 elog(NOTICE,
"No resampling parameters provided. Returning original raster");
605 PG_RETURN_POINTER(pgraster);
609 (grid_xw != NULL && grid_yw == NULL) ||
610 (grid_xw == NULL && grid_yw != NULL)
612 elog(NOTICE,
"Values must be provided for both X and Y when specifying the alignment. Returning original raster");
614 PG_RETURN_POINTER(pgraster);
618 (scale_x != NULL && scale_y == NULL) ||
619 (scale_x == NULL && scale_y != NULL)
621 elog(NOTICE,
"Values must be provided for both X and Y when specifying the scale. Returning original raster");
623 PG_RETURN_POINTER(pgraster);
627 (scale_x != NULL || scale_y != NULL) &&
628 (dim_x != NULL || dim_y != NULL)
630 elog(NOTICE,
"Scale X/Y and width/height are mutually exclusive. Only provide one. Returning original raster");
632 PG_RETURN_POINTER(pgraster);
639 if (NULL == src_srs) {
641 PG_FREE_IF_COPY(pgraster, 0);
642 elog(ERROR,
"RASTER_GDALWarp: Input raster has unknown SRID (%d)", src_srid);
648 if (NULL == dst_srs) {
649 if (!no_srid) pfree(src_srs);
651 PG_FREE_IF_COPY(pgraster, 0);
652 elog(ERROR,
"RASTER_GDALWarp: Target SRID (%d) is unknown", dst_srid);
668 PG_FREE_IF_COPY(pgraster, 0);
674 elog(ERROR,
"RASTER_band: Could not create transformed raster");
684 if (NULL == pgrast) PG_RETURN_NULL();
688 SET_VARSIZE(pgrast, pgrast->
size);
689 PG_RETURN_POINTER(pgrast);
int clamp_srid(int srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
int rt_util_gdal_register_all(int force_register_all)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
rt_raster rt_raster_gdal_warp(rt_raster raster, const char *src_srs, const char *dst_srs, double *scale_x, double *scale_y, int *width, int *height, double *ul_xw, double *ul_yw, double *grid_xw, double *grid_yw, double *skew_x, double *skew_y, GDALResampleAlg resample_alg, double max_err)
Return a warped raster using GDAL Warp API.
Datum RASTER_GDALWarp(PG_FUNCTION_ARGS)
#define POSTGIS_RT_DEBUGF(level, msg,...)
Datum RASTER_asGDALRaster(PG_FUNCTION_ARGS)
#define SRID_UNKNOWN
Unknown SRID value.
char * rtpg_trim(const char *input)
Datum RASTER_getGDALDrivers(PG_FUNCTION_ARGS)
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
PG_FUNCTION_INFO_V1(RASTER_fromGDALRaster)
uint8_t * rt_raster_to_gdal(rt_raster raster, const char *srs, char *format, char **options, uint64_t *gdalsize)
Return formatted GDAL raster from raster.
rt_gdaldriver rt_raster_gdal_drivers(uint32_t *drv_count, uint8_t cancc)
Returns a set of available GDAL drivers.
Datum RASTER_fromGDALRaster(PG_FUNCTION_ARGS)
char * rtpg_getSR(int srid)
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
#define POSTGIS_RT_DEBUG(level, msg)
GDALResampleAlg rt_util_gdal_resample_alg(const char *algname)
Convert cstring name to GDAL Resample Algorithm.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
GDALDatasetH rt_util_gdal_open(const char *fn, GDALAccess fn_access, int shared)
char * rtpg_strtoupper(char *str)