@@ -3634,6 +3634,39 @@ GDALDatasetH GDALDEMProcessing(const char *pszDest, GDALDatasetH hSrcDataset,
36343634
36353635 GDALDEMProcessingOptions *psOptions = psOptionsToFree.get ();
36363636
3637+ double adfGeoTransform[6 ] = {0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 };
3638+
3639+ // Keep that variable in that scope.
3640+ // A reference on that dataset will be taken by GDALGeneric3x3Dataset
3641+ // (and released at its destruction) if we go to that code path, and
3642+ // GDALWarp() (actually the VRTWarpedDataset) will itself take a reference
3643+ // on hSrcDataset.
3644+ std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser> poTmpSrcDS;
3645+
3646+ if (GDALGetGeoTransform (hSrcDataset, adfGeoTransform) == CE_None &&
3647+ // For following modes, detect non north-up datasets and autowarp
3648+ // them so they are north-up oriented.
3649+ (((eUtilityMode == ASPECT || eUtilityMode == TRI ||
3650+ eUtilityMode == TPI) &&
3651+ (adfGeoTransform[2 ] != 0 || adfGeoTransform[4 ] != 0 ||
3652+ adfGeoTransform[5 ] > 0 )) ||
3653+ // For following modes, detect rotated datasets and autowarp
3654+ // them so they are north-up oriented. (south-up are fine for those)
3655+ ((eUtilityMode == SLOPE || eUtilityMode == HILL_SHADE) &&
3656+ (adfGeoTransform[2 ] != 0 || adfGeoTransform[4 ] != 0 ))))
3657+ {
3658+ const char *const apszWarpOptions[] = {" -of" , " VRT" , nullptr };
3659+ GDALWarpAppOptions *psWarpOptions = GDALWarpAppOptionsNew (
3660+ const_cast <char **>(apszWarpOptions), nullptr );
3661+ poTmpSrcDS.reset (GDALDataset::FromHandle (
3662+ GDALWarp (" " , nullptr , 1 , &hSrcDataset, psWarpOptions, nullptr )));
3663+ GDALWarpAppOptionsFree (psWarpOptions);
3664+ if (!poTmpSrcDS)
3665+ return nullptr ;
3666+ hSrcDataset = GDALDataset::ToHandle (poTmpSrcDS.get ());
3667+ GDALGetGeoTransform (hSrcDataset, adfGeoTransform);
3668+ }
3669+
36373670 const int nXSize = GDALGetRasterXSize (hSrcDataset);
36383671 const int nYSize = GDALGetRasterYSize (hSrcDataset);
36393672
@@ -3730,12 +3763,8 @@ GDALDatasetH GDALDEMProcessing(const char *pszDest, GDALDatasetH hSrcDataset,
37303763 return nullptr ;
37313764 }
37323765
3733- double adfGeoTransform[6 ] = {0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 };
3734-
37353766 GDALRasterBandH hSrcBand = GDALGetRasterBand (hSrcDataset, psOptions->nBand );
37363767
3737- GDALGetGeoTransform (hSrcDataset, adfGeoTransform);
3738-
37393768 CPLString osFormat;
37403769 if (psOptions->osFormat .empty ())
37413770 {
0 commit comments