Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL
4 : * Purpose: gdal "raster tile" subcommand
5 : * Author: Even Rouault <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "gdalalg_raster_tile.h"
14 :
15 : #include "cpl_conv.h"
16 : #include "cpl_json.h"
17 : #include "cpl_mem_cache.h"
18 : #include "cpl_spawn.h"
19 : #include "cpl_time.h"
20 : #include "cpl_vsi_virtual.h"
21 : #include "cpl_worker_thread_pool.h"
22 : #include "gdal_alg_priv.h"
23 : #include "gdal_priv.h"
24 : #include "gdalgetgdalpath.h"
25 : #include "gdalwarper.h"
26 : #include "gdal_utils.h"
27 : #include "ogr_spatialref.h"
28 : #include "memdataset.h"
29 : #include "tilematrixset.hpp"
30 : #include "ogr_p.h"
31 :
32 : #include <algorithm>
33 : #include <array>
34 : #include <atomic>
35 : #include <cinttypes>
36 : #include <cmath>
37 : #include <mutex>
38 : #include <utility>
39 : #include <thread>
40 :
41 : #ifdef USE_NEON_OPTIMIZATIONS
42 : #include "include_sse2neon.h"
43 : #elif defined(__x86_64) || defined(_M_X64)
44 : #include <emmintrin.h>
45 : #if defined(__SSSE3__) || defined(__AVX__)
46 : #include <tmmintrin.h>
47 : #endif
48 : #if defined(__SSE4_1__) || defined(__AVX__)
49 : #include <smmintrin.h>
50 : #endif
51 : #endif
52 :
53 : #if defined(__x86_64) || defined(_M_X64) || defined(USE_NEON_OPTIMIZATIONS)
54 : #define USE_PAETH_SSE2
55 : #endif
56 :
57 : #ifndef _WIN32
58 : #define FORK_ALLOWED
59 : #endif
60 :
61 : #include "cpl_zlib_header.h" // for crc32()
62 :
63 : //! @cond Doxygen_Suppress
64 :
65 : #ifndef _
66 : #define _(x) (x)
67 : #endif
68 :
69 : // Unlikely substring to appear in stdout. We do that in case some GDAL
70 : // driver would output on stdout.
71 : constexpr const char PROGRESS_MARKER[] = {'!', '.', 'x'};
72 : constexpr const char END_MARKER[] = {'?', 'E', '?', 'N', '?', 'D', '?'};
73 :
74 : constexpr const char ERROR_START_MARKER[] = {'%', 'E', '%', 'R', '%', 'R',
75 : '%', '_', '%', 'S', '%', 'T',
76 : '%', 'A', '%', 'R', '%', 'T'};
77 :
78 : constexpr const char *STOP_MARKER = "STOP\n";
79 :
80 : namespace
81 : {
82 : struct BandMetadata
83 : {
84 : std::string osDescription{};
85 : GDALDataType eDT{};
86 : GDALColorInterp eColorInterp{};
87 : std::string osCenterWaveLength{};
88 : std::string osFWHM{};
89 : };
90 : } // namespace
91 :
92 : /************************************************************************/
93 : /* GetThresholdMinTilesPerJob() */
94 : /************************************************************************/
95 :
96 15 : static int GetThresholdMinThreadsForSpawn()
97 : {
98 : // Minimum number of threads for automatic switch to spawning
99 15 : constexpr int THRESHOLD_MIN_THREADS_FOR_SPAWN = 8;
100 :
101 : // Config option for test only
102 15 : return std::max(1, atoi(CPLGetConfigOption(
103 : "GDAL_THRESHOLD_MIN_THREADS_FOR_SPAWN",
104 15 : CPLSPrintf("%d", THRESHOLD_MIN_THREADS_FOR_SPAWN))));
105 : }
106 :
107 : /************************************************************************/
108 : /* GetThresholdMinTilesPerJob() */
109 : /************************************************************************/
110 :
111 300 : static int GetThresholdMinTilesPerJob()
112 : {
113 : // Minimum number of tiles per job to decide for automatic switch to spawning
114 300 : constexpr int THRESHOLD_TILES_PER_JOB = 100;
115 :
116 : // Config option for test only
117 : return std::max(
118 300 : 1, atoi(CPLGetConfigOption("GDAL_THRESHOLD_MIN_TILES_PER_JOB",
119 300 : CPLSPrintf("%d", THRESHOLD_TILES_PER_JOB))));
120 : }
121 :
122 : /************************************************************************/
123 : /* GDALRasterTileAlgorithm::GDALRasterTileAlgorithm() */
124 : /************************************************************************/
125 :
126 292 : GDALRasterTileAlgorithm::GDALRasterTileAlgorithm(bool standaloneStep)
127 : : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
128 0 : ConstructorOptions()
129 292 : .SetStandaloneStep(standaloneStep)
130 292 : .SetInputDatasetMaxCount(1)
131 292 : .SetAddDefaultArguments(false)
132 584 : .SetInputDatasetAlias("dataset"))
133 : {
134 292 : AddProgressArg();
135 : AddArg("spawned", 0, _("Whether this is a spawned worker"),
136 584 : &m_spawned)
137 292 : .SetHidden(); // Used in spawn mode
138 : #ifdef FORK_ALLOWED
139 : AddArg("forked", 0, _("Whether this is a forked worker"),
140 584 : &m_forked)
141 292 : .SetHidden(); // Used in forked mode
142 : #else
143 : CPL_IGNORE_RET_VAL(m_forked);
144 : #endif
145 584 : AddArg("config-options-in-stdin", 0, _(""), &m_dummy)
146 292 : .SetHidden(); // Used in spawn mode
147 : AddArg("ovr-zoom-level", 0, _("Overview zoom level to compute"),
148 584 : &m_ovrZoomLevel)
149 292 : .SetMinValueIncluded(0)
150 292 : .SetHidden(); // Used in spawn mode
151 584 : AddArg("ovr-min-x", 0, _("Minimum tile X coordinate"), &m_minOvrTileX)
152 292 : .SetMinValueIncluded(0)
153 292 : .SetHidden(); // Used in spawn mode
154 584 : AddArg("ovr-max-x", 0, _("Maximum tile X coordinate"), &m_maxOvrTileX)
155 292 : .SetMinValueIncluded(0)
156 292 : .SetHidden(); // Used in spawn mode
157 584 : AddArg("ovr-min-y", 0, _("Minimum tile Y coordinate"), &m_minOvrTileY)
158 292 : .SetMinValueIncluded(0)
159 292 : .SetHidden(); // Used in spawn mode
160 584 : AddArg("ovr-max-y", 0, _("Maximum tile Y coordinate"), &m_maxOvrTileY)
161 292 : .SetMinValueIncluded(0)
162 292 : .SetHidden(); // Used in spawn mode
163 :
164 292 : if (standaloneStep)
165 : {
166 255 : AddRasterInputArgs(/* openForMixedRasterVector = */ false,
167 : /* hiddenForCLI = */ false);
168 : }
169 : else
170 : {
171 37 : AddRasterHiddenInputDatasetArg();
172 : }
173 :
174 292 : m_format = "PNG";
175 292 : AddOutputFormatArg(&m_format)
176 292 : .SetDefault(m_format)
177 : .AddMetadataItem(
178 : GAAMDI_REQUIRED_CAPABILITIES,
179 1460 : {GDAL_DCAP_RASTER, GDAL_DCAP_CREATECOPY, GDAL_DMD_EXTENSIONS})
180 584 : .AddMetadataItem(GAAMDI_VRT_COMPATIBLE, {"false"});
181 292 : AddCreationOptionsArg(&m_creationOptions);
182 :
183 584 : AddArg(GDAL_ARG_NAME_OUTPUT, 'o', _("Output directory"), &m_output)
184 292 : .SetRequired()
185 292 : .SetIsInput()
186 292 : .SetMinCharCount(1)
187 292 : .SetPositional();
188 :
189 1168 : std::vector<std::string> tilingSchemes{"raster"};
190 2920 : for (const std::string &scheme :
191 6132 : gdal::TileMatrixSet::listPredefinedTileMatrixSets(/* hidden = */ true))
192 : {
193 5840 : auto poTMS = gdal::TileMatrixSet::parse(scheme.c_str());
194 5840 : OGRSpatialReference oSRS_TMS;
195 5840 : if (poTMS && !poTMS->hasVariableMatrixWidth() &&
196 2920 : oSRS_TMS.SetFromUserInput(poTMS->crs().c_str()) == OGRERR_NONE)
197 : {
198 2920 : std::string identifier = scheme == "GoogleMapsCompatible"
199 : ? "WebMercatorQuad"
200 5840 : : poTMS->identifier();
201 2920 : m_mapTileMatrixIdentifierToScheme[identifier] = scheme;
202 2920 : tilingSchemes.push_back(std::move(identifier));
203 : }
204 : }
205 584 : AddArg("tiling-scheme", 0, _("Tiling scheme"), &m_tilingScheme)
206 292 : .SetDefault("WebMercatorQuad")
207 292 : .SetChoices(tilingSchemes)
208 : .SetHiddenChoices(
209 : "GoogleMapsCompatible", // equivalent of WebMercatorQuad
210 : "mercator", // gdal2tiles equivalent of WebMercatorQuad
211 : "GlobalGeodeticOriginLat270" // gdal2tiles geodetic without --tmscompatible
212 292 : );
213 :
214 584 : AddArg("min-zoom", 0, _("Minimum zoom level"), &m_minZoomLevel)
215 292 : .SetMinValueIncluded(0);
216 584 : AddArg("max-zoom", 0, _("Maximum zoom level"), &m_maxZoomLevel)
217 292 : .SetMinValueIncluded(0);
218 :
219 584 : AddArg("min-x", 0, _("Minimum tile X coordinate"), &m_minTileX)
220 292 : .SetMinValueIncluded(0);
221 584 : AddArg("max-x", 0, _("Maximum tile X coordinate"), &m_maxTileX)
222 292 : .SetMinValueIncluded(0);
223 584 : AddArg("min-y", 0, _("Minimum tile Y coordinate"), &m_minTileY)
224 292 : .SetMinValueIncluded(0);
225 584 : AddArg("max-y", 0, _("Maximum tile Y coordinate"), &m_maxTileY)
226 292 : .SetMinValueIncluded(0);
227 : AddArg("no-intersection-ok", 0,
228 : _("Whether dataset extent not intersecting tile matrix is only a "
229 : "warning"),
230 292 : &m_noIntersectionIsOK);
231 :
232 : AddArg("resampling", 'r', _("Resampling method for max zoom"),
233 584 : &m_resampling)
234 : .SetChoices("nearest", "bilinear", "cubic", "cubicspline", "lanczos",
235 : "average", "rms", "mode", "min", "max", "med", "q1", "q3",
236 292 : "sum")
237 292 : .SetDefault("cubic")
238 292 : .SetHiddenChoices("near");
239 : AddArg("overview-resampling", 0, _("Resampling method for overviews"),
240 584 : &m_overviewResampling)
241 : .SetChoices("nearest", "bilinear", "cubic", "cubicspline", "lanczos",
242 : "average", "rms", "mode", "min", "max", "med", "q1", "q3",
243 292 : "sum")
244 292 : .SetHiddenChoices("near");
245 :
246 : AddArg("convention", 0,
247 : _("Tile numbering convention: xyz (from top) or tms (from bottom)"),
248 584 : &m_convention)
249 292 : .SetDefault(m_convention)
250 292 : .SetChoices("xyz", "tms");
251 584 : AddArg("tile-size", 0, _("Override default tile size"), &m_tileSize)
252 292 : .SetMinValueIncluded(64)
253 292 : .SetMaxValueIncluded(32768);
254 : AddArg("add-alpha", 0, _("Whether to force adding an alpha channel"),
255 584 : &m_addalpha)
256 292 : .SetMutualExclusionGroup("alpha");
257 : AddArg("no-alpha", 0, _("Whether to disable adding an alpha channel"),
258 584 : &m_noalpha)
259 292 : .SetMutualExclusionGroup("alpha");
260 : auto &dstNoDataArg =
261 292 : AddArg("dst-nodata", 0, _("Destination nodata value"), &m_dstNoData);
262 292 : AddArg("skip-blank", 0, _("Do not generate blank tiles"), &m_skipBlank);
263 :
264 : {
265 : auto &arg = AddArg("metadata", 0,
266 584 : _("Add metadata item to output tiles"), &m_metadata)
267 584 : .SetMetaVar("<KEY>=<VALUE>")
268 292 : .SetPackedValuesAllowed(false);
269 46 : arg.AddValidationAction([this, &arg]()
270 338 : { return ParseAndValidateKeyValue(arg); });
271 292 : arg.AddHiddenAlias("mo");
272 : }
273 : AddArg("copy-src-metadata", 0,
274 : _("Whether to copy metadata from source dataset"),
275 292 : &m_copySrcMetadata);
276 :
277 : AddArg("aux-xml", 0, _("Generate .aux.xml sidecar files when needed"),
278 292 : &m_auxXML);
279 292 : AddArg("kml", 0, _("Generate KML files"), &m_kml);
280 292 : AddArg("resume", 0, _("Generate only missing files"), &m_resume);
281 :
282 292 : AddNumThreadsArg(&m_numThreads, &m_numThreadsStr);
283 : AddArg("parallel-method", 0,
284 : #ifdef FORK_ALLOWED
285 : _("Parallelization method (thread, spawn, fork)")
286 : #else
287 : _("Parallelization method (thread / spawn)")
288 : #endif
289 : ,
290 584 : &m_parallelMethod)
291 : .SetChoices("thread", "spawn"
292 : #ifdef FORK_ALLOWED
293 : ,
294 : "fork"
295 : #endif
296 292 : );
297 :
298 292 : constexpr const char *ADVANCED_RESAMPLING_CATEGORY = "Advanced Resampling";
299 : auto &excludedValuesArg =
300 : AddArg("excluded-values", 0,
301 : _("Tuples of values (e.g. <R>,<G>,<B> or (<R1>,<G1>,<B1>),"
302 : "(<R2>,<G2>,<B2>)) that must beignored as contributing source "
303 : "pixels during (average) resampling"),
304 584 : &m_excludedValues)
305 292 : .SetCategory(ADVANCED_RESAMPLING_CATEGORY);
306 : auto &excludedValuesPctThresholdArg =
307 : AddArg(
308 : "excluded-values-pct-threshold", 0,
309 : _("Minimum percentage of source pixels that must be set at one of "
310 : "the --excluded-values to cause the excluded value to be used as "
311 : "the target pixel value"),
312 584 : &m_excludedValuesPctThreshold)
313 292 : .SetDefault(m_excludedValuesPctThreshold)
314 292 : .SetMinValueIncluded(0)
315 292 : .SetMaxValueIncluded(100)
316 292 : .SetCategory(ADVANCED_RESAMPLING_CATEGORY);
317 : auto &nodataValuesPctThresholdArg =
318 : AddArg(
319 : "nodata-values-pct-threshold", 0,
320 : _("Minimum percentage of source pixels that must be set at one of "
321 : "nodata (or alpha=0 or any other way to express transparent pixel"
322 : "to cause the target pixel value to be transparent"),
323 584 : &m_nodataValuesPctThreshold)
324 292 : .SetDefault(m_nodataValuesPctThreshold)
325 292 : .SetMinValueIncluded(0)
326 292 : .SetMaxValueIncluded(100)
327 292 : .SetCategory(ADVANCED_RESAMPLING_CATEGORY);
328 :
329 292 : constexpr const char *PUBLICATION_CATEGORY = "Publication";
330 584 : AddArg("webviewer", 0, _("Web viewer to generate"), &m_webviewers)
331 292 : .SetDefault("all")
332 292 : .SetChoices("none", "all", "leaflet", "openlayers", "mapml", "stac")
333 292 : .SetCategory(PUBLICATION_CATEGORY);
334 : AddArg("url", 0,
335 : _("URL address where the generated tiles are going to be published"),
336 584 : &m_url)
337 292 : .SetCategory(PUBLICATION_CATEGORY);
338 584 : AddArg("title", 0, _("Title of the map"), &m_title)
339 292 : .SetCategory(PUBLICATION_CATEGORY);
340 584 : AddArg("copyright", 0, _("Copyright for the map"), &m_copyright)
341 292 : .SetCategory(PUBLICATION_CATEGORY);
342 : AddArg("mapml-template", 0,
343 : _("Filename of a template mapml file where variables will be "
344 : "substituted"),
345 584 : &m_mapmlTemplate)
346 292 : .SetMinCharCount(1)
347 292 : .SetCategory(PUBLICATION_CATEGORY);
348 :
349 292 : AddValidationAction(
350 263 : [this, &dstNoDataArg, &excludedValuesArg,
351 1762 : &excludedValuesPctThresholdArg, &nodataValuesPctThresholdArg]()
352 : {
353 263 : if (m_minTileX >= 0 && m_maxTileX >= 0 && m_minTileX > m_maxTileX)
354 : {
355 1 : ReportError(CE_Failure, CPLE_IllegalArg,
356 : "'min-x' must be lesser or equal to 'max-x'");
357 1 : return false;
358 : }
359 :
360 262 : if (m_minTileY >= 0 && m_maxTileY >= 0 && m_minTileY > m_maxTileY)
361 : {
362 1 : ReportError(CE_Failure, CPLE_IllegalArg,
363 : "'min-y' must be lesser or equal to 'max-y'");
364 1 : return false;
365 : }
366 :
367 261 : if (m_minZoomLevel >= 0 && m_maxZoomLevel >= 0 &&
368 113 : m_minZoomLevel > m_maxZoomLevel)
369 : {
370 1 : ReportError(CE_Failure, CPLE_IllegalArg,
371 : "'min-zoom' must be lesser or equal to 'max-zoom'");
372 1 : return false;
373 : }
374 :
375 260 : if (m_addalpha && dstNoDataArg.IsExplicitlySet())
376 : {
377 1 : ReportError(
378 : CE_Failure, CPLE_IllegalArg,
379 : "'add-alpha' and 'dst-nodata' are mutually exclusive");
380 1 : return false;
381 : }
382 :
383 771 : for (const auto *arg :
384 : {&excludedValuesArg, &excludedValuesPctThresholdArg,
385 1030 : &nodataValuesPctThresholdArg})
386 : {
387 773 : if (arg->IsExplicitlySet() && m_resampling != "average")
388 : {
389 1 : ReportError(CE_Failure, CPLE_AppDefined,
390 : "'%s' can only be specified if 'resampling' is "
391 : "set to 'average'",
392 1 : arg->GetName().c_str());
393 2 : return false;
394 : }
395 773 : if (arg->IsExplicitlySet() && !m_overviewResampling.empty() &&
396 1 : m_overviewResampling != "average")
397 : {
398 1 : ReportError(CE_Failure, CPLE_AppDefined,
399 : "'%s' can only be specified if "
400 : "'overview-resampling' is set to 'average'",
401 1 : arg->GetName().c_str());
402 1 : return false;
403 : }
404 : }
405 :
406 257 : return true;
407 : });
408 292 : }
409 :
410 : /************************************************************************/
411 : /* GetTileIndices() */
412 : /************************************************************************/
413 :
414 706 : static bool GetTileIndices(gdal::TileMatrixSet::TileMatrix &tileMatrix,
415 : bool bInvertAxisTMS, int tileSize,
416 : const double adfExtent[4], int &nMinTileX,
417 : int &nMinTileY, int &nMaxTileX, int &nMaxTileY,
418 : bool noIntersectionIsOK, bool &bIntersects,
419 : bool checkRasterOverflow = true)
420 : {
421 706 : if (tileSize > 0)
422 : {
423 136 : tileMatrix.mResX *=
424 136 : static_cast<double>(tileMatrix.mTileWidth) / tileSize;
425 136 : tileMatrix.mResY *=
426 136 : static_cast<double>(tileMatrix.mTileHeight) / tileSize;
427 136 : tileMatrix.mTileWidth = tileSize;
428 136 : tileMatrix.mTileHeight = tileSize;
429 : }
430 :
431 706 : if (bInvertAxisTMS)
432 2 : std::swap(tileMatrix.mTopLeftX, tileMatrix.mTopLeftY);
433 :
434 706 : const double dfTileWidth = tileMatrix.mResX * tileMatrix.mTileWidth;
435 706 : const double dfTileHeight = tileMatrix.mResY * tileMatrix.mTileHeight;
436 :
437 706 : constexpr double EPSILON = 1e-3;
438 706 : const double dfMinTileX =
439 706 : (adfExtent[0] - tileMatrix.mTopLeftX) / dfTileWidth;
440 706 : nMinTileX = static_cast<int>(
441 1412 : std::clamp(std::floor(dfMinTileX + EPSILON), 0.0,
442 706 : static_cast<double>(tileMatrix.mMatrixWidth - 1)));
443 706 : const double dfMinTileY =
444 706 : (tileMatrix.mTopLeftY - adfExtent[3]) / dfTileHeight;
445 706 : nMinTileY = static_cast<int>(
446 1412 : std::clamp(std::floor(dfMinTileY + EPSILON), 0.0,
447 706 : static_cast<double>(tileMatrix.mMatrixHeight - 1)));
448 706 : const double dfMaxTileX =
449 706 : (adfExtent[2] - tileMatrix.mTopLeftX) / dfTileWidth;
450 706 : nMaxTileX = static_cast<int>(
451 1412 : std::clamp(std::floor(dfMaxTileX + EPSILON), 0.0,
452 706 : static_cast<double>(tileMatrix.mMatrixWidth - 1)));
453 706 : const double dfMaxTileY =
454 706 : (tileMatrix.mTopLeftY - adfExtent[1]) / dfTileHeight;
455 706 : nMaxTileY = static_cast<int>(
456 1412 : std::clamp(std::floor(dfMaxTileY + EPSILON), 0.0,
457 706 : static_cast<double>(tileMatrix.mMatrixHeight - 1)));
458 :
459 706 : bIntersects = (dfMinTileX <= tileMatrix.mMatrixWidth && dfMaxTileX >= 0 &&
460 1412 : dfMinTileY <= tileMatrix.mMatrixHeight && dfMaxTileY >= 0);
461 706 : if (!bIntersects)
462 : {
463 2 : CPLDebug("gdal_raster_tile",
464 : "dfMinTileX=%g dfMinTileY=%g dfMaxTileX=%g dfMaxTileY=%g",
465 : dfMinTileX, dfMinTileY, dfMaxTileX, dfMaxTileY);
466 2 : CPLError(noIntersectionIsOK ? CE_Warning : CE_Failure, CPLE_AppDefined,
467 : "Extent of source dataset is not compatible with extent of "
468 : "tile matrix %s",
469 : tileMatrix.mId.c_str());
470 2 : return noIntersectionIsOK;
471 : }
472 704 : if (checkRasterOverflow)
473 : {
474 524 : if (nMaxTileX - nMinTileX + 1 > INT_MAX / tileMatrix.mTileWidth ||
475 524 : nMaxTileY - nMinTileY + 1 > INT_MAX / tileMatrix.mTileHeight)
476 : {
477 0 : CPLError(CE_Failure, CPLE_AppDefined, "Too large zoom level");
478 0 : return false;
479 : }
480 : }
481 704 : return true;
482 : }
483 :
484 : /************************************************************************/
485 : /* GetFileY() */
486 : /************************************************************************/
487 :
488 6330 : static int GetFileY(int iY, const gdal::TileMatrixSet::TileMatrix &tileMatrix,
489 : const std::string &convention)
490 : {
491 6330 : return convention == "xyz" ? iY : tileMatrix.mMatrixHeight - 1 - iY;
492 : }
493 :
494 : /************************************************************************/
495 : /* GenerateTile() */
496 : /************************************************************************/
497 :
498 : // Cf http://www.libpng.org/pub/png/spec/1.2/PNG-Filters.html
499 : // for specification of SUB and AVG filters
500 528872 : inline GByte PNG_SUB(int nVal, int nValPrev)
501 : {
502 528872 : return static_cast<GByte>((nVal - nValPrev) & 0xff);
503 : }
504 :
505 135653000 : inline GByte PNG_AVG(int nVal, int nValPrev, int nValUp)
506 : {
507 135653000 : return static_cast<GByte>((nVal - (nValPrev + nValUp) / 2) & 0xff);
508 : }
509 :
510 5869560 : inline GByte PNG_PAETH(int nVal, int nValPrev, int nValUp, int nValUpPrev)
511 : {
512 5869560 : const int p = nValPrev + nValUp - nValUpPrev;
513 5869560 : const int pa = std::abs(p - nValPrev);
514 5869560 : const int pb = std::abs(p - nValUp);
515 5869560 : const int pc = std::abs(p - nValUpPrev);
516 5869560 : if (pa <= pb && pa <= pc)
517 3687170 : return static_cast<GByte>((nVal - nValPrev) & 0xff);
518 2182390 : else if (pb <= pc)
519 1918340 : return static_cast<GByte>((nVal - nValUp) & 0xff);
520 : else
521 264048 : return static_cast<GByte>((nVal - nValUpPrev) & 0xff);
522 : }
523 :
524 : #ifdef USE_PAETH_SSE2
525 :
526 47398200 : static inline __m128i abs_epi16(__m128i x)
527 : {
528 : #if defined(__SSSE3__) || defined(__AVX__) || defined(USE_NEON_OPTIMIZATIONS)
529 : return _mm_abs_epi16(x);
530 : #else
531 47398200 : __m128i mask = _mm_srai_epi16(x, 15);
532 94796500 : return _mm_sub_epi16(_mm_xor_si128(x, mask), mask);
533 : #endif
534 : }
535 :
536 47398200 : static inline __m128i blendv(__m128i a, __m128i b, __m128i mask)
537 : {
538 : #if defined(__SSE4_1__) || defined(__AVX__) || defined(USE_NEON_OPTIMIZATIONS)
539 : return _mm_blendv_epi8(a, b, mask);
540 : #else
541 142195000 : return _mm_or_si128(_mm_andnot_si128(mask, a), _mm_and_si128(mask, b));
542 : #endif
543 : }
544 :
545 7899710 : static inline __m128i PNG_PAETH_SSE2(__m128i up_prev, __m128i up, __m128i prev,
546 : __m128i cur, __m128i &cost)
547 : {
548 15799400 : auto cur_lo = _mm_unpacklo_epi8(cur, _mm_setzero_si128());
549 15799400 : auto prev_lo = _mm_unpacklo_epi8(prev, _mm_setzero_si128());
550 15799400 : auto up_lo = _mm_unpacklo_epi8(up, _mm_setzero_si128());
551 15799400 : auto up_prev_lo = _mm_unpacklo_epi8(up_prev, _mm_setzero_si128());
552 15799400 : auto cur_hi = _mm_unpackhi_epi8(cur, _mm_setzero_si128());
553 15799400 : auto prev_hi = _mm_unpackhi_epi8(prev, _mm_setzero_si128());
554 15799400 : auto up_hi = _mm_unpackhi_epi8(up, _mm_setzero_si128());
555 15799400 : auto up_prev_hi = _mm_unpackhi_epi8(up_prev, _mm_setzero_si128());
556 :
557 7899710 : auto pa_lo = _mm_sub_epi16(up_lo, up_prev_lo);
558 7899710 : auto pb_lo = _mm_sub_epi16(prev_lo, up_prev_lo);
559 7899710 : auto pc_lo = _mm_add_epi16(pa_lo, pb_lo);
560 7899710 : pa_lo = abs_epi16(pa_lo);
561 7899710 : pb_lo = abs_epi16(pb_lo);
562 7899710 : pc_lo = abs_epi16(pc_lo);
563 15799400 : auto min_lo = _mm_min_epi16(_mm_min_epi16(pa_lo, pb_lo), pc_lo);
564 :
565 7899710 : auto res_lo = blendv(up_prev_lo, up_lo, _mm_cmpeq_epi16(min_lo, pb_lo));
566 7899710 : res_lo = blendv(res_lo, prev_lo, _mm_cmpeq_epi16(min_lo, pa_lo));
567 23699100 : res_lo = _mm_and_si128(_mm_sub_epi16(cur_lo, res_lo), _mm_set1_epi16(0xFF));
568 :
569 31598800 : auto cost_lo = blendv(_mm_sub_epi16(_mm_set1_epi16(256), res_lo), res_lo,
570 : _mm_cmplt_epi16(res_lo, _mm_set1_epi16(128)));
571 :
572 7899710 : auto pa_hi = _mm_sub_epi16(up_hi, up_prev_hi);
573 7899710 : auto pb_hi = _mm_sub_epi16(prev_hi, up_prev_hi);
574 7899710 : auto pc_hi = _mm_add_epi16(pa_hi, pb_hi);
575 7899710 : pa_hi = abs_epi16(pa_hi);
576 7899710 : pb_hi = abs_epi16(pb_hi);
577 7899710 : pc_hi = abs_epi16(pc_hi);
578 15799400 : auto min_hi = _mm_min_epi16(_mm_min_epi16(pa_hi, pb_hi), pc_hi);
579 :
580 7899710 : auto res_hi = blendv(up_prev_hi, up_hi, _mm_cmpeq_epi16(min_hi, pb_hi));
581 7899710 : res_hi = blendv(res_hi, prev_hi, _mm_cmpeq_epi16(min_hi, pa_hi));
582 23699100 : res_hi = _mm_and_si128(_mm_sub_epi16(cur_hi, res_hi), _mm_set1_epi16(0xFF));
583 :
584 31598800 : auto cost_hi = blendv(_mm_sub_epi16(_mm_set1_epi16(256), res_hi), res_hi,
585 : _mm_cmplt_epi16(res_hi, _mm_set1_epi16(128)));
586 :
587 7899710 : cost_lo = _mm_add_epi16(cost_lo, cost_hi);
588 :
589 7899710 : cost =
590 23699100 : _mm_add_epi32(cost, _mm_unpacklo_epi16(cost_lo, _mm_setzero_si128()));
591 7899710 : cost =
592 23699100 : _mm_add_epi32(cost, _mm_unpackhi_epi16(cost_lo, _mm_setzero_si128()));
593 :
594 7899710 : return _mm_packus_epi16(res_lo, res_hi);
595 : }
596 :
597 173656 : static int RunPaeth(const GByte *srcBuffer, int nBands,
598 : int nSrcBufferBandStride, GByte *outBuffer, int W,
599 : int &costPaeth)
600 : {
601 173656 : __m128i xmm_cost = _mm_setzero_si128();
602 173656 : int i = 1;
603 699213 : for (int k = 0; k < nBands; ++k)
604 : {
605 8425260 : for (i = 1; i + 15 < W; i += 16)
606 : {
607 7899710 : auto up_prev = _mm_loadu_si128(
608 7899710 : reinterpret_cast<const __m128i *>(srcBuffer - W + (i - 1)));
609 7899710 : auto up = _mm_loadu_si128(
610 7899710 : reinterpret_cast<const __m128i *>(srcBuffer - W + i));
611 7899710 : auto prev = _mm_loadu_si128(
612 7899710 : reinterpret_cast<const __m128i *>(srcBuffer + (i - 1)));
613 7899710 : auto cur = _mm_loadu_si128(
614 7899710 : reinterpret_cast<const __m128i *>(srcBuffer + i));
615 :
616 7899710 : auto res = PNG_PAETH_SSE2(up_prev, up, prev, cur, xmm_cost);
617 :
618 7899710 : _mm_storeu_si128(reinterpret_cast<__m128i *>(outBuffer + k * W + i),
619 : res);
620 : }
621 525557 : srcBuffer += nSrcBufferBandStride;
622 : }
623 :
624 : int32_t ar_cost[4];
625 173656 : _mm_storeu_si128(reinterpret_cast<__m128i *>(ar_cost), xmm_cost);
626 868280 : for (int k = 0; k < 4; ++k)
627 694624 : costPaeth += ar_cost[k];
628 :
629 173656 : return i;
630 : }
631 :
632 : #endif // USE_PAETH_SSE2
633 :
634 782 : static bool GenerateTile(
635 : GDALDataset *poSrcDS, GDALDriver *m_poDstDriver, const char *pszExtension,
636 : CSLConstList creationOptions, GDALWarpOperation &oWO,
637 : const OGRSpatialReference &oSRS_TMS, GDALDataType eWorkingDataType,
638 : const gdal::TileMatrixSet::TileMatrix &tileMatrix,
639 : const std::string &outputDirectory, int nBands, const double *pdfDstNoData,
640 : int nZoomLevel, int iX, int iY, const std::string &convention,
641 : int nMinTileX, int nMinTileY, bool bSkipBlank, bool bUserAskedForAlpha,
642 : bool bAuxXML, bool bResume, const std::vector<std::string> &metadata,
643 : const GDALColorTable *poColorTable, std::vector<GByte> &dstBuffer,
644 : std::vector<GByte> &tmpBuffer)
645 : {
646 : const std::string osDirZ = CPLFormFilenameSafe(
647 1564 : outputDirectory.c_str(), CPLSPrintf("%d", nZoomLevel), nullptr);
648 : const std::string osDirX =
649 1564 : CPLFormFilenameSafe(osDirZ.c_str(), CPLSPrintf("%d", iX), nullptr);
650 782 : const int iFileY = GetFileY(iY, tileMatrix, convention);
651 : const std::string osFilename = CPLFormFilenameSafe(
652 1564 : osDirX.c_str(), CPLSPrintf("%d", iFileY), pszExtension);
653 :
654 782 : if (bResume)
655 : {
656 : VSIStatBufL sStat;
657 5 : if (VSIStatL(osFilename.c_str(), &sStat) == 0)
658 5 : return true;
659 : }
660 :
661 777 : const int nDstXOff = (iX - nMinTileX) * tileMatrix.mTileWidth;
662 777 : const int nDstYOff = (iY - nMinTileY) * tileMatrix.mTileHeight;
663 777 : memset(dstBuffer.data(), 0, dstBuffer.size());
664 1554 : const CPLErr eErr = oWO.WarpRegionToBuffer(
665 777 : nDstXOff, nDstYOff, tileMatrix.mTileWidth, tileMatrix.mTileHeight,
666 777 : dstBuffer.data(), eWorkingDataType);
667 777 : if (eErr != CE_None)
668 2 : return false;
669 :
670 : bool bDstHasAlpha =
671 848 : nBands > poSrcDS->GetRasterCount() ||
672 73 : (nBands == poSrcDS->GetRasterCount() &&
673 72 : poSrcDS->GetRasterBand(nBands)->GetColorInterpretation() ==
674 775 : GCI_AlphaBand);
675 775 : const size_t nBytesPerBand = static_cast<size_t>(tileMatrix.mTileWidth) *
676 775 : tileMatrix.mTileHeight *
677 775 : GDALGetDataTypeSizeBytes(eWorkingDataType);
678 775 : if (bDstHasAlpha && bSkipBlank)
679 : {
680 114 : bool bBlank = true;
681 3104800 : for (size_t i = 0; i < nBytesPerBand && bBlank; ++i)
682 : {
683 3104690 : bBlank = (dstBuffer[(nBands - 1) * nBytesPerBand + i] == 0);
684 : }
685 114 : if (bBlank)
686 43 : return true;
687 : }
688 732 : if (bDstHasAlpha && !bUserAskedForAlpha)
689 : {
690 711 : bool bAllOpaque = true;
691 40113600 : for (size_t i = 0; i < nBytesPerBand && bAllOpaque; ++i)
692 : {
693 40112900 : bAllOpaque = (dstBuffer[(nBands - 1) * nBytesPerBand + i] == 255);
694 : }
695 711 : if (bAllOpaque)
696 : {
697 598 : bDstHasAlpha = false;
698 598 : nBands--;
699 : }
700 : }
701 :
702 732 : VSIMkdir(osDirZ.c_str(), 0755);
703 732 : VSIMkdir(osDirX.c_str(), 0755);
704 :
705 : const bool bSupportsCreateOnlyVisibleAtCloseTime =
706 1464 : m_poDstDriver->GetMetadataItem(
707 732 : GDAL_DCAP_CREATE_ONLY_VISIBLE_AT_CLOSE_TIME) != nullptr;
708 :
709 : const std::string osTmpFilename = bSupportsCreateOnlyVisibleAtCloseTime
710 : ? osFilename
711 1464 : : osFilename + ".tmp." + pszExtension;
712 :
713 732 : const int W = tileMatrix.mTileWidth;
714 732 : const int H = tileMatrix.mTileHeight;
715 732 : constexpr int EXTRA_BYTE_PER_ROW = 1; // for filter type
716 732 : constexpr int EXTRA_ROWS = 2; // for paethBuffer and paethBufferTmp
717 728 : if (!bAuxXML && EQUAL(pszExtension, "png") &&
718 704 : eWorkingDataType == GDT_UInt8 && poColorTable == nullptr &&
719 703 : pdfDstNoData == nullptr && W <= INT_MAX / nBands &&
720 703 : nBands * W <= INT_MAX - EXTRA_BYTE_PER_ROW &&
721 703 : H <= INT_MAX - EXTRA_ROWS &&
722 703 : EXTRA_BYTE_PER_ROW + nBands * W <= INT_MAX / (H + EXTRA_ROWS) &&
723 2163 : CSLCount(creationOptions) == 0 &&
724 703 : CPLTestBool(
725 : CPLGetConfigOption("GDAL_RASTER_TILE_USE_PNG_OPTIM", "YES")))
726 : {
727 : // This is an optimized code path completely shortcircuiting libpng
728 : // We manually generate the PNG file using the Average or PAETH filter
729 : // and ZLIB compressing the whole buffer, hopefully with libdeflate.
730 :
731 703 : const int nDstBytesPerRow = EXTRA_BYTE_PER_ROW + nBands * W;
732 703 : const int nBPB = static_cast<int>(nBytesPerBand);
733 :
734 703 : bool bBlank = false;
735 703 : if (bDstHasAlpha)
736 : {
737 109 : bBlank = true;
738 2685550 : for (int i = 0; i < nBPB && bBlank; ++i)
739 : {
740 2685440 : bBlank = (dstBuffer[(nBands - 1) * nBPB + i] == 0);
741 : }
742 : }
743 :
744 703 : constexpr GByte PNG_FILTER_SUB = 1; // horizontal diff
745 703 : constexpr GByte PNG_FILTER_AVG = 3; // average with pixel before and up
746 703 : constexpr GByte PNG_FILTER_PAETH = 4;
747 :
748 703 : if (bBlank)
749 18 : tmpBuffer.clear();
750 703 : const int tmpBufferSize = cpl::fits_on<int>(nDstBytesPerRow * H);
751 : try
752 : {
753 : // cppcheck-suppress integerOverflowCond
754 703 : tmpBuffer.resize(tmpBufferSize + EXTRA_ROWS * nDstBytesPerRow);
755 : }
756 0 : catch (const std::exception &)
757 : {
758 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
759 : "Out of memory allocating temporary buffer");
760 0 : return false;
761 : }
762 703 : GByte *const paethBuffer = tmpBuffer.data() + tmpBufferSize;
763 : #ifdef USE_PAETH_SSE2
764 : GByte *const paethBufferTmp =
765 703 : tmpBuffer.data() + tmpBufferSize + nDstBytesPerRow;
766 : #endif
767 :
768 : const char *pszGDAL_RASTER_TILE_PNG_FILTER =
769 703 : CPLGetConfigOption("GDAL_RASTER_TILE_PNG_FILTER", "");
770 703 : const bool bForcePaeth = EQUAL(pszGDAL_RASTER_TILE_PNG_FILTER, "PAETH");
771 703 : const bool bForceAvg = EQUAL(pszGDAL_RASTER_TILE_PNG_FILTER, "AVERAGE");
772 :
773 176319 : for (int j = 0; !bBlank && j < H; ++j)
774 : {
775 175616 : if (j > 0)
776 : {
777 174931 : tmpBuffer[cpl::fits_on<int>(j * nDstBytesPerRow)] =
778 : PNG_FILTER_AVG;
779 703803 : for (int i = 0; i < nBands; ++i)
780 : {
781 528872 : tmpBuffer[1 + j * nDstBytesPerRow + i] =
782 528872 : PNG_AVG(dstBuffer[i * nBPB + j * W], 0,
783 528872 : dstBuffer[i * nBPB + (j - 1) * W]);
784 : }
785 : }
786 : else
787 : {
788 685 : tmpBuffer[cpl::fits_on<int>(j * nDstBytesPerRow)] =
789 : PNG_FILTER_SUB;
790 2757 : for (int i = 0; i < nBands; ++i)
791 : {
792 2072 : tmpBuffer[1 + j * nDstBytesPerRow + i] =
793 2072 : dstBuffer[i * nBPB + j * W];
794 : }
795 : }
796 :
797 175616 : if (nBands == 1)
798 : {
799 1792 : if (j > 0)
800 : {
801 1785 : int costAvg = 0;
802 456960 : for (int i = 1; i < W; ++i)
803 : {
804 : const GByte v =
805 455175 : PNG_AVG(dstBuffer[0 * nBPB + j * W + i],
806 455175 : dstBuffer[0 * nBPB + j * W + i - 1],
807 455175 : dstBuffer[0 * nBPB + (j - 1) * W + i]);
808 455175 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 0] = v;
809 :
810 455175 : costAvg += (v < 128) ? v : 256 - v;
811 : }
812 :
813 1785 : if (!bForceAvg)
814 : {
815 1530 : int costPaeth = 0;
816 : {
817 1530 : const int i = 0;
818 1530 : const GByte v = PNG_PAETH(
819 1530 : dstBuffer[0 * nBPB + j * W + i], 0,
820 1530 : dstBuffer[0 * nBPB + (j - 1) * W + i], 0);
821 1530 : paethBuffer[i] = v;
822 :
823 1530 : costPaeth += (v < 128) ? v : 256 - v;
824 : }
825 :
826 : #ifdef USE_PAETH_SSE2
827 : const int iLimitSSE2 =
828 1530 : RunPaeth(dstBuffer.data() + j * W, nBands, nBPB,
829 : paethBuffer, W, costPaeth);
830 1530 : int i = iLimitSSE2;
831 : #else
832 : int i = 1;
833 : #endif
834 7560 : for (; i < W && (costPaeth < costAvg || bForcePaeth);
835 : ++i)
836 : {
837 6030 : const GByte v = PNG_PAETH(
838 6030 : dstBuffer[0 * nBPB + j * W + i],
839 6030 : dstBuffer[0 * nBPB + j * W + i - 1],
840 6030 : dstBuffer[0 * nBPB + (j - 1) * W + i],
841 6030 : dstBuffer[0 * nBPB + (j - 1) * W + i - 1]);
842 6030 : paethBuffer[i] = v;
843 :
844 6030 : costPaeth += (v < 128) ? v : 256 - v;
845 : }
846 1530 : if (costPaeth < costAvg || bForcePaeth)
847 : {
848 402 : GByte *out = tmpBuffer.data() +
849 402 : cpl::fits_on<int>(j * nDstBytesPerRow);
850 402 : *out = PNG_FILTER_PAETH;
851 402 : ++out;
852 402 : memcpy(out, paethBuffer, nDstBytesPerRow - 1);
853 : }
854 : }
855 : }
856 : else
857 : {
858 1792 : for (int i = 1; i < W; ++i)
859 : {
860 1785 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 0] =
861 1785 : PNG_SUB(dstBuffer[0 * nBPB + j * W + i],
862 1785 : dstBuffer[0 * nBPB + j * W + i - 1]);
863 : }
864 : }
865 : }
866 173824 : else if (nBands == 2)
867 : {
868 7936 : if (j > 0)
869 : {
870 7906 : int costAvg = 0;
871 2154750 : for (int i = 1; i < W; ++i)
872 : {
873 : {
874 : const GByte v =
875 2146850 : PNG_AVG(dstBuffer[0 * nBPB + j * W + i],
876 2146850 : dstBuffer[0 * nBPB + j * W + i - 1],
877 2146850 : dstBuffer[0 * nBPB + (j - 1) * W + i]);
878 2146850 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands +
879 2146850 : 0] = v;
880 :
881 2146850 : costAvg += (v < 128) ? v : 256 - v;
882 : }
883 : {
884 : const GByte v =
885 2146850 : PNG_AVG(dstBuffer[1 * nBPB + j * W + i],
886 2146850 : dstBuffer[1 * nBPB + j * W + i - 1],
887 2146850 : dstBuffer[1 * nBPB + (j - 1) * W + i]);
888 2146850 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands +
889 2146850 : 1] = v;
890 :
891 2146850 : costAvg += (v < 128) ? v : 256 - v;
892 : }
893 : }
894 :
895 7906 : if (!bForceAvg)
896 : {
897 7651 : int costPaeth = 0;
898 22953 : for (int k = 0; k < nBands; ++k)
899 : {
900 15302 : const int i = 0;
901 15302 : const GByte v = PNG_PAETH(
902 15302 : dstBuffer[k * nBPB + j * W + i], 0,
903 15302 : dstBuffer[k * nBPB + (j - 1) * W + i], 0);
904 15302 : paethBuffer[i * nBands + k] = v;
905 :
906 15302 : costPaeth += (v < 128) ? v : 256 - v;
907 : }
908 :
909 : #ifdef USE_PAETH_SSE2
910 : const int iLimitSSE2 =
911 7651 : RunPaeth(dstBuffer.data() + j * W, nBands, nBPB,
912 : paethBufferTmp, W, costPaeth);
913 7651 : int i = iLimitSSE2;
914 : #else
915 : int i = 1;
916 : #endif
917 67816 : for (; i < W && (costPaeth < costAvg || bForcePaeth);
918 : ++i)
919 : {
920 : {
921 60165 : const GByte v = PNG_PAETH(
922 60165 : dstBuffer[0 * nBPB + j * W + i],
923 60165 : dstBuffer[0 * nBPB + j * W + i - 1],
924 60165 : dstBuffer[0 * nBPB + (j - 1) * W + i],
925 60165 : dstBuffer[0 * nBPB + (j - 1) * W + i - 1]);
926 60165 : paethBuffer[i * nBands + 0] = v;
927 :
928 60165 : costPaeth += (v < 128) ? v : 256 - v;
929 : }
930 : {
931 60165 : const GByte v = PNG_PAETH(
932 60165 : dstBuffer[1 * nBPB + j * W + i],
933 60165 : dstBuffer[1 * nBPB + j * W + i - 1],
934 60165 : dstBuffer[1 * nBPB + (j - 1) * W + i],
935 60165 : dstBuffer[1 * nBPB + (j - 1) * W + i - 1]);
936 60165 : paethBuffer[i * nBands + 1] = v;
937 :
938 60165 : costPaeth += (v < 128) ? v : 256 - v;
939 : }
940 : }
941 7651 : if (costPaeth < costAvg || bForcePaeth)
942 : {
943 4011 : GByte *out = tmpBuffer.data() +
944 4011 : cpl::fits_on<int>(j * nDstBytesPerRow);
945 4011 : *out = PNG_FILTER_PAETH;
946 4011 : ++out;
947 : #ifdef USE_PAETH_SSE2
948 4011 : memcpy(out, paethBuffer, nBands);
949 976635 : for (int iTmp = 1; iTmp < iLimitSSE2; ++iTmp)
950 : {
951 972624 : out[nBands * iTmp + 0] =
952 972624 : paethBufferTmp[0 * W + iTmp];
953 972624 : out[nBands * iTmp + 1] =
954 972624 : paethBufferTmp[1 * W + iTmp];
955 : }
956 4011 : memcpy(
957 4011 : out + iLimitSSE2 * nBands,
958 4011 : paethBuffer + iLimitSSE2 * nBands,
959 4011 : cpl::fits_on<int>((W - iLimitSSE2) * nBands));
960 : #else
961 : memcpy(out, paethBuffer, nDstBytesPerRow - 1);
962 : #endif
963 : }
964 : }
965 : }
966 : else
967 : {
968 7936 : for (int i = 1; i < W; ++i)
969 : {
970 7906 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 0] =
971 7906 : PNG_SUB(dstBuffer[0 * nBPB + j * W + i],
972 7906 : dstBuffer[0 * nBPB + j * W + i - 1]);
973 7906 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 1] =
974 7906 : PNG_SUB(dstBuffer[1 * nBPB + j * W + i],
975 7906 : dstBuffer[1 * nBPB + j * W + i - 1]);
976 : }
977 : }
978 : }
979 165888 : else if (nBands == 3)
980 : {
981 150272 : if (j > 0)
982 : {
983 149685 : int costAvg = 0;
984 38319400 : for (int i = 1; i < W; ++i)
985 : {
986 : {
987 : const GByte v =
988 38169700 : PNG_AVG(dstBuffer[0 * nBPB + j * W + i],
989 38169700 : dstBuffer[0 * nBPB + j * W + i - 1],
990 38169700 : dstBuffer[0 * nBPB + (j - 1) * W + i]);
991 38169700 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands +
992 38169700 : 0] = v;
993 :
994 38169700 : costAvg += (v < 128) ? v : 256 - v;
995 : }
996 : {
997 : const GByte v =
998 38169700 : PNG_AVG(dstBuffer[1 * nBPB + j * W + i],
999 38169700 : dstBuffer[1 * nBPB + j * W + i - 1],
1000 38169700 : dstBuffer[1 * nBPB + (j - 1) * W + i]);
1001 38169700 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands +
1002 38169700 : 1] = v;
1003 :
1004 38169700 : costAvg += (v < 128) ? v : 256 - v;
1005 : }
1006 : {
1007 : const GByte v =
1008 38169700 : PNG_AVG(dstBuffer[2 * nBPB + j * W + i],
1009 38169700 : dstBuffer[2 * nBPB + j * W + i - 1],
1010 38169700 : dstBuffer[2 * nBPB + (j - 1) * W + i]);
1011 38169700 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands +
1012 38169700 : 2] = v;
1013 :
1014 38169700 : costAvg += (v < 128) ? v : 256 - v;
1015 : }
1016 : }
1017 :
1018 149685 : if (!bForceAvg)
1019 : {
1020 149175 : int costPaeth = 0;
1021 596700 : for (int k = 0; k < nBands; ++k)
1022 : {
1023 447525 : const int i = 0;
1024 447525 : const GByte v = PNG_PAETH(
1025 447525 : dstBuffer[k * nBPB + j * W + i], 0,
1026 447525 : dstBuffer[k * nBPB + (j - 1) * W + i], 0);
1027 447525 : paethBuffer[i * nBands + k] = v;
1028 :
1029 447525 : costPaeth += (v < 128) ? v : 256 - v;
1030 : }
1031 :
1032 : #ifdef USE_PAETH_SSE2
1033 : const int iLimitSSE2 =
1034 149175 : RunPaeth(dstBuffer.data() + j * W, nBands, nBPB,
1035 : paethBufferTmp, W, costPaeth);
1036 149175 : int i = iLimitSSE2;
1037 : #else
1038 : int i = 1;
1039 : #endif
1040 1795980 : for (; i < W && (costPaeth < costAvg || bForcePaeth);
1041 : ++i)
1042 : {
1043 : {
1044 1646810 : const GByte v = PNG_PAETH(
1045 1646810 : dstBuffer[0 * nBPB + j * W + i],
1046 1646810 : dstBuffer[0 * nBPB + j * W + i - 1],
1047 1646810 : dstBuffer[0 * nBPB + (j - 1) * W + i],
1048 1646810 : dstBuffer[0 * nBPB + (j - 1) * W + i - 1]);
1049 1646810 : paethBuffer[i * nBands + 0] = v;
1050 :
1051 1646810 : costPaeth += (v < 128) ? v : 256 - v;
1052 : }
1053 : {
1054 1646810 : const GByte v = PNG_PAETH(
1055 1646810 : dstBuffer[1 * nBPB + j * W + i],
1056 1646810 : dstBuffer[1 * nBPB + j * W + i - 1],
1057 1646810 : dstBuffer[1 * nBPB + (j - 1) * W + i],
1058 1646810 : dstBuffer[1 * nBPB + (j - 1) * W + i - 1]);
1059 1646810 : paethBuffer[i * nBands + 1] = v;
1060 :
1061 1646810 : costPaeth += (v < 128) ? v : 256 - v;
1062 : }
1063 : {
1064 1646810 : const GByte v = PNG_PAETH(
1065 1646810 : dstBuffer[2 * nBPB + j * W + i],
1066 1646810 : dstBuffer[2 * nBPB + j * W + i - 1],
1067 1646810 : dstBuffer[2 * nBPB + (j - 1) * W + i],
1068 1646810 : dstBuffer[2 * nBPB + (j - 1) * W + i - 1]);
1069 1646810 : paethBuffer[i * nBands + 2] = v;
1070 :
1071 1646810 : costPaeth += (v < 128) ? v : 256 - v;
1072 : }
1073 : }
1074 :
1075 149175 : if (costPaeth < costAvg || bForcePaeth)
1076 : {
1077 108682 : GByte *out = tmpBuffer.data() +
1078 108682 : cpl::fits_on<int>(j * nDstBytesPerRow);
1079 108682 : *out = PNG_FILTER_PAETH;
1080 108682 : ++out;
1081 : #ifdef USE_PAETH_SSE2
1082 108682 : memcpy(out, paethBuffer, nBands);
1083 26192400 : for (int iTmp = 1; iTmp < iLimitSSE2; ++iTmp)
1084 : {
1085 26083700 : out[nBands * iTmp + 0] =
1086 26083700 : paethBufferTmp[0 * W + iTmp];
1087 26083700 : out[nBands * iTmp + 1] =
1088 26083700 : paethBufferTmp[1 * W + iTmp];
1089 26083700 : out[nBands * iTmp + 2] =
1090 26083700 : paethBufferTmp[2 * W + iTmp];
1091 : }
1092 108682 : memcpy(
1093 108682 : out + iLimitSSE2 * nBands,
1094 108682 : paethBuffer + iLimitSSE2 * nBands,
1095 108682 : cpl::fits_on<int>((W - iLimitSSE2) * nBands));
1096 : #else
1097 : memcpy(out, paethBuffer, nDstBytesPerRow - 1);
1098 : #endif
1099 : }
1100 : }
1101 : }
1102 : else
1103 : {
1104 150272 : for (int i = 1; i < W; ++i)
1105 : {
1106 149685 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 0] =
1107 149685 : PNG_SUB(dstBuffer[0 * nBPB + j * W + i],
1108 149685 : dstBuffer[0 * nBPB + j * W + i - 1]);
1109 149685 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 1] =
1110 149685 : PNG_SUB(dstBuffer[1 * nBPB + j * W + i],
1111 149685 : dstBuffer[1 * nBPB + j * W + i - 1]);
1112 149685 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 2] =
1113 149685 : PNG_SUB(dstBuffer[2 * nBPB + j * W + i],
1114 149685 : dstBuffer[2 * nBPB + j * W + i - 1]);
1115 : }
1116 : }
1117 : }
1118 : else /* if( nBands == 4 ) */
1119 : {
1120 15616 : if (j > 0)
1121 : {
1122 15555 : int costAvg = 0;
1123 3982080 : for (int i = 1; i < W; ++i)
1124 : {
1125 : {
1126 : const GByte v =
1127 3966520 : PNG_AVG(dstBuffer[0 * nBPB + j * W + i],
1128 3966520 : dstBuffer[0 * nBPB + j * W + i - 1],
1129 3966520 : dstBuffer[0 * nBPB + (j - 1) * W + i]);
1130 3966520 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands +
1131 3966520 : 0] = v;
1132 :
1133 3966520 : costAvg += (v < 128) ? v : 256 - v;
1134 : }
1135 : {
1136 : const GByte v =
1137 3966520 : PNG_AVG(dstBuffer[1 * nBPB + j * W + i],
1138 3966520 : dstBuffer[1 * nBPB + j * W + i - 1],
1139 3966520 : dstBuffer[1 * nBPB + (j - 1) * W + i]);
1140 3966520 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands +
1141 3966520 : 1] = v;
1142 :
1143 3966520 : costAvg += (v < 128) ? v : 256 - v;
1144 : }
1145 : {
1146 : const GByte v =
1147 3966520 : PNG_AVG(dstBuffer[2 * nBPB + j * W + i],
1148 3966520 : dstBuffer[2 * nBPB + j * W + i - 1],
1149 3966520 : dstBuffer[2 * nBPB + (j - 1) * W + i]);
1150 3966520 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands +
1151 3966520 : 2] = v;
1152 :
1153 3966520 : costAvg += (v < 128) ? v : 256 - v;
1154 : }
1155 : {
1156 : const GByte v =
1157 3966520 : PNG_AVG(dstBuffer[3 * nBPB + j * W + i],
1158 3966520 : dstBuffer[3 * nBPB + j * W + i - 1],
1159 3966520 : dstBuffer[3 * nBPB + (j - 1) * W + i]);
1160 3966520 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands +
1161 3966520 : 3] = v;
1162 :
1163 3966520 : costAvg += (v < 128) ? v : 256 - v;
1164 : }
1165 : }
1166 :
1167 15555 : if (!bForceAvg)
1168 : {
1169 15300 : int costPaeth = 0;
1170 76500 : for (int k = 0; k < nBands; ++k)
1171 : {
1172 61200 : const int i = 0;
1173 61200 : const GByte v = PNG_PAETH(
1174 61200 : dstBuffer[k * nBPB + j * W + i], 0,
1175 61200 : dstBuffer[k * nBPB + (j - 1) * W + i], 0);
1176 61200 : paethBuffer[i * nBands + k] = v;
1177 :
1178 61200 : costPaeth += (v < 128) ? v : 256 - v;
1179 : }
1180 :
1181 : #ifdef USE_PAETH_SSE2
1182 : const int iLimitSSE2 =
1183 15300 : RunPaeth(dstBuffer.data() + j * W, nBands, nBPB,
1184 : paethBufferTmp, W, costPaeth);
1185 15300 : int i = iLimitSSE2;
1186 : #else
1187 : int i = 1;
1188 : #endif
1189 84602 : for (; i < W && (costPaeth < costAvg || bForcePaeth);
1190 : ++i)
1191 : {
1192 : {
1193 69302 : const GByte v = PNG_PAETH(
1194 69302 : dstBuffer[0 * nBPB + j * W + i],
1195 69302 : dstBuffer[0 * nBPB + j * W + i - 1],
1196 69302 : dstBuffer[0 * nBPB + (j - 1) * W + i],
1197 69302 : dstBuffer[0 * nBPB + (j - 1) * W + i - 1]);
1198 69302 : paethBuffer[i * nBands + 0] = v;
1199 :
1200 69302 : costPaeth += (v < 128) ? v : 256 - v;
1201 : }
1202 : {
1203 69302 : const GByte v = PNG_PAETH(
1204 69302 : dstBuffer[1 * nBPB + j * W + i],
1205 69302 : dstBuffer[1 * nBPB + j * W + i - 1],
1206 69302 : dstBuffer[1 * nBPB + (j - 1) * W + i],
1207 69302 : dstBuffer[1 * nBPB + (j - 1) * W + i - 1]);
1208 69302 : paethBuffer[i * nBands + 1] = v;
1209 :
1210 69302 : costPaeth += (v < 128) ? v : 256 - v;
1211 : }
1212 : {
1213 69302 : const GByte v = PNG_PAETH(
1214 69302 : dstBuffer[2 * nBPB + j * W + i],
1215 69302 : dstBuffer[2 * nBPB + j * W + i - 1],
1216 69302 : dstBuffer[2 * nBPB + (j - 1) * W + i],
1217 69302 : dstBuffer[2 * nBPB + (j - 1) * W + i - 1]);
1218 69302 : paethBuffer[i * nBands + 2] = v;
1219 :
1220 69302 : costPaeth += (v < 128) ? v : 256 - v;
1221 : }
1222 : {
1223 69302 : const GByte v = PNG_PAETH(
1224 69302 : dstBuffer[3 * nBPB + j * W + i],
1225 69302 : dstBuffer[3 * nBPB + j * W + i - 1],
1226 69302 : dstBuffer[3 * nBPB + (j - 1) * W + i],
1227 69302 : dstBuffer[3 * nBPB + (j - 1) * W + i - 1]);
1228 69302 : paethBuffer[i * nBands + 3] = v;
1229 :
1230 69302 : costPaeth += (v < 128) ? v : 256 - v;
1231 : }
1232 : }
1233 15300 : if (costPaeth < costAvg || bForcePaeth)
1234 : {
1235 4110 : GByte *out = tmpBuffer.data() +
1236 4110 : cpl::fits_on<int>(j * nDstBytesPerRow);
1237 4110 : *out = PNG_FILTER_PAETH;
1238 4110 : ++out;
1239 : #ifdef USE_PAETH_SSE2
1240 4110 : memcpy(out, paethBuffer, nBands);
1241 990510 : for (int iTmp = 1; iTmp < iLimitSSE2; ++iTmp)
1242 : {
1243 986400 : out[nBands * iTmp + 0] =
1244 986400 : paethBufferTmp[0 * W + iTmp];
1245 986400 : out[nBands * iTmp + 1] =
1246 986400 : paethBufferTmp[1 * W + iTmp];
1247 986400 : out[nBands * iTmp + 2] =
1248 986400 : paethBufferTmp[2 * W + iTmp];
1249 986400 : out[nBands * iTmp + 3] =
1250 986400 : paethBufferTmp[3 * W + iTmp];
1251 : }
1252 4110 : memcpy(
1253 4110 : out + iLimitSSE2 * nBands,
1254 4110 : paethBuffer + iLimitSSE2 * nBands,
1255 4110 : cpl::fits_on<int>((W - iLimitSSE2) * nBands));
1256 : #else
1257 : memcpy(out, paethBuffer, nDstBytesPerRow - 1);
1258 : #endif
1259 : }
1260 : }
1261 : }
1262 : else
1263 : {
1264 15616 : for (int i = 1; i < W; ++i)
1265 : {
1266 15555 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 0] =
1267 15555 : PNG_SUB(dstBuffer[0 * nBPB + j * W + i],
1268 15555 : dstBuffer[0 * nBPB + j * W + i - 1]);
1269 15555 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 1] =
1270 15555 : PNG_SUB(dstBuffer[1 * nBPB + j * W + i],
1271 15555 : dstBuffer[1 * nBPB + j * W + i - 1]);
1272 15555 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 2] =
1273 15555 : PNG_SUB(dstBuffer[2 * nBPB + j * W + i],
1274 15555 : dstBuffer[2 * nBPB + j * W + i - 1]);
1275 15555 : tmpBuffer[1 + j * nDstBytesPerRow + i * nBands + 3] =
1276 15555 : PNG_SUB(dstBuffer[3 * nBPB + j * W + i],
1277 15555 : dstBuffer[3 * nBPB + j * W + i - 1]);
1278 : }
1279 : }
1280 : }
1281 : }
1282 703 : size_t nOutSize = 0;
1283 : // Shouldn't happen given the care we have done to dimension dstBuffer
1284 1406 : if (CPLZLibDeflate(tmpBuffer.data(), tmpBufferSize, -1,
1285 703 : dstBuffer.data(), dstBuffer.size(),
1286 1406 : &nOutSize) == nullptr ||
1287 703 : nOutSize > static_cast<size_t>(INT32_MAX))
1288 : {
1289 0 : CPLError(CE_Failure, CPLE_AppDefined,
1290 : "CPLZLibDeflate() failed: too small destination buffer");
1291 0 : return false;
1292 : }
1293 :
1294 703 : VSILFILE *fp = VSIFOpenL(osTmpFilename.c_str(), "wb");
1295 703 : if (!fp)
1296 : {
1297 0 : CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s",
1298 : osTmpFilename.c_str());
1299 0 : return false;
1300 : }
1301 :
1302 : // Cf https://en.wikipedia.org/wiki/PNG#Examples for formatting of
1303 : // IHDR, IDAT and IEND chunks
1304 :
1305 : // PNG Signature
1306 703 : fp->Write("\x89PNG\x0D\x0A\x1A\x0A", 8, 1);
1307 :
1308 : uLong crc;
1309 7030 : const auto WriteAndUpdateCRC_Byte = [fp, &crc](uint8_t nVal)
1310 : {
1311 3515 : fp->Write(&nVal, 1, sizeof(nVal));
1312 3515 : crc = crc32(crc, &nVal, sizeof(nVal));
1313 4218 : };
1314 4218 : const auto WriteAndUpdateCRC_Int = [fp, &crc](int32_t nVal)
1315 : {
1316 1406 : CPL_MSBPTR32(&nVal);
1317 1406 : fp->Write(&nVal, 1, sizeof(nVal));
1318 1406 : crc = crc32(crc, reinterpret_cast<const Bytef *>(&nVal),
1319 : sizeof(nVal));
1320 2109 : };
1321 :
1322 : // IHDR chunk
1323 703 : uint32_t nIHDRSize = 13;
1324 703 : CPL_MSBPTR32(&nIHDRSize);
1325 703 : fp->Write(&nIHDRSize, 1, sizeof(nIHDRSize));
1326 703 : crc = crc32(0, reinterpret_cast<const Bytef *>("IHDR"), 4);
1327 703 : fp->Write("IHDR", 1, 4);
1328 703 : WriteAndUpdateCRC_Int(W);
1329 703 : WriteAndUpdateCRC_Int(H);
1330 703 : WriteAndUpdateCRC_Byte(8); // Number of bits per pixel
1331 703 : const uint8_t nColorType = nBands == 1 ? 0
1332 : : nBands == 2 ? 4
1333 : : nBands == 3 ? 2
1334 : : 6;
1335 703 : WriteAndUpdateCRC_Byte(nColorType);
1336 703 : WriteAndUpdateCRC_Byte(0); // Compression method
1337 703 : WriteAndUpdateCRC_Byte(0); // Filter method
1338 703 : WriteAndUpdateCRC_Byte(0); // Interlacing=off
1339 : {
1340 703 : uint32_t nCrc32 = static_cast<uint32_t>(crc);
1341 703 : CPL_MSBPTR32(&nCrc32);
1342 703 : fp->Write(&nCrc32, 1, sizeof(nCrc32));
1343 : }
1344 :
1345 : // IDAT chunk
1346 703 : uint32_t nIDATSize = static_cast<uint32_t>(nOutSize);
1347 703 : CPL_MSBPTR32(&nIDATSize);
1348 703 : fp->Write(&nIDATSize, 1, sizeof(nIDATSize));
1349 703 : crc = crc32(0, reinterpret_cast<const Bytef *>("IDAT"), 4);
1350 703 : fp->Write("IDAT", 1, 4);
1351 703 : crc = crc32(crc, dstBuffer.data(), static_cast<uint32_t>(nOutSize));
1352 703 : fp->Write(dstBuffer.data(), 1, nOutSize);
1353 : {
1354 703 : uint32_t nCrc32 = static_cast<uint32_t>(crc);
1355 703 : CPL_MSBPTR32(&nCrc32);
1356 703 : fp->Write(&nCrc32, 1, sizeof(nCrc32));
1357 : }
1358 :
1359 : // IEND chunk
1360 703 : fp->Write("\x00\x00\x00\x00IEND\xAE\x42\x60\x82", 12, 1);
1361 :
1362 : bool bRet =
1363 703 : fp->Tell() == 8 + 4 + 4 + 13 + 4 + 4 + 4 + nOutSize + 4 + 12;
1364 1406 : bRet = VSIFCloseL(fp) == 0 && bRet &&
1365 703 : VSIRename(osTmpFilename.c_str(), osFilename.c_str()) == 0;
1366 703 : if (!bRet)
1367 0 : VSIUnlink(osTmpFilename.c_str());
1368 :
1369 703 : return bRet;
1370 : }
1371 :
1372 : auto memDS = std::unique_ptr<GDALDataset>(
1373 29 : MEMDataset::Create("", tileMatrix.mTileWidth, tileMatrix.mTileHeight, 0,
1374 58 : eWorkingDataType, nullptr));
1375 82 : for (int i = 0; i < nBands; ++i)
1376 : {
1377 53 : char szBuffer[32] = {'\0'};
1378 106 : int nRet = CPLPrintPointer(
1379 53 : szBuffer, dstBuffer.data() + i * nBytesPerBand, sizeof(szBuffer));
1380 53 : szBuffer[nRet] = 0;
1381 :
1382 53 : char szOption[64] = {'\0'};
1383 53 : snprintf(szOption, sizeof(szOption), "DATAPOINTER=%s", szBuffer);
1384 :
1385 53 : char *apszOptions[] = {szOption, nullptr};
1386 :
1387 53 : memDS->AddBand(eWorkingDataType, apszOptions);
1388 53 : auto poDstBand = memDS->GetRasterBand(i + 1);
1389 53 : if (i + 1 <= poSrcDS->GetRasterCount())
1390 47 : poDstBand->SetColorInterpretation(
1391 47 : poSrcDS->GetRasterBand(i + 1)->GetColorInterpretation());
1392 : else
1393 6 : poDstBand->SetColorInterpretation(GCI_AlphaBand);
1394 53 : if (pdfDstNoData)
1395 9 : poDstBand->SetNoDataValue(*pdfDstNoData);
1396 53 : if (i == 0 && poColorTable)
1397 1 : poDstBand->SetColorTable(
1398 1 : const_cast<GDALColorTable *>(poColorTable));
1399 : }
1400 58 : const CPLStringList aosMD(metadata);
1401 37 : for (const auto [key, value] : cpl::IterateNameValue(aosMD))
1402 : {
1403 8 : memDS->SetMetadataItem(key, value);
1404 : }
1405 :
1406 29 : GDALGeoTransform gt;
1407 29 : gt.xorig =
1408 29 : tileMatrix.mTopLeftX + iX * tileMatrix.mResX * tileMatrix.mTileWidth;
1409 29 : gt.xscale = tileMatrix.mResX;
1410 29 : gt.xrot = 0;
1411 29 : gt.yorig =
1412 29 : tileMatrix.mTopLeftY - iY * tileMatrix.mResY * tileMatrix.mTileHeight;
1413 29 : gt.yrot = 0;
1414 29 : gt.yscale = -tileMatrix.mResY;
1415 29 : memDS->SetGeoTransform(gt);
1416 :
1417 29 : memDS->SetSpatialRef(&oSRS_TMS);
1418 :
1419 : CPLConfigOptionSetter oSetter("GDAL_PAM_ENABLED", bAuxXML ? "YES" : "NO",
1420 58 : false);
1421 : CPLConfigOptionSetter oSetter2("GDAL_DISABLE_READDIR_ON_OPEN", "YES",
1422 58 : false);
1423 :
1424 29 : std::unique_ptr<CPLConfigOptionSetter> poSetter;
1425 : // No need to reopen the dataset at end of CreateCopy() (for PNG
1426 : // and JPEG) if we don't need to generate .aux.xml
1427 29 : if (!bAuxXML)
1428 25 : poSetter = std::make_unique<CPLConfigOptionSetter>(
1429 50 : "GDAL_OPEN_AFTER_COPY", "NO", false);
1430 29 : CPL_IGNORE_RET_VAL(poSetter);
1431 :
1432 58 : CPLStringList aosCreationOptions(creationOptions);
1433 29 : if (bSupportsCreateOnlyVisibleAtCloseTime)
1434 : aosCreationOptions.SetNameValue("@CREATE_ONLY_VISIBLE_AT_CLOSE_TIME",
1435 27 : "YES");
1436 :
1437 : std::unique_ptr<GDALDataset> poOutDS(
1438 : m_poDstDriver->CreateCopy(osTmpFilename.c_str(), memDS.get(), false,
1439 29 : aosCreationOptions.List(), nullptr, nullptr));
1440 29 : bool bRet = poOutDS && poOutDS->Close() == CE_None;
1441 29 : poOutDS.reset();
1442 29 : if (bRet)
1443 : {
1444 27 : if (!bSupportsCreateOnlyVisibleAtCloseTime)
1445 : {
1446 0 : bRet = VSIRename(osTmpFilename.c_str(), osFilename.c_str()) == 0;
1447 0 : if (bAuxXML)
1448 : {
1449 0 : VSIRename((osTmpFilename + ".aux.xml").c_str(),
1450 0 : (osFilename + ".aux.xml").c_str());
1451 : }
1452 : }
1453 : }
1454 : else
1455 : {
1456 2 : VSIUnlink(osTmpFilename.c_str());
1457 : }
1458 29 : return bRet;
1459 : }
1460 :
1461 : /************************************************************************/
1462 : /* GenerateOverviewTile() */
1463 : /************************************************************************/
1464 :
1465 : static bool
1466 236 : GenerateOverviewTile(GDALDataset &oSrcDS, GDALDriver *m_poDstDriver,
1467 : const std::string &outputFormat, const char *pszExtension,
1468 : CSLConstList creationOptions,
1469 : CSLConstList papszWarpOptions,
1470 : const std::string &resampling,
1471 : const gdal::TileMatrixSet::TileMatrix &tileMatrix,
1472 : const std::string &outputDirectory, int nZoomLevel, int iX,
1473 : int iY, const std::string &convention, bool bSkipBlank,
1474 : bool bUserAskedForAlpha, bool bAuxXML, bool bResume)
1475 : {
1476 : const std::string osDirZ = CPLFormFilenameSafe(
1477 472 : outputDirectory.c_str(), CPLSPrintf("%d", nZoomLevel), nullptr);
1478 : const std::string osDirX =
1479 472 : CPLFormFilenameSafe(osDirZ.c_str(), CPLSPrintf("%d", iX), nullptr);
1480 :
1481 236 : const int iFileY = GetFileY(iY, tileMatrix, convention);
1482 : const std::string osFilename = CPLFormFilenameSafe(
1483 472 : osDirX.c_str(), CPLSPrintf("%d", iFileY), pszExtension);
1484 :
1485 236 : if (bResume)
1486 : {
1487 : VSIStatBufL sStat;
1488 2 : if (VSIStatL(osFilename.c_str(), &sStat) == 0)
1489 1 : return true;
1490 : }
1491 :
1492 235 : VSIMkdir(osDirZ.c_str(), 0755);
1493 235 : VSIMkdir(osDirX.c_str(), 0755);
1494 :
1495 : const bool bSupportsCreateOnlyVisibleAtCloseTime =
1496 470 : m_poDstDriver->GetMetadataItem(
1497 235 : GDAL_DCAP_CREATE_ONLY_VISIBLE_AT_CLOSE_TIME) != nullptr;
1498 :
1499 470 : CPLStringList aosOptions;
1500 :
1501 235 : aosOptions.AddString("-of");
1502 235 : aosOptions.AddString(outputFormat.c_str());
1503 :
1504 264 : for (const char *pszCO : cpl::Iterate(creationOptions))
1505 : {
1506 29 : aosOptions.AddString("-co");
1507 29 : aosOptions.AddString(pszCO);
1508 : }
1509 235 : if (bSupportsCreateOnlyVisibleAtCloseTime)
1510 : {
1511 235 : aosOptions.AddString("-co");
1512 235 : aosOptions.AddString("@CREATE_ONLY_VISIBLE_AT_CLOSE_TIME=YES");
1513 : }
1514 :
1515 : CPLConfigOptionSetter oSetter("GDAL_PAM_ENABLED", bAuxXML ? "YES" : "NO",
1516 470 : false);
1517 : CPLConfigOptionSetter oSetter2("GDAL_DISABLE_READDIR_ON_OPEN", "YES",
1518 470 : false);
1519 :
1520 235 : aosOptions.AddString("-r");
1521 235 : aosOptions.AddString(resampling.c_str());
1522 :
1523 235 : std::unique_ptr<GDALDataset> poOutDS;
1524 235 : const double dfMinX =
1525 235 : tileMatrix.mTopLeftX + iX * tileMatrix.mResX * tileMatrix.mTileWidth;
1526 235 : const double dfMaxY =
1527 235 : tileMatrix.mTopLeftY - iY * tileMatrix.mResY * tileMatrix.mTileHeight;
1528 235 : const double dfMaxX = dfMinX + tileMatrix.mResX * tileMatrix.mTileWidth;
1529 235 : const double dfMinY = dfMaxY - tileMatrix.mResY * tileMatrix.mTileHeight;
1530 :
1531 : const bool resamplingCompatibleOfTranslate =
1532 688 : papszWarpOptions == nullptr &&
1533 642 : (resampling == "nearest" || resampling == "average" ||
1534 381 : resampling == "bilinear" || resampling == "cubic" ||
1535 9 : resampling == "cubicspline" || resampling == "lanczos" ||
1536 3 : resampling == "mode");
1537 :
1538 : const std::string osTmpFilename = bSupportsCreateOnlyVisibleAtCloseTime
1539 : ? osFilename
1540 470 : : osFilename + ".tmp." + pszExtension;
1541 :
1542 235 : if (resamplingCompatibleOfTranslate)
1543 : {
1544 226 : GDALGeoTransform upperGT;
1545 226 : oSrcDS.GetGeoTransform(upperGT);
1546 226 : const double dfMinXUpper = upperGT[0];
1547 : const double dfMaxXUpper =
1548 226 : dfMinXUpper + upperGT[1] * oSrcDS.GetRasterXSize();
1549 226 : const double dfMaxYUpper = upperGT[3];
1550 : const double dfMinYUpper =
1551 226 : dfMaxYUpper + upperGT[5] * oSrcDS.GetRasterYSize();
1552 226 : if (dfMinX >= dfMinXUpper && dfMaxX <= dfMaxXUpper &&
1553 189 : dfMinY >= dfMinYUpper && dfMaxY <= dfMaxYUpper)
1554 : {
1555 : // If the overview tile is fully within the extent of the
1556 : // upper zoom level, we can use GDALDataset::RasterIO() directly.
1557 :
1558 188 : const auto eDT = oSrcDS.GetRasterBand(1)->GetRasterDataType();
1559 : const size_t nBytesPerBand =
1560 188 : static_cast<size_t>(tileMatrix.mTileWidth) *
1561 188 : tileMatrix.mTileHeight * GDALGetDataTypeSizeBytes(eDT);
1562 : std::vector<GByte> dstBuffer(nBytesPerBand *
1563 188 : oSrcDS.GetRasterCount());
1564 :
1565 188 : const double dfXOff = (dfMinX - dfMinXUpper) / upperGT[1];
1566 188 : const double dfYOff = (dfMaxYUpper - dfMaxY) / -upperGT[5];
1567 188 : const double dfXSize = (dfMaxX - dfMinX) / upperGT[1];
1568 188 : const double dfYSize = (dfMaxY - dfMinY) / -upperGT[5];
1569 : GDALRasterIOExtraArg sExtraArg;
1570 188 : INIT_RASTERIO_EXTRA_ARG(sExtraArg);
1571 188 : CPL_IGNORE_RET_VAL(sExtraArg.eResampleAlg);
1572 188 : sExtraArg.eResampleAlg =
1573 188 : GDALRasterIOGetResampleAlg(resampling.c_str());
1574 188 : sExtraArg.dfXOff = dfXOff;
1575 188 : sExtraArg.dfYOff = dfYOff;
1576 188 : sExtraArg.dfXSize = dfXSize;
1577 188 : sExtraArg.dfYSize = dfYSize;
1578 188 : sExtraArg.bFloatingPointWindowValidity =
1579 188 : sExtraArg.eResampleAlg != GRIORA_NearestNeighbour;
1580 188 : constexpr double EPSILON = 1e-3;
1581 188 : if (oSrcDS.RasterIO(GF_Read, static_cast<int>(dfXOff + EPSILON),
1582 188 : static_cast<int>(dfYOff + EPSILON),
1583 188 : static_cast<int>(dfXSize + 0.5),
1584 188 : static_cast<int>(dfYSize + 0.5),
1585 188 : dstBuffer.data(), tileMatrix.mTileWidth,
1586 188 : tileMatrix.mTileHeight, eDT,
1587 : oSrcDS.GetRasterCount(), nullptr, 0, 0, 0,
1588 188 : &sExtraArg) == CE_None)
1589 : {
1590 187 : int nDstBands = oSrcDS.GetRasterCount();
1591 : const bool bDstHasAlpha =
1592 187 : oSrcDS.GetRasterBand(nDstBands)->GetColorInterpretation() ==
1593 187 : GCI_AlphaBand;
1594 187 : if (bDstHasAlpha && bSkipBlank)
1595 : {
1596 13 : bool bBlank = true;
1597 110074 : for (size_t i = 0; i < nBytesPerBand && bBlank; ++i)
1598 : {
1599 110061 : bBlank =
1600 110061 : (dstBuffer[(nDstBands - 1) * nBytesPerBand + i] ==
1601 : 0);
1602 : }
1603 13 : if (bBlank)
1604 1 : return true;
1605 12 : bSkipBlank = false;
1606 : }
1607 186 : if (bDstHasAlpha && !bUserAskedForAlpha)
1608 : {
1609 185 : bool bAllOpaque = true;
1610 11600200 : for (size_t i = 0; i < nBytesPerBand && bAllOpaque; ++i)
1611 : {
1612 11600100 : bAllOpaque =
1613 11600100 : (dstBuffer[(nDstBands - 1) * nBytesPerBand + i] ==
1614 : 255);
1615 : }
1616 185 : if (bAllOpaque)
1617 177 : nDstBands--;
1618 : }
1619 :
1620 372 : auto memDS = std::unique_ptr<GDALDataset>(MEMDataset::Create(
1621 186 : "", tileMatrix.mTileWidth, tileMatrix.mTileHeight, 0, eDT,
1622 372 : nullptr));
1623 748 : for (int i = 0; i < nDstBands; ++i)
1624 : {
1625 562 : char szBuffer[32] = {'\0'};
1626 1124 : int nRet = CPLPrintPointer(
1627 562 : szBuffer, dstBuffer.data() + i * nBytesPerBand,
1628 : sizeof(szBuffer));
1629 562 : szBuffer[nRet] = 0;
1630 :
1631 562 : char szOption[64] = {'\0'};
1632 562 : snprintf(szOption, sizeof(szOption), "DATAPOINTER=%s",
1633 : szBuffer);
1634 :
1635 562 : char *apszOptions[] = {szOption, nullptr};
1636 :
1637 562 : memDS->AddBand(eDT, apszOptions);
1638 562 : auto poSrcBand = oSrcDS.GetRasterBand(i + 1);
1639 562 : auto poDstBand = memDS->GetRasterBand(i + 1);
1640 562 : poDstBand->SetColorInterpretation(
1641 562 : poSrcBand->GetColorInterpretation());
1642 562 : int bHasNoData = false;
1643 : const double dfNoData =
1644 562 : poSrcBand->GetNoDataValue(&bHasNoData);
1645 562 : if (bHasNoData)
1646 0 : poDstBand->SetNoDataValue(dfNoData);
1647 562 : if (auto poCT = poSrcBand->GetColorTable())
1648 0 : poDstBand->SetColorTable(poCT);
1649 : }
1650 186 : memDS->SetMetadata(oSrcDS.GetMetadata());
1651 372 : memDS->SetGeoTransform(GDALGeoTransform(
1652 186 : dfMinX, tileMatrix.mResX, 0, dfMaxY, 0, -tileMatrix.mResY));
1653 :
1654 186 : memDS->SetSpatialRef(oSrcDS.GetSpatialRef());
1655 :
1656 186 : std::unique_ptr<CPLConfigOptionSetter> poSetter;
1657 : // No need to reopen the dataset at end of CreateCopy() (for PNG
1658 : // and JPEG) if we don't need to generate .aux.xml
1659 186 : if (!bAuxXML)
1660 186 : poSetter = std::make_unique<CPLConfigOptionSetter>(
1661 372 : "GDAL_OPEN_AFTER_COPY", "NO", false);
1662 186 : CPL_IGNORE_RET_VAL(poSetter);
1663 :
1664 372 : CPLStringList aosCreationOptions(creationOptions);
1665 186 : if (bSupportsCreateOnlyVisibleAtCloseTime)
1666 : aosCreationOptions.SetNameValue(
1667 186 : "@CREATE_ONLY_VISIBLE_AT_CLOSE_TIME", "YES");
1668 186 : poOutDS.reset(m_poDstDriver->CreateCopy(
1669 : osTmpFilename.c_str(), memDS.get(), false,
1670 186 : aosCreationOptions.List(), nullptr, nullptr));
1671 187 : }
1672 : }
1673 : else
1674 : {
1675 : // If the overview tile is not fully within the extent of the
1676 : // upper zoom level, use GDALTranslate() to use VRT padding
1677 :
1678 38 : aosOptions.AddString("-q");
1679 :
1680 38 : aosOptions.AddString("-projwin");
1681 38 : aosOptions.AddString(CPLSPrintf("%.17g", dfMinX));
1682 38 : aosOptions.AddString(CPLSPrintf("%.17g", dfMaxY));
1683 38 : aosOptions.AddString(CPLSPrintf("%.17g", dfMaxX));
1684 38 : aosOptions.AddString(CPLSPrintf("%.17g", dfMinY));
1685 :
1686 38 : aosOptions.AddString("-outsize");
1687 38 : aosOptions.AddString(CPLSPrintf("%d", tileMatrix.mTileWidth));
1688 38 : aosOptions.AddString(CPLSPrintf("%d", tileMatrix.mTileHeight));
1689 :
1690 : GDALTranslateOptions *psOptions =
1691 38 : GDALTranslateOptionsNew(aosOptions.List(), nullptr);
1692 38 : poOutDS.reset(GDALDataset::FromHandle(GDALTranslate(
1693 : osTmpFilename.c_str(), GDALDataset::ToHandle(&oSrcDS),
1694 : psOptions, nullptr)));
1695 38 : GDALTranslateOptionsFree(psOptions);
1696 : }
1697 : }
1698 : else
1699 : {
1700 9 : aosOptions.AddString("-te");
1701 9 : aosOptions.AddString(CPLSPrintf("%.17g", dfMinX));
1702 9 : aosOptions.AddString(CPLSPrintf("%.17g", dfMinY));
1703 9 : aosOptions.AddString(CPLSPrintf("%.17g", dfMaxX));
1704 9 : aosOptions.AddString(CPLSPrintf("%.17g", dfMaxY));
1705 :
1706 9 : aosOptions.AddString("-ts");
1707 9 : aosOptions.AddString(CPLSPrintf("%d", tileMatrix.mTileWidth));
1708 9 : aosOptions.AddString(CPLSPrintf("%d", tileMatrix.mTileHeight));
1709 :
1710 19 : for (int i = 0; papszWarpOptions && papszWarpOptions[i]; ++i)
1711 : {
1712 10 : aosOptions.AddString("-wo");
1713 10 : aosOptions.AddString(papszWarpOptions[i]);
1714 : }
1715 :
1716 : GDALWarpAppOptions *psOptions =
1717 9 : GDALWarpAppOptionsNew(aosOptions.List(), nullptr);
1718 9 : GDALDatasetH hSrcDS = GDALDataset::ToHandle(&oSrcDS);
1719 9 : poOutDS.reset(GDALDataset::FromHandle(GDALWarp(
1720 : osTmpFilename.c_str(), nullptr, 1, &hSrcDS, psOptions, nullptr)));
1721 9 : GDALWarpAppOptionsFree(psOptions);
1722 : }
1723 :
1724 234 : bool bRet = poOutDS != nullptr;
1725 234 : if (bRet && bSkipBlank)
1726 : {
1727 36 : auto poLastBand = poOutDS->GetRasterBand(poOutDS->GetRasterCount());
1728 36 : if (poLastBand->GetColorInterpretation() == GCI_AlphaBand)
1729 : {
1730 : std::vector<GByte> buffer(
1731 24 : static_cast<size_t>(tileMatrix.mTileWidth) *
1732 24 : tileMatrix.mTileHeight *
1733 24 : GDALGetDataTypeSizeBytes(poLastBand->GetRasterDataType()));
1734 48 : CPL_IGNORE_RET_VAL(poLastBand->RasterIO(
1735 24 : GF_Read, 0, 0, tileMatrix.mTileWidth, tileMatrix.mTileHeight,
1736 24 : buffer.data(), tileMatrix.mTileWidth, tileMatrix.mTileHeight,
1737 : poLastBand->GetRasterDataType(), 0, 0, nullptr));
1738 24 : bool bBlank = true;
1739 984132 : for (size_t i = 0; i < buffer.size() && bBlank; ++i)
1740 : {
1741 984108 : bBlank = (buffer[i] == 0);
1742 : }
1743 24 : if (bBlank)
1744 : {
1745 11 : poOutDS.reset();
1746 11 : VSIUnlink(osTmpFilename.c_str());
1747 11 : if (bAuxXML)
1748 0 : VSIUnlink((osTmpFilename + ".aux.xml").c_str());
1749 11 : return true;
1750 : }
1751 : }
1752 : }
1753 223 : bRet = bRet && poOutDS->Close() == CE_None;
1754 223 : poOutDS.reset();
1755 223 : if (bRet)
1756 : {
1757 222 : if (!bSupportsCreateOnlyVisibleAtCloseTime)
1758 : {
1759 0 : bRet = VSIRename(osTmpFilename.c_str(), osFilename.c_str()) == 0;
1760 0 : if (bAuxXML)
1761 : {
1762 0 : VSIRename((osTmpFilename + ".aux.xml").c_str(),
1763 0 : (osFilename + ".aux.xml").c_str());
1764 : }
1765 : }
1766 : }
1767 : else
1768 : {
1769 1 : VSIUnlink(osTmpFilename.c_str());
1770 : }
1771 223 : return bRet;
1772 : }
1773 :
1774 : namespace
1775 : {
1776 :
1777 : /************************************************************************/
1778 : /* FakeMaxZoomRasterBand */
1779 : /************************************************************************/
1780 :
1781 : class FakeMaxZoomRasterBand : public GDALRasterBand
1782 : {
1783 : void *m_pDstBuffer = nullptr;
1784 : CPL_DISALLOW_COPY_ASSIGN(FakeMaxZoomRasterBand)
1785 :
1786 : public:
1787 691 : FakeMaxZoomRasterBand(int nBandIn, int nWidth, int nHeight,
1788 : int nBlockXSizeIn, int nBlockYSizeIn,
1789 : GDALDataType eDT, void *pDstBuffer)
1790 691 : : m_pDstBuffer(pDstBuffer)
1791 : {
1792 691 : nBand = nBandIn;
1793 691 : nRasterXSize = nWidth;
1794 691 : nRasterYSize = nHeight;
1795 691 : nBlockXSize = nBlockXSizeIn;
1796 691 : nBlockYSize = nBlockYSizeIn;
1797 691 : eDataType = eDT;
1798 691 : }
1799 :
1800 0 : CPLErr IReadBlock(int, int, void *) override
1801 : {
1802 0 : CPLAssert(false);
1803 : return CE_Failure;
1804 : }
1805 :
1806 : #ifdef DEBUG
1807 0 : CPLErr IWriteBlock(int, int, void *) override
1808 : {
1809 0 : CPLAssert(false);
1810 : return CE_Failure;
1811 : }
1812 : #endif
1813 :
1814 1512 : CPLErr IRasterIO(GDALRWFlag eRWFlag, [[maybe_unused]] int nXOff,
1815 : [[maybe_unused]] int nYOff, [[maybe_unused]] int nXSize,
1816 : [[maybe_unused]] int nYSize, void *pData,
1817 : [[maybe_unused]] int nBufXSize,
1818 : [[maybe_unused]] int nBufYSize, GDALDataType eBufType,
1819 : GSpacing nPixelSpace, [[maybe_unused]] GSpacing nLineSpace,
1820 : GDALRasterIOExtraArg *) override
1821 : {
1822 : // For sake of implementation simplicity, check various assumptions of
1823 : // how GDALAlphaMask code does I/O
1824 1512 : CPLAssert((nXOff % nBlockXSize) == 0);
1825 1512 : CPLAssert((nYOff % nBlockYSize) == 0);
1826 1512 : CPLAssert(nXSize == nBufXSize);
1827 1512 : CPLAssert(nXSize == nBlockXSize);
1828 1512 : CPLAssert(nYSize == nBufYSize);
1829 1512 : CPLAssert(nYSize == nBlockYSize);
1830 1512 : CPLAssert(nLineSpace == nBlockXSize * nPixelSpace);
1831 1512 : CPLAssert(
1832 : nBand ==
1833 : poDS->GetRasterCount()); // only alpha band is accessed this way
1834 1512 : if (eRWFlag == GF_Read)
1835 : {
1836 756 : double dfZero = 0;
1837 756 : GDALCopyWords64(&dfZero, GDT_Float64, 0, pData, eBufType,
1838 : static_cast<int>(nPixelSpace),
1839 756 : static_cast<size_t>(nBlockXSize) * nBlockYSize);
1840 : }
1841 : else
1842 : {
1843 756 : GDALCopyWords64(pData, eBufType, static_cast<int>(nPixelSpace),
1844 : m_pDstBuffer, eDataType,
1845 : GDALGetDataTypeSizeBytes(eDataType),
1846 756 : static_cast<size_t>(nBlockXSize) * nBlockYSize);
1847 : }
1848 1512 : return CE_None;
1849 : }
1850 : };
1851 :
1852 : /************************************************************************/
1853 : /* FakeMaxZoomDataset */
1854 : /************************************************************************/
1855 :
1856 : // This class is used to create a fake output dataset for GDALWarpOperation.
1857 : // In particular we need to implement GDALRasterBand::IRasterIO(GF_Write, ...)
1858 : // to catch writes (of one single tile) to the alpha band and redirect them
1859 : // to the dstBuffer passed to FakeMaxZoomDataset constructor.
1860 :
1861 : class FakeMaxZoomDataset : public GDALDataset
1862 : {
1863 : const int m_nBlockXSize;
1864 : const int m_nBlockYSize;
1865 : const OGRSpatialReference m_oSRS;
1866 : const GDALGeoTransform m_gt{};
1867 :
1868 : public:
1869 196 : FakeMaxZoomDataset(int nWidth, int nHeight, int nBandsIn, int nBlockXSize,
1870 : int nBlockYSize, GDALDataType eDT,
1871 : const GDALGeoTransform >,
1872 : const OGRSpatialReference &oSRS,
1873 : std::vector<GByte> &dstBuffer)
1874 196 : : m_nBlockXSize(nBlockXSize), m_nBlockYSize(nBlockYSize), m_oSRS(oSRS),
1875 196 : m_gt(gt)
1876 : {
1877 196 : eAccess = GA_Update;
1878 196 : nRasterXSize = nWidth;
1879 196 : nRasterYSize = nHeight;
1880 887 : for (int i = 1; i <= nBandsIn; ++i)
1881 : {
1882 691 : SetBand(i,
1883 : new FakeMaxZoomRasterBand(
1884 : i, nWidth, nHeight, nBlockXSize, nBlockYSize, eDT,
1885 691 : dstBuffer.data() + static_cast<size_t>(i - 1) *
1886 1382 : nBlockXSize * nBlockYSize *
1887 691 : GDALGetDataTypeSizeBytes(eDT)));
1888 : }
1889 196 : }
1890 :
1891 385 : const OGRSpatialReference *GetSpatialRef() const override
1892 : {
1893 385 : return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
1894 : }
1895 :
1896 172 : CPLErr GetGeoTransform(GDALGeoTransform >) const override
1897 : {
1898 172 : gt = m_gt;
1899 172 : return CE_None;
1900 : }
1901 :
1902 : using GDALDataset::Clone;
1903 :
1904 : std::unique_ptr<FakeMaxZoomDataset>
1905 24 : Clone(std::vector<GByte> &dstBuffer) const
1906 : {
1907 : return std::make_unique<FakeMaxZoomDataset>(
1908 24 : nRasterXSize, nRasterYSize, nBands, m_nBlockXSize, m_nBlockYSize,
1909 48 : GetRasterBand(1)->GetRasterDataType(), m_gt, m_oSRS, dstBuffer);
1910 : }
1911 : };
1912 :
1913 : /************************************************************************/
1914 : /* MosaicRasterBand */
1915 : /************************************************************************/
1916 :
1917 : class MosaicRasterBand : public GDALRasterBand
1918 : {
1919 : const int m_tileMinX;
1920 : const int m_tileMinY;
1921 : const GDALColorInterp m_eColorInterp;
1922 : const gdal::TileMatrixSet::TileMatrix m_oTM;
1923 : const std::string m_convention;
1924 : const std::string m_directory;
1925 : const std::string m_extension;
1926 : const bool m_hasNoData;
1927 : const double m_noData;
1928 : std::unique_ptr<GDALColorTable> m_poColorTable{};
1929 :
1930 : public:
1931 430 : MosaicRasterBand(GDALDataset *poDSIn, int nBandIn, int nWidth, int nHeight,
1932 : int nBlockXSizeIn, int nBlockYSizeIn, GDALDataType eDT,
1933 : GDALColorInterp eColorInterp, int nTileMinX, int nTileMinY,
1934 : const gdal::TileMatrixSet::TileMatrix &oTM,
1935 : const std::string &convention,
1936 : const std::string &directory, const std::string &extension,
1937 : const double *pdfDstNoData,
1938 : const GDALColorTable *poColorTable)
1939 430 : : m_tileMinX(nTileMinX), m_tileMinY(nTileMinY),
1940 : m_eColorInterp(eColorInterp), m_oTM(oTM), m_convention(convention),
1941 : m_directory(directory), m_extension(extension),
1942 430 : m_hasNoData(pdfDstNoData != nullptr),
1943 430 : m_noData(pdfDstNoData ? *pdfDstNoData : 0),
1944 860 : m_poColorTable(poColorTable ? poColorTable->Clone() : nullptr)
1945 : {
1946 430 : poDS = poDSIn;
1947 430 : nBand = nBandIn;
1948 430 : nRasterXSize = nWidth;
1949 430 : nRasterYSize = nHeight;
1950 430 : nBlockXSize = nBlockXSizeIn;
1951 430 : nBlockYSize = nBlockYSizeIn;
1952 430 : eDataType = eDT;
1953 430 : }
1954 :
1955 : CPLErr IReadBlock(int nXBlock, int nYBlock, void *pData) override;
1956 :
1957 8391 : GDALColorTable *GetColorTable() override
1958 : {
1959 8391 : return m_poColorTable.get();
1960 : }
1961 :
1962 1162 : GDALColorInterp GetColorInterpretation() override
1963 : {
1964 1162 : return m_eColorInterp;
1965 : }
1966 :
1967 17023 : double GetNoDataValue(int *pbHasNoData) override
1968 : {
1969 17023 : if (pbHasNoData)
1970 17014 : *pbHasNoData = m_hasNoData;
1971 17023 : return m_noData;
1972 : }
1973 : };
1974 :
1975 : /************************************************************************/
1976 : /* MosaicDataset */
1977 : /************************************************************************/
1978 :
1979 : // This class is to expose the tiles of a given level as a mosaic that
1980 : // can be used as a source to generate the immediately below zoom level.
1981 :
1982 : class MosaicDataset : public GDALDataset
1983 : {
1984 : friend class MosaicRasterBand;
1985 :
1986 : const std::string m_directory;
1987 : const std::string m_extension;
1988 : const std::string m_format;
1989 : const std::vector<GDALColorInterp> m_aeColorInterp;
1990 : const gdal::TileMatrixSet::TileMatrix &m_oTM;
1991 : const OGRSpatialReference m_oSRS;
1992 : const int m_nTileMinX;
1993 : const int m_nTileMinY;
1994 : const int m_nTileMaxX;
1995 : const int m_nTileMaxY;
1996 : const std::string m_convention;
1997 : const GDALDataType m_eDT;
1998 : const double *const m_pdfDstNoData;
1999 : const std::vector<std::string> &m_metadata;
2000 : const GDALColorTable *const m_poCT;
2001 :
2002 : GDALGeoTransform m_gt{};
2003 : const int m_nMaxCacheTileSize;
2004 : lru11::Cache<std::string, std::shared_ptr<GDALDataset>> m_oCacheTile;
2005 :
2006 : CPL_DISALLOW_COPY_ASSIGN(MosaicDataset)
2007 :
2008 : public:
2009 122 : MosaicDataset(const std::string &directory, const std::string &extension,
2010 : const std::string &format,
2011 : const std::vector<GDALColorInterp> &aeColorInterp,
2012 : const gdal::TileMatrixSet::TileMatrix &oTM,
2013 : const OGRSpatialReference &oSRS, int nTileMinX, int nTileMinY,
2014 : int nTileMaxX, int nTileMaxY, const std::string &convention,
2015 : int nBandsIn, GDALDataType eDT, const double *pdfDstNoData,
2016 : const std::vector<std::string> &metadata,
2017 : const GDALColorTable *poCT, int maxCacheTileSize)
2018 122 : : m_directory(directory), m_extension(extension), m_format(format),
2019 : m_aeColorInterp(aeColorInterp), m_oTM(oTM), m_oSRS(oSRS),
2020 : m_nTileMinX(nTileMinX), m_nTileMinY(nTileMinY),
2021 : m_nTileMaxX(nTileMaxX), m_nTileMaxY(nTileMaxY),
2022 : m_convention(convention), m_eDT(eDT), m_pdfDstNoData(pdfDstNoData),
2023 : m_metadata(metadata), m_poCT(poCT),
2024 : m_nMaxCacheTileSize(maxCacheTileSize),
2025 122 : m_oCacheTile(/* max_size = */ maxCacheTileSize, /* elasticity = */ 0)
2026 : {
2027 122 : nRasterXSize = (nTileMaxX - nTileMinX + 1) * oTM.mTileWidth;
2028 122 : nRasterYSize = (nTileMaxY - nTileMinY + 1) * oTM.mTileHeight;
2029 122 : m_gt.xorig = oTM.mTopLeftX + nTileMinX * oTM.mResX * oTM.mTileWidth;
2030 122 : m_gt.xscale = oTM.mResX;
2031 122 : m_gt.xrot = 0;
2032 122 : m_gt.yorig = oTM.mTopLeftY - nTileMinY * oTM.mResY * oTM.mTileHeight;
2033 122 : m_gt.yrot = 0;
2034 122 : m_gt.yscale = -oTM.mResY;
2035 552 : for (int i = 1; i <= nBandsIn; ++i)
2036 : {
2037 : const GDALColorInterp eColorInterp =
2038 430 : (i <= static_cast<int>(m_aeColorInterp.size()))
2039 430 : ? m_aeColorInterp[i - 1]
2040 430 : : GCI_AlphaBand;
2041 430 : SetBand(i, new MosaicRasterBand(
2042 430 : this, i, nRasterXSize, nRasterYSize, oTM.mTileWidth,
2043 430 : oTM.mTileHeight, eDT, eColorInterp, nTileMinX,
2044 : nTileMinY, oTM, convention, directory, extension,
2045 430 : pdfDstNoData, poCT));
2046 : }
2047 122 : SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE");
2048 244 : const CPLStringList aosMD(metadata);
2049 139 : for (const auto [key, value] : cpl::IterateNameValue(aosMD))
2050 : {
2051 17 : SetMetadataItem(key, value);
2052 : }
2053 122 : }
2054 :
2055 269 : const OGRSpatialReference *GetSpatialRef() const override
2056 : {
2057 269 : return m_oSRS.IsEmpty() ? nullptr : &m_oSRS;
2058 : }
2059 :
2060 311 : CPLErr GetGeoTransform(GDALGeoTransform >) const override
2061 : {
2062 311 : gt = m_gt;
2063 311 : return CE_None;
2064 : }
2065 :
2066 : using GDALDataset::Clone;
2067 :
2068 24 : std::unique_ptr<MosaicDataset> Clone() const
2069 : {
2070 : return std::make_unique<MosaicDataset>(
2071 24 : m_directory, m_extension, m_format, m_aeColorInterp, m_oTM, m_oSRS,
2072 24 : m_nTileMinX, m_nTileMinY, m_nTileMaxX, m_nTileMaxY, m_convention,
2073 24 : nBands, m_eDT, m_pdfDstNoData, m_metadata, m_poCT,
2074 24 : m_nMaxCacheTileSize);
2075 : }
2076 : };
2077 :
2078 : /************************************************************************/
2079 : /* MosaicRasterBand::IReadBlock() */
2080 : /************************************************************************/
2081 :
2082 5209 : CPLErr MosaicRasterBand::IReadBlock(int nXBlock, int nYBlock, void *pData)
2083 : {
2084 5209 : auto poThisDS = cpl::down_cast<MosaicDataset *>(poDS);
2085 : std::string filename = CPLFormFilenameSafe(
2086 10418 : m_directory.c_str(), CPLSPrintf("%d", m_tileMinX + nXBlock), nullptr);
2087 5209 : const int iFileY = GetFileY(m_tileMinY + nYBlock, m_oTM, m_convention);
2088 10418 : filename = CPLFormFilenameSafe(filename.c_str(), CPLSPrintf("%d", iFileY),
2089 5209 : m_extension.c_str());
2090 :
2091 5209 : std::shared_ptr<GDALDataset> poTileDS;
2092 5209 : if (!poThisDS->m_oCacheTile.tryGet(filename, poTileDS))
2093 : {
2094 1471 : const char *const apszAllowedDrivers[] = {poThisDS->m_format.c_str(),
2095 1471 : nullptr};
2096 1471 : const char *const apszAllowedDriversForCOG[] = {"GTiff", "LIBERTIFF",
2097 : nullptr};
2098 : // CPLDebugOnly("gdal_raster_tile", "Opening %s", filename.c_str());
2099 1471 : poTileDS.reset(GDALDataset::Open(
2100 : filename.c_str(), GDAL_OF_RASTER | GDAL_OF_INTERNAL,
2101 1471 : EQUAL(poThisDS->m_format.c_str(), "COG") ? apszAllowedDriversForCOG
2102 : : apszAllowedDrivers));
2103 1471 : if (!poTileDS)
2104 : {
2105 : VSIStatBufL sStat;
2106 36 : if (VSIStatL(filename.c_str(), &sStat) == 0)
2107 : {
2108 1 : CPLError(CE_Failure, CPLE_AppDefined,
2109 : "File %s exists but cannot be opened with %s driver",
2110 : filename.c_str(), poThisDS->m_format.c_str());
2111 1 : return CE_Failure;
2112 : }
2113 : }
2114 1470 : poThisDS->m_oCacheTile.insert(filename, poTileDS);
2115 : }
2116 5208 : if (!poTileDS || nBand > poTileDS->GetRasterCount())
2117 : {
2118 2577 : memset(pData,
2119 1199 : (poTileDS && (nBand == poTileDS->GetRasterCount() + 1)) ? 255
2120 : : 0,
2121 1378 : static_cast<size_t>(nBlockXSize) * nBlockYSize *
2122 1378 : GDALGetDataTypeSizeBytes(eDataType));
2123 1378 : return CE_None;
2124 : }
2125 : else
2126 : {
2127 3830 : return poTileDS->GetRasterBand(nBand)->RasterIO(
2128 : GF_Read, 0, 0, nBlockXSize, nBlockYSize, pData, nBlockXSize,
2129 3830 : nBlockYSize, eDataType, 0, 0, nullptr);
2130 : }
2131 : }
2132 :
2133 : } // namespace
2134 :
2135 : /************************************************************************/
2136 : /* ApplySubstitutions() */
2137 : /************************************************************************/
2138 :
2139 261 : static void ApplySubstitutions(CPLString &s,
2140 : const std::map<std::string, std::string> &substs)
2141 : {
2142 3985 : for (const auto &[key, value] : substs)
2143 : {
2144 3724 : s.replaceAll("%(" + key + ")s", value);
2145 3724 : s.replaceAll("%(" + key + ")d", value);
2146 3724 : s.replaceAll("%(" + key + ")f", value);
2147 3724 : s.replaceAll("${" + key + "}", value);
2148 : }
2149 261 : }
2150 :
2151 : /************************************************************************/
2152 : /* GenerateLeaflet() */
2153 : /************************************************************************/
2154 :
2155 66 : static void GenerateLeaflet(const std::string &osDirectory,
2156 : const std::string &osTitle, double dfSouthLat,
2157 : double dfWestLon, double dfNorthLat,
2158 : double dfEastLon, int nMinZoom, int nMaxZoom,
2159 : int nTileSize, const std::string &osExtension,
2160 : const std::string &osURL,
2161 : const std::string &osCopyright, bool bXYZ)
2162 : {
2163 66 : if (const char *pszTemplate = CPLFindFile("gdal", "leaflet_template.html"))
2164 : {
2165 132 : const std::string osFilename(pszTemplate);
2166 132 : std::map<std::string, std::string> substs;
2167 :
2168 : // For tests
2169 : const char *pszFmt =
2170 66 : atoi(CPLGetConfigOption("GDAL_RASTER_TILE_HTML_PREC", "17")) == 10
2171 : ? "%.10g"
2172 66 : : "%.17g";
2173 :
2174 132 : substs["double_quote_escaped_title"] =
2175 198 : CPLString(osTitle).replaceAll('"', "\\\"");
2176 66 : char *pszStr = CPLEscapeString(osTitle.c_str(), -1, CPLES_XML);
2177 66 : substs["xml_escaped_title"] = pszStr;
2178 66 : CPLFree(pszStr);
2179 66 : substs["south"] = CPLSPrintf(pszFmt, dfSouthLat);
2180 66 : substs["west"] = CPLSPrintf(pszFmt, dfWestLon);
2181 66 : substs["north"] = CPLSPrintf(pszFmt, dfNorthLat);
2182 66 : substs["east"] = CPLSPrintf(pszFmt, dfEastLon);
2183 66 : substs["centerlon"] = CPLSPrintf(pszFmt, (dfNorthLat + dfSouthLat) / 2);
2184 66 : substs["centerlat"] = CPLSPrintf(pszFmt, (dfWestLon + dfEastLon) / 2);
2185 66 : substs["minzoom"] = CPLSPrintf("%d", nMinZoom);
2186 66 : substs["maxzoom"] = CPLSPrintf("%d", nMaxZoom);
2187 66 : substs["beginzoom"] = CPLSPrintf("%d", nMaxZoom);
2188 66 : substs["tile_size"] = CPLSPrintf("%d", nTileSize); // not used
2189 66 : substs["tileformat"] = osExtension;
2190 66 : substs["publishurl"] = osURL; // not used
2191 66 : substs["copyright"] = CPLString(osCopyright).replaceAll('"', "\\\"");
2192 66 : substs["tms"] = bXYZ ? "0" : "1";
2193 :
2194 66 : GByte *pabyRet = nullptr;
2195 66 : CPL_IGNORE_RET_VAL(VSIIngestFile(nullptr, osFilename.c_str(), &pabyRet,
2196 : nullptr, 10 * 1024 * 1024));
2197 66 : if (pabyRet)
2198 : {
2199 132 : CPLString osHTML(reinterpret_cast<char *>(pabyRet));
2200 66 : CPLFree(pabyRet);
2201 :
2202 66 : ApplySubstitutions(osHTML, substs);
2203 :
2204 66 : VSILFILE *f = VSIFOpenL(CPLFormFilenameSafe(osDirectory.c_str(),
2205 : "leaflet.html", nullptr)
2206 : .c_str(),
2207 : "wb");
2208 66 : if (f)
2209 : {
2210 66 : VSIFWriteL(osHTML.data(), 1, osHTML.size(), f);
2211 66 : VSIFCloseL(f);
2212 : }
2213 : }
2214 : }
2215 66 : }
2216 :
2217 : /************************************************************************/
2218 : /* GenerateMapML() */
2219 : /************************************************************************/
2220 :
2221 : static void
2222 40 : GenerateMapML(const std::string &osDirectory, const std::string &mapmlTemplate,
2223 : const std::string &osTitle, int nMinTileX, int nMinTileY,
2224 : int nMaxTileX, int nMaxTileY, int nMinZoom, int nMaxZoom,
2225 : const std::string &osExtension, const std::string &osURL,
2226 : const std::string &osCopyright, const gdal::TileMatrixSet &tms)
2227 : {
2228 40 : if (const char *pszTemplate =
2229 40 : (mapmlTemplate.empty() ? CPLFindFile("gdal", "template_tiles.mapml")
2230 40 : : mapmlTemplate.c_str()))
2231 : {
2232 80 : const std::string osFilename(pszTemplate);
2233 80 : std::map<std::string, std::string> substs;
2234 :
2235 40 : if (tms.identifier() == "GoogleMapsCompatible")
2236 37 : substs["TILING_SCHEME"] = "OSMTILE";
2237 3 : else if (tms.identifier() == "WorldCRS84Quad")
2238 2 : substs["TILING_SCHEME"] = "WGS84";
2239 : else
2240 1 : substs["TILING_SCHEME"] = tms.identifier();
2241 :
2242 40 : substs["URL"] = osURL.empty() ? "./" : osURL + "/";
2243 40 : substs["MINTILEX"] = CPLSPrintf("%d", nMinTileX);
2244 40 : substs["MINTILEY"] = CPLSPrintf("%d", nMinTileY);
2245 40 : substs["MAXTILEX"] = CPLSPrintf("%d", nMaxTileX);
2246 40 : substs["MAXTILEY"] = CPLSPrintf("%d", nMaxTileY);
2247 40 : substs["CURZOOM"] = CPLSPrintf("%d", nMaxZoom);
2248 40 : substs["MINZOOM"] = CPLSPrintf("%d", nMinZoom);
2249 40 : substs["MAXZOOM"] = CPLSPrintf("%d", nMaxZoom);
2250 40 : substs["TILEEXT"] = osExtension;
2251 40 : char *pszStr = CPLEscapeString(osTitle.c_str(), -1, CPLES_XML);
2252 40 : substs["TITLE"] = pszStr;
2253 40 : CPLFree(pszStr);
2254 40 : substs["COPYRIGHT"] = osCopyright;
2255 :
2256 40 : GByte *pabyRet = nullptr;
2257 40 : CPL_IGNORE_RET_VAL(VSIIngestFile(nullptr, osFilename.c_str(), &pabyRet,
2258 : nullptr, 10 * 1024 * 1024));
2259 40 : if (pabyRet)
2260 : {
2261 80 : CPLString osMAPML(reinterpret_cast<char *>(pabyRet));
2262 40 : CPLFree(pabyRet);
2263 :
2264 40 : ApplySubstitutions(osMAPML, substs);
2265 :
2266 40 : VSILFILE *f = VSIFOpenL(
2267 80 : CPLFormFilenameSafe(osDirectory.c_str(), "mapml.mapml", nullptr)
2268 : .c_str(),
2269 : "wb");
2270 40 : if (f)
2271 : {
2272 40 : VSIFWriteL(osMAPML.data(), 1, osMAPML.size(), f);
2273 40 : VSIFCloseL(f);
2274 : }
2275 : }
2276 : }
2277 40 : }
2278 :
2279 : /************************************************************************/
2280 : /* GenerateSTAC() */
2281 : /************************************************************************/
2282 :
2283 : static void
2284 44 : GenerateSTAC(const std::string &osDirectory, const std::string &osTitle,
2285 : double dfWestLon, double dfSouthLat, double dfEastLon,
2286 : double dfNorthLat, const std::vector<std::string> &metadata,
2287 : const std::vector<BandMetadata> &aoBandMetadata, int nMinZoom,
2288 : int nMaxZoom, const std::string &osExtension,
2289 : const std::string &osFormat, const std::string &osURL,
2290 : const std::string &osCopyright, const OGRSpatialReference &oSRS,
2291 : const gdal::TileMatrixSet &tms, bool bInvertAxisTMS, int tileSize,
2292 : const double adfExtent[4], const GDALArgDatasetValue &dataset)
2293 : {
2294 88 : CPLJSONObject oRoot;
2295 44 : oRoot["stac_version"] = "1.1.0";
2296 88 : CPLJSONArray oExtensions;
2297 44 : oRoot["stac_extensions"] = oExtensions;
2298 44 : oRoot["id"] = osTitle;
2299 44 : oRoot["type"] = "Feature";
2300 44 : oRoot["bbox"] = {dfWestLon, dfSouthLat, dfEastLon, dfNorthLat};
2301 88 : CPLJSONObject oGeometry;
2302 :
2303 44 : const auto BuildPolygon = [](double x1, double y1, double x2, double y2)
2304 : {
2305 : return CPLJSONArray::Build({CPLJSONArray::Build(
2306 : {CPLJSONArray::Build({x1, y1}), CPLJSONArray::Build({x1, y2}),
2307 : CPLJSONArray::Build({x2, y2}), CPLJSONArray::Build({x2, y1}),
2308 308 : CPLJSONArray::Build({x1, y1})})});
2309 : };
2310 :
2311 44 : if (dfWestLon <= dfEastLon)
2312 : {
2313 44 : oGeometry["type"] = "Polygon";
2314 : oGeometry["coordinates"] =
2315 44 : BuildPolygon(dfWestLon, dfSouthLat, dfEastLon, dfNorthLat);
2316 : }
2317 : else
2318 : {
2319 0 : oGeometry["type"] = "MultiPolygon";
2320 0 : oGeometry["coordinates"] = {
2321 : BuildPolygon(dfWestLon, dfSouthLat, 180.0, dfNorthLat),
2322 0 : BuildPolygon(-180.0, dfSouthLat, dfEastLon, dfNorthLat)};
2323 : }
2324 44 : oRoot["geometry"] = std::move(oGeometry);
2325 :
2326 88 : CPLJSONObject oProperties;
2327 44 : oRoot["properties"] = oProperties;
2328 88 : const CPLStringList aosMD(metadata);
2329 88 : std::string osDateTime = "1970-01-01T00:00:00.000Z";
2330 44 : if (!dataset.GetName().empty())
2331 : {
2332 : VSIStatBufL sStat;
2333 37 : if (VSIStatL(dataset.GetName().c_str(), &sStat) == 0 &&
2334 18 : sStat.st_mtime != 0)
2335 : {
2336 : struct tm tm;
2337 18 : CPLUnixTimeToYMDHMS(sStat.st_mtime, &tm);
2338 : osDateTime = CPLSPrintf(
2339 18 : "%04d-%02d-%02dT%02d:%02d:%02dZ", tm.tm_year + 1900,
2340 18 : tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
2341 : }
2342 : }
2343 88 : std::string osStartDateTime = "0001-01-01T00:00:00.000Z";
2344 88 : std::string osEndDateTime = "9999-12-31T23:59:59.999Z";
2345 :
2346 0 : const auto GetDateTimeAsISO8211 = [](const char *pszInput)
2347 : {
2348 0 : std::string osRet;
2349 : OGRField sField;
2350 0 : if (OGRParseDate(pszInput, &sField, 0))
2351 : {
2352 0 : char *pszDT = OGRGetXMLDateTime(&sField);
2353 0 : if (pszDT)
2354 0 : osRet = pszDT;
2355 0 : CPLFree(pszDT);
2356 : }
2357 0 : return osRet;
2358 : };
2359 :
2360 45 : for (const auto &[key, value] : cpl::IterateNameValue(aosMD))
2361 : {
2362 1 : if (EQUAL(key, "datetime"))
2363 : {
2364 0 : std::string osTmp = GetDateTimeAsISO8211(value);
2365 0 : if (!osTmp.empty())
2366 : {
2367 0 : osDateTime = std::move(osTmp);
2368 0 : continue;
2369 : }
2370 : }
2371 1 : else if (EQUAL(key, "start_datetime"))
2372 : {
2373 0 : std::string osTmp = GetDateTimeAsISO8211(value);
2374 0 : if (!osTmp.empty())
2375 : {
2376 0 : osStartDateTime = std::move(osTmp);
2377 0 : continue;
2378 : }
2379 : }
2380 1 : else if (EQUAL(key, "end_datetime"))
2381 : {
2382 0 : std::string osTmp = GetDateTimeAsISO8211(value);
2383 0 : if (!osTmp.empty())
2384 : {
2385 0 : osEndDateTime = std::move(osTmp);
2386 0 : continue;
2387 : }
2388 : }
2389 1 : else if (EQUAL(key, "TIFFTAG_DATETIME"))
2390 : {
2391 : int nYear, nMonth, nDay, nHour, nMin, nSec;
2392 0 : if (sscanf(value, "%04d:%02d:%02d %02d:%02d:%02d", &nYear, &nMonth,
2393 0 : &nDay, &nHour, &nMin, &nSec) == 6)
2394 : {
2395 : osDateTime = CPLSPrintf("%04d-%02d-%02dT%02d:%02d:%02dZ", nYear,
2396 0 : nMonth, nDay, nHour, nMin, nSec);
2397 0 : continue;
2398 : }
2399 : }
2400 :
2401 1 : oProperties[key] = value;
2402 : }
2403 44 : oProperties["datetime"] = osDateTime;
2404 44 : oProperties["start_datetime"] = osStartDateTime;
2405 44 : oProperties["end_datetime"] = osEndDateTime;
2406 44 : if (!osCopyright.empty())
2407 0 : oProperties["copyright"] = osCopyright;
2408 :
2409 : // Just keep the tile matrix zoom levels we use
2410 88 : gdal::TileMatrixSet tmsLimitedToZoomLevelUsed(tms);
2411 44 : auto &tileMatrixList = tmsLimitedToZoomLevelUsed.tileMatrixList();
2412 44 : tileMatrixList.erase(tileMatrixList.begin() + nMaxZoom + 1,
2413 88 : tileMatrixList.end());
2414 44 : tileMatrixList.erase(tileMatrixList.begin(),
2415 88 : tileMatrixList.begin() + nMinZoom);
2416 :
2417 88 : CPLJSONObject oLimits;
2418 : // Patch their definition with the potentially overridden tileSize.
2419 130 : for (auto &tm : tileMatrixList)
2420 : {
2421 86 : int nOvrMinTileX = 0;
2422 86 : int nOvrMinTileY = 0;
2423 86 : int nOvrMaxTileX = 0;
2424 86 : int nOvrMaxTileY = 0;
2425 86 : bool bIntersects = false;
2426 86 : CPL_IGNORE_RET_VAL(GetTileIndices(
2427 : tm, bInvertAxisTMS, tileSize, adfExtent, nOvrMinTileX, nOvrMinTileY,
2428 : nOvrMaxTileX, nOvrMaxTileY, /* noIntersectionIsOK = */ true,
2429 : bIntersects));
2430 :
2431 86 : CPLJSONObject oLimit;
2432 86 : oLimit["min_tile_col"] = nOvrMinTileX;
2433 86 : oLimit["max_tile_col"] = nOvrMaxTileX;
2434 86 : oLimit["min_tile_row"] = nOvrMinTileY;
2435 86 : oLimit["max_tile_row"] = nOvrMaxTileY;
2436 86 : oLimits[tm.mId] = std::move(oLimit);
2437 : }
2438 :
2439 88 : CPLJSONObject oTilesTileMatrixSets;
2440 : {
2441 44 : CPLJSONDocument oDoc;
2442 44 : CPL_IGNORE_RET_VAL(
2443 44 : oDoc.LoadMemory(tmsLimitedToZoomLevelUsed.exportToTMSJsonV1()));
2444 : oTilesTileMatrixSets[tmsLimitedToZoomLevelUsed.identifier()] =
2445 44 : oDoc.GetRoot();
2446 : }
2447 44 : oProperties["tiles:tile_matrix_sets"] = std::move(oTilesTileMatrixSets);
2448 :
2449 88 : CPLJSONObject oTilesTileMatrixLinks;
2450 88 : CPLJSONObject oTilesTileMatrixLink;
2451 : oTilesTileMatrixLink["url"] =
2452 44 : std::string("#").append(tmsLimitedToZoomLevelUsed.identifier());
2453 44 : oTilesTileMatrixLink["limits"] = std::move(oLimits);
2454 : oTilesTileMatrixLinks[tmsLimitedToZoomLevelUsed.identifier()] =
2455 44 : std::move(oTilesTileMatrixLink);
2456 44 : oProperties["tiles:tile_matrix_links"] = std::move(oTilesTileMatrixLinks);
2457 :
2458 44 : const char *pszAuthName = oSRS.GetAuthorityName(nullptr);
2459 44 : const char *pszAuthCode = oSRS.GetAuthorityCode(nullptr);
2460 44 : if (pszAuthName && pszAuthCode)
2461 : {
2462 43 : oProperties["proj:code"] =
2463 43 : std::string(pszAuthName).append(":").append(pszAuthCode);
2464 : }
2465 : else
2466 : {
2467 1 : char *pszPROJJSON = nullptr;
2468 1 : CPL_IGNORE_RET_VAL(oSRS.exportToPROJJSON(&pszPROJJSON, nullptr));
2469 1 : if (pszPROJJSON)
2470 : {
2471 0 : CPLJSONDocument oDoc;
2472 0 : CPL_IGNORE_RET_VAL(oDoc.LoadMemory(pszPROJJSON));
2473 0 : CPLFree(pszPROJJSON);
2474 0 : oProperties["proj:projjson"] = oDoc.GetRoot();
2475 : }
2476 : }
2477 : {
2478 44 : auto ovrTileMatrix = tms.tileMatrixList()[nMaxZoom];
2479 44 : int nOvrMinTileX = 0;
2480 44 : int nOvrMinTileY = 0;
2481 44 : int nOvrMaxTileX = 0;
2482 44 : int nOvrMaxTileY = 0;
2483 44 : bool bIntersects = false;
2484 44 : CPL_IGNORE_RET_VAL(GetTileIndices(
2485 : ovrTileMatrix, bInvertAxisTMS, tileSize, adfExtent, nOvrMinTileX,
2486 : nOvrMinTileY, nOvrMaxTileX, nOvrMaxTileY,
2487 : /* noIntersectionIsOK = */ true, bIntersects));
2488 0 : oProperties["proj:shape"] = {
2489 44 : (nOvrMaxTileY - nOvrMinTileY + 1) * ovrTileMatrix.mTileHeight,
2490 44 : (nOvrMaxTileX - nOvrMinTileX + 1) * ovrTileMatrix.mTileWidth};
2491 :
2492 0 : oProperties["proj:transform"] = {
2493 44 : ovrTileMatrix.mResX,
2494 : 0.0,
2495 44 : ovrTileMatrix.mTopLeftX +
2496 44 : nOvrMinTileX * ovrTileMatrix.mTileWidth * ovrTileMatrix.mResX,
2497 : 0.0,
2498 44 : -ovrTileMatrix.mResY,
2499 44 : ovrTileMatrix.mTopLeftY +
2500 44 : nOvrMinTileY * ovrTileMatrix.mTileHeight * ovrTileMatrix.mResY,
2501 : 0.0,
2502 : 0.0,
2503 44 : 0.0};
2504 : }
2505 :
2506 44 : constexpr const char *ASSET_NAME = "bands";
2507 :
2508 88 : CPLJSONObject oAssetTemplates;
2509 44 : oRoot["asset_templates"] = oAssetTemplates;
2510 :
2511 88 : CPLJSONObject oAssetTemplate;
2512 44 : oAssetTemplates[ASSET_NAME] = oAssetTemplate;
2513 :
2514 88 : std::string osHref = (osURL.empty() ? std::string(".") : std::string(osURL))
2515 44 : .append("/{TileMatrix}/{TileCol}/{TileRow}.")
2516 88 : .append(osExtension);
2517 :
2518 : const std::map<std::string, std::string> oMapVSIToURIPrefix = {
2519 : {"vsis3", "s3://"},
2520 : {"vsigs", "gs://"},
2521 : {"vsiaz", "az://"}, // Not universally recognized
2522 264 : };
2523 :
2524 : const CPLStringList aosSplitHref(
2525 88 : CSLTokenizeString2(osHref.c_str(), "/", 0));
2526 44 : if (!aosSplitHref.empty())
2527 : {
2528 44 : const auto oIter = oMapVSIToURIPrefix.find(aosSplitHref[0]);
2529 44 : if (oIter != oMapVSIToURIPrefix.end())
2530 : {
2531 : // +2 because of 2 slash characters
2532 0 : osHref = std::string(oIter->second)
2533 0 : .append(osHref.c_str() + strlen(aosSplitHref[0]) + 2);
2534 : }
2535 : }
2536 44 : oAssetTemplate["href"] = osHref;
2537 :
2538 44 : if (EQUAL(osFormat.c_str(), "COG"))
2539 : oAssetTemplate["type"] =
2540 0 : "image/tiff; application=geotiff; profile=cloud-optimized";
2541 44 : else if (osExtension == "tif")
2542 2 : oAssetTemplate["type"] = "image/tiff; application=geotiff";
2543 42 : else if (osExtension == "png")
2544 42 : oAssetTemplate["type"] = "image/png";
2545 0 : else if (osExtension == "jpg")
2546 0 : oAssetTemplate["type"] = "image/jpeg";
2547 0 : else if (osExtension == "webp")
2548 0 : oAssetTemplate["type"] = "image/webp";
2549 :
2550 : const std::map<GDALDataType, const char *> oMapDTToStac = {
2551 : {GDT_Int8, "int8"},
2552 : {GDT_Int16, "int16"},
2553 : {GDT_Int32, "int32"},
2554 : {GDT_Int64, "int64"},
2555 : {GDT_UInt8, "uint8"},
2556 : {GDT_UInt16, "uint16"},
2557 : {GDT_UInt32, "uint32"},
2558 : {GDT_UInt64, "uint64"},
2559 : // float16: 16-bit float; unhandled
2560 : {GDT_Float32, "float32"},
2561 : {GDT_Float64, "float64"},
2562 : {GDT_CInt16, "cint16"},
2563 : {GDT_CInt32, "cint32"},
2564 : // cfloat16: complex 16-bit float; unhandled
2565 : {GDT_CFloat32, "cfloat32"},
2566 : {GDT_CFloat64, "cfloat64"},
2567 88 : };
2568 :
2569 88 : CPLJSONArray oBands;
2570 44 : int iBand = 1;
2571 44 : bool bEOExtensionUsed = false;
2572 153 : for (const auto &bandMD : aoBandMetadata)
2573 : {
2574 218 : CPLJSONObject oBand;
2575 109 : oBand["name"] = bandMD.osDescription.empty()
2576 218 : ? std::string(CPLSPrintf("Band%d", iBand))
2577 109 : : bandMD.osDescription;
2578 :
2579 109 : const auto oIter = oMapDTToStac.find(bandMD.eDT);
2580 109 : if (oIter != oMapDTToStac.end())
2581 109 : oBand["data_type"] = oIter->second;
2582 :
2583 109 : if (const char *pszCommonName =
2584 109 : GDALGetSTACCommonNameFromColorInterp(bandMD.eColorInterp))
2585 : {
2586 54 : bEOExtensionUsed = true;
2587 54 : oBand["eo:common_name"] = pszCommonName;
2588 : }
2589 109 : if (!bandMD.osCenterWaveLength.empty() && !bandMD.osFWHM.empty())
2590 : {
2591 0 : bEOExtensionUsed = true;
2592 : oBand["eo:center_wavelength"] =
2593 0 : CPLAtof(bandMD.osCenterWaveLength.c_str());
2594 0 : oBand["eo:full_width_half_max"] = CPLAtof(bandMD.osFWHM.c_str());
2595 : }
2596 109 : ++iBand;
2597 109 : oBands.Add(oBand);
2598 : }
2599 44 : oAssetTemplate["bands"] = oBands;
2600 :
2601 44 : oRoot.Add("assets", CPLJSONObject());
2602 44 : oRoot.Add("links", CPLJSONArray());
2603 :
2604 44 : oExtensions.Add(
2605 : "https://stac-extensions.github.io/tiled-assets/v1.0.0/schema.json");
2606 44 : oExtensions.Add(
2607 : "https://stac-extensions.github.io/projection/v2.0.0/schema.json");
2608 44 : if (bEOExtensionUsed)
2609 18 : oExtensions.Add(
2610 : "https://stac-extensions.github.io/eo/v2.0.0/schema.json");
2611 :
2612 : // Serialize JSON document to file
2613 : const std::string osJSON =
2614 88 : CPLString(oRoot.Format(CPLJSONObject::PrettyFormat::Pretty))
2615 132 : .replaceAll("\\/", '/');
2616 44 : VSILFILE *f = VSIFOpenL(
2617 88 : CPLFormFilenameSafe(osDirectory.c_str(), "stacta.json", nullptr)
2618 : .c_str(),
2619 : "wb");
2620 44 : if (f)
2621 : {
2622 44 : VSIFWriteL(osJSON.data(), 1, osJSON.size(), f);
2623 44 : VSIFCloseL(f);
2624 : }
2625 44 : }
2626 :
2627 : /************************************************************************/
2628 : /* GenerateOpenLayers() */
2629 : /************************************************************************/
2630 :
2631 77 : static void GenerateOpenLayers(
2632 : const std::string &osDirectory, const std::string &osTitle, double dfMinX,
2633 : double dfMinY, double dfMaxX, double dfMaxY, int nMinZoom, int nMaxZoom,
2634 : int nTileSize, const std::string &osExtension, const std::string &osURL,
2635 : const std::string &osCopyright, const gdal::TileMatrixSet &tms,
2636 : bool bInvertAxisTMS, const OGRSpatialReference &oSRS_TMS, bool bXYZ)
2637 : {
2638 154 : std::map<std::string, std::string> substs;
2639 :
2640 : // For tests
2641 : const char *pszFmt =
2642 77 : atoi(CPLGetConfigOption("GDAL_RASTER_TILE_HTML_PREC", "17")) == 10
2643 : ? "%.10g"
2644 77 : : "%.17g";
2645 :
2646 77 : char *pszStr = CPLEscapeString(osTitle.c_str(), -1, CPLES_XML);
2647 77 : substs["xml_escaped_title"] = pszStr;
2648 77 : CPLFree(pszStr);
2649 77 : substs["ominx"] = CPLSPrintf(pszFmt, dfMinX);
2650 77 : substs["ominy"] = CPLSPrintf(pszFmt, dfMinY);
2651 77 : substs["omaxx"] = CPLSPrintf(pszFmt, dfMaxX);
2652 77 : substs["omaxy"] = CPLSPrintf(pszFmt, dfMaxY);
2653 77 : substs["center_x"] = CPLSPrintf(pszFmt, (dfMinX + dfMaxX) / 2);
2654 77 : substs["center_y"] = CPLSPrintf(pszFmt, (dfMinY + dfMaxY) / 2);
2655 77 : substs["minzoom"] = CPLSPrintf("%d", nMinZoom);
2656 77 : substs["maxzoom"] = CPLSPrintf("%d", nMaxZoom);
2657 77 : substs["tile_size"] = CPLSPrintf("%d", nTileSize);
2658 77 : substs["tileformat"] = osExtension;
2659 77 : substs["publishurl"] = osURL;
2660 77 : substs["copyright"] = osCopyright;
2661 77 : substs["sign_y"] = bXYZ ? "" : "-";
2662 :
2663 : CPLString s(R"raw(<!DOCTYPE html>
2664 : <html>
2665 : <head>
2666 : <title>%(xml_escaped_title)s</title>
2667 : <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
2668 : <meta http-equiv='imagetoolbar' content='no'/>
2669 : <style type="text/css"> v\:* {behavior:url(#default#VML);}
2670 : html, body { overflow: hidden; padding: 0; height: 100%; width: 100%; font-family: 'Lucida Grande',Geneva,Arial,Verdana,sans-serif; }
2671 : body { margin: 10px; background: #fff; }
2672 : h1 { margin: 0; padding: 6px; border:0; font-size: 20pt; }
2673 : #header { height: 43px; padding: 0; background-color: #eee; border: 1px solid #888; }
2674 : #subheader { height: 12px; text-align: right; font-size: 10px; color: #555;}
2675 : #map { height: 90%; border: 1px solid #888; }
2676 : </style>
2677 : <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@main/dist/en/v7.0.0/legacy/ol.css" type="text/css">
2678 : <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@main/dist/en/v7.0.0/legacy/ol.js"></script>
2679 : <script src="https://unpkg.com/ol-layerswitcher@4.1.1"></script>
2680 : <link rel="stylesheet" href="https://unpkg.com/ol-layerswitcher@4.1.1/src/ol-layerswitcher.css" />
2681 : </head>
2682 : <body>
2683 : <div id="header"><h1>%(xml_escaped_title)s</h1></div>
2684 : <div id="subheader">Generated by <a href="https://gdal.org/programs/gdal_raster_tile.html">gdal raster tile</a> </div>
2685 : <div id="map" class="map"></div>
2686 : <div id="mouse-position"></div>
2687 : <script type="text/javascript">
2688 : var mousePositionControl = new ol.control.MousePosition({
2689 : className: 'custom-mouse-position',
2690 : target: document.getElementById('mouse-position'),
2691 : undefinedHTML: ' '
2692 : });
2693 : var map = new ol.Map({
2694 : controls: ol.control.defaults.defaults().extend([mousePositionControl]),
2695 154 : target: 'map',)raw");
2696 :
2697 88 : if (tms.identifier() == "GoogleMapsCompatible" ||
2698 11 : tms.identifier() == "WorldCRS84Quad")
2699 : {
2700 69 : s += R"raw(
2701 : layers: [
2702 : new ol.layer.Group({
2703 : title: 'Base maps',
2704 : layers: [
2705 : new ol.layer.Tile({
2706 : title: 'OpenStreetMap',
2707 : type: 'base',
2708 : visible: true,
2709 : source: new ol.source.OSM()
2710 : }),
2711 : ]
2712 : }),)raw";
2713 : }
2714 :
2715 77 : if (tms.identifier() == "GoogleMapsCompatible")
2716 : {
2717 66 : s += R"raw(new ol.layer.Group({
2718 : title: 'Overlay',
2719 : layers: [
2720 : new ol.layer.Tile({
2721 : title: 'Overlay',
2722 : // opacity: 0.7,
2723 : extent: [%(ominx)f, %(ominy)f,%(omaxx)f, %(omaxy)f],
2724 : source: new ol.source.XYZ({
2725 : attributions: '%(copyright)s',
2726 : minZoom: %(minzoom)d,
2727 : maxZoom: %(maxzoom)d,
2728 : url: './{z}/{x}/{%(sign_y)sy}.%(tileformat)s',
2729 : tileSize: [%(tile_size)d, %(tile_size)d]
2730 : })
2731 : }),
2732 : ]
2733 : }),)raw";
2734 : }
2735 11 : else if (tms.identifier() == "WorldCRS84Quad")
2736 : {
2737 3 : const double base_res = 180.0 / nTileSize;
2738 6 : std::string resolutions = "[";
2739 7 : for (int i = 0; i <= nMaxZoom; ++i)
2740 : {
2741 4 : if (i > 0)
2742 1 : resolutions += ",";
2743 4 : resolutions += CPLSPrintf(pszFmt, base_res / (1 << i));
2744 : }
2745 3 : resolutions += "]";
2746 3 : substs["resolutions"] = std::move(resolutions);
2747 :
2748 3 : if (bXYZ)
2749 : {
2750 2 : substs["origin"] = "[-180,90]";
2751 2 : substs["y_formula"] = "tileCoord[2]";
2752 : }
2753 : else
2754 : {
2755 1 : substs["origin"] = "[-180,-90]";
2756 1 : substs["y_formula"] = "- 1 - tileCoord[2]";
2757 : }
2758 :
2759 3 : s += R"raw(
2760 : new ol.layer.Group({
2761 : title: 'Overlay',
2762 : layers: [
2763 : new ol.layer.Tile({
2764 : title: 'Overlay',
2765 : // opacity: 0.7,
2766 : extent: [%(ominx)f, %(ominy)f,%(omaxx)f, %(omaxy)f],
2767 : source: new ol.source.TileImage({
2768 : attributions: '%(copyright)s',
2769 : projection: 'EPSG:4326',
2770 : minZoom: %(minzoom)d,
2771 : maxZoom: %(maxzoom)d,
2772 : tileGrid: new ol.tilegrid.TileGrid({
2773 : extent: [-180,-90,180,90],
2774 : origin: %(origin)s,
2775 : resolutions: %(resolutions)s,
2776 : tileSize: [%(tile_size)d, %(tile_size)d]
2777 : }),
2778 : tileUrlFunction: function(tileCoord) {
2779 : return ('./{z}/{x}/{y}.%(tileformat)s'
2780 : .replace('{z}', String(tileCoord[0]))
2781 : .replace('{x}', String(tileCoord[1]))
2782 : .replace('{y}', String(%(y_formula)s)));
2783 : },
2784 : })
2785 : }),
2786 : ]
2787 : }),)raw";
2788 : }
2789 : else
2790 : {
2791 16 : substs["maxres"] =
2792 16 : CPLSPrintf(pszFmt, tms.tileMatrixList()[nMinZoom].mResX);
2793 16 : std::string resolutions = "[";
2794 22 : for (int i = 0; i <= nMaxZoom; ++i)
2795 : {
2796 14 : if (i > 0)
2797 6 : resolutions += ",";
2798 14 : resolutions += CPLSPrintf(pszFmt, tms.tileMatrixList()[i].mResX);
2799 : }
2800 8 : resolutions += "]";
2801 8 : substs["resolutions"] = std::move(resolutions);
2802 :
2803 16 : std::string matrixsizes = "[";
2804 22 : for (int i = 0; i <= nMaxZoom; ++i)
2805 : {
2806 14 : if (i > 0)
2807 6 : matrixsizes += ",";
2808 : matrixsizes +=
2809 14 : CPLSPrintf("[%d,%d]", tms.tileMatrixList()[i].mMatrixWidth,
2810 28 : tms.tileMatrixList()[i].mMatrixHeight);
2811 : }
2812 8 : matrixsizes += "]";
2813 8 : substs["matrixsizes"] = std::move(matrixsizes);
2814 :
2815 8 : double dfTopLeftX = tms.tileMatrixList()[0].mTopLeftX;
2816 8 : double dfTopLeftY = tms.tileMatrixList()[0].mTopLeftY;
2817 8 : if (bInvertAxisTMS)
2818 0 : std::swap(dfTopLeftX, dfTopLeftY);
2819 :
2820 8 : if (bXYZ)
2821 : {
2822 12 : substs["origin"] =
2823 12 : CPLSPrintf("[%.17g,%.17g]", dfTopLeftX, dfTopLeftY);
2824 6 : substs["y_formula"] = "tileCoord[2]";
2825 : }
2826 : else
2827 : {
2828 4 : substs["origin"] = CPLSPrintf(
2829 : "[%.17g,%.17g]", dfTopLeftX,
2830 2 : dfTopLeftY - tms.tileMatrixList()[0].mResY *
2831 6 : tms.tileMatrixList()[0].mTileHeight);
2832 2 : substs["y_formula"] = "- 1 - tileCoord[2]";
2833 : }
2834 :
2835 16 : substs["tilegrid_extent"] =
2836 : CPLSPrintf("[%.17g,%.17g,%.17g,%.17g]", dfTopLeftX,
2837 8 : dfTopLeftY - tms.tileMatrixList()[0].mMatrixHeight *
2838 8 : tms.tileMatrixList()[0].mResY *
2839 8 : tms.tileMatrixList()[0].mTileHeight,
2840 8 : dfTopLeftX + tms.tileMatrixList()[0].mMatrixWidth *
2841 8 : tms.tileMatrixList()[0].mResX *
2842 8 : tms.tileMatrixList()[0].mTileWidth,
2843 32 : dfTopLeftY);
2844 :
2845 8 : s += R"raw(
2846 : layers: [
2847 : new ol.layer.Group({
2848 : title: 'Overlay',
2849 : layers: [
2850 : new ol.layer.Tile({
2851 : title: 'Overlay',
2852 : // opacity: 0.7,
2853 : extent: [%(ominx)f, %(ominy)f,%(omaxx)f, %(omaxy)f],
2854 : source: new ol.source.TileImage({
2855 : attributions: '%(copyright)s',
2856 : minZoom: %(minzoom)d,
2857 : maxZoom: %(maxzoom)d,
2858 : tileGrid: new ol.tilegrid.TileGrid({
2859 : extent: %(tilegrid_extent)s,
2860 : origin: %(origin)s,
2861 : resolutions: %(resolutions)s,
2862 : sizes: %(matrixsizes)s,
2863 : tileSize: [%(tile_size)d, %(tile_size)d]
2864 : }),
2865 : tileUrlFunction: function(tileCoord) {
2866 : return ('./{z}/{x}/{y}.%(tileformat)s'
2867 : .replace('{z}', String(tileCoord[0]))
2868 : .replace('{x}', String(tileCoord[1]))
2869 : .replace('{y}', String(%(y_formula)s)));
2870 : },
2871 : })
2872 : }),
2873 : ]
2874 : }),)raw";
2875 : }
2876 :
2877 77 : s += R"raw(
2878 : ],
2879 : view: new ol.View({
2880 : center: [%(center_x)f, %(center_y)f],)raw";
2881 :
2882 88 : if (tms.identifier() == "GoogleMapsCompatible" ||
2883 11 : tms.identifier() == "WorldCRS84Quad")
2884 : {
2885 69 : substs["view_zoom"] = substs["minzoom"];
2886 69 : if (tms.identifier() == "WorldCRS84Quad")
2887 : {
2888 3 : substs["view_zoom"] = CPLSPrintf("%d", nMinZoom + 1);
2889 : }
2890 :
2891 69 : s += R"raw(
2892 : zoom: %(view_zoom)d,)raw";
2893 : }
2894 : else
2895 : {
2896 8 : s += R"raw(
2897 : resolution: %(maxres)f,)raw";
2898 : }
2899 :
2900 77 : if (tms.identifier() == "WorldCRS84Quad")
2901 : {
2902 3 : s += R"raw(
2903 : projection: 'EPSG:4326',)raw";
2904 : }
2905 74 : else if (!oSRS_TMS.IsEmpty() && tms.identifier() != "GoogleMapsCompatible")
2906 : {
2907 7 : const char *pszAuthName = oSRS_TMS.GetAuthorityName(nullptr);
2908 7 : const char *pszAuthCode = oSRS_TMS.GetAuthorityCode(nullptr);
2909 7 : if (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "EPSG"))
2910 : {
2911 5 : substs["epsg_code"] = pszAuthCode;
2912 5 : if (oSRS_TMS.IsGeographic())
2913 : {
2914 3 : substs["units"] = "deg";
2915 : }
2916 : else
2917 : {
2918 2 : const char *pszUnits = "";
2919 2 : if (oSRS_TMS.GetLinearUnits(&pszUnits) == 1.0)
2920 2 : substs["units"] = "m";
2921 : else
2922 0 : substs["units"] = pszUnits;
2923 : }
2924 5 : s += R"raw(
2925 : projection: new ol.proj.Projection({code: 'EPSG:%(epsg_code)s', units:'%(units)s'}),)raw";
2926 : }
2927 : }
2928 :
2929 77 : s += R"raw(
2930 : })
2931 : });)raw";
2932 :
2933 88 : if (tms.identifier() == "GoogleMapsCompatible" ||
2934 11 : tms.identifier() == "WorldCRS84Quad")
2935 : {
2936 69 : s += R"raw(
2937 : map.addControl(new ol.control.LayerSwitcher());)raw";
2938 : }
2939 :
2940 77 : s += R"raw(
2941 : </script>
2942 : </body>
2943 : </html>)raw";
2944 :
2945 77 : ApplySubstitutions(s, substs);
2946 :
2947 77 : VSILFILE *f = VSIFOpenL(
2948 154 : CPLFormFilenameSafe(osDirectory.c_str(), "openlayers.html", nullptr)
2949 : .c_str(),
2950 : "wb");
2951 77 : if (f)
2952 : {
2953 77 : VSIFWriteL(s.data(), 1, s.size(), f);
2954 77 : VSIFCloseL(f);
2955 : }
2956 77 : }
2957 :
2958 : /************************************************************************/
2959 : /* GetTileBoundingBox() */
2960 : /************************************************************************/
2961 :
2962 48 : static void GetTileBoundingBox(int nTileX, int nTileY, int nTileZ,
2963 : const gdal::TileMatrixSet *poTMS,
2964 : bool bInvertAxisTMS,
2965 : OGRCoordinateTransformation *poCTToWGS84,
2966 : double &dfTLX, double &dfTLY, double &dfTRX,
2967 : double &dfTRY, double &dfLLX, double &dfLLY,
2968 : double &dfLRX, double &dfLRY)
2969 : {
2970 : gdal::TileMatrixSet::TileMatrix tileMatrix =
2971 96 : poTMS->tileMatrixList()[nTileZ];
2972 48 : if (bInvertAxisTMS)
2973 0 : std::swap(tileMatrix.mTopLeftX, tileMatrix.mTopLeftY);
2974 :
2975 48 : dfTLX = tileMatrix.mTopLeftX +
2976 48 : nTileX * tileMatrix.mResX * tileMatrix.mTileWidth;
2977 48 : dfTLY = tileMatrix.mTopLeftY -
2978 48 : nTileY * tileMatrix.mResY * tileMatrix.mTileHeight;
2979 48 : poCTToWGS84->Transform(1, &dfTLX, &dfTLY);
2980 :
2981 48 : dfTRX = tileMatrix.mTopLeftX +
2982 48 : (nTileX + 1) * tileMatrix.mResX * tileMatrix.mTileWidth;
2983 48 : dfTRY = tileMatrix.mTopLeftY -
2984 48 : nTileY * tileMatrix.mResY * tileMatrix.mTileHeight;
2985 48 : poCTToWGS84->Transform(1, &dfTRX, &dfTRY);
2986 :
2987 48 : dfLLX = tileMatrix.mTopLeftX +
2988 48 : nTileX * tileMatrix.mResX * tileMatrix.mTileWidth;
2989 48 : dfLLY = tileMatrix.mTopLeftY -
2990 48 : (nTileY + 1) * tileMatrix.mResY * tileMatrix.mTileHeight;
2991 48 : poCTToWGS84->Transform(1, &dfLLX, &dfLLY);
2992 :
2993 48 : dfLRX = tileMatrix.mTopLeftX +
2994 48 : (nTileX + 1) * tileMatrix.mResX * tileMatrix.mTileWidth;
2995 48 : dfLRY = tileMatrix.mTopLeftY -
2996 48 : (nTileY + 1) * tileMatrix.mResY * tileMatrix.mTileHeight;
2997 48 : poCTToWGS84->Transform(1, &dfLRX, &dfLRY);
2998 48 : }
2999 :
3000 : /************************************************************************/
3001 : /* GenerateKML() */
3002 : /************************************************************************/
3003 :
3004 : namespace
3005 : {
3006 : struct TileCoordinates
3007 : {
3008 : int nTileX = 0;
3009 : int nTileY = 0;
3010 : int nTileZ = 0;
3011 : };
3012 : } // namespace
3013 :
3014 30 : static void GenerateKML(const std::string &osDirectory,
3015 : const std::string &osTitle, int nTileX, int nTileY,
3016 : int nTileZ, int nTileSize,
3017 : const std::string &osExtension,
3018 : const std::string &osURL,
3019 : const gdal::TileMatrixSet *poTMS, bool bInvertAxisTMS,
3020 : const std::string &convention,
3021 : OGRCoordinateTransformation *poCTToWGS84,
3022 : const std::vector<TileCoordinates> &children)
3023 : {
3024 60 : std::map<std::string, std::string> substs;
3025 :
3026 30 : const bool bIsTileKML = nTileX >= 0;
3027 :
3028 : // For tests
3029 : const char *pszFmt =
3030 30 : atoi(CPLGetConfigOption("GDAL_RASTER_TILE_KML_PREC", "14")) == 10
3031 : ? "%.10f"
3032 30 : : "%.14f";
3033 :
3034 30 : substs["tx"] = CPLSPrintf("%d", nTileX);
3035 30 : substs["tz"] = CPLSPrintf("%d", nTileZ);
3036 30 : substs["tileformat"] = osExtension;
3037 30 : substs["minlodpixels"] = CPLSPrintf("%d", nTileSize / 2);
3038 60 : substs["maxlodpixels"] =
3039 60 : children.empty() ? "-1" : CPLSPrintf("%d", nTileSize * 8);
3040 :
3041 30 : double dfTLX = 0;
3042 30 : double dfTLY = 0;
3043 30 : double dfTRX = 0;
3044 30 : double dfTRY = 0;
3045 30 : double dfLLX = 0;
3046 30 : double dfLLY = 0;
3047 30 : double dfLRX = 0;
3048 30 : double dfLRY = 0;
3049 :
3050 30 : int nFileY = -1;
3051 30 : if (!bIsTileKML)
3052 : {
3053 6 : char *pszStr = CPLEscapeString(osTitle.c_str(), -1, CPLES_XML);
3054 6 : substs["xml_escaped_title"] = pszStr;
3055 6 : CPLFree(pszStr);
3056 : }
3057 : else
3058 : {
3059 24 : nFileY = GetFileY(nTileY, poTMS->tileMatrixList()[nTileZ], convention);
3060 24 : substs["realtiley"] = CPLSPrintf("%d", nFileY);
3061 48 : substs["xml_escaped_title"] =
3062 48 : CPLSPrintf("%d/%d/%d.kml", nTileZ, nTileX, nFileY);
3063 :
3064 24 : GetTileBoundingBox(nTileX, nTileY, nTileZ, poTMS, bInvertAxisTMS,
3065 : poCTToWGS84, dfTLX, dfTLY, dfTRX, dfTRY, dfLLX,
3066 : dfLLY, dfLRX, dfLRY);
3067 : }
3068 :
3069 60 : substs["drawOrder"] = CPLSPrintf("%d", nTileX == 0 ? 2 * nTileZ + 1
3070 19 : : nTileX > 0 ? 2 * nTileZ
3071 79 : : 0);
3072 :
3073 30 : substs["url"] = osURL.empty() && bIsTileKML ? "../../" : "";
3074 :
3075 30 : const bool bIsRectangle =
3076 30 : (dfTLX == dfLLX && dfTRX == dfLRX && dfTLY == dfTRY && dfLLY == dfLRY);
3077 30 : const bool bUseGXNamespace = bIsTileKML && !bIsRectangle;
3078 :
3079 60 : substs["xmlns_gx"] = bUseGXNamespace
3080 : ? " xmlns:gx=\"http://www.google.com/kml/ext/2.2\""
3081 60 : : "";
3082 :
3083 : CPLString s(R"raw(<?xml version="1.0" encoding="utf-8"?>
3084 : <kml xmlns="http://www.opengis.net/kml/2.2"%(xmlns_gx)s>
3085 : <Document>
3086 : <name>%(xml_escaped_title)s</name>
3087 : <description></description>
3088 : <Style>
3089 : <ListStyle id="hideChildren">
3090 : <listItemType>checkHideChildren</listItemType>
3091 : </ListStyle>
3092 : </Style>
3093 60 : )raw");
3094 30 : ApplySubstitutions(s, substs);
3095 :
3096 30 : if (bIsTileKML)
3097 : {
3098 : CPLString s2(R"raw( <Region>
3099 : <LatLonAltBox>
3100 : <north>%(north)f</north>
3101 : <south>%(south)f</south>
3102 : <east>%(east)f</east>
3103 : <west>%(west)f</west>
3104 : </LatLonAltBox>
3105 : <Lod>
3106 : <minLodPixels>%(minlodpixels)d</minLodPixels>
3107 : <maxLodPixels>%(maxlodpixels)d</maxLodPixels>
3108 : </Lod>
3109 : </Region>
3110 : <GroundOverlay>
3111 : <drawOrder>%(drawOrder)d</drawOrder>
3112 : <Icon>
3113 : <href>%(realtiley)d.%(tileformat)s</href>
3114 : </Icon>
3115 : <LatLonBox>
3116 : <north>%(north)f</north>
3117 : <south>%(south)f</south>
3118 : <east>%(east)f</east>
3119 : <west>%(west)f</west>
3120 : </LatLonBox>
3121 48 : )raw");
3122 :
3123 24 : if (!bIsRectangle)
3124 : {
3125 : s2 +=
3126 1 : R"raw( <gx:LatLonQuad><coordinates>%(LLX)f,%(LLY)f %(LRX)f,%(LRY)f %(TRX)f,%(TRY)f %(TLX)f,%(TLY)f</coordinates></gx:LatLonQuad>
3127 : )raw";
3128 : }
3129 :
3130 24 : s2 += R"raw( </GroundOverlay>
3131 : )raw";
3132 24 : substs["north"] = CPLSPrintf(pszFmt, std::max(dfTLY, dfTRY));
3133 24 : substs["south"] = CPLSPrintf(pszFmt, std::min(dfLLY, dfLRY));
3134 24 : substs["east"] = CPLSPrintf(pszFmt, std::max(dfTRX, dfLRX));
3135 24 : substs["west"] = CPLSPrintf(pszFmt, std::min(dfLLX, dfTLX));
3136 :
3137 24 : if (!bIsRectangle)
3138 : {
3139 1 : substs["TLX"] = CPLSPrintf(pszFmt, dfTLX);
3140 1 : substs["TLY"] = CPLSPrintf(pszFmt, dfTLY);
3141 1 : substs["TRX"] = CPLSPrintf(pszFmt, dfTRX);
3142 1 : substs["TRY"] = CPLSPrintf(pszFmt, dfTRY);
3143 1 : substs["LRX"] = CPLSPrintf(pszFmt, dfLRX);
3144 1 : substs["LRY"] = CPLSPrintf(pszFmt, dfLRY);
3145 1 : substs["LLX"] = CPLSPrintf(pszFmt, dfLLX);
3146 1 : substs["LLY"] = CPLSPrintf(pszFmt, dfLLY);
3147 : }
3148 :
3149 24 : ApplySubstitutions(s2, substs);
3150 24 : s += s2;
3151 : }
3152 :
3153 54 : for (const auto &child : children)
3154 : {
3155 24 : substs["tx"] = CPLSPrintf("%d", child.nTileX);
3156 24 : substs["tz"] = CPLSPrintf("%d", child.nTileZ);
3157 48 : substs["realtiley"] = CPLSPrintf(
3158 24 : "%d", GetFileY(child.nTileY, poTMS->tileMatrixList()[child.nTileZ],
3159 48 : convention));
3160 :
3161 24 : GetTileBoundingBox(child.nTileX, child.nTileY, child.nTileZ, poTMS,
3162 : bInvertAxisTMS, poCTToWGS84, dfTLX, dfTLY, dfTRX,
3163 : dfTRY, dfLLX, dfLLY, dfLRX, dfLRY);
3164 :
3165 : CPLString s2(R"raw( <NetworkLink>
3166 : <name>%(tz)d/%(tx)d/%(realtiley)d.%(tileformat)s</name>
3167 : <Region>
3168 : <LatLonAltBox>
3169 : <north>%(north)f</north>
3170 : <south>%(south)f</south>
3171 : <east>%(east)f</east>
3172 : <west>%(west)f</west>
3173 : </LatLonAltBox>
3174 : <Lod>
3175 : <minLodPixels>%(minlodpixels)d</minLodPixels>
3176 : <maxLodPixels>-1</maxLodPixels>
3177 : </Lod>
3178 : </Region>
3179 : <Link>
3180 : <href>%(url)s%(tz)d/%(tx)d/%(realtiley)d.kml</href>
3181 : <viewRefreshMode>onRegion</viewRefreshMode>
3182 : <viewFormat/>
3183 : </Link>
3184 : </NetworkLink>
3185 48 : )raw");
3186 24 : substs["north"] = CPLSPrintf(pszFmt, std::max(dfTLY, dfTRY));
3187 24 : substs["south"] = CPLSPrintf(pszFmt, std::min(dfLLY, dfLRY));
3188 24 : substs["east"] = CPLSPrintf(pszFmt, std::max(dfTRX, dfLRX));
3189 24 : substs["west"] = CPLSPrintf(pszFmt, std::min(dfLLX, dfTLX));
3190 24 : ApplySubstitutions(s2, substs);
3191 24 : s += s2;
3192 : }
3193 :
3194 30 : s += R"raw(</Document>
3195 : </kml>)raw";
3196 :
3197 60 : std::string osFilename(osDirectory);
3198 30 : if (!bIsTileKML)
3199 : {
3200 : osFilename =
3201 6 : CPLFormFilenameSafe(osFilename.c_str(), "doc.kml", nullptr);
3202 : }
3203 : else
3204 : {
3205 48 : osFilename = CPLFormFilenameSafe(osFilename.c_str(),
3206 24 : CPLSPrintf("%d", nTileZ), nullptr);
3207 48 : osFilename = CPLFormFilenameSafe(osFilename.c_str(),
3208 24 : CPLSPrintf("%d", nTileX), nullptr);
3209 48 : osFilename = CPLFormFilenameSafe(osFilename.c_str(),
3210 24 : CPLSPrintf("%d.kml", nFileY), nullptr);
3211 : }
3212 :
3213 30 : VSILFILE *f = VSIFOpenL(osFilename.c_str(), "wb");
3214 30 : if (f)
3215 : {
3216 30 : VSIFWriteL(s.data(), 1, s.size(), f);
3217 30 : VSIFCloseL(f);
3218 : }
3219 30 : }
3220 :
3221 : namespace
3222 : {
3223 :
3224 : /************************************************************************/
3225 : /* ResourceManager */
3226 : /************************************************************************/
3227 :
3228 : // Generic cache managing resources
3229 : template <class Resource> class ResourceManager /* non final */
3230 : {
3231 : public:
3232 230 : virtual ~ResourceManager() = default;
3233 :
3234 48 : std::unique_ptr<Resource> AcquireResources()
3235 : {
3236 96 : std::lock_guard oLock(m_oMutex);
3237 48 : if (!m_oResources.empty())
3238 : {
3239 0 : auto ret = std::move(m_oResources.back());
3240 0 : m_oResources.pop_back();
3241 0 : return ret;
3242 : }
3243 :
3244 48 : return CreateResources();
3245 : }
3246 :
3247 48 : void ReleaseResources(std::unique_ptr<Resource> resources)
3248 : {
3249 96 : std::lock_guard oLock(m_oMutex);
3250 48 : m_oResources.push_back(std::move(resources));
3251 48 : }
3252 :
3253 0 : void SetError()
3254 : {
3255 0 : std::lock_guard oLock(m_oMutex);
3256 0 : if (m_errorMsg.empty())
3257 0 : m_errorMsg = CPLGetLastErrorMsg();
3258 0 : }
3259 :
3260 12 : const std::string &GetErrorMsg() const
3261 : {
3262 12 : std::lock_guard oLock(m_oMutex);
3263 24 : return m_errorMsg;
3264 : }
3265 :
3266 : protected:
3267 : virtual std::unique_ptr<Resource> CreateResources() = 0;
3268 :
3269 : private:
3270 : mutable std::mutex m_oMutex{};
3271 : std::vector<std::unique_ptr<Resource>> m_oResources{};
3272 : std::string m_errorMsg{};
3273 : };
3274 :
3275 : /************************************************************************/
3276 : /* PerThreadMaxZoomResources */
3277 : /************************************************************************/
3278 :
3279 : // Per-thread resources for generation of tiles at full resolution
3280 : struct PerThreadMaxZoomResources
3281 : {
3282 : struct GDALDatasetReleaser
3283 : {
3284 24 : void operator()(GDALDataset *poDS)
3285 : {
3286 24 : if (poDS)
3287 24 : poDS->ReleaseRef();
3288 24 : }
3289 : };
3290 :
3291 : std::unique_ptr<GDALDataset, GDALDatasetReleaser> poSrcDS{};
3292 : std::vector<GByte> dstBuffer{};
3293 : std::unique_ptr<FakeMaxZoomDataset> poFakeMaxZoomDS{};
3294 : std::unique_ptr<void, decltype(&GDALDestroyTransformer)> poTransformer{
3295 : nullptr, GDALDestroyTransformer};
3296 : std::unique_ptr<GDALWarpOperation> poWO{};
3297 : };
3298 :
3299 : /************************************************************************/
3300 : /* PerThreadMaxZoomResourceManager */
3301 : /************************************************************************/
3302 :
3303 : // Manage a cache of PerThreadMaxZoomResources instances
3304 : class PerThreadMaxZoomResourceManager final
3305 : : public ResourceManager<PerThreadMaxZoomResources>
3306 : {
3307 : public:
3308 132 : PerThreadMaxZoomResourceManager(GDALDataset *poSrcDS,
3309 : const GDALWarpOptions *psWO,
3310 : void *pTransformerArg,
3311 : const FakeMaxZoomDataset &oFakeMaxZoomDS,
3312 : size_t nBufferSize)
3313 132 : : m_poSrcDS(poSrcDS), m_psWOSource(psWO),
3314 : m_pTransformerArg(pTransformerArg), m_oFakeMaxZoomDS(oFakeMaxZoomDS),
3315 132 : m_nBufferSize(nBufferSize)
3316 : {
3317 132 : }
3318 :
3319 : protected:
3320 24 : std::unique_ptr<PerThreadMaxZoomResources> CreateResources() override
3321 : {
3322 48 : auto ret = std::make_unique<PerThreadMaxZoomResources>();
3323 :
3324 24 : ret->poSrcDS.reset(GDALGetThreadSafeDataset(m_poSrcDS, GDAL_OF_RASTER));
3325 24 : if (!ret->poSrcDS)
3326 0 : return nullptr;
3327 :
3328 : try
3329 : {
3330 24 : ret->dstBuffer.resize(m_nBufferSize);
3331 : }
3332 0 : catch (const std::exception &)
3333 : {
3334 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
3335 : "Out of memory allocating temporary buffer");
3336 0 : return nullptr;
3337 : }
3338 :
3339 24 : ret->poFakeMaxZoomDS = m_oFakeMaxZoomDS.Clone(ret->dstBuffer);
3340 :
3341 24 : ret->poTransformer.reset(GDALCloneTransformer(m_pTransformerArg));
3342 24 : if (!ret->poTransformer)
3343 0 : return nullptr;
3344 :
3345 : auto psWO =
3346 : std::unique_ptr<GDALWarpOptions, decltype(&GDALDestroyWarpOptions)>(
3347 48 : GDALCloneWarpOptions(m_psWOSource), GDALDestroyWarpOptions);
3348 24 : if (!psWO)
3349 0 : return nullptr;
3350 :
3351 24 : psWO->hSrcDS = GDALDataset::ToHandle(ret->poSrcDS.get());
3352 24 : psWO->hDstDS = GDALDataset::ToHandle(ret->poFakeMaxZoomDS.get());
3353 24 : psWO->pTransformerArg = ret->poTransformer.get();
3354 24 : psWO->pfnTransformer = m_psWOSource->pfnTransformer;
3355 :
3356 24 : ret->poWO = std::make_unique<GDALWarpOperation>();
3357 24 : if (ret->poWO->Initialize(psWO.get()) != CE_None)
3358 0 : return nullptr;
3359 :
3360 24 : return ret;
3361 : }
3362 :
3363 : private:
3364 : GDALDataset *const m_poSrcDS;
3365 : const GDALWarpOptions *const m_psWOSource;
3366 : void *const m_pTransformerArg;
3367 : const FakeMaxZoomDataset &m_oFakeMaxZoomDS;
3368 : const size_t m_nBufferSize;
3369 :
3370 : CPL_DISALLOW_COPY_ASSIGN(PerThreadMaxZoomResourceManager)
3371 : };
3372 :
3373 : /************************************************************************/
3374 : /* PerThreadLowerZoomResources */
3375 : /************************************************************************/
3376 :
3377 : // Per-thread resources for generation of tiles at zoom level < max
3378 : struct PerThreadLowerZoomResources
3379 : {
3380 : std::unique_ptr<GDALDataset> poSrcDS{};
3381 : };
3382 :
3383 : /************************************************************************/
3384 : /* PerThreadLowerZoomResourceManager */
3385 : /************************************************************************/
3386 :
3387 : // Manage a cache of PerThreadLowerZoomResources instances
3388 : class PerThreadLowerZoomResourceManager final
3389 : : public ResourceManager<PerThreadLowerZoomResources>
3390 : {
3391 : public:
3392 98 : explicit PerThreadLowerZoomResourceManager(const MosaicDataset &oSrcDS)
3393 98 : : m_oSrcDS(oSrcDS)
3394 : {
3395 98 : }
3396 :
3397 : protected:
3398 24 : std::unique_ptr<PerThreadLowerZoomResources> CreateResources() override
3399 : {
3400 24 : auto ret = std::make_unique<PerThreadLowerZoomResources>();
3401 24 : ret->poSrcDS = m_oSrcDS.Clone();
3402 24 : return ret;
3403 : }
3404 :
3405 : private:
3406 : const MosaicDataset &m_oSrcDS;
3407 : };
3408 :
3409 : } // namespace
3410 :
3411 : /************************************************************************/
3412 : /* GDALRasterTileAlgorithm::ValidateOutputFormat() */
3413 : /************************************************************************/
3414 :
3415 197 : bool GDALRasterTileAlgorithm::ValidateOutputFormat(GDALDataType eSrcDT) const
3416 : {
3417 197 : if (m_format == "PNG")
3418 : {
3419 170 : if (m_poSrcDS->GetRasterCount() > 4)
3420 : {
3421 1 : ReportError(CE_Failure, CPLE_NotSupported,
3422 : "Only up to 4 bands supported for PNG.");
3423 1 : return false;
3424 : }
3425 169 : if (eSrcDT != GDT_UInt8 && eSrcDT != GDT_UInt16)
3426 : {
3427 1 : ReportError(CE_Failure, CPLE_NotSupported,
3428 : "Only Byte and UInt16 data types supported for PNG.");
3429 1 : return false;
3430 : }
3431 : }
3432 27 : else if (m_format == "JPEG")
3433 : {
3434 7 : if (m_poSrcDS->GetRasterCount() > 4)
3435 : {
3436 1 : ReportError(
3437 : CE_Failure, CPLE_NotSupported,
3438 : "Only up to 4 bands supported for JPEG (with alpha ignored).");
3439 1 : return false;
3440 : }
3441 : const bool bUInt16Supported =
3442 6 : strstr(m_poDstDriver->GetMetadataItem(GDAL_DMD_CREATIONDATATYPES),
3443 6 : "UInt16");
3444 6 : if (eSrcDT != GDT_UInt8 && !(eSrcDT == GDT_UInt16 && bUInt16Supported))
3445 : {
3446 1 : ReportError(
3447 : CE_Failure, CPLE_NotSupported,
3448 : bUInt16Supported
3449 : ? "Only Byte and UInt16 data types supported for JPEG."
3450 : : "Only Byte data type supported for JPEG.");
3451 1 : return false;
3452 : }
3453 5 : if (eSrcDT == GDT_UInt16)
3454 : {
3455 3 : if (const char *pszNBITS =
3456 6 : m_poSrcDS->GetRasterBand(1)->GetMetadataItem(
3457 3 : "NBITS", "IMAGE_STRUCTURE"))
3458 : {
3459 1 : if (atoi(pszNBITS) > 12)
3460 : {
3461 1 : ReportError(CE_Failure, CPLE_NotSupported,
3462 : "JPEG output only supported up to 12 bits");
3463 1 : return false;
3464 : }
3465 : }
3466 : else
3467 : {
3468 2 : double adfMinMax[2] = {0, 0};
3469 2 : m_poSrcDS->GetRasterBand(1)->ComputeRasterMinMax(
3470 2 : /* bApproxOK = */ true, adfMinMax);
3471 2 : if (adfMinMax[1] >= (1 << 12))
3472 : {
3473 1 : ReportError(CE_Failure, CPLE_NotSupported,
3474 : "JPEG output only supported up to 12 bits");
3475 1 : return false;
3476 : }
3477 : }
3478 : }
3479 : }
3480 20 : else if (m_format == "WEBP")
3481 : {
3482 4 : if (m_poSrcDS->GetRasterCount() != 3 &&
3483 1 : m_poSrcDS->GetRasterCount() != 4)
3484 : {
3485 1 : ReportError(CE_Failure, CPLE_NotSupported,
3486 : "Only 3 or 4 bands supported for WEBP.");
3487 1 : return false;
3488 : }
3489 2 : if (eSrcDT != GDT_UInt8)
3490 : {
3491 1 : ReportError(CE_Failure, CPLE_NotSupported,
3492 : "Only Byte data type supported for WEBP.");
3493 1 : return false;
3494 : }
3495 : }
3496 189 : return true;
3497 : }
3498 :
3499 : /************************************************************************/
3500 : /* GDALRasterTileAlgorithm::ComputeJobChunkSize() */
3501 : /************************************************************************/
3502 :
3503 : // Given a number of tiles in the Y dimension being nTilesPerCol and
3504 : // in the X dimension being nTilesPerRow, compute the (upper bound of)
3505 : // number of jobs needed to be nYOuterIterations x nXOuterIterations,
3506 : // with each job processing in average dfTilesYPerJob x dfTilesXPerJob
3507 : // tiles.
3508 : /* static */
3509 31 : void GDALRasterTileAlgorithm::ComputeJobChunkSize(
3510 : int nMaxJobCount, int nTilesPerCol, int nTilesPerRow,
3511 : double &dfTilesYPerJob, int &nYOuterIterations, double &dfTilesXPerJob,
3512 : int &nXOuterIterations)
3513 : {
3514 31 : CPLAssert(nMaxJobCount >= 1);
3515 31 : dfTilesYPerJob = static_cast<double>(nTilesPerCol) / nMaxJobCount;
3516 31 : nYOuterIterations = dfTilesYPerJob >= 1 ? nMaxJobCount : 1;
3517 :
3518 62 : dfTilesXPerJob = dfTilesYPerJob >= 1
3519 31 : ? nTilesPerRow
3520 9 : : static_cast<double>(nTilesPerRow) / nMaxJobCount;
3521 31 : nXOuterIterations = dfTilesYPerJob >= 1 ? 1 : nMaxJobCount;
3522 :
3523 31 : if (dfTilesYPerJob < 1 && dfTilesXPerJob < 1 &&
3524 9 : nTilesPerCol <= nMaxJobCount / nTilesPerRow)
3525 : {
3526 9 : dfTilesYPerJob = 1;
3527 9 : dfTilesXPerJob = 1;
3528 9 : nYOuterIterations = nTilesPerCol;
3529 9 : nXOuterIterations = nTilesPerRow;
3530 : }
3531 31 : }
3532 :
3533 : /************************************************************************/
3534 : /* GDALRasterTileAlgorithm::AddArgToArgv() */
3535 : /************************************************************************/
3536 :
3537 208 : bool GDALRasterTileAlgorithm::AddArgToArgv(const GDALAlgorithmArg *arg,
3538 : CPLStringList &aosArgv) const
3539 : {
3540 208 : aosArgv.push_back(CPLSPrintf("--%s", arg->GetName().c_str()));
3541 208 : if (arg->GetType() == GAAT_STRING)
3542 : {
3543 74 : aosArgv.push_back(arg->Get<std::string>().c_str());
3544 : }
3545 134 : else if (arg->GetType() == GAAT_STRING_LIST)
3546 : {
3547 12 : bool bFirst = true;
3548 24 : for (const std::string &s : arg->Get<std::vector<std::string>>())
3549 : {
3550 12 : if (!bFirst)
3551 : {
3552 0 : aosArgv.push_back(CPLSPrintf("--%s", arg->GetName().c_str()));
3553 : }
3554 12 : bFirst = false;
3555 12 : aosArgv.push_back(s.c_str());
3556 : }
3557 : }
3558 122 : else if (arg->GetType() == GAAT_REAL)
3559 : {
3560 0 : aosArgv.push_back(CPLSPrintf("%.17g", arg->Get<double>()));
3561 : }
3562 122 : else if (arg->GetType() == GAAT_INTEGER)
3563 : {
3564 122 : aosArgv.push_back(CPLSPrintf("%d", arg->Get<int>()));
3565 : }
3566 0 : else if (arg->GetType() != GAAT_BOOLEAN)
3567 : {
3568 0 : ReportError(CE_Failure, CPLE_AppDefined,
3569 : "Bug: argument of type %d not handled "
3570 : "by gdal raster tile!",
3571 0 : static_cast<int>(arg->GetType()));
3572 0 : return false;
3573 : }
3574 208 : return true;
3575 : }
3576 :
3577 : /************************************************************************/
3578 : /* GDALRasterTileAlgorithm::IsCompatibleOfSpawn() */
3579 : /************************************************************************/
3580 :
3581 15 : bool GDALRasterTileAlgorithm::IsCompatibleOfSpawn(const char *&pszErrorMsg)
3582 : {
3583 15 : pszErrorMsg = "";
3584 15 : if (!m_bIsNamedNonMemSrcDS)
3585 : {
3586 1 : pszErrorMsg = "Unnamed or memory dataset sources are not supported "
3587 : "with spawn parallelization method";
3588 1 : return false;
3589 : }
3590 14 : if (cpl::starts_with(m_output, "/vsimem/"))
3591 : {
3592 4 : pszErrorMsg = "/vsimem/ output directory not supported with spawn "
3593 : "parallelization method";
3594 4 : return false;
3595 : }
3596 :
3597 10 : if (m_osGDALPath.empty())
3598 10 : m_osGDALPath = GDALGetGDALPath();
3599 10 : return !(m_osGDALPath.empty());
3600 : }
3601 :
3602 : /************************************************************************/
3603 : /* GetProgressForChildProcesses() */
3604 : /************************************************************************/
3605 :
3606 19 : static void GetProgressForChildProcesses(
3607 : bool &bRet, std::vector<CPLSpawnedProcess *> &ahSpawnedProcesses,
3608 : std::vector<uint64_t> &anRemainingTilesForProcess, uint64_t &nCurTile,
3609 : uint64_t nTotalTiles, GDALProgressFunc pfnProgress, void *pProgressData)
3610 : {
3611 19 : std::vector<unsigned int> anProgressState(ahSpawnedProcesses.size(), 0);
3612 19 : std::vector<unsigned int> anEndState(ahSpawnedProcesses.size(), 0);
3613 19 : std::vector<bool> abFinished(ahSpawnedProcesses.size(), false);
3614 19 : std::vector<unsigned int> anStartErrorState(ahSpawnedProcesses.size(), 0);
3615 :
3616 1957 : while (bRet)
3617 : {
3618 1957 : size_t iProcess = 0;
3619 1957 : size_t nFinished = 0;
3620 9657 : for (CPLSpawnedProcess *hSpawnedProcess : ahSpawnedProcesses)
3621 : {
3622 7700 : char ch = 0;
3623 23100 : if (abFinished[iProcess] ||
3624 7700 : !CPLPipeRead(CPLSpawnAsyncGetInputFileHandle(hSpawnedProcess),
3625 7700 : &ch, 1))
3626 : {
3627 0 : ++nFinished;
3628 : }
3629 7700 : else if (ch == PROGRESS_MARKER[anProgressState[iProcess]])
3630 : {
3631 1518 : ++anProgressState[iProcess];
3632 1518 : if (anProgressState[iProcess] == sizeof(PROGRESS_MARKER))
3633 : {
3634 506 : anProgressState[iProcess] = 0;
3635 506 : --anRemainingTilesForProcess[iProcess];
3636 506 : ++nCurTile;
3637 506 : if (bRet && pfnProgress)
3638 : {
3639 84 : if (!pfnProgress(static_cast<double>(nCurTile) /
3640 84 : static_cast<double>(nTotalTiles),
3641 : "", pProgressData))
3642 : {
3643 0 : CPLError(CE_Failure, CPLE_UserInterrupt,
3644 : "Process interrupted by user");
3645 0 : bRet = false;
3646 0 : return;
3647 : }
3648 : }
3649 : }
3650 : }
3651 6182 : else if (ch == END_MARKER[anEndState[iProcess]])
3652 : {
3653 518 : ++anEndState[iProcess];
3654 518 : if (anEndState[iProcess] == sizeof(END_MARKER))
3655 : {
3656 74 : anEndState[iProcess] = 0;
3657 74 : abFinished[iProcess] = true;
3658 74 : ++nFinished;
3659 : }
3660 : }
3661 5664 : else if (ch == ERROR_START_MARKER[anStartErrorState[iProcess]])
3662 : {
3663 5580 : ++anStartErrorState[iProcess];
3664 5580 : if (anStartErrorState[iProcess] == sizeof(ERROR_START_MARKER))
3665 : {
3666 310 : anStartErrorState[iProcess] = 0;
3667 310 : uint32_t nErr = 0;
3668 310 : CPLPipeRead(
3669 : CPLSpawnAsyncGetInputFileHandle(hSpawnedProcess), &nErr,
3670 : sizeof(nErr));
3671 310 : uint32_t nNum = 0;
3672 310 : CPLPipeRead(
3673 : CPLSpawnAsyncGetInputFileHandle(hSpawnedProcess), &nNum,
3674 : sizeof(nNum));
3675 310 : uint16_t nMsgLen = 0;
3676 310 : CPLPipeRead(
3677 : CPLSpawnAsyncGetInputFileHandle(hSpawnedProcess),
3678 : &nMsgLen, sizeof(nMsgLen));
3679 620 : std::string osMsg;
3680 310 : osMsg.resize(nMsgLen);
3681 310 : CPLPipeRead(
3682 : CPLSpawnAsyncGetInputFileHandle(hSpawnedProcess),
3683 310 : &osMsg[0], nMsgLen);
3684 310 : if (nErr <= CE_Fatal &&
3685 310 : nNum <= CPLE_ObjectStorageGenericError)
3686 : {
3687 310 : bool bDone = false;
3688 310 : if (nErr == CE_Debug)
3689 : {
3690 304 : auto nPos = osMsg.find(": ");
3691 304 : if (nPos != std::string::npos)
3692 : {
3693 304 : bDone = true;
3694 608 : CPLDebug(
3695 608 : osMsg.substr(0, nPos).c_str(),
3696 : "subprocess %d: %s",
3697 : static_cast<int>(iProcess),
3698 608 : osMsg.substr(nPos + strlen(": ")).c_str());
3699 : }
3700 : }
3701 : // cppcheck-suppress knownConditionTrueFalse
3702 310 : if (!bDone)
3703 : {
3704 6 : CPLError(nErr == CE_Fatal
3705 : ? CE_Failure
3706 : : static_cast<CPLErr>(nErr),
3707 : static_cast<CPLErrorNum>(nNum),
3708 : "Sub-process %d: %s",
3709 : static_cast<int>(iProcess), osMsg.c_str());
3710 : }
3711 : }
3712 : }
3713 : }
3714 : else
3715 : {
3716 84 : CPLErrorOnce(
3717 : CE_Warning, CPLE_AppDefined,
3718 : "Spurious character detected on stdout of child process");
3719 84 : anProgressState[iProcess] = 0;
3720 84 : if (ch == PROGRESS_MARKER[anProgressState[iProcess]])
3721 : {
3722 84 : ++anProgressState[iProcess];
3723 : }
3724 : }
3725 7700 : ++iProcess;
3726 : }
3727 1957 : if (!bRet || nFinished == ahSpawnedProcesses.size())
3728 19 : break;
3729 : }
3730 : }
3731 :
3732 : /************************************************************************/
3733 : /* WaitForSpawnedProcesses() */
3734 : /************************************************************************/
3735 :
3736 19 : void GDALRasterTileAlgorithm::WaitForSpawnedProcesses(
3737 : bool &bRet, const std::vector<std::string> &asCommandLines,
3738 : std::vector<CPLSpawnedProcess *> &ahSpawnedProcesses) const
3739 : {
3740 19 : size_t iProcess = 0;
3741 93 : for (CPLSpawnedProcess *hSpawnedProcess : ahSpawnedProcesses)
3742 : {
3743 74 : CPL_IGNORE_RET_VAL(
3744 74 : CPLPipeWrite(CPLSpawnAsyncGetOutputFileHandle(hSpawnedProcess),
3745 : STOP_MARKER, static_cast<int>(strlen(STOP_MARKER))));
3746 :
3747 74 : char ch = 0;
3748 74 : std::string errorMsg;
3749 74 : while (CPLPipeRead(CPLSpawnAsyncGetErrorFileHandle(hSpawnedProcess),
3750 74 : &ch, 1))
3751 : {
3752 0 : if (ch == '\n')
3753 : {
3754 0 : if (!errorMsg.empty())
3755 : {
3756 0 : if (cpl::starts_with(errorMsg, "ERROR "))
3757 : {
3758 0 : const auto nPos = errorMsg.find(": ");
3759 0 : if (nPos != std::string::npos)
3760 0 : errorMsg = errorMsg.substr(nPos + 1);
3761 0 : ReportError(CE_Failure, CPLE_AppDefined, "%s",
3762 : errorMsg.c_str());
3763 : }
3764 : else
3765 : {
3766 0 : std::string osComp = "GDAL";
3767 0 : const auto nPos = errorMsg.find(": ");
3768 0 : if (nPos != std::string::npos)
3769 : {
3770 0 : osComp = errorMsg.substr(0, nPos);
3771 0 : errorMsg = errorMsg.substr(nPos + 1);
3772 : }
3773 0 : CPLDebug(osComp.c_str(), "%s", errorMsg.c_str());
3774 : }
3775 0 : errorMsg.clear();
3776 : }
3777 : }
3778 : else
3779 : {
3780 0 : errorMsg += ch;
3781 : }
3782 : }
3783 :
3784 74 : if (CPLSpawnAsyncFinish(hSpawnedProcess, /* bWait = */ true,
3785 74 : /* bKill = */ false) != 0)
3786 : {
3787 2 : bRet = false;
3788 2 : ReportError(CE_Failure, CPLE_AppDefined,
3789 : "Child process '%s' failed",
3790 2 : asCommandLines[iProcess].c_str());
3791 : }
3792 74 : ++iProcess;
3793 : }
3794 19 : }
3795 :
3796 : /************************************************************************/
3797 : /* GDALRasterTileAlgorithm::GetMaxChildCount() */
3798 : /**********************************f**************************************/
3799 :
3800 19 : int GDALRasterTileAlgorithm::GetMaxChildCount(int nMaxJobCount) const
3801 : {
3802 : #ifndef _WIN32
3803 : // Limit the number of jobs compared to how many file descriptors we have
3804 : // left
3805 : const int remainingFileDescriptorCount =
3806 19 : CPLGetRemainingFileDescriptorCount();
3807 19 : constexpr int SOME_MARGIN = 3;
3808 19 : constexpr int FD_PER_CHILD = 3; /* stdin, stdout and stderr */
3809 19 : if (FD_PER_CHILD * nMaxJobCount + SOME_MARGIN >
3810 : remainingFileDescriptorCount)
3811 : {
3812 0 : nMaxJobCount = std::max(
3813 0 : 1, (remainingFileDescriptorCount - SOME_MARGIN) / FD_PER_CHILD);
3814 0 : ReportError(
3815 : CE_Warning, CPLE_AppDefined,
3816 : "Limiting the number of child workers to %d (instead of %d), "
3817 : "because there are not enough file descriptors left (%d)",
3818 0 : nMaxJobCount, m_numThreads, remainingFileDescriptorCount);
3819 : }
3820 : #endif
3821 19 : return nMaxJobCount;
3822 : }
3823 :
3824 : /************************************************************************/
3825 : /* SendConfigOptions() */
3826 : /************************************************************************/
3827 :
3828 50 : static void SendConfigOptions(CPLSpawnedProcess *hSpawnedProcess, bool &bRet)
3829 : {
3830 : // Send most config options through pipe, to avoid leaking
3831 : // secrets when listing processes
3832 50 : auto handle = CPLSpawnAsyncGetOutputFileHandle(hSpawnedProcess);
3833 150 : for (auto pfnFunc : {&CPLGetConfigOptions, &CPLGetThreadLocalConfigOptions})
3834 : {
3835 200 : CPLStringList aosConfigOptions((*pfnFunc)());
3836 222 : for (const char *pszNameValue : aosConfigOptions)
3837 : {
3838 122 : if (!STARTS_WITH(pszNameValue, "GDAL_CACHEMAX") &&
3839 122 : !STARTS_WITH(pszNameValue, "GDAL_NUM_THREADS"))
3840 : {
3841 122 : constexpr const char *CONFIG_MARKER = "--config\n";
3842 122 : bRet &= CPL_TO_BOOL(
3843 : CPLPipeWrite(handle, CONFIG_MARKER,
3844 122 : static_cast<int>(strlen(CONFIG_MARKER))));
3845 122 : char *pszEscaped = CPLEscapeString(pszNameValue, -1, CPLES_URL);
3846 122 : bRet &= CPL_TO_BOOL(CPLPipeWrite(
3847 122 : handle, pszEscaped, static_cast<int>(strlen(pszEscaped))));
3848 122 : CPLFree(pszEscaped);
3849 122 : bRet &= CPL_TO_BOOL(CPLPipeWrite(handle, "\n", 1));
3850 : }
3851 : }
3852 : }
3853 50 : constexpr const char *END_CONFIG_MARKER = "END_CONFIG\n";
3854 50 : bRet &=
3855 50 : CPL_TO_BOOL(CPLPipeWrite(handle, END_CONFIG_MARKER,
3856 50 : static_cast<int>(strlen(END_CONFIG_MARKER))));
3857 50 : }
3858 :
3859 : /************************************************************************/
3860 : /* GenerateTilesForkMethod() */
3861 : /************************************************************************/
3862 :
3863 : #ifdef FORK_ALLOWED
3864 :
3865 : namespace
3866 : {
3867 : struct ForkWorkStructure
3868 : {
3869 : uint64_t nCacheMaxPerProcess = 0;
3870 : CPLStringList aosArgv{};
3871 : GDALDataset *poMemSrcDS{};
3872 : };
3873 : } // namespace
3874 :
3875 : static CPL_FILE_HANDLE pipeIn = CPL_FILE_INVALID_HANDLE;
3876 : static CPL_FILE_HANDLE pipeOut = CPL_FILE_INVALID_HANDLE;
3877 :
3878 0 : static int GenerateTilesForkMethod(CPL_FILE_HANDLE in, CPL_FILE_HANDLE out)
3879 : {
3880 0 : pipeIn = in;
3881 0 : pipeOut = out;
3882 :
3883 0 : const ForkWorkStructure *pWorkStructure = nullptr;
3884 0 : CPLPipeRead(in, &pWorkStructure, sizeof(pWorkStructure));
3885 :
3886 0 : CPLSetConfigOption("GDAL_NUM_THREADS", "1");
3887 0 : GDALSetCacheMax64(pWorkStructure->nCacheMaxPerProcess);
3888 :
3889 0 : GDALRasterTileAlgorithmStandalone alg;
3890 0 : if (pWorkStructure->poMemSrcDS)
3891 : {
3892 0 : auto *inputArg = alg.GetArg(GDAL_ARG_NAME_INPUT);
3893 0 : std::vector<GDALArgDatasetValue> val;
3894 0 : val.resize(1);
3895 0 : val[0].Set(pWorkStructure->poMemSrcDS);
3896 0 : inputArg->Set(std::move(val));
3897 : }
3898 0 : return alg.ParseCommandLineArguments(pWorkStructure->aosArgv) && alg.Run()
3899 0 : ? 0
3900 0 : : 1;
3901 : }
3902 :
3903 : #endif // FORK_ALLOWED
3904 :
3905 : /************************************************************************/
3906 : /* GDALRasterTileAlgorithm::GenerateBaseTilesSpawnMethod() */
3907 : /************************************************************************/
3908 :
3909 7 : bool GDALRasterTileAlgorithm::GenerateBaseTilesSpawnMethod(
3910 : int nBaseTilesPerCol, int nBaseTilesPerRow, int nMinTileX, int nMinTileY,
3911 : int nMaxTileX, int nMaxTileY, uint64_t nTotalTiles, uint64_t nBaseTiles,
3912 : GDALProgressFunc pfnProgress, void *pProgressData)
3913 : {
3914 7 : if (m_parallelMethod == "spawn")
3915 : {
3916 5 : CPLAssert(!m_osGDALPath.empty());
3917 : }
3918 :
3919 7 : const int nMaxJobCount = GetMaxChildCount(std::max(
3920 0 : 1, static_cast<int>(std::min<uint64_t>(
3921 7 : m_numThreads, nBaseTiles / GetThresholdMinTilesPerJob()))));
3922 :
3923 : double dfTilesYPerJob;
3924 : int nYOuterIterations;
3925 : double dfTilesXPerJob;
3926 : int nXOuterIterations;
3927 7 : ComputeJobChunkSize(nMaxJobCount, nBaseTilesPerCol, nBaseTilesPerRow,
3928 : dfTilesYPerJob, nYOuterIterations, dfTilesXPerJob,
3929 : nXOuterIterations);
3930 :
3931 7 : CPLDebugOnly("gdal_raster_tile",
3932 : "nYOuterIterations=%d, dfTilesYPerJob=%g, "
3933 : "nXOuterIterations=%d, dfTilesXPerJob=%g",
3934 : nYOuterIterations, dfTilesYPerJob, nXOuterIterations,
3935 : dfTilesXPerJob);
3936 :
3937 14 : std::vector<std::string> asCommandLines;
3938 14 : std::vector<CPLSpawnedProcess *> ahSpawnedProcesses;
3939 14 : std::vector<uint64_t> anRemainingTilesForProcess;
3940 :
3941 7 : const uint64_t nCacheMaxPerProcess = GDALGetCacheMax64() / nMaxJobCount;
3942 :
3943 7 : const auto poSrcDriver = m_poSrcDS->GetDriver();
3944 : const bool bIsMEMSource =
3945 7 : poSrcDriver && EQUAL(poSrcDriver->GetDescription(), "MEM");
3946 :
3947 7 : int nLastYEndIncluded = nMinTileY - 1;
3948 :
3949 : #ifdef FORK_ALLOWED
3950 14 : std::vector<std::unique_ptr<ForkWorkStructure>> forkWorkStructures;
3951 : #endif
3952 :
3953 7 : bool bRet = true;
3954 33 : for (int iYOuterIter = 0; bRet && iYOuterIter < nYOuterIterations &&
3955 26 : nLastYEndIncluded < nMaxTileY;
3956 : ++iYOuterIter)
3957 : {
3958 26 : const int iYStart = nLastYEndIncluded + 1;
3959 : const int iYEndIncluded =
3960 26 : iYOuterIter + 1 == nYOuterIterations
3961 45 : ? nMaxTileY
3962 : : std::max(
3963 : iYStart,
3964 45 : static_cast<int>(std::floor(
3965 19 : nMinTileY + (iYOuterIter + 1) * dfTilesYPerJob - 1)));
3966 :
3967 26 : nLastYEndIncluded = iYEndIncluded;
3968 :
3969 26 : int nLastXEndIncluded = nMinTileX - 1;
3970 52 : for (int iXOuterIter = 0; bRet && iXOuterIter < nXOuterIterations &&
3971 26 : nLastXEndIncluded < nMaxTileX;
3972 : ++iXOuterIter)
3973 : {
3974 26 : const int iXStart = nLastXEndIncluded + 1;
3975 : const int iXEndIncluded =
3976 26 : iXOuterIter + 1 == nXOuterIterations
3977 26 : ? nMaxTileX
3978 : : std::max(iXStart,
3979 26 : static_cast<int>(std::floor(
3980 0 : nMinTileX +
3981 0 : (iXOuterIter + 1) * dfTilesXPerJob - 1)));
3982 :
3983 26 : nLastXEndIncluded = iXEndIncluded;
3984 :
3985 26 : anRemainingTilesForProcess.push_back(
3986 0 : static_cast<uint64_t>(iYEndIncluded - iYStart + 1) *
3987 26 : (iXEndIncluded - iXStart + 1));
3988 :
3989 26 : CPLStringList aosArgv;
3990 26 : if (m_parallelMethod == "spawn")
3991 : {
3992 18 : aosArgv.push_back(m_osGDALPath.c_str());
3993 18 : aosArgv.push_back("raster");
3994 18 : aosArgv.push_back("tile");
3995 18 : aosArgv.push_back("--config-options-in-stdin");
3996 18 : aosArgv.push_back("--config");
3997 18 : aosArgv.push_back("GDAL_NUM_THREADS=1");
3998 18 : aosArgv.push_back("--config");
3999 18 : aosArgv.push_back(
4000 : CPLSPrintf("GDAL_CACHEMAX=%" PRIu64, nCacheMaxPerProcess));
4001 : }
4002 26 : aosArgv.push_back(
4003 52 : std::string("--").append(GDAL_ARG_NAME_NUM_THREADS).c_str());
4004 26 : aosArgv.push_back("1");
4005 26 : aosArgv.push_back("--min-x");
4006 26 : aosArgv.push_back(CPLSPrintf("%d", iXStart));
4007 26 : aosArgv.push_back("--max-x");
4008 26 : aosArgv.push_back(CPLSPrintf("%d", iXEndIncluded));
4009 26 : aosArgv.push_back("--min-y");
4010 26 : aosArgv.push_back(CPLSPrintf("%d", iYStart));
4011 26 : aosArgv.push_back("--max-y");
4012 26 : aosArgv.push_back(CPLSPrintf("%d", iYEndIncluded));
4013 26 : aosArgv.push_back("--webviewer");
4014 26 : aosArgv.push_back("none");
4015 26 : aosArgv.push_back(m_parallelMethod == "spawn" ? "--spawned"
4016 : : "--forked");
4017 26 : if (!bIsMEMSource)
4018 : {
4019 22 : aosArgv.push_back("--input");
4020 22 : aosArgv.push_back(m_poSrcDS->GetDescription());
4021 : }
4022 1362 : for (const auto &arg : GetArgs())
4023 : {
4024 1564 : if (arg->IsExplicitlySet() && arg->GetName() != "min-x" &&
4025 342 : arg->GetName() != "min-y" && arg->GetName() != "max-x" &&
4026 318 : arg->GetName() != "max-y" && arg->GetName() != "min-zoom" &&
4027 180 : arg->GetName() != "progress" &&
4028 180 : arg->GetName() != "progress-forked" &&
4029 158 : arg->GetName() != GDAL_ARG_NAME_INPUT &&
4030 134 : arg->GetName() != GDAL_ARG_NAME_NUM_THREADS &&
4031 1516 : arg->GetName() != "webviewer" &&
4032 66 : arg->GetName() != "parallel-method")
4033 : {
4034 56 : if (!AddArgToArgv(arg.get(), aosArgv))
4035 0 : return false;
4036 : }
4037 : }
4038 :
4039 26 : std::string cmdLine;
4040 664 : for (const char *arg : aosArgv)
4041 : {
4042 638 : if (!cmdLine.empty())
4043 612 : cmdLine += ' ';
4044 1276 : CPLString sArg(arg);
4045 638 : if (sArg.find_first_of(" \"") != std::string::npos)
4046 : {
4047 4 : cmdLine += '"';
4048 4 : cmdLine += sArg.replaceAll('"', "\\\"");
4049 4 : cmdLine += '"';
4050 : }
4051 : else
4052 634 : cmdLine += sArg;
4053 : }
4054 26 : CPLDebugOnly("gdal_raster_tile", "%s %s",
4055 : m_parallelMethod == "spawn" ? "Spawning" : "Forking",
4056 : cmdLine.c_str());
4057 26 : asCommandLines.push_back(std::move(cmdLine));
4058 :
4059 : #ifdef FORK_ALLOWED
4060 26 : if (m_parallelMethod == "fork")
4061 : {
4062 8 : forkWorkStructures.push_back(
4063 16 : std::make_unique<ForkWorkStructure>());
4064 8 : ForkWorkStructure *pData = forkWorkStructures.back().get();
4065 8 : pData->nCacheMaxPerProcess = nCacheMaxPerProcess;
4066 8 : pData->aosArgv = aosArgv;
4067 8 : if (bIsMEMSource)
4068 4 : pData->poMemSrcDS = m_poSrcDS;
4069 : }
4070 26 : CPL_IGNORE_RET_VAL(aosArgv);
4071 : #endif
4072 :
4073 52 : CPLSpawnedProcess *hSpawnedProcess = CPLSpawnAsync(
4074 : #ifdef FORK_ALLOWED
4075 26 : m_parallelMethod == "fork" ? GenerateTilesForkMethod :
4076 : #endif
4077 : nullptr,
4078 44 : m_parallelMethod == "fork" ? nullptr : aosArgv.List(),
4079 : /* bCreateInputPipe = */ true,
4080 : /* bCreateOutputPipe = */ true,
4081 26 : /* bCreateErrorPipe = */ false, nullptr);
4082 26 : if (!hSpawnedProcess)
4083 : {
4084 0 : ReportError(CE_Failure, CPLE_AppDefined,
4085 : "Spawning child gdal process '%s' failed",
4086 0 : asCommandLines.back().c_str());
4087 0 : bRet = false;
4088 0 : break;
4089 : }
4090 :
4091 26 : CPLDebugOnly("gdal_raster_tile",
4092 : "Job for y in [%d,%d] and x in [%d,%d], "
4093 : "run by process %" PRIu64,
4094 : iYStart, iYEndIncluded, iXStart, iXEndIncluded,
4095 : static_cast<uint64_t>(
4096 : CPLSpawnAsyncGetChildProcessId(hSpawnedProcess)));
4097 :
4098 26 : ahSpawnedProcesses.push_back(hSpawnedProcess);
4099 :
4100 26 : if (m_parallelMethod == "spawn")
4101 : {
4102 18 : SendConfigOptions(hSpawnedProcess, bRet);
4103 : }
4104 :
4105 : #ifdef FORK_ALLOWED
4106 : else
4107 : {
4108 8 : ForkWorkStructure *pData = forkWorkStructures.back().get();
4109 8 : auto handle = CPLSpawnAsyncGetOutputFileHandle(hSpawnedProcess);
4110 8 : bRet &= CPL_TO_BOOL(CPLPipeWrite(
4111 8 : handle, &pData, static_cast<int>(sizeof(pData))));
4112 : }
4113 : #endif
4114 :
4115 26 : if (!bRet)
4116 : {
4117 0 : ReportError(CE_Failure, CPLE_AppDefined,
4118 : "Could not transmit config options to child gdal "
4119 : "process '%s'",
4120 0 : asCommandLines.back().c_str());
4121 0 : break;
4122 : }
4123 : }
4124 : }
4125 :
4126 7 : uint64_t nCurTile = 0;
4127 7 : GetProgressForChildProcesses(bRet, ahSpawnedProcesses,
4128 : anRemainingTilesForProcess, nCurTile,
4129 : nTotalTiles, pfnProgress, pProgressData);
4130 :
4131 7 : WaitForSpawnedProcesses(bRet, asCommandLines, ahSpawnedProcesses);
4132 :
4133 7 : if (bRet && nCurTile != nBaseTiles)
4134 : {
4135 0 : bRet = false;
4136 0 : ReportError(CE_Failure, CPLE_AppDefined,
4137 : "Not all tiles at max zoom level have been "
4138 : "generated. Got %" PRIu64 ", expected %" PRIu64,
4139 : nCurTile, nBaseTiles);
4140 : }
4141 :
4142 7 : return bRet;
4143 : }
4144 :
4145 : /************************************************************************/
4146 : /* GDALRasterTileAlgorithm::GenerateOverviewTilesSpawnMethod() */
4147 : /************************************************************************/
4148 :
4149 12 : bool GDALRasterTileAlgorithm::GenerateOverviewTilesSpawnMethod(
4150 : int iZ, int nOvrMinTileX, int nOvrMinTileY, int nOvrMaxTileX,
4151 : int nOvrMaxTileY, std::atomic<uint64_t> &nCurTile, uint64_t nTotalTiles,
4152 : GDALProgressFunc pfnProgress, void *pProgressData)
4153 : {
4154 12 : if (m_parallelMethod == "spawn")
4155 : {
4156 8 : CPLAssert(!m_osGDALPath.empty());
4157 : }
4158 :
4159 12 : const int nOvrTilesPerCol = nOvrMaxTileY - nOvrMinTileY + 1;
4160 12 : const int nOvrTilesPerRow = nOvrMaxTileX - nOvrMinTileX + 1;
4161 12 : const uint64_t nExpectedOvrTileCount =
4162 12 : static_cast<uint64_t>(nOvrTilesPerCol) * nOvrTilesPerRow;
4163 :
4164 12 : const int nMaxJobCount = GetMaxChildCount(
4165 0 : std::max(1, static_cast<int>(std::min<uint64_t>(
4166 0 : m_numThreads, nExpectedOvrTileCount /
4167 12 : GetThresholdMinTilesPerJob()))));
4168 :
4169 : double dfTilesYPerJob;
4170 : int nYOuterIterations;
4171 : double dfTilesXPerJob;
4172 : int nXOuterIterations;
4173 12 : ComputeJobChunkSize(nMaxJobCount, nOvrTilesPerCol, nOvrTilesPerRow,
4174 : dfTilesYPerJob, nYOuterIterations, dfTilesXPerJob,
4175 : nXOuterIterations);
4176 :
4177 12 : CPLDebugOnly("gdal_raster_tile",
4178 : "z=%d, nYOuterIterations=%d, dfTilesYPerJob=%g, "
4179 : "nXOuterIterations=%d, dfTilesXPerJob=%g",
4180 : iZ, nYOuterIterations, dfTilesYPerJob, nXOuterIterations,
4181 : dfTilesXPerJob);
4182 :
4183 24 : std::vector<std::string> asCommandLines;
4184 24 : std::vector<CPLSpawnedProcess *> ahSpawnedProcesses;
4185 24 : std::vector<uint64_t> anRemainingTilesForProcess;
4186 :
4187 : #ifdef FORK_ALLOWED
4188 24 : std::vector<std::unique_ptr<ForkWorkStructure>> forkWorkStructures;
4189 : #endif
4190 :
4191 12 : const uint64_t nCacheMaxPerProcess = GDALGetCacheMax64() / nMaxJobCount;
4192 :
4193 12 : const auto poSrcDriver = m_poSrcDS ? m_poSrcDS->GetDriver() : nullptr;
4194 : const bool bIsMEMSource =
4195 12 : poSrcDriver && EQUAL(poSrcDriver->GetDescription(), "MEM");
4196 :
4197 12 : int nLastYEndIncluded = nOvrMinTileY - 1;
4198 12 : bool bRet = true;
4199 48 : for (int iYOuterIter = 0; bRet && iYOuterIter < nYOuterIterations &&
4200 36 : nLastYEndIncluded < nOvrMaxTileY;
4201 : ++iYOuterIter)
4202 : {
4203 36 : const int iYStart = nLastYEndIncluded + 1;
4204 : const int iYEndIncluded =
4205 36 : iYOuterIter + 1 == nYOuterIterations
4206 60 : ? nOvrMaxTileY
4207 : : std::max(iYStart,
4208 60 : static_cast<int>(std::floor(
4209 24 : nOvrMinTileY +
4210 24 : (iYOuterIter + 1) * dfTilesYPerJob - 1)));
4211 :
4212 36 : nLastYEndIncluded = iYEndIncluded;
4213 :
4214 36 : int nLastXEndIncluded = nOvrMinTileX - 1;
4215 84 : for (int iXOuterIter = 0; bRet && iXOuterIter < nXOuterIterations &&
4216 48 : nLastXEndIncluded < nOvrMaxTileX;
4217 : ++iXOuterIter)
4218 : {
4219 48 : const int iXStart = nLastXEndIncluded + 1;
4220 : const int iXEndIncluded =
4221 48 : iXOuterIter + 1 == nXOuterIterations
4222 60 : ? nOvrMaxTileX
4223 : : std::max(iXStart,
4224 60 : static_cast<int>(std::floor(
4225 12 : nOvrMinTileX +
4226 12 : (iXOuterIter + 1) * dfTilesXPerJob - 1)));
4227 :
4228 48 : nLastXEndIncluded = iXEndIncluded;
4229 :
4230 48 : anRemainingTilesForProcess.push_back(
4231 0 : static_cast<uint64_t>(iYEndIncluded - iYStart + 1) *
4232 48 : (iXEndIncluded - iXStart + 1));
4233 :
4234 48 : CPLStringList aosArgv;
4235 48 : if (m_parallelMethod == "spawn")
4236 : {
4237 32 : aosArgv.push_back(m_osGDALPath.c_str());
4238 32 : aosArgv.push_back("raster");
4239 32 : aosArgv.push_back("tile");
4240 32 : aosArgv.push_back("--config-options-in-stdin");
4241 32 : aosArgv.push_back("--config");
4242 32 : aosArgv.push_back("GDAL_NUM_THREADS=1");
4243 32 : aosArgv.push_back("--config");
4244 32 : aosArgv.push_back(
4245 : CPLSPrintf("GDAL_CACHEMAX=%" PRIu64, nCacheMaxPerProcess));
4246 : }
4247 48 : aosArgv.push_back(
4248 96 : std::string("--").append(GDAL_ARG_NAME_NUM_THREADS).c_str());
4249 48 : aosArgv.push_back("1");
4250 48 : aosArgv.push_back("--ovr-zoom-level");
4251 48 : aosArgv.push_back(CPLSPrintf("%d", iZ));
4252 48 : aosArgv.push_back("--ovr-min-x");
4253 48 : aosArgv.push_back(CPLSPrintf("%d", iXStart));
4254 48 : aosArgv.push_back("--ovr-max-x");
4255 48 : aosArgv.push_back(CPLSPrintf("%d", iXEndIncluded));
4256 48 : aosArgv.push_back("--ovr-min-y");
4257 48 : aosArgv.push_back(CPLSPrintf("%d", iYStart));
4258 48 : aosArgv.push_back("--ovr-max-y");
4259 48 : aosArgv.push_back(CPLSPrintf("%d", iYEndIncluded));
4260 48 : aosArgv.push_back("--webviewer");
4261 48 : aosArgv.push_back("none");
4262 48 : aosArgv.push_back(m_parallelMethod == "spawn" ? "--spawned"
4263 : : "--forked");
4264 48 : if (!bIsMEMSource)
4265 : {
4266 40 : aosArgv.push_back("--input");
4267 40 : aosArgv.push_back(m_inputDataset[0].GetName().c_str());
4268 : }
4269 2512 : for (const auto &arg : GetArgs())
4270 : {
4271 2880 : if (arg->IsExplicitlySet() && arg->GetName() != "progress" &&
4272 416 : arg->GetName() != "progress-forked" &&
4273 376 : arg->GetName() != GDAL_ARG_NAME_INPUT &&
4274 336 : arg->GetName() != GDAL_ARG_NAME_NUM_THREADS &&
4275 2840 : arg->GetName() != "webviewer" &&
4276 168 : arg->GetName() != "parallel-method")
4277 : {
4278 152 : if (!AddArgToArgv(arg.get(), aosArgv))
4279 0 : return false;
4280 : }
4281 : }
4282 :
4283 48 : std::string cmdLine;
4284 1408 : for (const char *arg : aosArgv)
4285 : {
4286 1360 : if (!cmdLine.empty())
4287 1312 : cmdLine += ' ';
4288 2720 : CPLString sArg(arg);
4289 1360 : if (sArg.find_first_of(" \"") != std::string::npos)
4290 : {
4291 8 : cmdLine += '"';
4292 8 : cmdLine += sArg.replaceAll('"', "\\\"");
4293 8 : cmdLine += '"';
4294 : }
4295 : else
4296 1352 : cmdLine += sArg;
4297 : }
4298 48 : CPLDebugOnly("gdal_raster_tile", "%s %s",
4299 : m_parallelMethod == "spawn" ? "Spawning" : "Forking",
4300 : cmdLine.c_str());
4301 48 : asCommandLines.push_back(std::move(cmdLine));
4302 :
4303 : #ifdef FORK_ALLOWED
4304 48 : if (m_parallelMethod == "fork")
4305 : {
4306 16 : forkWorkStructures.push_back(
4307 32 : std::make_unique<ForkWorkStructure>());
4308 16 : ForkWorkStructure *pData = forkWorkStructures.back().get();
4309 16 : pData->nCacheMaxPerProcess = nCacheMaxPerProcess;
4310 16 : pData->aosArgv = aosArgv;
4311 16 : if (bIsMEMSource)
4312 8 : pData->poMemSrcDS = m_poSrcDS;
4313 : }
4314 48 : CPL_IGNORE_RET_VAL(aosArgv);
4315 : #endif
4316 :
4317 96 : CPLSpawnedProcess *hSpawnedProcess = CPLSpawnAsync(
4318 : #ifdef FORK_ALLOWED
4319 48 : m_parallelMethod == "fork" ? GenerateTilesForkMethod :
4320 : #endif
4321 : nullptr,
4322 80 : m_parallelMethod == "fork" ? nullptr : aosArgv.List(),
4323 : /* bCreateInputPipe = */ true,
4324 : /* bCreateOutputPipe = */ true,
4325 48 : /* bCreateErrorPipe = */ true, nullptr);
4326 48 : if (!hSpawnedProcess)
4327 : {
4328 0 : ReportError(CE_Failure, CPLE_AppDefined,
4329 : "Spawning child gdal process '%s' failed",
4330 0 : asCommandLines.back().c_str());
4331 0 : bRet = false;
4332 0 : break;
4333 : }
4334 :
4335 48 : CPLDebugOnly("gdal_raster_tile",
4336 : "Job for z = %d, y in [%d,%d] and x in [%d,%d], "
4337 : "run by process %" PRIu64,
4338 : iZ, iYStart, iYEndIncluded, iXStart, iXEndIncluded,
4339 : static_cast<uint64_t>(
4340 : CPLSpawnAsyncGetChildProcessId(hSpawnedProcess)));
4341 :
4342 48 : ahSpawnedProcesses.push_back(hSpawnedProcess);
4343 :
4344 48 : if (m_parallelMethod == "spawn")
4345 : {
4346 32 : SendConfigOptions(hSpawnedProcess, bRet);
4347 : }
4348 :
4349 : #ifdef FORK_ALLOWED
4350 : else
4351 : {
4352 16 : ForkWorkStructure *pData = forkWorkStructures.back().get();
4353 16 : auto handle = CPLSpawnAsyncGetOutputFileHandle(hSpawnedProcess);
4354 16 : bRet &= CPL_TO_BOOL(CPLPipeWrite(
4355 16 : handle, &pData, static_cast<int>(sizeof(pData))));
4356 : }
4357 : #endif
4358 48 : if (!bRet)
4359 : {
4360 0 : ReportError(CE_Failure, CPLE_AppDefined,
4361 : "Could not transmit config options to child gdal "
4362 : "process '%s'",
4363 0 : asCommandLines.back().c_str());
4364 0 : break;
4365 : }
4366 : }
4367 : }
4368 :
4369 12 : uint64_t nCurTileLocal = nCurTile;
4370 12 : GetProgressForChildProcesses(bRet, ahSpawnedProcesses,
4371 : anRemainingTilesForProcess, nCurTileLocal,
4372 : nTotalTiles, pfnProgress, pProgressData);
4373 :
4374 12 : WaitForSpawnedProcesses(bRet, asCommandLines, ahSpawnedProcesses);
4375 :
4376 12 : if (bRet && nCurTileLocal - nCurTile != nExpectedOvrTileCount)
4377 : {
4378 0 : bRet = false;
4379 0 : ReportError(CE_Failure, CPLE_AppDefined,
4380 : "Not all tiles at zoom level %d have been "
4381 : "generated. Got %" PRIu64 ", expected %" PRIu64,
4382 0 : iZ, nCurTileLocal - nCurTile, nExpectedOvrTileCount);
4383 : }
4384 :
4385 12 : nCurTile = nCurTileLocal;
4386 :
4387 12 : return bRet;
4388 : }
4389 :
4390 : /************************************************************************/
4391 : /* GDALRasterTileAlgorithm::RunImpl() */
4392 : /************************************************************************/
4393 :
4394 203 : bool GDALRasterTileAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
4395 : void *pProgressData)
4396 : {
4397 203 : GDALPipelineStepRunContext stepCtxt;
4398 203 : stepCtxt.m_pfnProgress = pfnProgress;
4399 203 : stepCtxt.m_pProgressData = pProgressData;
4400 406 : return RunStep(stepCtxt);
4401 : }
4402 :
4403 : /************************************************************************/
4404 : /* SpawnedErrorHandler() */
4405 : /************************************************************************/
4406 :
4407 310 : static void CPL_STDCALL SpawnedErrorHandler(CPLErr eErr, CPLErrorNum eNum,
4408 : const char *pszMsg)
4409 : {
4410 310 : fwrite(ERROR_START_MARKER, sizeof(ERROR_START_MARKER), 1, stdout);
4411 310 : uint32_t nErr = eErr;
4412 310 : fwrite(&nErr, sizeof(nErr), 1, stdout);
4413 310 : uint32_t nNum = eNum;
4414 310 : fwrite(&nNum, sizeof(nNum), 1, stdout);
4415 310 : uint16_t nLen = static_cast<uint16_t>(strlen(pszMsg));
4416 310 : fwrite(&nLen, sizeof(nLen), 1, stdout);
4417 310 : fwrite(pszMsg, nLen, 1, stdout);
4418 310 : fflush(stdout);
4419 310 : }
4420 :
4421 : /************************************************************************/
4422 : /* GDALRasterTileAlgorithm::RunStep() */
4423 : /************************************************************************/
4424 :
4425 205 : bool GDALRasterTileAlgorithm::RunStep(GDALPipelineStepRunContext &ctxt)
4426 : {
4427 205 : auto pfnProgress = ctxt.m_pfnProgress;
4428 205 : auto pProgressData = ctxt.m_pProgressData;
4429 205 : CPLAssert(m_inputDataset.size() == 1);
4430 205 : m_poSrcDS = m_inputDataset[0].GetDatasetRef();
4431 205 : CPLAssert(m_poSrcDS);
4432 :
4433 205 : const int nSrcWidth = m_poSrcDS->GetRasterXSize();
4434 205 : const int nSrcHeight = m_poSrcDS->GetRasterYSize();
4435 205 : if (m_poSrcDS->GetRasterCount() == 0 || nSrcWidth == 0 || nSrcHeight == 0)
4436 : {
4437 1 : ReportError(CE_Failure, CPLE_AppDefined, "Invalid source dataset");
4438 1 : return false;
4439 : }
4440 :
4441 204 : const bool bIsNamedSource = m_poSrcDS->GetDescription()[0] != 0;
4442 204 : auto poSrcDriver = m_poSrcDS->GetDriver();
4443 : const bool bIsMEMSource =
4444 204 : poSrcDriver && EQUAL(poSrcDriver->GetDescription(), "MEM");
4445 204 : m_bIsNamedNonMemSrcDS = bIsNamedSource && !bIsMEMSource;
4446 204 : const bool bSrcIsFineForFork = bIsNamedSource || bIsMEMSource;
4447 :
4448 204 : if (m_parallelMethod == "spawn")
4449 : {
4450 5 : const char *pszErrorMsg = "";
4451 5 : if (!IsCompatibleOfSpawn(pszErrorMsg))
4452 : {
4453 3 : if (pszErrorMsg[0])
4454 2 : ReportError(CE_Failure, CPLE_AppDefined, "%s", pszErrorMsg);
4455 3 : return false;
4456 : }
4457 : }
4458 : #ifdef FORK_ALLOWED
4459 199 : else if (m_parallelMethod == "fork")
4460 : {
4461 3 : if (!bSrcIsFineForFork)
4462 : {
4463 1 : ReportError(CE_Failure, CPLE_AppDefined,
4464 : "Unnamed non-MEM source are not supported "
4465 : "with fork parallelization method");
4466 1 : return false;
4467 : }
4468 2 : if (cpl::starts_with(m_output, "/vsimem/"))
4469 : {
4470 1 : ReportError(CE_Failure, CPLE_AppDefined,
4471 : "/vsimem/ output directory not supported with fork "
4472 : "parallelization method");
4473 1 : return false;
4474 : }
4475 : }
4476 : #endif
4477 :
4478 199 : if (m_resampling == "near")
4479 2 : m_resampling = "nearest";
4480 199 : if (m_overviewResampling == "near")
4481 1 : m_overviewResampling = "nearest";
4482 198 : else if (m_overviewResampling.empty())
4483 193 : m_overviewResampling = m_resampling;
4484 :
4485 398 : CPLStringList aosWarpOptions;
4486 199 : if (!m_excludedValues.empty() || m_nodataValuesPctThreshold < 100)
4487 : {
4488 : aosWarpOptions.SetNameValue(
4489 : "NODATA_VALUES_PCT_THRESHOLD",
4490 6 : CPLSPrintf("%g", m_nodataValuesPctThreshold));
4491 6 : if (!m_excludedValues.empty())
4492 : {
4493 : aosWarpOptions.SetNameValue("EXCLUDED_VALUES",
4494 2 : m_excludedValues.c_str());
4495 : aosWarpOptions.SetNameValue(
4496 : "EXCLUDED_VALUES_PCT_THRESHOLD",
4497 2 : CPLSPrintf("%g", m_excludedValuesPctThreshold));
4498 : }
4499 : }
4500 :
4501 199 : if (m_poSrcDS->GetRasterBand(1)->GetColorInterpretation() ==
4502 203 : GCI_PaletteIndex &&
4503 4 : ((m_resampling != "nearest" && m_resampling != "mode") ||
4504 1 : (m_overviewResampling != "nearest" && m_overviewResampling != "mode")))
4505 : {
4506 1 : ReportError(CE_Failure, CPLE_NotSupported,
4507 : "Datasets with color table not supported with non-nearest "
4508 : "or non-mode resampling. Run 'gdal raster "
4509 : "color-map' before or set the 'resampling' argument to "
4510 : "'nearest' or 'mode'.");
4511 1 : return false;
4512 : }
4513 :
4514 198 : const auto eSrcDT = m_poSrcDS->GetRasterBand(1)->GetRasterDataType();
4515 198 : m_poDstDriver = GetGDALDriverManager()->GetDriverByName(m_format.c_str());
4516 198 : if (!m_poDstDriver)
4517 : {
4518 1 : ReportError(CE_Failure, CPLE_AppDefined,
4519 : "Invalid value for argument 'output-format'. Driver '%s' "
4520 : "does not exist",
4521 : m_format.c_str());
4522 1 : return false;
4523 : }
4524 :
4525 197 : if (!ValidateOutputFormat(eSrcDT))
4526 8 : return false;
4527 :
4528 : const char *pszExtensions =
4529 189 : m_poDstDriver->GetMetadataItem(GDAL_DMD_EXTENSIONS);
4530 189 : CPLAssert(pszExtensions && pszExtensions[0] != 0);
4531 : const CPLStringList aosExtensions(
4532 378 : CSLTokenizeString2(pszExtensions, " ", 0));
4533 189 : const char *pszExtension = aosExtensions[0];
4534 189 : GDALGeoTransform srcGT;
4535 189 : const bool bHasSrcGT = m_poSrcDS->GetGeoTransform(srcGT) == CE_None;
4536 : const bool bHasNorthUpSrcGT =
4537 189 : bHasSrcGT && srcGT[2] == 0 && srcGT[4] == 0 && srcGT[5] < 0;
4538 378 : OGRSpatialReference oSRS_TMS;
4539 :
4540 189 : if (m_tilingScheme == "raster")
4541 : {
4542 6 : if (const auto poSRS = m_poSrcDS->GetSpatialRef())
4543 5 : oSRS_TMS = *poSRS;
4544 : }
4545 : else
4546 : {
4547 3 : if (!bHasSrcGT && m_poSrcDS->GetGCPCount() == 0 &&
4548 187 : m_poSrcDS->GetMetadata("GEOLOCATION") == nullptr &&
4549 1 : m_poSrcDS->GetMetadata("RPC") == nullptr)
4550 : {
4551 1 : ReportError(CE_Failure, CPLE_NotSupported,
4552 : "Ungeoreferenced datasets are not supported, unless "
4553 : "'tiling-scheme' is set to 'raster'");
4554 1 : return false;
4555 : }
4556 :
4557 182 : if (m_poSrcDS->GetMetadata("GEOLOCATION") == nullptr &&
4558 182 : m_poSrcDS->GetMetadata("RPC") == nullptr &&
4559 366 : m_poSrcDS->GetSpatialRef() == nullptr &&
4560 2 : m_poSrcDS->GetGCPSpatialRef() == nullptr)
4561 : {
4562 2 : ReportError(CE_Failure, CPLE_NotSupported,
4563 : "Ungeoreferenced datasets are not supported, unless "
4564 : "'tiling-scheme' is set to 'raster'");
4565 2 : return false;
4566 : }
4567 : }
4568 :
4569 186 : if (m_copySrcMetadata)
4570 : {
4571 8 : CPLStringList aosMD(CSLDuplicate(m_poSrcDS->GetMetadata()));
4572 4 : const CPLStringList aosNewMD(m_metadata);
4573 8 : for (const auto [key, value] : cpl::IterateNameValue(aosNewMD))
4574 : {
4575 4 : aosMD.SetNameValue(key, value);
4576 : }
4577 4 : m_metadata = aosMD;
4578 : }
4579 :
4580 372 : std::vector<BandMetadata> aoBandMetadata;
4581 66206 : for (int i = 1; i <= m_poSrcDS->GetRasterCount(); ++i)
4582 : {
4583 66020 : auto poBand = m_poSrcDS->GetRasterBand(i);
4584 132040 : BandMetadata bm;
4585 66020 : bm.osDescription = poBand->GetDescription();
4586 66020 : bm.eDT = poBand->GetRasterDataType();
4587 66020 : bm.eColorInterp = poBand->GetColorInterpretation();
4588 66020 : if (const char *pszCenterWavelength =
4589 66020 : poBand->GetMetadataItem("CENTRAL_WAVELENGTH_UM", "IMAGERY"))
4590 0 : bm.osCenterWaveLength = pszCenterWavelength;
4591 66020 : if (const char *pszFWHM = poBand->GetMetadataItem("FWHM_UM", "IMAGERY"))
4592 0 : bm.osFWHM = pszFWHM;
4593 66020 : aoBandMetadata.emplace_back(std::move(bm));
4594 : }
4595 :
4596 186 : GDALGeoTransform srcGTModif{0, 1, 0, 0, 0, -1};
4597 :
4598 186 : if (m_tilingScheme == "mercator")
4599 31 : m_tilingScheme = "WebMercatorQuad";
4600 155 : else if (m_tilingScheme == "raster")
4601 : {
4602 6 : if (m_tileSize == 0)
4603 4 : m_tileSize = 256;
4604 6 : if (m_maxZoomLevel < 0)
4605 : {
4606 3 : m_maxZoomLevel = static_cast<int>(std::ceil(std::log2(
4607 6 : std::max(1, std::max(nSrcWidth, nSrcHeight) / m_tileSize))));
4608 : }
4609 6 : if (bHasNorthUpSrcGT)
4610 : {
4611 5 : srcGTModif = srcGT;
4612 : }
4613 : }
4614 :
4615 : auto poTMS =
4616 186 : m_tilingScheme == "raster"
4617 : ? gdal::TileMatrixSet::createRaster(
4618 6 : nSrcWidth, nSrcHeight, m_tileSize, 1 + m_maxZoomLevel,
4619 24 : srcGTModif[0], srcGTModif[3], srcGTModif[1], -srcGTModif[5],
4620 192 : oSRS_TMS.IsEmpty() ? std::string() : oSRS_TMS.exportToWkt())
4621 : : gdal::TileMatrixSet::parse(
4622 378 : m_mapTileMatrixIdentifierToScheme[m_tilingScheme].c_str());
4623 : // Enforced by SetChoices() on the m_tilingScheme argument
4624 186 : CPLAssert(poTMS && !poTMS->hasVariableMatrixWidth());
4625 :
4626 372 : CPLStringList aosTO;
4627 186 : if (m_tilingScheme == "raster")
4628 : {
4629 6 : aosTO.SetNameValue("SRC_METHOD", "GEOTRANSFORM");
4630 : }
4631 : else
4632 : {
4633 180 : CPL_IGNORE_RET_VAL(oSRS_TMS.SetFromUserInput(poTMS->crs().c_str()));
4634 180 : aosTO.SetNameValue("DST_SRS", oSRS_TMS.exportToWkt().c_str());
4635 : }
4636 :
4637 186 : const char *pszAuthName = oSRS_TMS.GetAuthorityName(nullptr);
4638 186 : const char *pszAuthCode = oSRS_TMS.GetAuthorityCode(nullptr);
4639 186 : const int nEPSGCode =
4640 185 : (pszAuthName && pszAuthCode && EQUAL(pszAuthName, "EPSG"))
4641 371 : ? atoi(pszAuthCode)
4642 : : 0;
4643 :
4644 : const bool bInvertAxisTMS =
4645 366 : m_tilingScheme != "raster" &&
4646 180 : (oSRS_TMS.EPSGTreatsAsLatLong() != FALSE ||
4647 180 : oSRS_TMS.EPSGTreatsAsNorthingEasting() != FALSE);
4648 :
4649 186 : oSRS_TMS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
4650 :
4651 : std::unique_ptr<void, decltype(&GDALDestroyTransformer)> hTransformArg(
4652 372 : nullptr, GDALDestroyTransformer);
4653 :
4654 : // Hack to compensate for GDALSuggestedWarpOutput2() failure (or not
4655 : // ideal suggestion with PROJ 8) when reprojecting latitude = +/- 90 to
4656 : // EPSG:3857.
4657 186 : std::unique_ptr<GDALDataset> poTmpDS;
4658 186 : bool bEPSG3857Adjust = false;
4659 186 : if (nEPSGCode == 3857 && bHasNorthUpSrcGT)
4660 : {
4661 155 : const auto poSrcSRS = m_poSrcDS->GetSpatialRef();
4662 155 : if (poSrcSRS && poSrcSRS->IsGeographic())
4663 : {
4664 98 : double maxLat = srcGT[3];
4665 98 : double minLat = srcGT[3] + nSrcHeight * srcGT[5];
4666 : // Corresponds to the latitude of below MAX_GM
4667 98 : constexpr double MAX_LAT = 85.0511287798066;
4668 98 : bool bModified = false;
4669 98 : if (maxLat > MAX_LAT)
4670 : {
4671 97 : maxLat = MAX_LAT;
4672 97 : bModified = true;
4673 : }
4674 98 : if (minLat < -MAX_LAT)
4675 : {
4676 97 : minLat = -MAX_LAT;
4677 97 : bModified = true;
4678 : }
4679 98 : if (bModified)
4680 : {
4681 194 : CPLStringList aosOptions;
4682 97 : aosOptions.AddString("-of");
4683 97 : aosOptions.AddString("VRT");
4684 97 : aosOptions.AddString("-projwin");
4685 97 : aosOptions.AddString(CPLSPrintf("%.17g", srcGT[0]));
4686 97 : aosOptions.AddString(CPLSPrintf("%.17g", maxLat));
4687 : aosOptions.AddString(
4688 97 : CPLSPrintf("%.17g", srcGT[0] + nSrcWidth * srcGT[1]));
4689 97 : aosOptions.AddString(CPLSPrintf("%.17g", minLat));
4690 : auto psOptions =
4691 97 : GDALTranslateOptionsNew(aosOptions.List(), nullptr);
4692 97 : poTmpDS.reset(GDALDataset::FromHandle(GDALTranslate(
4693 : "", GDALDataset::ToHandle(m_poSrcDS), psOptions, nullptr)));
4694 97 : GDALTranslateOptionsFree(psOptions);
4695 97 : if (poTmpDS)
4696 : {
4697 97 : bEPSG3857Adjust = true;
4698 97 : hTransformArg.reset(GDALCreateGenImgProjTransformer2(
4699 97 : GDALDataset::FromHandle(poTmpDS.get()), nullptr,
4700 97 : aosTO.List()));
4701 : }
4702 : }
4703 : }
4704 : }
4705 :
4706 186 : GDALGeoTransform dstGT;
4707 : double adfExtent[4];
4708 : int nXSize, nYSize;
4709 :
4710 : bool bSuggestOK;
4711 186 : if (m_tilingScheme == "raster")
4712 : {
4713 6 : bSuggestOK = true;
4714 6 : nXSize = nSrcWidth;
4715 6 : nYSize = nSrcHeight;
4716 6 : dstGT = srcGTModif;
4717 6 : adfExtent[0] = dstGT[0];
4718 6 : adfExtent[1] = dstGT[3] + nSrcHeight * dstGT[5];
4719 6 : adfExtent[2] = dstGT[0] + nSrcWidth * dstGT[1];
4720 6 : adfExtent[3] = dstGT[3];
4721 : }
4722 : else
4723 : {
4724 180 : if (!hTransformArg)
4725 : {
4726 83 : hTransformArg.reset(GDALCreateGenImgProjTransformer2(
4727 83 : m_poSrcDS, nullptr, aosTO.List()));
4728 : }
4729 180 : if (!hTransformArg)
4730 : {
4731 1 : return false;
4732 : }
4733 179 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
4734 179 : bSuggestOK =
4735 358 : (GDALSuggestedWarpOutput2(
4736 179 : m_poSrcDS,
4737 179 : static_cast<GDALTransformerInfo *>(hTransformArg.get())
4738 : ->pfnTransform,
4739 : hTransformArg.get(), dstGT.data(), &nXSize, &nYSize, adfExtent,
4740 : 0) == CE_None);
4741 : }
4742 185 : if (!bSuggestOK)
4743 : {
4744 1 : ReportError(CE_Failure, CPLE_AppDefined,
4745 : "Cannot determine extent of raster in target CRS");
4746 1 : return false;
4747 : }
4748 :
4749 184 : poTmpDS.reset();
4750 :
4751 184 : if (bEPSG3857Adjust)
4752 : {
4753 97 : constexpr double SPHERICAL_RADIUS = 6378137.0;
4754 97 : constexpr double MAX_GM =
4755 : SPHERICAL_RADIUS * M_PI; // 20037508.342789244
4756 97 : double maxNorthing = dstGT[3];
4757 97 : double minNorthing = dstGT[3] + dstGT[5] * nYSize;
4758 97 : bool bChanged = false;
4759 97 : if (maxNorthing > MAX_GM)
4760 : {
4761 94 : bChanged = true;
4762 94 : maxNorthing = MAX_GM;
4763 : }
4764 97 : if (minNorthing < -MAX_GM)
4765 : {
4766 94 : bChanged = true;
4767 94 : minNorthing = -MAX_GM;
4768 : }
4769 97 : if (bChanged)
4770 : {
4771 94 : dstGT[3] = maxNorthing;
4772 94 : nYSize = int((maxNorthing - minNorthing) / (-dstGT[5]) + 0.5);
4773 94 : adfExtent[1] = maxNorthing + nYSize * dstGT[5];
4774 94 : adfExtent[3] = maxNorthing;
4775 : }
4776 : }
4777 :
4778 184 : const auto &tileMatrixList = poTMS->tileMatrixList();
4779 184 : if (m_maxZoomLevel >= 0)
4780 : {
4781 120 : if (m_maxZoomLevel >= static_cast<int>(tileMatrixList.size()))
4782 : {
4783 1 : ReportError(CE_Failure, CPLE_AppDefined,
4784 : "max-zoom = %d is invalid. It must be in [0,%d] range",
4785 : m_maxZoomLevel,
4786 1 : static_cast<int>(tileMatrixList.size()) - 1);
4787 1 : return false;
4788 : }
4789 : }
4790 : else
4791 : {
4792 64 : const double dfComputedRes = dstGT[1];
4793 64 : double dfPrevRes = 0.0;
4794 64 : double dfRes = 0.0;
4795 64 : constexpr double EPSILON = 1e-8;
4796 :
4797 64 : if (m_minZoomLevel >= 0)
4798 17 : m_maxZoomLevel = m_minZoomLevel;
4799 : else
4800 47 : m_maxZoomLevel = 0;
4801 :
4802 230 : for (; m_maxZoomLevel < static_cast<int>(tileMatrixList.size());
4803 166 : m_maxZoomLevel++)
4804 : {
4805 229 : dfRes = tileMatrixList[m_maxZoomLevel].mResX;
4806 229 : if (dfComputedRes > dfRes ||
4807 168 : fabs(dfComputedRes - dfRes) / dfRes <= EPSILON)
4808 : break;
4809 166 : dfPrevRes = dfRes;
4810 : }
4811 64 : if (m_maxZoomLevel >= static_cast<int>(tileMatrixList.size()))
4812 : {
4813 1 : ReportError(CE_Failure, CPLE_AppDefined,
4814 : "Could not find an appropriate zoom level. Perhaps "
4815 : "min-zoom is too large?");
4816 1 : return false;
4817 : }
4818 :
4819 63 : if (m_maxZoomLevel > 0 && fabs(dfComputedRes - dfRes) / dfRes > EPSILON)
4820 : {
4821 : // Round to closest resolution
4822 56 : if (dfPrevRes / dfComputedRes < dfComputedRes / dfRes)
4823 36 : m_maxZoomLevel--;
4824 : }
4825 : }
4826 182 : if (m_minZoomLevel < 0)
4827 89 : m_minZoomLevel = m_maxZoomLevel;
4828 :
4829 364 : auto tileMatrix = tileMatrixList[m_maxZoomLevel];
4830 182 : int nMinTileX = 0;
4831 182 : int nMinTileY = 0;
4832 182 : int nMaxTileX = 0;
4833 182 : int nMaxTileY = 0;
4834 182 : bool bIntersects = false;
4835 182 : if (!GetTileIndices(tileMatrix, bInvertAxisTMS, m_tileSize, adfExtent,
4836 : nMinTileX, nMinTileY, nMaxTileX, nMaxTileY,
4837 182 : m_noIntersectionIsOK, bIntersects,
4838 : /* checkRasterOverflow = */ false))
4839 : {
4840 1 : return false;
4841 : }
4842 181 : if (!bIntersects)
4843 1 : return true;
4844 :
4845 : // Potentially restrict tiling to user specified coordinates
4846 180 : if (m_minTileX >= tileMatrix.mMatrixWidth)
4847 : {
4848 1 : ReportError(CE_Failure, CPLE_IllegalArg,
4849 : "'min-x' value must be in [0,%d] range",
4850 1 : tileMatrix.mMatrixWidth - 1);
4851 1 : return false;
4852 : }
4853 179 : if (m_maxTileX >= tileMatrix.mMatrixWidth)
4854 : {
4855 1 : ReportError(CE_Failure, CPLE_IllegalArg,
4856 : "'max-x' value must be in [0,%d] range",
4857 1 : tileMatrix.mMatrixWidth - 1);
4858 1 : return false;
4859 : }
4860 178 : if (m_minTileY >= tileMatrix.mMatrixHeight)
4861 : {
4862 1 : ReportError(CE_Failure, CPLE_IllegalArg,
4863 : "'min-y' value must be in [0,%d] range",
4864 1 : tileMatrix.mMatrixHeight - 1);
4865 1 : return false;
4866 : }
4867 177 : if (m_maxTileY >= tileMatrix.mMatrixHeight)
4868 : {
4869 1 : ReportError(CE_Failure, CPLE_IllegalArg,
4870 : "'max-y' value must be in [0,%d] range",
4871 1 : tileMatrix.mMatrixHeight - 1);
4872 1 : return false;
4873 : }
4874 :
4875 176 : if ((m_minTileX >= 0 && m_minTileX > nMaxTileX) ||
4876 174 : (m_minTileY >= 0 && m_minTileY > nMaxTileY) ||
4877 174 : (m_maxTileX >= 0 && m_maxTileX < nMinTileX) ||
4878 174 : (m_maxTileY >= 0 && m_maxTileY < nMinTileY))
4879 : {
4880 2 : ReportError(
4881 2 : m_noIntersectionIsOK ? CE_Warning : CE_Failure, CPLE_AppDefined,
4882 : "Dataset extent not intersecting specified min/max X/Y tile "
4883 : "coordinates");
4884 2 : return m_noIntersectionIsOK;
4885 : }
4886 174 : if (m_minTileX >= 0 && m_minTileX > nMinTileX)
4887 : {
4888 2 : nMinTileX = m_minTileX;
4889 2 : adfExtent[0] = tileMatrix.mTopLeftX +
4890 2 : nMinTileX * tileMatrix.mResX * tileMatrix.mTileWidth;
4891 : }
4892 174 : if (m_minTileY >= 0 && m_minTileY > nMinTileY)
4893 : {
4894 14 : nMinTileY = m_minTileY;
4895 14 : adfExtent[3] = tileMatrix.mTopLeftY -
4896 14 : nMinTileY * tileMatrix.mResY * tileMatrix.mTileHeight;
4897 : }
4898 174 : if (m_maxTileX >= 0 && m_maxTileX < nMaxTileX)
4899 : {
4900 2 : nMaxTileX = m_maxTileX;
4901 2 : adfExtent[2] = tileMatrix.mTopLeftX + (nMaxTileX + 1) *
4902 2 : tileMatrix.mResX *
4903 2 : tileMatrix.mTileWidth;
4904 : }
4905 174 : if (m_maxTileY >= 0 && m_maxTileY < nMaxTileY)
4906 : {
4907 15 : nMaxTileY = m_maxTileY;
4908 15 : adfExtent[1] = tileMatrix.mTopLeftY - (nMaxTileY + 1) *
4909 15 : tileMatrix.mResY *
4910 15 : tileMatrix.mTileHeight;
4911 : }
4912 :
4913 174 : if (nMaxTileX - nMinTileX + 1 > INT_MAX / tileMatrix.mTileWidth ||
4914 173 : nMaxTileY - nMinTileY + 1 > INT_MAX / tileMatrix.mTileHeight)
4915 : {
4916 1 : ReportError(CE_Failure, CPLE_AppDefined, "Too large zoom level");
4917 1 : return false;
4918 : }
4919 :
4920 346 : dstGT[0] = tileMatrix.mTopLeftX +
4921 173 : nMinTileX * tileMatrix.mResX * tileMatrix.mTileWidth;
4922 173 : dstGT[1] = tileMatrix.mResX;
4923 173 : dstGT[2] = 0;
4924 346 : dstGT[3] = tileMatrix.mTopLeftY -
4925 173 : nMinTileY * tileMatrix.mResY * tileMatrix.mTileHeight;
4926 173 : dstGT[4] = 0;
4927 173 : dstGT[5] = -tileMatrix.mResY;
4928 :
4929 : /* -------------------------------------------------------------------- */
4930 : /* Setup warp options. */
4931 : /* -------------------------------------------------------------------- */
4932 : std::unique_ptr<GDALWarpOptions, decltype(&GDALDestroyWarpOptions)> psWO(
4933 346 : GDALCreateWarpOptions(), GDALDestroyWarpOptions);
4934 :
4935 173 : psWO->papszWarpOptions = CSLSetNameValue(nullptr, "OPTIMIZE_SIZE", "YES");
4936 346 : psWO->papszWarpOptions =
4937 173 : CSLSetNameValue(psWO->papszWarpOptions, "SAMPLE_GRID", "YES");
4938 346 : psWO->papszWarpOptions =
4939 173 : CSLMerge(psWO->papszWarpOptions, aosWarpOptions.List());
4940 :
4941 173 : int bHasSrcNoData = false;
4942 : const double dfSrcNoDataValue =
4943 173 : m_poSrcDS->GetRasterBand(1)->GetNoDataValue(&bHasSrcNoData);
4944 :
4945 : const bool bLastSrcBandIsAlpha =
4946 310 : (m_poSrcDS->GetRasterCount() > 1 &&
4947 137 : m_poSrcDS->GetRasterBand(m_poSrcDS->GetRasterCount())
4948 137 : ->GetColorInterpretation() == GCI_AlphaBand);
4949 :
4950 173 : const bool bOutputSupportsAlpha = !EQUAL(m_format.c_str(), "JPEG");
4951 173 : const bool bOutputSupportsNoData = EQUAL(m_format.c_str(), "GTiff");
4952 173 : const bool bDstNoDataSpecified = GetArg("dst-nodata")->IsExplicitlySet();
4953 : auto poColorTable = std::unique_ptr<GDALColorTable>(
4954 173 : [this]()
4955 : {
4956 173 : auto poCT = m_poSrcDS->GetRasterBand(1)->GetColorTable();
4957 173 : return poCT ? poCT->Clone() : nullptr;
4958 346 : }());
4959 :
4960 173 : const bool bUserAskedForAlpha = m_addalpha;
4961 173 : if (!m_noalpha && !m_addalpha)
4962 : {
4963 190 : m_addalpha = !(bHasSrcNoData && bOutputSupportsNoData) &&
4964 190 : !bDstNoDataSpecified && poColorTable == nullptr;
4965 : }
4966 173 : m_addalpha &= bOutputSupportsAlpha;
4967 :
4968 173 : psWO->nBandCount = m_poSrcDS->GetRasterCount();
4969 173 : if (bLastSrcBandIsAlpha)
4970 : {
4971 17 : --psWO->nBandCount;
4972 17 : psWO->nSrcAlphaBand = m_poSrcDS->GetRasterCount();
4973 : }
4974 :
4975 173 : if (bHasSrcNoData)
4976 : {
4977 40 : psWO->padfSrcNoDataReal =
4978 20 : static_cast<double *>(CPLCalloc(psWO->nBandCount, sizeof(double)));
4979 60 : for (int i = 0; i < psWO->nBandCount; ++i)
4980 : {
4981 40 : psWO->padfSrcNoDataReal[i] = dfSrcNoDataValue;
4982 : }
4983 : }
4984 :
4985 173 : if ((bHasSrcNoData && !m_addalpha && bOutputSupportsNoData) ||
4986 : bDstNoDataSpecified)
4987 : {
4988 18 : psWO->padfDstNoDataReal =
4989 9 : static_cast<double *>(CPLCalloc(psWO->nBandCount, sizeof(double)));
4990 18 : for (int i = 0; i < psWO->nBandCount; ++i)
4991 : {
4992 9 : psWO->padfDstNoDataReal[i] =
4993 9 : bDstNoDataSpecified ? m_dstNoData : dfSrcNoDataValue;
4994 : }
4995 : }
4996 :
4997 173 : psWO->eWorkingDataType = eSrcDT;
4998 :
4999 173 : GDALGetWarpResampleAlg(m_resampling.c_str(), psWO->eResampleAlg);
5000 :
5001 : /* -------------------------------------------------------------------- */
5002 : /* Setup band mapping. */
5003 : /* -------------------------------------------------------------------- */
5004 :
5005 346 : psWO->panSrcBands =
5006 173 : static_cast<int *>(CPLMalloc(psWO->nBandCount * sizeof(int)));
5007 346 : psWO->panDstBands =
5008 173 : static_cast<int *>(CPLMalloc(psWO->nBandCount * sizeof(int)));
5009 :
5010 66147 : for (int i = 0; i < psWO->nBandCount; i++)
5011 : {
5012 65974 : psWO->panSrcBands[i] = i + 1;
5013 65974 : psWO->panDstBands[i] = i + 1;
5014 : }
5015 :
5016 173 : if (m_addalpha)
5017 158 : psWO->nDstAlphaBand = psWO->nBandCount + 1;
5018 :
5019 : const int nDstBands =
5020 173 : psWO->nDstAlphaBand ? psWO->nDstAlphaBand : psWO->nBandCount;
5021 :
5022 346 : std::vector<GByte> dstBuffer;
5023 173 : const bool bIsPNGOutput = EQUAL(pszExtension, "png");
5024 : uint64_t dstBufferSize =
5025 173 : (static_cast<uint64_t>(tileMatrix.mTileWidth) *
5026 : // + 1 for PNG filter type / row byte
5027 173 : nDstBands * GDALGetDataTypeSizeBytes(psWO->eWorkingDataType) +
5028 173 : (bIsPNGOutput ? 1 : 0)) *
5029 173 : tileMatrix.mTileHeight;
5030 173 : if (bIsPNGOutput)
5031 : {
5032 : // Security margin for deflate compression
5033 152 : dstBufferSize += dstBufferSize / 10;
5034 : }
5035 : const uint64_t nUsableRAM =
5036 173 : std::min<uint64_t>(INT_MAX, CPLGetUsablePhysicalRAM() / 4);
5037 173 : if (dstBufferSize <=
5038 173 : (nUsableRAM ? nUsableRAM : static_cast<uint64_t>(INT_MAX)))
5039 : {
5040 : try
5041 : {
5042 172 : dstBuffer.resize(static_cast<size_t>(dstBufferSize));
5043 : }
5044 0 : catch (const std::exception &)
5045 : {
5046 : }
5047 : }
5048 173 : if (dstBuffer.size() < dstBufferSize)
5049 : {
5050 1 : ReportError(CE_Failure, CPLE_AppDefined,
5051 : "Tile size and/or number of bands too large compared to "
5052 : "available RAM");
5053 1 : return false;
5054 : }
5055 :
5056 : FakeMaxZoomDataset oFakeMaxZoomDS(
5057 172 : (nMaxTileX - nMinTileX + 1) * tileMatrix.mTileWidth,
5058 172 : (nMaxTileY - nMinTileY + 1) * tileMatrix.mTileHeight, nDstBands,
5059 172 : tileMatrix.mTileWidth, tileMatrix.mTileHeight, psWO->eWorkingDataType,
5060 344 : dstGT, oSRS_TMS, dstBuffer);
5061 172 : CPL_IGNORE_RET_VAL(oFakeMaxZoomDS.GetSpatialRef());
5062 :
5063 172 : psWO->hSrcDS = GDALDataset::ToHandle(m_poSrcDS);
5064 172 : psWO->hDstDS = GDALDataset::ToHandle(&oFakeMaxZoomDS);
5065 :
5066 172 : std::unique_ptr<GDALDataset> tmpSrcDS;
5067 172 : if (m_tilingScheme == "raster" && !bHasNorthUpSrcGT)
5068 : {
5069 1 : CPLStringList aosOptions;
5070 1 : aosOptions.AddString("-of");
5071 1 : aosOptions.AddString("VRT");
5072 1 : aosOptions.AddString("-a_ullr");
5073 1 : aosOptions.AddString(CPLSPrintf("%.17g", srcGTModif[0]));
5074 1 : aosOptions.AddString(CPLSPrintf("%.17g", srcGTModif[3]));
5075 : aosOptions.AddString(
5076 1 : CPLSPrintf("%.17g", srcGTModif[0] + nSrcWidth * srcGTModif[1]));
5077 : aosOptions.AddString(
5078 1 : CPLSPrintf("%.17g", srcGTModif[3] + nSrcHeight * srcGTModif[5]));
5079 1 : if (oSRS_TMS.IsEmpty())
5080 : {
5081 1 : aosOptions.AddString("-a_srs");
5082 1 : aosOptions.AddString("none");
5083 : }
5084 :
5085 : GDALTranslateOptions *psOptions =
5086 1 : GDALTranslateOptionsNew(aosOptions.List(), nullptr);
5087 :
5088 1 : tmpSrcDS.reset(GDALDataset::FromHandle(GDALTranslate(
5089 : "", GDALDataset::ToHandle(m_poSrcDS), psOptions, nullptr)));
5090 1 : GDALTranslateOptionsFree(psOptions);
5091 1 : if (!tmpSrcDS)
5092 0 : return false;
5093 : }
5094 173 : hTransformArg.reset(GDALCreateGenImgProjTransformer2(
5095 173 : tmpSrcDS ? tmpSrcDS.get() : m_poSrcDS, &oFakeMaxZoomDS, aosTO.List()));
5096 172 : CPLAssert(hTransformArg);
5097 :
5098 : /* -------------------------------------------------------------------- */
5099 : /* Warp the transformer with a linear approximator */
5100 : /* -------------------------------------------------------------------- */
5101 172 : hTransformArg.reset(GDALCreateApproxTransformer(
5102 : GDALGenImgProjTransform, hTransformArg.release(), 0.125));
5103 172 : GDALApproxTransformerOwnsSubtransformer(hTransformArg.get(), TRUE);
5104 :
5105 172 : psWO->pfnTransformer = GDALApproxTransform;
5106 172 : psWO->pTransformerArg = hTransformArg.get();
5107 :
5108 : /* -------------------------------------------------------------------- */
5109 : /* Determine total number of tiles */
5110 : /* -------------------------------------------------------------------- */
5111 172 : const int nBaseTilesPerRow = nMaxTileX - nMinTileX + 1;
5112 172 : const int nBaseTilesPerCol = nMaxTileY - nMinTileY + 1;
5113 172 : const uint64_t nBaseTiles =
5114 172 : static_cast<uint64_t>(nBaseTilesPerCol) * nBaseTilesPerRow;
5115 172 : uint64_t nTotalTiles = nBaseTiles;
5116 172 : std::atomic<uint64_t> nCurTile = 0;
5117 172 : bool bRet = true;
5118 :
5119 352 : for (int iZ = m_maxZoomLevel - 1;
5120 352 : bRet && bIntersects && iZ >= m_minZoomLevel; --iZ)
5121 : {
5122 360 : auto ovrTileMatrix = tileMatrixList[iZ];
5123 180 : int nOvrMinTileX = 0;
5124 180 : int nOvrMinTileY = 0;
5125 180 : int nOvrMaxTileX = 0;
5126 180 : int nOvrMaxTileY = 0;
5127 180 : bRet =
5128 360 : GetTileIndices(ovrTileMatrix, bInvertAxisTMS, m_tileSize, adfExtent,
5129 : nOvrMinTileX, nOvrMinTileY, nOvrMaxTileX,
5130 180 : nOvrMaxTileY, m_noIntersectionIsOK, bIntersects);
5131 180 : if (bIntersects)
5132 : {
5133 180 : nTotalTiles +=
5134 180 : static_cast<uint64_t>(nOvrMaxTileY - nOvrMinTileY + 1) *
5135 180 : (nOvrMaxTileX - nOvrMinTileX + 1);
5136 : }
5137 : }
5138 :
5139 : /* -------------------------------------------------------------------- */
5140 : /* Generate tiles at max zoom level */
5141 : /* -------------------------------------------------------------------- */
5142 344 : GDALWarpOperation oWO;
5143 :
5144 172 : bRet = oWO.Initialize(psWO.get()) == CE_None && bRet;
5145 :
5146 : const auto GetUpdatedCreationOptions =
5147 668 : [this](const gdal::TileMatrixSet::TileMatrix &oTM)
5148 : {
5149 230 : CPLStringList aosCreationOptions(m_creationOptions);
5150 230 : if (m_format == "GTiff")
5151 : {
5152 48 : if (aosCreationOptions.FetchNameValue("TILED") == nullptr &&
5153 24 : aosCreationOptions.FetchNameValue("BLOCKYSIZE") == nullptr)
5154 : {
5155 24 : if (oTM.mTileWidth <= 512 && oTM.mTileHeight <= 512)
5156 : {
5157 22 : aosCreationOptions.SetNameValue(
5158 22 : "BLOCKYSIZE", CPLSPrintf("%d", oTM.mTileHeight));
5159 : }
5160 : else
5161 : {
5162 2 : aosCreationOptions.SetNameValue("TILED", "YES");
5163 : }
5164 : }
5165 24 : if (aosCreationOptions.FetchNameValue("COMPRESS") == nullptr)
5166 24 : aosCreationOptions.SetNameValue("COMPRESS", "LZW");
5167 : }
5168 206 : else if (m_format == "COG")
5169 : {
5170 2 : if (aosCreationOptions.FetchNameValue("OVERVIEW_RESAMPLING") ==
5171 : nullptr)
5172 : {
5173 : aosCreationOptions.SetNameValue("OVERVIEW_RESAMPLING",
5174 2 : m_overviewResampling.c_str());
5175 : }
5176 2 : if (aosCreationOptions.FetchNameValue("BLOCKSIZE") == nullptr &&
5177 2 : oTM.mTileWidth <= 512 && oTM.mTileWidth == oTM.mTileHeight)
5178 : {
5179 : aosCreationOptions.SetNameValue(
5180 2 : "BLOCKSIZE", CPLSPrintf("%d", oTM.mTileWidth));
5181 : }
5182 : }
5183 230 : return aosCreationOptions;
5184 172 : };
5185 :
5186 172 : VSIMkdir(m_output.c_str(), 0755);
5187 : VSIStatBufL sStat;
5188 172 : if (VSIStatL(m_output.c_str(), &sStat) != 0 || !VSI_ISDIR(sStat.st_mode))
5189 : {
5190 1 : ReportError(CE_Failure, CPLE_FileIO,
5191 : "Cannot create output directory %s", m_output.c_str());
5192 1 : return false;
5193 : }
5194 :
5195 342 : OGRSpatialReference oWGS84;
5196 171 : oWGS84.importFromEPSG(4326);
5197 171 : oWGS84.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
5198 :
5199 171 : std::unique_ptr<OGRCoordinateTransformation> poCTToWGS84;
5200 171 : if (!oSRS_TMS.IsEmpty())
5201 : {
5202 340 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
5203 170 : poCTToWGS84.reset(
5204 : OGRCreateCoordinateTransformation(&oSRS_TMS, &oWGS84));
5205 : }
5206 :
5207 177 : const bool kmlCompatible = m_kml &&
5208 57 : [this, &poTMS, &poCTToWGS84, bInvertAxisTMS]()
5209 : {
5210 6 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
5211 6 : double dfX = poTMS->tileMatrixList()[0].mTopLeftX;
5212 6 : double dfY = poTMS->tileMatrixList()[0].mTopLeftY;
5213 6 : if (bInvertAxisTMS)
5214 0 : std::swap(dfX, dfY);
5215 11 : return (m_minZoomLevel == m_maxZoomLevel ||
5216 5 : (poTMS->haveAllLevelsSameTopLeft() &&
5217 5 : poTMS->haveAllLevelsSameTileSize() &&
5218 11 : poTMS->hasOnlyPowerOfTwoVaryingScales())) &&
5219 23 : poCTToWGS84 && poCTToWGS84->Transform(1, &dfX, &dfY);
5220 6 : }();
5221 : const int kmlTileSize =
5222 171 : m_tileSize > 0 ? m_tileSize : poTMS->tileMatrixList()[0].mTileWidth;
5223 171 : if (m_kml && !kmlCompatible)
5224 : {
5225 0 : ReportError(CE_Failure, CPLE_NotSupported,
5226 : "Tiling scheme not compatible with KML output");
5227 0 : return false;
5228 : }
5229 :
5230 171 : if (m_title.empty())
5231 135 : m_title = CPLGetFilename(m_inputDataset[0].GetName().c_str());
5232 :
5233 171 : if (!m_url.empty())
5234 : {
5235 3 : if (m_url.back() != '/')
5236 2 : m_url += '/';
5237 6 : std::string out_path = m_output;
5238 3 : if (m_output.back() == '/')
5239 0 : out_path.pop_back();
5240 3 : m_url += CPLGetFilename(out_path.c_str());
5241 : }
5242 :
5243 342 : CPLWorkerThreadPool oThreadPool;
5244 :
5245 171 : bool bThreadPoolInitialized = false;
5246 : const auto InitThreadPool =
5247 812 : [this, &oThreadPool, &bRet, &bThreadPoolInitialized]()
5248 : {
5249 230 : if (!bThreadPoolInitialized)
5250 : {
5251 170 : bThreadPoolInitialized = true;
5252 :
5253 170 : if (bRet && m_numThreads > 1)
5254 : {
5255 6 : CPLDebug("gdal_raster_tile", "Using %d threads", m_numThreads);
5256 6 : bRet = oThreadPool.Setup(m_numThreads, nullptr, nullptr);
5257 : }
5258 : }
5259 :
5260 230 : return bRet;
5261 171 : };
5262 :
5263 : // Just for unit test purposes
5264 171 : const bool bEmitSpuriousCharsOnStdout = CPLTestBool(
5265 : CPLGetConfigOption("GDAL_RASTER_TILE_EMIT_SPURIOUS_CHARS", "NO"));
5266 :
5267 38 : const auto IsCompatibleOfSpawnSilent = [bSrcIsFineForFork, this]()
5268 : {
5269 10 : const char *pszErrorMsg = "";
5270 : {
5271 10 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
5272 10 : if (IsCompatibleOfSpawn(pszErrorMsg))
5273 : {
5274 3 : m_parallelMethod = "spawn";
5275 3 : return true;
5276 : }
5277 : }
5278 : (void)bSrcIsFineForFork;
5279 : #ifdef FORK_ALLOWED
5280 7 : if (bSrcIsFineForFork && !cpl::starts_with(m_output, "/vsimem/"))
5281 : {
5282 4 : if (CPLGetCurrentThreadCount() == 1)
5283 : {
5284 1 : CPLDebugOnce(
5285 : "gdal_raster_tile",
5286 : "'gdal' binary not found. Using instead "
5287 : "parallel-method=fork. If causing instability issues, set "
5288 : "parallel-method to 'thread' or 'spawn'");
5289 1 : m_parallelMethod = "fork";
5290 1 : return true;
5291 : }
5292 : }
5293 : #endif
5294 6 : return false;
5295 171 : };
5296 :
5297 171 : m_numThreads = std::max(
5298 342 : 1, static_cast<int>(std::min<uint64_t>(
5299 171 : m_numThreads, nBaseTiles / GetThresholdMinTilesPerJob())));
5300 :
5301 171 : std::atomic<bool> bParentAskedForStop = false;
5302 342 : std::thread threadWaitForParentStop;
5303 171 : std::unique_ptr<CPLErrorHandlerPusher> poErrorHandlerPusher;
5304 171 : if (m_spawned)
5305 : {
5306 : // Redirect errors to stdout so the parent listens on a single
5307 : // file descriptor.
5308 : poErrorHandlerPusher =
5309 50 : std::make_unique<CPLErrorHandlerPusher>(SpawnedErrorHandler);
5310 :
5311 100 : threadWaitForParentStop = std::thread(
5312 100 : [&bParentAskedForStop]()
5313 : {
5314 50 : char szBuffer[81] = {0};
5315 50 : while (fgets(szBuffer, 80, stdin))
5316 : {
5317 50 : if (strcmp(szBuffer, STOP_MARKER) == 0)
5318 : {
5319 50 : bParentAskedForStop = true;
5320 50 : break;
5321 : }
5322 : else
5323 : {
5324 0 : CPLError(CE_Failure, CPLE_AppDefined,
5325 : "Got unexpected input from parent '%s'",
5326 : szBuffer);
5327 : }
5328 : }
5329 100 : });
5330 : }
5331 : #ifdef FORK_ALLOWED
5332 121 : else if (m_forked)
5333 : {
5334 0 : threadWaitForParentStop = std::thread(
5335 0 : [&bParentAskedForStop]()
5336 : {
5337 0 : std::string buffer;
5338 0 : buffer.resize(strlen(STOP_MARKER));
5339 0 : if (CPLPipeRead(pipeIn, buffer.data(),
5340 0 : static_cast<int>(strlen(STOP_MARKER))) &&
5341 0 : buffer == STOP_MARKER)
5342 : {
5343 0 : bParentAskedForStop = true;
5344 : }
5345 : else
5346 : {
5347 0 : CPLError(CE_Failure, CPLE_AppDefined,
5348 : "Got unexpected input from parent '%s'",
5349 : buffer.c_str());
5350 : }
5351 0 : });
5352 : }
5353 : #endif
5354 :
5355 171 : if (m_ovrZoomLevel >= 0)
5356 : {
5357 : // do not generate base tiles if called as a child process with
5358 : // --ovr-zoom-level
5359 : }
5360 165 : else if (m_numThreads > 1 && nBaseTiles > 1 &&
5361 13 : ((m_parallelMethod.empty() &&
5362 9 : m_numThreads >= GetThresholdMinThreadsForSpawn() &&
5363 15 : IsCompatibleOfSpawnSilent()) ||
5364 16 : (m_parallelMethod == "spawn" || m_parallelMethod == "fork")))
5365 : {
5366 7 : if (!GenerateBaseTilesSpawnMethod(nBaseTilesPerCol, nBaseTilesPerRow,
5367 : nMinTileX, nMinTileY, nMaxTileX,
5368 : nMaxTileY, nTotalTiles, nBaseTiles,
5369 : pfnProgress, pProgressData))
5370 : {
5371 1 : return false;
5372 : }
5373 6 : nCurTile = nBaseTiles;
5374 : }
5375 : else
5376 : {
5377 : // Branch for multi-threaded or single-threaded max zoom level tile
5378 : // generation
5379 :
5380 : PerThreadMaxZoomResourceManager oResourceManager(
5381 132 : m_poSrcDS, psWO.get(), hTransformArg.get(), oFakeMaxZoomDS,
5382 396 : dstBuffer.size());
5383 :
5384 : const CPLStringList aosCreationOptions(
5385 264 : GetUpdatedCreationOptions(tileMatrix));
5386 :
5387 132 : CPLDebug("gdal_raster_tile",
5388 : "Generating tiles z=%d, y=%d...%d, x=%d...%d", m_maxZoomLevel,
5389 : nMinTileY, nMaxTileY, nMinTileX, nMaxTileX);
5390 :
5391 132 : bRet &= InitThreadPool();
5392 :
5393 132 : if (bRet && m_numThreads > 1)
5394 : {
5395 6 : std::atomic<bool> bFailure = false;
5396 6 : std::atomic<int> nQueuedJobs = 0;
5397 :
5398 : double dfTilesYPerJob;
5399 : int nYOuterIterations;
5400 : double dfTilesXPerJob;
5401 : int nXOuterIterations;
5402 6 : ComputeJobChunkSize(m_numThreads, nBaseTilesPerCol,
5403 : nBaseTilesPerRow, dfTilesYPerJob,
5404 : nYOuterIterations, dfTilesXPerJob,
5405 : nXOuterIterations);
5406 :
5407 6 : CPLDebugOnly("gdal_raster_tile",
5408 : "nYOuterIterations=%d, dfTilesYPerJob=%g, "
5409 : "nXOuterIterations=%d, dfTilesXPerJob=%g",
5410 : nYOuterIterations, dfTilesYPerJob, nXOuterIterations,
5411 : dfTilesXPerJob);
5412 :
5413 6 : int nLastYEndIncluded = nMinTileY - 1;
5414 30 : for (int iYOuterIter = 0; bRet && iYOuterIter < nYOuterIterations &&
5415 24 : nLastYEndIncluded < nMaxTileY;
5416 : ++iYOuterIter)
5417 : {
5418 24 : const int iYStart = nLastYEndIncluded + 1;
5419 : const int iYEndIncluded =
5420 24 : iYOuterIter + 1 == nYOuterIterations
5421 42 : ? nMaxTileY
5422 : : std::max(
5423 : iYStart,
5424 42 : static_cast<int>(std::floor(
5425 18 : nMinTileY +
5426 18 : (iYOuterIter + 1) * dfTilesYPerJob - 1)));
5427 :
5428 24 : nLastYEndIncluded = iYEndIncluded;
5429 :
5430 24 : int nLastXEndIncluded = nMinTileX - 1;
5431 24 : for (int iXOuterIter = 0;
5432 48 : bRet && iXOuterIter < nXOuterIterations &&
5433 24 : nLastXEndIncluded < nMaxTileX;
5434 : ++iXOuterIter)
5435 : {
5436 24 : const int iXStart = nLastXEndIncluded + 1;
5437 : const int iXEndIncluded =
5438 24 : iXOuterIter + 1 == nXOuterIterations
5439 24 : ? nMaxTileX
5440 : : std::max(
5441 : iXStart,
5442 24 : static_cast<int>(std::floor(
5443 0 : nMinTileX +
5444 0 : (iXOuterIter + 1) * dfTilesXPerJob - 1)));
5445 :
5446 24 : nLastXEndIncluded = iXEndIncluded;
5447 :
5448 24 : CPLDebugOnly("gdal_raster_tile",
5449 : "Job for y in [%d,%d] and x in [%d,%d]",
5450 : iYStart, iYEndIncluded, iXStart,
5451 : iXEndIncluded);
5452 :
5453 24 : auto job = [this, &oThreadPool, &oResourceManager,
5454 : &bFailure, &bParentAskedForStop, &nCurTile,
5455 : &nQueuedJobs, pszExtension, &aosCreationOptions,
5456 : &psWO, &tileMatrix, nDstBands, iXStart,
5457 : iXEndIncluded, iYStart, iYEndIncluded,
5458 : nMinTileX, nMinTileY, &poColorTable,
5459 2091 : bUserAskedForAlpha]()
5460 : {
5461 24 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
5462 :
5463 24 : auto resources = oResourceManager.AcquireResources();
5464 24 : if (resources)
5465 : {
5466 24 : std::vector<GByte> tmpBuffer;
5467 60 : for (int iY = iYStart;
5468 60 : iY <= iYEndIncluded && !bParentAskedForStop;
5469 : ++iY)
5470 : {
5471 462 : for (int iX = iXStart; iX <= iXEndIncluded &&
5472 219 : !bParentAskedForStop;
5473 : ++iX)
5474 : {
5475 621 : if (!GenerateTile(
5476 207 : resources->poSrcDS.get(),
5477 : m_poDstDriver, pszExtension,
5478 : aosCreationOptions.List(),
5479 207 : *(resources->poWO.get()),
5480 207 : *(resources->poFakeMaxZoomDS
5481 207 : ->GetSpatialRef()),
5482 207 : psWO->eWorkingDataType, tileMatrix,
5483 207 : m_output, nDstBands,
5484 207 : psWO->padfDstNoDataReal
5485 0 : ? &(psWO->padfDstNoDataReal[0])
5486 : : nullptr,
5487 : m_maxZoomLevel, iX, iY,
5488 207 : m_convention, nMinTileX, nMinTileY,
5489 207 : m_skipBlank, bUserAskedForAlpha,
5490 207 : m_auxXML, m_resume, m_metadata,
5491 207 : poColorTable.get(),
5492 207 : resources->dstBuffer, tmpBuffer))
5493 : {
5494 0 : oResourceManager.SetError();
5495 0 : bFailure = true;
5496 0 : --nQueuedJobs;
5497 0 : return;
5498 : }
5499 207 : ++nCurTile;
5500 207 : oThreadPool.WakeUpWaitEvent();
5501 : }
5502 : }
5503 24 : oResourceManager.ReleaseResources(
5504 24 : std::move(resources));
5505 : }
5506 : else
5507 : {
5508 0 : oResourceManager.SetError();
5509 0 : bFailure = true;
5510 : }
5511 :
5512 24 : --nQueuedJobs;
5513 24 : };
5514 :
5515 24 : ++nQueuedJobs;
5516 24 : oThreadPool.SubmitJob(std::move(job));
5517 : }
5518 : }
5519 :
5520 : // Wait for completion of all jobs
5521 210 : while (bRet && nQueuedJobs > 0)
5522 : {
5523 204 : oThreadPool.WaitEvent();
5524 204 : bRet &= !bFailure;
5525 274 : if (bRet && pfnProgress &&
5526 140 : !pfnProgress(static_cast<double>(nCurTile) /
5527 70 : static_cast<double>(nTotalTiles),
5528 : "", pProgressData))
5529 : {
5530 3 : bParentAskedForStop = true;
5531 3 : bRet = false;
5532 3 : CPLError(CE_Failure, CPLE_UserInterrupt,
5533 : "Process interrupted by user");
5534 : }
5535 : }
5536 6 : oThreadPool.WaitCompletion();
5537 6 : bRet &=
5538 10 : !bFailure && (!pfnProgress ||
5539 8 : pfnProgress(static_cast<double>(nCurTile) /
5540 4 : static_cast<double>(nTotalTiles),
5541 6 : "", pProgressData));
5542 :
5543 6 : if (!oResourceManager.GetErrorMsg().empty())
5544 : {
5545 : // Re-emit error message from worker thread to main thread
5546 0 : ReportError(CE_Failure, CPLE_AppDefined, "%s",
5547 0 : oResourceManager.GetErrorMsg().c_str());
5548 6 : }
5549 : }
5550 : else
5551 : {
5552 : // Branch for single-thread max zoom level tile generation
5553 252 : std::vector<GByte> tmpBuffer;
5554 318 : for (int iY = nMinTileY;
5555 318 : bRet && !bParentAskedForStop && iY <= nMaxTileY; ++iY)
5556 : {
5557 767 : for (int iX = nMinTileX;
5558 767 : bRet && !bParentAskedForStop && iX <= nMaxTileX; ++iX)
5559 : {
5560 1150 : bRet = GenerateTile(
5561 : m_poSrcDS, m_poDstDriver, pszExtension,
5562 : aosCreationOptions.List(), oWO, oSRS_TMS,
5563 575 : psWO->eWorkingDataType, tileMatrix, m_output, nDstBands,
5564 575 : psWO->padfDstNoDataReal ? &(psWO->padfDstNoDataReal[0])
5565 : : nullptr,
5566 575 : m_maxZoomLevel, iX, iY, m_convention, nMinTileX,
5567 575 : nMinTileY, m_skipBlank, bUserAskedForAlpha, m_auxXML,
5568 575 : m_resume, m_metadata, poColorTable.get(), dstBuffer,
5569 : tmpBuffer);
5570 :
5571 575 : if (m_spawned)
5572 : {
5573 258 : if (bEmitSpuriousCharsOnStdout)
5574 64 : fwrite(&PROGRESS_MARKER[0], 1, 1, stdout);
5575 258 : fwrite(PROGRESS_MARKER, sizeof(PROGRESS_MARKER), 1,
5576 : stdout);
5577 258 : fflush(stdout);
5578 : }
5579 : #ifdef FORK_ALLOWED
5580 317 : else if (m_forked)
5581 : {
5582 0 : CPLPipeWrite(pipeOut, PROGRESS_MARKER,
5583 : sizeof(PROGRESS_MARKER));
5584 : }
5585 : #endif
5586 : else
5587 : {
5588 317 : ++nCurTile;
5589 336 : if (bRet && pfnProgress &&
5590 38 : !pfnProgress(static_cast<double>(nCurTile) /
5591 19 : static_cast<double>(nTotalTiles),
5592 : "", pProgressData))
5593 : {
5594 1 : bRet = false;
5595 1 : CPLError(CE_Failure, CPLE_UserInterrupt,
5596 : "Process interrupted by user");
5597 : }
5598 : }
5599 : }
5600 : }
5601 : }
5602 :
5603 132 : if (m_kml && bRet)
5604 : {
5605 14 : for (int iY = nMinTileY; iY <= nMaxTileY; ++iY)
5606 : {
5607 26 : for (int iX = nMinTileX; iX <= nMaxTileX; ++iX)
5608 : {
5609 : const int nFileY =
5610 18 : GetFileY(iY, poTMS->tileMatrixList()[m_maxZoomLevel],
5611 18 : m_convention);
5612 : std::string osFilename = CPLFormFilenameSafe(
5613 : m_output.c_str(), CPLSPrintf("%d", m_maxZoomLevel),
5614 36 : nullptr);
5615 36 : osFilename = CPLFormFilenameSafe(
5616 18 : osFilename.c_str(), CPLSPrintf("%d", iX), nullptr);
5617 36 : osFilename = CPLFormFilenameSafe(
5618 : osFilename.c_str(),
5619 18 : CPLSPrintf("%d.%s", nFileY, pszExtension), nullptr);
5620 18 : if (VSIStatL(osFilename.c_str(), &sStat) == 0)
5621 : {
5622 36 : GenerateKML(m_output, m_title, iX, iY, m_maxZoomLevel,
5623 18 : kmlTileSize, pszExtension, m_url,
5624 18 : poTMS.get(), bInvertAxisTMS, m_convention,
5625 : poCTToWGS84.get(), {});
5626 : }
5627 : }
5628 : }
5629 : }
5630 : }
5631 :
5632 : // Close source dataset if we have opened it (in GDALAlgorithm core code),
5633 : // to free file descriptors, particularly if it is a VRT file.
5634 170 : std::vector<GDALColorInterp> aeColorInterp;
5635 619 : for (int i = 1; i <= m_poSrcDS->GetRasterCount(); ++i)
5636 449 : aeColorInterp.push_back(
5637 449 : m_poSrcDS->GetRasterBand(i)->GetColorInterpretation());
5638 170 : if (m_inputDataset[0].HasDatasetBeenOpenedByAlgorithm())
5639 : {
5640 117 : m_inputDataset[0].Close();
5641 117 : m_poSrcDS = nullptr;
5642 : }
5643 :
5644 : /* -------------------------------------------------------------------- */
5645 : /* Generate tiles at lower zoom levels */
5646 : /* -------------------------------------------------------------------- */
5647 170 : const int iZStart =
5648 170 : m_ovrZoomLevel >= 0 ? m_ovrZoomLevel : m_maxZoomLevel - 1;
5649 170 : const int iZEnd = m_ovrZoomLevel >= 0 ? m_ovrZoomLevel : m_minZoomLevel;
5650 280 : for (int iZ = iZStart; bRet && iZ >= iZEnd; --iZ)
5651 : {
5652 110 : int nOvrMinTileX = 0;
5653 110 : int nOvrMinTileY = 0;
5654 110 : int nOvrMaxTileX = 0;
5655 110 : int nOvrMaxTileY = 0;
5656 :
5657 220 : auto ovrTileMatrix = tileMatrixList[iZ];
5658 110 : CPL_IGNORE_RET_VAL(
5659 110 : GetTileIndices(ovrTileMatrix, bInvertAxisTMS, m_tileSize, adfExtent,
5660 : nOvrMinTileX, nOvrMinTileY, nOvrMaxTileX,
5661 110 : nOvrMaxTileY, m_noIntersectionIsOK, bIntersects));
5662 :
5663 110 : bRet = bIntersects;
5664 :
5665 110 : if (m_minOvrTileX >= 0)
5666 : {
5667 32 : bRet = true;
5668 32 : nOvrMinTileX = m_minOvrTileX;
5669 32 : nOvrMinTileY = m_minOvrTileY;
5670 32 : nOvrMaxTileX = m_maxOvrTileX;
5671 32 : nOvrMaxTileY = m_maxOvrTileY;
5672 : }
5673 :
5674 110 : if (bRet)
5675 : {
5676 110 : CPLDebug("gdal_raster_tile",
5677 : "Generating overview tiles z=%d, y=%d...%d, x=%d...%d", iZ,
5678 : nOvrMinTileY, nOvrMaxTileY, nOvrMinTileX, nOvrMaxTileX);
5679 : }
5680 :
5681 110 : const int nOvrTilesPerCol = nOvrMaxTileY - nOvrMinTileY + 1;
5682 110 : const int nOvrTilesPerRow = nOvrMaxTileX - nOvrMinTileX + 1;
5683 110 : const uint64_t nOvrTileCount =
5684 110 : static_cast<uint64_t>(nOvrTilesPerCol) * nOvrTilesPerRow;
5685 :
5686 110 : m_numThreads = std::max(
5687 220 : 1,
5688 220 : static_cast<int>(std::min<uint64_t>(
5689 110 : m_numThreads, nOvrTileCount / GetThresholdMinTilesPerJob())));
5690 :
5691 146 : if (m_numThreads > 1 && nOvrTileCount > 1 &&
5692 18 : ((m_parallelMethod.empty() &&
5693 6 : m_numThreads >= GetThresholdMinThreadsForSpawn() &&
5694 22 : IsCompatibleOfSpawnSilent()) ||
5695 28 : (m_parallelMethod == "spawn" || m_parallelMethod == "fork")))
5696 : {
5697 12 : bRet &= GenerateOverviewTilesSpawnMethod(
5698 : iZ, nOvrMinTileX, nOvrMinTileY, nOvrMaxTileX, nOvrMaxTileY,
5699 12 : nCurTile, nTotalTiles, pfnProgress, pProgressData);
5700 : }
5701 : else
5702 : {
5703 98 : bRet &= InitThreadPool();
5704 :
5705 196 : auto srcTileMatrix = tileMatrixList[iZ + 1];
5706 98 : int nSrcMinTileX = 0;
5707 98 : int nSrcMinTileY = 0;
5708 98 : int nSrcMaxTileX = 0;
5709 98 : int nSrcMaxTileY = 0;
5710 :
5711 98 : CPL_IGNORE_RET_VAL(GetTileIndices(
5712 : srcTileMatrix, bInvertAxisTMS, m_tileSize, adfExtent,
5713 : nSrcMinTileX, nSrcMinTileY, nSrcMaxTileX, nSrcMaxTileY,
5714 98 : m_noIntersectionIsOK, bIntersects));
5715 :
5716 98 : constexpr double EPSILON = 1e-3;
5717 98 : int maxCacheTileSizePerThread = static_cast<int>(
5718 98 : (1 + std::ceil(
5719 98 : (ovrTileMatrix.mResY * ovrTileMatrix.mTileHeight) /
5720 98 : (srcTileMatrix.mResY * srcTileMatrix.mTileHeight) -
5721 98 : EPSILON)) *
5722 98 : (1 + std::ceil(
5723 98 : (ovrTileMatrix.mResX * ovrTileMatrix.mTileWidth) /
5724 98 : (srcTileMatrix.mResX * srcTileMatrix.mTileWidth) -
5725 : EPSILON)));
5726 :
5727 98 : CPLDebugOnly("gdal_raster_tile",
5728 : "Ideal maxCacheTileSizePerThread = %d",
5729 : maxCacheTileSizePerThread);
5730 :
5731 : #ifndef _WIN32
5732 : const int remainingFileDescriptorCount =
5733 98 : CPLGetRemainingFileDescriptorCount();
5734 98 : CPLDebugOnly("gdal_raster_tile",
5735 : "remainingFileDescriptorCount = %d",
5736 : remainingFileDescriptorCount);
5737 98 : if (remainingFileDescriptorCount >= 0 &&
5738 : remainingFileDescriptorCount <
5739 98 : (1 + maxCacheTileSizePerThread) * m_numThreads)
5740 : {
5741 : const int newNumThreads =
5742 0 : std::max(1, remainingFileDescriptorCount /
5743 0 : (1 + maxCacheTileSizePerThread));
5744 0 : if (newNumThreads < m_numThreads)
5745 : {
5746 0 : CPLError(CE_Warning, CPLE_AppDefined,
5747 : "Not enough file descriptors available given the "
5748 : "number of "
5749 : "threads. Reducing the number of threads %d to %d",
5750 : m_numThreads, newNumThreads);
5751 0 : m_numThreads = newNumThreads;
5752 : }
5753 : }
5754 : #endif
5755 :
5756 : MosaicDataset oSrcDS(
5757 196 : CPLFormFilenameSafe(m_output.c_str(), CPLSPrintf("%d", iZ + 1),
5758 : nullptr),
5759 98 : pszExtension, m_format, aeColorInterp, srcTileMatrix, oSRS_TMS,
5760 : nSrcMinTileX, nSrcMinTileY, nSrcMaxTileX, nSrcMaxTileY,
5761 98 : m_convention, nDstBands, psWO->eWorkingDataType,
5762 9 : psWO->padfDstNoDataReal ? &(psWO->padfDstNoDataReal[0])
5763 : : nullptr,
5764 499 : m_metadata, poColorTable.get(), maxCacheTileSizePerThread);
5765 :
5766 : const CPLStringList aosCreationOptions(
5767 196 : GetUpdatedCreationOptions(ovrTileMatrix));
5768 :
5769 196 : PerThreadLowerZoomResourceManager oResourceManager(oSrcDS);
5770 98 : std::atomic<bool> bFailure = false;
5771 98 : std::atomic<int> nQueuedJobs = 0;
5772 :
5773 98 : const bool bUseThreads = m_numThreads > 1 && nOvrTileCount > 1;
5774 :
5775 98 : if (bUseThreads)
5776 : {
5777 : double dfTilesYPerJob;
5778 : int nYOuterIterations;
5779 : double dfTilesXPerJob;
5780 : int nXOuterIterations;
5781 6 : ComputeJobChunkSize(m_numThreads, nOvrTilesPerCol,
5782 : nOvrTilesPerRow, dfTilesYPerJob,
5783 : nYOuterIterations, dfTilesXPerJob,
5784 : nXOuterIterations);
5785 :
5786 6 : CPLDebugOnly("gdal_raster_tile",
5787 : "z=%d, nYOuterIterations=%d, dfTilesYPerJob=%g, "
5788 : "nXOuterIterations=%d, dfTilesXPerJob=%g",
5789 : iZ, nYOuterIterations, dfTilesYPerJob,
5790 : nXOuterIterations, dfTilesXPerJob);
5791 :
5792 6 : int nLastYEndIncluded = nOvrMinTileY - 1;
5793 24 : for (int iYOuterIter = 0;
5794 24 : bRet && iYOuterIter < nYOuterIterations &&
5795 18 : nLastYEndIncluded < nOvrMaxTileY;
5796 : ++iYOuterIter)
5797 : {
5798 18 : const int iYStart = nLastYEndIncluded + 1;
5799 : const int iYEndIncluded =
5800 18 : iYOuterIter + 1 == nYOuterIterations
5801 30 : ? nOvrMaxTileY
5802 : : std::max(
5803 : iYStart,
5804 30 : static_cast<int>(std::floor(
5805 12 : nOvrMinTileY +
5806 12 : (iYOuterIter + 1) * dfTilesYPerJob - 1)));
5807 :
5808 18 : nLastYEndIncluded = iYEndIncluded;
5809 :
5810 18 : int nLastXEndIncluded = nOvrMinTileX - 1;
5811 18 : for (int iXOuterIter = 0;
5812 42 : bRet && iXOuterIter < nXOuterIterations &&
5813 24 : nLastXEndIncluded < nOvrMaxTileX;
5814 : ++iXOuterIter)
5815 : {
5816 24 : const int iXStart = nLastXEndIncluded + 1;
5817 : const int iXEndIncluded =
5818 24 : iXOuterIter + 1 == nXOuterIterations
5819 30 : ? nOvrMaxTileX
5820 30 : : std::max(iXStart, static_cast<int>(std::floor(
5821 6 : nOvrMinTileX +
5822 6 : (iXOuterIter + 1) *
5823 : dfTilesXPerJob -
5824 6 : 1)));
5825 :
5826 24 : nLastXEndIncluded = iXEndIncluded;
5827 :
5828 24 : CPLDebugOnly(
5829 : "gdal_raster_tile",
5830 : "Job for z=%d, y in [%d,%d] and x in [%d,%d]", iZ,
5831 : iYStart, iYEndIncluded, iXStart, iXEndIncluded);
5832 : auto job =
5833 24 : [this, &oThreadPool, &oResourceManager, &bFailure,
5834 : &bParentAskedForStop, &nCurTile, &nQueuedJobs,
5835 : pszExtension, &aosCreationOptions, &aosWarpOptions,
5836 : &ovrTileMatrix, iZ, iXStart, iXEndIncluded,
5837 588 : iYStart, iYEndIncluded, bUserAskedForAlpha]()
5838 : {
5839 : CPLErrorStateBackuper oBackuper(
5840 24 : CPLQuietErrorHandler);
5841 :
5842 : auto resources =
5843 24 : oResourceManager.AcquireResources();
5844 24 : if (resources)
5845 : {
5846 72 : for (int iY = iYStart; iY <= iYEndIncluded &&
5847 24 : !bParentAskedForStop;
5848 : ++iY)
5849 : {
5850 84 : for (int iX = iXStart;
5851 144 : iX <= iXEndIncluded &&
5852 60 : !bParentAskedForStop;
5853 : ++iX)
5854 : {
5855 120 : if (!GenerateOverviewTile(
5856 60 : *(resources->poSrcDS.get()),
5857 60 : m_poDstDriver, m_format,
5858 : pszExtension,
5859 : aosCreationOptions.List(),
5860 60 : aosWarpOptions.List(),
5861 60 : m_overviewResampling,
5862 60 : ovrTileMatrix, m_output, iZ, iX,
5863 60 : iY, m_convention, m_skipBlank,
5864 60 : bUserAskedForAlpha, m_auxXML,
5865 60 : m_resume))
5866 : {
5867 0 : oResourceManager.SetError();
5868 0 : bFailure = true;
5869 0 : --nQueuedJobs;
5870 0 : return;
5871 : }
5872 :
5873 60 : ++nCurTile;
5874 60 : oThreadPool.WakeUpWaitEvent();
5875 : }
5876 : }
5877 24 : oResourceManager.ReleaseResources(
5878 24 : std::move(resources));
5879 : }
5880 : else
5881 : {
5882 0 : oResourceManager.SetError();
5883 0 : bFailure = true;
5884 : }
5885 24 : --nQueuedJobs;
5886 24 : };
5887 :
5888 24 : ++nQueuedJobs;
5889 24 : oThreadPool.SubmitJob(std::move(job));
5890 : }
5891 : }
5892 :
5893 : // Wait for completion of all jobs
5894 73 : while (bRet && nQueuedJobs > 0)
5895 : {
5896 67 : oThreadPool.WaitEvent();
5897 67 : bRet &= !bFailure;
5898 88 : if (bRet && pfnProgress &&
5899 42 : !pfnProgress(static_cast<double>(nCurTile) /
5900 21 : static_cast<double>(nTotalTiles),
5901 : "", pProgressData))
5902 : {
5903 0 : bParentAskedForStop = true;
5904 0 : bRet = false;
5905 0 : CPLError(CE_Failure, CPLE_UserInterrupt,
5906 : "Process interrupted by user");
5907 : }
5908 : }
5909 6 : oThreadPool.WaitCompletion();
5910 8 : bRet &= !bFailure &&
5911 2 : (!pfnProgress ||
5912 4 : pfnProgress(static_cast<double>(nCurTile) /
5913 2 : static_cast<double>(nTotalTiles),
5914 6 : "", pProgressData));
5915 :
5916 6 : if (!oResourceManager.GetErrorMsg().empty())
5917 : {
5918 : // Re-emit error message from worker thread to main thread
5919 0 : ReportError(CE_Failure, CPLE_AppDefined, "%s",
5920 0 : oResourceManager.GetErrorMsg().c_str());
5921 : }
5922 : }
5923 : else
5924 : {
5925 : // Branch for single-thread overview generation
5926 :
5927 193 : for (int iY = nOvrMinTileY;
5928 193 : bRet && !bParentAskedForStop && iY <= nOvrMaxTileY; ++iY)
5929 : {
5930 277 : for (int iX = nOvrMinTileX;
5931 277 : bRet && !bParentAskedForStop && iX <= nOvrMaxTileX;
5932 : ++iX)
5933 : {
5934 176 : bRet = GenerateOverviewTile(
5935 176 : oSrcDS, m_poDstDriver, m_format, pszExtension,
5936 176 : aosCreationOptions.List(), aosWarpOptions.List(),
5937 176 : m_overviewResampling, ovrTileMatrix, m_output, iZ,
5938 176 : iX, iY, m_convention, m_skipBlank,
5939 176 : bUserAskedForAlpha, m_auxXML, m_resume);
5940 :
5941 176 : if (m_spawned)
5942 : {
5943 80 : if (bEmitSpuriousCharsOnStdout)
5944 20 : fwrite(&PROGRESS_MARKER[0], 1, 1, stdout);
5945 80 : fwrite(PROGRESS_MARKER, sizeof(PROGRESS_MARKER), 1,
5946 : stdout);
5947 80 : fflush(stdout);
5948 : }
5949 : #ifdef FORK_ALLOWED
5950 96 : else if (m_forked)
5951 : {
5952 0 : CPLPipeWrite(pipeOut, PROGRESS_MARKER,
5953 : sizeof(PROGRESS_MARKER));
5954 : }
5955 : #endif
5956 : else
5957 : {
5958 96 : ++nCurTile;
5959 111 : if (bRet && pfnProgress &&
5960 15 : !pfnProgress(
5961 15 : static_cast<double>(nCurTile) /
5962 15 : static_cast<double>(nTotalTiles),
5963 : "", pProgressData))
5964 : {
5965 0 : bRet = false;
5966 0 : CPLError(CE_Failure, CPLE_UserInterrupt,
5967 : "Process interrupted by user");
5968 : }
5969 : }
5970 : }
5971 : }
5972 : }
5973 : }
5974 :
5975 110 : if (m_kml && bRet)
5976 : {
5977 10 : for (int iY = nOvrMinTileY; bRet && iY <= nOvrMaxTileY; ++iY)
5978 : {
5979 11 : for (int iX = nOvrMinTileX; bRet && iX <= nOvrMaxTileX; ++iX)
5980 : {
5981 : int nFileY =
5982 6 : GetFileY(iY, poTMS->tileMatrixList()[iZ], m_convention);
5983 : std::string osFilename = CPLFormFilenameSafe(
5984 12 : m_output.c_str(), CPLSPrintf("%d", iZ), nullptr);
5985 12 : osFilename = CPLFormFilenameSafe(
5986 6 : osFilename.c_str(), CPLSPrintf("%d", iX), nullptr);
5987 12 : osFilename = CPLFormFilenameSafe(
5988 : osFilename.c_str(),
5989 6 : CPLSPrintf("%d.%s", nFileY, pszExtension), nullptr);
5990 6 : if (VSIStatL(osFilename.c_str(), &sStat) == 0)
5991 : {
5992 6 : std::vector<TileCoordinates> children;
5993 :
5994 18 : for (int iChildY = 0; iChildY <= 1; ++iChildY)
5995 : {
5996 36 : for (int iChildX = 0; iChildX <= 1; ++iChildX)
5997 : {
5998 : nFileY =
5999 24 : GetFileY(iY * 2 + iChildY,
6000 24 : poTMS->tileMatrixList()[iZ + 1],
6001 24 : m_convention);
6002 48 : osFilename = CPLFormFilenameSafe(
6003 : m_output.c_str(), CPLSPrintf("%d", iZ + 1),
6004 24 : nullptr);
6005 48 : osFilename = CPLFormFilenameSafe(
6006 : osFilename.c_str(),
6007 24 : CPLSPrintf("%d", iX * 2 + iChildX),
6008 24 : nullptr);
6009 48 : osFilename = CPLFormFilenameSafe(
6010 : osFilename.c_str(),
6011 : CPLSPrintf("%d.%s", nFileY, pszExtension),
6012 24 : nullptr);
6013 24 : if (VSIStatL(osFilename.c_str(), &sStat) == 0)
6014 : {
6015 17 : TileCoordinates tc;
6016 17 : tc.nTileX = iX * 2 + iChildX;
6017 17 : tc.nTileY = iY * 2 + iChildY;
6018 17 : tc.nTileZ = iZ + 1;
6019 17 : children.push_back(std::move(tc));
6020 : }
6021 : }
6022 : }
6023 :
6024 12 : GenerateKML(m_output, m_title, iX, iY, iZ, kmlTileSize,
6025 6 : pszExtension, m_url, poTMS.get(),
6026 6 : bInvertAxisTMS, m_convention,
6027 : poCTToWGS84.get(), children);
6028 : }
6029 : }
6030 : }
6031 : }
6032 : }
6033 :
6034 1482 : const auto IsWebViewerEnabled = [this](const char *name)
6035 : {
6036 494 : return std::find_if(m_webviewers.begin(), m_webviewers.end(),
6037 705 : [name](const std::string &s)
6038 992 : { return s == "all" || s == name; }) !=
6039 988 : m_webviewers.end();
6040 170 : };
6041 :
6042 267 : if (m_ovrZoomLevel < 0 && bRet &&
6043 437 : poTMS->identifier() == "GoogleMapsCompatible" &&
6044 107 : IsWebViewerEnabled("leaflet"))
6045 : {
6046 66 : double dfSouthLat = -90;
6047 66 : double dfWestLon = -180;
6048 66 : double dfNorthLat = 90;
6049 66 : double dfEastLon = 180;
6050 :
6051 66 : if (poCTToWGS84)
6052 : {
6053 66 : poCTToWGS84->TransformBounds(
6054 : adfExtent[0], adfExtent[1], adfExtent[2], adfExtent[3],
6055 66 : &dfWestLon, &dfSouthLat, &dfEastLon, &dfNorthLat, 21);
6056 : }
6057 :
6058 66 : GenerateLeaflet(m_output, m_title, dfSouthLat, dfWestLon, dfNorthLat,
6059 : dfEastLon, m_minZoomLevel, m_maxZoomLevel,
6060 66 : tileMatrix.mTileWidth, pszExtension, m_url, m_copyright,
6061 66 : m_convention == "xyz");
6062 : }
6063 :
6064 170 : if (m_ovrZoomLevel < 0 && bRet && IsWebViewerEnabled("openlayers"))
6065 : {
6066 77 : GenerateOpenLayers(m_output, m_title, adfExtent[0], adfExtent[1],
6067 : adfExtent[2], adfExtent[3], m_minZoomLevel,
6068 : m_maxZoomLevel, tileMatrix.mTileWidth, pszExtension,
6069 77 : m_url, m_copyright, *(poTMS.get()), bInvertAxisTMS,
6070 77 : oSRS_TMS, m_convention == "xyz");
6071 : }
6072 :
6073 214 : if (m_ovrZoomLevel < 0 && bRet && IsWebViewerEnabled("mapml") &&
6074 384 : poTMS->identifier() != "raster" && m_convention == "xyz")
6075 : {
6076 40 : GenerateMapML(m_output, m_mapmlTemplate, m_title, nMinTileX, nMinTileY,
6077 : nMaxTileX, nMaxTileY, m_minZoomLevel, m_maxZoomLevel,
6078 40 : pszExtension, m_url, m_copyright, *(poTMS.get()));
6079 : }
6080 :
6081 245 : if (m_ovrZoomLevel < 0 && bRet && IsWebViewerEnabled("stac") &&
6082 75 : m_convention == "xyz")
6083 : {
6084 44 : OGRCoordinateTransformation *poCT = poCTToWGS84.get();
6085 0 : std::unique_ptr<OGRCoordinateTransformation> poCTToLongLat;
6086 44 : if (!poCTToWGS84)
6087 : {
6088 2 : CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
6089 2 : OGRSpatialReference oLongLat;
6090 1 : oLongLat.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
6091 1 : oLongLat.CopyGeogCSFrom(&oSRS_TMS);
6092 1 : poCTToLongLat.reset(
6093 : OGRCreateCoordinateTransformation(&oSRS_TMS, &oLongLat));
6094 1 : poCT = poCTToLongLat.get();
6095 : }
6096 :
6097 44 : double dfSouthLat = -90;
6098 44 : double dfWestLon = -180;
6099 44 : double dfNorthLat = 90;
6100 44 : double dfEastLon = 180;
6101 44 : if (poCT)
6102 : {
6103 43 : poCT->TransformBounds(adfExtent[0], adfExtent[1], adfExtent[2],
6104 : adfExtent[3], &dfWestLon, &dfSouthLat,
6105 43 : &dfEastLon, &dfNorthLat, 21);
6106 : }
6107 :
6108 88 : GenerateSTAC(m_output, m_title, dfWestLon, dfSouthLat, dfEastLon,
6109 44 : dfNorthLat, m_metadata, aoBandMetadata, m_minZoomLevel,
6110 44 : m_maxZoomLevel, pszExtension, m_format, m_url, m_copyright,
6111 44 : oSRS_TMS, *(poTMS.get()), bInvertAxisTMS, m_tileSize,
6112 44 : adfExtent, m_inputDataset[0]);
6113 : }
6114 :
6115 170 : if (m_ovrZoomLevel < 0 && bRet && m_kml)
6116 : {
6117 12 : std::vector<TileCoordinates> children;
6118 :
6119 6 : auto ovrTileMatrix = tileMatrixList[m_minZoomLevel];
6120 6 : int nOvrMinTileX = 0;
6121 6 : int nOvrMinTileY = 0;
6122 6 : int nOvrMaxTileX = 0;
6123 6 : int nOvrMaxTileY = 0;
6124 6 : CPL_IGNORE_RET_VAL(
6125 6 : GetTileIndices(ovrTileMatrix, bInvertAxisTMS, m_tileSize, adfExtent,
6126 : nOvrMinTileX, nOvrMinTileY, nOvrMaxTileX,
6127 6 : nOvrMaxTileY, m_noIntersectionIsOK, bIntersects));
6128 :
6129 12 : for (int iY = nOvrMinTileY; bRet && iY <= nOvrMaxTileY; ++iY)
6130 : {
6131 13 : for (int iX = nOvrMinTileX; bRet && iX <= nOvrMaxTileX; ++iX)
6132 : {
6133 7 : int nFileY = GetFileY(
6134 7 : iY, poTMS->tileMatrixList()[m_minZoomLevel], m_convention);
6135 : std::string osFilename = CPLFormFilenameSafe(
6136 : m_output.c_str(), CPLSPrintf("%d", m_minZoomLevel),
6137 14 : nullptr);
6138 14 : osFilename = CPLFormFilenameSafe(osFilename.c_str(),
6139 7 : CPLSPrintf("%d", iX), nullptr);
6140 14 : osFilename = CPLFormFilenameSafe(
6141 : osFilename.c_str(),
6142 7 : CPLSPrintf("%d.%s", nFileY, pszExtension), nullptr);
6143 7 : if (VSIStatL(osFilename.c_str(), &sStat) == 0)
6144 : {
6145 7 : TileCoordinates tc;
6146 7 : tc.nTileX = iX;
6147 7 : tc.nTileY = iY;
6148 7 : tc.nTileZ = m_minZoomLevel;
6149 7 : children.push_back(std::move(tc));
6150 : }
6151 : }
6152 : }
6153 12 : GenerateKML(m_output, m_title, -1, -1, -1, kmlTileSize, pszExtension,
6154 6 : m_url, poTMS.get(), bInvertAxisTMS, m_convention,
6155 : poCTToWGS84.get(), children);
6156 : }
6157 :
6158 170 : if (!bRet && CPLGetLastErrorType() == CE_None)
6159 : {
6160 : // If that happens, this is a programming error
6161 0 : ReportError(CE_Failure, CPLE_AppDefined,
6162 : "Bug: process failed without returning an error message");
6163 : }
6164 :
6165 170 : if (m_spawned)
6166 : {
6167 : // Uninstall he custom error handler, before we close stdout.
6168 50 : poErrorHandlerPusher.reset();
6169 :
6170 50 : fwrite(END_MARKER, sizeof(END_MARKER), 1, stdout);
6171 50 : fflush(stdout);
6172 50 : fclose(stdout);
6173 50 : threadWaitForParentStop.join();
6174 : }
6175 : #ifdef FORK_ALLOWED
6176 120 : else if (m_forked)
6177 : {
6178 0 : CPLPipeWrite(pipeOut, END_MARKER, sizeof(END_MARKER));
6179 0 : threadWaitForParentStop.join();
6180 : }
6181 : #endif
6182 :
6183 170 : return bRet;
6184 : }
6185 :
6186 : GDALRasterTileAlgorithmStandalone::~GDALRasterTileAlgorithmStandalone() =
6187 : default;
6188 :
6189 : //! @endcond
|