Return a warped raster using GDAL Warp API.
186 {
187 CPLErr cplerr;
188 char *dst_options[] = {"SUBCLASS=VRTWarpedDataset", NULL};
190
191 GDALRasterBandH
band;
194 GDALDataType gdal_pt = GDT_Unknown;
195 double nodata = 0.0;
196
197 double _gt[6] = {0};
198 double dst_extent[4];
200
201 int _dim[2] = {0};
202 double _skew[2] = {0};
203 double _scale[2] = {0};
204 int ul_user = 0;
205
207 int i = 0;
208 int numBands = 0;
209
210
211 int subspatial = 0;
212
214
215 assert(NULL != raster);
216
217
219 if (arg == NULL) {
220 rterror(
"rt_raster_gdal_warp: Could not initialize internal variables");
221 return NULL;
222 }
223
224
225
226
227
228
229 if (max_err < 0.) max_err = 0.125;
231
232
233 if (src_srs != NULL) {
234
235 if (dst_srs != NULL && strcmp(src_srs, dst_srs) != 0) {
236 RASTER_DEBUG(4,
"Warp operation does include a reprojection");
239
241 rterror(
"rt_raster_gdal_warp: Could not convert srs values to GDAL accepted format");
243 return NULL;
244 }
245 }
246
247 else {
248 RASTER_DEBUG(4,
"Warp operation does NOT include reprojection");
249 }
250 }
251 else if (dst_srs != NULL) {
252
253 rterror(
"rt_raster_gdal_warp: SRS required for input raster if SRS provided for warped raster");
255 return NULL;
256 }
257
258
260 if (NULL == arg->
src.
ds) {
261 rterror(
"rt_raster_gdal_warp: Could not convert raster to GDAL MEM format");
263 return NULL;
264 }
266
267
268 if (
269 src_srs == NULL && dst_srs == NULL &&
271 ) {
273
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",
277 gt[0], gt[1], gt[2], gt[3], gt[4], gt[5]);
278#endif
279
280
282 RASTER_DEBUGF(3,
"raster geotransform: %f, %f, %f, %f, %f, %f",
283 gt[0], gt[1], gt[2], gt[3], gt[4], gt[5]);
284
285
288 {
289 double ngt[6] = {166021.4431, 0.1, 0, 10000000.0000, 0, -0.1};
290
291 rtwarn(
"Raster has default geotransform. Adjusting metadata for use of GDAL Warp API");
292
293 subspatial = 1;
294
295 GDALSetGeoTransform(arg->
src.
ds, ngt);
296 GDALFlushCache(arg->
src.
ds);
297
298
301
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",
305 gt[0], gt[1], gt[2], gt[3], gt[4], gt[5]);
306#endif
307 }
308 }
309
310
314 if (NULL == arg->
transform.option.item) {
315 rterror(
"rt_raster_gdal_warp: Could not allocation memory for transform options");
317 return NULL;
318 }
319 memset(arg->
transform.option.item, 0,
sizeof(
char *) * (arg->
transform.option.len + 1));
320
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");
330 return NULL;
331 }
332 sprintf(arg->
transform.option.item[i],
"%s%s", lbl, srs ? srs :
"");
334 }
335 }
336 else
338
339
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");
344 return NULL;
345 }
346
347
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");
354 return NULL;
355 }
356 GDALDestroyGenImgProjTransformer(arg->
transform.arg.transform);
358
359
360
361
362
363 _dim[0] = 0;
364 _dim[1] = 0;
365
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]);
368
369
370 extent.
MinX = dst_extent[0];
371 extent.
MinY = dst_extent[1];
372 extent.
MaxX = dst_extent[2];
373 extent.
MaxY = dst_extent[3];
374
377
380
381
382 if (
383 ((NULL != scale_x) || (NULL != scale_y)) &&
384 ((NULL != width) || (NULL != height))
385 ) {
386 rterror(
"rt_raster_gdal_warp: Scale X/Y and width/height are mutually exclusive. Only provide one");
388 return NULL;
389 }
390
391
392 if (NULL != width) {
393 _dim[0] = abs(*width);
394 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / ((
double) _dim[0]));
395 }
396
397 if (NULL != height) {
398 _dim[1] = abs(*height);
399 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / ((
double) _dim[1]));
400 }
401
402
403 if (
404 ((NULL != scale_x) && (
FLT_NEQ(*scale_x, 0.0))) &&
405 ((NULL != scale_y) && (
FLT_NEQ(*scale_y, 0.0)))
406 ) {
407 _scale[0] = fabs(*scale_x);
408 _scale[1] = fabs(*scale_y);
409
410
411 if (subspatial) {
412
413
414
415
416 _scale[0] /= 10;
417 _scale[1] /= 10;
418 }
419 }
420 else if (
421 ((NULL != scale_x) && (NULL == scale_y)) ||
422 ((NULL == scale_x) && (NULL != scale_y))
423 ) {
424 rterror(
"rt_raster_gdal_warp: Both X and Y scale values must be provided for scale");
426 return NULL;
427 }
428
429
431 {
432 _scale[0] = fabs(_gt[1]);
433 _scale[1] = fabs(_gt[5]);
434 }
435
436 RASTER_DEBUGF(4,
"Using scale: %f x %f", _scale[0], -1 * _scale[1]);
437
438
439 if (NULL != skew_x) {
440 _skew[0] = *skew_x;
441
442
443
444
445
446 if (
447 NULL != scale_x &&
448 *scale_x < 0.
449 ) {
450 _skew[0] *= -1;
451 }
452 }
453 if (NULL != skew_y) {
454 _skew[1] = *skew_y;
455
456
457
458
459
460 if (
461 NULL != scale_y &&
462 *scale_y > 0.
463 ) {
464 _skew[1] *= -1;
465 }
466 }
467
469
470
472 {
474
476
478 extent,
479 _skew,
480 _scale,
481 0.01
482 );
483 if (skewedrast == NULL) {
484 rterror(
"rt_raster_gdal_warp: Could not compute skewed raster");
486 return NULL;
487 }
488
489 if (_dim[0] == 0)
490 _dim[0] = skewedrast->
width;
491 if (_dim[1] == 0)
492 _dim[1] = skewedrast->
height;
493
496
498 }
499
500
501 if (!_dim[0])
502 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
503 if (!_dim[1])
504 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
505
506
508 if (rast == NULL) {
509 rterror(
"rt_raster_gdal_warp: Out of memory allocating temporary raster");
511 return NULL;
512 }
513
514
518
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",
523 _dim[0], _dim[1]);
524
525
526 if (
527 NULL != ul_xw &&
528 NULL != ul_yw
529 ) {
530 ul_user = 1;
531
532 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
533
534
538 }
539 else if (
540 ((NULL != ul_xw) && (NULL == ul_yw)) ||
541 ((NULL == ul_xw) && (NULL != ul_yw))
542 ) {
543 rterror(
"rt_raster_gdal_warp: Both X and Y upper-left corner values must be provided");
546 return NULL;
547 }
548
549
550 if (
551 !ul_user && (
552 (NULL != grid_xw) || (NULL != grid_yw)
553 )
554 ) {
555
556 if (
557 ((NULL != grid_xw) && (NULL == grid_yw)) ||
558 ((NULL == grid_xw) && (NULL != grid_yw))
559 ) {
560 rterror(
"rt_raster_gdal_warp: Both X and Y alignment values must be provided");
563 return NULL;
564 }
565
566 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
567
568 do {
569 double _r[2] = {0};
570 double _w[2] = {0};
571
572
574 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
575 break;
576 }
577
581
582
584 rast,
586 &(_r[0]), &(_r[1]),
587 NULL
589 rterror(
"rt_raster_gdal_warp: Could not compute raster pixel for spatial coordinates");
592 return NULL;
593 }
594
596 rast,
597 _r[0], _r[1],
598 &(_w[0]), &(_w[1]),
599 NULL
601 rterror(
"rt_raster_gdal_warp: Could not compute spatial coordinates for raster pixel");
602
605 return NULL;
606 }
607
608
610 if (NULL == width)
612 else if (NULL == scale_x) {
613 double _c[2] = {0};
614
616
617
619 rast,
621 &(_c[0]), &(_c[1]),
622 NULL
624 rterror(
"rt_raster_gdal_warp: Could not compute spatial coordinates for raster pixel");
627 return NULL;
628 }
629
630 rast->scaleX = fabs((_c[0] - _w[0]) / ((
double)
rast->width));
631 }
632 }
634 if (NULL == height)
636 else if (NULL == scale_y) {
637 double _c[2] = {0};
638
640
641
643 rast,
645 &(_c[0]), &(_c[1]),
646 NULL
648 rterror(
"rt_raster_gdal_warp: Could not compute spatial coordinates for raster pixel");
649
652 return NULL;
653 }
654
655 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double)
rast->height));
656 }
657 }
658
661 }
662 while (0);
663 }
664
665
666
667
668
669
670 _dim[0] =
rast->width;
671 _dim[1] =
rast->height;
673
674
675 if ((
676 (NULL != scale_x) && (*scale_x < 0.)
677 ) || (
678 (NULL != scale_y) && (*scale_y > 0)
679 )) {
680 double _w[2] = {0};
681
682
683 if (
684 (NULL != scale_x) &&
685 (*scale_x < 0.)
686 ) {
688 rast,
690 &(_w[0]), &(_w[1]),
691 NULL
693 rterror(
"rt_raster_gdal_warp: Could not compute spatial coordinates for raster pixel");
696 return NULL;
697 }
698
699 _gt[0] = _w[0];
700 _gt[1] = *scale_x;
701
702
703 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0.0))
704 _gt[2] = *skew_x;
705 }
706
707 if (
708 (NULL != scale_y) &&
709 (*scale_y > 0)
710 ) {
712 rast,
714 &(_w[0]), &(_w[1]),
715 NULL
717 rterror(
"rt_raster_gdal_warp: Could not compute spatial coordinates for raster pixel");
720 return NULL;
721 }
722
723 _gt[3] = _w[1];
724 _gt[5] = *scale_y;
725
726
727 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0.0))
728 _gt[4] = *skew_y;
729 }
730 }
731
734
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",
738 _dim[0], _dim[1]);
739
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]);
743 return NULL;
744 }
745
746
749 GDALRegister_VRT();
751 }
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");
756 return NULL;
757 }
758
759
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");
764 return NULL;
765 }
766
767
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");
773 return NULL;
774 }
776 }
777
778
779 cplerr = GDALSetGeoTransform(arg->
dst.
ds, _gt);
780 if (cplerr != CE_None) {
781 rterror(
"rt_raster_gdal_warp: Could not set geotransform");
783 return NULL;
784 }
785
786
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);
793 return NULL;
794 }
795
798 if (gdal_pt == GDT_Unknown)
799 rtwarn(
"rt_raster_gdal_warp: Unknown pixel type for band %d", i);
800
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");
805 return NULL;
806 }
807
808
810 band = GDALGetRasterBand(arg->
dst.
ds, i + 1);
811 if (NULL == band) {
812 rterror(
"rt_raster_gdal_warp: Could not get GDAL band for additional processing");
814 return NULL;
815 }
816
817
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));
823 }
824 }
825
826
827 arg->
transform.arg.transform = arg->
transform.arg.imgproj = GDALCreateGenImgProjTransformer2(
830 );
831 if (NULL == arg->
transform.arg.transform) {
832 rterror(
"rt_raster_gdal_warp: Could not create GDAL transformation object");
834 return NULL;
835 }
836 arg->
transform.func = GDALGenImgProjTransform;
837
838
839 if (max_err > 0.0) {
840 arg->
transform.arg.transform = arg->
transform.arg.approx = GDALCreateApproxTransformer(
841 GDALGenImgProjTransform,
843 );
844 if (NULL == arg->
transform.arg.transform) {
845 rterror(
"rt_raster_gdal_warp: Could not create GDAL approximate transformation object");
847 return NULL;
848 }
849
850 arg->
transform.func = GDALApproxTransform;
851 }
852
853
854 arg->
wopts = GDALCreateWarpOptions();
855 if (NULL == arg->
wopts) {
856 rterror(
"rt_raster_gdal_warp: Could not create GDAL warp options object");
858 return NULL;
859 }
860
861
862 arg->
wopts->eResampleAlg = resample_alg;
867 arg->
wopts->papszWarpOptions = (
char **) CPLMalloc(
sizeof(
char *) * 2);
868
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;
872
873
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;
879
880
881
882
883
884
885
886
887
888
889
890 {
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));
896 if (
897 NULL == arg->
wopts->padfSrcNoDataReal ||
898 NULL == arg->
wopts->padfDstNoDataReal ||
899 NULL == arg->
wopts->padfSrcNoDataImag ||
900 NULL == arg->
wopts->padfDstNoDataImag
901 ) {
902 rterror(
"rt_raster_gdal_warp: Out of memory allocating nodata mapping");
904 return NULL;
905 }
906 for (i = 0; i < numBands; i++) {
908 if (!band) {
909 rterror(
"rt_raster_gdal_warp: Could not process bands for nodata values");
911 return NULL;
912 }
913
915
916
917
918
919 arg->
wopts->padfSrcNoDataReal[i] = -123456.789;
920 }
921 else {
923 }
924
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)",
928 i,
929 arg->
wopts->padfSrcNoDataReal[i], arg->
wopts->padfSrcNoDataImag[i],
930 arg->
wopts->padfDstNoDataReal[i], arg->
wopts->padfDstNoDataImag[i]
931 );
932 }
933 }
934
935
937 cplerr = GDALInitializeWarpedVRT(arg->
dst.
ds, arg->
wopts);
938 if (cplerr != CE_None) {
939 rterror(
"rt_raster_gdal_warp: Could not warp raster");
941 return NULL;
942 }
943
944
945
946 GDALFlushCache(arg->
dst.
ds);
948
949
952
954
955 if (NULL == rast) {
956 rterror(
"rt_raster_gdal_warp: Could not warp raster");
957 return NULL;
958 }
959
960
961 if (subspatial) {
962 double gt[6] = {0, 1, 0, 0, 0, -1};
963
964
965
966
967 gt[1] = _scale[0] * 10;
968 gt[5] = -1 * _scale[1] * 10;
969
972 }
973
975
977}
#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.
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.
char * rt_util_gdal_convert_sr(const char *srs, int proj4)
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.
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