Skip to content

Commit

Permalink
gdalbuildvrt: add '-resolution same' mode to check all source rasters…
Browse files Browse the repository at this point in the history
… have the same resolution
  • Loading branch information
rouault committed Jan 27, 2025
1 parent 4f4c0a7 commit cd728bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
25 changes: 23 additions & 2 deletions apps/gdalbuildvrt_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ typedef enum
LOWEST_RESOLUTION,
HIGHEST_RESOLUTION,
AVERAGE_RESOLUTION,
SAME_RESOLUTION,
USER_RESOLUTION
} ResolutionStrategy;

Expand Down Expand Up @@ -985,6 +986,23 @@ std::string VRTBuilder::AnalyseRaster(GDALDatasetH hDS,
ns_res += dfDelta / nCountValid;
}
}
else if (resolutionStrategy == SAME_RESOLUTION)
{
if (bFirst)
{
we_res = padfGeoTransform[GEOTRSFRM_WE_RES];
ns_res = padfGeoTransform[GEOTRSFRM_NS_RES];
}
else if (we_res != padfGeoTransform[GEOTRSFRM_WE_RES] ||
ns_res != padfGeoTransform[GEOTRSFRM_NS_RES])
{
return CPLSPrintf("Dataset %s has resolution %f x %f, whereas "
"previous sources have resolution %f x %f",
dsFileName, padfGeoTransform[GEOTRSFRM_WE_RES],
padfGeoTransform[GEOTRSFRM_NS_RES], we_res,
ns_res);
}
}
else if (resolutionStrategy != USER_RESOLUTION)
{
if (bFirst)
Expand Down Expand Up @@ -1922,6 +1940,8 @@ GDALDatasetH GDALBuildVRT(const char *pszDest, int nSrcCount,
eStrategy = HIGHEST_RESOLUTION;
else if (EQUAL(sOptions.osResolution.c_str(), "lowest"))
eStrategy = LOWEST_RESOLUTION;
else if (EQUAL(sOptions.osResolution.c_str(), "same"))
eStrategy = SAME_RESOLUTION;

/* If -srcnodata is specified, use it as the -vrtnodata if the latter is not
*/
Expand Down Expand Up @@ -2055,15 +2075,16 @@ GDALBuildVRTOptionsGetParser(GDALBuildVRTOptions *psOptions,
"the default value which is 'location'."));

argParser->add_argument("-resolution")
.metavar("user|average|highest|lowest")
.metavar("user|average|highest|lowest|same")
.action(
[psOptions](const std::string &s)
{
psOptions->osResolution = s;
if (!EQUAL(psOptions->osResolution.c_str(), "user") &&
!EQUAL(psOptions->osResolution.c_str(), "average") &&
!EQUAL(psOptions->osResolution.c_str(), "highest") &&
!EQUAL(psOptions->osResolution.c_str(), "lowest"))
!EQUAL(psOptions->osResolution.c_str(), "lowest") &&
!EQUAL(psOptions->osResolution.c_str(), "same"))
{
throw std::invalid_argument(
CPLSPrintf("Illegal resolution value (%s).",
Expand Down
8 changes: 5 additions & 3 deletions doc/source/programs/gdalbuildvrt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Synopsis
[--quiet]
[[-strict]|[-non_strict]]
[-tile_index <field_name>]
[-resolution user|average|highest|lowest] [-tr <xres> <yes>]
[-input_file_list <filename>] [-separate]
[-resolution user|average|highest|lowest|same]
[-tr <xres> <yes>] [-input_file_list <filename>] [-separate]
[-allow_projection_difference] [-sd <n>] [-tap]
[-te <xmin> <ymin> <xmax> <ymax>] [-addalpha] [-b <band>]...
[-hidenodata] [-overwrite]
Expand Down Expand Up @@ -79,7 +79,7 @@ changed in later versions.
Use the specified value as the tile index field, instead of the default
value which is 'location'.

.. option:: -resolution {highest|lowest|average|user}
.. option:: -resolution {highest|lowest|average|user|same}

In case the resolution of all input files is not the same, the :option:`-resolution` flag
enables the user to control the way the output resolution is computed.
Expand All @@ -92,6 +92,8 @@ changed in later versions.

`user` must be used in combination with the :option:`-tr` option to specify the target resolution.

`same` (added in GDAL 3.11) checks that all source rasters have the same resolution and errors out when this is not the case.

.. option:: -tr <xres> <yres>

Set target resolution. The values must be expressed in georeferenced units.
Expand Down

0 comments on commit cd728bd

Please sign in to comment.