1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
|
/******************************************************************************
*
* Project: GDAL
* Purpose: gdal "raster overview refresh" subcommand
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "gdalalg_raster_overview_refresh.h"
#include "cpl_string.h"
#include "gdal_priv.h"
#include "vrtdataset.h"
#include "vrt_priv.h"
#include <algorithm>
#include <limits>
//! @cond Doxygen_Suppress
#ifndef _
#define _(x) (x)
#endif
/************************************************************************/
/* GDALRasterOverviewAlgorithmRefresh() */
/************************************************************************/
GDALRasterOverviewAlgorithmRefresh::GDALRasterOverviewAlgorithmRefresh()
: GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
{
AddProgressArg();
AddOpenOptionsArg(&m_openOptions);
AddArg("dataset", 0,
_("Dataset (to be updated in-place, unless --external)"), &m_dataset,
GDAL_OF_RASTER | GDAL_OF_UPDATE)
.SetPositional()
.SetRequired();
AddArg("external", 0, _("Refresh external overviews"), &m_readOnly)
.AddHiddenAlias("ro")
.AddHiddenAlias(GDAL_ARG_NAME_READ_ONLY);
AddArg("resampling", 'r', _("Resampling method"), &m_resampling)
.SetChoices("nearest", "average", "cubic", "cubicspline", "lanczos",
"bilinear", "gauss", "average_magphase", "rms", "mode")
.SetHiddenChoices("near", "none");
AddArg("levels", 0, _("Levels / decimation factors"), &m_levels)
.SetMinValueIncluded(2);
AddBBOXArg(&m_refreshBbox, _("Bounding box to refresh"))
.SetMutualExclusionGroup("refresh");
AddArg("like", 0, _("Use extent of dataset(s)"), &m_like)
.SetMutualExclusionGroup("refresh");
AddArg("use-source-timestamp", 0,
_("Use timestamp of VRT or GTI sources as refresh criterion"),
&m_refreshFromSourceTimestamp)
.SetMutualExclusionGroup("refresh");
}
/************************************************************************/
/* PartialRefresh() */
/************************************************************************/
static bool PartialRefresh(GDALDataset *poDS,
const std::vector<int> &anOvrIndices,
const char *pszResampling, int nXOff, int nYOff,
int nXSize, int nYSize, GDALProgressFunc pfnProgress,
void *pProgressArg)
{
int nOvCount = 0;
const int nBandCount = poDS->GetRasterCount();
for (int i = 0; i < nBandCount; ++i)
{
auto poSrcBand = poDS->GetRasterBand(i + 1);
if (i == 0)
nOvCount = poSrcBand->GetOverviewCount();
else if (nOvCount != poSrcBand->GetOverviewCount())
{
CPLError(CE_Failure, CPLE_AppDefined,
"Not same number of overviews on all bands");
return false;
}
}
std::vector<GDALRasterBand *> apoSrcBands;
std::vector<std::vector<GDALRasterBand *>> aapoOverviewBands;
for (int i = 0; i < nBandCount; ++i)
{
auto poSrcBand = poDS->GetRasterBand(i + 1);
apoSrcBands.push_back(poSrcBand);
std::vector<GDALRasterBand *> apoOverviewBands;
for (int nOvrIdx : anOvrIndices)
{
apoOverviewBands.push_back(poSrcBand->GetOverview(nOvrIdx));
}
aapoOverviewBands.push_back(std::move(apoOverviewBands));
}
CPLStringList aosOptions;
aosOptions.SetNameValue("XOFF", CPLSPrintf("%d", nXOff));
aosOptions.SetNameValue("YOFF", CPLSPrintf("%d", nYOff));
aosOptions.SetNameValue("XSIZE", CPLSPrintf("%d", nXSize));
aosOptions.SetNameValue("YSIZE", CPLSPrintf("%d", nYSize));
return GDALRegenerateOverviewsMultiBand(
apoSrcBands, aapoOverviewBands, pszResampling, pfnProgress,
pProgressArg, aosOptions.List()) == CE_None;
}
/************************************************************************/
/* PartialRefreshFromSourceTimestamp() */
/************************************************************************/
static bool
PartialRefreshFromSourceTimestamp(GDALDataset *poDS, const char *pszResampling,
const std::vector<int> &anOvrIndices,
GDALProgressFunc pfnProgress,
void *pProgressArg)
{
VSIStatBufL sStatOvr;
std::string osOvr(std::string(poDS->GetDescription()) + ".ovr");
if (VSIStatL(osOvr.c_str(), &sStatOvr) != 0)
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot find %s", osOvr.c_str());
return false;
}
if (sStatOvr.st_mtime == 0)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot get modification time of %s", osOvr.c_str());
return false;
}
std::vector<GTISourceDesc> regions;
// init slightly above zero to please Coverity Scan
double dfTotalPixels = std::numeric_limits<double>::min();
if (dynamic_cast<VRTDataset *>(poDS))
{
auto poVRTBand =
dynamic_cast<VRTSourcedRasterBand *>(poDS->GetRasterBand(1));
if (!poVRTBand)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Band is not a VRTSourcedRasterBand");
return false;
}
for (auto &poSource : poVRTBand->m_papoSources)
{
auto poSimpleSource =
dynamic_cast<VRTSimpleSource *>(poSource.get());
if (poSimpleSource)
{
VSIStatBufL sStatSource;
if (VSIStatL(poSimpleSource->GetSourceDatasetName().c_str(),
&sStatSource) == 0)
{
if (sStatSource.st_mtime > sStatOvr.st_mtime)
{
double dfXOff, dfYOff, dfXSize, dfYSize;
poSimpleSource->GetDstWindow(dfXOff, dfYOff, dfXSize,
dfYSize);
constexpr double EPS = 1e-8;
int nXOff = static_cast<int>(dfXOff + EPS);
int nYOff = static_cast<int>(dfYOff + EPS);
int nXSize = static_cast<int>(dfXSize + 0.5);
int nYSize = static_cast<int>(dfYSize + 0.5);
if (!(nXOff > poDS->GetRasterXSize() ||
nYOff > poDS->GetRasterYSize() || nXSize <= 0 ||
nYSize <= 0))
{
if (nXOff < 0)
{
nXSize += nXOff;
nXOff = 0;
}
if (nXOff > poDS->GetRasterXSize() - nXSize)
{
nXSize = poDS->GetRasterXSize() - nXOff;
}
if (nYOff < 0)
{
nYSize += nYOff;
nYOff = 0;
}
if (nYOff > poDS->GetRasterYSize() - nYSize)
{
nYSize = poDS->GetRasterYSize() - nYOff;
}
dfTotalPixels +=
static_cast<double>(nXSize) * nYSize;
GTISourceDesc region;
region.osFilename =
poSimpleSource->GetSourceDatasetName();
region.nDstXOff = nXOff;
region.nDstYOff = nYOff;
region.nDstXSize = nXSize;
region.nDstYSize = nYSize;
regions.push_back(std::move(region));
}
}
}
}
}
}
#ifdef GTI_DRIVER_DISABLED_OR_PLUGIN
else if (poDS->GetDriver() &&
EQUAL(poDS->GetDriver()->GetDescription(), "GTI"))
{
CPLError(CE_Failure, CPLE_NotSupported,
"--use-source-timestamp only works on a GTI "
"dataset if the GTI driver is not built as a plugin, "
"but in core library");
return false;
}
#else
else if (auto poGTIDS = GDALDatasetCastToGTIDataset(poDS))
{
regions = GTIGetSourcesMoreRecentThan(poGTIDS, sStatOvr.st_mtime);
for (const auto ®ion : regions)
{
dfTotalPixels +=
static_cast<double>(region.nDstXSize) * region.nDstYSize;
}
}
#endif
else
{
CPLError(CE_Failure, CPLE_AppDefined,
"--use-source-timestamp only works on a VRT or GTI "
"dataset");
return false;
}
bool bRet = true;
if (!regions.empty())
{
double dfCurPixels = 0;
for (const auto ®ion : regions)
{
if (bRet)
{
CPLDebug("GDAL", "Refresh from source %s",
region.osFilename.c_str());
double dfNextCurPixels =
dfCurPixels +
static_cast<double>(region.nDstXSize) * region.nDstYSize;
void *pScaledProgress = GDALCreateScaledProgress(
dfCurPixels / dfTotalPixels,
dfNextCurPixels / dfTotalPixels, pfnProgress, pProgressArg);
bRet = PartialRefresh(
poDS, anOvrIndices, pszResampling, region.nDstXOff,
region.nDstYOff, region.nDstXSize, region.nDstYSize,
pScaledProgress ? GDALScaledProgress : nullptr,
pScaledProgress);
GDALDestroyScaledProgress(pScaledProgress);
dfCurPixels = dfNextCurPixels;
}
}
}
else
{
CPLDebug("GDAL", "No source is more recent than the overviews");
}
return bRet;
}
/************************************************************************/
/* PartialRefreshFromSourceExtent() */
/************************************************************************/
static bool PartialRefreshFromSourceExtent(
GDALDataset *poDS, const std::vector<std::string> &sources,
const char *pszResampling, const std::vector<int> &anOvrIndices,
GDALProgressFunc pfnProgress, void *pProgressArg)
{
GDALGeoTransform gt;
if (poDS->GetGeoTransform(gt) != CE_None)
{
CPLError(CE_Failure, CPLE_AppDefined, "Dataset has no geotransform");
return false;
}
GDALGeoTransform invGT;
if (!gt.GetInverse(invGT))
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
return false;
}
struct Region
{
std::string osFileName{};
int nXOff = 0;
int nYOff = 0;
int nXSize = 0;
int nYSize = 0;
};
std::vector<Region> regions;
// init slightly above zero to please Coverity Scan
double dfTotalPixels = std::numeric_limits<double>::min();
for (const std::string &filename : sources)
{
auto poSrcDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
filename.c_str(), GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR));
if (!poSrcDS)
return false;
GDALGeoTransform srcGT;
if (poSrcDS->GetGeoTransform(srcGT) != CE_None)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Source dataset has no geotransform");
return false;
}
const double dfULX = srcGT[0];
const double dfULY = srcGT[3];
const double dfLRX = srcGT[0] + poSrcDS->GetRasterXSize() * srcGT[1] +
poSrcDS->GetRasterYSize() * srcGT[2];
const double dfLRY = srcGT[3] + poSrcDS->GetRasterXSize() * srcGT[4] +
poSrcDS->GetRasterYSize() * srcGT[5];
const double dfX1 = invGT[0] + invGT[1] * dfULX + invGT[2] * dfULY;
const double dfY1 = invGT[3] + invGT[4] * dfULX + invGT[5] * dfULY;
const double dfX2 = invGT[0] + invGT[1] * dfLRX + invGT[2] * dfLRY;
const double dfY2 = invGT[3] + invGT[4] * dfLRX + invGT[5] * dfLRY;
constexpr double EPS = 1e-8;
const int nXOff =
static_cast<int>(std::max(0.0, std::min(dfX1, dfX2)) + EPS);
const int nYOff =
static_cast<int>(std::max(0.0, std::min(dfY1, dfY2)) + EPS);
const int nXSize =
static_cast<int>(
std::ceil(std::min(static_cast<double>(poDS->GetRasterXSize()),
std::max(dfX1, dfX2)) -
EPS)) -
nXOff;
const int nYSize =
static_cast<int>(
std::ceil(std::min(static_cast<double>(poDS->GetRasterYSize()),
std::max(dfY1, dfY2)) -
EPS)) -
nYOff;
dfTotalPixels += static_cast<double>(nXSize) * nYSize;
Region region;
region.osFileName = filename;
region.nXOff = nXOff;
region.nYOff = nYOff;
region.nXSize = nXSize;
region.nYSize = nYSize;
regions.push_back(std::move(region));
}
bool bRet = true;
double dfCurPixels = 0;
for (const auto ®ion : regions)
{
if (bRet)
{
CPLDebug("GDAL", "Refresh from source %s",
region.osFileName.c_str());
double dfNextCurPixels =
dfCurPixels +
static_cast<double>(region.nXSize) * region.nYSize;
// coverity[divide_by_zero]
void *pScaledProgress = GDALCreateScaledProgress(
dfCurPixels / dfTotalPixels, dfNextCurPixels / dfTotalPixels,
pfnProgress, pProgressArg);
bRet =
PartialRefresh(poDS, anOvrIndices, pszResampling, region.nXOff,
region.nYOff, region.nXSize, region.nYSize,
pScaledProgress ? GDALScaledProgress : nullptr,
pScaledProgress);
GDALDestroyScaledProgress(pScaledProgress);
dfCurPixels = dfNextCurPixels;
}
}
return bRet;
}
/************************************************************************/
/* PartialRefreshFromBBOX() */
/************************************************************************/
static bool PartialRefreshFromBBOX(GDALDataset *poDS,
const std::vector<double> &bbox,
const char *pszResampling,
const std::vector<int> &anOvrIndices,
GDALProgressFunc pfnProgress,
void *pProgressArg)
{
const double dfULX = bbox[0];
const double dfLRY = bbox[1];
const double dfLRX = bbox[2];
const double dfULY = bbox[3];
GDALGeoTransform gt;
if (poDS->GetGeoTransform(gt) != CE_None)
{
CPLError(CE_Failure, CPLE_AppDefined, "Dataset has no geotransform");
return false;
}
GDALGeoTransform invGT;
if (!gt.GetInverse(invGT))
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
return false;
}
const double dfX1 = invGT[0] + invGT[1] * dfULX + invGT[2] * dfULY;
const double dfY1 = invGT[3] + invGT[4] * dfULX + invGT[5] * dfULY;
const double dfX2 = invGT[0] + invGT[1] * dfLRX + invGT[2] * dfLRY;
const double dfY2 = invGT[3] + invGT[4] * dfLRX + invGT[5] * dfLRY;
constexpr double EPS = 1e-8;
const int nXOff =
static_cast<int>(std::max(0.0, std::min(dfX1, dfX2)) + EPS);
const int nYOff =
static_cast<int>(std::max(0.0, std::min(dfY1, dfY2)) + EPS);
const int nXSize = static_cast<int>(std::ceil(
std::min(static_cast<double>(poDS->GetRasterXSize()),
std::max(dfX1, dfX2)) -
EPS)) -
nXOff;
const int nYSize = static_cast<int>(std::ceil(
std::min(static_cast<double>(poDS->GetRasterYSize()),
std::max(dfY1, dfY2)) -
EPS)) -
nYOff;
return PartialRefresh(poDS, anOvrIndices, pszResampling, nXOff, nYOff,
nXSize, nYSize, pfnProgress, pProgressArg);
}
/************************************************************************/
/* GDALRasterOverviewAlgorithmRefresh::RunImpl() */
/************************************************************************/
bool GDALRasterOverviewAlgorithmRefresh::RunImpl(GDALProgressFunc pfnProgress,
void *pProgressData)
{
auto poDS = m_dataset.GetDatasetRef();
CPLAssert(poDS);
if (poDS->GetRasterCount() == 0)
{
ReportError(CE_Failure, CPLE_AppDefined, "Dataset has no raster band");
return false;
}
auto poBand = poDS->GetRasterBand(1);
const int nOvCount = poBand->GetOverviewCount();
std::vector<int> levels = m_levels;
// If no levels are specified, reuse the potentially existing ones.
if (levels.empty())
{
for (int iOvr = 0; iOvr < nOvCount; ++iOvr)
{
auto poOverview = poBand->GetOverview(iOvr);
if (poOverview)
{
const int nOvFactor = GDALComputeOvFactor(
poOverview->GetXSize(), poBand->GetXSize(),
poOverview->GetYSize(), poBand->GetYSize());
levels.push_back(nOvFactor);
}
}
}
if (levels.empty())
{
ReportError(CE_Failure, CPLE_AppDefined, "No overviews to refresh");
return false;
}
std::vector<int> anOvrIndices;
for (int nLevel : levels)
{
int nIdx = -1;
for (int iOvr = 0; iOvr < nOvCount; iOvr++)
{
auto poOverview = poBand->GetOverview(iOvr);
if (poOverview)
{
const int nOvFactor = GDALComputeOvFactor(
poOverview->GetXSize(), poBand->GetXSize(),
poOverview->GetYSize(), poBand->GetYSize());
if (nOvFactor == nLevel ||
nOvFactor == GDALOvLevelAdjust2(nLevel, poBand->GetXSize(),
poBand->GetYSize()))
{
nIdx = iOvr;
break;
}
}
}
if (nIdx < 0)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot find overview level with subsampling factor of %d",
nLevel);
return false;
}
CPLDebug("GDAL", "Refreshing overview idx %d", nIdx);
anOvrIndices.push_back(nIdx);
}
std::string resampling = m_resampling;
if (resampling.empty())
{
const char *pszResampling =
poBand->GetOverview(0)->GetMetadataItem("RESAMPLING");
if (pszResampling)
{
resampling = pszResampling;
CPLDebug("GDAL",
"Reusing resampling method %s from existing "
"overview",
pszResampling);
}
}
if (resampling.empty())
resampling = "nearest";
if (m_refreshFromSourceTimestamp)
{
return PartialRefreshFromSourceTimestamp(
poDS, resampling.c_str(), anOvrIndices, pfnProgress, pProgressData);
}
else if (!m_refreshBbox.empty())
{
return PartialRefreshFromBBOX(poDS, m_refreshBbox, resampling.c_str(),
anOvrIndices, pfnProgress, pProgressData);
}
else if (!m_like.empty())
{
return PartialRefreshFromSourceExtent(poDS, m_like, resampling.c_str(),
anOvrIndices, pfnProgress,
pProgressData);
}
else
{
return GDALBuildOverviews(
GDALDataset::ToHandle(poDS), resampling.c_str(),
static_cast<int>(levels.size()), levels.data(), 0, nullptr,
pfnProgress, pProgressData) == CE_None;
}
}
//! @endcond
|