/src/gdal/frmts/gtiff/cogdriver.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: COG Driver |
4 | | * Purpose: Cloud optimized GeoTIFF write support. |
5 | | * Author: Even Rouault <even dot rouault at spatialys dot com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2019, Even Rouault <even dot rouault at spatialys dot com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "cpl_port.h" |
14 | | |
15 | | #include "gdal_priv.h" |
16 | | #include "gtiff.h" |
17 | | #include "gt_overview.h" |
18 | | #include "gdal_utils.h" |
19 | | #include "gdalwarper.h" |
20 | | #include "cogdriver.h" |
21 | | #include "geotiff.h" |
22 | | |
23 | | #include "tilematrixset.hpp" |
24 | | |
25 | | #include <algorithm> |
26 | | #include <memory> |
27 | | #include <mutex> |
28 | | #include <vector> |
29 | | |
30 | | static bool gbHasLZW = false; |
31 | | |
32 | | extern "C" CPL_DLL void GDALRegister_COG(); |
33 | | |
34 | | /************************************************************************/ |
35 | | /* HasZSTDCompression() */ |
36 | | /************************************************************************/ |
37 | | |
38 | | static bool HasZSTDCompression() |
39 | 0 | { |
40 | 0 | TIFFCodec *codecs = TIFFGetConfiguredCODECs(); |
41 | 0 | bool bHasZSTD = false; |
42 | 0 | for (TIFFCodec *c = codecs; c->name; ++c) |
43 | 0 | { |
44 | 0 | if (c->scheme == COMPRESSION_ZSTD) |
45 | 0 | { |
46 | 0 | bHasZSTD = true; |
47 | 0 | break; |
48 | 0 | } |
49 | 0 | } |
50 | 0 | _TIFFfree(codecs); |
51 | 0 | return bHasZSTD; |
52 | 0 | } |
53 | | |
54 | | /************************************************************************/ |
55 | | /* GetTmpFilename() */ |
56 | | /************************************************************************/ |
57 | | |
58 | | static CPLString GetTmpFilename(const char *pszFilename, const char *pszExt) |
59 | 0 | { |
60 | 0 | const bool bSupportsRandomWrite = |
61 | 0 | VSISupportsRandomWrite(pszFilename, false); |
62 | 0 | CPLString osTmpFilename; |
63 | 0 | if (!bSupportsRandomWrite || |
64 | 0 | CPLGetConfigOption("CPL_TMPDIR", nullptr) != nullptr) |
65 | 0 | { |
66 | 0 | osTmpFilename = CPLGenerateTempFilenameSafe( |
67 | 0 | CPLGetBasenameSafe(pszFilename).c_str()); |
68 | 0 | } |
69 | 0 | else |
70 | 0 | osTmpFilename = pszFilename; |
71 | 0 | osTmpFilename += '.'; |
72 | 0 | osTmpFilename += pszExt; |
73 | 0 | VSIUnlink(osTmpFilename); |
74 | 0 | return osTmpFilename; |
75 | 0 | } |
76 | | |
77 | | /************************************************************************/ |
78 | | /* GetResampling() */ |
79 | | /************************************************************************/ |
80 | | |
81 | | static const char *GetResampling(GDALDataset *poSrcDS) |
82 | 0 | { |
83 | 0 | return poSrcDS->GetRasterBand(1)->GetColorTable() || |
84 | 0 | GDALDataTypeIsComplex( |
85 | 0 | poSrcDS->GetRasterBand(1)->GetRasterDataType()) |
86 | 0 | ? "NEAREST" |
87 | 0 | : "CUBIC"; |
88 | 0 | } |
89 | | |
90 | | /************************************************************************/ |
91 | | /* GetPredictor() */ |
92 | | /************************************************************************/ |
93 | | static const char *GetPredictor(GDALDataset *poSrcDS, const char *pszPredictor) |
94 | 0 | { |
95 | 0 | if (pszPredictor == nullptr) |
96 | 0 | return nullptr; |
97 | | |
98 | 0 | if (EQUAL(pszPredictor, "YES") || EQUAL(pszPredictor, "ON") || |
99 | 0 | EQUAL(pszPredictor, "TRUE")) |
100 | 0 | { |
101 | 0 | if (GDALDataTypeIsFloating( |
102 | 0 | poSrcDS->GetRasterBand(1)->GetRasterDataType())) |
103 | 0 | return "3"; |
104 | 0 | else |
105 | 0 | return "2"; |
106 | 0 | } |
107 | 0 | else if (EQUAL(pszPredictor, "STANDARD") || EQUAL(pszPredictor, "2")) |
108 | 0 | { |
109 | 0 | return "2"; |
110 | 0 | } |
111 | 0 | else if (EQUAL(pszPredictor, "FLOATING_POINT") || EQUAL(pszPredictor, "3")) |
112 | 0 | { |
113 | 0 | return "3"; |
114 | 0 | } |
115 | 0 | return nullptr; |
116 | 0 | } |
117 | | |
118 | | /************************************************************************/ |
119 | | /* COGGetTargetSRS() */ |
120 | | /************************************************************************/ |
121 | | |
122 | | static bool COGGetTargetSRS(const char *const *papszOptions, |
123 | | CPLString &osTargetSRS, |
124 | | std::unique_ptr<gdal::TileMatrixSet> &poTM) |
125 | 0 | { |
126 | 0 | osTargetSRS = CSLFetchNameValueDef(papszOptions, "TARGET_SRS", ""); |
127 | 0 | CPLString osTilingScheme( |
128 | 0 | CSLFetchNameValueDef(papszOptions, "TILING_SCHEME", "CUSTOM")); |
129 | 0 | if (EQUAL(osTargetSRS, "") && EQUAL(osTilingScheme, "CUSTOM")) |
130 | 0 | return false; |
131 | | |
132 | 0 | if (!EQUAL(osTilingScheme, "CUSTOM")) |
133 | 0 | { |
134 | 0 | poTM = gdal::TileMatrixSet::parse(osTilingScheme); |
135 | 0 | if (poTM == nullptr) |
136 | 0 | return false; |
137 | 0 | if (!poTM->haveAllLevelsSameTopLeft()) |
138 | 0 | { |
139 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
140 | 0 | "Unsupported tiling scheme: not all zoom levels have same " |
141 | 0 | "top left corner"); |
142 | 0 | return false; |
143 | 0 | } |
144 | 0 | if (!poTM->haveAllLevelsSameTileSize()) |
145 | 0 | { |
146 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
147 | 0 | "Unsupported tiling scheme: not all zoom levels have same " |
148 | 0 | "tile size"); |
149 | 0 | return false; |
150 | 0 | } |
151 | 0 | if (poTM->hasVariableMatrixWidth()) |
152 | 0 | { |
153 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
154 | 0 | "Unsupported tiling scheme: some levels have variable " |
155 | 0 | "matrix width"); |
156 | 0 | return false; |
157 | 0 | } |
158 | 0 | if (!osTargetSRS.empty()) |
159 | 0 | { |
160 | 0 | CPLError(CE_Warning, CPLE_AppDefined, "Ignoring TARGET_SRS option"); |
161 | 0 | } |
162 | 0 | osTargetSRS = poTM->crs(); |
163 | | |
164 | | // "Normalize" SRS as AUTH:CODE |
165 | 0 | OGRSpatialReference oTargetSRS; |
166 | 0 | oTargetSRS.SetFromUserInput( |
167 | 0 | osTargetSRS, |
168 | 0 | OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get()); |
169 | 0 | const char *pszAuthCode = oTargetSRS.GetAuthorityCode(nullptr); |
170 | 0 | const char *pszAuthName = oTargetSRS.GetAuthorityName(nullptr); |
171 | 0 | if (pszAuthName && pszAuthCode) |
172 | 0 | { |
173 | 0 | osTargetSRS = pszAuthName; |
174 | 0 | osTargetSRS += ':'; |
175 | 0 | osTargetSRS += pszAuthCode; |
176 | 0 | } |
177 | 0 | } |
178 | | |
179 | 0 | return true; |
180 | 0 | } |
181 | | |
182 | | // Used by gdalwarp |
183 | | bool COGGetTargetSRS(const char *const *papszOptions, CPLString &osTargetSRS) |
184 | 0 | { |
185 | 0 | std::unique_ptr<gdal::TileMatrixSet> poTM; |
186 | 0 | return COGGetTargetSRS(papszOptions, osTargetSRS, poTM); |
187 | 0 | } |
188 | | |
189 | | // Used by gdalwarp |
190 | | std::string COGGetResampling(GDALDataset *poSrcDS, |
191 | | const char *const *papszOptions) |
192 | 0 | { |
193 | 0 | return CSLFetchNameValueDef(papszOptions, "WARP_RESAMPLING", |
194 | 0 | CSLFetchNameValueDef(papszOptions, "RESAMPLING", |
195 | 0 | GetResampling(poSrcDS))); |
196 | 0 | } |
197 | | |
198 | | /************************************************************************/ |
199 | | /* COGGetWarpingCharacteristics() */ |
200 | | /************************************************************************/ |
201 | | |
202 | | static bool COGGetWarpingCharacteristics( |
203 | | GDALDataset *poSrcDS, const char *const *papszOptions, |
204 | | CPLString &osResampling, CPLString &osTargetSRS, int &nXSize, int &nYSize, |
205 | | double &dfMinX, double &dfMinY, double &dfMaxX, double &dfMaxY, |
206 | | double &dfRes, std::unique_ptr<gdal::TileMatrixSet> &poTM, int &nZoomLevel, |
207 | | int &nAlignedLevels) |
208 | 0 | { |
209 | 0 | if (!COGGetTargetSRS(papszOptions, osTargetSRS, poTM)) |
210 | 0 | return false; |
211 | | |
212 | 0 | CPLStringList aosTO; |
213 | 0 | aosTO.SetNameValue("DST_SRS", osTargetSRS); |
214 | 0 | void *hTransformArg = nullptr; |
215 | |
|
216 | 0 | OGRSpatialReference oTargetSRS; |
217 | 0 | oTargetSRS.SetFromUserInput( |
218 | 0 | osTargetSRS, |
219 | 0 | OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get()); |
220 | 0 | const char *pszAuthCode = oTargetSRS.GetAuthorityCode(nullptr); |
221 | 0 | const int nEPSGCode = pszAuthCode ? atoi(pszAuthCode) : 0; |
222 | | |
223 | | // Hack to compensate for GDALSuggestedWarpOutput2() failure (or not |
224 | | // ideal suggestion with PROJ 8) when reprojecting latitude = +/- 90 to |
225 | | // EPSG:3857. |
226 | 0 | GDALGeoTransform srcGT; |
227 | 0 | std::unique_ptr<GDALDataset> poTmpDS; |
228 | 0 | if (nEPSGCode == 3857 && poSrcDS->GetGeoTransform(srcGT) == CE_None && |
229 | 0 | srcGT[2] == 0 && srcGT[4] == 0 && srcGT[5] < 0) |
230 | 0 | { |
231 | 0 | const auto poSrcSRS = poSrcDS->GetSpatialRef(); |
232 | 0 | if (poSrcSRS && poSrcSRS->IsGeographic() && |
233 | 0 | !poSrcSRS->IsDerivedGeographic()) |
234 | 0 | { |
235 | 0 | double maxLat = srcGT[3]; |
236 | 0 | double minLat = srcGT[3] + poSrcDS->GetRasterYSize() * srcGT[5]; |
237 | | // Corresponds to the latitude of below MAX_GM |
238 | 0 | constexpr double MAX_LAT = 85.0511287798066; |
239 | 0 | bool bModified = false; |
240 | 0 | if (maxLat > MAX_LAT) |
241 | 0 | { |
242 | 0 | maxLat = MAX_LAT; |
243 | 0 | bModified = true; |
244 | 0 | } |
245 | 0 | if (minLat < -MAX_LAT) |
246 | 0 | { |
247 | 0 | minLat = -MAX_LAT; |
248 | 0 | bModified = true; |
249 | 0 | } |
250 | 0 | if (bModified) |
251 | 0 | { |
252 | 0 | CPLStringList aosOptions; |
253 | 0 | aosOptions.AddString("-of"); |
254 | 0 | aosOptions.AddString("VRT"); |
255 | 0 | aosOptions.AddString("-projwin"); |
256 | 0 | aosOptions.AddString(CPLSPrintf("%.17g", srcGT[0])); |
257 | 0 | aosOptions.AddString(CPLSPrintf("%.17g", maxLat)); |
258 | 0 | aosOptions.AddString(CPLSPrintf( |
259 | 0 | "%.17g", srcGT[0] + poSrcDS->GetRasterXSize() * srcGT[1])); |
260 | 0 | aosOptions.AddString(CPLSPrintf("%.17g", minLat)); |
261 | 0 | auto psOptions = |
262 | 0 | GDALTranslateOptionsNew(aosOptions.List(), nullptr); |
263 | 0 | poTmpDS.reset(GDALDataset::FromHandle(GDALTranslate( |
264 | 0 | "", GDALDataset::ToHandle(poSrcDS), psOptions, nullptr))); |
265 | 0 | GDALTranslateOptionsFree(psOptions); |
266 | 0 | if (poTmpDS) |
267 | 0 | { |
268 | 0 | hTransformArg = GDALCreateGenImgProjTransformer2( |
269 | 0 | GDALDataset::FromHandle(poTmpDS.get()), nullptr, |
270 | 0 | aosTO.List()); |
271 | 0 | if (hTransformArg == nullptr) |
272 | 0 | { |
273 | 0 | return false; |
274 | 0 | } |
275 | 0 | } |
276 | 0 | } |
277 | 0 | } |
278 | 0 | } |
279 | 0 | if (hTransformArg == nullptr) |
280 | 0 | { |
281 | 0 | hTransformArg = |
282 | 0 | GDALCreateGenImgProjTransformer2(poSrcDS, nullptr, aosTO.List()); |
283 | 0 | if (hTransformArg == nullptr) |
284 | 0 | { |
285 | 0 | return false; |
286 | 0 | } |
287 | 0 | } |
288 | | |
289 | 0 | GDALTransformerInfo *psInfo = |
290 | 0 | static_cast<GDALTransformerInfo *>(hTransformArg); |
291 | 0 | double adfGeoTransform[6]; |
292 | 0 | double adfExtent[4]; |
293 | |
|
294 | 0 | if (GDALSuggestedWarpOutput2(poTmpDS ? poTmpDS.get() : poSrcDS, |
295 | 0 | psInfo->pfnTransform, hTransformArg, |
296 | 0 | adfGeoTransform, &nXSize, &nYSize, adfExtent, |
297 | 0 | 0) != CE_None) |
298 | 0 | { |
299 | 0 | GDALDestroyGenImgProjTransformer(hTransformArg); |
300 | 0 | return false; |
301 | 0 | } |
302 | | |
303 | 0 | GDALDestroyGenImgProjTransformer(hTransformArg); |
304 | 0 | hTransformArg = nullptr; |
305 | 0 | poTmpDS.reset(); |
306 | |
|
307 | 0 | dfMinX = adfExtent[0]; |
308 | 0 | dfMinY = adfExtent[1]; |
309 | 0 | dfMaxX = adfExtent[2]; |
310 | 0 | dfMaxY = adfExtent[3]; |
311 | 0 | dfRes = adfGeoTransform[1]; |
312 | |
|
313 | 0 | const CPLString osExtent(CSLFetchNameValueDef(papszOptions, "EXTENT", "")); |
314 | 0 | const CPLString osRes(CSLFetchNameValueDef(papszOptions, "RES", "")); |
315 | 0 | if (poTM) |
316 | 0 | { |
317 | 0 | if (!osExtent.empty()) |
318 | 0 | { |
319 | 0 | CPLError(CE_Warning, CPLE_AppDefined, "Ignoring EXTENT option"); |
320 | 0 | } |
321 | 0 | if (!osRes.empty()) |
322 | 0 | { |
323 | 0 | CPLError(CE_Warning, CPLE_AppDefined, "Ignoring RES option"); |
324 | 0 | } |
325 | 0 | const bool bInvertAxis = |
326 | 0 | oTargetSRS.EPSGTreatsAsLatLong() != FALSE || |
327 | 0 | oTargetSRS.EPSGTreatsAsNorthingEasting() != FALSE; |
328 | |
|
329 | 0 | const auto &bbox = poTM->bbox(); |
330 | 0 | if (bbox.mCrs == poTM->crs()) |
331 | 0 | { |
332 | 0 | if (dfMaxX < |
333 | 0 | (bInvertAxis ? bbox.mLowerCornerY : bbox.mLowerCornerX) || |
334 | 0 | dfMinX > |
335 | 0 | (bInvertAxis ? bbox.mUpperCornerY : bbox.mUpperCornerX) || |
336 | 0 | dfMaxY < |
337 | 0 | (bInvertAxis ? bbox.mLowerCornerX : bbox.mLowerCornerY) || |
338 | 0 | dfMinY > |
339 | 0 | (bInvertAxis ? bbox.mUpperCornerX : bbox.mUpperCornerY)) |
340 | 0 | { |
341 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
342 | 0 | "Raster extent completely outside of tile matrix set " |
343 | 0 | "bounding box"); |
344 | 0 | return false; |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | 0 | const auto &tmList = poTM->tileMatrixList(); |
349 | 0 | const int nBlockSize = atoi(CSLFetchNameValueDef( |
350 | 0 | papszOptions, "BLOCKSIZE", CPLSPrintf("%d", tmList[0].mTileWidth))); |
351 | 0 | dfRes = 0.0; |
352 | |
|
353 | 0 | const char *pszZoomLevel = |
354 | 0 | CSLFetchNameValue(papszOptions, "ZOOM_LEVEL"); |
355 | 0 | if (pszZoomLevel) |
356 | 0 | { |
357 | 0 | nZoomLevel = atoi(pszZoomLevel); |
358 | 0 | if (nZoomLevel < 0 || nZoomLevel >= static_cast<int>(tmList.size())) |
359 | 0 | { |
360 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
361 | 0 | "Invalid zoom level: should be in [0,%d]", |
362 | 0 | static_cast<int>(tmList.size()) - 1); |
363 | 0 | return false; |
364 | 0 | } |
365 | 0 | } |
366 | 0 | else |
367 | 0 | { |
368 | 0 | double dfComputedRes = adfGeoTransform[1]; |
369 | 0 | double dfPrevRes = 0.0; |
370 | 0 | for (; nZoomLevel < static_cast<int>(tmList.size()); nZoomLevel++) |
371 | 0 | { |
372 | 0 | dfRes = tmList[nZoomLevel].mResX * tmList[0].mTileWidth / |
373 | 0 | nBlockSize; |
374 | 0 | if (dfComputedRes > dfRes || |
375 | 0 | fabs(dfComputedRes - dfRes) / dfRes <= 1e-8) |
376 | 0 | break; |
377 | 0 | dfPrevRes = dfRes; |
378 | 0 | } |
379 | 0 | if (nZoomLevel == static_cast<int>(tmList.size())) |
380 | 0 | { |
381 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
382 | 0 | "Could not find an appropriate zoom level"); |
383 | 0 | return false; |
384 | 0 | } |
385 | | |
386 | 0 | if (nZoomLevel > 0 && fabs(dfComputedRes - dfRes) / dfRes > 1e-8) |
387 | 0 | { |
388 | 0 | const char *pszZoomLevelStrategy = CSLFetchNameValueDef( |
389 | 0 | papszOptions, "ZOOM_LEVEL_STRATEGY", "AUTO"); |
390 | 0 | if (EQUAL(pszZoomLevelStrategy, "LOWER")) |
391 | 0 | { |
392 | 0 | nZoomLevel--; |
393 | 0 | } |
394 | 0 | else if (EQUAL(pszZoomLevelStrategy, "UPPER")) |
395 | 0 | { |
396 | | /* do nothing */ |
397 | 0 | } |
398 | 0 | else |
399 | 0 | { |
400 | 0 | if (dfPrevRes / dfComputedRes < dfComputedRes / dfRes) |
401 | 0 | nZoomLevel--; |
402 | 0 | } |
403 | 0 | } |
404 | 0 | } |
405 | 0 | CPLDebug("COG", "Using ZOOM_LEVEL %d", nZoomLevel); |
406 | 0 | dfRes = tmList[nZoomLevel].mResX * tmList[0].mTileWidth / nBlockSize; |
407 | |
|
408 | 0 | const double dfOriX = |
409 | 0 | bInvertAxis ? tmList[0].mTopLeftY : tmList[0].mTopLeftX; |
410 | 0 | const double dfOriY = |
411 | 0 | bInvertAxis ? tmList[0].mTopLeftX : tmList[0].mTopLeftY; |
412 | 0 | const double dfTileExtent = dfRes * nBlockSize; |
413 | 0 | constexpr double TOLERANCE_IN_PIXEL = 0.499; |
414 | 0 | const double dfEps = TOLERANCE_IN_PIXEL * dfRes; |
415 | 0 | int nTLTileX = static_cast<int>( |
416 | 0 | std::floor((dfMinX - dfOriX + dfEps) / dfTileExtent)); |
417 | 0 | int nTLTileY = static_cast<int>( |
418 | 0 | std::floor((dfOriY - dfMaxY + dfEps) / dfTileExtent)); |
419 | 0 | int nBRTileX = static_cast<int>( |
420 | 0 | std::ceil((dfMaxX - dfOriX - dfEps) / dfTileExtent)); |
421 | 0 | int nBRTileY = static_cast<int>( |
422 | 0 | std::ceil((dfOriY - dfMinY - dfEps) / dfTileExtent)); |
423 | |
|
424 | 0 | nAlignedLevels = |
425 | 0 | std::min(std::min(10, atoi(CSLFetchNameValueDef( |
426 | 0 | papszOptions, "ALIGNED_LEVELS", "0"))), |
427 | 0 | nZoomLevel); |
428 | 0 | int nAccDivisor = 1; |
429 | 0 | for (int i = 0; i < nAlignedLevels - 1; i++) |
430 | 0 | { |
431 | 0 | const int nCurLevel = nZoomLevel - i; |
432 | 0 | const double dfResRatio = |
433 | 0 | tmList[nCurLevel - 1].mResX / tmList[nCurLevel].mResX; |
434 | | // Magical number that has a great number of divisors |
435 | | // For example if previous scale denom was 50K and current one |
436 | | // is 20K, then dfResRatio = 2.5 and dfScaledInvResRatio = 24 |
437 | | // We must then simplify 60 / 24 as 5 / 2, and make sure to |
438 | | // align tile coordinates on multiple of the 5 numerator |
439 | 0 | constexpr int MAGICAL = 60; |
440 | 0 | const double dfScaledInvResRatio = MAGICAL / dfResRatio; |
441 | 0 | if (dfScaledInvResRatio < 1 || dfScaledInvResRatio > 60 || |
442 | 0 | std::abs(std::round(dfScaledInvResRatio) - |
443 | 0 | dfScaledInvResRatio) > 1e-10) |
444 | 0 | { |
445 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
446 | 0 | "Unsupported ratio of resolution for " |
447 | 0 | "ALIGNED_LEVELS between zoom level %d and %d = %g", |
448 | 0 | nCurLevel - 1, nCurLevel, dfResRatio); |
449 | 0 | return false; |
450 | 0 | } |
451 | 0 | const int nScaledInvResRatio = |
452 | 0 | static_cast<int>(std::round(dfScaledInvResRatio)); |
453 | 0 | int nNumerator = 0; |
454 | 0 | for (int nDivisor = nScaledInvResRatio; nDivisor >= 2; --nDivisor) |
455 | 0 | { |
456 | 0 | if ((MAGICAL % nDivisor) == 0 && |
457 | 0 | (nScaledInvResRatio % nDivisor) == 0) |
458 | 0 | { |
459 | 0 | nNumerator = MAGICAL / nDivisor; |
460 | 0 | break; |
461 | 0 | } |
462 | 0 | } |
463 | 0 | if (nNumerator == 0) |
464 | 0 | { |
465 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
466 | 0 | "Unsupported ratio of resolution for " |
467 | 0 | "ALIGNED_LEVELS between zoom level %d and %d = %g", |
468 | 0 | nCurLevel - 1, nCurLevel, dfResRatio); |
469 | 0 | return false; |
470 | 0 | } |
471 | 0 | nAccDivisor *= nNumerator; |
472 | 0 | } |
473 | 0 | if (nAccDivisor > 1) |
474 | 0 | { |
475 | 0 | nTLTileX = (nTLTileX / nAccDivisor) * nAccDivisor; |
476 | 0 | nTLTileY = (nTLTileY / nAccDivisor) * nAccDivisor; |
477 | 0 | nBRTileY = DIV_ROUND_UP(nBRTileY, nAccDivisor) * nAccDivisor; |
478 | 0 | nBRTileX = DIV_ROUND_UP(nBRTileX, nAccDivisor) * nAccDivisor; |
479 | 0 | } |
480 | |
|
481 | 0 | if (nTLTileX < 0 || nTLTileY < 0 || |
482 | 0 | nBRTileX > tmList[nZoomLevel].mMatrixWidth || |
483 | 0 | nBRTileY > tmList[nZoomLevel].mMatrixHeight) |
484 | 0 | { |
485 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
486 | 0 | "Raster extent partially outside of tile matrix " |
487 | 0 | "bounding box. Clamping it to it"); |
488 | 0 | } |
489 | 0 | nTLTileX = std::max(0, nTLTileX); |
490 | 0 | nTLTileY = std::max(0, nTLTileY); |
491 | 0 | nBRTileX = std::min(tmList[nZoomLevel].mMatrixWidth, nBRTileX); |
492 | 0 | nBRTileY = std::min(tmList[nZoomLevel].mMatrixHeight, nBRTileY); |
493 | |
|
494 | 0 | dfMinX = dfOriX + nTLTileX * dfTileExtent; |
495 | 0 | dfMinY = dfOriY - nBRTileY * dfTileExtent; |
496 | 0 | dfMaxX = dfOriX + nBRTileX * dfTileExtent; |
497 | 0 | dfMaxY = dfOriY - nTLTileY * dfTileExtent; |
498 | 0 | } |
499 | 0 | else if (!osExtent.empty() || !osRes.empty()) |
500 | 0 | { |
501 | 0 | CPLStringList aosTokens(CSLTokenizeString2(osExtent, ",", 0)); |
502 | 0 | if (aosTokens.size() != 4) |
503 | 0 | { |
504 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid value for EXTENT"); |
505 | 0 | return false; |
506 | 0 | } |
507 | 0 | dfMinX = CPLAtof(aosTokens[0]); |
508 | 0 | dfMinY = CPLAtof(aosTokens[1]); |
509 | 0 | dfMaxX = CPLAtof(aosTokens[2]); |
510 | 0 | dfMaxY = CPLAtof(aosTokens[3]); |
511 | 0 | if (!osRes.empty()) |
512 | 0 | dfRes = CPLAtof(osRes); |
513 | 0 | } |
514 | | |
515 | 0 | nXSize = static_cast<int>(std::round((dfMaxX - dfMinX) / dfRes)); |
516 | 0 | nYSize = static_cast<int>(std::round((dfMaxY - dfMinY) / dfRes)); |
517 | |
|
518 | 0 | osResampling = COGGetResampling(poSrcDS, papszOptions); |
519 | |
|
520 | 0 | return true; |
521 | 0 | } |
522 | | |
523 | | // Used by gdalwarp |
524 | | bool COGGetWarpingCharacteristics(GDALDataset *poSrcDS, |
525 | | const char *const *papszOptions, |
526 | | CPLString &osResampling, |
527 | | CPLString &osTargetSRS, int &nXSize, |
528 | | int &nYSize, double &dfMinX, double &dfMinY, |
529 | | double &dfMaxX, double &dfMaxY) |
530 | 0 | { |
531 | 0 | std::unique_ptr<gdal::TileMatrixSet> poTM; |
532 | 0 | int nZoomLevel = 0; |
533 | 0 | int nAlignedLevels = 0; |
534 | 0 | double dfRes; |
535 | 0 | return COGGetWarpingCharacteristics(poSrcDS, papszOptions, osResampling, |
536 | 0 | osTargetSRS, nXSize, nYSize, dfMinX, |
537 | 0 | dfMinY, dfMaxX, dfMaxY, dfRes, poTM, |
538 | 0 | nZoomLevel, nAlignedLevels); |
539 | 0 | } |
540 | | |
541 | | /************************************************************************/ |
542 | | /* COGHasWarpingOptions() */ |
543 | | /************************************************************************/ |
544 | | |
545 | | bool COGHasWarpingOptions(CSLConstList papszOptions) |
546 | 0 | { |
547 | 0 | return CSLFetchNameValue(papszOptions, "TARGET_SRS") != nullptr || |
548 | 0 | !EQUAL(CSLFetchNameValueDef(papszOptions, "TILING_SCHEME", "CUSTOM"), |
549 | 0 | "CUSTOM"); |
550 | 0 | } |
551 | | |
552 | | /************************************************************************/ |
553 | | /* COGRemoveWarpingOptions() */ |
554 | | /************************************************************************/ |
555 | | |
556 | | void COGRemoveWarpingOptions(CPLStringList &aosOptions) |
557 | 0 | { |
558 | 0 | aosOptions.SetNameValue("TARGET_SRS", nullptr); |
559 | 0 | aosOptions.SetNameValue("TILING_SCHEME", nullptr); |
560 | 0 | aosOptions.SetNameValue("EXTENT", nullptr); |
561 | 0 | aosOptions.SetNameValue("RES", nullptr); |
562 | 0 | aosOptions.SetNameValue("ALIGNED_LEVELS", nullptr); |
563 | 0 | aosOptions.SetNameValue("ZOOM_LEVEL_STRATEGY", nullptr); |
564 | 0 | } |
565 | | |
566 | | /************************************************************************/ |
567 | | /* CreateReprojectedDS() */ |
568 | | /************************************************************************/ |
569 | | |
570 | | static std::unique_ptr<GDALDataset> CreateReprojectedDS( |
571 | | const char *pszDstFilename, GDALDataset *poSrcDS, |
572 | | const char *const *papszOptions, const CPLString &osResampling, |
573 | | const CPLString &osTargetSRS, const int nXSize, const int nYSize, |
574 | | const double dfMinX, const double dfMinY, const double dfMaxX, |
575 | | const double dfMaxY, const double dfRes, GDALProgressFunc pfnProgress, |
576 | | void *pProgressData, double &dfCurPixels, double &dfTotalPixelsToProcess) |
577 | 0 | { |
578 | 0 | char **papszArg = nullptr; |
579 | | // We could have done a warped VRT, but overview building on it might be |
580 | | // slow, so materialize as GTiff |
581 | 0 | papszArg = CSLAddString(papszArg, "-of"); |
582 | 0 | papszArg = CSLAddString(papszArg, "GTiff"); |
583 | 0 | papszArg = CSLAddString(papszArg, "-co"); |
584 | 0 | papszArg = CSLAddString(papszArg, "TILED=YES"); |
585 | 0 | papszArg = CSLAddString(papszArg, "-co"); |
586 | 0 | papszArg = CSLAddString(papszArg, "SPARSE_OK=YES"); |
587 | 0 | const char *pszBIGTIFF = CSLFetchNameValue(papszOptions, "BIGTIFF"); |
588 | 0 | if (pszBIGTIFF) |
589 | 0 | { |
590 | 0 | papszArg = CSLAddString(papszArg, "-co"); |
591 | 0 | papszArg = CSLAddString(papszArg, |
592 | 0 | (CPLString("BIGTIFF=") + pszBIGTIFF).c_str()); |
593 | 0 | } |
594 | 0 | papszArg = CSLAddString(papszArg, "-co"); |
595 | 0 | papszArg = CSLAddString(papszArg, HasZSTDCompression() ? "COMPRESS=ZSTD" |
596 | 0 | : "COMPRESS=LZW"); |
597 | 0 | papszArg = CSLAddString(papszArg, "-t_srs"); |
598 | 0 | papszArg = CSLAddString(papszArg, osTargetSRS); |
599 | 0 | papszArg = CSLAddString(papszArg, "-te"); |
600 | 0 | papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfMinX)); |
601 | 0 | papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfMinY)); |
602 | 0 | papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfMaxX)); |
603 | 0 | papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfMaxY)); |
604 | 0 | papszArg = CSLAddString(papszArg, "-ts"); |
605 | 0 | papszArg = CSLAddString(papszArg, CPLSPrintf("%d", nXSize)); |
606 | 0 | papszArg = CSLAddString(papszArg, CPLSPrintf("%d", nYSize)); |
607 | | |
608 | | // to be kept in sync with gdalwarp_lib.cpp |
609 | 0 | constexpr double RELATIVE_ERROR_RES_SHARED_BY_COG_AND_GDALWARP = 1e-8; |
610 | 0 | if (fabs((dfMaxX - dfMinX) / dfRes - nXSize) <= |
611 | 0 | RELATIVE_ERROR_RES_SHARED_BY_COG_AND_GDALWARP && |
612 | 0 | fabs((dfMaxY - dfMinY) / dfRes - nYSize) <= |
613 | 0 | RELATIVE_ERROR_RES_SHARED_BY_COG_AND_GDALWARP) |
614 | 0 | { |
615 | | // Try to produce exactly square pixels |
616 | 0 | papszArg = CSLAddString(papszArg, "-tr"); |
617 | 0 | papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfRes)); |
618 | 0 | papszArg = CSLAddString(papszArg, CPLSPrintf("%.17g", dfRes)); |
619 | 0 | } |
620 | 0 | else |
621 | 0 | { |
622 | 0 | CPLDebug("COG", "Cannot pass -tr option to GDALWarp() due to extent, " |
623 | 0 | "size and resolution not consistent enough"); |
624 | 0 | } |
625 | |
|
626 | 0 | int bHasNoData = FALSE; |
627 | 0 | poSrcDS->GetRasterBand(1)->GetNoDataValue(&bHasNoData); |
628 | 0 | if (!bHasNoData && |
629 | 0 | CPLTestBool(CSLFetchNameValueDef(papszOptions, "ADD_ALPHA", "YES"))) |
630 | 0 | { |
631 | 0 | papszArg = CSLAddString(papszArg, "-dstalpha"); |
632 | 0 | } |
633 | 0 | papszArg = CSLAddString(papszArg, "-r"); |
634 | 0 | papszArg = CSLAddString(papszArg, osResampling); |
635 | 0 | papszArg = CSLAddString(papszArg, "-wo"); |
636 | 0 | papszArg = CSLAddString(papszArg, "SAMPLE_GRID=YES"); |
637 | 0 | const char *pszNumThreads = CSLFetchNameValue(papszOptions, "NUM_THREADS"); |
638 | 0 | if (pszNumThreads) |
639 | 0 | { |
640 | 0 | papszArg = CSLAddString(papszArg, "-wo"); |
641 | 0 | papszArg = CSLAddString( |
642 | 0 | papszArg, (CPLString("NUM_THREADS=") + pszNumThreads).c_str()); |
643 | 0 | } |
644 | |
|
645 | 0 | const auto poFirstBand = poSrcDS->GetRasterBand(1); |
646 | 0 | const bool bHasMask = poFirstBand->GetMaskFlags() == GMF_PER_DATASET; |
647 | |
|
648 | 0 | const int nBands = poSrcDS->GetRasterCount(); |
649 | 0 | const char *pszOverviews = |
650 | 0 | CSLFetchNameValueDef(papszOptions, "OVERVIEWS", "AUTO"); |
651 | 0 | const bool bUseExistingOrNone = EQUAL(pszOverviews, "FORCE_USE_EXISTING") || |
652 | 0 | EQUAL(pszOverviews, "NONE"); |
653 | 0 | dfTotalPixelsToProcess = |
654 | 0 | double(nXSize) * nYSize * (nBands + (bHasMask ? 1 : 0)) + |
655 | 0 | ((bHasMask && !bUseExistingOrNone) ? double(nXSize) * nYSize / 3 : 0) + |
656 | 0 | (!bUseExistingOrNone ? double(nXSize) * nYSize * nBands / 3 : 0) + |
657 | 0 | double(nXSize) * nYSize * (nBands + (bHasMask ? 1 : 0)) * 4. / 3; |
658 | |
|
659 | 0 | auto psOptions = GDALWarpAppOptionsNew(papszArg, nullptr); |
660 | 0 | CSLDestroy(papszArg); |
661 | 0 | if (psOptions == nullptr) |
662 | 0 | return nullptr; |
663 | | |
664 | 0 | const double dfNextPixels = |
665 | 0 | double(nXSize) * nYSize * (nBands + (bHasMask ? 1 : 0)); |
666 | 0 | void *pScaledProgress = GDALCreateScaledProgress( |
667 | 0 | dfCurPixels / dfTotalPixelsToProcess, |
668 | 0 | dfNextPixels / dfTotalPixelsToProcess, pfnProgress, pProgressData); |
669 | 0 | dfCurPixels = dfNextPixels; |
670 | |
|
671 | 0 | CPLDebug("COG", "Reprojecting source dataset: start"); |
672 | 0 | GDALWarpAppOptionsSetProgress(psOptions, GDALScaledProgress, |
673 | 0 | pScaledProgress); |
674 | 0 | CPLString osTmpFile(GetTmpFilename(pszDstFilename, "warped.tif.tmp")); |
675 | 0 | auto hSrcDS = GDALDataset::ToHandle(poSrcDS); |
676 | |
|
677 | 0 | std::unique_ptr<CPLConfigOptionSetter> poWarpThreadSetter; |
678 | 0 | if (pszNumThreads) |
679 | 0 | { |
680 | 0 | poWarpThreadSetter.reset(new CPLConfigOptionSetter( |
681 | 0 | "GDAL_NUM_THREADS", pszNumThreads, false)); |
682 | 0 | } |
683 | |
|
684 | 0 | auto hRet = GDALWarp(osTmpFile, nullptr, 1, &hSrcDS, psOptions, nullptr); |
685 | 0 | GDALWarpAppOptionsFree(psOptions); |
686 | 0 | CPLDebug("COG", "Reprojecting source dataset: end"); |
687 | |
|
688 | 0 | GDALDestroyScaledProgress(pScaledProgress); |
689 | |
|
690 | 0 | return std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(hRet)); |
691 | 0 | } |
692 | | |
693 | | /************************************************************************/ |
694 | | /* GDALCOGCreator */ |
695 | | /************************************************************************/ |
696 | | |
697 | | struct GDALCOGCreator final |
698 | | { |
699 | | std::unique_ptr<GDALDataset> m_poReprojectedDS{}; |
700 | | std::unique_ptr<GDALDataset> m_poRGBMaskDS{}; |
701 | | std::unique_ptr<GDALDataset> m_poVRTWithOrWithoutStats{}; |
702 | | CPLString m_osTmpOverviewFilename{}; |
703 | | CPLString m_osTmpMskOverviewFilename{}; |
704 | | |
705 | | ~GDALCOGCreator(); |
706 | | |
707 | | GDALDataset *Create(const char *pszFilename, GDALDataset *const poSrcDS, |
708 | | char **papszOptions, GDALProgressFunc pfnProgress, |
709 | | void *pProgressData); |
710 | | }; |
711 | | |
712 | | /************************************************************************/ |
713 | | /* GDALCOGCreator::~GDALCOGCreator() */ |
714 | | /************************************************************************/ |
715 | | |
716 | | GDALCOGCreator::~GDALCOGCreator() |
717 | 0 | { |
718 | | // Destroy m_poRGBMaskDS before m_poReprojectedDS since the former |
719 | | // may reference the later |
720 | 0 | m_poRGBMaskDS.reset(); |
721 | | |
722 | | // Config option just for testing purposes |
723 | 0 | const bool bDeleteTempFiles = |
724 | 0 | CPLTestBool(CPLGetConfigOption("COG_DELETE_TEMP_FILES", "YES")); |
725 | 0 | if (bDeleteTempFiles) |
726 | 0 | { |
727 | 0 | if (m_poReprojectedDS) |
728 | 0 | { |
729 | 0 | CPLString osProjectedDSName(m_poReprojectedDS->GetDescription()); |
730 | 0 | m_poReprojectedDS.reset(); |
731 | 0 | VSIUnlink(osProjectedDSName); |
732 | 0 | } |
733 | 0 | if (!m_osTmpOverviewFilename.empty()) |
734 | 0 | { |
735 | 0 | VSIUnlink(m_osTmpOverviewFilename); |
736 | 0 | } |
737 | 0 | if (!m_osTmpMskOverviewFilename.empty()) |
738 | 0 | { |
739 | 0 | VSIUnlink(m_osTmpMskOverviewFilename); |
740 | 0 | } |
741 | 0 | } |
742 | 0 | } |
743 | | |
744 | | /************************************************************************/ |
745 | | /* GDALCOGCreator::Create() */ |
746 | | /************************************************************************/ |
747 | | |
748 | | GDALDataset *GDALCOGCreator::Create(const char *pszFilename, |
749 | | GDALDataset *const poSrcDS, |
750 | | char **papszOptions, |
751 | | GDALProgressFunc pfnProgress, |
752 | | void *pProgressData) |
753 | 0 | { |
754 | 0 | if (pfnProgress == nullptr) |
755 | 0 | pfnProgress = GDALDummyProgress; |
756 | |
|
757 | 0 | if (poSrcDS->GetRasterCount() == 0) |
758 | 0 | { |
759 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
760 | 0 | "COG driver does not support 0-band source raster"); |
761 | 0 | return nullptr; |
762 | 0 | } |
763 | | |
764 | 0 | const CPLString osCompress = CSLFetchNameValueDef( |
765 | 0 | papszOptions, "COMPRESS", gbHasLZW ? "LZW" : "NONE"); |
766 | |
|
767 | 0 | const char *pszInterleave = |
768 | 0 | CSLFetchNameValueDef(papszOptions, "INTERLEAVE", "PIXEL"); |
769 | 0 | if (EQUAL(osCompress, "WEBP")) |
770 | 0 | { |
771 | 0 | if (!EQUAL(pszInterleave, "PIXEL")) |
772 | 0 | { |
773 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
774 | 0 | "COMPRESS=WEBP only supported for INTERLEAVE=PIXEL"); |
775 | 0 | return nullptr; |
776 | 0 | } |
777 | 0 | } |
778 | | |
779 | 0 | CPLConfigOptionSetter oSetterReportDirtyBlockFlushing( |
780 | 0 | "GDAL_REPORT_DIRTY_BLOCK_FLUSHING", "NO", true); |
781 | |
|
782 | 0 | const char *pszStatistics = |
783 | 0 | CSLFetchNameValueDef(papszOptions, "STATISTICS", "AUTO"); |
784 | 0 | auto poSrcFirstBand = poSrcDS->GetRasterBand(1); |
785 | 0 | const bool bSrcHasStatistics = |
786 | 0 | poSrcFirstBand->GetMetadataItem("STATISTICS_MINIMUM") && |
787 | 0 | poSrcFirstBand->GetMetadataItem("STATISTICS_MAXIMUM") && |
788 | 0 | poSrcFirstBand->GetMetadataItem("STATISTICS_MEAN") && |
789 | 0 | poSrcFirstBand->GetMetadataItem("STATISTICS_STDDEV"); |
790 | 0 | bool bNeedStats = false; |
791 | 0 | bool bRemoveStats = false; |
792 | 0 | bool bWrkHasStatistics = bSrcHasStatistics; |
793 | 0 | if (EQUAL(pszStatistics, "AUTO")) |
794 | 0 | { |
795 | | // nothing |
796 | 0 | } |
797 | 0 | else if (CPLTestBool(pszStatistics)) |
798 | 0 | { |
799 | 0 | bNeedStats = true; |
800 | 0 | } |
801 | 0 | else |
802 | 0 | { |
803 | 0 | bRemoveStats = true; |
804 | 0 | } |
805 | |
|
806 | 0 | double dfCurPixels = 0; |
807 | 0 | double dfTotalPixelsToProcess = 0; |
808 | 0 | GDALDataset *poCurDS = poSrcDS; |
809 | |
|
810 | 0 | std::unique_ptr<gdal::TileMatrixSet> poTM; |
811 | 0 | int nZoomLevel = 0; |
812 | 0 | int nAlignedLevels = 0; |
813 | 0 | if (COGHasWarpingOptions(papszOptions)) |
814 | 0 | { |
815 | 0 | CPLString osTargetResampling; |
816 | 0 | CPLString osTargetSRS; |
817 | 0 | int nTargetXSize = 0; |
818 | 0 | int nTargetYSize = 0; |
819 | 0 | double dfTargetMinX = 0; |
820 | 0 | double dfTargetMinY = 0; |
821 | 0 | double dfTargetMaxX = 0; |
822 | 0 | double dfTargetMaxY = 0; |
823 | 0 | double dfRes = 0; |
824 | 0 | if (!COGGetWarpingCharacteristics( |
825 | 0 | poCurDS, papszOptions, osTargetResampling, osTargetSRS, |
826 | 0 | nTargetXSize, nTargetYSize, dfTargetMinX, dfTargetMinY, |
827 | 0 | dfTargetMaxX, dfTargetMaxY, dfRes, poTM, nZoomLevel, |
828 | 0 | nAlignedLevels)) |
829 | 0 | { |
830 | 0 | return nullptr; |
831 | 0 | } |
832 | | |
833 | | // Collect information on source dataset to see if it already |
834 | | // matches the warping specifications |
835 | 0 | CPLString osSrcSRS; |
836 | 0 | const auto poSrcSRS = poCurDS->GetSpatialRef(); |
837 | 0 | if (poSrcSRS) |
838 | 0 | { |
839 | 0 | const char *pszAuthName = poSrcSRS->GetAuthorityName(nullptr); |
840 | 0 | const char *pszAuthCode = poSrcSRS->GetAuthorityCode(nullptr); |
841 | 0 | if (pszAuthName && pszAuthCode) |
842 | 0 | { |
843 | 0 | osSrcSRS = pszAuthName; |
844 | 0 | osSrcSRS += ':'; |
845 | 0 | osSrcSRS += pszAuthCode; |
846 | 0 | } |
847 | 0 | } |
848 | 0 | double dfSrcMinX = 0; |
849 | 0 | double dfSrcMinY = 0; |
850 | 0 | double dfSrcMaxX = 0; |
851 | 0 | double dfSrcMaxY = 0; |
852 | 0 | GDALGeoTransform srcGT; |
853 | 0 | const int nSrcXSize = poCurDS->GetRasterXSize(); |
854 | 0 | const int nSrcYSize = poCurDS->GetRasterYSize(); |
855 | 0 | if (poCurDS->GetGeoTransform(srcGT) == CE_None) |
856 | 0 | { |
857 | 0 | dfSrcMinX = srcGT[0]; |
858 | 0 | dfSrcMaxY = srcGT[3]; |
859 | 0 | dfSrcMaxX = srcGT[0] + nSrcXSize * srcGT[1]; |
860 | 0 | dfSrcMinY = srcGT[3] + nSrcYSize * srcGT[5]; |
861 | 0 | } |
862 | |
|
863 | 0 | if (nTargetXSize == nSrcXSize && nTargetYSize == nSrcYSize && |
864 | 0 | osTargetSRS == osSrcSRS && |
865 | 0 | fabs(dfSrcMinX - dfTargetMinX) < 1e-10 * fabs(dfSrcMinX) && |
866 | 0 | fabs(dfSrcMinY - dfTargetMinY) < 1e-10 * fabs(dfSrcMinY) && |
867 | 0 | fabs(dfSrcMaxX - dfTargetMaxX) < 1e-10 * fabs(dfSrcMaxX) && |
868 | 0 | fabs(dfSrcMaxY - dfTargetMaxY) < 1e-10 * fabs(dfSrcMaxY)) |
869 | 0 | { |
870 | 0 | CPLDebug("COG", |
871 | 0 | "Skipping reprojection step: " |
872 | 0 | "source dataset matches reprojection specifications"); |
873 | 0 | } |
874 | 0 | else |
875 | 0 | { |
876 | 0 | m_poReprojectedDS = CreateReprojectedDS( |
877 | 0 | pszFilename, poCurDS, papszOptions, osTargetResampling, |
878 | 0 | osTargetSRS, nTargetXSize, nTargetYSize, dfTargetMinX, |
879 | 0 | dfTargetMinY, dfTargetMaxX, dfTargetMaxY, dfRes, pfnProgress, |
880 | 0 | pProgressData, dfCurPixels, dfTotalPixelsToProcess); |
881 | 0 | if (!m_poReprojectedDS) |
882 | 0 | return nullptr; |
883 | 0 | poCurDS = m_poReprojectedDS.get(); |
884 | |
|
885 | 0 | if (bSrcHasStatistics && !bNeedStats && !bRemoveStats) |
886 | 0 | { |
887 | 0 | bNeedStats = true; |
888 | 0 | } |
889 | 0 | bWrkHasStatistics = false; |
890 | 0 | } |
891 | 0 | } |
892 | | |
893 | 0 | if (EQUAL(osCompress, "JPEG") && EQUAL(pszInterleave, "PIXEL") && |
894 | 0 | (poCurDS->GetRasterCount() == 2 || poCurDS->GetRasterCount() == 4) && |
895 | 0 | poCurDS->GetRasterBand(poCurDS->GetRasterCount()) |
896 | 0 | ->GetColorInterpretation() == GCI_AlphaBand) |
897 | 0 | { |
898 | 0 | char **papszArg = nullptr; |
899 | 0 | papszArg = CSLAddString(papszArg, "-of"); |
900 | 0 | papszArg = CSLAddString(papszArg, "VRT"); |
901 | 0 | papszArg = CSLAddString(papszArg, "-b"); |
902 | 0 | papszArg = CSLAddString(papszArg, "1"); |
903 | 0 | if (poCurDS->GetRasterCount() == 2) |
904 | 0 | { |
905 | 0 | papszArg = CSLAddString(papszArg, "-mask"); |
906 | 0 | papszArg = CSLAddString(papszArg, "2"); |
907 | 0 | } |
908 | 0 | else |
909 | 0 | { |
910 | 0 | CPLAssert(poCurDS->GetRasterCount() == 4); |
911 | 0 | papszArg = CSLAddString(papszArg, "-b"); |
912 | 0 | papszArg = CSLAddString(papszArg, "2"); |
913 | 0 | papszArg = CSLAddString(papszArg, "-b"); |
914 | 0 | papszArg = CSLAddString(papszArg, "3"); |
915 | 0 | papszArg = CSLAddString(papszArg, "-mask"); |
916 | 0 | papszArg = CSLAddString(papszArg, "4"); |
917 | 0 | } |
918 | 0 | GDALTranslateOptions *psOptions = |
919 | 0 | GDALTranslateOptionsNew(papszArg, nullptr); |
920 | 0 | CSLDestroy(papszArg); |
921 | 0 | GDALDatasetH hRGBMaskDS = GDALTranslate( |
922 | 0 | "", GDALDataset::ToHandle(poCurDS), psOptions, nullptr); |
923 | 0 | GDALTranslateOptionsFree(psOptions); |
924 | 0 | if (!hRGBMaskDS) |
925 | 0 | { |
926 | 0 | return nullptr; |
927 | 0 | } |
928 | 0 | m_poRGBMaskDS.reset(GDALDataset::FromHandle(hRGBMaskDS)); |
929 | 0 | poCurDS = m_poRGBMaskDS.get(); |
930 | |
|
931 | 0 | if (bSrcHasStatistics && !bNeedStats && !bRemoveStats) |
932 | 0 | { |
933 | 0 | bNeedStats = true; |
934 | 0 | } |
935 | 0 | else if (bRemoveStats && bWrkHasStatistics) |
936 | 0 | { |
937 | 0 | poCurDS->ClearStatistics(); |
938 | 0 | bRemoveStats = false; |
939 | 0 | } |
940 | 0 | } |
941 | | |
942 | 0 | const int nBands = poCurDS->GetRasterCount(); |
943 | 0 | const int nXSize = poCurDS->GetRasterXSize(); |
944 | 0 | const int nYSize = poCurDS->GetRasterYSize(); |
945 | |
|
946 | 0 | const auto CreateVRTWithOrWithoutStats = [this, &poCurDS]() |
947 | 0 | { |
948 | 0 | const char *const apszOptions[] = {"-of", "VRT", nullptr}; |
949 | 0 | GDALTranslateOptions *psOptions = |
950 | 0 | GDALTranslateOptionsNew(const_cast<char **>(apszOptions), nullptr); |
951 | 0 | GDALDatasetH hVRTDS = GDALTranslate("", GDALDataset::ToHandle(poCurDS), |
952 | 0 | psOptions, nullptr); |
953 | 0 | GDALTranslateOptionsFree(psOptions); |
954 | 0 | if (!hVRTDS) |
955 | 0 | return false; |
956 | 0 | m_poVRTWithOrWithoutStats.reset(GDALDataset::FromHandle(hVRTDS)); |
957 | 0 | poCurDS = m_poVRTWithOrWithoutStats.get(); |
958 | 0 | return true; |
959 | 0 | }; |
960 | |
|
961 | 0 | if (bNeedStats) |
962 | 0 | { |
963 | 0 | if (poSrcDS == poCurDS && !CreateVRTWithOrWithoutStats()) |
964 | 0 | { |
965 | 0 | return nullptr; |
966 | 0 | } |
967 | 0 | poCurDS->ClearStatistics(); |
968 | | |
969 | | // Avoid source files to be modified |
970 | 0 | CPLConfigOptionSetter enablePamDirtyDisabler( |
971 | 0 | "GDAL_PAM_ENABLE_MARK_DIRTY", "NO", true); |
972 | |
|
973 | 0 | for (int i = 1; i <= nBands; ++i) |
974 | 0 | { |
975 | 0 | poCurDS->GetRasterBand(i)->ComputeStatistics( |
976 | 0 | /*bApproxOK=*/FALSE, nullptr, nullptr, nullptr, nullptr, |
977 | 0 | nullptr, nullptr); |
978 | 0 | } |
979 | 0 | } |
980 | 0 | else if (bRemoveStats && bWrkHasStatistics) |
981 | 0 | { |
982 | 0 | if (!CreateVRTWithOrWithoutStats()) |
983 | 0 | return nullptr; |
984 | | |
985 | 0 | m_poVRTWithOrWithoutStats->ClearStatistics(); |
986 | 0 | } |
987 | | |
988 | 0 | CPLString osBlockSize(CSLFetchNameValueDef(papszOptions, "BLOCKSIZE", "")); |
989 | 0 | if (osBlockSize.empty()) |
990 | 0 | { |
991 | 0 | if (poTM) |
992 | 0 | { |
993 | 0 | osBlockSize.Printf("%d", poTM->tileMatrixList()[0].mTileWidth); |
994 | 0 | } |
995 | 0 | else |
996 | 0 | { |
997 | 0 | osBlockSize = "512"; |
998 | 0 | } |
999 | 0 | } |
1000 | |
|
1001 | 0 | const int nOvrThresholdSize = atoi(osBlockSize); |
1002 | |
|
1003 | 0 | const auto poFirstBand = poCurDS->GetRasterBand(1); |
1004 | 0 | const bool bHasMask = poFirstBand->GetMaskFlags() == GMF_PER_DATASET; |
1005 | |
|
1006 | 0 | CPLString osOverviews = |
1007 | 0 | CSLFetchNameValueDef(papszOptions, "OVERVIEWS", "AUTO"); |
1008 | 0 | const bool bUseExistingOrNone = |
1009 | 0 | EQUAL(osOverviews, "FORCE_USE_EXISTING") || EQUAL(osOverviews, "NONE"); |
1010 | |
|
1011 | 0 | const int nOverviewCount = |
1012 | 0 | atoi(CSLFetchNameValueDef(papszOptions, "OVERVIEW_COUNT", "-1")); |
1013 | |
|
1014 | 0 | const bool bGenerateMskOvr = |
1015 | 0 | !bUseExistingOrNone && bHasMask && |
1016 | 0 | (nXSize > nOvrThresholdSize || nYSize > nOvrThresholdSize || |
1017 | 0 | nOverviewCount > 0) && |
1018 | 0 | (EQUAL(osOverviews, "IGNORE_EXISTING") || |
1019 | 0 | poFirstBand->GetMaskBand()->GetOverviewCount() == 0); |
1020 | 0 | const bool bGenerateOvr = |
1021 | 0 | !bUseExistingOrNone && |
1022 | 0 | (nXSize > nOvrThresholdSize || nYSize > nOvrThresholdSize || |
1023 | 0 | nOverviewCount > 0) && |
1024 | 0 | (EQUAL(osOverviews, "IGNORE_EXISTING") || |
1025 | 0 | poFirstBand->GetOverviewCount() == 0); |
1026 | |
|
1027 | 0 | std::vector<std::pair<int, int>> asOverviewDims; |
1028 | 0 | int nTmpXSize = nXSize; |
1029 | 0 | int nTmpYSize = nYSize; |
1030 | 0 | if (poTM) |
1031 | 0 | { |
1032 | 0 | const auto &tmList = poTM->tileMatrixList(); |
1033 | 0 | int nCurLevel = nZoomLevel; |
1034 | 0 | while (true) |
1035 | 0 | { |
1036 | 0 | if (nOverviewCount < 0) |
1037 | 0 | { |
1038 | 0 | if (nTmpXSize <= nOvrThresholdSize && |
1039 | 0 | nTmpYSize <= nOvrThresholdSize) |
1040 | 0 | break; |
1041 | 0 | } |
1042 | 0 | else if (static_cast<int>(asOverviewDims.size()) == |
1043 | 0 | nOverviewCount || |
1044 | 0 | (nTmpXSize == 1 && nTmpYSize == 1)) |
1045 | 0 | { |
1046 | 0 | break; |
1047 | 0 | } |
1048 | 0 | const double dfResRatio = |
1049 | 0 | (nCurLevel >= 1) |
1050 | 0 | ? tmList[nCurLevel - 1].mResX / tmList[nCurLevel].mResX |
1051 | 0 | : 2; |
1052 | 0 | nTmpXSize = static_cast<int>(nTmpXSize / dfResRatio + 0.5); |
1053 | 0 | nTmpYSize = static_cast<int>(nTmpYSize / dfResRatio + 0.5); |
1054 | 0 | if (nTmpXSize == 0) |
1055 | 0 | nTmpXSize = 1; |
1056 | 0 | if (nTmpYSize == 0) |
1057 | 0 | nTmpYSize = 1; |
1058 | 0 | asOverviewDims.emplace_back(std::pair(nTmpXSize, nTmpYSize)); |
1059 | 0 | nCurLevel--; |
1060 | 0 | } |
1061 | 0 | } |
1062 | 0 | else if (bGenerateMskOvr || bGenerateOvr) |
1063 | 0 | { |
1064 | 0 | if (!bGenerateOvr) |
1065 | 0 | { |
1066 | | // If generating only .msk.ovr, use the exact overview size as |
1067 | | // the overviews of the imagery. |
1068 | 0 | int nIters = poFirstBand->GetOverviewCount(); |
1069 | 0 | if (nOverviewCount >= 0 && nOverviewCount < nIters) |
1070 | 0 | nIters = nOverviewCount; |
1071 | 0 | for (int i = 0; i < nIters; i++) |
1072 | 0 | { |
1073 | 0 | auto poOvrBand = poFirstBand->GetOverview(i); |
1074 | 0 | asOverviewDims.emplace_back( |
1075 | 0 | std::pair(poOvrBand->GetXSize(), poOvrBand->GetYSize())); |
1076 | 0 | } |
1077 | 0 | } |
1078 | 0 | else |
1079 | 0 | { |
1080 | 0 | while (true) |
1081 | 0 | { |
1082 | 0 | if (nOverviewCount < 0) |
1083 | 0 | { |
1084 | 0 | if (nTmpXSize <= nOvrThresholdSize && |
1085 | 0 | nTmpYSize <= nOvrThresholdSize) |
1086 | 0 | break; |
1087 | 0 | } |
1088 | 0 | else if (static_cast<int>(asOverviewDims.size()) == |
1089 | 0 | nOverviewCount || |
1090 | 0 | (nTmpXSize == 1 && nTmpYSize == 1)) |
1091 | 0 | { |
1092 | 0 | break; |
1093 | 0 | } |
1094 | 0 | nTmpXSize /= 2; |
1095 | 0 | nTmpYSize /= 2; |
1096 | 0 | if (nTmpXSize == 0) |
1097 | 0 | nTmpXSize = 1; |
1098 | 0 | if (nTmpYSize == 0) |
1099 | 0 | nTmpYSize = 1; |
1100 | 0 | asOverviewDims.emplace_back(std::pair(nTmpXSize, nTmpYSize)); |
1101 | 0 | } |
1102 | 0 | } |
1103 | 0 | } |
1104 | |
|
1105 | 0 | if (dfTotalPixelsToProcess == 0.0) |
1106 | 0 | { |
1107 | 0 | dfTotalPixelsToProcess = |
1108 | 0 | (bGenerateMskOvr ? double(nXSize) * nYSize / 3 : 0) + |
1109 | 0 | (bGenerateOvr ? double(nXSize) * nYSize * nBands / 3 : 0) + |
1110 | 0 | double(nXSize) * nYSize * (nBands + (bHasMask ? 1 : 0)) * 4. / 3; |
1111 | 0 | } |
1112 | |
|
1113 | 0 | CPLStringList aosOverviewOptions; |
1114 | 0 | aosOverviewOptions.SetNameValue( |
1115 | 0 | "COMPRESS", |
1116 | 0 | CPLGetConfigOption("COG_TMP_COMPRESSION", // only for debug purposes |
1117 | 0 | HasZSTDCompression() ? "ZSTD" : "LZW")); |
1118 | 0 | aosOverviewOptions.SetNameValue( |
1119 | 0 | "NUM_THREADS", CSLFetchNameValue(papszOptions, "NUM_THREADS")); |
1120 | 0 | aosOverviewOptions.SetNameValue("BIGTIFF", "YES"); |
1121 | 0 | aosOverviewOptions.SetNameValue("SPARSE_OK", "YES"); |
1122 | |
|
1123 | 0 | if (bGenerateMskOvr) |
1124 | 0 | { |
1125 | 0 | CPLDebug("COG", "Generating overviews of the mask: start"); |
1126 | 0 | m_osTmpMskOverviewFilename = GetTmpFilename(pszFilename, "msk.ovr.tmp"); |
1127 | 0 | GDALRasterBand *poSrcMask = poFirstBand->GetMaskBand(); |
1128 | 0 | const char *pszResampling = CSLFetchNameValueDef( |
1129 | 0 | papszOptions, "OVERVIEW_RESAMPLING", |
1130 | 0 | CSLFetchNameValueDef(papszOptions, "RESAMPLING", |
1131 | 0 | GetResampling(poSrcDS))); |
1132 | |
|
1133 | 0 | double dfNextPixels = dfCurPixels + double(nXSize) * nYSize / 3; |
1134 | 0 | void *pScaledProgress = GDALCreateScaledProgress( |
1135 | 0 | dfCurPixels / dfTotalPixelsToProcess, |
1136 | 0 | dfNextPixels / dfTotalPixelsToProcess, pfnProgress, pProgressData); |
1137 | 0 | dfCurPixels = dfNextPixels; |
1138 | |
|
1139 | 0 | CPLErr eErr = GTIFFBuildOverviewsEx( |
1140 | 0 | m_osTmpMskOverviewFilename, 1, &poSrcMask, |
1141 | 0 | static_cast<int>(asOverviewDims.size()), nullptr, |
1142 | 0 | asOverviewDims.data(), pszResampling, aosOverviewOptions.List(), |
1143 | 0 | GDALScaledProgress, pScaledProgress); |
1144 | 0 | CPLDebug("COG", "Generating overviews of the mask: end"); |
1145 | |
|
1146 | 0 | GDALDestroyScaledProgress(pScaledProgress); |
1147 | 0 | if (eErr != CE_None) |
1148 | 0 | { |
1149 | 0 | return nullptr; |
1150 | 0 | } |
1151 | 0 | } |
1152 | | |
1153 | 0 | if (bGenerateOvr) |
1154 | 0 | { |
1155 | 0 | CPLDebug("COG", "Generating overviews of the imagery: start"); |
1156 | 0 | m_osTmpOverviewFilename = GetTmpFilename(pszFilename, "ovr.tmp"); |
1157 | 0 | std::vector<GDALRasterBand *> apoSrcBands; |
1158 | 0 | for (int i = 0; i < nBands; i++) |
1159 | 0 | apoSrcBands.push_back(poCurDS->GetRasterBand(i + 1)); |
1160 | 0 | const char *pszResampling = CSLFetchNameValueDef( |
1161 | 0 | papszOptions, "OVERVIEW_RESAMPLING", |
1162 | 0 | CSLFetchNameValueDef(papszOptions, "RESAMPLING", |
1163 | 0 | GetResampling(poSrcDS))); |
1164 | |
|
1165 | 0 | double dfNextPixels = |
1166 | 0 | dfCurPixels + double(nXSize) * nYSize * nBands / 3; |
1167 | 0 | void *pScaledProgress = GDALCreateScaledProgress( |
1168 | 0 | dfCurPixels / dfTotalPixelsToProcess, |
1169 | 0 | dfNextPixels / dfTotalPixelsToProcess, pfnProgress, pProgressData); |
1170 | 0 | dfCurPixels = dfNextPixels; |
1171 | |
|
1172 | 0 | if (nBands > 1) |
1173 | 0 | { |
1174 | 0 | aosOverviewOptions.SetNameValue("INTERLEAVE", "PIXEL"); |
1175 | 0 | } |
1176 | 0 | if (!m_osTmpMskOverviewFilename.empty()) |
1177 | 0 | { |
1178 | 0 | aosOverviewOptions.SetNameValue("MASK_OVERVIEW_DATASET", |
1179 | 0 | m_osTmpMskOverviewFilename); |
1180 | 0 | } |
1181 | 0 | CPLErr eErr = GTIFFBuildOverviewsEx( |
1182 | 0 | m_osTmpOverviewFilename, nBands, &apoSrcBands[0], |
1183 | 0 | static_cast<int>(asOverviewDims.size()), nullptr, |
1184 | 0 | asOverviewDims.data(), pszResampling, aosOverviewOptions.List(), |
1185 | 0 | GDALScaledProgress, pScaledProgress); |
1186 | 0 | CPLDebug("COG", "Generating overviews of the imagery: end"); |
1187 | |
|
1188 | 0 | GDALDestroyScaledProgress(pScaledProgress); |
1189 | 0 | if (eErr != CE_None) |
1190 | 0 | { |
1191 | 0 | return nullptr; |
1192 | 0 | } |
1193 | 0 | } |
1194 | | |
1195 | 0 | CPLStringList aosOptions; |
1196 | 0 | aosOptions.SetNameValue("COPY_SRC_OVERVIEWS", "YES"); |
1197 | 0 | aosOptions.SetNameValue("COMPRESS", osCompress); |
1198 | 0 | aosOptions.SetNameValue("TILED", "YES"); |
1199 | 0 | aosOptions.SetNameValue("BLOCKXSIZE", osBlockSize); |
1200 | 0 | aosOptions.SetNameValue("BLOCKYSIZE", osBlockSize); |
1201 | 0 | const char *pszPredictor = |
1202 | 0 | CSLFetchNameValueDef(papszOptions, "PREDICTOR", "FALSE"); |
1203 | 0 | const char *pszPredictorValue = GetPredictor(poSrcDS, pszPredictor); |
1204 | 0 | if (pszPredictorValue != nullptr) |
1205 | 0 | { |
1206 | 0 | aosOptions.SetNameValue("PREDICTOR", pszPredictorValue); |
1207 | 0 | } |
1208 | |
|
1209 | 0 | const char *pszQuality = CSLFetchNameValue(papszOptions, "QUALITY"); |
1210 | 0 | if (EQUAL(osCompress, "JPEG")) |
1211 | 0 | { |
1212 | 0 | aosOptions.SetNameValue("JPEG_QUALITY", pszQuality); |
1213 | 0 | if (nBands == 3 && EQUAL(pszInterleave, "PIXEL")) |
1214 | 0 | aosOptions.SetNameValue("PHOTOMETRIC", "YCBCR"); |
1215 | 0 | } |
1216 | 0 | else if (EQUAL(osCompress, "WEBP")) |
1217 | 0 | { |
1218 | 0 | if (pszQuality && atoi(pszQuality) == 100) |
1219 | 0 | aosOptions.SetNameValue("WEBP_LOSSLESS", "YES"); |
1220 | 0 | aosOptions.SetNameValue("WEBP_LEVEL", pszQuality); |
1221 | 0 | } |
1222 | 0 | else if (EQUAL(osCompress, "DEFLATE") || EQUAL(osCompress, "LERC_DEFLATE")) |
1223 | 0 | { |
1224 | 0 | aosOptions.SetNameValue("ZLEVEL", |
1225 | 0 | CSLFetchNameValue(papszOptions, "LEVEL")); |
1226 | 0 | } |
1227 | 0 | else if (EQUAL(osCompress, "ZSTD") || EQUAL(osCompress, "LERC_ZSTD")) |
1228 | 0 | { |
1229 | 0 | aosOptions.SetNameValue("ZSTD_LEVEL", |
1230 | 0 | CSLFetchNameValue(papszOptions, "LEVEL")); |
1231 | 0 | } |
1232 | 0 | else if (EQUAL(osCompress, "LZMA")) |
1233 | 0 | { |
1234 | 0 | aosOptions.SetNameValue("LZMA_PRESET", |
1235 | 0 | CSLFetchNameValue(papszOptions, "LEVEL")); |
1236 | 0 | } |
1237 | |
|
1238 | 0 | if (STARTS_WITH_CI(osCompress, "LERC")) |
1239 | 0 | { |
1240 | 0 | aosOptions.SetNameValue("MAX_Z_ERROR", |
1241 | 0 | CSLFetchNameValue(papszOptions, "MAX_Z_ERROR")); |
1242 | 0 | aosOptions.SetNameValue( |
1243 | 0 | "MAX_Z_ERROR_OVERVIEW", |
1244 | 0 | CSLFetchNameValue(papszOptions, "MAX_Z_ERROR_OVERVIEW")); |
1245 | 0 | } |
1246 | |
|
1247 | 0 | if (STARTS_WITH_CI(osCompress, "JXL")) |
1248 | 0 | { |
1249 | 0 | for (const char *pszKey : {"JXL_LOSSLESS", "JXL_EFFORT", "JXL_DISTANCE", |
1250 | 0 | "JXL_ALPHA_DISTANCE"}) |
1251 | 0 | { |
1252 | 0 | const char *pszValue = CSLFetchNameValue(papszOptions, pszKey); |
1253 | 0 | if (pszValue) |
1254 | 0 | aosOptions.SetNameValue(pszKey, pszValue); |
1255 | 0 | } |
1256 | 0 | } |
1257 | |
|
1258 | 0 | aosOptions.SetNameValue("BIGTIFF", |
1259 | 0 | CSLFetchNameValue(papszOptions, "BIGTIFF")); |
1260 | 0 | aosOptions.SetNameValue("NUM_THREADS", |
1261 | 0 | CSLFetchNameValue(papszOptions, "NUM_THREADS")); |
1262 | 0 | aosOptions.SetNameValue("GEOTIFF_VERSION", |
1263 | 0 | CSLFetchNameValue(papszOptions, "GEOTIFF_VERSION")); |
1264 | 0 | aosOptions.SetNameValue("SPARSE_OK", |
1265 | 0 | CSLFetchNameValue(papszOptions, "SPARSE_OK")); |
1266 | 0 | aosOptions.SetNameValue("NBITS", CSLFetchNameValue(papszOptions, "NBITS")); |
1267 | |
|
1268 | 0 | if (EQUAL(osOverviews, "NONE")) |
1269 | 0 | { |
1270 | 0 | aosOptions.SetNameValue("@OVERVIEW_DATASET", ""); |
1271 | 0 | } |
1272 | 0 | else |
1273 | 0 | { |
1274 | 0 | if (!m_osTmpOverviewFilename.empty()) |
1275 | 0 | { |
1276 | 0 | aosOptions.SetNameValue("@OVERVIEW_DATASET", |
1277 | 0 | m_osTmpOverviewFilename); |
1278 | 0 | } |
1279 | 0 | if (!m_osTmpMskOverviewFilename.empty()) |
1280 | 0 | { |
1281 | 0 | aosOptions.SetNameValue("@MASK_OVERVIEW_DATASET", |
1282 | 0 | m_osTmpMskOverviewFilename); |
1283 | 0 | } |
1284 | 0 | aosOptions.SetNameValue( |
1285 | 0 | "@OVERVIEW_COUNT", |
1286 | 0 | CSLFetchNameValue(papszOptions, "OVERVIEW_COUNT")); |
1287 | 0 | } |
1288 | |
|
1289 | 0 | const CPLString osTilingScheme( |
1290 | 0 | CSLFetchNameValueDef(papszOptions, "TILING_SCHEME", "CUSTOM")); |
1291 | 0 | if (osTilingScheme != "CUSTOM") |
1292 | 0 | { |
1293 | 0 | aosOptions.SetNameValue("@TILING_SCHEME_NAME", osTilingScheme); |
1294 | 0 | aosOptions.SetNameValue("@TILING_SCHEME_ZOOM_LEVEL", |
1295 | 0 | CPLSPrintf("%d", nZoomLevel)); |
1296 | 0 | if (nAlignedLevels > 0) |
1297 | 0 | { |
1298 | 0 | aosOptions.SetNameValue("@TILING_SCHEME_ALIGNED_LEVELS", |
1299 | 0 | CPLSPrintf("%d", nAlignedLevels)); |
1300 | 0 | } |
1301 | 0 | } |
1302 | 0 | const char *pszOverviewCompress = CSLFetchNameValueDef( |
1303 | 0 | papszOptions, "OVERVIEW_COMPRESS", osCompress.c_str()); |
1304 | |
|
1305 | 0 | CPLConfigOptionSetter ovrCompressSetter("COMPRESS_OVERVIEW", |
1306 | 0 | pszOverviewCompress, true); |
1307 | 0 | const char *pszOverviewQuality = |
1308 | 0 | CSLFetchNameValue(papszOptions, "OVERVIEW_QUALITY"); |
1309 | 0 | CPLConfigOptionSetter ovrQualityJpegSetter("JPEG_QUALITY_OVERVIEW", |
1310 | 0 | pszOverviewQuality, true); |
1311 | |
|
1312 | 0 | std::unique_ptr<CPLConfigOptionSetter> poWebpLosslessSetter; |
1313 | 0 | std::unique_ptr<CPLConfigOptionSetter> poWebpLevelSetter; |
1314 | 0 | if (EQUAL(pszOverviewCompress, "WEBP")) |
1315 | 0 | { |
1316 | 0 | if (pszOverviewQuality && CPLAtof(pszOverviewQuality) == 100.0) |
1317 | 0 | { |
1318 | 0 | poWebpLosslessSetter.reset(new CPLConfigOptionSetter( |
1319 | 0 | "WEBP_LOSSLESS_OVERVIEW", "TRUE", true)); |
1320 | 0 | } |
1321 | 0 | else |
1322 | 0 | { |
1323 | 0 | poWebpLosslessSetter.reset(new CPLConfigOptionSetter( |
1324 | 0 | "WEBP_LOSSLESS_OVERVIEW", "FALSE", true)); |
1325 | 0 | poWebpLevelSetter.reset(new CPLConfigOptionSetter( |
1326 | 0 | "WEBP_LEVEL_OVERVIEW", pszOverviewQuality, true)); |
1327 | 0 | } |
1328 | 0 | } |
1329 | |
|
1330 | 0 | std::unique_ptr<CPLConfigOptionSetter> poPhotometricSetter; |
1331 | 0 | if (nBands == 3 && EQUAL(pszOverviewCompress, "JPEG") && |
1332 | 0 | EQUAL(pszInterleave, "PIXEL")) |
1333 | 0 | { |
1334 | 0 | poPhotometricSetter.reset( |
1335 | 0 | new CPLConfigOptionSetter("PHOTOMETRIC_OVERVIEW", "YCBCR", true)); |
1336 | 0 | } |
1337 | |
|
1338 | 0 | const char *osOvrPredictor = |
1339 | 0 | CSLFetchNameValueDef(papszOptions, "OVERVIEW_PREDICTOR", "FALSE"); |
1340 | 0 | const char *pszOvrPredictorValue = GetPredictor(poSrcDS, osOvrPredictor); |
1341 | 0 | CPLConfigOptionSetter ovrPredictorSetter("PREDICTOR_OVERVIEW", |
1342 | 0 | pszOvrPredictorValue, true); |
1343 | |
|
1344 | 0 | GDALDriver *poGTiffDrv = |
1345 | 0 | GDALDriver::FromHandle(GDALGetDriverByName("GTiff")); |
1346 | 0 | if (!poGTiffDrv) |
1347 | 0 | return nullptr; |
1348 | 0 | void *pScaledProgress = GDALCreateScaledProgress( |
1349 | 0 | dfCurPixels / dfTotalPixelsToProcess, 1.0, pfnProgress, pProgressData); |
1350 | |
|
1351 | 0 | CPLConfigOptionSetter oSetterInternalMask("GDAL_TIFF_INTERNAL_MASK", "YES", |
1352 | 0 | false); |
1353 | |
|
1354 | 0 | const char *pszCopySrcMDD = CSLFetchNameValue(papszOptions, "COPY_SRC_MDD"); |
1355 | 0 | if (pszCopySrcMDD) |
1356 | 0 | aosOptions.SetNameValue("COPY_SRC_MDD", pszCopySrcMDD); |
1357 | 0 | char **papszSrcMDD = CSLFetchNameValueMultiple(papszOptions, "SRC_MDD"); |
1358 | 0 | for (CSLConstList papszSrcMDDIter = papszSrcMDD; |
1359 | 0 | papszSrcMDDIter && *papszSrcMDDIter; ++papszSrcMDDIter) |
1360 | 0 | aosOptions.AddNameValue("SRC_MDD", *papszSrcMDDIter); |
1361 | 0 | CSLDestroy(papszSrcMDD); |
1362 | |
|
1363 | 0 | if (EQUAL(pszInterleave, "TILE")) |
1364 | 0 | { |
1365 | 0 | aosOptions.SetNameValue("INTERLEAVE", "BAND"); |
1366 | 0 | aosOptions.SetNameValue("@TILE_INTERLEAVE", "YES"); |
1367 | 0 | } |
1368 | 0 | else |
1369 | 0 | { |
1370 | 0 | aosOptions.SetNameValue("INTERLEAVE", pszInterleave); |
1371 | 0 | } |
1372 | |
|
1373 | 0 | aosOptions.SetNameValue("@FLUSHCACHE", "YES"); |
1374 | |
|
1375 | 0 | CPLDebug("COG", "Generating final product: start"); |
1376 | 0 | auto poRet = |
1377 | 0 | poGTiffDrv->CreateCopy(pszFilename, poCurDS, false, aosOptions.List(), |
1378 | 0 | GDALScaledProgress, pScaledProgress); |
1379 | |
|
1380 | 0 | GDALDestroyScaledProgress(pScaledProgress); |
1381 | |
|
1382 | 0 | CPLDebug("COG", "Generating final product: end"); |
1383 | 0 | return poRet; |
1384 | 0 | } |
1385 | | |
1386 | | /************************************************************************/ |
1387 | | /* COGCreateCopy() */ |
1388 | | /************************************************************************/ |
1389 | | |
1390 | | static GDALDataset *COGCreateCopy(const char *pszFilename, GDALDataset *poSrcDS, |
1391 | | int /*bStrict*/, char **papszOptions, |
1392 | | GDALProgressFunc pfnProgress, |
1393 | | void *pProgressData) |
1394 | 0 | { |
1395 | 0 | return GDALCOGCreator().Create(pszFilename, poSrcDS, papszOptions, |
1396 | 0 | pfnProgress, pProgressData); |
1397 | 0 | } |
1398 | | |
1399 | | /************************************************************************/ |
1400 | | /* GDALRegister_COG() */ |
1401 | | /************************************************************************/ |
1402 | | |
1403 | | class GDALCOGDriver final : public GDALDriver |
1404 | | { |
1405 | | std::mutex m_oMutex{}; |
1406 | | bool m_bInitialized = false; |
1407 | | |
1408 | | bool bHasLZW = false; |
1409 | | bool bHasDEFLATE = false; |
1410 | | bool bHasLZMA = false; |
1411 | | bool bHasZSTD = false; |
1412 | | bool bHasJPEG = false; |
1413 | | bool bHasWebP = false; |
1414 | | bool bHasLERC = false; |
1415 | | std::string osCompressValues{}; |
1416 | | |
1417 | | void InitializeCreationOptionList(); |
1418 | | |
1419 | | public: |
1420 | | GDALCOGDriver(); |
1421 | | |
1422 | | const char *GetMetadataItem(const char *pszName, |
1423 | | const char *pszDomain) override; |
1424 | | |
1425 | | char **GetMetadata(const char *pszDomain) override |
1426 | 0 | { |
1427 | 0 | std::lock_guard oLock(m_oMutex); |
1428 | 0 | InitializeCreationOptionList(); |
1429 | 0 | return GDALDriver::GetMetadata(pszDomain); |
1430 | 0 | } |
1431 | | }; |
1432 | | |
1433 | | GDALCOGDriver::GDALCOGDriver() |
1434 | 0 | { |
1435 | | // We could defer this in InitializeCreationOptionList() but with currently |
1436 | | // released libtiff versions where there was a bug (now fixed) in |
1437 | | // TIFFGetConfiguredCODECs(), this wouldn't work properly if the LERC codec |
1438 | | // had been registered in between |
1439 | 0 | osCompressValues = GTiffGetCompressValues(bHasLZW, bHasDEFLATE, bHasLZMA, |
1440 | 0 | bHasZSTD, bHasJPEG, bHasWebP, |
1441 | 0 | bHasLERC, true /* bForCOG */); |
1442 | 0 | gbHasLZW = bHasLZW; |
1443 | 0 | } |
1444 | | |
1445 | | const char *GDALCOGDriver::GetMetadataItem(const char *pszName, |
1446 | | const char *pszDomain) |
1447 | 0 | { |
1448 | 0 | std::lock_guard oLock(m_oMutex); |
1449 | 0 | if (EQUAL(pszName, GDAL_DMD_CREATIONOPTIONLIST)) |
1450 | 0 | { |
1451 | 0 | InitializeCreationOptionList(); |
1452 | 0 | } |
1453 | 0 | return GDALDriver::GetMetadataItem(pszName, pszDomain); |
1454 | 0 | } |
1455 | | |
1456 | | void GDALCOGDriver::InitializeCreationOptionList() |
1457 | 0 | { |
1458 | 0 | if (m_bInitialized) |
1459 | 0 | return; |
1460 | 0 | m_bInitialized = true; |
1461 | |
|
1462 | 0 | CPLString osOptions; |
1463 | 0 | osOptions = "<CreationOptionList>" |
1464 | 0 | " <Option name='COMPRESS' type='string-select' default='"; |
1465 | 0 | osOptions += bHasLZW ? "LZW" : "NONE"; |
1466 | 0 | osOptions += "'>"; |
1467 | 0 | osOptions += osCompressValues; |
1468 | 0 | osOptions += " </Option>"; |
1469 | |
|
1470 | 0 | osOptions += |
1471 | 0 | " <Option name='OVERVIEW_COMPRESS' type='string-select' default='"; |
1472 | 0 | osOptions += bHasLZW ? "LZW" : "NONE"; |
1473 | 0 | osOptions += "'>"; |
1474 | 0 | osOptions += osCompressValues; |
1475 | 0 | osOptions += " </Option>"; |
1476 | |
|
1477 | 0 | if (bHasLZW || bHasDEFLATE || bHasZSTD || bHasLZMA) |
1478 | 0 | { |
1479 | 0 | const char *osPredictorOptions = |
1480 | 0 | " <Value>YES</Value>" |
1481 | 0 | " <Value>NO</Value>" |
1482 | 0 | " <Value alias='2'>STANDARD</Value>" |
1483 | 0 | " <Value alias='3'>FLOATING_POINT</Value>"; |
1484 | |
|
1485 | 0 | osOptions += |
1486 | 0 | " <Option name='LEVEL' type='int' " |
1487 | 0 | "description='DEFLATE/ZSTD/LZMA compression level: 1 (fastest)'/>"; |
1488 | |
|
1489 | 0 | osOptions += |
1490 | 0 | " <Option name='PREDICTOR' type='string-select' default='FALSE'>"; |
1491 | 0 | osOptions += osPredictorOptions; |
1492 | 0 | osOptions += " </Option>" |
1493 | 0 | " <Option name='OVERVIEW_PREDICTOR' " |
1494 | 0 | "type='string-select' default='FALSE'>"; |
1495 | 0 | osOptions += osPredictorOptions; |
1496 | 0 | osOptions += " </Option>"; |
1497 | 0 | } |
1498 | 0 | if (bHasJPEG || bHasWebP) |
1499 | 0 | { |
1500 | 0 | std::string osJPEG_WEBP; |
1501 | 0 | if (bHasJPEG) |
1502 | 0 | osJPEG_WEBP = "JPEG"; |
1503 | 0 | if (bHasWebP) |
1504 | 0 | { |
1505 | 0 | if (!osJPEG_WEBP.empty()) |
1506 | 0 | osJPEG_WEBP += '/'; |
1507 | 0 | osJPEG_WEBP += "WEBP"; |
1508 | 0 | } |
1509 | 0 | osOptions += " <Option name='QUALITY' type='int' " |
1510 | 0 | "description='" + |
1511 | 0 | osJPEG_WEBP + |
1512 | 0 | " quality 1-100' min='1' max='100' default='75'/>" |
1513 | 0 | " <Option name='OVERVIEW_QUALITY' type='int' " |
1514 | 0 | "description='Overview " + |
1515 | 0 | osJPEG_WEBP + |
1516 | 0 | " quality 1-100' min='1' max='100' " |
1517 | 0 | "default='75'/>"; |
1518 | 0 | } |
1519 | 0 | if (bHasLERC) |
1520 | 0 | { |
1521 | 0 | osOptions += |
1522 | 0 | "" |
1523 | 0 | " <Option name='MAX_Z_ERROR' type='float' description='Maximum " |
1524 | 0 | "error for LERC compression' default='0'/>" |
1525 | 0 | " <Option name='MAX_Z_ERROR_OVERVIEW' type='float' " |
1526 | 0 | "description='Maximum error for LERC compression in overviews' " |
1527 | 0 | "default='0'/>"; |
1528 | 0 | } |
1529 | | #ifdef HAVE_JXL |
1530 | | osOptions += |
1531 | | "" |
1532 | | " <Option name='JXL_LOSSLESS' type='boolean' description='Whether " |
1533 | | "JPEGXL compression should be lossless' default='YES'/>" |
1534 | | " <Option name='JXL_EFFORT' type='int' description='Level of effort " |
1535 | | "1(fast)-9(slow)' min='1' max='9' default='5'/>" |
1536 | | " <Option name='JXL_DISTANCE' type='float' description='Distance " |
1537 | | "level for lossy compression (0=mathematically lossless, 1.0=visually " |
1538 | | "lossless, usual range [0.5,3])' default='1.0' min='0.01' max='25.0'/>"; |
1539 | | #ifdef HAVE_JxlEncoderSetExtraChannelDistance |
1540 | | osOptions += " <Option name='JXL_ALPHA_DISTANCE' type='float' " |
1541 | | "description='Distance level for alpha channel " |
1542 | | "(-1=same as non-alpha channels, " |
1543 | | "0=mathematically lossless, 1.0=visually lossless, " |
1544 | | "usual range [0.5,3])' default='-1' min='-1' max='25.0'/>"; |
1545 | | #endif |
1546 | | #endif |
1547 | 0 | osOptions += |
1548 | 0 | " <Option name='NUM_THREADS' type='string' " |
1549 | 0 | "description='Number of worker threads for compression. " |
1550 | 0 | "Can be set to ALL_CPUS' default='1'/>" |
1551 | 0 | " <Option name='NBITS' type='int' description='BITS for sub-byte " |
1552 | 0 | "files (1-7), sub-uint16_t (9-15), sub-uint32_t (17-31), or float32 " |
1553 | 0 | "(16)'/>" |
1554 | 0 | " <Option name='BLOCKSIZE' type='int' " |
1555 | 0 | "description='Tile size in pixels' min='128' default='512'/>" |
1556 | 0 | " <Option name='INTERLEAVE' type='string-select' default='PIXEL'>" |
1557 | 0 | " <Value>BAND</Value>" |
1558 | 0 | " <Value>PIXEL</Value>" |
1559 | 0 | " <Value>TILE</Value>" |
1560 | 0 | " </Option>" |
1561 | 0 | " <Option name='BIGTIFF' type='string-select' description='" |
1562 | 0 | "Force creation of BigTIFF file'>" |
1563 | 0 | " <Value>YES</Value>" |
1564 | 0 | " <Value>NO</Value>" |
1565 | 0 | " <Value>IF_NEEDED</Value>" |
1566 | 0 | " <Value>IF_SAFER</Value>" |
1567 | 0 | " </Option>" |
1568 | 0 | " <Option name='RESAMPLING' type='string' " |
1569 | 0 | "description='Resampling method for overviews or warping'/>" |
1570 | 0 | " <Option name='OVERVIEW_RESAMPLING' type='string' " |
1571 | 0 | "description='Resampling method for overviews'/>" |
1572 | 0 | " <Option name='WARP_RESAMPLING' type='string' " |
1573 | 0 | "description='Resampling method for warping'/>" |
1574 | 0 | " <Option name='OVERVIEWS' type='string-select' description='" |
1575 | 0 | "Behavior regarding overviews'>" |
1576 | 0 | " <Value>AUTO</Value>" |
1577 | 0 | " <Value>IGNORE_EXISTING</Value>" |
1578 | 0 | " <Value>FORCE_USE_EXISTING</Value>" |
1579 | 0 | " <Value>NONE</Value>" |
1580 | 0 | " </Option>" |
1581 | 0 | " <Option name='OVERVIEW_COUNT' type='int' min='0' " |
1582 | 0 | "description='Number of overviews'/>" |
1583 | 0 | " <Option name='TILING_SCHEME' type='string-select' description='" |
1584 | 0 | "Which tiling scheme to use pre-defined value or custom inline/outline " |
1585 | 0 | "JSON definition' default='CUSTOM'>" |
1586 | 0 | " <Value>CUSTOM</Value>"; |
1587 | |
|
1588 | 0 | const auto tmsList = gdal::TileMatrixSet::listPredefinedTileMatrixSets(); |
1589 | 0 | for (const auto &tmsName : tmsList) |
1590 | 0 | { |
1591 | 0 | const auto poTM = gdal::TileMatrixSet::parse(tmsName.c_str()); |
1592 | 0 | if (poTM && poTM->haveAllLevelsSameTopLeft() && |
1593 | 0 | poTM->haveAllLevelsSameTileSize() && |
1594 | 0 | !poTM->hasVariableMatrixWidth()) |
1595 | 0 | { |
1596 | 0 | osOptions += " <Value>"; |
1597 | 0 | osOptions += tmsName; |
1598 | 0 | osOptions += "</Value>"; |
1599 | 0 | } |
1600 | 0 | } |
1601 | |
|
1602 | 0 | osOptions += |
1603 | 0 | " </Option>" |
1604 | 0 | " <Option name='ZOOM_LEVEL' type='int' description='Target zoom " |
1605 | 0 | "level. " |
1606 | 0 | "Only used for TILING_SCHEME != CUSTOM'/>" |
1607 | 0 | " <Option name='ZOOM_LEVEL_STRATEGY' type='string-select' " |
1608 | 0 | "description='Strategy to determine zoom level. " |
1609 | 0 | "Only used for TILING_SCHEME != CUSTOM' default='AUTO'>" |
1610 | 0 | " <Value>AUTO</Value>" |
1611 | 0 | " <Value>LOWER</Value>" |
1612 | 0 | " <Value>UPPER</Value>" |
1613 | 0 | " </Option>" |
1614 | 0 | " <Option name='TARGET_SRS' type='string' " |
1615 | 0 | "description='Target SRS as EPSG:XXXX, WKT or PROJ string for " |
1616 | 0 | "reprojection'/>" |
1617 | 0 | " <Option name='RES' type='float' description='" |
1618 | 0 | "Target resolution for reprojection'/>" |
1619 | 0 | " <Option name='EXTENT' type='string' description='" |
1620 | 0 | "Target extent as minx,miny,maxx,maxy for reprojection'/>" |
1621 | 0 | " <Option name='ALIGNED_LEVELS' type='int' description='" |
1622 | 0 | "Number of resolution levels for which the tiles from GeoTIFF and the " |
1623 | 0 | "specified tiling scheme match'/>" |
1624 | 0 | " <Option name='ADD_ALPHA' type='boolean' description='Can be set to " |
1625 | 0 | "NO to " |
1626 | 0 | "disable the addition of an alpha band in case of reprojection' " |
1627 | 0 | "default='YES'/>" |
1628 | 0 | #if LIBGEOTIFF_VERSION >= 1600 |
1629 | 0 | " <Option name='GEOTIFF_VERSION' type='string-select' default='AUTO' " |
1630 | 0 | "description='Which version of GeoTIFF must be used'>" |
1631 | 0 | " <Value>AUTO</Value>" |
1632 | 0 | " <Value>1.0</Value>" |
1633 | 0 | " <Value>1.1</Value>" |
1634 | 0 | " </Option>" |
1635 | 0 | #endif |
1636 | 0 | " <Option name='SPARSE_OK' type='boolean' description='Should empty " |
1637 | 0 | "blocks be omitted on disk?' default='FALSE'/>" |
1638 | 0 | " <Option name='STATISTICS' type='string-select' default='AUTO' " |
1639 | 0 | "description='Which to add statistics to the output file'>" |
1640 | 0 | " <Value>AUTO</Value>" |
1641 | 0 | " <Value>YES</Value>" |
1642 | 0 | " <Value>NO</Value>" |
1643 | 0 | " </Option>" |
1644 | 0 | "</CreationOptionList>"; |
1645 | |
|
1646 | 0 | SetMetadataItem(GDAL_DMD_CREATIONOPTIONLIST, osOptions.c_str()); |
1647 | 0 | } |
1648 | | |
1649 | | void GDALRegister_COG() |
1650 | | |
1651 | 0 | { |
1652 | 0 | if (GDALGetDriverByName("COG") != nullptr) |
1653 | 0 | return; |
1654 | | |
1655 | 0 | auto poDriver = new GDALCOGDriver(); |
1656 | 0 | poDriver->SetDescription("COG"); |
1657 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES"); |
1658 | 0 | poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, |
1659 | 0 | "Cloud optimized GeoTIFF generator"); |
1660 | 0 | poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/cog.html"); |
1661 | 0 | poDriver->SetMetadataItem(GDAL_DMD_EXTENSIONS, "tif tiff"); |
1662 | |
|
1663 | 0 | poDriver->SetMetadataItem( |
1664 | 0 | GDAL_DMD_CREATIONDATATYPES, |
1665 | 0 | "Byte Int8 UInt16 Int16 UInt32 Int32 UInt64 Int64 Float32 " |
1666 | 0 | "Float64 CInt16 CInt32 CFloat32 CFloat64"); |
1667 | |
|
1668 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES"); |
1669 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_CREATE_ONLY_VISIBLE_AT_CLOSE_TIME, |
1670 | 0 | "YES"); |
1671 | |
|
1672 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_COORDINATE_EPOCH, "YES"); |
1673 | |
|
1674 | 0 | poDriver->pfnCreateCopy = COGCreateCopy; |
1675 | |
|
1676 | 0 | GetGDALDriverManager()->RegisterDriver(poDriver); |
1677 | 0 | } |