Return a warped raster using GDAL Warp API.
188 char *dst_options[] = {
"SUBCLASS=VRTWarpedDataset", NULL};
191 GDALRasterBandH
band;
194 GDALDataType gdal_pt = GDT_Unknown;
198 double dst_extent[4];
202 double _skew[2] = {0};
203 double _scale[2] = {0};
220 rterror(
"rt_raster_gdal_warp: Could not initialize internal variables");
229 if (max_err < 0.) max_err = 0.125;
233 if (src_srs != NULL) {
235 if (dst_srs != NULL && strcmp(src_srs, dst_srs) != 0) {
236 RASTER_DEBUG(4,
"Warp operation does include a reprojection");
241 rterror(
"rt_raster_gdal_warp: Could not convert srs values to GDAL accepted format");
248 RASTER_DEBUG(4,
"Warp operation does NOT include reprojection");
251 else if (dst_srs != NULL) {
253 rterror(
"rt_raster_gdal_warp: SRS required for input raster if SRS provided for warped raster");
260 if (NULL == arg->
src.
ds) {
261 rterror(
"rt_raster_gdal_warp: Could not convert raster to GDAL MEM format");
269 src_srs == NULL && dst_srs == NULL &&
274 #if POSTGIS_DEBUG_LEVEL > 3
275 GDALGetGeoTransform(arg->
src.
ds,
gt);
276 RASTER_DEBUGF(3,
"GDAL MEM geotransform: %f, %f, %f, %f, %f, %f",
282 RASTER_DEBUGF(3,
"raster geotransform: %f, %f, %f, %f, %f, %f",
289 double ngt[6] = {166021.4431, 0.1, 0, 10000000.0000, 0, -0.1};
291 rtwarn(
"Raster has default geotransform. Adjusting metadata for use of GDAL Warp API");
295 GDALSetGeoTransform(arg->
src.
ds, ngt);
296 GDALFlushCache(arg->
src.
ds);
302 #if POSTGIS_DEBUG_LEVEL > 3
303 GDALGetGeoTransform(arg->
src.
ds,
gt);
304 RASTER_DEBUGF(3,
"GDAL MEM geotransform: %f, %f, %f, %f, %f, %f",
314 if (NULL == arg->
transform.option.item) {
315 rterror(
"rt_raster_gdal_warp: Could not allocation memory for transform options");
319 memset(arg->
transform.option.item, 0,
sizeof(
char *) * (arg->
transform.option.len + 1));
321 for (i = 0; i < arg->
transform.option.len; i++) {
323 const char *lbl = i ?
"DST_SRS=" :
"SRC_SRS=";
324 size_t sz =
sizeof(char) * (strlen(lbl) + 1);
325 if ( srs ) sz += strlen(srs);
327 if (NULL == arg->
transform.option.item[i]) {
328 rterror(
"rt_raster_gdal_warp: Could not allocation memory for transform options");
332 sprintf(arg->
transform.option.item[i],
"%s%s", lbl, srs ? srs :
"");
340 arg->
transform.arg.transform = GDALCreateGenImgProjTransformer2(arg->
src.
ds, NULL, arg->
transform.option.item);
341 if (NULL == arg->
transform.arg.transform) {
342 rterror(
"rt_raster_gdal_warp: Could not create GDAL transformation object for output dataset creation");
348 cplerr = GDALSuggestedWarpOutput2(
349 arg->
src.
ds, GDALGenImgProjTransform,
350 arg->
transform.arg.transform, _gt, &(_dim[0]), &(_dim[1]), dst_extent, 0);
351 if (cplerr != CE_None) {
352 rterror(
"rt_raster_gdal_warp: Could not get GDAL suggested warp output for output dataset creation");
356 GDALDestroyGenImgProjTransformer(arg->
transform.arg.transform);
366 RASTER_DEBUGF(3,
"Suggested geotransform: %f, %f, %f, %f, %f, %f",
367 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
370 extent.
MinX = dst_extent[0];
371 extent.
MinY = dst_extent[1];
372 extent.
MaxX = dst_extent[2];
373 extent.
MaxY = dst_extent[3];
383 ((NULL != scale_x) || (NULL != scale_y)) &&
384 ((NULL != width) || (NULL != height))
386 rterror(
"rt_raster_gdal_warp: Scale X/Y and width/height are mutually exclusive. Only provide one");
393 _dim[0] = abs(*width);
394 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / ((
double) _dim[0]));
397 if (NULL != height) {
398 _dim[1] = abs(*height);
399 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / ((
double) _dim[1]));
404 ((NULL != scale_x) && (
FLT_NEQ(*scale_x, 0.0))) &&
405 ((NULL != scale_y) && (
FLT_NEQ(*scale_y, 0.0)))
407 _scale[0] = fabs(*scale_x);
408 _scale[1] = fabs(*scale_y);
421 ((NULL != scale_x) && (NULL == scale_y)) ||
422 ((NULL == scale_x) && (NULL != scale_y))
424 rterror(
"rt_raster_gdal_warp: Both X and Y scale values must be provided for scale");
432 _scale[0] = fabs(_gt[1]);
433 _scale[1] = fabs(_gt[5]);
436 RASTER_DEBUGF(4,
"Using scale: %f x %f", _scale[0], -1 * _scale[1]);
439 if (NULL != skew_x) {
453 if (NULL != skew_y) {
483 if (skewedrast == NULL) {
484 rterror(
"rt_raster_gdal_warp: Could not compute skewed raster");
490 _dim[0] = skewedrast->
width;
492 _dim[1] = skewedrast->
height;
502 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
504 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
509 rterror(
"rt_raster_gdal_warp: Out of memory allocating temporary raster");
520 RASTER_DEBUGF(3,
"Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
521 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
522 RASTER_DEBUGF(3,
"Temp raster's dimensions (width x height): %d x %d",
532 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
540 ((NULL != ul_xw) && (NULL == ul_yw)) ||
541 ((NULL == ul_xw) && (NULL != ul_yw))
543 rterror(
"rt_raster_gdal_warp: Both X and Y upper-left corner values must be provided");
552 (NULL != grid_xw) || (NULL != grid_yw)
557 ((NULL != grid_xw) && (NULL == grid_yw)) ||
558 ((NULL == grid_xw) && (NULL != grid_yw))
560 rterror(
"rt_raster_gdal_warp: Both X and Y alignment values must be provided");
566 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
574 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
589 rterror(
"rt_raster_gdal_warp: Could not compute raster pixel for spatial coordinates");
601 rterror(
"rt_raster_gdal_warp: Could not compute spatial coordinates for raster pixel");
612 else if (NULL == scale_x) {
624 rterror(
"rt_raster_gdal_warp: Could not compute spatial coordinates for raster pixel");
630 rast->scaleX = fabs((_c[0] - _w[0]) / ((
double)
rast->width));
636 else if (NULL == scale_y) {
648 rterror(
"rt_raster_gdal_warp: Could not compute spatial coordinates for raster pixel");
655 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double)
rast->height));
670 _dim[0] =
rast->width;
671 _dim[1] =
rast->height;
676 (NULL != scale_x) && (*scale_x < 0.)
678 (NULL != scale_y) && (*scale_y > 0)
693 rterror(
"rt_raster_gdal_warp: Could not compute spatial coordinates for raster pixel");
703 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0.0))
717 rterror(
"rt_raster_gdal_warp: Could not compute spatial coordinates for raster pixel");
727 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0.0))
735 RASTER_DEBUGF(3,
"Applied geotransform: %f, %f, %f, %f, %f, %f",
736 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
737 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d",
740 if ( _dim[0] == 0 || _dim[1] == 0 ) {
741 rterror(
"rt_raster_gdal_warp: The width (%d) or height (%d) of the warped raster is zero", _dim[0], _dim[1]);
752 arg->
dst.
drv = GDALGetDriverByName(
"VRT");
753 if (NULL == arg->
dst.
drv) {
754 rterror(
"rt_raster_gdal_warp: Could not load the output GDAL VRT driver");
760 arg->
dst.
ds = GDALCreate(arg->
dst.
drv,
"", _dim[0], _dim[1], 0, GDT_Byte, dst_options);
761 if (NULL == arg->
dst.
ds) {
762 rterror(
"rt_raster_gdal_warp: Could not create GDAL VRT dataset");
768 if (arg->
dst.
srs != NULL) {
769 cplerr = GDALSetProjection(arg->
dst.
ds, arg->
dst.
srs);
770 if (cplerr != CE_None) {
771 rterror(
"rt_raster_gdal_warp: Could not set projection");
779 cplerr = GDALSetGeoTransform(arg->
dst.
ds, _gt);
780 if (cplerr != CE_None) {
781 rterror(
"rt_raster_gdal_warp: Could not set geotransform");
788 for (i = 0; i < numBands; i++) {
790 if (NULL == rtband) {
791 rterror(
"rt_raster_gdal_warp: Could not get band %d for adding to VRT dataset", i);
798 if (gdal_pt == GDT_Unknown)
799 rtwarn(
"rt_raster_gdal_warp: Unknown pixel type for band %d", i);
801 cplerr = GDALAddBand(arg->
dst.
ds, gdal_pt, NULL);
802 if (cplerr != CE_None) {
803 rterror(
"rt_raster_gdal_warp: Could not add band to VRT dataset");
810 band = GDALGetRasterBand(arg->
dst.
ds, i + 1);
812 rterror(
"rt_raster_gdal_warp: Could not get GDAL band for additional processing");
820 if (GDALSetRasterNoDataValue(
band, nodata) != CE_None)
821 rtwarn(
"rt_raster_gdal_warp: Could not set nodata value for band %d", i);
822 RASTER_DEBUGF(3,
"nodata value set to %f", GDALGetRasterNoDataValue(
band, NULL));
827 arg->
transform.arg.transform = arg->
transform.arg.imgproj = GDALCreateGenImgProjTransformer2(
831 if (NULL == arg->
transform.arg.transform) {
832 rterror(
"rt_raster_gdal_warp: Could not create GDAL transformation object");
836 arg->
transform.func = GDALGenImgProjTransform;
840 arg->
transform.arg.transform = arg->
transform.arg.approx = GDALCreateApproxTransformer(
841 GDALGenImgProjTransform,
844 if (NULL == arg->
transform.arg.transform) {
845 rterror(
"rt_raster_gdal_warp: Could not create GDAL approximate transformation object");
850 arg->
transform.func = GDALApproxTransform;
854 arg->
wopts = GDALCreateWarpOptions();
855 if (NULL == arg->
wopts) {
856 rterror(
"rt_raster_gdal_warp: Could not create GDAL warp options object");
862 arg->
wopts->eResampleAlg = resample_alg;
867 arg->
wopts->papszWarpOptions = (
char **) CPLMalloc(
sizeof(
char *) * 2);
869 arg->
wopts->papszWarpOptions[0] = (
char *) CPLMalloc(
sizeof(
char) * (strlen(
"INIT_DEST=NO_DATA") + 1));
870 strcpy(arg->
wopts->papszWarpOptions[0],
"INIT_DEST=NO_DATA");
871 arg->
wopts->papszWarpOptions[1] = NULL;
874 arg->
wopts->nBandCount = numBands;
875 arg->
wopts->panSrcBands = (
int *) CPLMalloc(
sizeof(
int) * arg->
wopts->nBandCount);
876 arg->
wopts->panDstBands = (
int *) CPLMalloc(
sizeof(
int) * arg->
wopts->nBandCount);
877 for (i = 0; i < arg->
wopts->nBandCount; i++)
878 arg->
wopts->panDstBands[i] = arg->
wopts->panSrcBands[i] = i + 1;
892 arg->
wopts->padfSrcNoDataReal = (
double *) CPLMalloc(numBands *
sizeof(
double));
893 arg->
wopts->padfDstNoDataReal = (
double *) CPLMalloc(numBands *
sizeof(
double));
894 arg->
wopts->padfSrcNoDataImag = (
double *) CPLMalloc(numBands *
sizeof(
double));
895 arg->
wopts->padfDstNoDataImag = (
double *) CPLMalloc(numBands *
sizeof(
double));
897 NULL == arg->
wopts->padfSrcNoDataReal ||
898 NULL == arg->
wopts->padfDstNoDataReal ||
899 NULL == arg->
wopts->padfSrcNoDataImag ||
900 NULL == arg->
wopts->padfDstNoDataImag
902 rterror(
"rt_raster_gdal_warp: Out of memory allocating nodata mapping");
906 for (i = 0; i < numBands; i++) {
909 rterror(
"rt_raster_gdal_warp: Could not process bands for nodata values");
919 arg->
wopts->padfSrcNoDataReal[i] = -123456.789;
925 arg->
wopts->padfDstNoDataReal[i] = arg->
wopts->padfSrcNoDataReal[i];
926 arg->
wopts->padfDstNoDataImag[i] = arg->
wopts->padfSrcNoDataImag[i] = 0.0;
927 RASTER_DEBUGF(4,
"Mapped nodata value for band %d: %f (%f) => %f (%f)",
929 arg->
wopts->padfSrcNoDataReal[i], arg->
wopts->padfSrcNoDataImag[i],
930 arg->
wopts->padfDstNoDataReal[i], arg->
wopts->padfDstNoDataImag[i]
937 cplerr = GDALInitializeWarpedVRT(arg->
dst.
ds, arg->
wopts);
938 if (cplerr != CE_None) {
939 rterror(
"rt_raster_gdal_warp: Could not warp raster");
946 GDALFlushCache(arg->
dst.
ds);
956 rterror(
"rt_raster_gdal_warp: Could not warp raster");
962 double gt[6] = {0, 1, 0, 0, 0, -1};
967 gt[1] = _scale[0] * 10;
968 gt[5] = -1 * _scale[1] * 10;
#define SRID_UNKNOWN
Unknown SRID value.
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
#define RASTER_DEBUG(level, msg)
rt_errorstate rt_raster_cell_to_geopoint(rt_raster raster, double xr, double yr, double *xw, double *yw, double *gt)
Convert an xr, yr raster point to an xw, yw point on map.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
#define RASTER_DEBUGF(level, msg,...)
void rt_raster_set_geotransform_matrix(rt_raster raster, double *gt)
Set raster's geotransform using 6-element array.
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_errorstate rt_raster_geopoint_to_cell(rt_raster raster, double xw, double yw, double *xr, double *yr, double *igt)
Convert an xw, yw map point to a xr, yr raster point.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
rt_raster rt_raster_compute_skewed_raster(rt_envelope extent, double *skew, double *scale, double tolerance)
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
uint16_t rt_raster_get_num_bands(rt_raster raster)
int rt_util_gdal_driver_registered(const char *drv)
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
GDALDatasetH rt_raster_to_gdal_mem(rt_raster raster, const char *srs, uint32_t *bandNums, int *excludeNodataValues, int count, GDALDriverH *rtn_drv, int *destroy_rtn_drv)
Return GDAL dataset using GDAL MEM driver from raster.
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
static void _rti_warp_arg_destroy(_rti_warp_arg arg)
static _rti_warp_arg _rti_warp_arg_init()
struct _rti_warp_arg_t::@20 src
struct _rti_warp_arg_t::@20 dst