Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: Virtual GDAL Datasets
4 : * Purpose: Implementation of VRTWarpedRasterBand *and VRTWarpedDataset.
5 : * Author: Frank Warmerdam <warmerdam@pobox.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2004, Frank Warmerdam <warmerdam@pobox.com>
9 : * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #include "cpl_port.h"
15 : #include "vrtdataset.h"
16 :
17 : #include <stddef.h>
18 : #include <stdio.h>
19 : #include <stdlib.h>
20 : #include <string.h>
21 : #include <algorithm>
22 : #include <map>
23 :
24 : // Suppress deprecation warning for GDALOpenVerticalShiftGrid and
25 : // GDALApplyVerticalShiftGrid
26 : #ifndef CPL_WARN_DEPRECATED_GDALOpenVerticalShiftGrid
27 : #define CPL_WARN_DEPRECATED_GDALOpenVerticalShiftGrid(x)
28 : #define CPL_WARN_DEPRECATED_GDALApplyVerticalShiftGrid(x)
29 : #endif
30 :
31 : #include "cpl_conv.h"
32 : #include "cpl_error.h"
33 : #include "cpl_minixml.h"
34 : #include "cpl_progress.h"
35 : #include "cpl_string.h"
36 : #include "cpl_vsi.h"
37 : #include "gdal.h"
38 : #include "gdal_alg.h"
39 : #include "gdal_alg_priv.h"
40 : #include "gdal_priv.h"
41 : #include "gdalwarper.h"
42 : #include "ogr_geometry.h"
43 :
44 : /************************************************************************/
45 : /* GDALAutoCreateWarpedVRT() */
46 : /************************************************************************/
47 :
48 : /**
49 : * Create virtual warped dataset automatically.
50 : *
51 : * This function will create a warped virtual file representing the
52 : * input image warped into the target coordinate system. A GenImgProj
53 : * transformation is created to accomplish any required GCP/Geotransform
54 : * warp and reprojection to the target coordinate system. The output virtual
55 : * dataset will be "northup" in the target coordinate system. The
56 : * GDALSuggestedWarpOutput() function is used to determine the bounds and
57 : * resolution of the output virtual file which should be large enough to
58 : * include all the input image
59 : *
60 : * If you want to create an alpha band if the source dataset has none, set
61 : * psOptionsIn->nDstAlphaBand = GDALGetRasterCount(hSrcDS) + 1.
62 : *
63 : * Note that the constructed GDALDatasetH will acquire one or more references
64 : * to the passed in hSrcDS. Reference counting semantics on the source
65 : * dataset should be honoured. That is, don't just GDALClose() it unless it
66 : * was opened with GDALOpenShared().
67 : *
68 : * It is possible to "transfer" the ownership of the source dataset
69 : * to the warped dataset in the following way:
70 : *
71 : * \code{.c}
72 : * GDALDatasetH src_ds = GDALOpen("source.tif");
73 : * GDALDatasetH warped_ds = GDALAutoCreateWarpedVRT( src_ds, ... );
74 : * GDALReleaseDataset(src_ds); // src_ds is not "owned" fully by warped_ds.
75 : * Do NOT use GDALClose(src_ds) here
76 : * ...
77 : * ...
78 : * GDALReleaseDataset(warped_ds); // or GDALClose(warped_ds);
79 : * \endcode
80 : *
81 : * Traditonal nested calls are also possible of course:
82 : *
83 : * \code{.c}
84 : * GDALDatasetH src_ds = GDALOpen("source.tif");
85 : * GDALDatasetH warped_ds = GDALAutoCreateWarpedVRT( src_ds, ... );
86 : * ...
87 : * ...
88 : * GDALReleaseDataset(warped_ds); // or GDALClose(warped_ds);
89 : * GDALReleaseDataset(src_ds); // or GDALClose(src_ds);
90 : * \endcode
91 : *
92 : * The returned dataset will have no associated filename for itself. If you
93 : * want to write the virtual dataset description to a file, use the
94 : * GDALSetDescription() function (or SetDescription() method) on the dataset
95 : * to assign a filename before it is closed.
96 : *
97 : * @param hSrcDS The source dataset.
98 : *
99 : * @param pszSrcWKT The coordinate system of the source image. If NULL, it
100 : * will be read from the source image.
101 : *
102 : * @param pszDstWKT The coordinate system to convert to. If NULL no change
103 : * of coordinate system will take place.
104 : *
105 : * @param eResampleAlg One of GRA_NearestNeighbour, GRA_Bilinear, GRA_Cubic,
106 : * GRA_CubicSpline, GRA_Lanczos, GRA_Average, GRA_RMS or GRA_Mode.
107 : * Controls the sampling method used.
108 : *
109 : * @param dfMaxError Maximum error measured in input pixels that is allowed in
110 : * approximating the transformation (0.0 for exact calculations).
111 : *
112 : * @param psOptionsIn Additional warp options, normally NULL.
113 : *
114 : * @return NULL on failure, or a new virtual dataset handle on success.
115 : */
116 :
117 : GDALDatasetH CPL_STDCALL
118 25 : GDALAutoCreateWarpedVRT(GDALDatasetH hSrcDS, const char *pszSrcWKT,
119 : const char *pszDstWKT, GDALResampleAlg eResampleAlg,
120 : double dfMaxError, const GDALWarpOptions *psOptionsIn)
121 : {
122 25 : return GDALAutoCreateWarpedVRTEx(hSrcDS, pszSrcWKT, pszDstWKT, eResampleAlg,
123 25 : dfMaxError, psOptionsIn, nullptr);
124 : }
125 :
126 : /************************************************************************/
127 : /* GDALAutoCreateWarpedVRTEx() */
128 : /************************************************************************/
129 :
130 : /**
131 : * Create virtual warped dataset automatically.
132 : *
133 : * Compared to GDALAutoCreateWarpedVRT() this function adds one extra
134 : * argument: options to be passed to GDALCreateGenImgProjTransformer2().
135 : *
136 : * @since 3.2
137 : */
138 :
139 25 : GDALDatasetH CPL_STDCALL GDALAutoCreateWarpedVRTEx(
140 : GDALDatasetH hSrcDS, const char *pszSrcWKT, const char *pszDstWKT,
141 : GDALResampleAlg eResampleAlg, double dfMaxError,
142 : const GDALWarpOptions *psOptionsIn, CSLConstList papszTransformerOptions)
143 : {
144 25 : VALIDATE_POINTER1(hSrcDS, "GDALAutoCreateWarpedVRT", nullptr);
145 :
146 : /* -------------------------------------------------------------------- */
147 : /* Populate the warp options. */
148 : /* -------------------------------------------------------------------- */
149 25 : GDALWarpOptions *psWO = nullptr;
150 25 : if (psOptionsIn != nullptr)
151 1 : psWO = GDALCloneWarpOptions(psOptionsIn);
152 : else
153 24 : psWO = GDALCreateWarpOptions();
154 :
155 25 : psWO->eResampleAlg = eResampleAlg;
156 :
157 25 : psWO->hSrcDS = hSrcDS;
158 :
159 25 : GDALWarpInitDefaultBandMapping(psWO, GDALGetRasterCount(hSrcDS));
160 :
161 : /* -------------------------------------------------------------------- */
162 : /* Setup no data values (if not done in psOptionsIn) */
163 : /* -------------------------------------------------------------------- */
164 25 : if (psWO->padfSrcNoDataReal == nullptr &&
165 25 : psWO->padfDstNoDataReal == nullptr && psWO->nSrcAlphaBand == 0)
166 : {
167 : // If none of the provided input nodata values can be represented in the
168 : // data type of the corresponding source band, ignore them.
169 25 : int nCountInvalidSrcNoDataReal = 0;
170 55 : for (int i = 0; i < psWO->nBandCount; i++)
171 : {
172 : GDALRasterBandH rasterBand =
173 30 : GDALGetRasterBand(psWO->hSrcDS, psWO->panSrcBands[i]);
174 :
175 : int hasNoDataValue;
176 : double noDataValue =
177 30 : GDALGetRasterNoDataValue(rasterBand, &hasNoDataValue);
178 :
179 30 : if (hasNoDataValue &&
180 7 : !GDALIsValueExactAs(noDataValue,
181 30 : GDALGetRasterDataType(rasterBand)))
182 : {
183 6 : nCountInvalidSrcNoDataReal++;
184 : }
185 : }
186 :
187 25 : if (nCountInvalidSrcNoDataReal != psWO->nBandCount)
188 : {
189 46 : for (int i = 0; i < psWO->nBandCount; i++)
190 : {
191 : GDALRasterBandH rasterBand =
192 24 : GDALGetRasterBand(psWO->hSrcDS, psWO->panSrcBands[i]);
193 :
194 : int hasNoDataValue;
195 : double noDataValue =
196 24 : GDALGetRasterNoDataValue(rasterBand, &hasNoDataValue);
197 :
198 24 : if (hasNoDataValue)
199 : {
200 : // Check if the nodata value is out of range
201 1 : int bClamped = FALSE;
202 1 : int bRounded = FALSE;
203 1 : CPL_IGNORE_RET_VAL(GDALAdjustValueToDataType(
204 : GDALGetRasterDataType(rasterBand), noDataValue,
205 : &bClamped, &bRounded));
206 1 : if (!bClamped)
207 : {
208 1 : GDALWarpInitNoDataReal(psWO, -1e10);
209 1 : if (psWO->padfSrcNoDataReal != nullptr &&
210 1 : psWO->padfDstNoDataReal != nullptr)
211 : {
212 1 : psWO->padfSrcNoDataReal[i] = noDataValue;
213 1 : psWO->padfDstNoDataReal[i] = noDataValue;
214 : }
215 : }
216 : }
217 : }
218 : }
219 :
220 25 : if (psWO->padfDstNoDataReal != nullptr)
221 : {
222 1 : if (CSLFetchNameValue(psWO->papszWarpOptions, "INIT_DEST") ==
223 : nullptr)
224 : {
225 1 : psWO->papszWarpOptions = CSLSetNameValue(
226 : psWO->papszWarpOptions, "INIT_DEST", "NO_DATA");
227 : }
228 : }
229 : }
230 :
231 : /* -------------------------------------------------------------------- */
232 : /* Create the transformer. */
233 : /* -------------------------------------------------------------------- */
234 25 : psWO->pfnTransformer = GDALGenImgProjTransform;
235 :
236 25 : char **papszOptions = nullptr;
237 25 : if (pszSrcWKT != nullptr)
238 5 : papszOptions = CSLSetNameValue(papszOptions, "SRC_SRS", pszSrcWKT);
239 25 : if (pszDstWKT != nullptr)
240 8 : papszOptions = CSLSetNameValue(papszOptions, "DST_SRS", pszDstWKT);
241 25 : papszOptions = CSLMerge(papszOptions, papszTransformerOptions);
242 25 : psWO->pTransformerArg =
243 25 : GDALCreateGenImgProjTransformer2(psWO->hSrcDS, nullptr, papszOptions);
244 25 : CSLDestroy(papszOptions);
245 :
246 25 : if (psWO->pTransformerArg == nullptr)
247 : {
248 1 : GDALDestroyWarpOptions(psWO);
249 1 : return nullptr;
250 : }
251 :
252 : /* -------------------------------------------------------------------- */
253 : /* Figure out the desired output bounds and resolution. */
254 : /* -------------------------------------------------------------------- */
255 24 : double adfDstGeoTransform[6] = {0.0};
256 24 : int nDstPixels = 0;
257 24 : int nDstLines = 0;
258 24 : CPLErr eErr = GDALSuggestedWarpOutput(
259 : hSrcDS, psWO->pfnTransformer, psWO->pTransformerArg, adfDstGeoTransform,
260 : &nDstPixels, &nDstLines);
261 24 : if (eErr != CE_None)
262 : {
263 0 : GDALDestroyTransformer(psWO->pTransformerArg);
264 0 : GDALDestroyWarpOptions(psWO);
265 0 : return nullptr;
266 : }
267 :
268 : /* -------------------------------------------------------------------- */
269 : /* Update the transformer to include an output geotransform */
270 : /* back to pixel/line coordinates. */
271 : /* */
272 : /* -------------------------------------------------------------------- */
273 24 : GDALSetGenImgProjTransformerDstGeoTransform(psWO->pTransformerArg,
274 : adfDstGeoTransform);
275 :
276 : /* -------------------------------------------------------------------- */
277 : /* Do we want to apply an approximating transformation? */
278 : /* -------------------------------------------------------------------- */
279 24 : if (dfMaxError > 0.0)
280 : {
281 3 : psWO->pTransformerArg = GDALCreateApproxTransformer(
282 : psWO->pfnTransformer, psWO->pTransformerArg, dfMaxError);
283 3 : psWO->pfnTransformer = GDALApproxTransform;
284 3 : GDALApproxTransformerOwnsSubtransformer(psWO->pTransformerArg, TRUE);
285 : }
286 :
287 : /* -------------------------------------------------------------------- */
288 : /* Create the VRT file. */
289 : /* -------------------------------------------------------------------- */
290 24 : GDALDatasetH hDstDS = GDALCreateWarpedVRT(hSrcDS, nDstPixels, nDstLines,
291 : adfDstGeoTransform, psWO);
292 :
293 24 : GDALDestroyWarpOptions(psWO);
294 :
295 24 : if (hDstDS != nullptr)
296 : {
297 24 : if (pszDstWKT != nullptr)
298 7 : GDALSetProjection(hDstDS, pszDstWKT);
299 17 : else if (pszSrcWKT != nullptr)
300 0 : GDALSetProjection(hDstDS, pszSrcWKT);
301 17 : else if (GDALGetGCPCount(hSrcDS) > 0)
302 2 : GDALSetProjection(hDstDS, GDALGetGCPProjection(hSrcDS));
303 : else
304 15 : GDALSetProjection(hDstDS, GDALGetProjectionRef(hSrcDS));
305 : }
306 :
307 24 : return hDstDS;
308 : }
309 :
310 : /************************************************************************/
311 : /* GDALCreateWarpedVRT() */
312 : /************************************************************************/
313 :
314 : /**
315 : * Create virtual warped dataset.
316 : *
317 : * This function will create a warped virtual file representing the
318 : * input image warped based on a provided transformation. Output bounds
319 : * and resolution are provided explicitly.
320 : *
321 : * If you want to create an alpha band if the source dataset has none, set
322 : * psOptions->nDstAlphaBand = GDALGetRasterCount(hSrcDS) + 1.
323 : *
324 : * Note that the constructed GDALDatasetH will acquire one or more references
325 : * to the passed in hSrcDS. Reference counting semantics on the source
326 : * dataset should be honoured. That is, don't just GDALClose() it unless it
327 : * was opened with GDALOpenShared().
328 : *
329 : * It is possible to "transfer" the ownership of the source dataset
330 : * to the warped dataset in the following way:
331 : *
332 : * \code{.c}
333 : * GDALDatasetH src_ds = GDALOpen("source.tif");
334 : * GDALDatasetH warped_ds = GDALAutoCreateWarpedVRT( src_ds, ... );
335 : * GDALReleaseDataset(src_ds); // src_ds is not "owned" fully by warped_ds.
336 : * Do NOT use GDALClose(src_ds) here
337 : * ...
338 : * ...
339 : * GDALReleaseDataset(warped_ds); // or GDALClose(warped_ds);
340 : * \endcode
341 : *
342 : * Traditonal nested calls are also possible of course:
343 : *
344 : * \code{.c}
345 : * GDALDatasetH src_ds = GDALOpen("source.tif");
346 : * GDALDatasetH warped_ds = GDALAutoCreateWarpedVRT( src_ds, ... );
347 : * ...
348 : * ...
349 : * GDALReleaseDataset(warped_ds); // or GDALClose(warped_ds);
350 : * GDALReleaseDataset(src_ds); // or GDALClose(src_ds);
351 : * \endcode
352 : *
353 : * The returned dataset will have no associated filename for itself. If you
354 : * want to write the virtual dataset description to a file, use the
355 : * GDALSetDescription() function (or SetDescription() method) on the dataset
356 : * to assign a filename before it is closed.
357 : *
358 : * @param hSrcDS The source dataset.
359 : *
360 : * @param nPixels Width of the virtual warped dataset to create
361 : *
362 : * @param nLines Height of the virtual warped dataset to create
363 : *
364 : * @param padfGeoTransform Geotransform matrix of the virtual warped dataset
365 : * to create
366 : *
367 : * @param psOptions Warp options. Must be different from NULL.
368 : *
369 : * @return NULL on failure, or a new virtual dataset handle on success.
370 : */
371 :
372 46 : GDALDatasetH CPL_STDCALL GDALCreateWarpedVRT(GDALDatasetH hSrcDS, int nPixels,
373 : int nLines,
374 : const double *padfGeoTransform,
375 : GDALWarpOptions *psOptions)
376 :
377 : {
378 46 : VALIDATE_POINTER1(hSrcDS, "GDALCreateWarpedVRT", nullptr);
379 46 : VALIDATE_POINTER1(psOptions, "GDALCreateWarpedVRT", nullptr);
380 :
381 : /* -------------------------------------------------------------------- */
382 : /* Create the VRTDataset and populate it with bands. */
383 : /* -------------------------------------------------------------------- */
384 46 : VRTWarpedDataset *poDS = new VRTWarpedDataset(nPixels, nLines);
385 :
386 : // Call this before assigning hDstDS
387 46 : GDALWarpResolveWorkingDataType(psOptions);
388 :
389 46 : psOptions->hDstDS = poDS;
390 46 : poDS->SetGeoTransform(GDALGeoTransform(padfGeoTransform));
391 :
392 101 : for (int i = 0; i < psOptions->nBandCount; i++)
393 : {
394 55 : int nDstBand = psOptions->panDstBands[i];
395 110 : while (poDS->GetRasterCount() < nDstBand)
396 : {
397 55 : poDS->AddBand(psOptions->eWorkingDataType, nullptr);
398 : }
399 :
400 : VRTWarpedRasterBand *poBand =
401 55 : static_cast<VRTWarpedRasterBand *>(poDS->GetRasterBand(nDstBand));
402 : GDALRasterBand *poSrcBand = static_cast<GDALRasterBand *>(
403 55 : GDALGetRasterBand(hSrcDS, psOptions->panSrcBands[i]));
404 :
405 55 : poBand->CopyCommonInfoFrom(poSrcBand);
406 : }
407 :
408 47 : while (poDS->GetRasterCount() < psOptions->nDstAlphaBand)
409 : {
410 1 : poDS->AddBand(psOptions->eWorkingDataType, nullptr);
411 : }
412 46 : if (psOptions->nDstAlphaBand)
413 : {
414 1 : poDS->GetRasterBand(psOptions->nDstAlphaBand)
415 1 : ->SetColorInterpretation(GCI_AlphaBand);
416 : }
417 :
418 : /* -------------------------------------------------------------------- */
419 : /* Initialize the warp on the VRTWarpedDataset. */
420 : /* -------------------------------------------------------------------- */
421 46 : const CPLErr eErr = poDS->Initialize(psOptions);
422 46 : if (eErr == CE_Failure)
423 : {
424 0 : psOptions->hDstDS = nullptr;
425 0 : delete poDS;
426 0 : return nullptr;
427 : }
428 :
429 46 : return poDS;
430 : }
431 :
432 : /*! @cond Doxygen_Suppress */
433 :
434 : /************************************************************************/
435 : /* ==================================================================== */
436 : /* VRTWarpedDataset */
437 : /* ==================================================================== */
438 : /************************************************************************/
439 :
440 : /************************************************************************/
441 : /* VRTWarpedDataset() */
442 : /************************************************************************/
443 :
444 372 : VRTWarpedDataset::VRTWarpedDataset(int nXSize, int nYSize, int nBlockXSize,
445 372 : int nBlockYSize)
446 : : VRTDataset(nXSize, nYSize,
447 371 : nBlockXSize > 0 ? nBlockXSize : std::min(nXSize, 512),
448 371 : nBlockYSize > 0 ? nBlockYSize : std::min(nYSize, 128)),
449 1114 : m_poWarper(nullptr), m_nSrcOvrLevel(-2)
450 : {
451 372 : eAccess = GA_Update;
452 372 : DisableReadWriteMutex();
453 372 : }
454 :
455 : /************************************************************************/
456 : /* ~VRTWarpedDataset() */
457 : /************************************************************************/
458 :
459 744 : VRTWarpedDataset::~VRTWarpedDataset()
460 :
461 : {
462 372 : VRTWarpedDataset::FlushCache(true);
463 372 : VRTWarpedDataset::CloseDependentDatasets();
464 744 : }
465 :
466 : /************************************************************************/
467 : /* CloseDependentDatasets() */
468 : /************************************************************************/
469 :
470 384 : int VRTWarpedDataset::CloseDependentDatasets()
471 : {
472 384 : bool bHasDroppedRef = CPL_TO_BOOL(VRTDataset::CloseDependentDatasets());
473 :
474 : /* -------------------------------------------------------------------- */
475 : /* Cleanup overviews. */
476 : /* -------------------------------------------------------------------- */
477 408 : for (auto &poDS : m_apoOverviews)
478 : {
479 24 : if (poDS && poDS->Release())
480 : {
481 0 : bHasDroppedRef = true;
482 : }
483 : }
484 :
485 384 : m_apoOverviews.clear();
486 :
487 : /* -------------------------------------------------------------------- */
488 : /* Cleanup warper if one is in effect. */
489 : /* -------------------------------------------------------------------- */
490 384 : if (m_poWarper != nullptr)
491 : {
492 370 : const GDALWarpOptions *psWO = m_poWarper->GetOptions();
493 :
494 : /* --------------------------------------------------------------------
495 : */
496 : /* We take care to only call GDALClose() on psWO->hSrcDS if the */
497 : /* reference count drops to zero. This is makes it so that we */
498 : /* can operate reference counting semantics more-or-less */
499 : /* properly even if the dataset isn't open in shared mode, */
500 : /* though we require that the caller also honour the reference */
501 : /* counting semantics even though it isn't a shared dataset. */
502 : /* --------------------------------------------------------------------
503 : */
504 370 : if (psWO != nullptr && psWO->hSrcDS != nullptr)
505 : {
506 369 : if (GDALReleaseDataset(psWO->hSrcDS))
507 : {
508 288 : bHasDroppedRef = true;
509 : }
510 : }
511 :
512 : /* --------------------------------------------------------------------
513 : */
514 : /* We are responsible for cleaning up the transformer ourselves. */
515 : /* --------------------------------------------------------------------
516 : */
517 370 : if (psWO != nullptr && psWO->pTransformerArg != nullptr)
518 369 : GDALDestroyTransformer(psWO->pTransformerArg);
519 :
520 370 : delete m_poWarper;
521 370 : m_poWarper = nullptr;
522 : }
523 :
524 : /* -------------------------------------------------------------------- */
525 : /* Destroy the raster bands if they exist. */
526 : /* -------------------------------------------------------------------- */
527 1081 : for (int iBand = 0; iBand < nBands; iBand++)
528 : {
529 697 : delete papoBands[iBand];
530 : }
531 384 : nBands = 0;
532 :
533 384 : return bHasDroppedRef;
534 : }
535 :
536 : /************************************************************************/
537 : /* SetSrcOverviewLevel() */
538 : /************************************************************************/
539 :
540 111 : CPLErr VRTWarpedDataset::SetMetadataItem(const char *pszName,
541 : const char *pszValue,
542 : const char *pszDomain)
543 :
544 : {
545 111 : if ((pszDomain == nullptr || EQUAL(pszDomain, "")) &&
546 111 : EQUAL(pszName, "SrcOvrLevel"))
547 : {
548 111 : const int nOldValue = m_nSrcOvrLevel;
549 111 : if (pszValue == nullptr || EQUAL(pszValue, "AUTO"))
550 1 : m_nSrcOvrLevel = -2;
551 110 : else if (STARTS_WITH_CI(pszValue, "AUTO-"))
552 1 : m_nSrcOvrLevel = -2 - atoi(pszValue + 5);
553 109 : else if (EQUAL(pszValue, "NONE"))
554 1 : m_nSrcOvrLevel = -1;
555 108 : else if (CPLGetValueType(pszValue) == CPL_VALUE_INTEGER)
556 108 : m_nSrcOvrLevel = atoi(pszValue);
557 111 : if (m_nSrcOvrLevel != nOldValue)
558 2 : SetNeedsFlush();
559 111 : return CE_None;
560 : }
561 0 : return VRTDataset::SetMetadataItem(pszName, pszValue, pszDomain);
562 : }
563 :
564 : /************************************************************************/
565 : /* VRTWarpedAddOptions() */
566 : /************************************************************************/
567 :
568 370 : static char **VRTWarpedAddOptions(char **papszWarpOptions)
569 : {
570 : /* Avoid errors when adding an alpha band, but source dataset has */
571 : /* no alpha band (#4571), and generally don't leave our buffer uninitialized
572 : */
573 370 : if (CSLFetchNameValue(papszWarpOptions, "INIT_DEST") == nullptr)
574 52 : papszWarpOptions = CSLSetNameValue(papszWarpOptions, "INIT_DEST", "0");
575 :
576 : /* For https://github.com/OSGeo/gdal/issues/1985 */
577 370 : if (CSLFetchNameValue(papszWarpOptions,
578 370 : "ERROR_OUT_IF_EMPTY_SOURCE_WINDOW") == nullptr)
579 : {
580 240 : papszWarpOptions = CSLSetNameValue(
581 : papszWarpOptions, "ERROR_OUT_IF_EMPTY_SOURCE_WINDOW", "FALSE");
582 : }
583 370 : return papszWarpOptions;
584 : }
585 :
586 : /************************************************************************/
587 : /* Initialize() */
588 : /* */
589 : /* Initialize a dataset from passed in warp options. */
590 : /************************************************************************/
591 :
592 171 : CPLErr VRTWarpedDataset::Initialize(void *psWO)
593 :
594 : {
595 171 : if (m_poWarper != nullptr)
596 0 : delete m_poWarper;
597 :
598 171 : m_poWarper = new GDALWarpOperation();
599 :
600 : GDALWarpOptions *psWO_Dup =
601 171 : GDALCloneWarpOptions(static_cast<GDALWarpOptions *>(psWO));
602 :
603 171 : psWO_Dup->papszWarpOptions =
604 171 : VRTWarpedAddOptions(psWO_Dup->papszWarpOptions);
605 :
606 171 : CPLErr eErr = m_poWarper->Initialize(psWO_Dup);
607 :
608 : // The act of initializing this warped dataset with this warp options
609 : // will result in our assuming ownership of a reference to the
610 : // hSrcDS.
611 :
612 171 : if (eErr == CE_None &&
613 170 : static_cast<GDALWarpOptions *>(psWO)->hSrcDS != nullptr)
614 : {
615 170 : GDALReferenceDataset(psWO_Dup->hSrcDS);
616 : }
617 :
618 171 : GDALDestroyWarpOptions(psWO_Dup);
619 :
620 171 : if (nBands > 1)
621 : {
622 35 : GDALDataset::SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
623 : }
624 :
625 171 : return eErr;
626 : }
627 :
628 : /************************************************************************/
629 : /* GDALWarpCoordRescaler */
630 : /************************************************************************/
631 :
632 1 : class GDALWarpCoordRescaler final : public OGRCoordinateTransformation
633 : {
634 : const double m_dfRatioX;
635 : const double m_dfRatioY;
636 :
637 : GDALWarpCoordRescaler &operator=(const GDALWarpCoordRescaler &) = delete;
638 : GDALWarpCoordRescaler(GDALWarpCoordRescaler &&) = delete;
639 : GDALWarpCoordRescaler &operator=(GDALWarpCoordRescaler &&) = delete;
640 :
641 : public:
642 1 : GDALWarpCoordRescaler(double dfRatioX, double dfRatioY)
643 1 : : m_dfRatioX(dfRatioX), m_dfRatioY(dfRatioY)
644 : {
645 1 : }
646 :
647 0 : GDALWarpCoordRescaler(const GDALWarpCoordRescaler &) = default;
648 :
649 : ~GDALWarpCoordRescaler() override;
650 :
651 0 : const OGRSpatialReference *GetSourceCS() const override
652 : {
653 0 : return nullptr;
654 : }
655 :
656 6 : const OGRSpatialReference *GetTargetCS() const override
657 : {
658 6 : return nullptr;
659 : }
660 :
661 3 : virtual int Transform(size_t nCount, double *x, double *y, double * /*z*/,
662 : double * /*t*/, int *pabSuccess) override
663 : {
664 17 : for (size_t i = 0; i < nCount; i++)
665 : {
666 14 : x[i] *= m_dfRatioX;
667 14 : y[i] *= m_dfRatioY;
668 14 : if (pabSuccess)
669 14 : pabSuccess[i] = TRUE;
670 : }
671 3 : return TRUE;
672 : }
673 :
674 0 : OGRCoordinateTransformation *Clone() const override
675 : {
676 0 : return new GDALWarpCoordRescaler(*this);
677 : }
678 :
679 0 : OGRCoordinateTransformation *GetInverse() const override
680 : {
681 0 : return nullptr;
682 : }
683 : };
684 :
685 : GDALWarpCoordRescaler::~GDALWarpCoordRescaler() = default;
686 :
687 : /************************************************************************/
688 : /* RescaleDstGeoTransform() */
689 : /************************************************************************/
690 :
691 22 : static void RescaleDstGeoTransform(double adfDstGeoTransform[6],
692 : int nRasterXSize, int nDstPixels,
693 : int nRasterYSize, int nDstLines)
694 : {
695 22 : adfDstGeoTransform[1] *= static_cast<double>(nRasterXSize) / nDstPixels;
696 22 : adfDstGeoTransform[2] *= static_cast<double>(nRasterXSize) / nDstPixels;
697 22 : adfDstGeoTransform[4] *= static_cast<double>(nRasterYSize) / nDstLines;
698 22 : adfDstGeoTransform[5] *= static_cast<double>(nRasterYSize) / nDstLines;
699 22 : }
700 :
701 : /************************************************************************/
702 : /* GetSrcOverviewLevel() */
703 : /************************************************************************/
704 :
705 79 : int VRTWarpedDataset::GetSrcOverviewLevel(int iOvr,
706 : bool &bThisLevelOnlyOut) const
707 : {
708 79 : bThisLevelOnlyOut = false;
709 79 : if (m_nSrcOvrLevel < -2)
710 : {
711 6 : if (iOvr + m_nSrcOvrLevel + 2 >= 0)
712 : {
713 3 : return iOvr + m_nSrcOvrLevel + 2;
714 : }
715 : }
716 73 : else if (m_nSrcOvrLevel == -2)
717 : {
718 67 : return iOvr;
719 : }
720 6 : else if (m_nSrcOvrLevel >= 0)
721 : {
722 0 : bThisLevelOnlyOut = true;
723 0 : return m_nSrcOvrLevel;
724 : }
725 9 : return -1;
726 : }
727 :
728 : /************************************************************************/
729 : /* GetOverviewSize() */
730 : /************************************************************************/
731 :
732 73 : bool VRTWarpedDataset::GetOverviewSize(GDALDataset *poSrcDS, int iOvr,
733 : int iSrcOvr, int &nOvrXSize,
734 : int &nOvrYSize, double &dfSrcRatioX,
735 : double &dfSrcRatioY) const
736 : {
737 : auto poSrcOvrBand = iSrcOvr >= 0
738 73 : ? poSrcDS->GetRasterBand(1)->GetOverview(iSrcOvr)
739 3 : : poSrcDS->GetRasterBand(1);
740 73 : if (!poSrcOvrBand)
741 : {
742 0 : return false;
743 : }
744 73 : dfSrcRatioX = static_cast<double>(poSrcDS->GetRasterXSize()) /
745 73 : poSrcOvrBand->GetXSize();
746 73 : dfSrcRatioY = static_cast<double>(poSrcDS->GetRasterYSize()) /
747 73 : poSrcOvrBand->GetYSize();
748 : const double dfTargetRatio =
749 73 : static_cast<double>(poSrcDS->GetRasterXSize()) /
750 73 : poSrcDS->GetRasterBand(1)->GetOverview(iOvr)->GetXSize();
751 :
752 73 : nOvrXSize = static_cast<int>(nRasterXSize / dfTargetRatio + 0.5);
753 73 : nOvrYSize = static_cast<int>(nRasterYSize / dfTargetRatio + 0.5);
754 73 : return nOvrXSize >= 1 && nOvrYSize >= 1;
755 : }
756 :
757 : /************************************************************************/
758 : /* CreateImplicitOverview() */
759 : /************************************************************************/
760 :
761 20 : VRTWarpedDataset *VRTWarpedDataset::CreateImplicitOverview(int iOvr) const
762 : {
763 20 : if (!m_poWarper)
764 0 : return nullptr;
765 20 : const GDALWarpOptions *psWO = m_poWarper->GetOptions();
766 20 : if (!psWO->hSrcDS || GDALGetRasterCount(psWO->hSrcDS) == 0)
767 0 : return nullptr;
768 20 : GDALDataset *poSrcDS = GDALDataset::FromHandle(psWO->hSrcDS);
769 20 : GDALDataset *poSrcOvrDS = poSrcDS;
770 20 : bool bThisLevelOnly = false;
771 20 : const int iSrcOvr = GetSrcOverviewLevel(iOvr, bThisLevelOnly);
772 20 : if (iSrcOvr >= 0)
773 : {
774 : poSrcOvrDS =
775 17 : GDALCreateOverviewDataset(poSrcDS, iSrcOvr, bThisLevelOnly);
776 : }
777 20 : if (poSrcOvrDS == nullptr)
778 0 : return nullptr;
779 20 : if (poSrcOvrDS == poSrcDS)
780 3 : poSrcOvrDS->Reference();
781 :
782 20 : int nDstPixels = 0;
783 20 : int nDstLines = 0;
784 20 : double dfSrcRatioX = 0;
785 20 : double dfSrcRatioY = 0;
786 : // Figure out the desired output bounds and resolution.
787 20 : if (!GetOverviewSize(poSrcDS, iOvr, iSrcOvr, nDstPixels, nDstLines,
788 : dfSrcRatioX, dfSrcRatioY))
789 : {
790 0 : poSrcOvrDS->ReleaseRef();
791 0 : return nullptr;
792 : }
793 :
794 : /* --------------------------------------------------------------------
795 : */
796 : /* Create transformer and warping options. */
797 : /* --------------------------------------------------------------------
798 : */
799 40 : void *pTransformerArg = GDALCreateSimilarTransformer(
800 20 : psWO->pTransformerArg, dfSrcRatioX, dfSrcRatioY);
801 20 : if (pTransformerArg == nullptr)
802 : {
803 0 : poSrcOvrDS->ReleaseRef();
804 0 : return nullptr;
805 : }
806 :
807 20 : GDALWarpOptions *psWOOvr = GDALCloneWarpOptions(psWO);
808 20 : psWOOvr->hSrcDS = poSrcOvrDS;
809 20 : psWOOvr->pfnTransformer = psWO->pfnTransformer;
810 20 : psWOOvr->pTransformerArg = pTransformerArg;
811 :
812 : /* --------------------------------------------------------------------
813 : */
814 : /* We need to rescale the potential CUTLINE */
815 : /* --------------------------------------------------------------------
816 : */
817 20 : if (psWOOvr->hCutline)
818 : {
819 2 : GDALWarpCoordRescaler oRescaler(1.0 / dfSrcRatioX, 1.0 / dfSrcRatioY);
820 1 : static_cast<OGRGeometry *>(psWOOvr->hCutline)->transform(&oRescaler);
821 : }
822 :
823 : /* --------------------------------------------------------------------
824 : */
825 : /* Rescale the output geotransform on the transformer. */
826 : /* --------------------------------------------------------------------
827 : */
828 20 : double adfDstGeoTransform[6] = {0.0};
829 20 : GDALGetTransformerDstGeoTransform(psWOOvr->pTransformerArg,
830 : adfDstGeoTransform);
831 20 : RescaleDstGeoTransform(adfDstGeoTransform, nRasterXSize, nDstPixels,
832 20 : nRasterYSize, nDstLines);
833 20 : GDALSetTransformerDstGeoTransform(psWOOvr->pTransformerArg,
834 : adfDstGeoTransform);
835 :
836 : /* --------------------------------------------------------------------
837 : */
838 : /* Create the VRT file. */
839 : /* --------------------------------------------------------------------
840 : */
841 20 : GDALDatasetH hDstDS = GDALCreateWarpedVRT(poSrcOvrDS, nDstPixels, nDstLines,
842 : adfDstGeoTransform, psWOOvr);
843 :
844 20 : poSrcOvrDS->ReleaseRef();
845 :
846 20 : GDALDestroyWarpOptions(psWOOvr);
847 :
848 20 : if (hDstDS == nullptr)
849 : {
850 0 : GDALDestroyTransformer(pTransformerArg);
851 0 : return nullptr;
852 : }
853 :
854 20 : auto poOvrDS = static_cast<VRTWarpedDataset *>(hDstDS);
855 20 : poOvrDS->m_bIsOverview = true;
856 20 : return poOvrDS;
857 : }
858 :
859 : /************************************************************************/
860 : /* GetOverviewCount() */
861 : /************************************************************************/
862 :
863 69 : int VRTWarpedDataset::GetOverviewCount() const
864 : {
865 69 : if (m_poWarper)
866 : {
867 69 : const GDALWarpOptions *psWO = m_poWarper->GetOptions();
868 69 : if (!m_bIsOverview && psWO->hSrcDS && GDALGetRasterCount(psWO->hSrcDS))
869 : {
870 69 : GDALDataset *poSrcDS = GDALDataset::FromHandle(psWO->hSrcDS);
871 : int nSrcOverviewCount =
872 69 : poSrcDS->GetRasterBand(1)->GetOverviewCount();
873 69 : int nCount = 0;
874 128 : for (int i = 0; i < nSrcOverviewCount; ++i)
875 : {
876 59 : bool bThisLevelOnly = false;
877 59 : const int iSrcOvr = GetSrcOverviewLevel(i, bThisLevelOnly);
878 59 : if (iSrcOvr >= 0)
879 : {
880 53 : int nDstPixels = 0;
881 53 : int nDstLines = 0;
882 53 : double dfSrcRatioX = 0;
883 53 : double dfSrcRatioY = 0;
884 53 : if (!GetOverviewSize(poSrcDS, i, iSrcOvr, nDstPixels,
885 : nDstLines, dfSrcRatioX, dfSrcRatioY))
886 : {
887 0 : break;
888 : }
889 : }
890 59 : ++nCount;
891 : }
892 69 : return nCount;
893 : }
894 : }
895 0 : return 0;
896 : }
897 :
898 : /************************************************************************/
899 : /* CreateImplicitOverviews() */
900 : /* */
901 : /* For each overview of the source dataset, create an overview */
902 : /* in the warped VRT dataset. */
903 : /************************************************************************/
904 :
905 8 : void VRTWarpedDataset::CreateImplicitOverviews()
906 : {
907 8 : if (m_bIsOverview)
908 0 : return;
909 8 : const int nOvrCount = GetOverviewCount();
910 8 : if (m_apoOverviews.empty())
911 4 : m_apoOverviews.resize(nOvrCount);
912 18 : for (int iOvr = 0; iOvr < nOvrCount; iOvr++)
913 : {
914 10 : if (!m_apoOverviews[iOvr])
915 : {
916 2 : m_apoOverviews[iOvr] = CreateImplicitOverview(iOvr);
917 : }
918 : }
919 : }
920 :
921 : /************************************************************************/
922 : /* GetFileList() */
923 : /************************************************************************/
924 :
925 20 : char **VRTWarpedDataset::GetFileList()
926 : {
927 20 : char **papszFileList = GDALDataset::GetFileList();
928 :
929 20 : if (m_poWarper != nullptr)
930 : {
931 20 : const GDALWarpOptions *psWO = m_poWarper->GetOptions();
932 20 : const char *pszFilename = nullptr;
933 :
934 40 : if (psWO->hSrcDS != nullptr &&
935 : (pszFilename =
936 20 : static_cast<GDALDataset *>(psWO->hSrcDS)->GetDescription()) !=
937 : nullptr)
938 : {
939 : VSIStatBufL sStat;
940 20 : if (VSIStatL(pszFilename, &sStat) == 0)
941 : {
942 20 : papszFileList = CSLAddString(papszFileList, pszFilename);
943 : }
944 : }
945 : }
946 :
947 20 : return papszFileList;
948 : }
949 :
950 : /************************************************************************/
951 : /* ==================================================================== */
952 : /* VRTWarpedOverviewTransformer */
953 : /* ==================================================================== */
954 : /************************************************************************/
955 :
956 : typedef struct
957 : {
958 : GDALTransformerInfo sTI;
959 :
960 : GDALTransformerFunc pfnBaseTransformer;
961 : void *pBaseTransformerArg;
962 : bool bOwnSubtransformer;
963 :
964 : double dfXOverviewFactor;
965 : double dfYOverviewFactor;
966 : } VWOTInfo;
967 :
968 : static void *VRTCreateWarpedOverviewTransformer(
969 : GDALTransformerFunc pfnBaseTransformer, void *pBaseTransformArg,
970 : double dfXOverviewFactor, double dfYOverviewFactor);
971 : static void VRTDestroyWarpedOverviewTransformer(void *pTransformArg);
972 :
973 : static int VRTWarpedOverviewTransform(void *pTransformArg, int bDstToSrc,
974 : int nPointCount, double *padfX,
975 : double *padfY, double *padfZ,
976 : int *panSuccess);
977 :
978 : #if 0 // TODO: Why?
979 : /************************************************************************/
980 : /* VRTSerializeWarpedOverviewTransformer() */
981 : /************************************************************************/
982 :
983 : static CPLXMLNode *
984 : VRTSerializeWarpedOverviewTransformer( void *pTransformArg )
985 :
986 : {
987 : VWOTInfo *psInfo = static_cast<VWOTInfo *>( pTransformArg );
988 :
989 : CPLXMLNode *psTree
990 : = CPLCreateXMLNode( NULL, CXT_Element, "WarpedOverviewTransformer" );
991 :
992 : CPLCreateXMLElementAndValue(
993 : psTree, "XFactor",
994 : CPLString().Printf("%g",psInfo->dfXOverviewFactor) );
995 : CPLCreateXMLElementAndValue(
996 : psTree, "YFactor",
997 : CPLString().Printf("%g",psInfo->dfYOverviewFactor) );
998 :
999 : /* -------------------------------------------------------------------- */
1000 : /* Capture underlying transformer. */
1001 : /* -------------------------------------------------------------------- */
1002 : CPLXMLNode *psTransformerContainer
1003 : = CPLCreateXMLNode( psTree, CXT_Element, "BaseTransformer" );
1004 :
1005 : CPLXMLNode *psTransformer
1006 : = GDALSerializeTransformer( psInfo->pfnBaseTransformer,
1007 : psInfo->pBaseTransformerArg );
1008 : if( psTransformer != NULL )
1009 : CPLAddXMLChild( psTransformerContainer, psTransformer );
1010 :
1011 : return psTree;
1012 : }
1013 :
1014 : /************************************************************************/
1015 : /* VRTWarpedOverviewTransformerOwnsSubtransformer() */
1016 : /************************************************************************/
1017 :
1018 : static void VRTWarpedOverviewTransformerOwnsSubtransformer( void *pTransformArg,
1019 : bool bOwnFlag )
1020 : {
1021 : VWOTInfo *psInfo = static_cast<VWOTInfo *>( pTransformArg );
1022 :
1023 : psInfo->bOwnSubtransformer = bOwnFlag;
1024 : }
1025 :
1026 : /************************************************************************/
1027 : /* VRTDeserializeWarpedOverviewTransformer() */
1028 : /************************************************************************/
1029 :
1030 : void* VRTDeserializeWarpedOverviewTransformer( CPLXMLNode *psTree )
1031 :
1032 : {
1033 : const double dfXOverviewFactor =
1034 : CPLAtof(CPLGetXMLValue( psTree, "XFactor", "1" ));
1035 : const double dfYOverviewFactor =
1036 : CPLAtof(CPLGetXMLValue( psTree, "YFactor", "1" ));
1037 : GDALTransformerFunc pfnBaseTransform = NULL;
1038 : void *pBaseTransformerArg = NULL;
1039 :
1040 : CPLXMLNode *psContainer = CPLGetXMLNode( psTree, "BaseTransformer" );
1041 :
1042 : if( psContainer != NULL && psContainer->psChild != NULL )
1043 : {
1044 : GDALDeserializeTransformer( psContainer->psChild,
1045 : &pfnBaseTransform,
1046 : &pBaseTransformerArg );
1047 : }
1048 :
1049 : if( pfnBaseTransform == NULL )
1050 : {
1051 : CPLError( CE_Failure, CPLE_AppDefined,
1052 : "Cannot get base transform for scaled coord transformer." );
1053 : return NULL;
1054 : }
1055 : else
1056 : {
1057 : void *pApproxCBData =
1058 : VRTCreateWarpedOverviewTransformer( pfnBaseTransform,
1059 : pBaseTransformerArg,
1060 : dfXOverviewFactor,
1061 : dfYOverviewFactor );
1062 : VRTWarpedOverviewTransformerOwnsSubtransformer( pApproxCBData, true );
1063 :
1064 : return pApproxCBData;
1065 : }
1066 : }
1067 : #endif // TODO: Why disabled?
1068 :
1069 : /************************************************************************/
1070 : /* VRTCreateWarpedOverviewTransformer() */
1071 : /************************************************************************/
1072 :
1073 4 : static void *VRTCreateWarpedOverviewTransformer(
1074 : GDALTransformerFunc pfnBaseTransformer, void *pBaseTransformerArg,
1075 : double dfXOverviewFactor, double dfYOverviewFactor)
1076 :
1077 : {
1078 4 : if (pfnBaseTransformer == nullptr)
1079 0 : return nullptr;
1080 :
1081 4 : VWOTInfo *psSCTInfo = static_cast<VWOTInfo *>(CPLMalloc(sizeof(VWOTInfo)));
1082 4 : psSCTInfo->pfnBaseTransformer = pfnBaseTransformer;
1083 4 : psSCTInfo->pBaseTransformerArg = pBaseTransformerArg;
1084 4 : psSCTInfo->dfXOverviewFactor = dfXOverviewFactor;
1085 4 : psSCTInfo->dfYOverviewFactor = dfYOverviewFactor;
1086 4 : psSCTInfo->bOwnSubtransformer = false;
1087 :
1088 4 : memcpy(psSCTInfo->sTI.abySignature, GDAL_GTI2_SIGNATURE,
1089 : strlen(GDAL_GTI2_SIGNATURE));
1090 4 : psSCTInfo->sTI.pszClassName = "VRTWarpedOverviewTransformer";
1091 4 : psSCTInfo->sTI.pfnTransform = VRTWarpedOverviewTransform;
1092 4 : psSCTInfo->sTI.pfnCleanup = VRTDestroyWarpedOverviewTransformer;
1093 : #if 0
1094 : psSCTInfo->sTI.pfnSerialize = VRTSerializeWarpedOverviewTransformer;
1095 : #endif
1096 4 : return psSCTInfo;
1097 : }
1098 :
1099 : /************************************************************************/
1100 : /* VRTDestroyWarpedOverviewTransformer() */
1101 : /************************************************************************/
1102 :
1103 4 : static void VRTDestroyWarpedOverviewTransformer(void *pTransformArg)
1104 : {
1105 4 : VWOTInfo *psInfo = static_cast<VWOTInfo *>(pTransformArg);
1106 :
1107 4 : if (psInfo->bOwnSubtransformer)
1108 0 : GDALDestroyTransformer(psInfo->pBaseTransformerArg);
1109 :
1110 4 : CPLFree(psInfo);
1111 4 : }
1112 :
1113 : /************************************************************************/
1114 : /* VRTWarpedOverviewTransform() */
1115 : /************************************************************************/
1116 :
1117 262 : static int VRTWarpedOverviewTransform(void *pTransformArg, int bDstToSrc,
1118 : int nPointCount, double *padfX,
1119 : double *padfY, double *padfZ,
1120 : int *panSuccess)
1121 :
1122 : {
1123 262 : VWOTInfo *psInfo = static_cast<VWOTInfo *>(pTransformArg);
1124 :
1125 262 : if (bDstToSrc)
1126 : {
1127 63871 : for (int i = 0; i < nPointCount; i++)
1128 : {
1129 63609 : padfX[i] *= psInfo->dfXOverviewFactor;
1130 63609 : padfY[i] *= psInfo->dfYOverviewFactor;
1131 : }
1132 : }
1133 :
1134 262 : const int bSuccess = psInfo->pfnBaseTransformer(
1135 : psInfo->pBaseTransformerArg, bDstToSrc, nPointCount, padfX, padfY,
1136 : padfZ, panSuccess);
1137 :
1138 262 : if (!bDstToSrc)
1139 : {
1140 0 : for (int i = 0; i < nPointCount; i++)
1141 : {
1142 0 : padfX[i] /= psInfo->dfXOverviewFactor;
1143 0 : padfY[i] /= psInfo->dfYOverviewFactor;
1144 : }
1145 : }
1146 :
1147 262 : return bSuccess;
1148 : }
1149 :
1150 : /************************************************************************/
1151 : /* BuildOverviews() */
1152 : /* */
1153 : /* For overviews, we actually just build a whole new dataset */
1154 : /* with an extra layer of transformation on the warper used to */
1155 : /* accomplish downsampling by the desired factor. */
1156 : /************************************************************************/
1157 :
1158 6 : CPLErr VRTWarpedDataset::IBuildOverviews(
1159 : const char * /* pszResampling */, int nOverviews,
1160 : const int *panOverviewList, int /* nListBands */,
1161 : const int * /* panBandList */, GDALProgressFunc pfnProgress,
1162 : void *pProgressData, CSLConstList /*papszOptions*/)
1163 : {
1164 6 : if (m_poWarper == nullptr || m_bIsOverview)
1165 0 : return CE_Failure;
1166 :
1167 : /* -------------------------------------------------------------------- */
1168 : /* Initial progress result. */
1169 : /* -------------------------------------------------------------------- */
1170 6 : if (!pfnProgress(0.0, nullptr, pProgressData))
1171 : {
1172 0 : CPLError(CE_Failure, CPLE_UserInterrupt, "User terminated");
1173 0 : return CE_Failure;
1174 : }
1175 :
1176 6 : CreateImplicitOverviews();
1177 :
1178 : /* -------------------------------------------------------------------- */
1179 : /* Establish which of the overview levels we already have, and */
1180 : /* which are new. */
1181 : /* -------------------------------------------------------------------- */
1182 6 : int nNewOverviews = 0;
1183 : int *panNewOverviewList =
1184 6 : static_cast<int *>(CPLCalloc(sizeof(int), nOverviews));
1185 6 : std::vector<bool> abFoundOverviewFactor(nOverviews);
1186 14 : for (int i = 0; i < nOverviews; i++)
1187 : {
1188 20 : for (GDALDataset *const poOverview : m_apoOverviews)
1189 : {
1190 12 : if (poOverview)
1191 : {
1192 12 : const int nOvFactor = GDALComputeOvFactor(
1193 : poOverview->GetRasterXSize(), GetRasterXSize(),
1194 : poOverview->GetRasterYSize(), GetRasterYSize());
1195 :
1196 20 : if (nOvFactor == panOverviewList[i] ||
1197 8 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
1198 : GetRasterXSize(),
1199 : GetRasterYSize()))
1200 4 : abFoundOverviewFactor[i] = true;
1201 : }
1202 : }
1203 :
1204 8 : if (!abFoundOverviewFactor[i])
1205 4 : panNewOverviewList[nNewOverviews++] = panOverviewList[i];
1206 : }
1207 :
1208 : /* -------------------------------------------------------------------- */
1209 : /* Create each missing overview (we don't need to do anything */
1210 : /* to update existing overviews). */
1211 : /* -------------------------------------------------------------------- */
1212 6 : CPLErr eErr = CE_None;
1213 10 : for (int i = 0; i < nNewOverviews; i++)
1214 : {
1215 : /* --------------------------------------------------------------------
1216 : */
1217 : /* What size should this overview be. */
1218 : /* --------------------------------------------------------------------
1219 : */
1220 4 : const int nOXSize = (GetRasterXSize() + panNewOverviewList[i] - 1) /
1221 4 : panNewOverviewList[i];
1222 :
1223 4 : const int nOYSize = (GetRasterYSize() + panNewOverviewList[i] - 1) /
1224 4 : panNewOverviewList[i];
1225 :
1226 : /* --------------------------------------------------------------------
1227 : */
1228 : /* Find the most appropriate base dataset onto which to build the
1229 : */
1230 : /* new one. The preference will be an overview dataset with a
1231 : * ratio*/
1232 : /* greater than ours, and which is not using */
1233 : /* VRTWarpedOverviewTransform, since those ones are slow. The
1234 : * other*/
1235 : /* ones are based on overviews of the source dataset. */
1236 : /* --------------------------------------------------------------------
1237 : */
1238 4 : VRTWarpedDataset *poBaseDataset = this;
1239 8 : for (auto *poOverview : m_apoOverviews)
1240 : {
1241 4 : if (poOverview && poOverview->GetRasterXSize() > nOXSize &&
1242 4 : poOverview->m_poWarper->GetOptions()->pfnTransformer !=
1243 8 : VRTWarpedOverviewTransform &&
1244 4 : poOverview->GetRasterXSize() < poBaseDataset->GetRasterXSize())
1245 : {
1246 4 : poBaseDataset = poOverview;
1247 : }
1248 : }
1249 :
1250 : /* --------------------------------------------------------------------
1251 : */
1252 : /* Create the overview dataset. */
1253 : /* --------------------------------------------------------------------
1254 : */
1255 4 : VRTWarpedDataset *poOverviewDS = new VRTWarpedDataset(nOXSize, nOYSize);
1256 :
1257 8 : for (int iBand = 0; iBand < GetRasterCount(); iBand++)
1258 : {
1259 4 : GDALRasterBand *const poOldBand = GetRasterBand(iBand + 1);
1260 : VRTWarpedRasterBand *const poNewBand = new VRTWarpedRasterBand(
1261 4 : poOverviewDS, iBand + 1, poOldBand->GetRasterDataType());
1262 :
1263 4 : poNewBand->CopyCommonInfoFrom(poOldBand);
1264 4 : poOverviewDS->SetBand(iBand + 1, poNewBand);
1265 : }
1266 :
1267 : /* --------------------------------------------------------------------
1268 : */
1269 : /* Prepare update transformation information that will apply */
1270 : /* the overview decimation. */
1271 : /* --------------------------------------------------------------------
1272 : */
1273 : GDALWarpOptions *psWO = const_cast<GDALWarpOptions *>(
1274 4 : poBaseDataset->m_poWarper->GetOptions());
1275 :
1276 : /* --------------------------------------------------------------------
1277 : */
1278 : /* Initialize the new dataset with adjusted warp options, and */
1279 : /* then restore to original condition. */
1280 : /* --------------------------------------------------------------------
1281 : */
1282 4 : GDALTransformerFunc pfnTransformerBase = psWO->pfnTransformer;
1283 4 : void *pTransformerBaseArg = psWO->pTransformerArg;
1284 :
1285 4 : psWO->pfnTransformer = VRTWarpedOverviewTransform;
1286 12 : psWO->pTransformerArg = VRTCreateWarpedOverviewTransformer(
1287 : pfnTransformerBase, pTransformerBaseArg,
1288 4 : poBaseDataset->GetRasterXSize() / static_cast<double>(nOXSize),
1289 4 : poBaseDataset->GetRasterYSize() / static_cast<double>(nOYSize));
1290 :
1291 4 : eErr = poOverviewDS->Initialize(psWO);
1292 :
1293 4 : psWO->pfnTransformer = pfnTransformerBase;
1294 4 : psWO->pTransformerArg = pTransformerBaseArg;
1295 :
1296 4 : if (eErr != CE_None)
1297 : {
1298 0 : delete poOverviewDS;
1299 0 : break;
1300 : }
1301 :
1302 4 : m_apoOverviews.push_back(poOverviewDS);
1303 : }
1304 :
1305 6 : CPLFree(panNewOverviewList);
1306 :
1307 : /* -------------------------------------------------------------------- */
1308 : /* Progress finished. */
1309 : /* -------------------------------------------------------------------- */
1310 6 : pfnProgress(1.0, nullptr, pProgressData);
1311 :
1312 6 : SetNeedsFlush();
1313 :
1314 6 : return eErr;
1315 : }
1316 :
1317 : /*! @endcond */
1318 :
1319 : /************************************************************************/
1320 : /* GDALInitializeWarpedVRT() */
1321 : /************************************************************************/
1322 :
1323 : /**
1324 : * Set warp info on virtual warped dataset.
1325 : *
1326 : * Initializes all the warping information for a virtual warped dataset.
1327 : *
1328 : * This method is the same as the C++ method VRTWarpedDataset::Initialize().
1329 : *
1330 : * @param hDS dataset previously created with the VRT driver, and a
1331 : * SUBCLASS of "VRTWarpedDataset".
1332 : *
1333 : * @param psWO the warp options to apply. Note that ownership of the
1334 : * transformation information is taken over by the function though everything
1335 : * else remains the property of the caller.
1336 : *
1337 : * @return CE_None on success or CE_Failure if an error occurs.
1338 : */
1339 :
1340 108 : CPLErr CPL_STDCALL GDALInitializeWarpedVRT(GDALDatasetH hDS,
1341 : GDALWarpOptions *psWO)
1342 :
1343 : {
1344 108 : VALIDATE_POINTER1(hDS, "GDALInitializeWarpedVRT", CE_Failure);
1345 :
1346 108 : return static_cast<VRTWarpedDataset *>(GDALDataset::FromHandle(hDS))
1347 108 : ->Initialize(psWO);
1348 : }
1349 :
1350 : /*! @cond Doxygen_Suppress */
1351 :
1352 : /************************************************************************/
1353 : /* XMLInit() */
1354 : /************************************************************************/
1355 :
1356 201 : CPLErr VRTWarpedDataset::XMLInit(const CPLXMLNode *psTree,
1357 : const char *pszVRTPathIn)
1358 :
1359 : {
1360 :
1361 : /* -------------------------------------------------------------------- */
1362 : /* Initialize blocksize before calling sub-init so that the */
1363 : /* band initializers can get it from the dataset object when */
1364 : /* they are created. */
1365 : /* -------------------------------------------------------------------- */
1366 201 : m_nBlockXSize = atoi(CPLGetXMLValue(psTree, "BlockXSize", "512"));
1367 201 : m_nBlockYSize = atoi(CPLGetXMLValue(psTree, "BlockYSize", "128"));
1368 :
1369 : /* -------------------------------------------------------------------- */
1370 : /* Initialize all the general VRT stuff. This will even */
1371 : /* create the VRTWarpedRasterBands and initialize them. */
1372 : /* -------------------------------------------------------------------- */
1373 : {
1374 201 : const CPLErr eErr = VRTDataset::XMLInit(psTree, pszVRTPathIn);
1375 :
1376 201 : if (eErr != CE_None)
1377 0 : return eErr;
1378 : }
1379 :
1380 : // Check that band block sizes didn't change the dataset block size.
1381 650 : for (int i = 1; i <= nBands; i++)
1382 : {
1383 451 : int nBlockXSize = 0;
1384 451 : int nBlockYSize = 0;
1385 451 : GetRasterBand(i)->GetBlockSize(&nBlockXSize, &nBlockYSize);
1386 451 : if (nBlockXSize != m_nBlockXSize || nBlockYSize != m_nBlockYSize)
1387 : {
1388 2 : CPLError(CE_Failure, CPLE_AppDefined,
1389 : "Block size specified on band %d not consistent with "
1390 : "dataset block size",
1391 : i);
1392 2 : return CE_Failure;
1393 : }
1394 : }
1395 :
1396 199 : if (nBands > 1)
1397 : {
1398 93 : GDALDataset::SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
1399 : }
1400 :
1401 : /* -------------------------------------------------------------------- */
1402 : /* Find the GDALWarpOptions XML tree. */
1403 : /* -------------------------------------------------------------------- */
1404 : const CPLXMLNode *const psOptionsTree =
1405 199 : CPLGetXMLNode(psTree, "GDALWarpOptions");
1406 199 : if (psOptionsTree == nullptr)
1407 : {
1408 0 : CPLError(CE_Failure, CPLE_AppDefined,
1409 : "Count not find required GDALWarpOptions in XML.");
1410 0 : return CE_Failure;
1411 : }
1412 :
1413 : /* -------------------------------------------------------------------- */
1414 : /* Adjust the SourceDataset in the warp options to take into */
1415 : /* account that it is relative to the VRT if appropriate. */
1416 : /* -------------------------------------------------------------------- */
1417 199 : const bool bRelativeToVRT = CPL_TO_BOOL(atoi(
1418 : CPLGetXMLValue(psOptionsTree, "SourceDataset.relativeToVRT", "0")));
1419 :
1420 : const char *pszRelativePath =
1421 199 : CPLGetXMLValue(psOptionsTree, "SourceDataset", "");
1422 199 : char *pszAbsolutePath = nullptr;
1423 :
1424 199 : if (bRelativeToVRT)
1425 132 : pszAbsolutePath = CPLStrdup(
1426 264 : CPLProjectRelativeFilenameSafe(pszVRTPathIn, pszRelativePath)
1427 : .c_str());
1428 : else
1429 67 : pszAbsolutePath = CPLStrdup(pszRelativePath);
1430 :
1431 199 : CPLXMLNode *psOptionsTreeCloned = CPLCloneXMLTree(psOptionsTree);
1432 199 : CPLSetXMLValue(psOptionsTreeCloned, "SourceDataset", pszAbsolutePath);
1433 199 : CPLFree(pszAbsolutePath);
1434 :
1435 : /* -------------------------------------------------------------------- */
1436 : /* And instantiate the warp options, and corresponding warp */
1437 : /* operation. */
1438 : /* -------------------------------------------------------------------- */
1439 199 : GDALWarpOptions *psWO = GDALDeserializeWarpOptions(psOptionsTreeCloned);
1440 199 : CPLDestroyXMLNode(psOptionsTreeCloned);
1441 199 : if (psWO == nullptr)
1442 0 : return CE_Failure;
1443 :
1444 199 : psWO->papszWarpOptions = VRTWarpedAddOptions(psWO->papszWarpOptions);
1445 :
1446 199 : eAccess = GA_Update;
1447 :
1448 199 : if (psWO->hDstDS != nullptr)
1449 : {
1450 0 : GDALClose(psWO->hDstDS);
1451 0 : psWO->hDstDS = nullptr;
1452 : }
1453 :
1454 199 : psWO->hDstDS = this;
1455 :
1456 : /* -------------------------------------------------------------------- */
1457 : /* Deserialize vertical shift grids. */
1458 : /* -------------------------------------------------------------------- */
1459 199 : CPLXMLNode *psIter = psTree->psChild;
1460 2406 : for (; psWO->hSrcDS != nullptr && psIter != nullptr;
1461 2207 : psIter = psIter->psNext)
1462 : {
1463 2207 : if (psIter->eType != CXT_Element ||
1464 1610 : !EQUAL(psIter->pszValue, "VerticalShiftGrids"))
1465 : {
1466 2207 : continue;
1467 : }
1468 :
1469 0 : CPLError(CE_Warning, CPLE_AppDefined,
1470 : "The VerticalShiftGrids in a warped VRT is now deprecated, "
1471 : "and will no longer be handled in GDAL 4.0");
1472 :
1473 0 : const char *pszVGrids = CPLGetXMLValue(psIter, "Grids", nullptr);
1474 0 : if (pszVGrids)
1475 : {
1476 : int bInverse =
1477 0 : CSLTestBoolean(CPLGetXMLValue(psIter, "Inverse", "FALSE"));
1478 : double dfToMeterSrc =
1479 0 : CPLAtof(CPLGetXMLValue(psIter, "ToMeterSrc", "1.0"));
1480 : double dfToMeterDest =
1481 0 : CPLAtof(CPLGetXMLValue(psIter, "ToMeterDest", "1.0"));
1482 0 : char **papszOptions = nullptr;
1483 0 : CPLXMLNode *psIter2 = psIter->psChild;
1484 0 : for (; psIter2 != nullptr; psIter2 = psIter2->psNext)
1485 : {
1486 0 : if (psIter2->eType != CXT_Element ||
1487 0 : !EQUAL(psIter2->pszValue, "Option"))
1488 : {
1489 0 : continue;
1490 : }
1491 0 : const char *pszName = CPLGetXMLValue(psIter2, "name", nullptr);
1492 : const char *pszValue =
1493 0 : CPLGetXMLValue(psIter2, nullptr, nullptr);
1494 0 : if (pszName && pszValue)
1495 : {
1496 : papszOptions =
1497 0 : CSLSetNameValue(papszOptions, pszName, pszValue);
1498 : }
1499 : }
1500 :
1501 0 : int bError = FALSE;
1502 : GDALDatasetH hGridDataset =
1503 0 : GDALOpenVerticalShiftGrid(pszVGrids, &bError);
1504 0 : if (bError && hGridDataset == nullptr)
1505 : {
1506 0 : CPLError(CE_Warning, CPLE_AppDefined,
1507 : "Cannot open %s. Source dataset will no "
1508 : "be vertically adjusted regarding "
1509 : "vertical datum",
1510 : pszVGrids);
1511 : }
1512 0 : else if (hGridDataset != nullptr)
1513 : {
1514 : // Transform from source vertical datum to WGS84
1515 0 : GDALDatasetH hTmpDS = GDALApplyVerticalShiftGrid(
1516 : psWO->hSrcDS, hGridDataset, bInverse, dfToMeterSrc,
1517 : dfToMeterDest, papszOptions);
1518 0 : GDALReleaseDataset(hGridDataset);
1519 0 : if (hTmpDS == nullptr)
1520 : {
1521 0 : CPLError(CE_Warning, CPLE_AppDefined,
1522 : "Source dataset will no "
1523 : "be vertically adjusted regarding "
1524 : "vertical datum %s",
1525 : pszVGrids);
1526 : }
1527 : else
1528 : {
1529 0 : CPLDebug("GDALWARP",
1530 : "Adjusting source dataset "
1531 : "with vertical datum using %s",
1532 : pszVGrids);
1533 0 : GDALReleaseDataset(psWO->hSrcDS);
1534 0 : psWO->hSrcDS = hTmpDS;
1535 : }
1536 : }
1537 :
1538 0 : CSLDestroy(papszOptions);
1539 : }
1540 : }
1541 :
1542 : /* -------------------------------------------------------------------- */
1543 : /* Instantiate the warp operation. */
1544 : /* -------------------------------------------------------------------- */
1545 199 : m_poWarper = new GDALWarpOperation();
1546 :
1547 199 : const CPLErr eErr = m_poWarper->Initialize(psWO);
1548 199 : if (eErr != CE_None)
1549 : {
1550 : /* --------------------------------------------------------------------
1551 : */
1552 : /* We are responsible for cleaning up the transformer ourselves. */
1553 : /* --------------------------------------------------------------------
1554 : */
1555 0 : if (psWO->pTransformerArg != nullptr)
1556 : {
1557 0 : GDALDestroyTransformer(psWO->pTransformerArg);
1558 0 : psWO->pTransformerArg = nullptr;
1559 : }
1560 :
1561 0 : if (psWO->hSrcDS != nullptr)
1562 : {
1563 0 : GDALClose(psWO->hSrcDS);
1564 0 : psWO->hSrcDS = nullptr;
1565 : }
1566 : }
1567 :
1568 199 : GDALDestroyWarpOptions(psWO);
1569 199 : if (eErr != CE_None)
1570 : {
1571 0 : delete m_poWarper;
1572 0 : m_poWarper = nullptr;
1573 : }
1574 :
1575 : /* -------------------------------------------------------------------- */
1576 : /* Deserialize SrcOvrLevel */
1577 : /* -------------------------------------------------------------------- */
1578 199 : const char *pszSrcOvrLevel = CPLGetXMLValue(psTree, "SrcOvrLevel", nullptr);
1579 199 : if (pszSrcOvrLevel != nullptr)
1580 : {
1581 0 : SetMetadataItem("SrcOvrLevel", pszSrcOvrLevel);
1582 : }
1583 :
1584 : /* -------------------------------------------------------------------- */
1585 : /* Generate overviews, if appropriate. */
1586 : /* -------------------------------------------------------------------- */
1587 :
1588 : // OverviewList is historical, and quite inefficient, since it uses
1589 : // the full resolution source dataset, so only build it afterwards.
1590 : const CPLStringList aosOverviews(
1591 199 : CSLTokenizeString(CPLGetXMLValue(psTree, "OverviewList", "")));
1592 199 : if (!aosOverviews.empty())
1593 2 : CreateImplicitOverviews();
1594 :
1595 203 : for (int iOverview = 0; iOverview < aosOverviews.size(); ++iOverview)
1596 : {
1597 4 : int nOvFactor = atoi(aosOverviews[iOverview]);
1598 :
1599 4 : if (nOvFactor > 0)
1600 4 : BuildOverviews("NEAREST", 1, &nOvFactor, 0, nullptr, nullptr,
1601 : nullptr,
1602 : /*papszOptions=*/nullptr);
1603 : else
1604 0 : CPLError(CE_Failure, CPLE_AppDefined,
1605 : "Bad value for overview factor : %s",
1606 : aosOverviews[iOverview]);
1607 : }
1608 :
1609 199 : return eErr;
1610 : }
1611 :
1612 : /************************************************************************/
1613 : /* SerializeToXML() */
1614 : /************************************************************************/
1615 :
1616 78 : CPLXMLNode *VRTWarpedDataset::SerializeToXML(const char *pszVRTPathIn)
1617 :
1618 : {
1619 78 : CPLXMLNode *psTree = VRTDataset::SerializeToXML(pszVRTPathIn);
1620 :
1621 78 : if (psTree == nullptr)
1622 0 : return psTree;
1623 :
1624 : /* -------------------------------------------------------------------- */
1625 : /* Set subclass. */
1626 : /* -------------------------------------------------------------------- */
1627 78 : CPLCreateXMLNode(CPLCreateXMLNode(psTree, CXT_Attribute, "subClass"),
1628 : CXT_Text, "VRTWarpedDataset");
1629 :
1630 : /* -------------------------------------------------------------------- */
1631 : /* Serialize the block size. */
1632 : /* -------------------------------------------------------------------- */
1633 78 : CPLCreateXMLElementAndValue(psTree, "BlockXSize",
1634 : CPLSPrintf("%d", m_nBlockXSize));
1635 78 : CPLCreateXMLElementAndValue(psTree, "BlockYSize",
1636 : CPLSPrintf("%d", m_nBlockYSize));
1637 :
1638 : /* -------------------------------------------------------------------- */
1639 : /* Serialize the overview list (only for non implicit overviews) */
1640 : /* -------------------------------------------------------------------- */
1641 78 : if (!m_apoOverviews.empty())
1642 : {
1643 3 : int nSrcDSOvrCount = 0;
1644 3 : if (m_poWarper != nullptr && m_poWarper->GetOptions() != nullptr &&
1645 9 : m_poWarper->GetOptions()->hSrcDS != nullptr &&
1646 3 : GDALGetRasterCount(m_poWarper->GetOptions()->hSrcDS) > 0)
1647 : {
1648 : nSrcDSOvrCount =
1649 3 : static_cast<GDALDataset *>(m_poWarper->GetOptions()->hSrcDS)
1650 3 : ->GetRasterBand(1)
1651 3 : ->GetOverviewCount();
1652 : }
1653 :
1654 3 : if (static_cast<int>(m_apoOverviews.size()) != nSrcDSOvrCount)
1655 : {
1656 2 : const size_t nLen = m_apoOverviews.size() * 8 + 10;
1657 2 : char *pszOverviewList = static_cast<char *>(CPLMalloc(nLen));
1658 2 : pszOverviewList[0] = '\0';
1659 6 : for (auto *poOverviewDS : m_apoOverviews)
1660 : {
1661 4 : if (poOverviewDS)
1662 : {
1663 : const int nOvFactor = static_cast<int>(
1664 4 : 0.5 +
1665 4 : GetRasterXSize() / static_cast<double>(
1666 4 : poOverviewDS->GetRasterXSize()));
1667 :
1668 4 : snprintf(pszOverviewList + strlen(pszOverviewList),
1669 4 : nLen - strlen(pszOverviewList), "%d ", nOvFactor);
1670 : }
1671 : }
1672 :
1673 2 : CPLCreateXMLElementAndValue(psTree, "OverviewList",
1674 : pszOverviewList);
1675 :
1676 2 : CPLFree(pszOverviewList);
1677 : }
1678 : }
1679 :
1680 : /* -------------------------------------------------------------------- */
1681 : /* Serialize source overview level. */
1682 : /* -------------------------------------------------------------------- */
1683 78 : if (m_nSrcOvrLevel != -2)
1684 : {
1685 0 : if (m_nSrcOvrLevel < -2)
1686 0 : CPLCreateXMLElementAndValue(
1687 : psTree, "SrcOvrLevel",
1688 0 : CPLSPrintf("AUTO%d", m_nSrcOvrLevel + 2));
1689 0 : else if (m_nSrcOvrLevel == -1)
1690 0 : CPLCreateXMLElementAndValue(psTree, "SrcOvrLevel", "NONE");
1691 : else
1692 0 : CPLCreateXMLElementAndValue(psTree, "SrcOvrLevel",
1693 : CPLSPrintf("%d", m_nSrcOvrLevel));
1694 : }
1695 :
1696 : /* ==================================================================== */
1697 : /* Serialize the warp options. */
1698 : /* ==================================================================== */
1699 78 : if (m_poWarper != nullptr)
1700 : {
1701 : /* --------------------------------------------------------------------
1702 : */
1703 : /* We reset the destination dataset name so it doesn't get */
1704 : /* written out in the serialize warp options. */
1705 : /* --------------------------------------------------------------------
1706 : */
1707 78 : char *const pszSavedName = CPLStrdup(GetDescription());
1708 78 : SetDescription("");
1709 :
1710 : CPLXMLNode *const psWOTree =
1711 78 : GDALSerializeWarpOptions(m_poWarper->GetOptions());
1712 78 : CPLAddXMLChild(psTree, psWOTree);
1713 :
1714 78 : SetDescription(pszSavedName);
1715 78 : CPLFree(pszSavedName);
1716 :
1717 : /* --------------------------------------------------------------------
1718 : */
1719 : /* We need to consider making the source dataset relative to */
1720 : /* the VRT file if possible. Adjust accordingly. */
1721 : /* --------------------------------------------------------------------
1722 : */
1723 78 : CPLXMLNode *psSDS = CPLGetXMLNode(psWOTree, "SourceDataset");
1724 78 : int bRelativeToVRT = FALSE;
1725 : VSIStatBufL sStat;
1726 :
1727 78 : if (VSIStatExL(psSDS->psChild->pszValue, &sStat,
1728 78 : VSI_STAT_EXISTS_FLAG) == 0)
1729 : {
1730 152 : std::string osVRTFilename = pszVRTPathIn;
1731 76 : std::string osSourceDataset = psSDS->psChild->pszValue;
1732 76 : char *pszCurDir = CPLGetCurrentDir();
1733 76 : if (CPLIsFilenameRelative(osSourceDataset.c_str()) &&
1734 76 : !CPLIsFilenameRelative(osVRTFilename.c_str()) &&
1735 : pszCurDir != nullptr)
1736 : {
1737 48 : osSourceDataset = CPLFormFilenameSafe(
1738 24 : pszCurDir, osSourceDataset.c_str(), nullptr);
1739 : }
1740 52 : else if (!CPLIsFilenameRelative(osSourceDataset.c_str()) &&
1741 52 : CPLIsFilenameRelative(osVRTFilename.c_str()) &&
1742 : pszCurDir != nullptr)
1743 : {
1744 8 : osVRTFilename = CPLFormFilenameSafe(
1745 4 : pszCurDir, osVRTFilename.c_str(), nullptr);
1746 : }
1747 76 : CPLFree(pszCurDir);
1748 76 : char *pszRelativePath = CPLStrdup(CPLExtractRelativePath(
1749 : osVRTFilename.c_str(), osSourceDataset.c_str(),
1750 : &bRelativeToVRT));
1751 :
1752 76 : CPLFree(psSDS->psChild->pszValue);
1753 76 : psSDS->psChild->pszValue = pszRelativePath;
1754 : }
1755 :
1756 78 : CPLCreateXMLNode(
1757 : CPLCreateXMLNode(psSDS, CXT_Attribute, "relativeToVRT"), CXT_Text,
1758 78 : bRelativeToVRT ? "1" : "0");
1759 : }
1760 :
1761 78 : return psTree;
1762 : }
1763 :
1764 : /************************************************************************/
1765 : /* GetBlockSize() */
1766 : /************************************************************************/
1767 :
1768 697 : void VRTWarpedDataset::GetBlockSize(int *pnBlockXSize, int *pnBlockYSize) const
1769 :
1770 : {
1771 697 : CPLAssert(nullptr != pnBlockXSize);
1772 697 : CPLAssert(nullptr != pnBlockYSize);
1773 :
1774 697 : *pnBlockXSize = m_nBlockXSize;
1775 697 : *pnBlockYSize = m_nBlockYSize;
1776 697 : }
1777 :
1778 : /************************************************************************/
1779 : /* ProcessBlock() */
1780 : /* */
1781 : /* Warp a single requested block, and then push each band of */
1782 : /* the result into the block cache. */
1783 : /************************************************************************/
1784 :
1785 249 : CPLErr VRTWarpedDataset::ProcessBlock(int iBlockX, int iBlockY)
1786 :
1787 : {
1788 249 : if (m_poWarper == nullptr)
1789 0 : return CE_Failure;
1790 :
1791 249 : const int nReqXOff = iBlockX * m_nBlockXSize;
1792 249 : const int nReqYOff = iBlockY * m_nBlockYSize;
1793 249 : const int nReqXSize = std::min(m_nBlockXSize, nRasterXSize - nReqXOff);
1794 249 : const int nReqYSize = std::min(m_nBlockYSize, nRasterYSize - nReqYOff);
1795 :
1796 : GByte *pabyDstBuffer = static_cast<GByte *>(
1797 249 : m_poWarper->CreateDestinationBuffer(nReqXSize, nReqYSize));
1798 :
1799 249 : if (pabyDstBuffer == nullptr)
1800 : {
1801 0 : return CE_Failure;
1802 : }
1803 :
1804 : /* -------------------------------------------------------------------- */
1805 : /* Warp into this buffer. */
1806 : /* -------------------------------------------------------------------- */
1807 :
1808 249 : const GDALWarpOptions *psWO = m_poWarper->GetOptions();
1809 : const CPLErr eErr =
1810 498 : m_poWarper->WarpRegionToBuffer(nReqXOff, nReqYOff, nReqXSize, nReqYSize,
1811 249 : pabyDstBuffer, psWO->eWorkingDataType);
1812 :
1813 249 : if (eErr != CE_None)
1814 : {
1815 0 : m_poWarper->DestroyDestinationBuffer(pabyDstBuffer);
1816 0 : return eErr;
1817 : }
1818 :
1819 : /* -------------------------------------------------------------------- */
1820 : /* Copy out into cache blocks for each band. */
1821 : /* -------------------------------------------------------------------- */
1822 249 : const int nWordSize = GDALGetDataTypeSizeBytes(psWO->eWorkingDataType);
1823 891 : for (int i = 0; i < psWO->nBandCount; i++)
1824 : {
1825 642 : int nDstBand = psWO->panDstBands[i];
1826 642 : if (GetRasterCount() < nDstBand)
1827 : {
1828 0 : continue;
1829 : }
1830 :
1831 642 : GDALRasterBand *poBand = GetRasterBand(nDstBand);
1832 : GDALRasterBlock *poBlock =
1833 642 : poBand->GetLockedBlockRef(iBlockX, iBlockY, TRUE);
1834 :
1835 642 : const GByte *pabyDstBandBuffer =
1836 : pabyDstBuffer +
1837 642 : static_cast<GPtrDiff_t>(i) * nReqXSize * nReqYSize * nWordSize;
1838 :
1839 642 : if (poBlock != nullptr)
1840 : {
1841 642 : if (poBlock->GetDataRef() != nullptr)
1842 : {
1843 642 : if (nReqXSize == m_nBlockXSize && nReqYSize == m_nBlockYSize)
1844 : {
1845 419 : GDALCopyWords64(
1846 419 : pabyDstBandBuffer, psWO->eWorkingDataType, nWordSize,
1847 : poBlock->GetDataRef(), poBlock->GetDataType(),
1848 : GDALGetDataTypeSizeBytes(poBlock->GetDataType()),
1849 419 : static_cast<GPtrDiff_t>(m_nBlockXSize) * m_nBlockYSize);
1850 : }
1851 : else
1852 : {
1853 : GByte *pabyBlock =
1854 223 : static_cast<GByte *>(poBlock->GetDataRef());
1855 : const int nDTSize =
1856 223 : GDALGetDataTypeSizeBytes(poBlock->GetDataType());
1857 18716 : for (int iY = 0; iY < nReqYSize; iY++)
1858 : {
1859 18493 : GDALCopyWords(
1860 18493 : pabyDstBandBuffer + static_cast<GPtrDiff_t>(iY) *
1861 18493 : nReqXSize * nWordSize,
1862 18493 : psWO->eWorkingDataType, nWordSize,
1863 18493 : pabyBlock + static_cast<GPtrDiff_t>(iY) *
1864 18493 : m_nBlockXSize * nDTSize,
1865 : poBlock->GetDataType(), nDTSize, nReqXSize);
1866 : }
1867 : }
1868 : }
1869 :
1870 642 : poBlock->DropLock();
1871 : }
1872 : }
1873 :
1874 249 : m_poWarper->DestroyDestinationBuffer(pabyDstBuffer);
1875 :
1876 249 : return CE_None;
1877 : }
1878 :
1879 : /************************************************************************/
1880 : /* IRasterIO() */
1881 : /************************************************************************/
1882 :
1883 : // Specialized implementation of IRasterIO() that will be faster than
1884 : // using the VRTWarpedRasterBand::IReadBlock() method in situations where
1885 : // - a large enough chunk of data is requested at once
1886 : // - and multi-threaded warping is enabled (it only kicks in if the warped
1887 : // chunk is large enough) and/or when reading the source dataset is
1888 : // multi-threaded (e.g JP2KAK or JP2OpenJPEG driver).
1889 1750 : CPLErr VRTWarpedDataset::IRasterIO(
1890 : GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize,
1891 : void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
1892 : int nBandCount, BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
1893 : GSpacing nLineSpace, GSpacing nBandSpace, GDALRasterIOExtraArg *psExtraArg)
1894 : {
1895 1726 : const bool bWholeImage = nXOff == 0 && nYOff == 0 &&
1896 3476 : nXSize == nRasterXSize && nYSize == nRasterYSize;
1897 :
1898 3500 : if (eRWFlag == GF_Write ||
1899 : // For too small request fall back to the block-based approach to
1900 : // benefit from caching
1901 1750 : (!bWholeImage &&
1902 1540 : (nBufXSize <= m_nBlockXSize || nBufYSize <= m_nBlockYSize)) ||
1903 : // Or if we don't request all bands at once
1904 3696 : nBandCount < nBands ||
1905 196 : !CPLTestBool(
1906 : CPLGetConfigOption("GDAL_VRT_WARP_USE_DATASET_RASTERIO", "YES")))
1907 : {
1908 1562 : return GDALDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
1909 : pData, nBufXSize, nBufYSize, eBufType,
1910 : nBandCount, panBandMap, nPixelSpace,
1911 1562 : nLineSpace, nBandSpace, psExtraArg);
1912 : }
1913 :
1914 : // Try overviews for sub-sampled requests
1915 188 : if (nBufXSize < nXSize || nBufYSize < nYSize)
1916 : {
1917 5 : int bTried = FALSE;
1918 5 : const CPLErr eErr = TryOverviewRasterIO(
1919 : eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize,
1920 : eBufType, nBandCount, panBandMap, nPixelSpace, nLineSpace,
1921 : nBandSpace, psExtraArg, &bTried);
1922 :
1923 5 : if (bTried)
1924 : {
1925 3 : return eErr;
1926 : }
1927 : }
1928 :
1929 185 : if (m_poWarper == nullptr)
1930 0 : return CE_Failure;
1931 :
1932 185 : const GDALWarpOptions *psWO = m_poWarper->GetOptions();
1933 :
1934 185 : if (nBufXSize != nXSize || nBufYSize != nYSize)
1935 : {
1936 2 : if (!bWholeImage || !GDALTransformHasFastClone(psWO->pTransformerArg))
1937 : {
1938 0 : return GDALDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
1939 : pData, nBufXSize, nBufYSize, eBufType,
1940 : nBandCount, panBandMap, nPixelSpace,
1941 0 : nLineSpace, nBandSpace, psExtraArg);
1942 : }
1943 :
1944 : // Build a temporary dataset taking into account the rescaling
1945 2 : void *pTransformerArg = GDALCloneTransformer(psWO->pTransformerArg);
1946 2 : if (pTransformerArg == nullptr)
1947 : {
1948 0 : return GDALDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
1949 : pData, nBufXSize, nBufYSize, eBufType,
1950 : nBandCount, panBandMap, nPixelSpace,
1951 0 : nLineSpace, nBandSpace, psExtraArg);
1952 : }
1953 :
1954 2 : GDALWarpOptions *psRescaledWO = GDALCloneWarpOptions(psWO);
1955 2 : psRescaledWO->hSrcDS = psWO->hSrcDS;
1956 2 : psRescaledWO->pfnTransformer = psWO->pfnTransformer;
1957 2 : psRescaledWO->pTransformerArg = pTransformerArg;
1958 :
1959 : // Rescale the output geotransform on the transformer.
1960 2 : double adfDstGeoTransform[6] = {0.0};
1961 2 : GDALGetTransformerDstGeoTransform(psRescaledWO->pTransformerArg,
1962 : adfDstGeoTransform);
1963 2 : RescaleDstGeoTransform(adfDstGeoTransform, nRasterXSize, nBufXSize,
1964 : nRasterYSize, nBufYSize);
1965 2 : GDALSetTransformerDstGeoTransform(psRescaledWO->pTransformerArg,
1966 : adfDstGeoTransform);
1967 :
1968 : GDALDatasetH hDstDS =
1969 2 : GDALCreateWarpedVRT(psWO->hSrcDS, nBufXSize, nBufYSize,
1970 : adfDstGeoTransform, psRescaledWO);
1971 :
1972 2 : GDALDestroyWarpOptions(psRescaledWO);
1973 :
1974 2 : if (hDstDS == nullptr)
1975 : {
1976 : // Not supposed to happen in nominal circumstances. Could perhaps
1977 : // happen if some memory allocation error occurred in code called
1978 : // by GDALCreateWarpedVRT()
1979 0 : GDALDestroyTransformer(pTransformerArg);
1980 0 : return GDALDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
1981 : pData, nBufXSize, nBufYSize, eBufType,
1982 : nBandCount, panBandMap, nPixelSpace,
1983 0 : nLineSpace, nBandSpace, psExtraArg);
1984 : }
1985 :
1986 2 : auto poOvrDS = static_cast<VRTWarpedDataset *>(hDstDS);
1987 2 : poOvrDS->m_bIsOverview = true;
1988 :
1989 : GDALRasterIOExtraArg sExtraArg;
1990 2 : INIT_RASTERIO_EXTRA_ARG(sExtraArg);
1991 2 : CPLErr eErr = poOvrDS->IRasterIO(GF_Read, 0, 0, nBufXSize, nBufYSize,
1992 : pData, nBufXSize, nBufYSize, eBufType,
1993 : nBandCount, panBandMap, nPixelSpace,
1994 : nLineSpace, nBandSpace, &sExtraArg);
1995 :
1996 2 : poOvrDS->ReleaseRef();
1997 2 : return eErr;
1998 : }
1999 :
2000 : // Build a map from warped output bands to their index
2001 366 : std::map<int, int> oMapBandToWarpingBandIndex;
2002 183 : bool bAllBandsIncreasingOrder =
2003 183 : (psWO->nBandCount == nBands && nBands == nBandCount);
2004 386 : for (int i = 0; i < psWO->nBandCount; ++i)
2005 : {
2006 203 : oMapBandToWarpingBandIndex[psWO->panDstBands[i]] = i;
2007 203 : if (psWO->panDstBands[i] != i + 1 || panBandMap[i] != i + 1)
2008 : {
2009 3 : bAllBandsIncreasingOrder = false;
2010 : }
2011 : }
2012 :
2013 : // Check that all requested bands are actually warped output bands.
2014 386 : for (int i = 0; i < nBandCount; ++i)
2015 : {
2016 206 : const int nRasterIOBand = panBandMap[i];
2017 206 : if (oMapBandToWarpingBandIndex.find(nRasterIOBand) ==
2018 412 : oMapBandToWarpingBandIndex.end())
2019 : {
2020 : // Not sure if that can happen...
2021 : // but if that does, that will likely later fail in ProcessBlock()
2022 3 : return GDALDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
2023 : pData, nBufXSize, nBufYSize, eBufType,
2024 : nBandCount, panBandMap, nPixelSpace,
2025 3 : nLineSpace, nBandSpace, psExtraArg);
2026 : }
2027 : }
2028 :
2029 180 : int nSrcXOff = 0;
2030 180 : int nSrcYOff = 0;
2031 180 : int nSrcXSize = 0;
2032 180 : int nSrcYSize = 0;
2033 180 : double dfSrcXExtraSize = 0;
2034 180 : double dfSrcYExtraSize = 0;
2035 180 : double dfSrcFillRatio = 0;
2036 : // Find the source window that corresponds to our target window
2037 180 : if (m_poWarper->ComputeSourceWindow(nXOff, nYOff, nXSize, nYSize, &nSrcXOff,
2038 : &nSrcYOff, &nSrcXSize, &nSrcYSize,
2039 : &dfSrcXExtraSize, &dfSrcYExtraSize,
2040 180 : &dfSrcFillRatio) != CE_None)
2041 : {
2042 0 : return CE_Failure;
2043 : }
2044 :
2045 180 : GByte *const pabyDst = static_cast<GByte *>(pData);
2046 180 : const int nWarpDTSize = GDALGetDataTypeSizeBytes(psWO->eWorkingDataType);
2047 :
2048 180 : const double dfMemRequired = m_poWarper->GetWorkingMemoryForWindow(
2049 : nSrcXSize, nSrcYSize, nXSize, nYSize);
2050 : // If we need more warp working memory than allowed, we have to use a
2051 : // splitting strategy until we get below the limit.
2052 180 : if (dfMemRequired > psWO->dfWarpMemoryLimit && nXSize >= 2 && nYSize >= 2)
2053 : {
2054 3 : CPLDebugOnly("VRT", "VRTWarpedDataset::IRasterIO(): exceeding warp "
2055 : "memory. Splitting region");
2056 :
2057 : GDALRasterIOExtraArg sExtraArg;
2058 3 : INIT_RASTERIO_EXTRA_ARG(sExtraArg);
2059 :
2060 : bool bOK;
2061 : // Split along the longest dimension
2062 3 : if (nXSize >= nYSize)
2063 : {
2064 2 : const int nHalfXSize = nXSize / 2;
2065 4 : bOK = IRasterIO(GF_Read, nXOff, nYOff, nHalfXSize, nYSize, pabyDst,
2066 : nHalfXSize, nYSize, eBufType, nBandCount,
2067 : panBandMap, nPixelSpace, nLineSpace, nBandSpace,
2068 4 : &sExtraArg) == CE_None &&
2069 2 : IRasterIO(GF_Read, nXOff + nHalfXSize, nYOff,
2070 : nXSize - nHalfXSize, nYSize,
2071 2 : pabyDst + nHalfXSize * nPixelSpace,
2072 : nXSize - nHalfXSize, nYSize, eBufType, nBandCount,
2073 : panBandMap, nPixelSpace, nLineSpace, nBandSpace,
2074 : &sExtraArg) == CE_None;
2075 : }
2076 : else
2077 : {
2078 1 : const int nHalfYSize = nYSize / 2;
2079 2 : bOK = IRasterIO(GF_Read, nXOff, nYOff, nXSize, nHalfYSize, pabyDst,
2080 : nXSize, nHalfYSize, eBufType, nBandCount,
2081 : panBandMap, nPixelSpace, nLineSpace, nBandSpace,
2082 2 : &sExtraArg) == CE_None &&
2083 1 : IRasterIO(GF_Read, nXOff, nYOff + nHalfYSize, nXSize,
2084 : nYSize - nHalfYSize,
2085 1 : pabyDst + nHalfYSize * nLineSpace, nXSize,
2086 : nYSize - nHalfYSize, eBufType, nBandCount,
2087 : panBandMap, nPixelSpace, nLineSpace, nBandSpace,
2088 : &sExtraArg) == CE_None;
2089 : }
2090 3 : return bOK ? CE_None : CE_Failure;
2091 : }
2092 :
2093 177 : CPLDebugOnly("VRT",
2094 : "Using optimized VRTWarpedDataset::IRasterIO() code path");
2095 :
2096 : // Allocate a warping destination buffer if needed.
2097 : // We can use directly the output buffer pData if:
2098 : // - we request exactly all warped bands, and that there are as many
2099 : // warped bands as dataset bands (no alpha)
2100 : // - the output buffer data atype is the warping working data type
2101 : // - the output buffer has a band-sequential layout.
2102 : GByte *pabyWarpBuffer;
2103 :
2104 175 : if (bAllBandsIncreasingOrder && psWO->eWorkingDataType == eBufType &&
2105 91 : nPixelSpace == GDALGetDataTypeSizeBytes(eBufType) &&
2106 431 : nLineSpace == nPixelSpace * nXSize &&
2107 79 : (nBands == 1 || nBandSpace == nLineSpace * nYSize))
2108 : {
2109 79 : pabyWarpBuffer = static_cast<GByte *>(pData);
2110 79 : m_poWarper->InitializeDestinationBuffer(pabyWarpBuffer, nXSize, nYSize);
2111 : }
2112 : else
2113 : {
2114 : pabyWarpBuffer = static_cast<GByte *>(
2115 98 : m_poWarper->CreateDestinationBuffer(nXSize, nYSize));
2116 :
2117 98 : if (pabyWarpBuffer == nullptr)
2118 : {
2119 0 : return CE_Failure;
2120 : }
2121 : }
2122 :
2123 354 : const CPLErr eErr = m_poWarper->WarpRegionToBuffer(
2124 177 : nXOff, nYOff, nXSize, nYSize, pabyWarpBuffer, psWO->eWorkingDataType,
2125 : nSrcXOff, nSrcYOff, nSrcXSize, nSrcYSize, dfSrcXExtraSize,
2126 : dfSrcYExtraSize);
2127 :
2128 177 : if (pabyWarpBuffer != pData)
2129 : {
2130 98 : if (eErr == CE_None)
2131 : {
2132 : // Copy warping buffer into user destination buffer
2133 204 : for (int i = 0; i < nBandCount; i++)
2134 : {
2135 106 : const int nRasterIOBand = panBandMap[i];
2136 : const auto oIterToWarpingBandIndex =
2137 106 : oMapBandToWarpingBandIndex.find(nRasterIOBand);
2138 : // cannot happen due to earlier check
2139 106 : CPLAssert(oIterToWarpingBandIndex !=
2140 : oMapBandToWarpingBandIndex.end());
2141 :
2142 : const GByte *const pabyWarpBandBuffer =
2143 : pabyWarpBuffer +
2144 106 : static_cast<GPtrDiff_t>(oIterToWarpingBandIndex->second) *
2145 106 : nXSize * nYSize * nWarpDTSize;
2146 106 : GByte *const pabyDstBand = pabyDst + i * nBandSpace;
2147 :
2148 17277 : for (int iY = 0; iY < nYSize; iY++)
2149 : {
2150 17171 : GDALCopyWords(pabyWarpBandBuffer +
2151 17171 : static_cast<GPtrDiff_t>(iY) * nXSize *
2152 17171 : nWarpDTSize,
2153 17171 : psWO->eWorkingDataType, nWarpDTSize,
2154 17171 : pabyDstBand + iY * nLineSpace, eBufType,
2155 : static_cast<int>(nPixelSpace), nXSize);
2156 : }
2157 : }
2158 : }
2159 :
2160 98 : m_poWarper->DestroyDestinationBuffer(pabyWarpBuffer);
2161 : }
2162 :
2163 177 : return eErr;
2164 : }
2165 :
2166 : /************************************************************************/
2167 : /* AddBand() */
2168 : /************************************************************************/
2169 :
2170 243 : CPLErr VRTWarpedDataset::AddBand(GDALDataType eType,
2171 : CSLConstList /* papszOptions */)
2172 :
2173 : {
2174 243 : if (eType == GDT_Unknown || eType == GDT_TypeCount)
2175 : {
2176 1 : ReportError(CE_Failure, CPLE_IllegalArg,
2177 : "Illegal GDT_Unknown/GDT_TypeCount argument");
2178 1 : return CE_Failure;
2179 : }
2180 :
2181 242 : SetBand(GetRasterCount() + 1,
2182 242 : new VRTWarpedRasterBand(this, GetRasterCount() + 1, eType));
2183 :
2184 242 : return CE_None;
2185 : }
2186 :
2187 : /************************************************************************/
2188 : /* ==================================================================== */
2189 : /* VRTWarpedRasterBand */
2190 : /* ==================================================================== */
2191 : /************************************************************************/
2192 :
2193 : /************************************************************************/
2194 : /* VRTWarpedRasterBand() */
2195 : /************************************************************************/
2196 :
2197 697 : VRTWarpedRasterBand::VRTWarpedRasterBand(GDALDataset *poDSIn, int nBandIn,
2198 697 : GDALDataType eType)
2199 :
2200 : {
2201 697 : Initialize(poDSIn->GetRasterXSize(), poDSIn->GetRasterYSize());
2202 :
2203 697 : poDS = poDSIn;
2204 697 : nBand = nBandIn;
2205 697 : eAccess = GA_Update;
2206 :
2207 697 : static_cast<VRTWarpedDataset *>(poDS)->GetBlockSize(&nBlockXSize,
2208 : &nBlockYSize);
2209 :
2210 697 : if (eType != GDT_Unknown)
2211 246 : eDataType = eType;
2212 697 : }
2213 :
2214 : /************************************************************************/
2215 : /* ~VRTWarpedRasterBand() */
2216 : /************************************************************************/
2217 :
2218 1394 : VRTWarpedRasterBand::~VRTWarpedRasterBand()
2219 :
2220 : {
2221 697 : FlushCache(true);
2222 1394 : }
2223 :
2224 : /************************************************************************/
2225 : /* IReadBlock() */
2226 : /************************************************************************/
2227 :
2228 249 : CPLErr VRTWarpedRasterBand::IReadBlock(int nBlockXOff, int nBlockYOff,
2229 : void *pImage)
2230 :
2231 : {
2232 249 : VRTWarpedDataset *poWDS = static_cast<VRTWarpedDataset *>(poDS);
2233 : const GPtrDiff_t nDataBytes =
2234 249 : static_cast<GPtrDiff_t>(GDALGetDataTypeSizeBytes(eDataType)) *
2235 249 : nBlockXSize * nBlockYSize;
2236 :
2237 249 : GDALRasterBlock *poBlock = GetLockedBlockRef(nBlockXOff, nBlockYOff, TRUE);
2238 249 : if (poBlock == nullptr)
2239 0 : return CE_Failure;
2240 :
2241 249 : if (poWDS->m_poWarper)
2242 : {
2243 249 : const GDALWarpOptions *psWO = poWDS->m_poWarper->GetOptions();
2244 249 : if (nBand == psWO->nDstAlphaBand)
2245 : {
2246 : // For a reader starting by asking on band 1, we should normally
2247 : // not reach here, because ProcessBlock() on band 1 will have
2248 : // populated the block cache for the regular bands and the alpha
2249 : // band.
2250 : // But if there's no source window corresponding to the block,
2251 : // the alpha band block will not be written through RasterIO(),
2252 : // so we nee to initialize it.
2253 88 : memset(poBlock->GetDataRef(), 0, nDataBytes);
2254 : }
2255 : }
2256 :
2257 249 : const CPLErr eErr = poWDS->ProcessBlock(nBlockXOff, nBlockYOff);
2258 :
2259 249 : if (eErr == CE_None && pImage != poBlock->GetDataRef())
2260 : {
2261 0 : memcpy(pImage, poBlock->GetDataRef(), nDataBytes);
2262 : }
2263 :
2264 249 : poBlock->DropLock();
2265 :
2266 249 : return eErr;
2267 : }
2268 :
2269 : /************************************************************************/
2270 : /* IWriteBlock() */
2271 : /************************************************************************/
2272 :
2273 89 : CPLErr VRTWarpedRasterBand::IWriteBlock(int nBlockXOff, int nBlockYOff,
2274 : void *pImage)
2275 :
2276 : {
2277 89 : VRTWarpedDataset *poWDS = static_cast<VRTWarpedDataset *>(poDS);
2278 :
2279 : // This is a bit tricky. In the case we are warping a VRTWarpedDataset
2280 : // with a destination alpha band, IWriteBlock can be called on that alpha
2281 : // band by GDALWarpDstAlphaMasker
2282 : // We don't need to do anything since the data will have hopefully been
2283 : // read from the block cache before if the reader processes all the bands
2284 : // of a same block.
2285 89 : if (poWDS->m_poWarper->GetOptions()->nDstAlphaBand != nBand)
2286 : {
2287 : /* Otherwise, call the superclass method, that will fail of course */
2288 0 : return VRTRasterBand::IWriteBlock(nBlockXOff, nBlockYOff, pImage);
2289 : }
2290 :
2291 89 : return CE_None;
2292 : }
2293 :
2294 : /************************************************************************/
2295 : /* EmitErrorMessageIfWriteNotSupported() */
2296 : /************************************************************************/
2297 :
2298 91 : bool VRTWarpedRasterBand::EmitErrorMessageIfWriteNotSupported(
2299 : const char *pszCaller) const
2300 : {
2301 91 : VRTWarpedDataset *poWDS = static_cast<VRTWarpedDataset *>(poDS);
2302 : // Cf comment in IWriteBlock()
2303 91 : if (poWDS->m_poWarper->GetOptions()->nDstAlphaBand != nBand)
2304 : {
2305 2 : ReportError(CE_Failure, CPLE_NoWriteAccess,
2306 : "%s: attempt to write to a VRTWarpedRasterBand.",
2307 : pszCaller);
2308 :
2309 2 : return true;
2310 : }
2311 89 : return false;
2312 : }
2313 :
2314 : /************************************************************************/
2315 : /* GetBestOverviewLevel() */
2316 : /************************************************************************/
2317 :
2318 0 : int VRTWarpedRasterBand::GetBestOverviewLevel(
2319 : int &nXOff, int &nYOff, int &nXSize, int &nYSize, int nBufXSize,
2320 : int nBufYSize, GDALRasterIOExtraArg *psExtraArg) const
2321 : {
2322 0 : VRTWarpedDataset *poWDS = static_cast<VRTWarpedDataset *>(poDS);
2323 :
2324 : /* -------------------------------------------------------------------- */
2325 : /* Compute the desired downsampling factor. It is */
2326 : /* based on the least reduced axis, and represents the number */
2327 : /* of source pixels to one destination pixel. */
2328 : /* -------------------------------------------------------------------- */
2329 0 : const double dfDesiredDownsamplingFactor =
2330 0 : ((nXSize / static_cast<double>(nBufXSize)) <
2331 0 : (nYSize / static_cast<double>(nBufYSize)) ||
2332 : nBufYSize == 1)
2333 0 : ? nXSize / static_cast<double>(nBufXSize)
2334 0 : : nYSize / static_cast<double>(nBufYSize);
2335 :
2336 : /* -------------------------------------------------------------------- */
2337 : /* Find the overview level that largest downsampling factor (most */
2338 : /* downsampled) that is still less than (or only a little more) */
2339 : /* downsampled than the request. */
2340 : /* -------------------------------------------------------------------- */
2341 0 : const GDALWarpOptions *psWO = poWDS->m_poWarper->GetOptions();
2342 0 : GDALDataset *poSrcDS = GDALDataset::FromHandle(psWO->hSrcDS);
2343 0 : const int nOverviewCount = poSrcDS->GetRasterBand(1)->GetOverviewCount();
2344 :
2345 0 : int nBestOverviewXSize = 1;
2346 0 : int nBestOverviewYSize = 1;
2347 0 : double dfBestDownsamplingFactor = 0;
2348 0 : int nBestOverviewLevel = -1;
2349 :
2350 : const char *pszOversampligThreshold =
2351 0 : CPLGetConfigOption("GDAL_OVERVIEW_OVERSAMPLING_THRESHOLD", nullptr);
2352 :
2353 : // Cf https://github.com/OSGeo/gdal/pull/9040#issuecomment-1898524693
2354 : // Do not exactly use a oversampling threshold of 1.0 because of numerical
2355 : // instability.
2356 0 : const auto AdjustThreshold = [](double x)
2357 : {
2358 0 : constexpr double EPS = 1e-2;
2359 0 : return x == 1.0 ? x + EPS : x;
2360 : };
2361 0 : const double dfOversamplingThreshold = AdjustThreshold(
2362 0 : pszOversampligThreshold ? CPLAtof(pszOversampligThreshold)
2363 0 : : psExtraArg && psExtraArg->eResampleAlg != GRIORA_NearestNeighbour
2364 0 : ? 1.0
2365 : : 1.2);
2366 0 : for (int iOverview = 0; iOverview < nOverviewCount; iOverview++)
2367 : {
2368 0 : const GDALRasterBand *poSrcOvrBand = this;
2369 0 : bool bThisLevelOnly = false;
2370 : const int iSrcOvr =
2371 0 : poWDS->GetSrcOverviewLevel(iOverview, bThisLevelOnly);
2372 0 : if (iSrcOvr >= 0)
2373 : {
2374 0 : poSrcOvrBand = poSrcDS->GetRasterBand(1)->GetOverview(iSrcOvr);
2375 : }
2376 0 : if (poSrcOvrBand == nullptr)
2377 0 : break;
2378 :
2379 0 : int nDstPixels = 0;
2380 0 : int nDstLines = 0;
2381 0 : double dfSrcRatioX = 0;
2382 0 : double dfSrcRatioY = 0;
2383 0 : if (!poWDS->GetOverviewSize(poSrcDS, iOverview, iSrcOvr, nDstPixels,
2384 : nDstLines, dfSrcRatioX, dfSrcRatioY))
2385 : {
2386 0 : break;
2387 : }
2388 :
2389 : // Compute downsampling factor of this overview
2390 : const double dfDownsamplingFactor =
2391 0 : std::min(nRasterXSize / static_cast<double>(nDstPixels),
2392 0 : nRasterYSize / static_cast<double>(nDstLines));
2393 :
2394 : // Is it nearly the requested factor and better (lower) than
2395 : // the current best factor?
2396 0 : if (dfDownsamplingFactor >=
2397 0 : dfDesiredDownsamplingFactor * dfOversamplingThreshold ||
2398 : dfDownsamplingFactor <= dfBestDownsamplingFactor)
2399 : {
2400 0 : continue;
2401 : }
2402 :
2403 : // Ignore AVERAGE_BIT2GRAYSCALE overviews for RasterIO purposes.
2404 : const char *pszResampling = const_cast<GDALRasterBand *>(poSrcOvrBand)
2405 0 : ->GetMetadataItem("RESAMPLING");
2406 :
2407 0 : if (pszResampling != nullptr &&
2408 0 : STARTS_WITH_CI(pszResampling, "AVERAGE_BIT2"))
2409 0 : continue;
2410 :
2411 : // OK, this is our new best overview.
2412 0 : nBestOverviewXSize = nDstPixels;
2413 0 : nBestOverviewYSize = nDstLines;
2414 0 : nBestOverviewLevel = iOverview;
2415 0 : dfBestDownsamplingFactor = dfDownsamplingFactor;
2416 : }
2417 :
2418 : /* -------------------------------------------------------------------- */
2419 : /* If we didn't find an overview that helps us, just return */
2420 : /* indicating failure and the full resolution image will be used. */
2421 : /* -------------------------------------------------------------------- */
2422 0 : if (nBestOverviewLevel < 0)
2423 0 : return -1;
2424 :
2425 : /* -------------------------------------------------------------------- */
2426 : /* Recompute the source window in terms of the selected */
2427 : /* overview. */
2428 : /* -------------------------------------------------------------------- */
2429 0 : const double dfXFactor =
2430 0 : nRasterXSize / static_cast<double>(nBestOverviewXSize);
2431 0 : const double dfYFactor =
2432 0 : nRasterYSize / static_cast<double>(nBestOverviewYSize);
2433 0 : CPLDebug("GDAL", "Selecting overview %d x %d", nBestOverviewXSize,
2434 : nBestOverviewYSize);
2435 :
2436 0 : const int nOXOff = std::min(nBestOverviewXSize - 1,
2437 0 : static_cast<int>(nXOff / dfXFactor + 0.5));
2438 0 : const int nOYOff = std::min(nBestOverviewYSize - 1,
2439 0 : static_cast<int>(nYOff / dfYFactor + 0.5));
2440 0 : int nOXSize = std::max(1, static_cast<int>(nXSize / dfXFactor + 0.5));
2441 0 : int nOYSize = std::max(1, static_cast<int>(nYSize / dfYFactor + 0.5));
2442 0 : if (nOXOff + nOXSize > nBestOverviewXSize)
2443 0 : nOXSize = nBestOverviewXSize - nOXOff;
2444 0 : if (nOYOff + nOYSize > nBestOverviewYSize)
2445 0 : nOYSize = nBestOverviewYSize - nOYOff;
2446 :
2447 0 : if (psExtraArg)
2448 : {
2449 0 : if (psExtraArg->bFloatingPointWindowValidity)
2450 : {
2451 0 : psExtraArg->dfXOff /= dfXFactor;
2452 0 : psExtraArg->dfXSize /= dfXFactor;
2453 0 : psExtraArg->dfYOff /= dfYFactor;
2454 0 : psExtraArg->dfYSize /= dfYFactor;
2455 : }
2456 0 : else if (psExtraArg->eResampleAlg != GRIORA_NearestNeighbour)
2457 : {
2458 0 : psExtraArg->bFloatingPointWindowValidity = true;
2459 0 : psExtraArg->dfXOff = nXOff / dfXFactor;
2460 0 : psExtraArg->dfXSize = nXSize / dfXFactor;
2461 0 : psExtraArg->dfYOff = nYOff / dfYFactor;
2462 0 : psExtraArg->dfYSize = nYSize / dfYFactor;
2463 : }
2464 : }
2465 :
2466 0 : nXOff = nOXOff;
2467 0 : nYOff = nOYOff;
2468 0 : nXSize = nOXSize;
2469 0 : nYSize = nOYSize;
2470 :
2471 0 : return nBestOverviewLevel;
2472 : }
2473 :
2474 : /************************************************************************/
2475 : /* IRasterIO() */
2476 : /************************************************************************/
2477 :
2478 5582 : CPLErr VRTWarpedRasterBand::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
2479 : int nXSize, int nYSize, void *pData,
2480 : int nBufXSize, int nBufYSize,
2481 : GDALDataType eBufType,
2482 : GSpacing nPixelSpace, GSpacing nLineSpace,
2483 : GDALRasterIOExtraArg *psExtraArg)
2484 : {
2485 5582 : VRTWarpedDataset *poWDS = static_cast<VRTWarpedDataset *>(poDS);
2486 5582 : if (m_nIRasterIOCounter == 0 && poWDS->GetRasterCount() == 1)
2487 : {
2488 903 : int anBandMap[] = {nBand};
2489 903 : ++m_nIRasterIOCounter;
2490 903 : const CPLErr eErr = poWDS->IRasterIO(
2491 : eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize,
2492 : eBufType, 1, anBandMap, nPixelSpace, nLineSpace, 0, psExtraArg);
2493 903 : --m_nIRasterIOCounter;
2494 903 : return eErr;
2495 : }
2496 :
2497 : /* ==================================================================== */
2498 : /* Do we have overviews that would be appropriate to satisfy */
2499 : /* this request? */
2500 : /* ==================================================================== */
2501 4679 : if ((nBufXSize < nXSize || nBufYSize < nYSize) && GetOverviewCount() &&
2502 : eRWFlag == GF_Read)
2503 : {
2504 : GDALRasterIOExtraArg sExtraArg;
2505 0 : GDALCopyRasterIOExtraArg(&sExtraArg, psExtraArg);
2506 :
2507 0 : const int nOverview = GetBestOverviewLevel(
2508 : nXOff, nYOff, nXSize, nYSize, nBufXSize, nBufYSize, &sExtraArg);
2509 0 : if (nOverview >= 0)
2510 : {
2511 0 : auto poOvrBand = GetOverview(nOverview);
2512 0 : if (!poOvrBand)
2513 0 : return CE_Failure;
2514 :
2515 0 : return poOvrBand->RasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
2516 : pData, nBufXSize, nBufYSize, eBufType,
2517 0 : nPixelSpace, nLineSpace, &sExtraArg);
2518 : }
2519 : }
2520 :
2521 4679 : return GDALRasterBand::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
2522 : pData, nBufXSize, nBufYSize, eBufType,
2523 4679 : nPixelSpace, nLineSpace, psExtraArg);
2524 : }
2525 :
2526 : /************************************************************************/
2527 : /* SerializeToXML() */
2528 : /************************************************************************/
2529 :
2530 187 : CPLXMLNode *VRTWarpedRasterBand::SerializeToXML(const char *pszVRTPathIn,
2531 : bool &bHasWarnedAboutRAMUsage,
2532 : size_t &nAccRAMUsage)
2533 :
2534 : {
2535 187 : CPLXMLNode *const psTree = VRTRasterBand::SerializeToXML(
2536 : pszVRTPathIn, bHasWarnedAboutRAMUsage, nAccRAMUsage);
2537 :
2538 : /* -------------------------------------------------------------------- */
2539 : /* Set subclass. */
2540 : /* -------------------------------------------------------------------- */
2541 187 : CPLCreateXMLNode(CPLCreateXMLNode(psTree, CXT_Attribute, "subClass"),
2542 : CXT_Text, "VRTWarpedRasterBand");
2543 :
2544 187 : return psTree;
2545 : }
2546 :
2547 : /************************************************************************/
2548 : /* GetOverviewCount() */
2549 : /************************************************************************/
2550 :
2551 90 : int VRTWarpedRasterBand::GetOverviewCount()
2552 :
2553 : {
2554 90 : VRTWarpedDataset *const poWDS = static_cast<VRTWarpedDataset *>(poDS);
2555 90 : if (poWDS->m_bIsOverview)
2556 1 : return 0;
2557 :
2558 89 : if (poWDS->m_apoOverviews.empty())
2559 : {
2560 61 : return poWDS->GetOverviewCount();
2561 : }
2562 :
2563 28 : return static_cast<int>(poWDS->m_apoOverviews.size());
2564 : }
2565 :
2566 : /************************************************************************/
2567 : /* GetOverview() */
2568 : /************************************************************************/
2569 :
2570 41 : GDALRasterBand *VRTWarpedRasterBand::GetOverview(int iOverview)
2571 :
2572 : {
2573 41 : VRTWarpedDataset *const poWDS = static_cast<VRTWarpedDataset *>(poDS);
2574 :
2575 41 : const int nOvrCount = GetOverviewCount();
2576 41 : if (iOverview < 0 || iOverview >= nOvrCount)
2577 6 : return nullptr;
2578 :
2579 35 : if (poWDS->m_apoOverviews.empty())
2580 11 : poWDS->m_apoOverviews.resize(nOvrCount);
2581 35 : if (!poWDS->m_apoOverviews[iOverview])
2582 18 : poWDS->m_apoOverviews[iOverview] =
2583 18 : poWDS->CreateImplicitOverview(iOverview);
2584 35 : if (!poWDS->m_apoOverviews[iOverview])
2585 0 : return nullptr;
2586 35 : return poWDS->m_apoOverviews[iOverview]->GetRasterBand(nBand);
2587 : }
2588 :
2589 : /*! @endcond */
|