/src/gdal/frmts/vrt/vrtdataset.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: Virtual GDAL Datasets |
4 | | * Purpose: Implementation of VRTDataset |
5 | | * Author: Frank Warmerdam <warmerdam@pobox.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2001, Frank Warmerdam <warmerdam@pobox.com> |
9 | | * Copyright (c) 2007-2014, Even Rouault <even dot rouault at spatialys.com> |
10 | | * |
11 | | * SPDX-License-Identifier: MIT |
12 | | ****************************************************************************/ |
13 | | |
14 | | #include "vrtdataset.h" |
15 | | |
16 | | #include "cpl_error_internal.h" |
17 | | #include "cpl_minixml.h" |
18 | | #include "cpl_string.h" |
19 | | #include "gdal_frmts.h" |
20 | | #include "ogr_spatialref.h" |
21 | | #include "gdal_thread_pool.h" |
22 | | #include "gdal_utils.h" |
23 | | |
24 | | #include <algorithm> |
25 | | #include <cassert> |
26 | | #include <cmath> |
27 | | #include <set> |
28 | | #include <typeinfo> |
29 | | #include "gdal_proxy.h" |
30 | | |
31 | | /*! @cond Doxygen_Suppress */ |
32 | | |
33 | 0 | #define VRT_PROTOCOL_PREFIX "vrt://" |
34 | | |
35 | | constexpr int DEFAULT_BLOCK_SIZE = 128; |
36 | | |
37 | | /************************************************************************/ |
38 | | /* VRTDataset() */ |
39 | | /************************************************************************/ |
40 | | |
41 | | VRTDataset::VRTDataset(int nXSize, int nYSize, int nBlockXSize, int nBlockYSize) |
42 | 0 | { |
43 | 0 | nRasterXSize = nXSize; |
44 | 0 | nRasterYSize = nYSize; |
45 | |
|
46 | 0 | m_bBlockSizeSpecified = nBlockXSize > 0 && nBlockYSize > 0; |
47 | 0 | m_nBlockXSize = |
48 | 0 | nBlockXSize > 0 ? nBlockXSize : std::min(DEFAULT_BLOCK_SIZE, nXSize); |
49 | 0 | m_nBlockYSize = |
50 | 0 | nBlockYSize > 0 ? nBlockYSize : std::min(DEFAULT_BLOCK_SIZE, nYSize); |
51 | |
|
52 | 0 | GDALRegister_VRT(); |
53 | |
|
54 | 0 | poDriver = static_cast<GDALDriver *>(GDALGetDriverByName("VRT")); |
55 | 0 | } |
56 | | |
57 | | /************************************************************************/ |
58 | | /* IsDefaultBlockSize() */ |
59 | | /************************************************************************/ |
60 | | |
61 | | /* static */ bool VRTDataset::IsDefaultBlockSize(int nBlockSize, int nDimension) |
62 | 0 | { |
63 | 0 | return nBlockSize == DEFAULT_BLOCK_SIZE || |
64 | 0 | (nBlockSize < DEFAULT_BLOCK_SIZE && nBlockSize == nDimension); |
65 | 0 | } |
66 | | |
67 | | /*! @endcond */ |
68 | | |
69 | | /************************************************************************/ |
70 | | /* VRTCreate() */ |
71 | | /************************************************************************/ |
72 | | |
73 | | /** |
74 | | * @see VRTDataset::VRTDataset() |
75 | | */ |
76 | | |
77 | | VRTDatasetH CPL_STDCALL VRTCreate(int nXSize, int nYSize) |
78 | | |
79 | 0 | { |
80 | 0 | auto poDS = new VRTDataset(nXSize, nYSize); |
81 | 0 | poDS->eAccess = GA_Update; |
82 | 0 | return poDS; |
83 | 0 | } |
84 | | |
85 | | /*! @cond Doxygen_Suppress */ |
86 | | |
87 | | /************************************************************************/ |
88 | | /* ~VRTDataset() */ |
89 | | /************************************************************************/ |
90 | | |
91 | | VRTDataset::~VRTDataset() |
92 | | |
93 | 0 | { |
94 | 0 | VRTDataset::FlushCache(true); |
95 | 0 | CPLFree(m_pszVRTPath); |
96 | |
|
97 | 0 | for (size_t i = 0; i < m_apoOverviews.size(); i++) |
98 | 0 | delete m_apoOverviews[i]; |
99 | 0 | for (size_t i = 0; i < m_apoOverviewsBak.size(); i++) |
100 | 0 | delete m_apoOverviewsBak[i]; |
101 | 0 | CSLDestroy(m_papszXMLVRTMetadata); |
102 | 0 | } |
103 | | |
104 | | /************************************************************************/ |
105 | | /* FlushCache() */ |
106 | | /************************************************************************/ |
107 | | |
108 | | CPLErr VRTDataset::FlushCache(bool bAtClosing) |
109 | | |
110 | 0 | { |
111 | 0 | if (m_poRootGroup) |
112 | 0 | return m_poRootGroup->Serialize() ? CE_None : CE_Failure; |
113 | 0 | else |
114 | 0 | return VRTFlushCacheStruct<VRTDataset>::FlushCache(*this, bAtClosing); |
115 | 0 | } |
116 | | |
117 | | /************************************************************************/ |
118 | | /* FlushCache() */ |
119 | | /************************************************************************/ |
120 | | |
121 | | CPLErr VRTWarpedDataset::FlushCache(bool bAtClosing) |
122 | | |
123 | 0 | { |
124 | 0 | return VRTFlushCacheStruct<VRTWarpedDataset>::FlushCache(*this, bAtClosing); |
125 | 0 | } |
126 | | |
127 | | /************************************************************************/ |
128 | | /* FlushCache() */ |
129 | | /************************************************************************/ |
130 | | |
131 | | CPLErr VRTPansharpenedDataset::FlushCache(bool bAtClosing) |
132 | | |
133 | 0 | { |
134 | 0 | return VRTFlushCacheStruct<VRTPansharpenedDataset>::FlushCache(*this, |
135 | 0 | bAtClosing); |
136 | 0 | } |
137 | | |
138 | | /************************************************************************/ |
139 | | /* FlushCache() */ |
140 | | /************************************************************************/ |
141 | | |
142 | | CPLErr VRTProcessedDataset::FlushCache(bool bAtClosing) |
143 | | |
144 | 0 | { |
145 | 0 | return VRTFlushCacheStruct<VRTProcessedDataset>::FlushCache(*this, |
146 | 0 | bAtClosing); |
147 | 0 | } |
148 | | |
149 | | /************************************************************************/ |
150 | | /* FlushCache() */ |
151 | | /************************************************************************/ |
152 | | |
153 | | template <class T> |
154 | | CPLErr VRTFlushCacheStruct<T>::FlushCache(T &obj, bool bAtClosing) |
155 | 0 | { |
156 | 0 | CPLErr eErr = obj.GDALDataset::FlushCache(bAtClosing); |
157 | |
|
158 | 0 | if (!obj.m_bNeedsFlush || !obj.m_bWritable) |
159 | 0 | return eErr; |
160 | | |
161 | | // We don't write to disk if there is no filename. This is a |
162 | | // memory only dataset. |
163 | 0 | if (strlen(obj.GetDescription()) == 0 || |
164 | 0 | STARTS_WITH_CI(obj.GetDescription(), "<VRTDataset")) |
165 | 0 | return eErr; |
166 | | |
167 | 0 | obj.m_bNeedsFlush = false; |
168 | | |
169 | | // Serialize XML representation to disk |
170 | 0 | const std::string osVRTPath(CPLGetPathSafe(obj.GetDescription())); |
171 | 0 | CPLXMLNode *psDSTree = obj.T::SerializeToXML(osVRTPath.c_str()); |
172 | 0 | if (!CPLSerializeXMLTreeToFile(psDSTree, obj.GetDescription())) |
173 | 0 | eErr = CE_Failure; |
174 | 0 | CPLDestroyXMLNode(psDSTree); |
175 | 0 | return eErr; |
176 | 0 | } Unexecuted instantiation: VRTFlushCacheStruct<VRTDataset>::FlushCache(VRTDataset&, bool) Unexecuted instantiation: VRTFlushCacheStruct<VRTWarpedDataset>::FlushCache(VRTWarpedDataset&, bool) Unexecuted instantiation: VRTFlushCacheStruct<VRTPansharpenedDataset>::FlushCache(VRTPansharpenedDataset&, bool) Unexecuted instantiation: VRTFlushCacheStruct<VRTProcessedDataset>::FlushCache(VRTProcessedDataset&, bool) |
177 | | |
178 | | /************************************************************************/ |
179 | | /* GetMetadata() */ |
180 | | /************************************************************************/ |
181 | | |
182 | | char **VRTDataset::GetMetadata(const char *pszDomain) |
183 | 0 | { |
184 | 0 | if (pszDomain != nullptr && EQUAL(pszDomain, "xml:VRT")) |
185 | 0 | { |
186 | | /* ------------------------------------------------------------------ */ |
187 | | /* Convert tree to a single block of XML text. */ |
188 | | /* ------------------------------------------------------------------ */ |
189 | 0 | const char *pszDescription = GetDescription(); |
190 | 0 | char *l_pszVRTPath = CPLStrdup( |
191 | 0 | pszDescription[0] && !STARTS_WITH(pszDescription, "<VRTDataset") |
192 | 0 | ? CPLGetPathSafe(pszDescription).c_str() |
193 | 0 | : ""); |
194 | 0 | CPLXMLNode *psDSTree = SerializeToXML(l_pszVRTPath); |
195 | 0 | char *pszXML = CPLSerializeXMLTree(psDSTree); |
196 | |
|
197 | 0 | CPLDestroyXMLNode(psDSTree); |
198 | |
|
199 | 0 | CPLFree(l_pszVRTPath); |
200 | |
|
201 | 0 | CSLDestroy(m_papszXMLVRTMetadata); |
202 | 0 | m_papszXMLVRTMetadata = |
203 | 0 | static_cast<char **>(CPLMalloc(2 * sizeof(char *))); |
204 | 0 | m_papszXMLVRTMetadata[0] = pszXML; |
205 | 0 | m_papszXMLVRTMetadata[1] = nullptr; |
206 | 0 | return m_papszXMLVRTMetadata; |
207 | 0 | } |
208 | | |
209 | 0 | return GDALDataset::GetMetadata(pszDomain); |
210 | 0 | } |
211 | | |
212 | | /************************************************************************/ |
213 | | /* GetMetadataItem() */ |
214 | | /************************************************************************/ |
215 | | |
216 | | const char *VRTDataset::GetMetadataItem(const char *pszName, |
217 | | const char *pszDomain) |
218 | | |
219 | 0 | { |
220 | 0 | if (pszName && pszDomain && EQUAL(pszDomain, "__DEBUG__")) |
221 | 0 | { |
222 | 0 | if (EQUAL(pszName, "MULTI_THREADED_RASTERIO_LAST_USED")) |
223 | 0 | return m_bMultiThreadedRasterIOLastUsed ? "1" : "0"; |
224 | 0 | else if (EQUAL(pszName, "CheckCompatibleForDatasetIO()")) |
225 | 0 | return CheckCompatibleForDatasetIO() ? "1" : "0"; |
226 | 0 | } |
227 | 0 | return GDALDataset::GetMetadataItem(pszName, pszDomain); |
228 | 0 | } |
229 | | |
230 | | /*! @endcond */ |
231 | | |
232 | | /************************************************************************/ |
233 | | /* VRTFlushCache(bool bAtClosing) */ |
234 | | /************************************************************************/ |
235 | | |
236 | | /** |
237 | | * @see VRTDataset::FlushCache(bool bAtClosing) |
238 | | */ |
239 | | |
240 | | void CPL_STDCALL VRTFlushCache(VRTDatasetH hDataset) |
241 | 0 | { |
242 | 0 | VALIDATE_POINTER0(hDataset, "VRTFlushCache"); |
243 | | |
244 | 0 | static_cast<VRTDataset *>(GDALDataset::FromHandle(hDataset)) |
245 | 0 | ->FlushCache(false); |
246 | 0 | } |
247 | | |
248 | | /*! @cond Doxygen_Suppress */ |
249 | | |
250 | | /************************************************************************/ |
251 | | /* SerializeToXML() */ |
252 | | /************************************************************************/ |
253 | | |
254 | | CPLXMLNode *VRTDataset::SerializeToXML(const char *pszVRTPathIn) |
255 | | |
256 | 0 | { |
257 | 0 | if (m_poRootGroup) |
258 | 0 | return m_poRootGroup->SerializeToXML(pszVRTPathIn); |
259 | | |
260 | | /* -------------------------------------------------------------------- */ |
261 | | /* Setup root node and attributes. */ |
262 | | /* -------------------------------------------------------------------- */ |
263 | 0 | CPLXMLNode *psDSTree = CPLCreateXMLNode(nullptr, CXT_Element, "VRTDataset"); |
264 | |
|
265 | 0 | char szNumber[128] = {'\0'}; |
266 | 0 | snprintf(szNumber, sizeof(szNumber), "%d", GetRasterXSize()); |
267 | 0 | CPLSetXMLValue(psDSTree, "#rasterXSize", szNumber); |
268 | |
|
269 | 0 | snprintf(szNumber, sizeof(szNumber), "%d", GetRasterYSize()); |
270 | 0 | CPLSetXMLValue(psDSTree, "#rasterYSize", szNumber); |
271 | | |
272 | | /* -------------------------------------------------------------------- */ |
273 | | /* SRS */ |
274 | | /* -------------------------------------------------------------------- */ |
275 | 0 | if (m_poSRS && !m_poSRS->IsEmpty()) |
276 | 0 | { |
277 | 0 | char *pszWKT = nullptr; |
278 | 0 | m_poSRS->exportToWkt(&pszWKT); |
279 | 0 | CPLXMLNode *psSRSNode = |
280 | 0 | CPLCreateXMLElementAndValue(psDSTree, "SRS", pszWKT); |
281 | 0 | CPLFree(pszWKT); |
282 | 0 | const auto &mapping = m_poSRS->GetDataAxisToSRSAxisMapping(); |
283 | 0 | CPLString osMapping; |
284 | 0 | for (size_t i = 0; i < mapping.size(); ++i) |
285 | 0 | { |
286 | 0 | if (!osMapping.empty()) |
287 | 0 | osMapping += ","; |
288 | 0 | osMapping += CPLSPrintf("%d", mapping[i]); |
289 | 0 | } |
290 | 0 | CPLAddXMLAttributeAndValue(psSRSNode, "dataAxisToSRSAxisMapping", |
291 | 0 | osMapping.c_str()); |
292 | 0 | const double dfCoordinateEpoch = m_poSRS->GetCoordinateEpoch(); |
293 | 0 | if (dfCoordinateEpoch > 0) |
294 | 0 | { |
295 | 0 | std::string osCoordinateEpoch = CPLSPrintf("%f", dfCoordinateEpoch); |
296 | 0 | if (osCoordinateEpoch.find('.') != std::string::npos) |
297 | 0 | { |
298 | 0 | while (osCoordinateEpoch.back() == '0') |
299 | 0 | osCoordinateEpoch.pop_back(); |
300 | 0 | } |
301 | 0 | CPLAddXMLAttributeAndValue(psSRSNode, "coordinateEpoch", |
302 | 0 | osCoordinateEpoch.c_str()); |
303 | 0 | } |
304 | 0 | } |
305 | | |
306 | | /* -------------------------------------------------------------------- */ |
307 | | /* Geotransform. */ |
308 | | /* -------------------------------------------------------------------- */ |
309 | 0 | if (m_bGeoTransformSet) |
310 | 0 | { |
311 | 0 | CPLSetXMLValue( |
312 | 0 | psDSTree, "GeoTransform", |
313 | 0 | CPLSPrintf("%24.16e,%24.16e,%24.16e,%24.16e,%24.16e,%24.16e", |
314 | 0 | m_gt[0], m_gt[1], m_gt[2], m_gt[3], m_gt[4], m_gt[5])); |
315 | 0 | } |
316 | | |
317 | | /* -------------------------------------------------------------------- */ |
318 | | /* Metadata */ |
319 | | /* -------------------------------------------------------------------- */ |
320 | 0 | CPLXMLNode *psMD = oMDMD.Serialize(); |
321 | 0 | if (psMD != nullptr) |
322 | 0 | { |
323 | 0 | CPLAddXMLChild(psDSTree, psMD); |
324 | 0 | } |
325 | | |
326 | | /* -------------------------------------------------------------------- */ |
327 | | /* GCPs */ |
328 | | /* -------------------------------------------------------------------- */ |
329 | 0 | if (!m_asGCPs.empty()) |
330 | 0 | { |
331 | 0 | GDALSerializeGCPListToXML(psDSTree, m_asGCPs, m_poGCP_SRS.get()); |
332 | 0 | } |
333 | | |
334 | | /* -------------------------------------------------------------------- */ |
335 | | /* Serialize bands. */ |
336 | | /* -------------------------------------------------------------------- */ |
337 | 0 | CPLXMLNode *psLastChild = psDSTree->psChild; |
338 | 0 | for (; psLastChild != nullptr && psLastChild->psNext; |
339 | 0 | psLastChild = psLastChild->psNext) |
340 | 0 | { |
341 | 0 | } |
342 | 0 | CPLAssert(psLastChild); // we have at least rasterXSize |
343 | 0 | bool bHasWarnedAboutRAMUsage = false; |
344 | 0 | size_t nAccRAMUsage = 0; |
345 | 0 | for (int iBand = 0; iBand < nBands; iBand++) |
346 | 0 | { |
347 | 0 | CPLXMLNode *psBandTree = |
348 | 0 | static_cast<VRTRasterBand *>(papoBands[iBand]) |
349 | 0 | ->SerializeToXML(pszVRTPathIn, bHasWarnedAboutRAMUsage, |
350 | 0 | nAccRAMUsage); |
351 | |
|
352 | 0 | if (psBandTree != nullptr) |
353 | 0 | { |
354 | 0 | psLastChild->psNext = psBandTree; |
355 | 0 | psLastChild = psBandTree; |
356 | 0 | } |
357 | 0 | } |
358 | | |
359 | | /* -------------------------------------------------------------------- */ |
360 | | /* Serialize dataset mask band. */ |
361 | | /* -------------------------------------------------------------------- */ |
362 | 0 | if (m_poMaskBand) |
363 | 0 | { |
364 | 0 | CPLXMLNode *psBandTree = m_poMaskBand->SerializeToXML( |
365 | 0 | pszVRTPathIn, bHasWarnedAboutRAMUsage, nAccRAMUsage); |
366 | |
|
367 | 0 | if (psBandTree != nullptr) |
368 | 0 | { |
369 | 0 | CPLXMLNode *psMaskBandElement = |
370 | 0 | CPLCreateXMLNode(psDSTree, CXT_Element, "MaskBand"); |
371 | 0 | CPLAddXMLChild(psMaskBandElement, psBandTree); |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | | /* -------------------------------------------------------------------- */ |
376 | | /* Overview factors. */ |
377 | | /* -------------------------------------------------------------------- */ |
378 | 0 | if (!m_anOverviewFactors.empty()) |
379 | 0 | { |
380 | 0 | CPLString osOverviewList; |
381 | 0 | for (int nOvFactor : m_anOverviewFactors) |
382 | 0 | { |
383 | 0 | if (!osOverviewList.empty()) |
384 | 0 | osOverviewList += " "; |
385 | 0 | osOverviewList += CPLSPrintf("%d", nOvFactor); |
386 | 0 | } |
387 | 0 | CPLXMLNode *psOverviewList = CPLCreateXMLElementAndValue( |
388 | 0 | psDSTree, "OverviewList", osOverviewList); |
389 | 0 | if (!m_osOverviewResampling.empty()) |
390 | 0 | { |
391 | 0 | CPLAddXMLAttributeAndValue(psOverviewList, "resampling", |
392 | 0 | m_osOverviewResampling); |
393 | 0 | } |
394 | 0 | } |
395 | |
|
396 | 0 | return psDSTree; |
397 | 0 | } |
398 | | |
399 | | /*! @endcond */ |
400 | | /************************************************************************/ |
401 | | /* VRTSerializeToXML() */ |
402 | | /************************************************************************/ |
403 | | |
404 | | /** |
405 | | * @see VRTDataset::SerializeToXML() |
406 | | */ |
407 | | |
408 | | CPLXMLNode *CPL_STDCALL VRTSerializeToXML(VRTDatasetH hDataset, |
409 | | const char *pszVRTPath) |
410 | 0 | { |
411 | 0 | VALIDATE_POINTER1(hDataset, "VRTSerializeToXML", nullptr); |
412 | | |
413 | 0 | return static_cast<VRTDataset *>(GDALDataset::FromHandle(hDataset)) |
414 | 0 | ->SerializeToXML(pszVRTPath); |
415 | 0 | } |
416 | | |
417 | | /*! @cond Doxygen_Suppress */ |
418 | | |
419 | | /************************************************************************/ |
420 | | /* IsRawRasterBandEnabled() */ |
421 | | /************************************************************************/ |
422 | | |
423 | | /** Return whether VRTRawRasterBand support is enabled */ |
424 | | |
425 | | /* static */ |
426 | | bool VRTDataset::IsRawRasterBandEnabled() |
427 | 0 | { |
428 | 0 | #ifdef GDAL_VRT_ENABLE_RAWRASTERBAND |
429 | 0 | if (CPLTestBool(CPLGetConfigOption("GDAL_VRT_ENABLE_RAWRASTERBAND", "YES"))) |
430 | 0 | { |
431 | 0 | return true; |
432 | 0 | } |
433 | 0 | else |
434 | 0 | { |
435 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
436 | 0 | "VRTRawRasterBand support has been disabled at run-time."); |
437 | 0 | } |
438 | 0 | return false; |
439 | | #else |
440 | | CPLError(CE_Failure, CPLE_NotSupported, |
441 | | "VRTRawRasterBand is disabled in this GDAL build"); |
442 | | return false; |
443 | | #endif |
444 | 0 | } |
445 | | |
446 | | /************************************************************************/ |
447 | | /* InitBand() */ |
448 | | /************************************************************************/ |
449 | | |
450 | | std::unique_ptr<VRTRasterBand> |
451 | | VRTDataset::InitBand(const char *pszSubclass, int nBand, |
452 | | bool bAllowPansharpenedOrProcessed) |
453 | 0 | { |
454 | 0 | if (auto poProcessedDS = dynamic_cast<VRTProcessedDataset *>(this)) |
455 | 0 | { |
456 | 0 | if (bAllowPansharpenedOrProcessed && |
457 | 0 | EQUAL(pszSubclass, "VRTProcessedRasterBand")) |
458 | 0 | { |
459 | 0 | return std::make_unique<VRTProcessedRasterBand>(poProcessedDS, |
460 | 0 | nBand); |
461 | 0 | } |
462 | 0 | } |
463 | 0 | else if (EQUAL(pszSubclass, "VRTSourcedRasterBand")) |
464 | 0 | return std::make_unique<VRTSourcedRasterBand>(this, nBand); |
465 | 0 | else if (EQUAL(pszSubclass, "VRTDerivedRasterBand")) |
466 | 0 | return std::make_unique<VRTDerivedRasterBand>(this, nBand); |
467 | 0 | else if (EQUAL(pszSubclass, "VRTRawRasterBand")) |
468 | 0 | { |
469 | 0 | #ifdef GDAL_VRT_ENABLE_RAWRASTERBAND |
470 | 0 | if (!VRTDataset::IsRawRasterBandEnabled()) |
471 | 0 | { |
472 | 0 | return nullptr; |
473 | 0 | } |
474 | 0 | return std::make_unique<VRTRawRasterBand>(this, nBand); |
475 | | #else |
476 | | CPLError(CE_Failure, CPLE_NotSupported, |
477 | | "VRTDataset::InitBand(): cannot instantiate VRTRawRasterBand, " |
478 | | "because disabled in this GDAL build"); |
479 | | return nullptr; |
480 | | #endif |
481 | 0 | } |
482 | 0 | else if (EQUAL(pszSubclass, "VRTWarpedRasterBand") && |
483 | 0 | dynamic_cast<VRTWarpedDataset *>(this) != nullptr) |
484 | 0 | { |
485 | 0 | return std::make_unique<VRTWarpedRasterBand>(this, nBand); |
486 | 0 | } |
487 | 0 | else if (bAllowPansharpenedOrProcessed && |
488 | 0 | EQUAL(pszSubclass, "VRTPansharpenedRasterBand") && |
489 | 0 | dynamic_cast<VRTPansharpenedDataset *>(this) != nullptr) |
490 | 0 | { |
491 | 0 | return std::make_unique<VRTPansharpenedRasterBand>(this, nBand); |
492 | 0 | } |
493 | | |
494 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
495 | 0 | "VRTRasterBand of unrecognized subclass '%s'.", pszSubclass); |
496 | 0 | return nullptr; |
497 | 0 | } |
498 | | |
499 | | /************************************************************************/ |
500 | | /* XMLInit() */ |
501 | | /************************************************************************/ |
502 | | |
503 | | CPLErr VRTDataset::XMLInit(const CPLXMLNode *psTree, const char *pszVRTPathIn) |
504 | | |
505 | 0 | { |
506 | 0 | if (pszVRTPathIn != nullptr) |
507 | 0 | m_pszVRTPath = CPLStrdup(pszVRTPathIn); |
508 | | |
509 | | /* -------------------------------------------------------------------- */ |
510 | | /* Check for an SRS node. */ |
511 | | /* -------------------------------------------------------------------- */ |
512 | 0 | const CPLXMLNode *psSRSNode = CPLGetXMLNode(psTree, "SRS"); |
513 | 0 | if (psSRSNode) |
514 | 0 | { |
515 | 0 | m_poSRS.reset(new OGRSpatialReference()); |
516 | 0 | m_poSRS->SetFromUserInput( |
517 | 0 | CPLGetXMLValue(psSRSNode, nullptr, ""), |
518 | 0 | OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get()); |
519 | 0 | const char *pszMapping = |
520 | 0 | CPLGetXMLValue(psSRSNode, "dataAxisToSRSAxisMapping", nullptr); |
521 | 0 | if (pszMapping) |
522 | 0 | { |
523 | 0 | char **papszTokens = |
524 | 0 | CSLTokenizeStringComplex(pszMapping, ",", FALSE, FALSE); |
525 | 0 | std::vector<int> anMapping; |
526 | 0 | for (int i = 0; papszTokens && papszTokens[i]; i++) |
527 | 0 | { |
528 | 0 | anMapping.push_back(atoi(papszTokens[i])); |
529 | 0 | } |
530 | 0 | CSLDestroy(papszTokens); |
531 | 0 | m_poSRS->SetDataAxisToSRSAxisMapping(anMapping); |
532 | 0 | } |
533 | 0 | else |
534 | 0 | { |
535 | 0 | m_poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
536 | 0 | } |
537 | |
|
538 | 0 | const char *pszCoordinateEpoch = |
539 | 0 | CPLGetXMLValue(psSRSNode, "coordinateEpoch", nullptr); |
540 | 0 | if (pszCoordinateEpoch) |
541 | 0 | m_poSRS->SetCoordinateEpoch(CPLAtof(pszCoordinateEpoch)); |
542 | 0 | } |
543 | | |
544 | | /* -------------------------------------------------------------------- */ |
545 | | /* Check for a GeoTransform node. */ |
546 | | /* -------------------------------------------------------------------- */ |
547 | 0 | const char *pszGT = CPLGetXMLValue(psTree, "GeoTransform", ""); |
548 | 0 | if (strlen(pszGT) > 0) |
549 | 0 | { |
550 | 0 | const CPLStringList aosTokens( |
551 | 0 | CSLTokenizeStringComplex(pszGT, ",", FALSE, FALSE)); |
552 | 0 | if (aosTokens.size() != 6) |
553 | 0 | { |
554 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
555 | 0 | "GeoTransform node does not have expected six values."); |
556 | 0 | } |
557 | 0 | else |
558 | 0 | { |
559 | 0 | for (int iTA = 0; iTA < 6; iTA++) |
560 | 0 | m_gt[iTA] = CPLAtof(aosTokens[iTA]); |
561 | 0 | m_bGeoTransformSet = TRUE; |
562 | 0 | } |
563 | 0 | } |
564 | | |
565 | | /* -------------------------------------------------------------------- */ |
566 | | /* Check for GCPs. */ |
567 | | /* -------------------------------------------------------------------- */ |
568 | 0 | if (const CPLXMLNode *psGCPList = CPLGetXMLNode(psTree, "GCPList")) |
569 | 0 | { |
570 | 0 | OGRSpatialReference *poSRS = nullptr; |
571 | 0 | GDALDeserializeGCPListFromXML(psGCPList, m_asGCPs, &poSRS); |
572 | 0 | m_poGCP_SRS.reset(poSRS); |
573 | 0 | } |
574 | | |
575 | | /* -------------------------------------------------------------------- */ |
576 | | /* Apply any dataset level metadata. */ |
577 | | /* -------------------------------------------------------------------- */ |
578 | 0 | oMDMD.XMLInit(psTree, TRUE); |
579 | | |
580 | | /* -------------------------------------------------------------------- */ |
581 | | /* Create dataset mask band. */ |
582 | | /* -------------------------------------------------------------------- */ |
583 | | |
584 | | /* Parse dataset mask band first */ |
585 | 0 | const CPLXMLNode *psMaskBandNode = CPLGetXMLNode(psTree, "MaskBand"); |
586 | |
|
587 | 0 | const CPLXMLNode *psChild = nullptr; |
588 | 0 | if (psMaskBandNode) |
589 | 0 | psChild = psMaskBandNode->psChild; |
590 | 0 | else |
591 | 0 | psChild = nullptr; |
592 | |
|
593 | 0 | for (; psChild != nullptr; psChild = psChild->psNext) |
594 | 0 | { |
595 | 0 | if (psChild->eType == CXT_Element && |
596 | 0 | EQUAL(psChild->pszValue, "VRTRasterBand")) |
597 | 0 | { |
598 | 0 | const char *pszSubclass = |
599 | 0 | CPLGetXMLValue(psChild, "subclass", "VRTSourcedRasterBand"); |
600 | |
|
601 | 0 | auto poBand = InitBand(pszSubclass, 0, false); |
602 | 0 | if (poBand != nullptr && |
603 | 0 | poBand->XMLInit(psChild, pszVRTPathIn, m_oMapSharedSources) == |
604 | 0 | CE_None) |
605 | 0 | { |
606 | 0 | SetMaskBand(std::move(poBand)); |
607 | 0 | break; |
608 | 0 | } |
609 | 0 | else |
610 | 0 | { |
611 | 0 | return CE_Failure; |
612 | 0 | } |
613 | 0 | } |
614 | 0 | } |
615 | | |
616 | | /* -------------------------------------------------------------------- */ |
617 | | /* Create band information objects. */ |
618 | | /* -------------------------------------------------------------------- */ |
619 | 0 | int l_nBands = 0; |
620 | 0 | for (psChild = psTree->psChild; psChild != nullptr; |
621 | 0 | psChild = psChild->psNext) |
622 | 0 | { |
623 | 0 | if (psChild->eType == CXT_Element && |
624 | 0 | EQUAL(psChild->pszValue, "VRTRasterBand")) |
625 | 0 | { |
626 | 0 | const char *pszSubclass = |
627 | 0 | CPLGetXMLValue(psChild, "subclass", "VRTSourcedRasterBand"); |
628 | 0 | if (dynamic_cast<VRTProcessedDataset *>(this) && |
629 | 0 | !EQUAL(pszSubclass, "VRTProcessedRasterBand")) |
630 | 0 | { |
631 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
632 | 0 | "Only subClass=VRTProcessedRasterBand supported"); |
633 | 0 | return CE_Failure; |
634 | 0 | } |
635 | | |
636 | 0 | if (CPLGetXMLNode(psChild, "PixelFunctionType") != nullptr && |
637 | 0 | !EQUAL(pszSubclass, "VRTDerivedRasterBand")) |
638 | 0 | { |
639 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
640 | 0 | "Pixel functions may only be used with " |
641 | 0 | "subClass=VRTDerivedRasterBand"); |
642 | 0 | return CE_Failure; |
643 | 0 | } |
644 | | |
645 | 0 | auto poBand = InitBand(pszSubclass, l_nBands + 1, true); |
646 | 0 | if (poBand != nullptr && |
647 | 0 | poBand->XMLInit(psChild, pszVRTPathIn, m_oMapSharedSources) == |
648 | 0 | CE_None) |
649 | 0 | { |
650 | 0 | l_nBands++; |
651 | 0 | SetBand(l_nBands, std::move(poBand)); |
652 | 0 | } |
653 | 0 | else |
654 | 0 | { |
655 | 0 | return CE_Failure; |
656 | 0 | } |
657 | 0 | } |
658 | 0 | } |
659 | | |
660 | 0 | if (const CPLXMLNode *psGroup = CPLGetXMLNode(psTree, "Group")) |
661 | 0 | { |
662 | 0 | const char *pszName = CPLGetXMLValue(psGroup, "name", nullptr); |
663 | 0 | if (pszName == nullptr || !EQUAL(pszName, "/")) |
664 | 0 | { |
665 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
666 | 0 | "Missing name or not equal to '/'"); |
667 | 0 | return CE_Failure; |
668 | 0 | } |
669 | | |
670 | 0 | m_poRootGroup = VRTGroup::Create(std::string(), "/"); |
671 | 0 | m_poRootGroup->SetIsRootGroup(); |
672 | 0 | if (!m_poRootGroup->XMLInit(m_poRootGroup, m_poRootGroup, psGroup, |
673 | 0 | pszVRTPathIn)) |
674 | 0 | { |
675 | 0 | return CE_Failure; |
676 | 0 | } |
677 | 0 | } |
678 | | |
679 | | /* -------------------------------------------------------------------- */ |
680 | | /* Create virtual overviews. */ |
681 | | /* -------------------------------------------------------------------- */ |
682 | 0 | const char *pszSubClass = CPLGetXMLValue(psTree, "subClass", ""); |
683 | 0 | if (EQUAL(pszSubClass, "")) |
684 | 0 | { |
685 | 0 | m_aosOverviewList = |
686 | 0 | CSLTokenizeString(CPLGetXMLValue(psTree, "OverviewList", "")); |
687 | 0 | m_osOverviewResampling = |
688 | 0 | CPLGetXMLValue(psTree, "OverviewList.resampling", ""); |
689 | 0 | } |
690 | |
|
691 | 0 | return CE_None; |
692 | 0 | } |
693 | | |
694 | | /************************************************************************/ |
695 | | /* GetGCPCount() */ |
696 | | /************************************************************************/ |
697 | | |
698 | | int VRTDataset::GetGCPCount() |
699 | | |
700 | 0 | { |
701 | 0 | return static_cast<int>(m_asGCPs.size()); |
702 | 0 | } |
703 | | |
704 | | /************************************************************************/ |
705 | | /* GetGCPs() */ |
706 | | /************************************************************************/ |
707 | | |
708 | | const GDAL_GCP *VRTDataset::GetGCPs() |
709 | | |
710 | 0 | { |
711 | 0 | return gdal::GCP::c_ptr(m_asGCPs); |
712 | 0 | } |
713 | | |
714 | | /************************************************************************/ |
715 | | /* SetGCPs() */ |
716 | | /************************************************************************/ |
717 | | |
718 | | CPLErr VRTDataset::SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn, |
719 | | const OGRSpatialReference *poGCP_SRS) |
720 | | |
721 | 0 | { |
722 | 0 | m_poGCP_SRS.reset(poGCP_SRS ? poGCP_SRS->Clone() : nullptr); |
723 | 0 | m_asGCPs = gdal::GCP::fromC(pasGCPListIn, nGCPCountIn); |
724 | |
|
725 | 0 | SetNeedsFlush(); |
726 | |
|
727 | 0 | return CE_None; |
728 | 0 | } |
729 | | |
730 | | /************************************************************************/ |
731 | | /* SetSpatialRef() */ |
732 | | /************************************************************************/ |
733 | | |
734 | | CPLErr VRTDataset::SetSpatialRef(const OGRSpatialReference *poSRS) |
735 | | |
736 | 0 | { |
737 | 0 | m_poSRS.reset(poSRS ? poSRS->Clone() : nullptr); |
738 | |
|
739 | 0 | SetNeedsFlush(); |
740 | |
|
741 | 0 | return CE_None; |
742 | 0 | } |
743 | | |
744 | | /************************************************************************/ |
745 | | /* SetGeoTransform() */ |
746 | | /************************************************************************/ |
747 | | |
748 | | CPLErr VRTDataset::SetGeoTransform(const GDALGeoTransform >) |
749 | | |
750 | 0 | { |
751 | 0 | m_gt = gt; |
752 | 0 | m_bGeoTransformSet = TRUE; |
753 | |
|
754 | 0 | SetNeedsFlush(); |
755 | |
|
756 | 0 | return CE_None; |
757 | 0 | } |
758 | | |
759 | | /************************************************************************/ |
760 | | /* GetGeoTransform() */ |
761 | | /************************************************************************/ |
762 | | |
763 | | CPLErr VRTDataset::GetGeoTransform(GDALGeoTransform >) const |
764 | | |
765 | 0 | { |
766 | 0 | gt = m_gt; |
767 | |
|
768 | 0 | return m_bGeoTransformSet ? CE_None : CE_Failure; |
769 | 0 | } |
770 | | |
771 | | /************************************************************************/ |
772 | | /* SetMetadata() */ |
773 | | /************************************************************************/ |
774 | | |
775 | | CPLErr VRTDataset::SetMetadata(char **papszMetadata, const char *pszDomain) |
776 | | |
777 | 0 | { |
778 | 0 | SetNeedsFlush(); |
779 | |
|
780 | 0 | return GDALDataset::SetMetadata(papszMetadata, pszDomain); |
781 | 0 | } |
782 | | |
783 | | /************************************************************************/ |
784 | | /* SetMetadataItem() */ |
785 | | /************************************************************************/ |
786 | | |
787 | | CPLErr VRTDataset::SetMetadataItem(const char *pszName, const char *pszValue, |
788 | | const char *pszDomain) |
789 | | |
790 | 0 | { |
791 | 0 | SetNeedsFlush(); |
792 | |
|
793 | 0 | return GDALDataset::SetMetadataItem(pszName, pszValue, pszDomain); |
794 | 0 | } |
795 | | |
796 | | /************************************************************************/ |
797 | | /* Identify() */ |
798 | | /************************************************************************/ |
799 | | |
800 | | int VRTDataset::Identify(GDALOpenInfo *poOpenInfo) |
801 | | |
802 | 0 | { |
803 | 0 | if (poOpenInfo->nHeaderBytes > 20 && |
804 | 0 | strstr(reinterpret_cast<const char *>(poOpenInfo->pabyHeader), |
805 | 0 | "<VRTDataset") != nullptr) |
806 | 0 | return TRUE; |
807 | | |
808 | 0 | if (strstr(poOpenInfo->pszFilename, "<VRTDataset") != nullptr) |
809 | 0 | return TRUE; |
810 | | |
811 | 0 | if (STARTS_WITH_CI(poOpenInfo->pszFilename, VRT_PROTOCOL_PREFIX)) |
812 | 0 | return TRUE; |
813 | | |
814 | 0 | return FALSE; |
815 | 0 | } |
816 | | |
817 | | /************************************************************************/ |
818 | | /* Open() */ |
819 | | /************************************************************************/ |
820 | | |
821 | | GDALDataset *VRTDataset::Open(GDALOpenInfo *poOpenInfo) |
822 | | |
823 | 0 | { |
824 | | /* -------------------------------------------------------------------- */ |
825 | | /* Does this appear to be a virtual dataset definition XML */ |
826 | | /* file? */ |
827 | | /* -------------------------------------------------------------------- */ |
828 | 0 | if (!Identify(poOpenInfo)) |
829 | 0 | return nullptr; |
830 | | |
831 | 0 | if (STARTS_WITH_CI(poOpenInfo->pszFilename, VRT_PROTOCOL_PREFIX)) |
832 | 0 | return OpenVRTProtocol(poOpenInfo->pszFilename); |
833 | | |
834 | | /* -------------------------------------------------------------------- */ |
835 | | /* Try to read the whole file into memory. */ |
836 | | /* -------------------------------------------------------------------- */ |
837 | 0 | char *pszXML = nullptr; |
838 | 0 | VSILFILE *fp = poOpenInfo->fpL; |
839 | |
|
840 | 0 | char *pszVRTPath = nullptr; |
841 | 0 | if (fp != nullptr) |
842 | 0 | { |
843 | 0 | poOpenInfo->fpL = nullptr; |
844 | |
|
845 | 0 | GByte *pabyOut = nullptr; |
846 | 0 | if (!VSIIngestFile(fp, poOpenInfo->pszFilename, &pabyOut, nullptr, |
847 | 0 | INT_MAX - 1)) |
848 | 0 | { |
849 | 0 | CPL_IGNORE_RET_VAL(VSIFCloseL(fp)); |
850 | 0 | return nullptr; |
851 | 0 | } |
852 | 0 | pszXML = reinterpret_cast<char *>(pabyOut); |
853 | |
|
854 | 0 | char *pszCurDir = CPLGetCurrentDir(); |
855 | 0 | std::string currentVrtFilename = |
856 | 0 | CPLProjectRelativeFilenameSafe(pszCurDir, poOpenInfo->pszFilename); |
857 | 0 | CPLString osInitialCurrentVrtFilename(currentVrtFilename); |
858 | 0 | CPLFree(pszCurDir); |
859 | |
|
860 | 0 | #if !defined(_WIN32) |
861 | 0 | char filenameBuffer[2048]; |
862 | |
|
863 | 0 | while (true) |
864 | 0 | { |
865 | 0 | VSIStatBuf statBuffer; |
866 | 0 | int lstatCode = lstat(currentVrtFilename.c_str(), &statBuffer); |
867 | 0 | if (lstatCode == -1) |
868 | 0 | { |
869 | 0 | if (errno == ENOENT) |
870 | 0 | { |
871 | | // File could be a virtual file, let later checks handle it. |
872 | 0 | break; |
873 | 0 | } |
874 | 0 | else |
875 | 0 | { |
876 | 0 | CPL_IGNORE_RET_VAL(VSIFCloseL(fp)); |
877 | 0 | CPLFree(pszXML); |
878 | 0 | CPLError(CE_Failure, CPLE_FileIO, "Failed to lstat %s: %s", |
879 | 0 | currentVrtFilename.c_str(), VSIStrerror(errno)); |
880 | 0 | return nullptr; |
881 | 0 | } |
882 | 0 | } |
883 | | |
884 | 0 | if (!VSI_ISLNK(statBuffer.st_mode)) |
885 | 0 | { |
886 | 0 | break; |
887 | 0 | } |
888 | | |
889 | 0 | const int bufferSize = static_cast<int>( |
890 | 0 | readlink(currentVrtFilename.c_str(), filenameBuffer, |
891 | 0 | sizeof(filenameBuffer))); |
892 | 0 | if (bufferSize != -1) |
893 | 0 | { |
894 | 0 | filenameBuffer[std::min( |
895 | 0 | bufferSize, static_cast<int>(sizeof(filenameBuffer)) - 1)] = |
896 | 0 | 0; |
897 | | // The filename in filenameBuffer might be a relative path |
898 | | // from the linkfile resolve it before looping |
899 | 0 | currentVrtFilename = CPLProjectRelativeFilenameSafe( |
900 | 0 | CPLGetDirnameSafe(currentVrtFilename.c_str()).c_str(), |
901 | 0 | filenameBuffer); |
902 | 0 | } |
903 | 0 | else |
904 | 0 | { |
905 | 0 | CPL_IGNORE_RET_VAL(VSIFCloseL(fp)); |
906 | 0 | CPLFree(pszXML); |
907 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
908 | 0 | "Failed to read filename from symlink %s: %s", |
909 | 0 | currentVrtFilename.c_str(), VSIStrerror(errno)); |
910 | 0 | return nullptr; |
911 | 0 | } |
912 | 0 | } |
913 | 0 | #endif // !defined(__WIN32) |
914 | | |
915 | 0 | if (osInitialCurrentVrtFilename == currentVrtFilename) |
916 | 0 | pszVRTPath = |
917 | 0 | CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str()); |
918 | 0 | else |
919 | 0 | pszVRTPath = |
920 | 0 | CPLStrdup(CPLGetPathSafe(currentVrtFilename.c_str()).c_str()); |
921 | |
|
922 | 0 | CPL_IGNORE_RET_VAL(VSIFCloseL(fp)); |
923 | 0 | } |
924 | | /* -------------------------------------------------------------------- */ |
925 | | /* Or use the filename as the XML input. */ |
926 | | /* -------------------------------------------------------------------- */ |
927 | 0 | else |
928 | 0 | { |
929 | 0 | pszXML = CPLStrdup(poOpenInfo->pszFilename); |
930 | 0 | } |
931 | | |
932 | 0 | if (CSLFetchNameValue(poOpenInfo->papszOpenOptions, "ROOT_PATH") != nullptr) |
933 | 0 | { |
934 | 0 | CPLFree(pszVRTPath); |
935 | 0 | pszVRTPath = CPLStrdup( |
936 | 0 | CSLFetchNameValue(poOpenInfo->papszOpenOptions, "ROOT_PATH")); |
937 | 0 | } |
938 | | |
939 | | /* -------------------------------------------------------------------- */ |
940 | | /* Turn the XML representation into a VRTDataset. */ |
941 | | /* -------------------------------------------------------------------- */ |
942 | 0 | auto poDS = OpenXML(pszXML, pszVRTPath, poOpenInfo->eAccess); |
943 | |
|
944 | 0 | if (poDS != nullptr) |
945 | 0 | poDS->m_bNeedsFlush = false; |
946 | |
|
947 | 0 | if (poDS != nullptr) |
948 | 0 | { |
949 | 0 | if (poDS->GetRasterCount() == 0 && |
950 | 0 | (poOpenInfo->nOpenFlags & GDAL_OF_MULTIDIM_RASTER) == 0 && |
951 | 0 | strstr(pszXML, "VRTPansharpenedDataset") == nullptr) |
952 | 0 | { |
953 | 0 | poDS.reset(); |
954 | 0 | } |
955 | 0 | else if (poDS->GetRootGroup() == nullptr && |
956 | 0 | (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) == 0 && |
957 | 0 | (poOpenInfo->nOpenFlags & GDAL_OF_MULTIDIM_RASTER) != 0) |
958 | 0 | { |
959 | 0 | poDS.reset(); |
960 | 0 | } |
961 | 0 | } |
962 | |
|
963 | 0 | CPLFree(pszXML); |
964 | 0 | CPLFree(pszVRTPath); |
965 | | |
966 | | /* -------------------------------------------------------------------- */ |
967 | | /* Initialize info for later overview discovery. */ |
968 | | /* -------------------------------------------------------------------- */ |
969 | |
|
970 | 0 | if (poDS != nullptr) |
971 | 0 | { |
972 | 0 | if (fp != nullptr) |
973 | 0 | { |
974 | 0 | poDS->oOvManager.Initialize(poDS.get(), poOpenInfo->pszFilename); |
975 | 0 | if (poOpenInfo->AreSiblingFilesLoaded()) |
976 | 0 | poDS->oOvManager.TransferSiblingFiles( |
977 | 0 | poOpenInfo->StealSiblingFiles()); |
978 | 0 | } |
979 | | |
980 | | // Creating virtual overviews, but only if there is no higher priority |
981 | | // overview source, ie. a Overview element at VRT band level, |
982 | | // or external .vrt.ovr |
983 | 0 | if (!poDS->m_aosOverviewList.empty()) |
984 | 0 | { |
985 | 0 | if (poDS->nBands > 0) |
986 | 0 | { |
987 | 0 | auto poBand = dynamic_cast<VRTRasterBand *>(poDS->papoBands[0]); |
988 | 0 | if (poBand && !poBand->m_aoOverviewInfos.empty()) |
989 | 0 | { |
990 | 0 | poDS->m_aosOverviewList.Clear(); |
991 | 0 | CPLDebug("VRT", |
992 | 0 | "Ignoring virtual overviews of OverviewList " |
993 | 0 | "because Overview element is present on VRT band"); |
994 | 0 | } |
995 | 0 | else if (poBand && |
996 | 0 | poBand->GDALRasterBand::GetOverviewCount() > 0) |
997 | 0 | { |
998 | 0 | poDS->m_aosOverviewList.Clear(); |
999 | 0 | CPLDebug("VRT", |
1000 | 0 | "Ignoring virtual overviews of OverviewList " |
1001 | 0 | "because external .vrt.ovr is available"); |
1002 | 0 | } |
1003 | 0 | } |
1004 | 0 | for (int iOverview = 0; iOverview < poDS->m_aosOverviewList.size(); |
1005 | 0 | iOverview++) |
1006 | 0 | { |
1007 | 0 | const int nOvFactor = atoi(poDS->m_aosOverviewList[iOverview]); |
1008 | 0 | if (nOvFactor <= 1) |
1009 | 0 | { |
1010 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1011 | 0 | "Invalid overview factor"); |
1012 | 0 | return nullptr; |
1013 | 0 | } |
1014 | | |
1015 | 0 | poDS->AddVirtualOverview( |
1016 | 0 | nOvFactor, poDS->m_osOverviewResampling.empty() |
1017 | 0 | ? "nearest" |
1018 | 0 | : poDS->m_osOverviewResampling.c_str()); |
1019 | 0 | } |
1020 | 0 | poDS->m_aosOverviewList.Clear(); |
1021 | 0 | } |
1022 | | |
1023 | 0 | if (poDS->eAccess == GA_Update && poDS->m_poRootGroup && |
1024 | 0 | !STARTS_WITH_CI(poOpenInfo->pszFilename, "<VRT")) |
1025 | 0 | { |
1026 | 0 | poDS->m_poRootGroup->SetFilename(poOpenInfo->pszFilename); |
1027 | 0 | } |
1028 | 0 | } |
1029 | | |
1030 | 0 | return poDS.release(); |
1031 | 0 | } |
1032 | | |
1033 | | /************************************************************************/ |
1034 | | /* OpenVRTProtocol() */ |
1035 | | /* */ |
1036 | | /* Create an open VRTDataset from a vrt:// string. */ |
1037 | | /************************************************************************/ |
1038 | | |
1039 | | GDALDataset *VRTDataset::OpenVRTProtocol(const char *pszSpec) |
1040 | | |
1041 | 0 | { |
1042 | 0 | CPLAssert(STARTS_WITH_CI(pszSpec, VRT_PROTOCOL_PREFIX)); |
1043 | 0 | CPLString osFilename(pszSpec + strlen(VRT_PROTOCOL_PREFIX)); |
1044 | 0 | const auto nPosQuotationMark = osFilename.find('?'); |
1045 | 0 | CPLString osQueryString; |
1046 | 0 | if (nPosQuotationMark != std::string::npos) |
1047 | 0 | { |
1048 | 0 | osQueryString = osFilename.substr(nPosQuotationMark + 1); |
1049 | 0 | osFilename.resize(nPosQuotationMark); |
1050 | 0 | } |
1051 | | |
1052 | | // Parse query string, get args required for initial Open() |
1053 | 0 | const CPLStringList aosTokens(CSLTokenizeString2(osQueryString, "&", 0)); |
1054 | 0 | CPLStringList aosAllowedDrivers; |
1055 | 0 | CPLStringList aosOpenOptions; |
1056 | |
|
1057 | 0 | for (const auto &[pszKey, pszValue] : cpl::IterateNameValue( |
1058 | 0 | aosTokens, /* bReturnNullKeyIfNotNameValue = */ true)) |
1059 | 0 | { |
1060 | 0 | if (!pszKey) |
1061 | 0 | { |
1062 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
1063 | 0 | "Invalid option specification: %s\n" |
1064 | 0 | "must be in the form 'key=value'", |
1065 | 0 | pszValue); |
1066 | 0 | return nullptr; |
1067 | 0 | } |
1068 | 0 | else if (EQUAL(pszKey, "if")) |
1069 | 0 | { |
1070 | 0 | if (!aosAllowedDrivers.empty()) |
1071 | 0 | { |
1072 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1073 | 0 | "'if' option should be specified once, use commas " |
1074 | 0 | "to input multiple values."); |
1075 | 0 | return nullptr; |
1076 | 0 | } |
1077 | 0 | aosAllowedDrivers = CSLTokenizeString2(pszValue, ",", 0); |
1078 | 0 | } |
1079 | 0 | else if (EQUAL(pszKey, "oo")) |
1080 | 0 | { |
1081 | 0 | if (!aosOpenOptions.empty()) |
1082 | 0 | { |
1083 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1084 | 0 | "'oo' option should be specified once, use commas " |
1085 | 0 | "to input multiple values."); |
1086 | 0 | return nullptr; |
1087 | 0 | } |
1088 | 0 | aosOpenOptions = CSLTokenizeString2(pszValue, ",", 0); |
1089 | 0 | } |
1090 | 0 | } |
1091 | | |
1092 | | // We don't open in GDAL_OF_SHARED mode to avoid issues when we open a |
1093 | | // http://.jp2 file with the JP2OpenJPEG driver through the HTTP driver, |
1094 | | // which returns a /vsimem/ file |
1095 | 0 | auto poSrcDS = std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser>( |
1096 | 0 | GDALDataset::Open(osFilename, GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, |
1097 | 0 | aosAllowedDrivers.List(), aosOpenOptions.List(), |
1098 | 0 | nullptr)); |
1099 | 0 | if (poSrcDS == nullptr) |
1100 | 0 | { |
1101 | 0 | return nullptr; |
1102 | 0 | } |
1103 | | |
1104 | 0 | bool bFound_transpose = false; |
1105 | 0 | for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(aosTokens)) |
1106 | 0 | { |
1107 | 0 | if (EQUAL(pszKey, "transpose")) |
1108 | 0 | { |
1109 | 0 | bFound_transpose = true; |
1110 | 0 | const CPLStringList aosTransposeTokens( |
1111 | 0 | CSLTokenizeString2(pszValue, ":", 0)); |
1112 | 0 | if (aosTransposeTokens.size() != 2) |
1113 | 0 | { |
1114 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1115 | 0 | "Invalid transpose option: %s", pszValue); |
1116 | 0 | return nullptr; |
1117 | 0 | } |
1118 | 0 | const CPLStringList aosTransposeIndex( |
1119 | 0 | CSLTokenizeString2(aosTransposeTokens[1], ",", 0)); |
1120 | | // fail if not two values |
1121 | 0 | if (aosTransposeIndex.size() != 2) |
1122 | 0 | { |
1123 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1124 | 0 | "Invalid transpose option: %s", pszValue); |
1125 | 0 | return nullptr; |
1126 | 0 | } |
1127 | 0 | int index_x = atoi(aosTransposeIndex[0]); |
1128 | 0 | int index_y = atoi(aosTransposeIndex[1]); |
1129 | |
|
1130 | 0 | auto poMDimDS = std::unique_ptr<GDALDataset>( |
1131 | 0 | GDALDataset::Open(osFilename, GDAL_OF_MULTIDIM_RASTER)); |
1132 | 0 | if (!poMDimDS) |
1133 | 0 | return nullptr; |
1134 | 0 | auto poMdimGroup = poMDimDS->GetRootGroup(); |
1135 | 0 | if (!poMdimGroup) |
1136 | 0 | { |
1137 | 0 | return nullptr; |
1138 | 0 | } |
1139 | 0 | auto poArray = |
1140 | 0 | poMdimGroup->OpenMDArrayFromFullname(aosTransposeTokens[0]); |
1141 | 0 | if (!poArray) |
1142 | 0 | { |
1143 | 0 | return nullptr; |
1144 | 0 | } |
1145 | | |
1146 | 0 | auto poClassicDS = poArray->AsClassicDataset(index_x, index_y); |
1147 | |
|
1148 | 0 | if (!poClassicDS) |
1149 | 0 | return nullptr; |
1150 | 0 | poSrcDS = |
1151 | 0 | std::unique_ptr<GDALDataset, GDALDatasetUniquePtrReleaser>( |
1152 | 0 | poClassicDS); |
1153 | 0 | } |
1154 | 0 | } |
1155 | | // scan for sd_name/sd in tokens, close the source dataset and reopen if found/valid |
1156 | 0 | bool bFound_subdataset = false; |
1157 | 0 | for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(aosTokens)) |
1158 | 0 | { |
1159 | 0 | if (EQUAL(pszKey, "sd_name")) |
1160 | 0 | { |
1161 | 0 | if (bFound_transpose) |
1162 | 0 | { |
1163 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1164 | 0 | "'sd_name' is mutually exclusive with option " |
1165 | 0 | "'transpose'"); |
1166 | 0 | return nullptr; |
1167 | 0 | } |
1168 | 0 | if (bFound_subdataset) |
1169 | 0 | { |
1170 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1171 | 0 | "'sd_name' is mutually exclusive with option " |
1172 | 0 | "'sd'"); |
1173 | 0 | return nullptr; |
1174 | 0 | } |
1175 | 0 | char **papszSubdatasets = poSrcDS->GetMetadata("SUBDATASETS"); |
1176 | 0 | int nSubdatasets = CSLCount(papszSubdatasets); |
1177 | |
|
1178 | 0 | if (nSubdatasets > 0) |
1179 | 0 | { |
1180 | 0 | bool bFound = false; |
1181 | 0 | for (int j = 0; j < nSubdatasets && papszSubdatasets[j]; j += 2) |
1182 | 0 | { |
1183 | 0 | const char *pszEqual = strchr(papszSubdatasets[j], '='); |
1184 | 0 | if (!pszEqual) |
1185 | 0 | { |
1186 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1187 | 0 | "'sd_name:' failed to obtain " |
1188 | 0 | "subdataset string "); |
1189 | 0 | return nullptr; |
1190 | 0 | } |
1191 | 0 | const char *pszSubdatasetSource = pszEqual + 1; |
1192 | 0 | GDALSubdatasetInfoH info = |
1193 | 0 | GDALGetSubdatasetInfo(pszSubdatasetSource); |
1194 | 0 | char *component = |
1195 | 0 | info ? GDALSubdatasetInfoGetSubdatasetComponent(info) |
1196 | 0 | : nullptr; |
1197 | |
|
1198 | 0 | bFound = component && EQUAL(pszValue, component); |
1199 | 0 | bFound_subdataset = true; |
1200 | 0 | CPLFree(component); |
1201 | 0 | GDALDestroySubdatasetInfo(info); |
1202 | 0 | if (bFound) |
1203 | 0 | { |
1204 | 0 | poSrcDS.reset(GDALDataset::Open( |
1205 | 0 | pszSubdatasetSource, |
1206 | 0 | GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, |
1207 | 0 | aosAllowedDrivers.List(), aosOpenOptions.List(), |
1208 | 0 | nullptr)); |
1209 | 0 | if (poSrcDS == nullptr) |
1210 | 0 | { |
1211 | 0 | return nullptr; |
1212 | 0 | } |
1213 | | |
1214 | 0 | break; |
1215 | 0 | } |
1216 | 0 | } |
1217 | | |
1218 | 0 | if (!bFound) |
1219 | 0 | { |
1220 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1221 | 0 | "'sd_name' option should be be a valid " |
1222 | 0 | "subdataset component name"); |
1223 | 0 | return nullptr; |
1224 | 0 | } |
1225 | 0 | } |
1226 | 0 | } |
1227 | | |
1228 | 0 | if (EQUAL(pszKey, "sd")) |
1229 | 0 | { |
1230 | 0 | if (bFound_transpose) |
1231 | 0 | { |
1232 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1233 | 0 | "'sd' is mutually exclusive with option " |
1234 | 0 | "'transpose'"); |
1235 | 0 | return nullptr; |
1236 | 0 | } |
1237 | 0 | if (bFound_subdataset) |
1238 | 0 | { |
1239 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1240 | 0 | "'sd' is mutually exclusive with option " |
1241 | 0 | "'sd_name'"); |
1242 | 0 | return nullptr; |
1243 | 0 | } |
1244 | 0 | CSLConstList papszSubdatasets = poSrcDS->GetMetadata("SUBDATASETS"); |
1245 | 0 | int nSubdatasets = CSLCount(papszSubdatasets); |
1246 | |
|
1247 | 0 | if (nSubdatasets > 0) |
1248 | 0 | { |
1249 | 0 | int iSubdataset = atoi(pszValue); |
1250 | 0 | if (iSubdataset < 1 || iSubdataset > (nSubdatasets) / 2) |
1251 | 0 | { |
1252 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1253 | 0 | "'sd' option should indicate a valid " |
1254 | 0 | "subdataset component number (starting with 1)"); |
1255 | 0 | return nullptr; |
1256 | 0 | } |
1257 | 0 | const std::string osSubdatasetSource( |
1258 | 0 | strstr(papszSubdatasets[(iSubdataset - 1) * 2], "=") + 1); |
1259 | 0 | if (osSubdatasetSource.empty()) |
1260 | 0 | { |
1261 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1262 | 0 | "'sd:' failed to obtain subdataset " |
1263 | 0 | "string "); |
1264 | 0 | return nullptr; |
1265 | 0 | } |
1266 | | |
1267 | 0 | poSrcDS.reset(GDALDataset::Open( |
1268 | 0 | osSubdatasetSource.c_str(), |
1269 | 0 | GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, |
1270 | 0 | aosAllowedDrivers.List(), aosOpenOptions.List(), nullptr)); |
1271 | 0 | if (poSrcDS == nullptr) |
1272 | 0 | { |
1273 | 0 | return nullptr; |
1274 | 0 | } |
1275 | 0 | bFound_subdataset = true; |
1276 | 0 | } |
1277 | 0 | } |
1278 | 0 | } |
1279 | | |
1280 | 0 | std::vector<int> anBands; |
1281 | |
|
1282 | 0 | CPLStringList argv; |
1283 | 0 | argv.AddString("-of"); |
1284 | 0 | argv.AddString("VRT"); |
1285 | |
|
1286 | 0 | for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(aosTokens)) |
1287 | 0 | { |
1288 | 0 | if (EQUAL(pszKey, "bands")) |
1289 | 0 | { |
1290 | 0 | const CPLStringList aosBands(CSLTokenizeString2(pszValue, ",", 0)); |
1291 | 0 | for (int j = 0; j < aosBands.size(); j++) |
1292 | 0 | { |
1293 | 0 | if (EQUAL(aosBands[j], "mask")) |
1294 | 0 | { |
1295 | 0 | anBands.push_back(0); |
1296 | 0 | } |
1297 | 0 | else |
1298 | 0 | { |
1299 | 0 | const int nBand = atoi(aosBands[j]); |
1300 | 0 | if (nBand <= 0 || nBand > poSrcDS->GetRasterCount()) |
1301 | 0 | { |
1302 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1303 | 0 | "Invalid band number: %s", aosBands[j]); |
1304 | 0 | return nullptr; |
1305 | 0 | } |
1306 | 0 | anBands.push_back(nBand); |
1307 | 0 | } |
1308 | 0 | } |
1309 | | |
1310 | 0 | for (const int nBand : anBands) |
1311 | 0 | { |
1312 | 0 | argv.AddString("-b"); |
1313 | 0 | argv.AddString(nBand == 0 ? "mask" : CPLSPrintf("%d", nBand)); |
1314 | 0 | } |
1315 | 0 | } |
1316 | | |
1317 | 0 | else if (EQUAL(pszKey, "a_nodata")) |
1318 | 0 | { |
1319 | 0 | argv.AddString("-a_nodata"); |
1320 | 0 | argv.AddString(pszValue); |
1321 | 0 | } |
1322 | | |
1323 | 0 | else if (EQUAL(pszKey, "a_srs")) |
1324 | 0 | { |
1325 | 0 | argv.AddString("-a_srs"); |
1326 | 0 | argv.AddString(pszValue); |
1327 | 0 | } |
1328 | | |
1329 | 0 | else if (EQUAL(pszKey, "a_ullr")) |
1330 | 0 | { |
1331 | | // Parse the limits |
1332 | 0 | const CPLStringList aosUllr(CSLTokenizeString2(pszValue, ",", 0)); |
1333 | | // fail if not four values |
1334 | 0 | if (aosUllr.size() != 4) |
1335 | 0 | { |
1336 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1337 | 0 | "Invalid a_ullr option: %s", pszValue); |
1338 | 0 | return nullptr; |
1339 | 0 | } |
1340 | | |
1341 | 0 | argv.AddString("-a_ullr"); |
1342 | 0 | argv.AddString(aosUllr[0]); |
1343 | 0 | argv.AddString(aosUllr[1]); |
1344 | 0 | argv.AddString(aosUllr[2]); |
1345 | 0 | argv.AddString(aosUllr[3]); |
1346 | 0 | } |
1347 | | |
1348 | 0 | else if (EQUAL(pszKey, "ovr")) |
1349 | 0 | { |
1350 | 0 | argv.AddString("-ovr"); |
1351 | 0 | argv.AddString(pszValue); |
1352 | 0 | } |
1353 | 0 | else if (EQUAL(pszKey, "expand")) |
1354 | 0 | { |
1355 | 0 | argv.AddString("-expand"); |
1356 | 0 | argv.AddString(pszValue); |
1357 | 0 | } |
1358 | 0 | else if (EQUAL(pszKey, "a_scale")) |
1359 | 0 | { |
1360 | 0 | argv.AddString("-a_scale"); |
1361 | 0 | argv.AddString(pszValue); |
1362 | 0 | } |
1363 | 0 | else if (EQUAL(pszKey, "a_offset")) |
1364 | 0 | { |
1365 | 0 | argv.AddString("-a_offset"); |
1366 | 0 | argv.AddString(pszValue); |
1367 | 0 | } |
1368 | 0 | else if (EQUAL(pszKey, "ot")) |
1369 | 0 | { |
1370 | 0 | argv.AddString("-ot"); |
1371 | 0 | argv.AddString(pszValue); |
1372 | 0 | } |
1373 | 0 | else if (EQUAL(pszKey, "gcp")) |
1374 | 0 | { |
1375 | 0 | const CPLStringList aosGCP(CSLTokenizeString2(pszValue, ",", 0)); |
1376 | |
|
1377 | 0 | if (aosGCP.size() < 4 || aosGCP.size() > 5) |
1378 | 0 | { |
1379 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1380 | 0 | "Invalid value for GCP: %s\n need 4, or 5 " |
1381 | 0 | "numbers, comma separated: " |
1382 | 0 | "'gcp=<pixel>,<line>,<easting>,<northing>[,<" |
1383 | 0 | "elevation>]'", |
1384 | 0 | pszValue); |
1385 | 0 | return nullptr; |
1386 | 0 | } |
1387 | 0 | argv.AddString("-gcp"); |
1388 | 0 | for (int j = 0; j < aosGCP.size(); j++) |
1389 | 0 | { |
1390 | 0 | argv.AddString(aosGCP[j]); |
1391 | 0 | } |
1392 | 0 | } |
1393 | 0 | else if (EQUAL(pszKey, "scale") || STARTS_WITH_CI(pszKey, "scale_")) |
1394 | 0 | { |
1395 | 0 | const CPLStringList aosScaleParams( |
1396 | 0 | CSLTokenizeString2(pszValue, ",", 0)); |
1397 | |
|
1398 | 0 | if (!(aosScaleParams.size() == 2) && |
1399 | 0 | !(aosScaleParams.size() == 4) && !(aosScaleParams.size() == 1)) |
1400 | 0 | { |
1401 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1402 | 0 | "Invalid value for scale, (or scale_bn): " |
1403 | 0 | "%s\n need 'scale=true', or 2 or 4 " |
1404 | 0 | "numbers, comma separated: " |
1405 | 0 | "'scale=src_min,src_max[,dst_min,dst_max]' or " |
1406 | 0 | "'scale_bn=src_min,src_max[,dst_min,dst_max]'", |
1407 | 0 | pszValue); |
1408 | 0 | return nullptr; |
1409 | 0 | } |
1410 | | |
1411 | | // -scale because scale=true or scale=min,max or scale=min,max,dstmin,dstmax |
1412 | 0 | if (aosScaleParams.size() == 1 && CPLTestBool(aosScaleParams[0])) |
1413 | 0 | { |
1414 | 0 | argv.AddString(CPLSPrintf("-%s", pszKey)); |
1415 | 0 | } |
1416 | | // add remaining params (length 2 or 4) |
1417 | 0 | if (aosScaleParams.size() > 1) |
1418 | 0 | { |
1419 | 0 | argv.AddString(CPLSPrintf("-%s", pszKey)); |
1420 | 0 | for (int j = 0; j < aosScaleParams.size(); j++) |
1421 | 0 | { |
1422 | 0 | argv.AddString(aosScaleParams[j]); |
1423 | 0 | } |
1424 | 0 | } |
1425 | 0 | } |
1426 | 0 | else if (EQUAL(pszKey, "exponent") || |
1427 | 0 | STARTS_WITH_CI(pszKey, "exponent_")) |
1428 | 0 | { |
1429 | 0 | argv.AddString(CPLSPrintf("-%s", pszKey)); |
1430 | 0 | argv.AddString(pszValue); |
1431 | 0 | } |
1432 | 0 | else if (EQUAL(pszKey, "outsize")) |
1433 | 0 | { |
1434 | 0 | const CPLStringList aosOutSize( |
1435 | 0 | CSLTokenizeString2(pszValue, ",", 0)); |
1436 | 0 | if (aosOutSize.size() != 2) |
1437 | 0 | { |
1438 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1439 | 0 | "Invalid outsize option: %s, must be two" |
1440 | 0 | "values separated by comma pixel,line or two " |
1441 | 0 | "fraction values with percent symbol", |
1442 | 0 | pszValue); |
1443 | 0 | return nullptr; |
1444 | 0 | } |
1445 | 0 | argv.AddString("-outsize"); |
1446 | 0 | argv.AddString(aosOutSize[0]); |
1447 | 0 | argv.AddString(aosOutSize[1]); |
1448 | 0 | } |
1449 | 0 | else if (EQUAL(pszKey, "projwin")) |
1450 | 0 | { |
1451 | | // Parse the limits |
1452 | 0 | const CPLStringList aosProjWin( |
1453 | 0 | CSLTokenizeString2(pszValue, ",", 0)); |
1454 | | // fail if not four values |
1455 | 0 | if (aosProjWin.size() != 4) |
1456 | 0 | { |
1457 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1458 | 0 | "Invalid projwin option: %s", pszValue); |
1459 | 0 | return nullptr; |
1460 | 0 | } |
1461 | | |
1462 | 0 | argv.AddString("-projwin"); |
1463 | 0 | argv.AddString(aosProjWin[0]); |
1464 | 0 | argv.AddString(aosProjWin[1]); |
1465 | 0 | argv.AddString(aosProjWin[2]); |
1466 | 0 | argv.AddString(aosProjWin[3]); |
1467 | 0 | } |
1468 | 0 | else if (EQUAL(pszKey, "projwin_srs")) |
1469 | 0 | { |
1470 | 0 | argv.AddString("-projwin_srs"); |
1471 | 0 | argv.AddString(pszValue); |
1472 | 0 | } |
1473 | 0 | else if (EQUAL(pszKey, "tr")) |
1474 | 0 | { |
1475 | 0 | const CPLStringList aosTargetResolution( |
1476 | 0 | CSLTokenizeString2(pszValue, ",", 0)); |
1477 | 0 | if (aosTargetResolution.size() != 2) |
1478 | 0 | { |
1479 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1480 | 0 | "Invalid tr option: %s, must be two " |
1481 | 0 | "values separated by comma xres,yres", |
1482 | 0 | pszValue); |
1483 | 0 | return nullptr; |
1484 | 0 | } |
1485 | 0 | argv.AddString("-tr"); |
1486 | 0 | argv.AddString(aosTargetResolution[0]); |
1487 | 0 | argv.AddString(aosTargetResolution[1]); |
1488 | 0 | } |
1489 | 0 | else if (EQUAL(pszKey, "r")) |
1490 | 0 | { |
1491 | 0 | argv.AddString("-r"); |
1492 | 0 | argv.AddString(pszValue); |
1493 | 0 | } |
1494 | | |
1495 | 0 | else if (EQUAL(pszKey, "srcwin")) |
1496 | 0 | { |
1497 | | // Parse the limits |
1498 | 0 | const CPLStringList aosSrcWin(CSLTokenizeString2(pszValue, ",", 0)); |
1499 | | // fail if not four values |
1500 | 0 | if (aosSrcWin.size() != 4) |
1501 | 0 | { |
1502 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, |
1503 | 0 | "Invalid srcwin option: %s, must be four " |
1504 | 0 | "values separated by comma xoff,yoff,xsize,ysize", |
1505 | 0 | pszValue); |
1506 | 0 | return nullptr; |
1507 | 0 | } |
1508 | | |
1509 | 0 | argv.AddString("-srcwin"); |
1510 | 0 | argv.AddString(aosSrcWin[0]); |
1511 | 0 | argv.AddString(aosSrcWin[1]); |
1512 | 0 | argv.AddString(aosSrcWin[2]); |
1513 | 0 | argv.AddString(aosSrcWin[3]); |
1514 | 0 | } |
1515 | | |
1516 | 0 | else if (EQUAL(pszKey, "a_gt")) |
1517 | 0 | { |
1518 | | // Parse the limits |
1519 | 0 | const CPLStringList aosAGeoTransform( |
1520 | 0 | CSLTokenizeString2(pszValue, ",", 0)); |
1521 | | // fail if not six values |
1522 | 0 | if (aosAGeoTransform.size() != 6) |
1523 | 0 | { |
1524 | 0 | CPLError(CE_Failure, CPLE_IllegalArg, "Invalid a_gt option: %s", |
1525 | 0 | pszValue); |
1526 | 0 | return nullptr; |
1527 | 0 | } |
1528 | | |
1529 | 0 | argv.AddString("-a_gt"); |
1530 | 0 | argv.AddString(aosAGeoTransform[0]); |
1531 | 0 | argv.AddString(aosAGeoTransform[1]); |
1532 | 0 | argv.AddString(aosAGeoTransform[2]); |
1533 | 0 | argv.AddString(aosAGeoTransform[3]); |
1534 | 0 | argv.AddString(aosAGeoTransform[4]); |
1535 | 0 | argv.AddString(aosAGeoTransform[5]); |
1536 | 0 | } |
1537 | 0 | else if (EQUAL(pszKey, "oo")) |
1538 | 0 | { |
1539 | | // do nothing, we passed this in earlier |
1540 | 0 | } |
1541 | 0 | else if (EQUAL(pszKey, "if")) |
1542 | 0 | { |
1543 | | // do nothing, we passed this in earlier |
1544 | 0 | } |
1545 | 0 | else if (EQUAL(pszKey, "sd_name")) |
1546 | 0 | { |
1547 | | // do nothing, we passed this in earlier |
1548 | 0 | } |
1549 | 0 | else if (EQUAL(pszKey, "sd")) |
1550 | 0 | { |
1551 | | // do nothing, we passed this in earlier |
1552 | 0 | } |
1553 | 0 | else if (EQUAL(pszKey, "transpose")) |
1554 | 0 | { |
1555 | | // do nothing, we passed this in earlier |
1556 | 0 | } |
1557 | 0 | else if (EQUAL(pszKey, "unscale")) |
1558 | 0 | { |
1559 | 0 | if (CPLTestBool(pszValue)) |
1560 | 0 | { |
1561 | 0 | argv.AddString("-unscale"); |
1562 | 0 | } |
1563 | 0 | } |
1564 | 0 | else if (EQUAL(pszKey, "a_coord_epoch")) |
1565 | 0 | { |
1566 | 0 | argv.AddString("-a_coord_epoch"); |
1567 | 0 | argv.AddString(pszValue); |
1568 | 0 | } |
1569 | 0 | else if (EQUAL(pszKey, "nogcp")) |
1570 | 0 | { |
1571 | 0 | if (CPLTestBool(pszValue)) |
1572 | 0 | { |
1573 | 0 | argv.AddString("-nogcp"); |
1574 | 0 | } |
1575 | 0 | } |
1576 | 0 | else if (EQUAL(pszKey, "epo")) |
1577 | 0 | { |
1578 | 0 | if (CPLTestBool(pszValue)) |
1579 | 0 | { |
1580 | 0 | argv.AddString("-epo"); |
1581 | 0 | } |
1582 | 0 | } |
1583 | 0 | else if (EQUAL(pszKey, "eco")) |
1584 | 0 | { |
1585 | 0 | if (CPLTestBool(pszValue)) |
1586 | 0 | { |
1587 | 0 | argv.AddString("-eco"); |
1588 | 0 | } |
1589 | 0 | } |
1590 | | |
1591 | 0 | else |
1592 | 0 | { |
1593 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "Unknown option: %s", |
1594 | 0 | pszKey); |
1595 | 0 | return nullptr; |
1596 | 0 | } |
1597 | 0 | } |
1598 | | |
1599 | 0 | GDALTranslateOptions *psOptions = |
1600 | 0 | GDALTranslateOptionsNew(argv.List(), nullptr); |
1601 | |
|
1602 | 0 | auto hRet = GDALTranslate("", GDALDataset::ToHandle(poSrcDS.get()), |
1603 | 0 | psOptions, nullptr); |
1604 | |
|
1605 | 0 | GDALTranslateOptionsFree(psOptions); |
1606 | | |
1607 | | // Situation where we open a http://.jp2 file with the JP2OpenJPEG driver |
1608 | | // through the HTTP driver, which returns a /vsimem/ file |
1609 | 0 | const bool bPatchSourceFilename = |
1610 | 0 | (STARTS_WITH(osFilename.c_str(), "http://") || |
1611 | 0 | STARTS_WITH(osFilename.c_str(), "https://")) && |
1612 | 0 | osFilename != poSrcDS->GetDescription(); |
1613 | |
|
1614 | 0 | poSrcDS.reset(); |
1615 | |
|
1616 | 0 | auto poDS = dynamic_cast<VRTDataset *>(GDALDataset::FromHandle(hRet)); |
1617 | 0 | if (poDS) |
1618 | 0 | { |
1619 | 0 | if (bPatchSourceFilename) |
1620 | 0 | { |
1621 | 0 | for (int i = 0; i < poDS->nBands; ++i) |
1622 | 0 | { |
1623 | 0 | auto poBand = |
1624 | 0 | dynamic_cast<VRTSourcedRasterBand *>(poDS->papoBands[i]); |
1625 | 0 | if (poBand && poBand->m_papoSources.size() == 1 && |
1626 | 0 | poBand->m_papoSources[0]->IsSimpleSource()) |
1627 | 0 | { |
1628 | 0 | auto poSource = cpl::down_cast<VRTSimpleSource *>( |
1629 | 0 | poBand->m_papoSources[0].get()); |
1630 | 0 | poSource->m_bRelativeToVRTOri = 0; |
1631 | 0 | poSource->m_osSourceFileNameOri = osFilename; |
1632 | 0 | } |
1633 | 0 | } |
1634 | 0 | } |
1635 | 0 | poDS->SetDescription(pszSpec); |
1636 | 0 | poDS->SetWritable(false); |
1637 | 0 | } |
1638 | 0 | return poDS; |
1639 | 0 | } |
1640 | | |
1641 | | /************************************************************************/ |
1642 | | /* OpenXML() */ |
1643 | | /* */ |
1644 | | /* Create an open VRTDataset from a supplied XML representation */ |
1645 | | /* of the dataset. */ |
1646 | | /************************************************************************/ |
1647 | | |
1648 | | std::unique_ptr<VRTDataset> VRTDataset::OpenXML(const char *pszXML, |
1649 | | const char *pszVRTPath, |
1650 | | GDALAccess eAccessIn) |
1651 | | |
1652 | 0 | { |
1653 | | /* -------------------------------------------------------------------- */ |
1654 | | /* Parse the XML. */ |
1655 | | /* -------------------------------------------------------------------- */ |
1656 | 0 | CPLXMLTreeCloser psTree(CPLParseXMLString(pszXML)); |
1657 | 0 | if (psTree == nullptr) |
1658 | 0 | return nullptr; |
1659 | | |
1660 | 0 | CPLXMLNode *psRoot = CPLGetXMLNode(psTree.get(), "=VRTDataset"); |
1661 | 0 | if (psRoot == nullptr) |
1662 | 0 | { |
1663 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Missing VRTDataset element."); |
1664 | 0 | return nullptr; |
1665 | 0 | } |
1666 | | |
1667 | 0 | const char *pszSubClass = CPLGetXMLValue(psRoot, "subClass", ""); |
1668 | |
|
1669 | 0 | const bool bIsPansharpened = |
1670 | 0 | strcmp(pszSubClass, "VRTPansharpenedDataset") == 0; |
1671 | 0 | const bool bIsProcessed = strcmp(pszSubClass, "VRTProcessedDataset") == 0; |
1672 | |
|
1673 | 0 | if (!bIsPansharpened && !bIsProcessed && |
1674 | 0 | CPLGetXMLNode(psRoot, "Group") == nullptr && |
1675 | 0 | (CPLGetXMLNode(psRoot, "rasterXSize") == nullptr || |
1676 | 0 | CPLGetXMLNode(psRoot, "rasterYSize") == nullptr || |
1677 | 0 | CPLGetXMLNode(psRoot, "VRTRasterBand") == nullptr)) |
1678 | 0 | { |
1679 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1680 | 0 | "Missing one of rasterXSize, rasterYSize or bands on" |
1681 | 0 | " VRTDataset."); |
1682 | 0 | return nullptr; |
1683 | 0 | } |
1684 | | |
1685 | | /* -------------------------------------------------------------------- */ |
1686 | | /* Create the new virtual dataset object. */ |
1687 | | /* -------------------------------------------------------------------- */ |
1688 | 0 | const int nXSize = atoi(CPLGetXMLValue(psRoot, "rasterXSize", "0")); |
1689 | 0 | const int nYSize = atoi(CPLGetXMLValue(psRoot, "rasterYSize", "0")); |
1690 | |
|
1691 | 0 | if (!bIsPansharpened && !bIsProcessed && |
1692 | 0 | CPLGetXMLNode(psRoot, "VRTRasterBand") != nullptr && |
1693 | 0 | !GDALCheckDatasetDimensions(nXSize, nYSize)) |
1694 | 0 | { |
1695 | 0 | return nullptr; |
1696 | 0 | } |
1697 | | |
1698 | 0 | std::unique_ptr<VRTDataset> poDS; |
1699 | 0 | if (strcmp(pszSubClass, "VRTWarpedDataset") == 0) |
1700 | 0 | poDS = std::make_unique<VRTWarpedDataset>(nXSize, nYSize); |
1701 | 0 | else if (bIsPansharpened) |
1702 | 0 | poDS = std::make_unique<VRTPansharpenedDataset>(nXSize, nYSize); |
1703 | 0 | else if (bIsProcessed) |
1704 | 0 | poDS = std::make_unique<VRTProcessedDataset>(nXSize, nYSize); |
1705 | 0 | else |
1706 | 0 | { |
1707 | 0 | poDS = std::make_unique<VRTDataset>(nXSize, nYSize); |
1708 | 0 | poDS->eAccess = eAccessIn; |
1709 | 0 | } |
1710 | |
|
1711 | 0 | if (poDS->XMLInit(psRoot, pszVRTPath) != CE_None) |
1712 | 0 | { |
1713 | 0 | poDS.reset(); |
1714 | 0 | } |
1715 | | |
1716 | | /* -------------------------------------------------------------------- */ |
1717 | | /* Try to return a regular handle on the file. */ |
1718 | | /* -------------------------------------------------------------------- */ |
1719 | |
|
1720 | 0 | return poDS; |
1721 | 0 | } |
1722 | | |
1723 | | /************************************************************************/ |
1724 | | /* AddBand() */ |
1725 | | /************************************************************************/ |
1726 | | |
1727 | | CPLErr VRTDataset::AddBand(GDALDataType eType, char **papszOptions) |
1728 | | |
1729 | 0 | { |
1730 | 0 | if (eType == GDT_Unknown || eType == GDT_TypeCount) |
1731 | 0 | { |
1732 | 0 | ReportError(CE_Failure, CPLE_IllegalArg, |
1733 | 0 | "Illegal GDT_Unknown/GDT_TypeCount argument"); |
1734 | 0 | return CE_Failure; |
1735 | 0 | } |
1736 | | |
1737 | 0 | SetNeedsFlush(); |
1738 | | |
1739 | | /* ==================================================================== */ |
1740 | | /* Handle a new raw band. */ |
1741 | | /* ==================================================================== */ |
1742 | 0 | const char *pszSubClass = CSLFetchNameValue(papszOptions, "subclass"); |
1743 | |
|
1744 | 0 | if (pszSubClass != nullptr && EQUAL(pszSubClass, "VRTRawRasterBand")) |
1745 | 0 | { |
1746 | 0 | #ifdef GDAL_VRT_ENABLE_RAWRASTERBAND |
1747 | 0 | if (!VRTDataset::IsRawRasterBandEnabled()) |
1748 | 0 | { |
1749 | 0 | return CE_Failure; |
1750 | 0 | } |
1751 | 0 | const int nWordDataSize = GDALGetDataTypeSizeBytes(eType); |
1752 | | |
1753 | | /* ---------------------------------------------------------------- */ |
1754 | | /* Collect required information. */ |
1755 | | /* ---------------------------------------------------------------- */ |
1756 | 0 | const char *pszImageOffset = |
1757 | 0 | CSLFetchNameValueDef(papszOptions, "ImageOffset", "0"); |
1758 | 0 | vsi_l_offset nImageOffset = CPLScanUIntBig( |
1759 | 0 | pszImageOffset, static_cast<int>(strlen(pszImageOffset))); |
1760 | |
|
1761 | 0 | int nPixelOffset = nWordDataSize; |
1762 | 0 | const char *pszPixelOffset = |
1763 | 0 | CSLFetchNameValue(papszOptions, "PixelOffset"); |
1764 | 0 | if (pszPixelOffset != nullptr) |
1765 | 0 | nPixelOffset = atoi(pszPixelOffset); |
1766 | |
|
1767 | 0 | int nLineOffset; |
1768 | 0 | const char *pszLineOffset = |
1769 | 0 | CSLFetchNameValue(papszOptions, "LineOffset"); |
1770 | 0 | if (pszLineOffset != nullptr) |
1771 | 0 | nLineOffset = atoi(pszLineOffset); |
1772 | 0 | else |
1773 | 0 | { |
1774 | 0 | if (nPixelOffset > INT_MAX / GetRasterXSize() || |
1775 | 0 | nPixelOffset < INT_MIN / GetRasterXSize()) |
1776 | 0 | { |
1777 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Int overflow"); |
1778 | 0 | return CE_Failure; |
1779 | 0 | } |
1780 | 0 | nLineOffset = nPixelOffset * GetRasterXSize(); |
1781 | 0 | } |
1782 | | |
1783 | 0 | const char *pszByteOrder = CSLFetchNameValue(papszOptions, "ByteOrder"); |
1784 | |
|
1785 | 0 | const char *pszFilename = |
1786 | 0 | CSLFetchNameValue(papszOptions, "SourceFilename"); |
1787 | 0 | if (pszFilename == nullptr) |
1788 | 0 | { |
1789 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1790 | 0 | "AddBand() requires a SourceFilename option for " |
1791 | 0 | "VRTRawRasterBands."); |
1792 | 0 | return CE_Failure; |
1793 | 0 | } |
1794 | | |
1795 | 0 | const bool bRelativeToVRT = |
1796 | 0 | CPLFetchBool(papszOptions, "relativeToVRT", false); |
1797 | | |
1798 | | /* --------------------------------------------------------------- */ |
1799 | | /* Create and initialize the band. */ |
1800 | | /* --------------------------------------------------------------- */ |
1801 | |
|
1802 | 0 | auto poBand = std::make_unique<VRTRawRasterBand>( |
1803 | 0 | this, GetRasterCount() + 1, eType); |
1804 | |
|
1805 | 0 | const std::string osPath = CPLGetPathSafe(GetDescription()); |
1806 | 0 | const CPLErr eErr = poBand->SetRawLink( |
1807 | 0 | pszFilename, osPath.empty() ? nullptr : osPath.c_str(), |
1808 | 0 | bRelativeToVRT, nImageOffset, nPixelOffset, nLineOffset, |
1809 | 0 | pszByteOrder); |
1810 | 0 | if (eErr == CE_None) |
1811 | 0 | SetBand(GetRasterCount() + 1, std::move(poBand)); |
1812 | |
|
1813 | 0 | return eErr; |
1814 | | #else |
1815 | | CPLError(CE_Failure, CPLE_NotSupported, |
1816 | | "VRTDataset::AddBand(): cannot instantiate VRTRawRasterBand, " |
1817 | | "because disabled in this GDAL build"); |
1818 | | return CE_Failure; |
1819 | | #endif |
1820 | 0 | } |
1821 | | |
1822 | | /* ==================================================================== */ |
1823 | | /* Handle a new "sourced" band. */ |
1824 | | /* ==================================================================== */ |
1825 | 0 | else |
1826 | 0 | { |
1827 | 0 | VRTSourcedRasterBand *poBand = nullptr; |
1828 | |
|
1829 | 0 | int nBlockXSizeIn = |
1830 | 0 | atoi(CSLFetchNameValueDef(papszOptions, "BLOCKXSIZE", "0")); |
1831 | 0 | int nBlockYSizeIn = |
1832 | 0 | atoi(CSLFetchNameValueDef(papszOptions, "BLOCKYSIZE", "0")); |
1833 | 0 | if (nBlockXSizeIn == 0 && nBlockYSizeIn == 0) |
1834 | 0 | { |
1835 | 0 | nBlockXSizeIn = m_nBlockXSize; |
1836 | 0 | nBlockYSizeIn = m_nBlockYSize; |
1837 | 0 | } |
1838 | | |
1839 | | /* ---- Check for our sourced band 'derived' subclass ---- */ |
1840 | 0 | if (pszSubClass != nullptr && |
1841 | 0 | EQUAL(pszSubClass, "VRTDerivedRasterBand")) |
1842 | 0 | { |
1843 | | |
1844 | | /* We'll need a pointer to the subclass in case we need */ |
1845 | | /* to set the new band's pixel function below. */ |
1846 | 0 | VRTDerivedRasterBand *poDerivedBand = new VRTDerivedRasterBand( |
1847 | 0 | this, GetRasterCount() + 1, eType, GetRasterXSize(), |
1848 | 0 | GetRasterYSize(), nBlockXSizeIn, nBlockYSizeIn); |
1849 | | |
1850 | | /* Set the pixel function options it provided. */ |
1851 | 0 | const char *pszFuncName = |
1852 | 0 | CSLFetchNameValue(papszOptions, "PixelFunctionType"); |
1853 | 0 | if (pszFuncName != nullptr) |
1854 | 0 | poDerivedBand->SetPixelFunctionName(pszFuncName); |
1855 | |
|
1856 | 0 | const char *pszLanguage = |
1857 | 0 | CSLFetchNameValue(papszOptions, "PixelFunctionLanguage"); |
1858 | 0 | if (pszLanguage != nullptr) |
1859 | 0 | poDerivedBand->SetPixelFunctionLanguage(pszLanguage); |
1860 | |
|
1861 | 0 | const char *pszSkipNonContributingSources = |
1862 | 0 | CSLFetchNameValue(papszOptions, "SkipNonContributingSources"); |
1863 | 0 | if (pszSkipNonContributingSources != nullptr) |
1864 | 0 | { |
1865 | 0 | poDerivedBand->SetSkipNonContributingSources( |
1866 | 0 | CPLTestBool(pszSkipNonContributingSources)); |
1867 | 0 | } |
1868 | 0 | for (const auto &[pszKey, pszValue] : |
1869 | 0 | cpl::IterateNameValue(static_cast<CSLConstList>(papszOptions))) |
1870 | 0 | { |
1871 | 0 | if (STARTS_WITH(pszKey, "_PIXELFN_ARG_")) |
1872 | 0 | { |
1873 | 0 | poDerivedBand->AddPixelFunctionArgument(pszKey + 13, |
1874 | 0 | pszValue); |
1875 | 0 | } |
1876 | 0 | } |
1877 | |
|
1878 | 0 | const char *pszTransferTypeName = |
1879 | 0 | CSLFetchNameValue(papszOptions, "SourceTransferType"); |
1880 | 0 | if (pszTransferTypeName != nullptr) |
1881 | 0 | { |
1882 | 0 | const GDALDataType eTransferType = |
1883 | 0 | GDALGetDataTypeByName(pszTransferTypeName); |
1884 | 0 | if (eTransferType == GDT_Unknown) |
1885 | 0 | { |
1886 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1887 | 0 | "invalid SourceTransferType: \"%s\".", |
1888 | 0 | pszTransferTypeName); |
1889 | 0 | delete poDerivedBand; |
1890 | 0 | return CE_Failure; |
1891 | 0 | } |
1892 | 0 | poDerivedBand->SetSourceTransferType(eTransferType); |
1893 | 0 | } |
1894 | | |
1895 | | /* We're done with the derived band specific stuff, so */ |
1896 | | /* we can assign the base class pointer now. */ |
1897 | 0 | poBand = poDerivedBand; |
1898 | 0 | } |
1899 | 0 | else |
1900 | 0 | { |
1901 | | /* ---- Standard sourced band ---- */ |
1902 | 0 | poBand = new VRTSourcedRasterBand( |
1903 | 0 | this, GetRasterCount() + 1, eType, GetRasterXSize(), |
1904 | 0 | GetRasterYSize(), nBlockXSizeIn, nBlockYSizeIn); |
1905 | 0 | } |
1906 | | |
1907 | 0 | SetBand(GetRasterCount() + 1, poBand); |
1908 | |
|
1909 | 0 | for (int i = 0; papszOptions != nullptr && papszOptions[i] != nullptr; |
1910 | 0 | i++) |
1911 | 0 | { |
1912 | 0 | if (STARTS_WITH_CI(papszOptions[i], "AddFuncSource=")) |
1913 | 0 | { |
1914 | 0 | char **papszTokens = CSLTokenizeStringComplex( |
1915 | 0 | papszOptions[i] + 14, ",", TRUE, FALSE); |
1916 | 0 | if (CSLCount(papszTokens) < 1) |
1917 | 0 | { |
1918 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1919 | 0 | "AddFuncSource(): required argument missing."); |
1920 | | // TODO: How should this error be handled? Return |
1921 | | // CE_Failure? |
1922 | 0 | } |
1923 | |
|
1924 | 0 | VRTImageReadFunc pfnReadFunc = nullptr; |
1925 | 0 | sscanf(papszTokens[0], "%p", &pfnReadFunc); |
1926 | |
|
1927 | 0 | void *pCBData = nullptr; |
1928 | 0 | if (CSLCount(papszTokens) > 1) |
1929 | 0 | sscanf(papszTokens[1], "%p", &pCBData); |
1930 | |
|
1931 | 0 | const double dfNoDataValue = (CSLCount(papszTokens) > 2) |
1932 | 0 | ? CPLAtof(papszTokens[2]) |
1933 | 0 | : VRT_NODATA_UNSET; |
1934 | |
|
1935 | 0 | poBand->AddFuncSource(pfnReadFunc, pCBData, dfNoDataValue); |
1936 | |
|
1937 | 0 | CSLDestroy(papszTokens); |
1938 | 0 | } |
1939 | 0 | } |
1940 | |
|
1941 | 0 | return CE_None; |
1942 | 0 | } |
1943 | 0 | } |
1944 | | |
1945 | | /*! @endcond */ |
1946 | | /************************************************************************/ |
1947 | | /* VRTAddBand() */ |
1948 | | /************************************************************************/ |
1949 | | |
1950 | | /** |
1951 | | * @see VRTDataset::VRTAddBand(). |
1952 | | * |
1953 | | * @note The return type of this function is int, but the actual values |
1954 | | * returned are of type CPLErr. |
1955 | | */ |
1956 | | |
1957 | | int CPL_STDCALL VRTAddBand(VRTDatasetH hDataset, GDALDataType eType, |
1958 | | char **papszOptions) |
1959 | | |
1960 | 0 | { |
1961 | 0 | VALIDATE_POINTER1(hDataset, "VRTAddBand", 0); |
1962 | | |
1963 | 0 | return static_cast<VRTDataset *>(GDALDataset::FromHandle(hDataset)) |
1964 | 0 | ->AddBand(eType, papszOptions); |
1965 | 0 | } |
1966 | | |
1967 | | /*! @cond Doxygen_Suppress */ |
1968 | | |
1969 | | /************************************************************************/ |
1970 | | /* Create() */ |
1971 | | /************************************************************************/ |
1972 | | |
1973 | | GDALDataset *VRTDataset::Create(const char *pszName, int nXSize, int nYSize, |
1974 | | int nBandsIn, GDALDataType eType, |
1975 | | char **papszOptions) |
1976 | | |
1977 | 0 | { |
1978 | 0 | return CreateVRTDataset(pszName, nXSize, nYSize, nBandsIn, eType, |
1979 | 0 | const_cast<CSLConstList>(papszOptions)) |
1980 | 0 | .release(); |
1981 | 0 | } |
1982 | | |
1983 | | /************************************************************************/ |
1984 | | /* CreateVRTDataset() */ |
1985 | | /************************************************************************/ |
1986 | | |
1987 | | std::unique_ptr<VRTDataset> |
1988 | | VRTDataset::CreateVRTDataset(const char *pszName, int nXSize, int nYSize, |
1989 | | int nBandsIn, GDALDataType eType, |
1990 | | CSLConstList papszOptions) |
1991 | | |
1992 | 0 | { |
1993 | 0 | if (STARTS_WITH_CI(pszName, "<VRTDataset")) |
1994 | 0 | { |
1995 | 0 | auto poDS = OpenXML(pszName, nullptr, GA_Update); |
1996 | 0 | if (poDS != nullptr) |
1997 | 0 | poDS->SetDescription("<FromXML>"); |
1998 | 0 | return poDS; |
1999 | 0 | } |
2000 | | |
2001 | 0 | const char *pszSubclass = CSLFetchNameValue(papszOptions, "SUBCLASS"); |
2002 | |
|
2003 | 0 | std::unique_ptr<VRTDataset> poDS; |
2004 | |
|
2005 | 0 | const int nBlockXSize = |
2006 | 0 | atoi(CSLFetchNameValueDef(papszOptions, "BLOCKXSIZE", "0")); |
2007 | 0 | const int nBlockYSize = |
2008 | 0 | atoi(CSLFetchNameValueDef(papszOptions, "BLOCKYSIZE", "0")); |
2009 | 0 | if (pszSubclass == nullptr || EQUAL(pszSubclass, "VRTDataset")) |
2010 | 0 | poDS = std::make_unique<VRTDataset>(nXSize, nYSize, nBlockXSize, |
2011 | 0 | nBlockYSize); |
2012 | 0 | else if (EQUAL(pszSubclass, "VRTWarpedDataset")) |
2013 | 0 | { |
2014 | 0 | poDS = std::make_unique<VRTWarpedDataset>(nXSize, nYSize, nBlockXSize, |
2015 | 0 | nBlockYSize); |
2016 | 0 | } |
2017 | 0 | else |
2018 | 0 | { |
2019 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "SUBCLASS=%s not recognised.", |
2020 | 0 | pszSubclass); |
2021 | 0 | return nullptr; |
2022 | 0 | } |
2023 | 0 | poDS->eAccess = GA_Update; |
2024 | |
|
2025 | 0 | poDS->SetDescription(pszName); |
2026 | |
|
2027 | 0 | for (int iBand = 0; iBand < nBandsIn; iBand++) |
2028 | 0 | poDS->AddBand(eType, nullptr); |
2029 | |
|
2030 | 0 | poDS->SetNeedsFlush(); |
2031 | |
|
2032 | 0 | poDS->oOvManager.Initialize(poDS.get(), pszName); |
2033 | |
|
2034 | 0 | return poDS; |
2035 | 0 | } |
2036 | | |
2037 | | /************************************************************************/ |
2038 | | /* CreateMultiDimensional() */ |
2039 | | /************************************************************************/ |
2040 | | |
2041 | | GDALDataset * |
2042 | | VRTDataset::CreateMultiDimensional(const char *pszFilename, |
2043 | | CSLConstList /*papszRootGroupOptions*/, |
2044 | | CSLConstList /*papszOptions*/) |
2045 | 0 | { |
2046 | 0 | VRTDataset *poDS = new VRTDataset(0, 0); |
2047 | 0 | poDS->eAccess = GA_Update; |
2048 | 0 | poDS->SetDescription(pszFilename); |
2049 | 0 | poDS->m_poRootGroup = VRTGroup::Create(std::string(), "/"); |
2050 | 0 | poDS->m_poRootGroup->SetIsRootGroup(); |
2051 | 0 | poDS->m_poRootGroup->SetFilename(pszFilename); |
2052 | 0 | poDS->m_poRootGroup->SetDirty(); |
2053 | |
|
2054 | 0 | return poDS; |
2055 | 0 | } |
2056 | | |
2057 | | /************************************************************************/ |
2058 | | /* GetFileList() */ |
2059 | | /************************************************************************/ |
2060 | | |
2061 | | char **VRTDataset::GetFileList() |
2062 | 0 | { |
2063 | 0 | char **papszFileList = GDALDataset::GetFileList(); |
2064 | |
|
2065 | 0 | int nSize = CSLCount(papszFileList); |
2066 | 0 | int nMaxSize = nSize; |
2067 | | |
2068 | | // Do not need an element deallocator as each string points to an |
2069 | | // element of the papszFileList. |
2070 | 0 | CPLHashSet *hSetFiles = |
2071 | 0 | CPLHashSetNew(CPLHashSetHashStr, CPLHashSetEqualStr, nullptr); |
2072 | |
|
2073 | 0 | for (int iBand = 0; iBand < nBands; iBand++) |
2074 | 0 | { |
2075 | 0 | static_cast<VRTRasterBand *>(papoBands[iBand]) |
2076 | 0 | ->GetFileList(&papszFileList, &nSize, &nMaxSize, hSetFiles); |
2077 | 0 | } |
2078 | |
|
2079 | 0 | CPLHashSetDestroy(hSetFiles); |
2080 | |
|
2081 | 0 | return papszFileList; |
2082 | 0 | } |
2083 | | |
2084 | | /************************************************************************/ |
2085 | | /* Delete() */ |
2086 | | /************************************************************************/ |
2087 | | |
2088 | | /* We implement Delete() to avoid that the default implementation */ |
2089 | | /* in GDALDriver::Delete() destroys the source files listed by GetFileList(),*/ |
2090 | | /* which would be an undesired effect... */ |
2091 | | CPLErr VRTDataset::Delete(const char *pszFilename) |
2092 | 0 | { |
2093 | 0 | GDALDriverH hDriver = GDALIdentifyDriver(pszFilename, nullptr); |
2094 | |
|
2095 | 0 | if (!hDriver || !EQUAL(GDALGetDriverShortName(hDriver), "VRT")) |
2096 | 0 | return CE_Failure; |
2097 | | |
2098 | 0 | if (strstr(pszFilename, "<VRTDataset") == nullptr && |
2099 | 0 | VSIUnlink(pszFilename) != 0) |
2100 | 0 | { |
2101 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Deleting %s failed:\n%s", |
2102 | 0 | pszFilename, VSIStrerror(errno)); |
2103 | 0 | return CE_Failure; |
2104 | 0 | } |
2105 | | |
2106 | 0 | return CE_None; |
2107 | 0 | } |
2108 | | |
2109 | | /************************************************************************/ |
2110 | | /* CreateMaskBand() */ |
2111 | | /************************************************************************/ |
2112 | | |
2113 | | CPLErr VRTDataset::CreateMaskBand(int) |
2114 | 0 | { |
2115 | 0 | if (m_poMaskBand != nullptr) |
2116 | 0 | { |
2117 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2118 | 0 | "This VRT dataset has already a mask band"); |
2119 | 0 | return CE_Failure; |
2120 | 0 | } |
2121 | | |
2122 | 0 | SetMaskBand(std::make_unique<VRTSourcedRasterBand>(this, 0)); |
2123 | |
|
2124 | 0 | return CE_None; |
2125 | 0 | } |
2126 | | |
2127 | | /************************************************************************/ |
2128 | | /* SetMaskBand() */ |
2129 | | /************************************************************************/ |
2130 | | |
2131 | | void VRTDataset::SetMaskBand(std::unique_ptr<VRTRasterBand> poMaskBandIn) |
2132 | 0 | { |
2133 | 0 | m_poMaskBand = std::move(poMaskBandIn); |
2134 | 0 | m_poMaskBand->SetIsMaskBand(); |
2135 | 0 | } |
2136 | | |
2137 | | /************************************************************************/ |
2138 | | /* CloseDependentDatasets() */ |
2139 | | /************************************************************************/ |
2140 | | |
2141 | | int VRTDataset::CloseDependentDatasets() |
2142 | 0 | { |
2143 | | /* We need to call it before removing the sources, otherwise */ |
2144 | | /* we would remove them from the serizalized VRT */ |
2145 | 0 | FlushCache(true); |
2146 | |
|
2147 | 0 | int bHasDroppedRef = GDALDataset::CloseDependentDatasets(); |
2148 | |
|
2149 | 0 | for (int iBand = 0; iBand < nBands; iBand++) |
2150 | 0 | { |
2151 | 0 | bHasDroppedRef |= static_cast<VRTRasterBand *>(papoBands[iBand]) |
2152 | 0 | ->CloseDependentDatasets(); |
2153 | 0 | } |
2154 | |
|
2155 | 0 | return bHasDroppedRef; |
2156 | 0 | } |
2157 | | |
2158 | | /************************************************************************/ |
2159 | | /* CheckCompatibleForDatasetIO() */ |
2160 | | /************************************************************************/ |
2161 | | |
2162 | | /* We will return TRUE only if all the bands are VRTSourcedRasterBands */ |
2163 | | /* made of identical sources, that are strictly VRTSimpleSource, and that */ |
2164 | | /* the band number of each source is the band number of the */ |
2165 | | /* VRTSourcedRasterBand. */ |
2166 | | |
2167 | | bool VRTDataset::CheckCompatibleForDatasetIO() const |
2168 | 0 | { |
2169 | 0 | size_t nSources = 0; |
2170 | 0 | const std::unique_ptr<VRTSource> *papoSources = nullptr; |
2171 | 0 | CPLString osResampling; |
2172 | |
|
2173 | 0 | if (m_nCompatibleForDatasetIO >= 0) |
2174 | 0 | { |
2175 | 0 | return CPL_TO_BOOL(m_nCompatibleForDatasetIO); |
2176 | 0 | } |
2177 | | |
2178 | 0 | m_nCompatibleForDatasetIO = false; |
2179 | |
|
2180 | 0 | GDALDataset *poFirstBandSourceDS = nullptr; |
2181 | 0 | for (int iBand = 0; iBand < nBands; iBand++) |
2182 | 0 | { |
2183 | 0 | auto poVRTBand = static_cast<VRTRasterBand *>(papoBands[iBand]); |
2184 | 0 | assert(poVRTBand); |
2185 | 0 | if (!poVRTBand->IsSourcedRasterBand()) |
2186 | 0 | return false; |
2187 | | |
2188 | 0 | const VRTSourcedRasterBand *poBand = |
2189 | 0 | static_cast<const VRTSourcedRasterBand *>(poVRTBand); |
2190 | | |
2191 | | // Do not allow VRTDerivedRasterBand for example |
2192 | 0 | if (typeid(*poBand) != typeid(VRTSourcedRasterBand)) |
2193 | 0 | return false; |
2194 | | |
2195 | 0 | if (iBand == 0) |
2196 | 0 | { |
2197 | 0 | nSources = poBand->m_papoSources.size(); |
2198 | 0 | papoSources = poBand->m_papoSources.data(); |
2199 | 0 | for (auto &poSource : poBand->m_papoSources) |
2200 | 0 | { |
2201 | 0 | if (!poSource->IsSimpleSource()) |
2202 | 0 | return false; |
2203 | | |
2204 | 0 | const VRTSimpleSource *poSimpleSource = |
2205 | 0 | static_cast<const VRTSimpleSource *>(poSource.get()); |
2206 | 0 | if (poSimpleSource->GetType() != |
2207 | 0 | VRTSimpleSource::GetTypeStatic()) |
2208 | 0 | return false; |
2209 | | |
2210 | 0 | if (poSimpleSource->m_nBand != iBand + 1 || |
2211 | 0 | poSimpleSource->m_bGetMaskBand || |
2212 | 0 | (nSources > 1 && poSimpleSource->m_osSrcDSName.empty())) |
2213 | 0 | { |
2214 | 0 | return false; |
2215 | 0 | } |
2216 | 0 | if (nSources == 1 && poSimpleSource->m_osSrcDSName.empty()) |
2217 | 0 | { |
2218 | 0 | if (auto poSourceBand = poSimpleSource->GetRasterBand()) |
2219 | 0 | { |
2220 | 0 | poFirstBandSourceDS = poSourceBand->GetDataset(); |
2221 | 0 | } |
2222 | 0 | else |
2223 | 0 | { |
2224 | 0 | return false; |
2225 | 0 | } |
2226 | 0 | } |
2227 | 0 | osResampling = poSimpleSource->GetResampling(); |
2228 | 0 | } |
2229 | 0 | } |
2230 | 0 | else if (nSources != poBand->m_papoSources.size()) |
2231 | 0 | { |
2232 | 0 | return false; |
2233 | 0 | } |
2234 | 0 | else |
2235 | 0 | { |
2236 | 0 | for (size_t iSource = 0; iSource < nSources; iSource++) |
2237 | 0 | { |
2238 | 0 | if (!poBand->m_papoSources[iSource]->IsSimpleSource()) |
2239 | 0 | return false; |
2240 | 0 | const VRTSimpleSource *poRefSource = |
2241 | 0 | static_cast<const VRTSimpleSource *>( |
2242 | 0 | papoSources[iSource].get()); |
2243 | |
|
2244 | 0 | const VRTSimpleSource *poSource = |
2245 | 0 | static_cast<const VRTSimpleSource *>( |
2246 | 0 | poBand->m_papoSources[iSource].get()); |
2247 | 0 | if (poSource->GetType() != VRTSimpleSource::GetTypeStatic()) |
2248 | 0 | return false; |
2249 | 0 | if (poSource->m_nBand != iBand + 1 || |
2250 | 0 | poSource->m_bGetMaskBand || |
2251 | 0 | (nSources > 1 && poSource->m_osSrcDSName.empty())) |
2252 | 0 | return false; |
2253 | 0 | if (!poSource->IsSameExceptBandNumber(poRefSource)) |
2254 | 0 | return false; |
2255 | 0 | if (osResampling.compare(poSource->GetResampling()) != 0) |
2256 | 0 | return false; |
2257 | 0 | if (nSources == 1 && poSource->m_osSrcDSName.empty()) |
2258 | 0 | { |
2259 | 0 | auto poSourceBand = poSource->GetRasterBand(); |
2260 | 0 | if (!poSourceBand || |
2261 | 0 | poFirstBandSourceDS != poSourceBand->GetDataset()) |
2262 | 0 | { |
2263 | 0 | return false; |
2264 | 0 | } |
2265 | 0 | } |
2266 | 0 | } |
2267 | 0 | } |
2268 | 0 | } |
2269 | | |
2270 | 0 | m_nCompatibleForDatasetIO = nSources != 0; |
2271 | 0 | return CPL_TO_BOOL(m_nCompatibleForDatasetIO); |
2272 | 0 | } |
2273 | | |
2274 | | /************************************************************************/ |
2275 | | /* GetSingleSimpleSource() */ |
2276 | | /* */ |
2277 | | /* Returns a non-NULL dataset if the VRT is made of a single source */ |
2278 | | /* that is a simple source, in its full extent, and with all of its */ |
2279 | | /* bands. Basically something produced by : */ |
2280 | | /* gdal_translate src dst.vrt -of VRT (-a_srs / -a_ullr) */ |
2281 | | /************************************************************************/ |
2282 | | |
2283 | | GDALDataset *VRTDataset::GetSingleSimpleSource() |
2284 | 0 | { |
2285 | 0 | if (!CheckCompatibleForDatasetIO()) |
2286 | 0 | return nullptr; |
2287 | | |
2288 | 0 | VRTSourcedRasterBand *poVRTBand = |
2289 | 0 | static_cast<VRTSourcedRasterBand *>(papoBands[0]); |
2290 | 0 | if (poVRTBand->m_papoSources.size() != 1) |
2291 | 0 | return nullptr; |
2292 | | |
2293 | 0 | VRTSimpleSource *poSource = |
2294 | 0 | static_cast<VRTSimpleSource *>(poVRTBand->m_papoSources[0].get()); |
2295 | |
|
2296 | 0 | GDALRasterBand *poBand = poSource->GetRasterBand(); |
2297 | 0 | if (poBand == nullptr || poSource->GetMaskBandMainBand() != nullptr) |
2298 | 0 | return nullptr; |
2299 | | |
2300 | 0 | GDALDataset *poSrcDS = poBand->GetDataset(); |
2301 | 0 | if (poSrcDS == nullptr) |
2302 | 0 | return nullptr; |
2303 | | |
2304 | | /* Check that it uses the full source dataset */ |
2305 | 0 | double dfReqXOff = 0.0; |
2306 | 0 | double dfReqYOff = 0.0; |
2307 | 0 | double dfReqXSize = 0.0; |
2308 | 0 | double dfReqYSize = 0.0; |
2309 | 0 | int nReqXOff = 0; |
2310 | 0 | int nReqYOff = 0; |
2311 | 0 | int nReqXSize = 0; |
2312 | 0 | int nReqYSize = 0; |
2313 | 0 | int nOutXOff = 0; |
2314 | 0 | int nOutYOff = 0; |
2315 | 0 | int nOutXSize = 0; |
2316 | 0 | int nOutYSize = 0; |
2317 | 0 | bool bError = false; |
2318 | 0 | if (!poSource->GetSrcDstWindow( |
2319 | 0 | 0, 0, poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize(), |
2320 | 0 | poSrcDS->GetRasterXSize(), poSrcDS->GetRasterYSize(), &dfReqXOff, |
2321 | 0 | &dfReqYOff, &dfReqXSize, &dfReqYSize, &nReqXOff, &nReqYOff, |
2322 | 0 | &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff, &nOutXSize, |
2323 | 0 | &nOutYSize, bError)) |
2324 | 0 | return nullptr; |
2325 | | |
2326 | 0 | if (nReqXOff != 0 || nReqYOff != 0 || |
2327 | 0 | nReqXSize != poSrcDS->GetRasterXSize() || |
2328 | 0 | nReqYSize != poSrcDS->GetRasterYSize()) |
2329 | 0 | return nullptr; |
2330 | | |
2331 | 0 | if (nOutXOff != 0 || nOutYOff != 0 || |
2332 | 0 | nOutXSize != poSrcDS->GetRasterXSize() || |
2333 | 0 | nOutYSize != poSrcDS->GetRasterYSize()) |
2334 | 0 | return nullptr; |
2335 | | |
2336 | 0 | return poSrcDS; |
2337 | 0 | } |
2338 | | |
2339 | | /************************************************************************/ |
2340 | | /* AdviseRead() */ |
2341 | | /************************************************************************/ |
2342 | | |
2343 | | CPLErr VRTDataset::AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize, |
2344 | | int nBufXSize, int nBufYSize, GDALDataType eDT, |
2345 | | int nBandCount, int *panBandList, |
2346 | | char **papszOptions) |
2347 | 0 | { |
2348 | 0 | if (!CheckCompatibleForDatasetIO()) |
2349 | 0 | return CE_None; |
2350 | | |
2351 | 0 | VRTSourcedRasterBand *poVRTBand = |
2352 | 0 | static_cast<VRTSourcedRasterBand *>(papoBands[0]); |
2353 | 0 | if (poVRTBand->m_papoSources.size() != 1) |
2354 | 0 | return CE_None; |
2355 | | |
2356 | 0 | VRTSimpleSource *poSource = |
2357 | 0 | static_cast<VRTSimpleSource *>(poVRTBand->m_papoSources[0].get()); |
2358 | | |
2359 | | /* Find source window and buffer size */ |
2360 | 0 | double dfReqXOff = 0.0; |
2361 | 0 | double dfReqYOff = 0.0; |
2362 | 0 | double dfReqXSize = 0.0; |
2363 | 0 | double dfReqYSize = 0.0; |
2364 | 0 | int nReqXOff = 0; |
2365 | 0 | int nReqYOff = 0; |
2366 | 0 | int nReqXSize = 0; |
2367 | 0 | int nReqYSize = 0; |
2368 | 0 | int nOutXOff = 0; |
2369 | 0 | int nOutYOff = 0; |
2370 | 0 | int nOutXSize = 0; |
2371 | 0 | int nOutYSize = 0; |
2372 | 0 | bool bError = false; |
2373 | 0 | if (!poSource->GetSrcDstWindow(nXOff, nYOff, nXSize, nYSize, nBufXSize, |
2374 | 0 | nBufYSize, &dfReqXOff, &dfReqYOff, |
2375 | 0 | &dfReqXSize, &dfReqYSize, &nReqXOff, |
2376 | 0 | &nReqYOff, &nReqXSize, &nReqYSize, &nOutXOff, |
2377 | 0 | &nOutYOff, &nOutXSize, &nOutYSize, bError)) |
2378 | 0 | { |
2379 | 0 | return bError ? CE_Failure : CE_None; |
2380 | 0 | } |
2381 | | |
2382 | 0 | GDALRasterBand *poBand = poSource->GetRasterBand(); |
2383 | 0 | if (poBand == nullptr || poSource->GetMaskBandMainBand() != nullptr) |
2384 | 0 | return CE_None; |
2385 | | |
2386 | 0 | GDALDataset *poSrcDS = poBand->GetDataset(); |
2387 | 0 | if (poSrcDS == nullptr) |
2388 | 0 | return CE_None; |
2389 | | |
2390 | 0 | return poSrcDS->AdviseRead(nReqXOff, nReqYOff, nReqXSize, nReqYSize, |
2391 | 0 | nOutXSize, nOutYSize, eDT, nBandCount, |
2392 | 0 | panBandList, papszOptions); |
2393 | 0 | } |
2394 | | |
2395 | | /************************************************************************/ |
2396 | | /* GetNumThreads() */ |
2397 | | /************************************************************************/ |
2398 | | |
2399 | | /* static */ int VRTDataset::GetNumThreads(GDALDataset *poDS) |
2400 | 0 | { |
2401 | 0 | const char *pszNumThreads = nullptr; |
2402 | 0 | if (poDS) |
2403 | 0 | pszNumThreads = CSLFetchNameValueDef(poDS->GetOpenOptions(), |
2404 | 0 | "NUM_THREADS", nullptr); |
2405 | 0 | if (!pszNumThreads) |
2406 | 0 | pszNumThreads = CPLGetConfigOption("VRT_NUM_THREADS", nullptr); |
2407 | 0 | if (!pszNumThreads) |
2408 | 0 | pszNumThreads = CPLGetConfigOption("GDAL_NUM_THREADS", "ALL_CPUS"); |
2409 | 0 | if (EQUAL(pszNumThreads, "0") || EQUAL(pszNumThreads, "1")) |
2410 | 0 | return atoi(pszNumThreads); |
2411 | 0 | const int nMaxPoolSize = GDALGetMaxDatasetPoolSize(); |
2412 | 0 | const int nLimit = std::min(CPLGetNumCPUs(), nMaxPoolSize); |
2413 | 0 | if (EQUAL(pszNumThreads, "ALL_CPUS")) |
2414 | 0 | return nLimit; |
2415 | 0 | return std::min(atoi(pszNumThreads), nLimit); |
2416 | 0 | } |
2417 | | |
2418 | | /************************************************************************/ |
2419 | | /* VRTDatasetRasterIOJob */ |
2420 | | /************************************************************************/ |
2421 | | |
2422 | | /** Structure used to declare a threaded job to satisfy IRasterIO() |
2423 | | * on a given source. |
2424 | | */ |
2425 | | struct VRTDatasetRasterIOJob |
2426 | | { |
2427 | | std::atomic<int> *pnCompletedJobs = nullptr; |
2428 | | std::atomic<bool> *pbSuccess = nullptr; |
2429 | | CPLErrorAccumulator *poErrorAccumulator = nullptr; |
2430 | | |
2431 | | GDALDataType eVRTBandDataType = GDT_Unknown; |
2432 | | int nXOff = 0; |
2433 | | int nYOff = 0; |
2434 | | int nXSize = 0; |
2435 | | int nYSize = 0; |
2436 | | void *pData = nullptr; |
2437 | | int nBufXSize = 0; |
2438 | | int nBufYSize = 0; |
2439 | | int nBandCount = 0; |
2440 | | BANDMAP_TYPE panBandMap = nullptr; |
2441 | | GDALDataType eBufType = GDT_Unknown; |
2442 | | GSpacing nPixelSpace = 0; |
2443 | | GSpacing nLineSpace = 0; |
2444 | | GSpacing nBandSpace = 0; |
2445 | | GDALRasterIOExtraArg *psExtraArg = nullptr; |
2446 | | VRTSimpleSource *poSource = nullptr; |
2447 | | |
2448 | | static void Func(void *pData); |
2449 | | }; |
2450 | | |
2451 | | /************************************************************************/ |
2452 | | /* VRTDatasetRasterIOJob::Func() */ |
2453 | | /************************************************************************/ |
2454 | | |
2455 | | void VRTDatasetRasterIOJob::Func(void *pData) |
2456 | 0 | { |
2457 | 0 | auto psJob = std::unique_ptr<VRTDatasetRasterIOJob>( |
2458 | 0 | static_cast<VRTDatasetRasterIOJob *>(pData)); |
2459 | 0 | if (*psJob->pbSuccess) |
2460 | 0 | { |
2461 | 0 | GDALRasterIOExtraArg sArg = *(psJob->psExtraArg); |
2462 | 0 | sArg.pfnProgress = nullptr; |
2463 | 0 | sArg.pProgressData = nullptr; |
2464 | |
|
2465 | 0 | auto oAccumulator = psJob->poErrorAccumulator->InstallForCurrentScope(); |
2466 | 0 | CPL_IGNORE_RET_VAL(oAccumulator); |
2467 | |
|
2468 | 0 | if (psJob->poSource->DatasetRasterIO( |
2469 | 0 | psJob->eVRTBandDataType, psJob->nXOff, psJob->nYOff, |
2470 | 0 | psJob->nXSize, psJob->nYSize, psJob->pData, psJob->nBufXSize, |
2471 | 0 | psJob->nBufYSize, psJob->eBufType, psJob->nBandCount, |
2472 | 0 | psJob->panBandMap, psJob->nPixelSpace, psJob->nLineSpace, |
2473 | 0 | psJob->nBandSpace, &sArg) != CE_None) |
2474 | 0 | { |
2475 | 0 | *psJob->pbSuccess = false; |
2476 | 0 | } |
2477 | 0 | } |
2478 | |
|
2479 | 0 | ++(*psJob->pnCompletedJobs); |
2480 | 0 | } |
2481 | | |
2482 | | /************************************************************************/ |
2483 | | /* IRasterIO() */ |
2484 | | /************************************************************************/ |
2485 | | |
2486 | | CPLErr VRTDataset::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, |
2487 | | int nXSize, int nYSize, void *pData, int nBufXSize, |
2488 | | int nBufYSize, GDALDataType eBufType, |
2489 | | int nBandCount, BANDMAP_TYPE panBandMap, |
2490 | | GSpacing nPixelSpace, GSpacing nLineSpace, |
2491 | | GSpacing nBandSpace, |
2492 | | GDALRasterIOExtraArg *psExtraArg) |
2493 | 0 | { |
2494 | 0 | m_bMultiThreadedRasterIOLastUsed = false; |
2495 | |
|
2496 | 0 | if (nBands == 1 && nBandCount == 1) |
2497 | 0 | { |
2498 | 0 | VRTSourcedRasterBand *poBand = |
2499 | 0 | dynamic_cast<VRTSourcedRasterBand *>(papoBands[0]); |
2500 | 0 | if (poBand) |
2501 | 0 | { |
2502 | 0 | return poBand->IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, |
2503 | 0 | pData, nBufXSize, nBufYSize, eBufType, |
2504 | 0 | nPixelSpace, nLineSpace, psExtraArg); |
2505 | 0 | } |
2506 | 0 | } |
2507 | | |
2508 | 0 | bool bLocalCompatibleForDatasetIO = |
2509 | 0 | CPL_TO_BOOL(CheckCompatibleForDatasetIO()); |
2510 | 0 | if (bLocalCompatibleForDatasetIO && eRWFlag == GF_Read && |
2511 | 0 | (nBufXSize < nXSize || nBufYSize < nYSize) && m_apoOverviews.empty()) |
2512 | 0 | { |
2513 | 0 | int bTried = FALSE; |
2514 | 0 | const CPLErr eErr = TryOverviewRasterIO( |
2515 | 0 | eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, |
2516 | 0 | eBufType, nBandCount, panBandMap, nPixelSpace, nLineSpace, |
2517 | 0 | nBandSpace, psExtraArg, &bTried); |
2518 | |
|
2519 | 0 | if (bTried) |
2520 | 0 | { |
2521 | 0 | return eErr; |
2522 | 0 | } |
2523 | | |
2524 | 0 | for (int iBand = 0; iBand < nBands; iBand++) |
2525 | 0 | { |
2526 | 0 | VRTSourcedRasterBand *poBand = |
2527 | 0 | static_cast<VRTSourcedRasterBand *>(papoBands[iBand]); |
2528 | | |
2529 | | // If there are overviews, let VRTSourcedRasterBand::IRasterIO() |
2530 | | // do the job. |
2531 | 0 | if (poBand->GetOverviewCount() != 0) |
2532 | 0 | { |
2533 | 0 | bLocalCompatibleForDatasetIO = false; |
2534 | 0 | break; |
2535 | 0 | } |
2536 | 0 | } |
2537 | 0 | } |
2538 | | |
2539 | | // If resampling with non-nearest neighbour, we need to be careful |
2540 | | // if the VRT band exposes a nodata value, but the sources do not have it. |
2541 | | // To also avoid edge effects on sources when downsampling, use the |
2542 | | // base implementation of IRasterIO() (that is acquiring sources at their |
2543 | | // nominal resolution, and then downsampling), but only if none of the |
2544 | | // contributing sources have overviews. |
2545 | 0 | if (bLocalCompatibleForDatasetIO && eRWFlag == GF_Read && |
2546 | 0 | (nXSize != nBufXSize || nYSize != nBufYSize) && |
2547 | 0 | psExtraArg->eResampleAlg != GRIORA_NearestNeighbour) |
2548 | 0 | { |
2549 | 0 | for (int iBandIndex = 0; iBandIndex < nBandCount; iBandIndex++) |
2550 | 0 | { |
2551 | 0 | VRTSourcedRasterBand *poBand = static_cast<VRTSourcedRasterBand *>( |
2552 | 0 | GetRasterBand(panBandMap[iBandIndex])); |
2553 | 0 | if (!poBand->CanIRasterIOBeForwardedToEachSource( |
2554 | 0 | eRWFlag, nXOff, nYOff, nXSize, nYSize, nBufXSize, nBufYSize, |
2555 | 0 | psExtraArg)) |
2556 | 0 | { |
2557 | 0 | bLocalCompatibleForDatasetIO = false; |
2558 | 0 | break; |
2559 | 0 | } |
2560 | 0 | } |
2561 | 0 | } |
2562 | |
|
2563 | 0 | if (bLocalCompatibleForDatasetIO && eRWFlag == GF_Read) |
2564 | 0 | { |
2565 | 0 | for (int iBandIndex = 0; iBandIndex < nBandCount; iBandIndex++) |
2566 | 0 | { |
2567 | 0 | VRTSourcedRasterBand *poBand = static_cast<VRTSourcedRasterBand *>( |
2568 | 0 | GetRasterBand(panBandMap[iBandIndex])); |
2569 | | |
2570 | | /* Dirty little trick to initialize the buffer without doing */ |
2571 | | /* any real I/O */ |
2572 | 0 | std::vector<std::unique_ptr<VRTSource>> papoSavedSources; |
2573 | 0 | std::swap(papoSavedSources, poBand->m_papoSources); |
2574 | |
|
2575 | 0 | GDALProgressFunc pfnProgressGlobal = psExtraArg->pfnProgress; |
2576 | 0 | psExtraArg->pfnProgress = nullptr; |
2577 | |
|
2578 | 0 | GByte *pabyBandData = |
2579 | 0 | static_cast<GByte *>(pData) + iBandIndex * nBandSpace; |
2580 | |
|
2581 | 0 | poBand->IRasterIO(GF_Read, nXOff, nYOff, nXSize, nYSize, |
2582 | 0 | pabyBandData, nBufXSize, nBufYSize, eBufType, |
2583 | 0 | nPixelSpace, nLineSpace, psExtraArg); |
2584 | |
|
2585 | 0 | psExtraArg->pfnProgress = pfnProgressGlobal; |
2586 | |
|
2587 | 0 | std::swap(papoSavedSources, poBand->m_papoSources); |
2588 | 0 | } |
2589 | |
|
2590 | 0 | CPLErr eErr = CE_None; |
2591 | | |
2592 | | // Use the last band, because when sources reference a GDALProxyDataset, |
2593 | | // they don't necessary instantiate all underlying rasterbands. |
2594 | 0 | VRTSourcedRasterBand *poBand = |
2595 | 0 | static_cast<VRTSourcedRasterBand *>(papoBands[nBands - 1]); |
2596 | |
|
2597 | 0 | double dfXOff = nXOff; |
2598 | 0 | double dfYOff = nYOff; |
2599 | 0 | double dfXSize = nXSize; |
2600 | 0 | double dfYSize = nYSize; |
2601 | 0 | if (psExtraArg->bFloatingPointWindowValidity) |
2602 | 0 | { |
2603 | 0 | dfXOff = psExtraArg->dfXOff; |
2604 | 0 | dfYOff = psExtraArg->dfYOff; |
2605 | 0 | dfXSize = psExtraArg->dfXSize; |
2606 | 0 | dfYSize = psExtraArg->dfYSize; |
2607 | 0 | } |
2608 | |
|
2609 | 0 | int nContributingSources = 0; |
2610 | 0 | int nMaxThreads = 0; |
2611 | 0 | constexpr int MINIMUM_PIXEL_COUNT_FOR_THREADED_IO = 1000 * 1000; |
2612 | 0 | if ((static_cast<int64_t>(nBufXSize) * nBufYSize >= |
2613 | 0 | MINIMUM_PIXEL_COUNT_FOR_THREADED_IO || |
2614 | 0 | static_cast<int64_t>(nXSize) * nYSize >= |
2615 | 0 | MINIMUM_PIXEL_COUNT_FOR_THREADED_IO) && |
2616 | 0 | poBand->CanMultiThreadRasterIO(dfXOff, dfYOff, dfXSize, dfYSize, |
2617 | 0 | nContributingSources) && |
2618 | 0 | nContributingSources > 1 && |
2619 | 0 | (nMaxThreads = VRTDataset::GetNumThreads(this)) > 1) |
2620 | 0 | { |
2621 | 0 | m_bMultiThreadedRasterIOLastUsed = true; |
2622 | 0 | m_oMapSharedSources.InitMutex(); |
2623 | |
|
2624 | 0 | CPLErrorAccumulator errorAccumulator; |
2625 | 0 | std::atomic<bool> bSuccess = true; |
2626 | 0 | CPLWorkerThreadPool *psThreadPool = GDALGetGlobalThreadPool( |
2627 | 0 | std::min(nContributingSources, nMaxThreads)); |
2628 | |
|
2629 | 0 | CPLDebugOnly( |
2630 | 0 | "VRT", |
2631 | 0 | "IRasterIO(): use optimized " |
2632 | 0 | "multi-threaded code path for mosaic. " |
2633 | 0 | "Using %d threads", |
2634 | 0 | std::min(nContributingSources, psThreadPool->GetThreadCount())); |
2635 | |
|
2636 | 0 | auto oQueue = psThreadPool->CreateJobQueue(); |
2637 | 0 | std::atomic<int> nCompletedJobs = 0; |
2638 | 0 | for (auto &poSource : poBand->m_papoSources) |
2639 | 0 | { |
2640 | 0 | if (!poSource->IsSimpleSource()) |
2641 | 0 | continue; |
2642 | 0 | auto poSimpleSource = |
2643 | 0 | cpl::down_cast<VRTSimpleSource *>(poSource.get()); |
2644 | 0 | if (poSimpleSource->DstWindowIntersects(dfXOff, dfYOff, dfXSize, |
2645 | 0 | dfYSize)) |
2646 | 0 | { |
2647 | 0 | auto psJob = new VRTDatasetRasterIOJob(); |
2648 | 0 | psJob->pbSuccess = &bSuccess; |
2649 | 0 | psJob->poErrorAccumulator = &errorAccumulator; |
2650 | 0 | psJob->pnCompletedJobs = &nCompletedJobs; |
2651 | 0 | psJob->eVRTBandDataType = poBand->GetRasterDataType(); |
2652 | 0 | psJob->nXOff = nXOff; |
2653 | 0 | psJob->nYOff = nYOff; |
2654 | 0 | psJob->nXSize = nXSize; |
2655 | 0 | psJob->nYSize = nYSize; |
2656 | 0 | psJob->pData = pData; |
2657 | 0 | psJob->nBufXSize = nBufXSize; |
2658 | 0 | psJob->nBufYSize = nBufYSize; |
2659 | 0 | psJob->eBufType = eBufType; |
2660 | 0 | psJob->nBandCount = nBandCount; |
2661 | 0 | psJob->panBandMap = panBandMap; |
2662 | 0 | psJob->nPixelSpace = nPixelSpace; |
2663 | 0 | psJob->nLineSpace = nLineSpace; |
2664 | 0 | psJob->nBandSpace = nBandSpace; |
2665 | 0 | psJob->psExtraArg = psExtraArg; |
2666 | 0 | psJob->poSource = poSimpleSource; |
2667 | |
|
2668 | 0 | if (!oQueue->SubmitJob(VRTDatasetRasterIOJob::Func, psJob)) |
2669 | 0 | { |
2670 | 0 | delete psJob; |
2671 | 0 | bSuccess = false; |
2672 | 0 | break; |
2673 | 0 | } |
2674 | 0 | } |
2675 | 0 | } |
2676 | |
|
2677 | 0 | while (oQueue->WaitEvent()) |
2678 | 0 | { |
2679 | | // Quite rough progress callback. We could do better by counting |
2680 | | // the number of contributing pixels. |
2681 | 0 | if (psExtraArg->pfnProgress) |
2682 | 0 | { |
2683 | 0 | psExtraArg->pfnProgress(double(nCompletedJobs.load()) / |
2684 | 0 | nContributingSources, |
2685 | 0 | "", psExtraArg->pProgressData); |
2686 | 0 | } |
2687 | 0 | } |
2688 | |
|
2689 | 0 | errorAccumulator.ReplayErrors(); |
2690 | 0 | eErr = bSuccess ? CE_None : CE_Failure; |
2691 | 0 | } |
2692 | 0 | else |
2693 | 0 | { |
2694 | 0 | GDALProgressFunc pfnProgressGlobal = psExtraArg->pfnProgress; |
2695 | 0 | void *pProgressDataGlobal = psExtraArg->pProgressData; |
2696 | |
|
2697 | 0 | const int nSources = static_cast<int>(poBand->m_papoSources.size()); |
2698 | 0 | for (int iSource = 0; eErr == CE_None && iSource < nSources; |
2699 | 0 | iSource++) |
2700 | 0 | { |
2701 | 0 | psExtraArg->pfnProgress = GDALScaledProgress; |
2702 | 0 | psExtraArg->pProgressData = GDALCreateScaledProgress( |
2703 | 0 | 1.0 * iSource / nSources, 1.0 * (iSource + 1) / nSources, |
2704 | 0 | pfnProgressGlobal, pProgressDataGlobal); |
2705 | |
|
2706 | 0 | VRTSimpleSource *poSource = static_cast<VRTSimpleSource *>( |
2707 | 0 | poBand->m_papoSources[iSource].get()); |
2708 | |
|
2709 | 0 | eErr = poSource->DatasetRasterIO( |
2710 | 0 | poBand->GetRasterDataType(), nXOff, nYOff, nXSize, nYSize, |
2711 | 0 | pData, nBufXSize, nBufYSize, eBufType, nBandCount, |
2712 | 0 | panBandMap, nPixelSpace, nLineSpace, nBandSpace, |
2713 | 0 | psExtraArg); |
2714 | |
|
2715 | 0 | GDALDestroyScaledProgress(psExtraArg->pProgressData); |
2716 | 0 | } |
2717 | |
|
2718 | 0 | psExtraArg->pfnProgress = pfnProgressGlobal; |
2719 | 0 | psExtraArg->pProgressData = pProgressDataGlobal; |
2720 | 0 | } |
2721 | |
|
2722 | 0 | if (eErr == CE_None && psExtraArg->pfnProgress) |
2723 | 0 | { |
2724 | 0 | psExtraArg->pfnProgress(1.0, "", psExtraArg->pProgressData); |
2725 | 0 | } |
2726 | |
|
2727 | 0 | return eErr; |
2728 | 0 | } |
2729 | | |
2730 | 0 | CPLErr eErr; |
2731 | 0 | if (eRWFlag == GF_Read && |
2732 | 0 | psExtraArg->eResampleAlg != GRIORA_NearestNeighbour && |
2733 | 0 | nBufXSize < nXSize && nBufYSize < nYSize && nBandCount > 1) |
2734 | 0 | { |
2735 | | // Force going through VRTSourcedRasterBand::IRasterIO(), otherwise |
2736 | | // GDALDataset::IRasterIOResampled() would be used without source |
2737 | | // overviews being potentially used. |
2738 | 0 | eErr = GDALDataset::BandBasedRasterIO( |
2739 | 0 | eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, |
2740 | 0 | eBufType, nBandCount, panBandMap, nPixelSpace, nLineSpace, |
2741 | 0 | nBandSpace, psExtraArg); |
2742 | 0 | } |
2743 | 0 | else |
2744 | 0 | { |
2745 | 0 | eErr = GDALDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, |
2746 | 0 | pData, nBufXSize, nBufYSize, eBufType, |
2747 | 0 | nBandCount, panBandMap, nPixelSpace, |
2748 | 0 | nLineSpace, nBandSpace, psExtraArg); |
2749 | 0 | } |
2750 | 0 | return eErr; |
2751 | 0 | } |
2752 | | |
2753 | | /************************************************************************/ |
2754 | | /* UnsetPreservedRelativeFilenames() */ |
2755 | | /************************************************************************/ |
2756 | | |
2757 | | void VRTDataset::UnsetPreservedRelativeFilenames() |
2758 | 0 | { |
2759 | 0 | for (int iBand = 0; iBand < nBands; iBand++) |
2760 | 0 | { |
2761 | 0 | if (!static_cast<VRTRasterBand *>(papoBands[iBand]) |
2762 | 0 | ->IsSourcedRasterBand()) |
2763 | 0 | continue; |
2764 | | |
2765 | 0 | VRTSourcedRasterBand *poBand = |
2766 | 0 | static_cast<VRTSourcedRasterBand *>(papoBands[iBand]); |
2767 | 0 | for (auto &poSource : poBand->m_papoSources) |
2768 | 0 | { |
2769 | 0 | if (!poSource->IsSimpleSource()) |
2770 | 0 | continue; |
2771 | | |
2772 | 0 | VRTSimpleSource *poSimpleSource = |
2773 | 0 | static_cast<VRTSimpleSource *>(poSource.get()); |
2774 | 0 | poSimpleSource->UnsetPreservedRelativeFilenames(); |
2775 | 0 | } |
2776 | 0 | } |
2777 | 0 | } |
2778 | | |
2779 | | /************************************************************************/ |
2780 | | /* BuildVirtualOverviews() */ |
2781 | | /************************************************************************/ |
2782 | | |
2783 | | static bool CheckBandForOverview(GDALRasterBand *poBand, |
2784 | | GDALRasterBand *&poFirstBand, int &nOverviews, |
2785 | | std::set<std::pair<int, int>> &oSetOvrSizes, |
2786 | | std::vector<GDALDataset *> &apoOverviewsBak) |
2787 | 0 | { |
2788 | 0 | if (!cpl::down_cast<VRTRasterBand *>(poBand)->IsSourcedRasterBand()) |
2789 | 0 | return false; |
2790 | | |
2791 | 0 | VRTSourcedRasterBand *poVRTBand = |
2792 | 0 | cpl::down_cast<VRTSourcedRasterBand *>(poBand); |
2793 | 0 | if (poVRTBand->m_papoSources.size() != 1) |
2794 | 0 | return false; |
2795 | 0 | if (!poVRTBand->m_papoSources[0]->IsSimpleSource()) |
2796 | 0 | return false; |
2797 | | |
2798 | 0 | VRTSimpleSource *poSource = |
2799 | 0 | cpl::down_cast<VRTSimpleSource *>(poVRTBand->m_papoSources[0].get()); |
2800 | 0 | const char *pszType = poSource->GetType(); |
2801 | 0 | if (pszType != VRTSimpleSource::GetTypeStatic() && |
2802 | 0 | pszType != VRTComplexSource::GetTypeStatic()) |
2803 | 0 | { |
2804 | 0 | return false; |
2805 | 0 | } |
2806 | 0 | GDALRasterBand *poSrcBand = poBand->GetBand() == 0 |
2807 | 0 | ? poSource->GetMaskBandMainBand() |
2808 | 0 | : poSource->GetRasterBand(); |
2809 | 0 | if (poSrcBand == nullptr) |
2810 | 0 | return false; |
2811 | | |
2812 | | // To prevent recursion |
2813 | 0 | apoOverviewsBak.push_back(nullptr); |
2814 | 0 | const int nOvrCount = poSrcBand->GetOverviewCount(); |
2815 | 0 | oSetOvrSizes.insert( |
2816 | 0 | std::pair<int, int>(poSrcBand->GetXSize(), poSrcBand->GetYSize())); |
2817 | 0 | for (int i = 0; i < nOvrCount; ++i) |
2818 | 0 | { |
2819 | 0 | auto poSrcOvrBand = poSrcBand->GetOverview(i); |
2820 | 0 | if (poSrcOvrBand) |
2821 | 0 | { |
2822 | 0 | oSetOvrSizes.insert(std::pair<int, int>(poSrcOvrBand->GetXSize(), |
2823 | 0 | poSrcOvrBand->GetYSize())); |
2824 | 0 | } |
2825 | 0 | } |
2826 | 0 | apoOverviewsBak.resize(0); |
2827 | |
|
2828 | 0 | if (nOvrCount == 0) |
2829 | 0 | return false; |
2830 | 0 | if (poFirstBand == nullptr) |
2831 | 0 | { |
2832 | 0 | if (poSrcBand->GetXSize() == 0 || poSrcBand->GetYSize() == 0) |
2833 | 0 | return false; |
2834 | 0 | poFirstBand = poSrcBand; |
2835 | 0 | nOverviews = nOvrCount; |
2836 | 0 | } |
2837 | 0 | else if (nOvrCount < nOverviews) |
2838 | 0 | nOverviews = nOvrCount; |
2839 | 0 | return true; |
2840 | 0 | } |
2841 | | |
2842 | | void VRTDataset::BuildVirtualOverviews() |
2843 | 0 | { |
2844 | | // Currently we expose virtual overviews only if the dataset is made of |
2845 | | // a single SimpleSource/ComplexSource, in each band. |
2846 | | // And if the underlying sources have overviews of course |
2847 | 0 | if (!m_apoOverviews.empty() || !m_apoOverviewsBak.empty()) |
2848 | 0 | return; |
2849 | | |
2850 | 0 | int nOverviews = 0; |
2851 | 0 | GDALRasterBand *poFirstBand = nullptr; |
2852 | 0 | std::set<std::pair<int, int>> oSetOvrSizes; |
2853 | |
|
2854 | 0 | for (int iBand = 0; iBand < nBands; iBand++) |
2855 | 0 | { |
2856 | 0 | if (!CheckBandForOverview(papoBands[iBand], poFirstBand, nOverviews, |
2857 | 0 | oSetOvrSizes, m_apoOverviewsBak)) |
2858 | 0 | return; |
2859 | 0 | } |
2860 | | |
2861 | 0 | if (m_poMaskBand) |
2862 | 0 | { |
2863 | 0 | if (!CheckBandForOverview(m_poMaskBand.get(), poFirstBand, nOverviews, |
2864 | 0 | oSetOvrSizes, m_apoOverviewsBak)) |
2865 | 0 | return; |
2866 | 0 | } |
2867 | 0 | if (poFirstBand == nullptr) |
2868 | 0 | { |
2869 | | // to make cppcheck happy |
2870 | 0 | CPLAssert(false); |
2871 | 0 | return; |
2872 | 0 | } |
2873 | | |
2874 | 0 | VRTSourcedRasterBand *l_poVRTBand = |
2875 | 0 | cpl::down_cast<VRTSourcedRasterBand *>(papoBands[0]); |
2876 | 0 | VRTSimpleSource *poSource = |
2877 | 0 | cpl::down_cast<VRTSimpleSource *>(l_poVRTBand->m_papoSources[0].get()); |
2878 | 0 | const double dfDstToSrcXRatio = |
2879 | 0 | poSource->m_dfDstXSize / poSource->m_dfSrcXSize; |
2880 | 0 | const double dfDstToSrcYRatio = |
2881 | 0 | poSource->m_dfDstYSize / poSource->m_dfSrcYSize; |
2882 | |
|
2883 | 0 | for (int j = 0; j < nOverviews; j++) |
2884 | 0 | { |
2885 | 0 | auto poOvrBand = poFirstBand->GetOverview(j); |
2886 | 0 | if (!poOvrBand) |
2887 | 0 | return; |
2888 | 0 | const double dfXRatio = static_cast<double>(poOvrBand->GetXSize()) / |
2889 | 0 | poFirstBand->GetXSize(); |
2890 | 0 | const double dfYRatio = static_cast<double>(poOvrBand->GetYSize()) / |
2891 | 0 | poFirstBand->GetYSize(); |
2892 | 0 | if (dfXRatio >= dfDstToSrcXRatio || dfYRatio >= dfDstToSrcYRatio) |
2893 | 0 | { |
2894 | 0 | continue; |
2895 | 0 | } |
2896 | 0 | int nOvrXSize = static_cast<int>(0.5 + nRasterXSize * dfXRatio); |
2897 | 0 | int nOvrYSize = static_cast<int>(0.5 + nRasterYSize * dfYRatio); |
2898 | | |
2899 | | // Look for a source overview whose size is very close to the |
2900 | | // theoretical computed one. |
2901 | 0 | bool bSrcOvrMatchFound = false; |
2902 | 0 | for (const auto &ovrSize : oSetOvrSizes) |
2903 | 0 | { |
2904 | 0 | if (std::abs(ovrSize.first - nOvrXSize) <= 1 && |
2905 | 0 | std::abs(ovrSize.second - nOvrYSize) <= 1) |
2906 | 0 | { |
2907 | 0 | bSrcOvrMatchFound = true; |
2908 | 0 | nOvrXSize = ovrSize.first; |
2909 | 0 | nOvrYSize = ovrSize.second; |
2910 | 0 | break; |
2911 | 0 | } |
2912 | 0 | } |
2913 | |
|
2914 | 0 | if (!bSrcOvrMatchFound && |
2915 | 0 | (nOvrXSize < DEFAULT_BLOCK_SIZE || nOvrYSize < DEFAULT_BLOCK_SIZE)) |
2916 | 0 | { |
2917 | 0 | break; |
2918 | 0 | } |
2919 | | |
2920 | 0 | int nBlockXSize = 0; |
2921 | 0 | int nBlockYSize = 0; |
2922 | 0 | l_poVRTBand->GetBlockSize(&nBlockXSize, &nBlockYSize); |
2923 | 0 | if (VRTDataset::IsDefaultBlockSize(nBlockXSize, nRasterXSize)) |
2924 | 0 | nBlockXSize = 0; |
2925 | 0 | if (VRTDataset::IsDefaultBlockSize(nBlockYSize, nRasterYSize)) |
2926 | 0 | nBlockYSize = 0; |
2927 | |
|
2928 | 0 | VRTDataset *poOvrVDS = |
2929 | 0 | new VRTDataset(nOvrXSize, nOvrYSize, nBlockXSize, nBlockYSize); |
2930 | 0 | m_apoOverviews.push_back(poOvrVDS); |
2931 | |
|
2932 | 0 | const auto CreateOverviewBand = |
2933 | 0 | [&poOvrVDS, nOvrXSize, nOvrYSize, dfXRatio, |
2934 | 0 | dfYRatio](VRTSourcedRasterBand *poVRTBand) |
2935 | 0 | { |
2936 | 0 | auto poOvrVRTBand = std::make_unique<VRTSourcedRasterBand>( |
2937 | 0 | poOvrVDS, poVRTBand->GetBand(), poVRTBand->GetRasterDataType(), |
2938 | 0 | nOvrXSize, nOvrYSize); |
2939 | 0 | poOvrVRTBand->CopyCommonInfoFrom(poVRTBand); |
2940 | 0 | poOvrVRTBand->m_bNoDataValueSet = poVRTBand->m_bNoDataValueSet; |
2941 | 0 | poOvrVRTBand->m_dfNoDataValue = poVRTBand->m_dfNoDataValue; |
2942 | 0 | poOvrVRTBand->m_bHideNoDataValue = poVRTBand->m_bHideNoDataValue; |
2943 | |
|
2944 | 0 | VRTSimpleSource *poSrcSource = cpl::down_cast<VRTSimpleSource *>( |
2945 | 0 | poVRTBand->m_papoSources[0].get()); |
2946 | 0 | std::unique_ptr<VRTSimpleSource> poNewSource; |
2947 | 0 | const char *pszType = poSrcSource->GetType(); |
2948 | 0 | if (pszType == VRTSimpleSource::GetTypeStatic()) |
2949 | 0 | { |
2950 | 0 | poNewSource = std::make_unique<VRTSimpleSource>( |
2951 | 0 | poSrcSource, dfXRatio, dfYRatio); |
2952 | 0 | } |
2953 | 0 | else if (pszType == VRTComplexSource::GetTypeStatic()) |
2954 | 0 | { |
2955 | 0 | poNewSource = std::make_unique<VRTComplexSource>( |
2956 | 0 | cpl::down_cast<VRTComplexSource *>(poSrcSource), dfXRatio, |
2957 | 0 | dfYRatio); |
2958 | 0 | } |
2959 | 0 | else |
2960 | 0 | { |
2961 | 0 | CPLAssert(false); |
2962 | 0 | } |
2963 | 0 | if (poNewSource) |
2964 | 0 | { |
2965 | 0 | auto poNewSourceBand = poVRTBand->GetBand() == 0 |
2966 | 0 | ? poNewSource->GetMaskBandMainBand() |
2967 | 0 | : poNewSource->GetRasterBand(); |
2968 | 0 | CPLAssert(poNewSourceBand); |
2969 | 0 | auto poNewSourceBandDS = poNewSourceBand->GetDataset(); |
2970 | 0 | if (poNewSourceBandDS) |
2971 | 0 | poNewSourceBandDS->Reference(); |
2972 | 0 | poOvrVRTBand->AddSource(std::move(poNewSource)); |
2973 | 0 | } |
2974 | | |
2975 | 0 | return poOvrVRTBand; |
2976 | 0 | }; |
2977 | |
|
2978 | 0 | for (int i = 0; i < nBands; i++) |
2979 | 0 | { |
2980 | 0 | VRTSourcedRasterBand *poSrcBand = |
2981 | 0 | cpl::down_cast<VRTSourcedRasterBand *>(GetRasterBand(i + 1)); |
2982 | 0 | poOvrVDS->SetBand(poOvrVDS->GetRasterCount() + 1, |
2983 | 0 | CreateOverviewBand(poSrcBand)); |
2984 | 0 | } |
2985 | |
|
2986 | 0 | if (m_poMaskBand) |
2987 | 0 | { |
2988 | 0 | VRTSourcedRasterBand *poSrcBand = |
2989 | 0 | cpl::down_cast<VRTSourcedRasterBand *>(m_poMaskBand.get()); |
2990 | 0 | poOvrVDS->SetMaskBand(CreateOverviewBand(poSrcBand)); |
2991 | 0 | } |
2992 | 0 | } |
2993 | 0 | } |
2994 | | |
2995 | | /************************************************************************/ |
2996 | | /* AddVirtualOverview() */ |
2997 | | /************************************************************************/ |
2998 | | |
2999 | | bool VRTDataset::AddVirtualOverview(int nOvFactor, const char *pszResampling) |
3000 | 0 | { |
3001 | 0 | if (nRasterXSize / nOvFactor == 0 || nRasterYSize / nOvFactor == 0) |
3002 | 0 | { |
3003 | 0 | return false; |
3004 | 0 | } |
3005 | | |
3006 | 0 | CPLStringList argv; |
3007 | 0 | argv.AddString("-of"); |
3008 | 0 | argv.AddString("VRT"); |
3009 | 0 | argv.AddString("-outsize"); |
3010 | 0 | argv.AddString(CPLSPrintf("%d", nRasterXSize / nOvFactor)); |
3011 | 0 | argv.AddString(CPLSPrintf("%d", nRasterYSize / nOvFactor)); |
3012 | 0 | argv.AddString("-r"); |
3013 | 0 | argv.AddString(pszResampling); |
3014 | |
|
3015 | 0 | int nBlockXSize = 0; |
3016 | 0 | int nBlockYSize = 0; |
3017 | 0 | GetRasterBand(1)->GetBlockSize(&nBlockXSize, &nBlockYSize); |
3018 | 0 | if (!VRTDataset::IsDefaultBlockSize(nBlockXSize, nRasterXSize)) |
3019 | 0 | { |
3020 | 0 | argv.AddString("-co"); |
3021 | 0 | argv.AddString(CPLSPrintf("BLOCKXSIZE=%d", nBlockXSize)); |
3022 | 0 | } |
3023 | 0 | if (!VRTDataset::IsDefaultBlockSize(nBlockYSize, nRasterYSize)) |
3024 | 0 | { |
3025 | 0 | argv.AddString("-co"); |
3026 | 0 | argv.AddString(CPLSPrintf("BLOCKYSIZE=%d", nBlockYSize)); |
3027 | 0 | } |
3028 | |
|
3029 | 0 | GDALTranslateOptions *psOptions = |
3030 | 0 | GDALTranslateOptionsNew(argv.List(), nullptr); |
3031 | | |
3032 | | // Add a dummy overview so that BuildVirtualOverviews() doesn't trigger |
3033 | 0 | m_apoOverviews.push_back(nullptr); |
3034 | 0 | CPLAssert(m_bCanTakeRef); |
3035 | 0 | m_bCanTakeRef = |
3036 | 0 | false; // we don't want hOverviewDS to take a reference on ourselves. |
3037 | 0 | GDALDatasetH hOverviewDS = |
3038 | 0 | GDALTranslate("", GDALDataset::ToHandle(this), psOptions, nullptr); |
3039 | 0 | m_bCanTakeRef = true; |
3040 | 0 | m_apoOverviews.pop_back(); |
3041 | |
|
3042 | 0 | GDALTranslateOptionsFree(psOptions); |
3043 | 0 | if (hOverviewDS == nullptr) |
3044 | 0 | return false; |
3045 | | |
3046 | 0 | m_anOverviewFactors.push_back(nOvFactor); |
3047 | 0 | m_apoOverviews.push_back(GDALDataset::FromHandle(hOverviewDS)); |
3048 | 0 | return true; |
3049 | 0 | } |
3050 | | |
3051 | | /************************************************************************/ |
3052 | | /* IBuildOverviews() */ |
3053 | | /************************************************************************/ |
3054 | | |
3055 | | CPLErr VRTDataset::IBuildOverviews(const char *pszResampling, int nOverviews, |
3056 | | const int *panOverviewList, int nListBands, |
3057 | | const int *panBandList, |
3058 | | GDALProgressFunc pfnProgress, |
3059 | | void *pProgressData, |
3060 | | CSLConstList papszOptions) |
3061 | 0 | { |
3062 | 0 | if (CPLTestBool(CPLGetConfigOption("VRT_VIRTUAL_OVERVIEWS", "NO"))) |
3063 | 0 | { |
3064 | 0 | SetNeedsFlush(); |
3065 | 0 | if (nOverviews == 0 || |
3066 | 0 | (!m_apoOverviews.empty() && m_anOverviewFactors.empty())) |
3067 | 0 | { |
3068 | 0 | m_anOverviewFactors.clear(); |
3069 | 0 | m_apoOverviewsBak.insert(m_apoOverviewsBak.end(), |
3070 | 0 | m_apoOverviews.begin(), |
3071 | 0 | m_apoOverviews.end()); |
3072 | 0 | m_apoOverviews.clear(); |
3073 | 0 | } |
3074 | 0 | m_osOverviewResampling = pszResampling; |
3075 | 0 | for (int i = 0; i < nOverviews; i++) |
3076 | 0 | { |
3077 | 0 | if (std::find(m_anOverviewFactors.begin(), |
3078 | 0 | m_anOverviewFactors.end(), |
3079 | 0 | panOverviewList[i]) == m_anOverviewFactors.end()) |
3080 | 0 | { |
3081 | 0 | AddVirtualOverview(panOverviewList[i], pszResampling); |
3082 | 0 | } |
3083 | 0 | } |
3084 | 0 | return CE_None; |
3085 | 0 | } |
3086 | | |
3087 | 0 | if (!oOvManager.IsInitialized()) |
3088 | 0 | { |
3089 | 0 | const char *pszDesc = GetDescription(); |
3090 | 0 | if (pszDesc[0]) |
3091 | 0 | { |
3092 | 0 | oOvManager.Initialize(this, pszDesc); |
3093 | 0 | } |
3094 | 0 | } |
3095 | | |
3096 | | // Make implicit overviews invisible, but do not destroy them in case they |
3097 | | // are already used. Should the client do that? Behavior might undefined |
3098 | | // in GDAL API? |
3099 | 0 | if (!m_apoOverviews.empty()) |
3100 | 0 | { |
3101 | 0 | m_apoOverviewsBak.insert(m_apoOverviewsBak.end(), |
3102 | 0 | m_apoOverviews.begin(), m_apoOverviews.end()); |
3103 | 0 | m_apoOverviews.clear(); |
3104 | 0 | } |
3105 | 0 | else |
3106 | 0 | { |
3107 | | // Add a dummy overview so that GDALDataset::IBuildOverviews() |
3108 | | // doesn't manage to get a virtual implicit overview. |
3109 | 0 | m_apoOverviews.push_back(nullptr); |
3110 | 0 | } |
3111 | |
|
3112 | 0 | CPLErr eErr = GDALDataset::IBuildOverviews( |
3113 | 0 | pszResampling, nOverviews, panOverviewList, nListBands, panBandList, |
3114 | 0 | pfnProgress, pProgressData, papszOptions); |
3115 | |
|
3116 | 0 | m_apoOverviews.clear(); |
3117 | 0 | return eErr; |
3118 | 0 | } |
3119 | | |
3120 | | /************************************************************************/ |
3121 | | /* GetShiftedDataset() */ |
3122 | | /* */ |
3123 | | /* Returns true if the VRT is made of a single source that is a simple */ |
3124 | | /* in its full resolution. */ |
3125 | | /************************************************************************/ |
3126 | | |
3127 | | bool VRTDataset::GetShiftedDataset(int nXOff, int nYOff, int nXSize, int nYSize, |
3128 | | GDALDataset *&poSrcDataset, int &nSrcXOff, |
3129 | | int &nSrcYOff) |
3130 | 0 | { |
3131 | 0 | if (!CheckCompatibleForDatasetIO()) |
3132 | 0 | return false; |
3133 | | |
3134 | 0 | VRTSourcedRasterBand *poVRTBand = |
3135 | 0 | static_cast<VRTSourcedRasterBand *>(papoBands[0]); |
3136 | 0 | if (poVRTBand->m_papoSources.size() != 1) |
3137 | 0 | return false; |
3138 | | |
3139 | 0 | VRTSimpleSource *poSource = |
3140 | 0 | static_cast<VRTSimpleSource *>(poVRTBand->m_papoSources[0].get()); |
3141 | |
|
3142 | 0 | GDALRasterBand *poBand = poSource->GetRasterBand(); |
3143 | 0 | if (!poBand || poSource->GetMaskBandMainBand() || |
3144 | 0 | poBand->GetRasterDataType() != poVRTBand->GetRasterDataType()) |
3145 | 0 | return false; |
3146 | | |
3147 | 0 | poSrcDataset = poBand->GetDataset(); |
3148 | 0 | if (!poSrcDataset) |
3149 | 0 | return false; |
3150 | | |
3151 | 0 | double dfReqXOff = 0.0; |
3152 | 0 | double dfReqYOff = 0.0; |
3153 | 0 | double dfReqXSize = 0.0; |
3154 | 0 | double dfReqYSize = 0.0; |
3155 | 0 | int nReqXOff = 0; |
3156 | 0 | int nReqYOff = 0; |
3157 | 0 | int nReqXSize = 0; |
3158 | 0 | int nReqYSize = 0; |
3159 | 0 | int nOutXOff = 0; |
3160 | 0 | int nOutYOff = 0; |
3161 | 0 | int nOutXSize = 0; |
3162 | 0 | int nOutYSize = 0; |
3163 | 0 | bool bError = false; |
3164 | 0 | if (!poSource->GetSrcDstWindow(nXOff, nYOff, nXSize, nYSize, nXSize, nYSize, |
3165 | 0 | &dfReqXOff, &dfReqYOff, &dfReqXSize, |
3166 | 0 | &dfReqYSize, &nReqXOff, &nReqYOff, |
3167 | 0 | &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff, |
3168 | 0 | &nOutXSize, &nOutYSize, bError)) |
3169 | 0 | return false; |
3170 | | |
3171 | 0 | if (nReqXSize != nXSize || nReqYSize != nYSize || nReqXSize != nOutXSize || |
3172 | 0 | nReqYSize != nOutYSize) |
3173 | 0 | return false; |
3174 | | |
3175 | 0 | nSrcXOff = nReqXOff; |
3176 | 0 | nSrcYOff = nReqYOff; |
3177 | 0 | return true; |
3178 | 0 | } |
3179 | | |
3180 | | /************************************************************************/ |
3181 | | /* GetCompressionFormats() */ |
3182 | | /************************************************************************/ |
3183 | | |
3184 | | CPLStringList VRTDataset::GetCompressionFormats(int nXOff, int nYOff, |
3185 | | int nXSize, int nYSize, |
3186 | | int nBandCount, |
3187 | | const int *panBandList) |
3188 | 0 | { |
3189 | 0 | GDALDataset *poSrcDataset; |
3190 | 0 | int nSrcXOff; |
3191 | 0 | int nSrcYOff; |
3192 | 0 | if (!GetShiftedDataset(nXOff, nYOff, nXSize, nYSize, poSrcDataset, nSrcXOff, |
3193 | 0 | nSrcYOff)) |
3194 | 0 | return CPLStringList(); |
3195 | 0 | return poSrcDataset->GetCompressionFormats(nSrcXOff, nSrcYOff, nXSize, |
3196 | 0 | nYSize, nBandCount, panBandList); |
3197 | 0 | } |
3198 | | |
3199 | | /************************************************************************/ |
3200 | | /* ReadCompressedData() */ |
3201 | | /************************************************************************/ |
3202 | | |
3203 | | CPLErr VRTDataset::ReadCompressedData(const char *pszFormat, int nXOff, |
3204 | | int nYOff, int nXSize, int nYSize, |
3205 | | int nBandCount, const int *panBandList, |
3206 | | void **ppBuffer, size_t *pnBufferSize, |
3207 | | char **ppszDetailedFormat) |
3208 | 0 | { |
3209 | 0 | GDALDataset *poSrcDataset; |
3210 | 0 | int nSrcXOff; |
3211 | 0 | int nSrcYOff; |
3212 | 0 | if (!GetShiftedDataset(nXOff, nYOff, nXSize, nYSize, poSrcDataset, nSrcXOff, |
3213 | 0 | nSrcYOff)) |
3214 | 0 | return CE_Failure; |
3215 | 0 | return poSrcDataset->ReadCompressedData( |
3216 | 0 | pszFormat, nSrcXOff, nSrcYOff, nXSize, nYSize, nBandCount, panBandList, |
3217 | 0 | ppBuffer, pnBufferSize, ppszDetailedFormat); |
3218 | 0 | } |
3219 | | |
3220 | | /************************************************************************/ |
3221 | | /* ClearStatistics() */ |
3222 | | /************************************************************************/ |
3223 | | |
3224 | | void VRTDataset::ClearStatistics() |
3225 | 0 | { |
3226 | 0 | for (int i = 1; i <= nBands; ++i) |
3227 | 0 | { |
3228 | 0 | bool bChanged = false; |
3229 | 0 | GDALRasterBand *poBand = GetRasterBand(i); |
3230 | 0 | CSLConstList papszOldMD = poBand->GetMetadata(); |
3231 | 0 | CPLStringList aosNewMD; |
3232 | 0 | for (const char *pszMDItem : cpl::Iterate(papszOldMD)) |
3233 | 0 | { |
3234 | 0 | if (STARTS_WITH_CI(pszMDItem, "STATISTICS_")) |
3235 | 0 | { |
3236 | 0 | bChanged = true; |
3237 | 0 | } |
3238 | 0 | else |
3239 | 0 | { |
3240 | 0 | aosNewMD.AddString(pszMDItem); |
3241 | 0 | } |
3242 | 0 | } |
3243 | 0 | if (bChanged) |
3244 | 0 | { |
3245 | 0 | poBand->SetMetadata(aosNewMD.List()); |
3246 | 0 | } |
3247 | 0 | } |
3248 | |
|
3249 | 0 | GDALDataset::ClearStatistics(); |
3250 | 0 | } |
3251 | | |
3252 | | /************************************************************************/ |
3253 | | /* VRTMapSharedResources::LockGuard() */ |
3254 | | /************************************************************************/ |
3255 | | |
3256 | | std::unique_ptr<std::lock_guard<std::mutex>> |
3257 | | VRTMapSharedResources::LockGuard() const |
3258 | 0 | { |
3259 | 0 | std::unique_ptr<std::lock_guard<std::mutex>> poLockGuard; |
3260 | 0 | if (m_bUseMutex) |
3261 | 0 | { |
3262 | 0 | poLockGuard = std::make_unique<std::lock_guard<std::mutex>>(m_oMutex); |
3263 | 0 | } |
3264 | 0 | return poLockGuard; |
3265 | 0 | } |
3266 | | |
3267 | | /************************************************************************/ |
3268 | | /* VRTMapSharedResources::Get() */ |
3269 | | /************************************************************************/ |
3270 | | |
3271 | | GDALDataset *VRTMapSharedResources::Get(const std::string &osKey) const |
3272 | 0 | { |
3273 | 0 | auto poLockGuard = LockGuard(); |
3274 | 0 | CPL_IGNORE_RET_VAL(poLockGuard); |
3275 | 0 | auto oIter = m_oMap.find(osKey); |
3276 | 0 | GDALDataset *poRet = nullptr; |
3277 | 0 | if (oIter != m_oMap.end()) |
3278 | 0 | poRet = oIter->second; |
3279 | 0 | return poRet; |
3280 | 0 | } |
3281 | | |
3282 | | /************************************************************************/ |
3283 | | /* VRTMapSharedResources::Insert() */ |
3284 | | /************************************************************************/ |
3285 | | |
3286 | | void VRTMapSharedResources::Insert(const std::string &osKey, GDALDataset *poDS) |
3287 | 0 | { |
3288 | 0 | auto poLockGuard = LockGuard(); |
3289 | 0 | CPL_IGNORE_RET_VAL(poLockGuard); |
3290 | 0 | m_oMap[osKey] = poDS; |
3291 | 0 | } |
3292 | | |
3293 | | /************************************************************************/ |
3294 | | /* VRTMapSharedResources::InitMutex() */ |
3295 | | /************************************************************************/ |
3296 | | |
3297 | | void VRTMapSharedResources::InitMutex() |
3298 | 0 | { |
3299 | 0 | m_bUseMutex = true; |
3300 | 0 | } |
3301 | | |
3302 | | /*! @endcond */ |