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