/src/gdal/frmts/ogcapi/gdalogcapidataset.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL |
4 | | * Purpose: OGC API interface |
5 | | * Author: Even Rouault, <even.rouault at spatialys.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2020, Even Rouault, <even.rouault at spatialys.com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "cpl_error.h" |
14 | | #include "cpl_json.h" |
15 | | #include "cpl_http.h" |
16 | | #include "gdal_frmts.h" |
17 | | #include "gdal_priv.h" |
18 | | #include "tilematrixset.hpp" |
19 | | #include "gdal_utils.h" |
20 | | #include "ogrsf_frmts.h" |
21 | | #include "ogr_spatialref.h" |
22 | | #include "gdalplugindriverproxy.h" |
23 | | |
24 | | #include "parsexsd.h" |
25 | | |
26 | | #include <algorithm> |
27 | | #include <memory> |
28 | | #include <vector> |
29 | | |
30 | 0 | #define MEDIA_TYPE_OAPI_3_0 "application/vnd.oai.openapi+json;version=3.0" |
31 | | #define MEDIA_TYPE_OAPI_3_0_ALT "application/openapi+json;version=3.0" |
32 | 0 | #define MEDIA_TYPE_JSON "application/json" |
33 | 0 | #define MEDIA_TYPE_GEOJSON "application/geo+json" |
34 | 0 | #define MEDIA_TYPE_TEXT_XML "text/xml" |
35 | 0 | #define MEDIA_TYPE_APPLICATION_XML "application/xml" |
36 | 0 | #define MEDIA_TYPE_JSON_SCHEMA "application/schema+json" |
37 | | |
38 | | /************************************************************************/ |
39 | | /* ==================================================================== */ |
40 | | /* OGCAPIDataset */ |
41 | | /* ==================================================================== */ |
42 | | /************************************************************************/ |
43 | | |
44 | | class OGCAPIDataset final : public GDALDataset |
45 | | { |
46 | | friend class OGCAPIMapWrapperBand; |
47 | | friend class OGCAPITilesWrapperBand; |
48 | | friend class OGCAPITiledLayer; |
49 | | |
50 | | bool m_bMustCleanPersistent = false; |
51 | | CPLString m_osRootURL{}; |
52 | | CPLString m_osUserPwd{}; |
53 | | CPLString m_osUserQueryParams{}; |
54 | | GDALGeoTransform m_gt{}; |
55 | | |
56 | | OGRSpatialReference m_oSRS{}; |
57 | | CPLString m_osTileData{}; |
58 | | |
59 | | // Classic OGC API features /items access |
60 | | std::unique_ptr<GDALDataset> m_poOAPIFDS{}; |
61 | | |
62 | | // Map API |
63 | | std::unique_ptr<GDALDataset> m_poWMSDS{}; |
64 | | |
65 | | // Tiles API |
66 | | std::vector<std::unique_ptr<GDALDataset>> m_apoDatasetsElementary{}; |
67 | | std::vector<std::unique_ptr<GDALDataset>> m_apoDatasetsAssembled{}; |
68 | | std::vector<std::unique_ptr<GDALDataset>> m_apoDatasetsCropped{}; |
69 | | |
70 | | std::vector<std::unique_ptr<OGRLayer>> m_apoLayers{}; |
71 | | |
72 | | CPLString BuildURL(const std::string &href) const; |
73 | | void SetRootURLFromURL(const std::string &osURL); |
74 | | int FigureBands(const std::string &osContentType, |
75 | | const CPLString &osImageURL); |
76 | | |
77 | | bool InitFromFile(GDALOpenInfo *poOpenInfo); |
78 | | bool InitFromURL(GDALOpenInfo *poOpenInfo); |
79 | | bool ProcessScale(const CPLJSONObject &oScaleDenominator, |
80 | | const double dfXMin, const double dfYMin, |
81 | | const double dfXMax, const double dfYMax); |
82 | | bool InitFromCollection(GDALOpenInfo *poOpenInfo, CPLJSONDocument &oDoc); |
83 | | bool Download(const CPLString &osURL, const char *pszPostContent, |
84 | | const char *pszAccept, CPLString &osResult, |
85 | | CPLString &osContentType, bool bEmptyContentOK, |
86 | | CPLStringList *paosHeaders); |
87 | | |
88 | | bool DownloadJSon(const CPLString &osURL, CPLJSONDocument &oDoc, |
89 | | const char *pszPostContent = nullptr, |
90 | | const char *pszAccept = MEDIA_TYPE_GEOJSON |
91 | | ", " MEDIA_TYPE_JSON, |
92 | | CPLStringList *paosHeaders = nullptr); |
93 | | |
94 | | std::unique_ptr<GDALDataset> |
95 | | OpenTile(const CPLString &osURLPattern, int nMatrix, int nColumn, int nRow, |
96 | | bool &bEmptyContent, unsigned int nOpenTileFlags = 0, |
97 | | const CPLString &osPrefix = {}, |
98 | | const char *const *papszOpenOptions = nullptr); |
99 | | |
100 | | bool InitWithMapAPI(GDALOpenInfo *poOpenInfo, |
101 | | const CPLJSONObject &oCollection, double dfXMin, |
102 | | double dfYMin, double dfXMax, double dfYMax); |
103 | | bool InitWithTilesAPI(GDALOpenInfo *poOpenInfo, const CPLString &osTilesURL, |
104 | | bool bIsMap, double dfXMin, double dfYMin, |
105 | | double dfXMax, double dfYMax, bool bBBOXIsInCRS84, |
106 | | const CPLJSONObject &oJsonCollection); |
107 | | bool InitWithCoverageAPI(GDALOpenInfo *poOpenInfo, |
108 | | const CPLString &osTilesURL, double dfXMin, |
109 | | double dfYMin, double dfXMax, double dfYMax, |
110 | | const CPLJSONObject &oJsonCollection); |
111 | | |
112 | | protected: |
113 | | CPLErr IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, |
114 | | int nYSize, void *pData, int nBufXSize, int nBufYSize, |
115 | | GDALDataType eBufType, int nBandCount, |
116 | | BANDMAP_TYPE panBandMap, GSpacing nPixelSpace, |
117 | | GSpacing nLineSpace, GSpacing nBandSpace, |
118 | | GDALRasterIOExtraArg *psExtraArg) override; |
119 | | |
120 | | int CloseDependentDatasets() override; |
121 | | |
122 | | public: |
123 | 68 | OGCAPIDataset() = default; |
124 | | ~OGCAPIDataset() override; |
125 | | |
126 | | CPLErr GetGeoTransform(GDALGeoTransform >) const override; |
127 | | const OGRSpatialReference *GetSpatialRef() const override; |
128 | | |
129 | | int GetLayerCount() const override |
130 | 0 | { |
131 | 0 | return m_poOAPIFDS ? m_poOAPIFDS->GetLayerCount() |
132 | 0 | : static_cast<int>(m_apoLayers.size()); |
133 | 0 | } |
134 | | |
135 | | const OGRLayer *GetLayer(int idx) const override |
136 | 0 | { |
137 | 0 | return m_poOAPIFDS ? m_poOAPIFDS->GetLayer(idx) |
138 | 0 | : idx >= 0 && idx < GetLayerCount() ? m_apoLayers[idx].get() |
139 | 0 | : nullptr; |
140 | 0 | } |
141 | | |
142 | | static int Identify(GDALOpenInfo *poOpenInfo); |
143 | | static GDALDataset *Open(GDALOpenInfo *poOpenInfo); |
144 | | }; |
145 | | |
146 | | /************************************************************************/ |
147 | | /* ==================================================================== */ |
148 | | /* OGCAPIMapWrapperBand */ |
149 | | /* ==================================================================== */ |
150 | | /************************************************************************/ |
151 | | |
152 | | class OGCAPIMapWrapperBand final : public GDALRasterBand |
153 | | { |
154 | | public: |
155 | | OGCAPIMapWrapperBand(OGCAPIDataset *poDS, int nBand); |
156 | | |
157 | | GDALRasterBand *GetOverview(int nLevel) override; |
158 | | int GetOverviewCount() override; |
159 | | GDALColorInterp GetColorInterpretation() override; |
160 | | |
161 | | protected: |
162 | | CPLErr IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage) override; |
163 | | CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int, |
164 | | GDALDataType, GSpacing, GSpacing, |
165 | | GDALRasterIOExtraArg *psExtraArg) override; |
166 | | }; |
167 | | |
168 | | /************************************************************************/ |
169 | | /* ==================================================================== */ |
170 | | /* OGCAPITilesWrapperBand */ |
171 | | /* ==================================================================== */ |
172 | | /************************************************************************/ |
173 | | |
174 | | class OGCAPITilesWrapperBand final : public GDALRasterBand |
175 | | { |
176 | | public: |
177 | | OGCAPITilesWrapperBand(OGCAPIDataset *poDS, int nBand); |
178 | | |
179 | | GDALRasterBand *GetOverview(int nLevel) override; |
180 | | int GetOverviewCount() override; |
181 | | GDALColorInterp GetColorInterpretation() override; |
182 | | |
183 | | protected: |
184 | | CPLErr IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage) override; |
185 | | CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int, |
186 | | GDALDataType, GSpacing, GSpacing, |
187 | | GDALRasterIOExtraArg *psExtraArg) override; |
188 | | }; |
189 | | |
190 | | /************************************************************************/ |
191 | | /* ==================================================================== */ |
192 | | /* OGCAPITiledLayer */ |
193 | | /* ==================================================================== */ |
194 | | /************************************************************************/ |
195 | | |
196 | | class OGCAPITiledLayer; |
197 | | |
198 | | class OGCAPITiledLayerFeatureDefn final : public OGRFeatureDefn |
199 | | { |
200 | | OGCAPITiledLayer *m_poLayer = nullptr; |
201 | | |
202 | | CPL_DISALLOW_COPY_ASSIGN(OGCAPITiledLayerFeatureDefn) |
203 | | |
204 | | public: |
205 | | OGCAPITiledLayerFeatureDefn(OGCAPITiledLayer *poLayer, const char *pszName) |
206 | 0 | : OGRFeatureDefn(pszName), m_poLayer(poLayer) |
207 | 0 | { |
208 | 0 | } |
209 | | |
210 | | int GetFieldCount() const override; |
211 | | |
212 | | void InvalidateLayer() |
213 | 0 | { |
214 | 0 | m_poLayer = nullptr; |
215 | 0 | } |
216 | | }; |
217 | | |
218 | | class OGCAPITiledLayer final |
219 | | : public OGRLayer, |
220 | | public OGRGetNextFeatureThroughRaw<OGCAPITiledLayer> |
221 | | { |
222 | | OGCAPIDataset *m_poDS = nullptr; |
223 | | bool m_bFeatureDefnEstablished = false; |
224 | | bool m_bEstablishFieldsCalled = |
225 | | false; // prevent recursion in EstablishFields() |
226 | | OGCAPITiledLayerFeatureDefn *m_poFeatureDefn = nullptr; |
227 | | OGREnvelope m_sEnvelope{}; |
228 | | std::unique_ptr<GDALDataset> m_poUnderlyingDS{}; |
229 | | OGRLayer *m_poUnderlyingLayer = nullptr; |
230 | | int m_nCurY = 0; |
231 | | int m_nCurX = 0; |
232 | | |
233 | | CPLString m_osTileURL{}; |
234 | | bool m_bIsMVT = false; |
235 | | |
236 | | const gdal::TileMatrixSet::TileMatrix m_oTileMatrix{}; |
237 | | bool m_bInvertAxis = false; |
238 | | |
239 | | // absolute bounds |
240 | | int m_nMinX = 0; |
241 | | int m_nMaxX = 0; |
242 | | int m_nMinY = 0; |
243 | | int m_nMaxY = 0; |
244 | | |
245 | | // depends on spatial filter |
246 | | int m_nCurMinX = 0; |
247 | | int m_nCurMaxX = 0; |
248 | | int m_nCurMinY = 0; |
249 | | int m_nCurMaxY = 0; |
250 | | |
251 | | int GetCoalesceFactorForRow(int nRow) const; |
252 | | bool IncrementTileIndices(); |
253 | | OGRFeature *GetNextRawFeature(); |
254 | | GDALDataset *OpenTile(int nX, int nY, bool &bEmptyContent); |
255 | | void FinalizeFeatureDefnWithLayer(OGRLayer *poUnderlyingLayer); |
256 | | OGRFeature *BuildFeature(OGRFeature *poSrcFeature, int nX, int nY); |
257 | | |
258 | | CPL_DISALLOW_COPY_ASSIGN(OGCAPITiledLayer) |
259 | | |
260 | | protected: |
261 | | friend class OGCAPITiledLayerFeatureDefn; |
262 | | void EstablishFields(); |
263 | | |
264 | | public: |
265 | | OGCAPITiledLayer(OGCAPIDataset *poDS, bool bInvertAxis, |
266 | | const CPLString &osTileURL, bool bIsMVT, |
267 | | const gdal::TileMatrixSet::TileMatrix &tileMatrix, |
268 | | OGRwkbGeometryType eGeomType); |
269 | | ~OGCAPITiledLayer() override; |
270 | | |
271 | | void SetExtent(double dfXMin, double dfYMin, double dfXMax, double dfYMax); |
272 | | void SetFields(const std::vector<std::unique_ptr<OGRFieldDefn>> &apoFields); |
273 | | void SetMinMaxXY(int minCol, int minRow, int maxCol, int maxRow); |
274 | | |
275 | | void ResetReading() override; |
276 | | |
277 | | const OGRFeatureDefn *GetLayerDefn() const override |
278 | 0 | { |
279 | 0 | return m_poFeatureDefn; |
280 | 0 | } |
281 | | |
282 | | const char *GetName() const override |
283 | 0 | { |
284 | 0 | return m_poFeatureDefn->GetName(); |
285 | 0 | } |
286 | | |
287 | | OGRwkbGeometryType GetGeomType() const override |
288 | 0 | { |
289 | 0 | return m_poFeatureDefn->GetGeomType(); |
290 | 0 | } |
291 | | DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(OGCAPITiledLayer) |
292 | | |
293 | | GIntBig GetFeatureCount(int /* bForce */) override |
294 | 0 | { |
295 | 0 | return -1; |
296 | 0 | } |
297 | | |
298 | | OGRErr IGetExtent(int iGeomField, OGREnvelope *psExtent, |
299 | | bool bForce) override; |
300 | | |
301 | | OGRErr ISetSpatialFilter(int iGeomField, |
302 | | const OGRGeometry *poGeom) override; |
303 | | |
304 | | OGRFeature *GetFeature(GIntBig nFID) override; |
305 | | int TestCapability(const char *) const override; |
306 | | }; |
307 | | |
308 | | /************************************************************************/ |
309 | | /* GetFieldCount() */ |
310 | | /************************************************************************/ |
311 | | |
312 | | int OGCAPITiledLayerFeatureDefn::GetFieldCount() const |
313 | 0 | { |
314 | 0 | if (m_poLayer) |
315 | 0 | { |
316 | 0 | m_poLayer->EstablishFields(); |
317 | 0 | } |
318 | 0 | return OGRFeatureDefn::GetFieldCount(); |
319 | 0 | } |
320 | | |
321 | | /************************************************************************/ |
322 | | /* ~OGCAPIDataset() */ |
323 | | /************************************************************************/ |
324 | | |
325 | | OGCAPIDataset::~OGCAPIDataset() |
326 | 68 | { |
327 | 68 | if (m_bMustCleanPersistent) |
328 | 68 | { |
329 | 68 | char **papszOptions = CSLSetNameValue(nullptr, "CLOSE_PERSISTENT", |
330 | 68 | CPLSPrintf("OGCAPI:%p", this)); |
331 | 68 | CPLHTTPDestroyResult(CPLHTTPFetch(m_osRootURL, papszOptions)); |
332 | 68 | CSLDestroy(papszOptions); |
333 | 68 | } |
334 | | |
335 | 68 | OGCAPIDataset::CloseDependentDatasets(); |
336 | 68 | } |
337 | | |
338 | | /************************************************************************/ |
339 | | /* CloseDependentDatasets() */ |
340 | | /************************************************************************/ |
341 | | |
342 | | int OGCAPIDataset::CloseDependentDatasets() |
343 | 68 | { |
344 | 68 | if (m_apoDatasetsElementary.empty()) |
345 | 68 | return false; |
346 | | |
347 | | // in this order |
348 | 0 | m_apoDatasetsCropped.clear(); |
349 | 0 | m_apoDatasetsAssembled.clear(); |
350 | 0 | m_apoDatasetsElementary.clear(); |
351 | 0 | return true; |
352 | 68 | } |
353 | | |
354 | | /************************************************************************/ |
355 | | /* GetGeoTransform() */ |
356 | | /************************************************************************/ |
357 | | |
358 | | CPLErr OGCAPIDataset::GetGeoTransform(GDALGeoTransform >) const |
359 | 0 | { |
360 | 0 | gt = m_gt; |
361 | 0 | return CE_None; |
362 | 0 | } |
363 | | |
364 | | /************************************************************************/ |
365 | | /* GetSpatialRef() */ |
366 | | /************************************************************************/ |
367 | | |
368 | | const OGRSpatialReference *OGCAPIDataset::GetSpatialRef() const |
369 | 0 | { |
370 | 0 | return !m_oSRS.IsEmpty() ? &m_oSRS : nullptr; |
371 | 0 | } |
372 | | |
373 | | /************************************************************************/ |
374 | | /* CheckContentType() */ |
375 | | /************************************************************************/ |
376 | | |
377 | | // We may ask for "application/openapi+json;version=3.0" |
378 | | // and the server returns "application/openapi+json; charset=utf-8; version=3.0" |
379 | | static bool CheckContentType(const char *pszGotContentType, |
380 | | const char *pszExpectedContentType) |
381 | 0 | { |
382 | 0 | CPLStringList aosGotTokens(CSLTokenizeString2(pszGotContentType, "; ", 0)); |
383 | 0 | CPLStringList aosExpectedTokens( |
384 | 0 | CSLTokenizeString2(pszExpectedContentType, "; ", 0)); |
385 | 0 | for (int i = 0; i < aosExpectedTokens.size(); i++) |
386 | 0 | { |
387 | 0 | bool bFound = false; |
388 | 0 | for (int j = 0; j < aosGotTokens.size(); j++) |
389 | 0 | { |
390 | 0 | if (EQUAL(aosExpectedTokens[i], aosGotTokens[j])) |
391 | 0 | { |
392 | 0 | bFound = true; |
393 | 0 | break; |
394 | 0 | } |
395 | 0 | } |
396 | 0 | if (!bFound) |
397 | 0 | return false; |
398 | 0 | } |
399 | 0 | return true; |
400 | 0 | } |
401 | | |
402 | | /************************************************************************/ |
403 | | /* Download() */ |
404 | | /************************************************************************/ |
405 | | |
406 | | bool OGCAPIDataset::Download(const CPLString &osURL, const char *pszPostContent, |
407 | | const char *pszAccept, CPLString &osResult, |
408 | | CPLString &osContentType, bool bEmptyContentOK, |
409 | | CPLStringList *paosHeaders) |
410 | 68 | { |
411 | 68 | char **papszOptions = nullptr; |
412 | 68 | CPLString osHeaders; |
413 | 68 | if (pszAccept) |
414 | 68 | { |
415 | 68 | osHeaders += "Accept: "; |
416 | 68 | osHeaders += pszAccept; |
417 | 68 | } |
418 | 68 | if (pszPostContent) |
419 | 0 | { |
420 | 0 | if (!osHeaders.empty()) |
421 | 0 | { |
422 | 0 | osHeaders += "\r\n"; |
423 | 0 | } |
424 | 0 | osHeaders += "Content-Type: application/json"; |
425 | 0 | } |
426 | 68 | if (!osHeaders.empty()) |
427 | 68 | { |
428 | 68 | papszOptions = |
429 | 68 | CSLSetNameValue(papszOptions, "HEADERS", osHeaders.c_str()); |
430 | 68 | } |
431 | 68 | if (!m_osUserPwd.empty()) |
432 | 0 | { |
433 | 0 | papszOptions = |
434 | 0 | CSLSetNameValue(papszOptions, "USERPWD", m_osUserPwd.c_str()); |
435 | 0 | } |
436 | 68 | m_bMustCleanPersistent = true; |
437 | 68 | papszOptions = |
438 | 68 | CSLAddString(papszOptions, CPLSPrintf("PERSISTENT=OGCAPI:%p", this)); |
439 | 68 | CPLString osURLWithQueryParameters(osURL); |
440 | 68 | if (!m_osUserQueryParams.empty() && |
441 | 0 | osURL.find('?' + m_osUserQueryParams) == std::string::npos && |
442 | 0 | osURL.find('&' + m_osUserQueryParams) == std::string::npos) |
443 | 0 | { |
444 | 0 | if (osURL.find('?') == std::string::npos) |
445 | 0 | { |
446 | 0 | osURLWithQueryParameters += '?'; |
447 | 0 | } |
448 | 0 | else |
449 | 0 | { |
450 | 0 | osURLWithQueryParameters += '&'; |
451 | 0 | } |
452 | 0 | osURLWithQueryParameters += m_osUserQueryParams; |
453 | 0 | } |
454 | 68 | if (pszPostContent) |
455 | 0 | { |
456 | 0 | papszOptions = |
457 | 0 | CSLSetNameValue(papszOptions, "POSTFIELDS", pszPostContent); |
458 | 0 | } |
459 | 68 | CPLHTTPResult *psResult = |
460 | 68 | CPLHTTPFetch(osURLWithQueryParameters, papszOptions); |
461 | 68 | CSLDestroy(papszOptions); |
462 | 68 | if (!psResult) |
463 | 0 | return false; |
464 | | |
465 | 68 | if (paosHeaders) |
466 | 0 | { |
467 | 0 | *paosHeaders = CSLDuplicate(psResult->papszHeaders); |
468 | 0 | } |
469 | | |
470 | 68 | if (psResult->pszErrBuf != nullptr) |
471 | 68 | { |
472 | 68 | std::string osErrorMsg(psResult->pszErrBuf); |
473 | 68 | const char *pszData = |
474 | 68 | reinterpret_cast<const char *>(psResult->pabyData); |
475 | 68 | if (pszData) |
476 | 0 | { |
477 | 0 | osErrorMsg += ", "; |
478 | 0 | osErrorMsg.append(pszData, CPLStrnlen(pszData, 1000)); |
479 | 0 | } |
480 | 68 | CPLError(CE_Failure, CPLE_AppDefined, "%s", osErrorMsg.c_str()); |
481 | 68 | CPLHTTPDestroyResult(psResult); |
482 | 68 | return false; |
483 | 68 | } |
484 | | |
485 | 0 | if (psResult->pszContentType) |
486 | 0 | osContentType = psResult->pszContentType; |
487 | |
|
488 | 0 | if (pszAccept != nullptr) |
489 | 0 | { |
490 | 0 | bool bFoundExpectedContentType = false; |
491 | 0 | if (strstr(pszAccept, "xml") && psResult->pszContentType != nullptr && |
492 | 0 | (CheckContentType(psResult->pszContentType, MEDIA_TYPE_TEXT_XML) || |
493 | 0 | CheckContentType(psResult->pszContentType, |
494 | 0 | MEDIA_TYPE_APPLICATION_XML))) |
495 | 0 | { |
496 | 0 | bFoundExpectedContentType = true; |
497 | 0 | } |
498 | |
|
499 | 0 | if (strstr(pszAccept, MEDIA_TYPE_JSON_SCHEMA) && |
500 | 0 | psResult->pszContentType != nullptr && |
501 | 0 | (CheckContentType(psResult->pszContentType, MEDIA_TYPE_JSON) || |
502 | 0 | CheckContentType(psResult->pszContentType, |
503 | 0 | MEDIA_TYPE_JSON_SCHEMA))) |
504 | 0 | { |
505 | 0 | bFoundExpectedContentType = true; |
506 | 0 | } |
507 | |
|
508 | 0 | for (const char *pszMediaType : { |
509 | 0 | MEDIA_TYPE_JSON, |
510 | 0 | MEDIA_TYPE_GEOJSON, |
511 | 0 | MEDIA_TYPE_OAPI_3_0, |
512 | 0 | }) |
513 | 0 | { |
514 | 0 | if (strstr(pszAccept, pszMediaType) && |
515 | 0 | psResult->pszContentType != nullptr && |
516 | 0 | CheckContentType(psResult->pszContentType, pszMediaType)) |
517 | 0 | { |
518 | 0 | bFoundExpectedContentType = true; |
519 | 0 | break; |
520 | 0 | } |
521 | 0 | } |
522 | |
|
523 | 0 | if (!bFoundExpectedContentType) |
524 | 0 | { |
525 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Unexpected Content-Type: %s", |
526 | 0 | psResult->pszContentType ? psResult->pszContentType |
527 | 0 | : "(null)"); |
528 | 0 | CPLHTTPDestroyResult(psResult); |
529 | 0 | return false; |
530 | 0 | } |
531 | 0 | } |
532 | | |
533 | 0 | if (psResult->pabyData == nullptr) |
534 | 0 | { |
535 | 0 | osResult.clear(); |
536 | 0 | if (!bEmptyContentOK) |
537 | 0 | { |
538 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
539 | 0 | "Empty content returned by server"); |
540 | 0 | CPLHTTPDestroyResult(psResult); |
541 | 0 | return false; |
542 | 0 | } |
543 | 0 | } |
544 | 0 | else |
545 | 0 | { |
546 | 0 | osResult.assign(reinterpret_cast<const char *>(psResult->pabyData), |
547 | 0 | psResult->nDataLen); |
548 | | #ifdef DEBUG_VERBOSE |
549 | | CPLDebug("OGCAPI", "%s", osResult.c_str()); |
550 | | #endif |
551 | 0 | } |
552 | 0 | CPLHTTPDestroyResult(psResult); |
553 | 0 | return true; |
554 | 0 | } |
555 | | |
556 | | /************************************************************************/ |
557 | | /* DownloadJSon() */ |
558 | | /************************************************************************/ |
559 | | |
560 | | bool OGCAPIDataset::DownloadJSon(const CPLString &osURL, CPLJSONDocument &oDoc, |
561 | | const char *pszPostContent, |
562 | | const char *pszAccept, |
563 | | CPLStringList *paosHeaders) |
564 | 68 | { |
565 | 68 | CPLString osResult; |
566 | 68 | CPLString osContentType; |
567 | 68 | if (!Download(osURL, pszPostContent, pszAccept, osResult, osContentType, |
568 | 68 | false, paosHeaders)) |
569 | 68 | return false; |
570 | 0 | return oDoc.LoadMemory(osResult); |
571 | 68 | } |
572 | | |
573 | | /************************************************************************/ |
574 | | /* OpenTile() */ |
575 | | /************************************************************************/ |
576 | | |
577 | | std::unique_ptr<GDALDataset> |
578 | | OGCAPIDataset::OpenTile(const CPLString &osURLPattern, int nMatrix, int nColumn, |
579 | | int nRow, bool &bEmptyContent, |
580 | | unsigned int nOpenTileFlags, const CPLString &osPrefix, |
581 | | const char *const *papszOpenTileOptions) |
582 | 0 | { |
583 | 0 | CPLString osURL(osURLPattern); |
584 | 0 | osURL.replaceAll("{tileMatrix}", CPLSPrintf("%d", nMatrix)); |
585 | 0 | osURL.replaceAll("{tileCol}", CPLSPrintf("%d", nColumn)); |
586 | 0 | osURL.replaceAll("{tileRow}", CPLSPrintf("%d", nRow)); |
587 | |
|
588 | 0 | CPLString osContentType; |
589 | 0 | if (!this->Download(osURL, nullptr, nullptr, m_osTileData, osContentType, |
590 | 0 | true, nullptr)) |
591 | 0 | { |
592 | 0 | return nullptr; |
593 | 0 | } |
594 | | |
595 | 0 | bEmptyContent = m_osTileData.empty(); |
596 | 0 | if (bEmptyContent) |
597 | 0 | return nullptr; |
598 | | |
599 | 0 | const CPLString osTempFile(VSIMemGenerateHiddenFilename("ogcapi")); |
600 | 0 | VSIFCloseL(VSIFileFromMemBuffer(osTempFile.c_str(), |
601 | 0 | reinterpret_cast<GByte *>(&m_osTileData[0]), |
602 | 0 | m_osTileData.size(), false)); |
603 | |
|
604 | 0 | GDALDataset *result = nullptr; |
605 | |
|
606 | 0 | if (osPrefix.empty()) |
607 | 0 | result = GDALDataset::Open(osTempFile.c_str(), nOpenTileFlags, nullptr, |
608 | 0 | papszOpenTileOptions); |
609 | 0 | else |
610 | 0 | result = |
611 | 0 | GDALDataset::Open((osPrefix + ":" + osTempFile).c_str(), |
612 | 0 | nOpenTileFlags, nullptr, papszOpenTileOptions); |
613 | |
|
614 | 0 | VSIUnlink(osTempFile); |
615 | |
|
616 | 0 | return std::unique_ptr<GDALDataset>(result); |
617 | 0 | } |
618 | | |
619 | | /************************************************************************/ |
620 | | /* Identify() */ |
621 | | /************************************************************************/ |
622 | | |
623 | | int OGCAPIDataset::Identify(GDALOpenInfo *poOpenInfo) |
624 | 674k | { |
625 | 674k | if (STARTS_WITH_CI(poOpenInfo->pszFilename, "OGCAPI:")) |
626 | 136 | return TRUE; |
627 | 674k | if (poOpenInfo->IsExtensionEqualToCI("moaw")) |
628 | 0 | return TRUE; |
629 | 674k | if (poOpenInfo->IsSingleAllowedDriver("OGCAPI")) |
630 | 0 | { |
631 | 0 | return TRUE; |
632 | 0 | } |
633 | 674k | return FALSE; |
634 | 674k | } |
635 | | |
636 | | /************************************************************************/ |
637 | | /* BuildURL() */ |
638 | | /************************************************************************/ |
639 | | |
640 | | CPLString OGCAPIDataset::BuildURL(const std::string &href) const |
641 | 0 | { |
642 | 0 | if (!href.empty() && href[0] == '/') |
643 | 0 | return m_osRootURL + href; |
644 | 0 | return href; |
645 | 0 | } |
646 | | |
647 | | /************************************************************************/ |
648 | | /* SetRootURLFromURL() */ |
649 | | /************************************************************************/ |
650 | | |
651 | | void OGCAPIDataset::SetRootURLFromURL(const std::string &osURL) |
652 | 0 | { |
653 | 0 | const char *pszStr = osURL.c_str(); |
654 | 0 | const char *pszPtr = pszStr; |
655 | 0 | if (STARTS_WITH(pszPtr, "http://")) |
656 | 0 | pszPtr += strlen("http://"); |
657 | 0 | else if (STARTS_WITH(pszPtr, "https://")) |
658 | 0 | pszPtr += strlen("https://"); |
659 | 0 | pszPtr = strchr(pszPtr, '/'); |
660 | 0 | if (pszPtr) |
661 | 0 | m_osRootURL.assign(pszStr, pszPtr - pszStr); |
662 | 0 | } |
663 | | |
664 | | /************************************************************************/ |
665 | | /* FigureBands() */ |
666 | | /************************************************************************/ |
667 | | |
668 | | int OGCAPIDataset::FigureBands(const std::string &osContentType, |
669 | | const CPLString &osImageURL) |
670 | 0 | { |
671 | 0 | int result = 0; |
672 | |
|
673 | 0 | if (osContentType == "image/png") |
674 | 0 | { |
675 | 0 | result = 4; |
676 | 0 | } |
677 | 0 | else if (osContentType == "image/jpeg") |
678 | 0 | { |
679 | 0 | result = 3; |
680 | 0 | } |
681 | 0 | else |
682 | 0 | { |
683 | | // Since we don't know the format download a tile and find out |
684 | 0 | bool bEmptyContent = false; |
685 | 0 | std::unique_ptr<GDALDataset> dataset = |
686 | 0 | OpenTile(osImageURL, 0, 0, 0, bEmptyContent, GDAL_OF_RASTER); |
687 | | |
688 | | // Return the bands from the image, if we didn't get an image then assume 3. |
689 | 0 | result = dataset ? static_cast<int>(dataset->GetBands().size()) : 3; |
690 | 0 | } |
691 | |
|
692 | 0 | return result; |
693 | 0 | } |
694 | | |
695 | | /************************************************************************/ |
696 | | /* InitFromFile() */ |
697 | | /************************************************************************/ |
698 | | |
699 | | bool OGCAPIDataset::InitFromFile(GDALOpenInfo *poOpenInfo) |
700 | 0 | { |
701 | 0 | CPLJSONDocument oDoc; |
702 | 0 | if (!oDoc.Load(poOpenInfo->pszFilename)) |
703 | 0 | return false; |
704 | 0 | auto oProcess = oDoc.GetRoot()["process"]; |
705 | 0 | if (oProcess.GetType() != CPLJSONObject::Type::String) |
706 | 0 | { |
707 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
708 | 0 | "Cannot find 'process' key in .moaw file"); |
709 | 0 | return false; |
710 | 0 | } |
711 | | |
712 | 0 | const CPLString osURLProcess(oProcess.ToString()); |
713 | 0 | SetRootURLFromURL(osURLProcess); |
714 | |
|
715 | 0 | GByte *pabyContent = nullptr; |
716 | 0 | vsi_l_offset nSize = 0; |
717 | 0 | if (!VSIIngestFile(poOpenInfo->fpL, nullptr, &pabyContent, &nSize, |
718 | 0 | 1024 * 1024)) |
719 | 0 | return false; |
720 | 0 | CPLString osPostContent(reinterpret_cast<const char *>(pabyContent)); |
721 | 0 | CPLFree(pabyContent); |
722 | 0 | if (!DownloadJSon(osURLProcess.c_str(), oDoc, osPostContent.c_str())) |
723 | 0 | return false; |
724 | | |
725 | 0 | return InitFromCollection(poOpenInfo, oDoc); |
726 | 0 | } |
727 | | |
728 | | /************************************************************************/ |
729 | | /* ProcessScale() */ |
730 | | /************************************************************************/ |
731 | | |
732 | | bool OGCAPIDataset::ProcessScale(const CPLJSONObject &oScaleDenominator, |
733 | | const double dfXMin, const double dfYMin, |
734 | | const double dfXMax, const double dfYMax) |
735 | | |
736 | 0 | { |
737 | 0 | double dfRes = 1e-8; // arbitrary |
738 | 0 | if (oScaleDenominator.IsValid()) |
739 | 0 | { |
740 | 0 | const double dfScaleDenominator = oScaleDenominator.ToDouble(); |
741 | 0 | constexpr double HALF_CIRCUMFERENCE = 6378137 * M_PI; |
742 | 0 | dfRes = dfScaleDenominator / ((HALF_CIRCUMFERENCE / 180) / 0.28e-3); |
743 | 0 | } |
744 | 0 | if (dfRes == 0.0) |
745 | 0 | return false; |
746 | | |
747 | 0 | double dfXSize = (dfXMax - dfXMin) / dfRes; |
748 | 0 | double dfYSize = (dfYMax - dfYMin) / dfRes; |
749 | 0 | while (dfXSize > INT_MAX || dfYSize > INT_MAX) |
750 | 0 | { |
751 | 0 | dfXSize /= 2; |
752 | 0 | dfYSize /= 2; |
753 | 0 | } |
754 | |
|
755 | 0 | nRasterXSize = std::max(1, static_cast<int>(0.5 + dfXSize)); |
756 | 0 | nRasterYSize = std::max(1, static_cast<int>(0.5 + dfYSize)); |
757 | 0 | m_gt[0] = dfXMin; |
758 | 0 | m_gt[1] = (dfXMax - dfXMin) / nRasterXSize; |
759 | 0 | m_gt[3] = dfYMax; |
760 | 0 | m_gt[5] = -(dfYMax - dfYMin) / nRasterYSize; |
761 | |
|
762 | 0 | return true; |
763 | 0 | } |
764 | | |
765 | | /************************************************************************/ |
766 | | /* InitFromCollection() */ |
767 | | /************************************************************************/ |
768 | | |
769 | | bool OGCAPIDataset::InitFromCollection(GDALOpenInfo *poOpenInfo, |
770 | | CPLJSONDocument &oDoc) |
771 | 0 | { |
772 | 0 | const CPLJSONObject oRoot = oDoc.GetRoot(); |
773 | 0 | auto osTitle = oRoot.GetString("title"); |
774 | 0 | if (!osTitle.empty()) |
775 | 0 | { |
776 | 0 | SetMetadataItem("TITLE", osTitle.c_str()); |
777 | 0 | } |
778 | |
|
779 | 0 | auto oLinks = oRoot.GetArray("links"); |
780 | 0 | if (!oLinks.IsValid()) |
781 | 0 | { |
782 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Missing links"); |
783 | 0 | return false; |
784 | 0 | } |
785 | 0 | auto oBboxes = oRoot["extent"]["spatial"]["bbox"].ToArray(); |
786 | 0 | if (oBboxes.Size() != 1) |
787 | 0 | { |
788 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Missing bbox"); |
789 | 0 | return false; |
790 | 0 | } |
791 | 0 | auto oBbox = oBboxes[0].ToArray(); |
792 | 0 | if (oBbox.Size() != 4) |
793 | 0 | { |
794 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid bbox"); |
795 | 0 | return false; |
796 | 0 | } |
797 | 0 | const bool bBBOXIsInCRS84 = |
798 | 0 | CSLFetchNameValue(poOpenInfo->papszOpenOptions, "MINX") == nullptr; |
799 | 0 | const double dfXMin = |
800 | 0 | CPLAtof(CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "MINX", |
801 | 0 | CPLSPrintf("%.17g", oBbox[0].ToDouble()))); |
802 | 0 | const double dfYMin = |
803 | 0 | CPLAtof(CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "MINY", |
804 | 0 | CPLSPrintf("%.17g", oBbox[1].ToDouble()))); |
805 | 0 | const double dfXMax = |
806 | 0 | CPLAtof(CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "MAXX", |
807 | 0 | CPLSPrintf("%.17g", oBbox[2].ToDouble()))); |
808 | 0 | const double dfYMax = |
809 | 0 | CPLAtof(CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "MAXY", |
810 | 0 | CPLSPrintf("%.17g", oBbox[3].ToDouble()))); |
811 | |
|
812 | 0 | auto oScaleDenominator = oRoot["scaleDenominator"]; |
813 | |
|
814 | 0 | if (!ProcessScale(oScaleDenominator, dfXMin, dfYMin, dfXMax, dfYMax)) |
815 | 0 | return false; |
816 | | |
817 | 0 | bool bFoundMap = false; |
818 | |
|
819 | 0 | CPLString osTilesetsMapURL; |
820 | 0 | bool bTilesetsMapURLJson = false; |
821 | |
|
822 | 0 | CPLString osTilesetsVectorURL; |
823 | 0 | bool bTilesetsVectorURLJson = false; |
824 | |
|
825 | 0 | CPLString osCoverageURL; |
826 | 0 | bool bCoverageGeotiff = false; |
827 | |
|
828 | 0 | CPLString osItemsURL; |
829 | 0 | bool bItemsJson = false; |
830 | |
|
831 | 0 | CPLString osSelfURL; |
832 | 0 | bool bSelfJson = false; |
833 | |
|
834 | 0 | for (const auto &oLink : oLinks) |
835 | 0 | { |
836 | 0 | const auto osRel = oLink.GetString("rel"); |
837 | 0 | const auto osType = oLink.GetString("type"); |
838 | 0 | if ((osRel == "http://www.opengis.net/def/rel/ogc/1.0/map" || |
839 | 0 | osRel == "[ogc-rel:map]") && |
840 | 0 | (osType == "image/png" || osType == "image/jpeg")) |
841 | 0 | { |
842 | 0 | bFoundMap = true; |
843 | 0 | } |
844 | 0 | else if (!bTilesetsMapURLJson && |
845 | 0 | (osRel == |
846 | 0 | "http://www.opengis.net/def/rel/ogc/1.0/tilesets-map" || |
847 | 0 | osRel == "[ogc-rel:tilesets-map]")) |
848 | 0 | { |
849 | 0 | if (osType == MEDIA_TYPE_JSON) |
850 | 0 | { |
851 | 0 | bTilesetsMapURLJson = true; |
852 | 0 | osTilesetsMapURL = BuildURL(oLink["href"].ToString()); |
853 | 0 | } |
854 | 0 | else if (osType.empty()) |
855 | 0 | { |
856 | 0 | osTilesetsMapURL = BuildURL(oLink["href"].ToString()); |
857 | 0 | } |
858 | 0 | } |
859 | 0 | else if (!bTilesetsVectorURLJson && |
860 | 0 | (osRel == "http://www.opengis.net/def/rel/ogc/1.0/" |
861 | 0 | "tilesets-vector" || |
862 | 0 | osRel == "[ogc-rel:tilesets-vector]")) |
863 | 0 | { |
864 | 0 | if (osType == MEDIA_TYPE_JSON) |
865 | 0 | { |
866 | 0 | bTilesetsVectorURLJson = true; |
867 | 0 | osTilesetsVectorURL = BuildURL(oLink["href"].ToString()); |
868 | 0 | } |
869 | 0 | else if (osType.empty()) |
870 | 0 | { |
871 | 0 | osTilesetsVectorURL = BuildURL(oLink["href"].ToString()); |
872 | 0 | } |
873 | 0 | } |
874 | 0 | else if ((osRel == "http://www.opengis.net/def/rel/ogc/1.0/coverage" || |
875 | 0 | osRel == "[ogc-rel:coverage]") && |
876 | 0 | (osType == "image/tiff; application=geotiff" || |
877 | 0 | osType == "application/x-geotiff")) |
878 | 0 | { |
879 | 0 | if (!bCoverageGeotiff) |
880 | 0 | { |
881 | 0 | osCoverageURL = BuildURL(oLink["href"].ToString()); |
882 | 0 | bCoverageGeotiff = true; |
883 | 0 | } |
884 | 0 | } |
885 | 0 | else if ((osRel == "http://www.opengis.net/def/rel/ogc/1.0/coverage" || |
886 | 0 | osRel == "[ogc-rel:coverage]") && |
887 | 0 | osType.empty()) |
888 | 0 | { |
889 | 0 | osCoverageURL = BuildURL(oLink["href"].ToString()); |
890 | 0 | } |
891 | 0 | else if (!bItemsJson && osRel == "items") |
892 | 0 | { |
893 | 0 | if (osType == MEDIA_TYPE_GEOJSON || osType == MEDIA_TYPE_JSON) |
894 | 0 | { |
895 | 0 | bItemsJson = true; |
896 | 0 | osItemsURL = BuildURL(oLink["href"].ToString()); |
897 | 0 | } |
898 | 0 | else if (osType.empty()) |
899 | 0 | { |
900 | 0 | osItemsURL = BuildURL(oLink["href"].ToString()); |
901 | 0 | } |
902 | 0 | } |
903 | 0 | else if (!bSelfJson && osRel == "self") |
904 | 0 | { |
905 | 0 | if (osType == "application/json") |
906 | 0 | { |
907 | 0 | bSelfJson = true; |
908 | 0 | osSelfURL = BuildURL(oLink["href"].ToString()); |
909 | 0 | } |
910 | 0 | else if (osType.empty()) |
911 | 0 | { |
912 | 0 | osSelfURL = BuildURL(oLink["href"].ToString()); |
913 | 0 | } |
914 | 0 | } |
915 | 0 | } |
916 | |
|
917 | 0 | if (!bFoundMap && osTilesetsMapURL.empty() && osTilesetsVectorURL.empty() && |
918 | 0 | osCoverageURL.empty() && osSelfURL.empty() && osItemsURL.empty()) |
919 | 0 | { |
920 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
921 | 0 | "Missing map, tilesets, coverage or items relation in links"); |
922 | 0 | return false; |
923 | 0 | } |
924 | | |
925 | 0 | const char *pszAPI = |
926 | 0 | CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "API", "AUTO"); |
927 | 0 | if ((EQUAL(pszAPI, "AUTO") || EQUAL(pszAPI, "COVERAGE")) && |
928 | 0 | !osCoverageURL.empty()) |
929 | 0 | { |
930 | 0 | return InitWithCoverageAPI(poOpenInfo, osCoverageURL, dfXMin, dfYMin, |
931 | 0 | dfXMax, dfYMax, oDoc.GetRoot()); |
932 | 0 | } |
933 | 0 | else if ((EQUAL(pszAPI, "AUTO") || EQUAL(pszAPI, "TILES")) && |
934 | 0 | (!osTilesetsMapURL.empty() || !osTilesetsVectorURL.empty())) |
935 | 0 | { |
936 | 0 | bool bRet = false; |
937 | 0 | if (!osTilesetsMapURL.empty()) |
938 | 0 | bRet = InitWithTilesAPI(poOpenInfo, osTilesetsMapURL, true, dfXMin, |
939 | 0 | dfYMin, dfXMax, dfYMax, bBBOXIsInCRS84, |
940 | 0 | oDoc.GetRoot()); |
941 | 0 | if (!bRet && !osTilesetsVectorURL.empty()) |
942 | 0 | bRet = InitWithTilesAPI(poOpenInfo, osTilesetsVectorURL, false, |
943 | 0 | dfXMin, dfYMin, dfXMax, dfYMax, |
944 | 0 | bBBOXIsInCRS84, oDoc.GetRoot()); |
945 | 0 | return bRet; |
946 | 0 | } |
947 | 0 | else if ((EQUAL(pszAPI, "AUTO") || EQUAL(pszAPI, "MAP")) && bFoundMap) |
948 | 0 | { |
949 | 0 | return InitWithMapAPI(poOpenInfo, oRoot, dfXMin, dfYMin, dfXMax, |
950 | 0 | dfYMax); |
951 | 0 | } |
952 | 0 | else if ((EQUAL(pszAPI, "AUTO") || EQUAL(pszAPI, "ITEMS")) && |
953 | 0 | !osSelfURL.empty() && !osItemsURL.empty() && |
954 | 0 | (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0) |
955 | 0 | { |
956 | 0 | m_poOAPIFDS = std::unique_ptr<GDALDataset>(GDALDataset::Open( |
957 | 0 | ("OAPIF_COLLECTION:" + osSelfURL).c_str(), GDAL_OF_VECTOR)); |
958 | 0 | if (m_poOAPIFDS) |
959 | 0 | return true; |
960 | 0 | } |
961 | | |
962 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "API %s requested, but not available", |
963 | 0 | pszAPI); |
964 | 0 | return false; |
965 | 0 | } |
966 | | |
967 | | /************************************************************************/ |
968 | | /* InitFromURL() */ |
969 | | /************************************************************************/ |
970 | | |
971 | | bool OGCAPIDataset::InitFromURL(GDALOpenInfo *poOpenInfo) |
972 | 68 | { |
973 | 68 | const char *pszInitialURL = |
974 | 68 | STARTS_WITH_CI(poOpenInfo->pszFilename, "OGCAPI:") |
975 | 68 | ? poOpenInfo->pszFilename + strlen("OGCAPI:") |
976 | 68 | : poOpenInfo->pszFilename; |
977 | 68 | CPLJSONDocument oDoc; |
978 | 68 | CPLString osURL(pszInitialURL); |
979 | 68 | if (!DownloadJSon(osURL, oDoc)) |
980 | 68 | return false; |
981 | | |
982 | 0 | SetRootURLFromURL(osURL); |
983 | |
|
984 | 0 | auto oCollections = oDoc.GetRoot().GetArray("collections"); |
985 | 0 | if (!oCollections.IsValid()) |
986 | 0 | { |
987 | 0 | if (!oDoc.GetRoot().GetArray("extent").IsValid()) |
988 | 0 | { |
989 | | // If there is no "collections" or "extent" member, then it is |
990 | | // perhaps a landing page |
991 | 0 | const auto oLinks = oDoc.GetRoot().GetArray("links"); |
992 | 0 | osURL.clear(); |
993 | 0 | for (const auto &oLink : oLinks) |
994 | 0 | { |
995 | 0 | if (oLink["rel"].ToString() == "data" && |
996 | 0 | oLink["type"].ToString() == MEDIA_TYPE_JSON) |
997 | 0 | { |
998 | 0 | osURL = BuildURL(oLink["href"].ToString()); |
999 | 0 | break; |
1000 | 0 | } |
1001 | 0 | else if (oLink["rel"].ToString() == "data" && |
1002 | 0 | !oLink.GetObj("type").IsValid()) |
1003 | 0 | { |
1004 | 0 | osURL = BuildURL(oLink["href"].ToString()); |
1005 | 0 | } |
1006 | 0 | } |
1007 | 0 | if (!osURL.empty()) |
1008 | 0 | { |
1009 | 0 | if (!DownloadJSon(osURL, oDoc)) |
1010 | 0 | return false; |
1011 | 0 | oCollections = oDoc.GetRoot().GetArray("collections"); |
1012 | 0 | } |
1013 | 0 | } |
1014 | | |
1015 | 0 | if (!oCollections.IsValid()) |
1016 | 0 | { |
1017 | | // This is hopefully a /collections/{id} response |
1018 | 0 | return InitFromCollection(poOpenInfo, oDoc); |
1019 | 0 | } |
1020 | 0 | } |
1021 | | |
1022 | | // This is a /collections response |
1023 | 0 | CPLStringList aosSubdatasets; |
1024 | 0 | for (const auto &oCollection : oCollections) |
1025 | 0 | { |
1026 | 0 | const auto osTitle = oCollection.GetString("title"); |
1027 | 0 | const auto osLayerDataType = oCollection.GetString("layerDataType"); |
1028 | | // CPLDebug("OGCAPI", "%s: %s", osTitle.c_str(), |
1029 | | // osLayerDataType.c_str()); |
1030 | 0 | if (!osLayerDataType.empty() && |
1031 | 0 | (EQUAL(osLayerDataType.c_str(), "Raster") || |
1032 | 0 | EQUAL(osLayerDataType.c_str(), "Coverage")) && |
1033 | 0 | (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) == 0) |
1034 | 0 | { |
1035 | 0 | continue; |
1036 | 0 | } |
1037 | 0 | if (!osLayerDataType.empty() && |
1038 | 0 | EQUAL(osLayerDataType.c_str(), "Vector") && |
1039 | 0 | (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) == 0) |
1040 | 0 | { |
1041 | 0 | continue; |
1042 | 0 | } |
1043 | 0 | osURL.clear(); |
1044 | 0 | const auto oLinks = oCollection.GetArray("links"); |
1045 | 0 | for (const auto &oLink : oLinks) |
1046 | 0 | { |
1047 | 0 | if (oLink["rel"].ToString() == "self" && |
1048 | 0 | oLink["type"].ToString() == "application/json") |
1049 | 0 | { |
1050 | 0 | osURL = BuildURL(oLink["href"].ToString()); |
1051 | 0 | break; |
1052 | 0 | } |
1053 | 0 | else if (oLink["rel"].ToString() == "self" && |
1054 | 0 | oLink.GetString("type").empty()) |
1055 | 0 | { |
1056 | 0 | osURL = BuildURL(oLink["href"].ToString()); |
1057 | 0 | } |
1058 | 0 | } |
1059 | 0 | if (osURL.empty()) |
1060 | 0 | { |
1061 | 0 | continue; |
1062 | 0 | } |
1063 | 0 | const int nIdx = 1 + aosSubdatasets.size() / 2; |
1064 | 0 | aosSubdatasets.AddNameValue(CPLSPrintf("SUBDATASET_%d_NAME", nIdx), |
1065 | 0 | CPLSPrintf("OGCAPI:%s", osURL.c_str())); |
1066 | 0 | aosSubdatasets.AddNameValue( |
1067 | 0 | CPLSPrintf("SUBDATASET_%d_DESC", nIdx), |
1068 | 0 | CPLSPrintf("Collection %s", osTitle.c_str())); |
1069 | 0 | } |
1070 | 0 | SetMetadata(aosSubdatasets.List(), "SUBDATASETS"); |
1071 | |
|
1072 | 0 | return true; |
1073 | 0 | } |
1074 | | |
1075 | | /************************************************************************/ |
1076 | | /* SelectImageURL() */ |
1077 | | /************************************************************************/ |
1078 | | |
1079 | | static const std::pair<std::string, std::string> |
1080 | | SelectImageURL(const char *const *papszOptionOptions, |
1081 | | std::map<std::string, std::string> &oMapItemUrls) |
1082 | 0 | { |
1083 | | // Map IMAGE_FORMATS to their content types. Would be nice if this was |
1084 | | // globally defined someplace |
1085 | 0 | const std::map<std::string, std::vector<std::string>> |
1086 | 0 | oFormatContentTypeMap = { |
1087 | 0 | {"AUTO", |
1088 | 0 | {"image/png", "image/jpeg", "image/tiff; application=geotiff"}}, |
1089 | 0 | {"PNG_PREFERRED", |
1090 | 0 | {"image/png", "image/jpeg", "image/tiff; application=geotiff"}}, |
1091 | 0 | {"JPEG_PREFERRED", |
1092 | 0 | {"image/jpeg", "image/png", "image/tiff; application=geotiff"}}, |
1093 | 0 | {"PNG", {"image/png"}}, |
1094 | 0 | {"JPEG", {"image/jpeg"}}, |
1095 | 0 | {"GEOTIFF", {"image/tiff; application=geotiff"}}}; |
1096 | | |
1097 | | // Get the IMAGE_FORMAT |
1098 | 0 | const std::string osFormat = |
1099 | 0 | CSLFetchNameValueDef(papszOptionOptions, "IMAGE_FORMAT", "AUTO"); |
1100 | | |
1101 | | // Get a list of content types we will search for in priority order based on IMAGE_FORMAT |
1102 | 0 | auto iterFormat = oFormatContentTypeMap.find(osFormat); |
1103 | 0 | if (iterFormat == oFormatContentTypeMap.end()) |
1104 | 0 | { |
1105 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1106 | 0 | "Unknown IMAGE_FORMAT specified: %s", osFormat.c_str()); |
1107 | 0 | return std::pair<std::string, CPLString>(); |
1108 | 0 | } |
1109 | 0 | std::vector<std::string> oContentTypes = iterFormat->second; |
1110 | | |
1111 | | // For "special" IMAGE_FORMATS we will also accept additional content types |
1112 | | // specified by the server. Note that this will likely result in having |
1113 | | // some content types duplicated in the vector but that is fine. |
1114 | 0 | if (osFormat == "AUTO" || osFormat == "PNG_PREFERRED" || |
1115 | 0 | osFormat == "JPEG_PREFERRED") |
1116 | 0 | { |
1117 | 0 | std::transform(oMapItemUrls.begin(), oMapItemUrls.end(), |
1118 | 0 | std::back_inserter(oContentTypes), |
1119 | 0 | [](const auto &pair) -> const std::string & |
1120 | 0 | { return pair.first; }); |
1121 | 0 | } |
1122 | | |
1123 | | // Loop over each content type - return the first one we find |
1124 | 0 | for (auto &oContentType : oContentTypes) |
1125 | 0 | { |
1126 | 0 | auto iterContentType = oMapItemUrls.find(oContentType); |
1127 | 0 | if (iterContentType != oMapItemUrls.end()) |
1128 | 0 | { |
1129 | 0 | return *iterContentType; |
1130 | 0 | } |
1131 | 0 | } |
1132 | | |
1133 | 0 | if (osFormat != "AUTO") |
1134 | 0 | { |
1135 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1136 | 0 | "Server does not support specified IMAGE_FORMAT: %s", |
1137 | 0 | osFormat.c_str()); |
1138 | 0 | } |
1139 | 0 | return std::pair<std::string, CPLString>(); |
1140 | 0 | } |
1141 | | |
1142 | | /************************************************************************/ |
1143 | | /* SelectVectorFormatURL() */ |
1144 | | /************************************************************************/ |
1145 | | |
1146 | | static const CPLString |
1147 | | SelectVectorFormatURL(const char *const *papszOptionOptions, |
1148 | | const CPLString &osMVT_URL, |
1149 | | const CPLString &osGEOJSON_URL) |
1150 | 0 | { |
1151 | 0 | const char *pszFormat = |
1152 | 0 | CSLFetchNameValueDef(papszOptionOptions, "VECTOR_FORMAT", "AUTO"); |
1153 | 0 | if (EQUAL(pszFormat, "AUTO") || EQUAL(pszFormat, "MVT_PREFERRED")) |
1154 | 0 | return !osMVT_URL.empty() ? osMVT_URL : osGEOJSON_URL; |
1155 | 0 | else if (EQUAL(pszFormat, "MVT")) |
1156 | 0 | return osMVT_URL; |
1157 | 0 | else if (EQUAL(pszFormat, "GEOJSON")) |
1158 | 0 | return osGEOJSON_URL; |
1159 | 0 | else if (EQUAL(pszFormat, "GEOJSON_PREFERRED")) |
1160 | 0 | return !osGEOJSON_URL.empty() ? osGEOJSON_URL : osMVT_URL; |
1161 | 0 | return CPLString(); |
1162 | 0 | } |
1163 | | |
1164 | | /************************************************************************/ |
1165 | | /* InitWithMapAPI() */ |
1166 | | /************************************************************************/ |
1167 | | |
1168 | | bool OGCAPIDataset::InitWithMapAPI(GDALOpenInfo *poOpenInfo, |
1169 | | const CPLJSONObject &oRoot, double dfXMin, |
1170 | | double dfYMin, double dfXMax, double dfYMax) |
1171 | 0 | { |
1172 | 0 | auto oLinks = oRoot["links"].ToArray(); |
1173 | | |
1174 | | // Key - mime type, Value url |
1175 | 0 | std::map<std::string, std::string> oMapItemUrls; |
1176 | |
|
1177 | 0 | for (const auto &oLink : oLinks) |
1178 | 0 | { |
1179 | 0 | if (oLink["rel"].ToString() == |
1180 | 0 | "http://www.opengis.net/def/rel/ogc/1.0/map" && |
1181 | 0 | oLink["type"].IsValid()) |
1182 | 0 | { |
1183 | 0 | oMapItemUrls[oLink["type"].ToString()] = |
1184 | 0 | BuildURL(oLink["href"].ToString()); |
1185 | 0 | } |
1186 | 0 | else |
1187 | 0 | { |
1188 | | // For lack of additional information assume we are getting some bytes |
1189 | 0 | oMapItemUrls["application/octet-stream"] = |
1190 | 0 | BuildURL(oLink["href"].ToString()); |
1191 | 0 | } |
1192 | 0 | } |
1193 | |
|
1194 | 0 | const std::pair<std::string, std::string> oContentUrlPair = |
1195 | 0 | SelectImageURL(poOpenInfo->papszOpenOptions, oMapItemUrls); |
1196 | 0 | const std::string osContentType = oContentUrlPair.first; |
1197 | 0 | const std::string osImageURL = oContentUrlPair.second; |
1198 | |
|
1199 | 0 | if (osImageURL.empty()) |
1200 | 0 | { |
1201 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1202 | 0 | "Cannot find link to tileset items"); |
1203 | 0 | return false; |
1204 | 0 | } |
1205 | | |
1206 | 0 | int l_nBands = FigureBands(osContentType, osImageURL); |
1207 | 0 | int nOverviewCount = 0; |
1208 | 0 | int nLargestDim = std::max(nRasterXSize, nRasterYSize); |
1209 | 0 | while (nLargestDim > 256) |
1210 | 0 | { |
1211 | 0 | nOverviewCount++; |
1212 | 0 | nLargestDim /= 2; |
1213 | 0 | } |
1214 | |
|
1215 | 0 | m_oSRS.importFromEPSG(4326); |
1216 | 0 | m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
1217 | |
|
1218 | 0 | const bool bCache = CPLTestBool( |
1219 | 0 | CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "CACHE", "YES")); |
1220 | 0 | const int nMaxConnections = atoi( |
1221 | 0 | CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "MAX_CONNECTIONS", |
1222 | 0 | CPLGetConfigOption("GDAL_MAX_CONNECTIONS", "5"))); |
1223 | 0 | CPLString osWMS_XML; |
1224 | 0 | char *pszEscapedURL = CPLEscapeString(osImageURL.c_str(), -1, CPLES_XML); |
1225 | 0 | osWMS_XML.Printf("<GDAL_WMS>" |
1226 | 0 | " <Service name=\"OGCAPIMaps\">" |
1227 | 0 | " <ServerUrl>%s</ServerUrl>" |
1228 | 0 | " </Service>" |
1229 | 0 | " <DataWindow>" |
1230 | 0 | " <UpperLeftX>%.17g</UpperLeftX>" |
1231 | 0 | " <UpperLeftY>%.17g</UpperLeftY>" |
1232 | 0 | " <LowerRightX>%.17g</LowerRightX>" |
1233 | 0 | " <LowerRightY>%.17g</LowerRightY>" |
1234 | 0 | " <SizeX>%d</SizeX>" |
1235 | 0 | " <SizeY>%d</SizeY>" |
1236 | 0 | " </DataWindow>" |
1237 | 0 | " <OverviewCount>%d</OverviewCount>" |
1238 | 0 | " <BlockSizeX>256</BlockSizeX>" |
1239 | 0 | " <BlockSizeY>256</BlockSizeY>" |
1240 | 0 | " <BandsCount>%d</BandsCount>" |
1241 | 0 | " <MaxConnections>%d</MaxConnections>" |
1242 | 0 | " %s" |
1243 | 0 | "</GDAL_WMS>", |
1244 | 0 | pszEscapedURL, dfXMin, dfYMax, dfXMax, dfYMin, |
1245 | 0 | nRasterXSize, nRasterYSize, nOverviewCount, l_nBands, |
1246 | 0 | nMaxConnections, bCache ? "<Cache />" : ""); |
1247 | 0 | CPLFree(pszEscapedURL); |
1248 | 0 | CPLDebug("OGCAPI", "%s", osWMS_XML.c_str()); |
1249 | 0 | m_poWMSDS.reset( |
1250 | 0 | GDALDataset::Open(osWMS_XML, GDAL_OF_RASTER | GDAL_OF_INTERNAL)); |
1251 | 0 | if (m_poWMSDS == nullptr) |
1252 | 0 | return false; |
1253 | | |
1254 | 0 | for (int i = 1; i <= m_poWMSDS->GetRasterCount(); i++) |
1255 | 0 | { |
1256 | 0 | SetBand(i, new OGCAPIMapWrapperBand(this, i)); |
1257 | 0 | } |
1258 | 0 | SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE"); |
1259 | |
|
1260 | 0 | return true; |
1261 | 0 | } |
1262 | | |
1263 | | /************************************************************************/ |
1264 | | /* InitWithCoverageAPI() */ |
1265 | | /************************************************************************/ |
1266 | | |
1267 | | bool OGCAPIDataset::InitWithCoverageAPI(GDALOpenInfo *poOpenInfo, |
1268 | | const CPLString &osCoverageURL, |
1269 | | double dfXMin, double dfYMin, |
1270 | | double dfXMax, double dfYMax, |
1271 | | const CPLJSONObject &oJsonCollection) |
1272 | 0 | { |
1273 | 0 | int l_nBands = 1; |
1274 | 0 | GDALDataType eDT = GDT_Float32; |
1275 | |
|
1276 | 0 | auto oRangeType = oJsonCollection["rangeType"]; |
1277 | 0 | if (!oRangeType.IsValid()) |
1278 | 0 | oRangeType = oJsonCollection["rangetype"]; |
1279 | |
|
1280 | 0 | auto oDomainSet = oJsonCollection["domainset"]; |
1281 | 0 | if (!oDomainSet.IsValid()) |
1282 | 0 | oDomainSet = oJsonCollection["domainSet"]; |
1283 | |
|
1284 | 0 | if (!oRangeType.IsValid() || !oDomainSet.IsValid()) |
1285 | 0 | { |
1286 | 0 | auto oLinks = oJsonCollection.GetArray("links"); |
1287 | 0 | for (const auto &oLink : oLinks) |
1288 | 0 | { |
1289 | 0 | const auto osRel = oLink.GetString("rel"); |
1290 | 0 | const auto osType = oLink.GetString("type"); |
1291 | 0 | if (osRel == "http://www.opengis.net/def/rel/ogc/1.0/" |
1292 | 0 | "coverage-domainset" && |
1293 | 0 | (osType == "application/json" || osType.empty())) |
1294 | 0 | { |
1295 | 0 | CPLString osURL = BuildURL(oLink["href"].ToString()); |
1296 | 0 | CPLJSONDocument oDoc; |
1297 | 0 | if (DownloadJSon(osURL.c_str(), oDoc)) |
1298 | 0 | { |
1299 | 0 | oDomainSet = oDoc.GetRoot(); |
1300 | 0 | } |
1301 | 0 | } |
1302 | 0 | else if (osRel == "http://www.opengis.net/def/rel/ogc/1.0/" |
1303 | 0 | "coverage-rangetype" && |
1304 | 0 | (osType == "application/json" || osType.empty())) |
1305 | 0 | { |
1306 | 0 | CPLString osURL = BuildURL(oLink["href"].ToString()); |
1307 | 0 | CPLJSONDocument oDoc; |
1308 | 0 | if (DownloadJSon(osURL.c_str(), oDoc)) |
1309 | 0 | { |
1310 | 0 | oRangeType = oDoc.GetRoot(); |
1311 | 0 | } |
1312 | 0 | } |
1313 | 0 | } |
1314 | 0 | } |
1315 | |
|
1316 | 0 | if (oRangeType.IsValid()) |
1317 | 0 | { |
1318 | 0 | auto oField = oRangeType.GetArray("field"); |
1319 | 0 | if (oField.IsValid()) |
1320 | 0 | { |
1321 | 0 | l_nBands = oField.Size(); |
1322 | | // Such as in https://maps.gnosis.earth/ogcapi/collections/NaturalEarth:raster:HYP_HR_SR_OB_DR/coverage/rangetype?f=json |
1323 | | // https://github.com/opengeospatial/coverage-implementation-schema/blob/main/standard/schemas/1.1/json/examples/generalGrid/2D_regular.json |
1324 | 0 | std::string osDataType = |
1325 | 0 | oField[0].GetString("encodingInfo/dataType"); |
1326 | 0 | if (osDataType.empty()) |
1327 | 0 | { |
1328 | | // Older way? |
1329 | 0 | osDataType = oField[0].GetString("definition"); |
1330 | 0 | } |
1331 | 0 | static const std::map<std::string, GDALDataType> oMapTypes = { |
1332 | | // https://edc-oapi.dev.hub.eox.at/oapi/collections/S2L2A |
1333 | 0 | {"UINT8", GDT_UInt8}, |
1334 | 0 | {"INT16", GDT_Int16}, |
1335 | 0 | {"UINT16", GDT_UInt16}, |
1336 | 0 | {"INT32", GDT_Int32}, |
1337 | 0 | {"UINT32", GDT_UInt32}, |
1338 | 0 | {"FLOAT32", GDT_Float32}, |
1339 | 0 | {"FLOAT64", GDT_Float64}, |
1340 | | // https://test.cubewerx.com/cubewerx/cubeserv/demo/ogcapi/Daraa/collections/Daraa_DTED/coverage/rangetype?f=json |
1341 | 0 | {"ogcType:unsignedByte", GDT_UInt8}, |
1342 | 0 | {"ogcType:signedShort", GDT_Int16}, |
1343 | 0 | {"ogcType:unsignedShort", GDT_UInt16}, |
1344 | 0 | {"ogcType:signedInt", GDT_Int32}, |
1345 | 0 | {"ogcType:unsignedInt", GDT_UInt32}, |
1346 | 0 | {"ogcType:float32", GDT_Float32}, |
1347 | 0 | {"ogcType:float64", GDT_Float64}, |
1348 | 0 | {"ogcType:double", GDT_Float64}, |
1349 | 0 | }; |
1350 | | // 08-094r1_SWE_Common_Data_Model_2.0_Submission_Package.pdf page |
1351 | | // 112 |
1352 | 0 | auto oIter = oMapTypes.find( |
1353 | 0 | CPLString(osDataType) |
1354 | 0 | .replaceAll("http://www.opengis.net/def/dataType/OGC/0/", |
1355 | 0 | "ogcType:")); |
1356 | 0 | if (oIter != oMapTypes.end()) |
1357 | 0 | { |
1358 | 0 | eDT = oIter->second; |
1359 | 0 | } |
1360 | 0 | else |
1361 | 0 | { |
1362 | 0 | CPLDebug("OGCAPI", "Unhandled data type: %s", |
1363 | 0 | osDataType.c_str()); |
1364 | 0 | } |
1365 | 0 | } |
1366 | 0 | } |
1367 | |
|
1368 | 0 | CPLString osXAxisName; |
1369 | 0 | CPLString osYAxisName; |
1370 | 0 | if (oDomainSet.IsValid()) |
1371 | 0 | { |
1372 | 0 | auto oAxisLabels = oDomainSet["generalGrid"]["axisLabels"].ToArray(); |
1373 | 0 | if (oAxisLabels.IsValid() && oAxisLabels.Size() >= 2) |
1374 | 0 | { |
1375 | 0 | osXAxisName = oAxisLabels[0].ToString(); |
1376 | 0 | osYAxisName = oAxisLabels[1].ToString(); |
1377 | 0 | } |
1378 | |
|
1379 | 0 | auto oAxis = oDomainSet["generalGrid"]["axis"].ToArray(); |
1380 | 0 | if (oAxis.IsValid() && oAxis.Size() >= 2) |
1381 | 0 | { |
1382 | 0 | double dfXRes = std::abs(oAxis[0].GetDouble("resolution")); |
1383 | 0 | double dfYRes = std::abs(oAxis[1].GetDouble("resolution")); |
1384 | |
|
1385 | 0 | dfXMin = oAxis[0].GetDouble("lowerBound"); |
1386 | 0 | dfXMax = oAxis[0].GetDouble("upperBound"); |
1387 | 0 | dfYMin = oAxis[1].GetDouble("lowerBound"); |
1388 | 0 | dfYMax = oAxis[1].GetDouble("upperBound"); |
1389 | |
|
1390 | 0 | if (osXAxisName == "Lat") |
1391 | 0 | { |
1392 | 0 | std::swap(dfXRes, dfYRes); |
1393 | 0 | std::swap(dfXMin, dfYMin); |
1394 | 0 | std::swap(dfXMax, dfYMax); |
1395 | 0 | } |
1396 | |
|
1397 | 0 | double dfXSize = (dfXMax - dfXMin) / dfXRes; |
1398 | 0 | double dfYSize = (dfYMax - dfYMin) / dfYRes; |
1399 | 0 | while (dfXSize > INT_MAX || dfYSize > INT_MAX) |
1400 | 0 | { |
1401 | 0 | dfXSize /= 2; |
1402 | 0 | dfYSize /= 2; |
1403 | 0 | } |
1404 | |
|
1405 | 0 | nRasterXSize = std::max(1, static_cast<int>(0.5 + dfXSize)); |
1406 | 0 | nRasterYSize = std::max(1, static_cast<int>(0.5 + dfYSize)); |
1407 | 0 | m_gt[0] = dfXMin; |
1408 | 0 | m_gt[1] = (dfXMax - dfXMin) / nRasterXSize; |
1409 | 0 | m_gt[3] = dfYMax; |
1410 | 0 | m_gt[5] = -(dfYMax - dfYMin) / nRasterYSize; |
1411 | 0 | } |
1412 | |
|
1413 | 0 | OGRSpatialReference oSRS; |
1414 | 0 | std::string srsName(oDomainSet["generalGrid"].GetString("srsName")); |
1415 | 0 | bool bSwap = false; |
1416 | | |
1417 | | // Strip of time component, as found in |
1418 | | // OGCAPI:https://maps.ecere.com/ogcapi/collections/blueMarble |
1419 | 0 | if (STARTS_WITH(srsName.c_str(), |
1420 | 0 | "http://www.opengis.net/def/crs-compound?1=") && |
1421 | 0 | srsName.find("&2=http://www.opengis.net/def/crs/OGC/0/") != |
1422 | 0 | std::string::npos) |
1423 | 0 | { |
1424 | 0 | srsName = srsName.substr( |
1425 | 0 | strlen("http://www.opengis.net/def/crs-compound?1=")); |
1426 | 0 | srsName.resize(srsName.find("&2=")); |
1427 | 0 | } |
1428 | |
|
1429 | 0 | if (oSRS.SetFromUserInput( |
1430 | 0 | srsName.c_str(), |
1431 | 0 | OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get()) == |
1432 | 0 | OGRERR_NONE) |
1433 | 0 | { |
1434 | 0 | if (oSRS.EPSGTreatsAsLatLong() || |
1435 | 0 | oSRS.EPSGTreatsAsNorthingEasting()) |
1436 | 0 | { |
1437 | 0 | bSwap = true; |
1438 | 0 | } |
1439 | 0 | } |
1440 | 0 | else if (srsName == |
1441 | 0 | "https://ows.rasdaman.org/def/crs/EPSG/0/4326") // HACK |
1442 | 0 | { |
1443 | 0 | bSwap = true; |
1444 | 0 | } |
1445 | 0 | if (bSwap) |
1446 | 0 | { |
1447 | 0 | std::swap(osXAxisName, osYAxisName); |
1448 | 0 | } |
1449 | 0 | } |
1450 | |
|
1451 | 0 | int nOverviewCount = 0; |
1452 | 0 | int nLargestDim = std::max(nRasterXSize, nRasterYSize); |
1453 | 0 | while (nLargestDim > 256) |
1454 | 0 | { |
1455 | 0 | nOverviewCount++; |
1456 | 0 | nLargestDim /= 2; |
1457 | 0 | } |
1458 | |
|
1459 | 0 | m_oSRS.importFromEPSG(4326); |
1460 | 0 | m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
1461 | |
|
1462 | 0 | CPLString osCoverageURLModified(osCoverageURL); |
1463 | 0 | if (osCoverageURLModified.find('&') == std::string::npos && |
1464 | 0 | osCoverageURLModified.find('?') == std::string::npos) |
1465 | 0 | { |
1466 | 0 | osCoverageURLModified += '?'; |
1467 | 0 | } |
1468 | 0 | else |
1469 | 0 | { |
1470 | 0 | osCoverageURLModified += '&'; |
1471 | 0 | } |
1472 | |
|
1473 | 0 | if (!osXAxisName.empty() && !osYAxisName.empty()) |
1474 | 0 | { |
1475 | 0 | osCoverageURLModified += |
1476 | 0 | CPLSPrintf("subset=%s(${minx}:${maxx}),%s(${miny}:${maxy})&" |
1477 | 0 | "scaleSize=%s(${width}),%s(${height})", |
1478 | 0 | osXAxisName.c_str(), osYAxisName.c_str(), |
1479 | 0 | osXAxisName.c_str(), osYAxisName.c_str()); |
1480 | 0 | } |
1481 | 0 | else |
1482 | 0 | { |
1483 | | // FIXME |
1484 | 0 | osCoverageURLModified += "bbox=${minx},${miny},${maxx},${maxy}&" |
1485 | 0 | "scaleSize=Lat(${height}),Long(${width})"; |
1486 | 0 | } |
1487 | |
|
1488 | 0 | const bool bCache = CPLTestBool( |
1489 | 0 | CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "CACHE", "YES")); |
1490 | 0 | const int nMaxConnections = atoi( |
1491 | 0 | CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "MAX_CONNECTIONS", |
1492 | 0 | CPLGetConfigOption("GDAL_MAX_CONNECTIONS", "5"))); |
1493 | 0 | CPLString osWMS_XML; |
1494 | 0 | char *pszEscapedURL = CPLEscapeString(osCoverageURLModified, -1, CPLES_XML); |
1495 | 0 | std::string osAccept("<Accept>image/tiff;application=geotiff</Accept>"); |
1496 | 0 | osWMS_XML.Printf("<GDAL_WMS>" |
1497 | 0 | " <Service name=\"OGCAPICoverage\">" |
1498 | 0 | " <ServerUrl>%s</ServerUrl>" |
1499 | 0 | " </Service>" |
1500 | 0 | " <DataWindow>" |
1501 | 0 | " <UpperLeftX>%.17g</UpperLeftX>" |
1502 | 0 | " <UpperLeftY>%.17g</UpperLeftY>" |
1503 | 0 | " <LowerRightX>%.17g</LowerRightX>" |
1504 | 0 | " <LowerRightY>%.17g</LowerRightY>" |
1505 | 0 | " <SizeX>%d</SizeX>" |
1506 | 0 | " <SizeY>%d</SizeY>" |
1507 | 0 | " </DataWindow>" |
1508 | 0 | " <OverviewCount>%d</OverviewCount>" |
1509 | 0 | " <BlockSizeX>256</BlockSizeX>" |
1510 | 0 | " <BlockSizeY>256</BlockSizeY>" |
1511 | 0 | " <BandsCount>%d</BandsCount>" |
1512 | 0 | " <DataType>%s</DataType>" |
1513 | 0 | " <MaxConnections>%d</MaxConnections>" |
1514 | 0 | " %s" |
1515 | 0 | " %s" |
1516 | 0 | "</GDAL_WMS>", |
1517 | 0 | pszEscapedURL, dfXMin, dfYMax, dfXMax, dfYMin, |
1518 | 0 | nRasterXSize, nRasterYSize, nOverviewCount, l_nBands, |
1519 | 0 | GDALGetDataTypeName(eDT), nMaxConnections, |
1520 | 0 | osAccept.c_str(), bCache ? "<Cache />" : ""); |
1521 | 0 | CPLFree(pszEscapedURL); |
1522 | 0 | CPLDebug("OGCAPI", "%s", osWMS_XML.c_str()); |
1523 | 0 | m_poWMSDS.reset( |
1524 | 0 | GDALDataset::Open(osWMS_XML, GDAL_OF_RASTER | GDAL_OF_INTERNAL)); |
1525 | 0 | if (m_poWMSDS == nullptr) |
1526 | 0 | return false; |
1527 | | |
1528 | 0 | for (int i = 1; i <= m_poWMSDS->GetRasterCount(); i++) |
1529 | 0 | { |
1530 | 0 | SetBand(i, new OGCAPIMapWrapperBand(this, i)); |
1531 | 0 | } |
1532 | 0 | SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE"); |
1533 | |
|
1534 | 0 | return true; |
1535 | 0 | } |
1536 | | |
1537 | | /************************************************************************/ |
1538 | | /* OGCAPIMapWrapperBand() */ |
1539 | | /************************************************************************/ |
1540 | | |
1541 | | OGCAPIMapWrapperBand::OGCAPIMapWrapperBand(OGCAPIDataset *poDSIn, int nBandIn) |
1542 | 0 | { |
1543 | 0 | poDS = poDSIn; |
1544 | 0 | nBand = nBandIn; |
1545 | 0 | eDataType = poDSIn->m_poWMSDS->GetRasterBand(nBand)->GetRasterDataType(); |
1546 | 0 | poDSIn->m_poWMSDS->GetRasterBand(nBand)->GetBlockSize(&nBlockXSize, |
1547 | 0 | &nBlockYSize); |
1548 | 0 | } |
1549 | | |
1550 | | /************************************************************************/ |
1551 | | /* IReadBlock() */ |
1552 | | /************************************************************************/ |
1553 | | |
1554 | | CPLErr OGCAPIMapWrapperBand::IReadBlock(int nBlockXOff, int nBlockYOff, |
1555 | | void *pImage) |
1556 | 0 | { |
1557 | 0 | OGCAPIDataset *poGDS = cpl::down_cast<OGCAPIDataset *>(poDS); |
1558 | 0 | return poGDS->m_poWMSDS->GetRasterBand(nBand)->ReadBlock( |
1559 | 0 | nBlockXOff, nBlockYOff, pImage); |
1560 | 0 | } |
1561 | | |
1562 | | /************************************************************************/ |
1563 | | /* IRasterIO() */ |
1564 | | /************************************************************************/ |
1565 | | |
1566 | | CPLErr OGCAPIMapWrapperBand::IRasterIO( |
1567 | | GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize, |
1568 | | void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType, |
1569 | | GSpacing nPixelSpace, GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg) |
1570 | 0 | { |
1571 | 0 | OGCAPIDataset *poGDS = cpl::down_cast<OGCAPIDataset *>(poDS); |
1572 | 0 | return poGDS->m_poWMSDS->GetRasterBand(nBand)->RasterIO( |
1573 | 0 | eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, |
1574 | 0 | eBufType, nPixelSpace, nLineSpace, psExtraArg); |
1575 | 0 | } |
1576 | | |
1577 | | /************************************************************************/ |
1578 | | /* GetOverviewCount() */ |
1579 | | /************************************************************************/ |
1580 | | |
1581 | | int OGCAPIMapWrapperBand::GetOverviewCount() |
1582 | 0 | { |
1583 | 0 | OGCAPIDataset *poGDS = cpl::down_cast<OGCAPIDataset *>(poDS); |
1584 | 0 | return poGDS->m_poWMSDS->GetRasterBand(nBand)->GetOverviewCount(); |
1585 | 0 | } |
1586 | | |
1587 | | /************************************************************************/ |
1588 | | /* GetOverview() */ |
1589 | | /************************************************************************/ |
1590 | | |
1591 | | GDALRasterBand *OGCAPIMapWrapperBand::GetOverview(int nLevel) |
1592 | 0 | { |
1593 | 0 | OGCAPIDataset *poGDS = cpl::down_cast<OGCAPIDataset *>(poDS); |
1594 | 0 | return poGDS->m_poWMSDS->GetRasterBand(nBand)->GetOverview(nLevel); |
1595 | 0 | } |
1596 | | |
1597 | | /************************************************************************/ |
1598 | | /* GetColorInterpretation() */ |
1599 | | /************************************************************************/ |
1600 | | |
1601 | | GDALColorInterp OGCAPIMapWrapperBand::GetColorInterpretation() |
1602 | 0 | { |
1603 | 0 | OGCAPIDataset *poGDS = cpl::down_cast<OGCAPIDataset *>(poDS); |
1604 | | // The WMS driver returns Grey-Alpha for 2 band, RGB(A) for 3 or 4 bands |
1605 | | // Restrict that behavior to Byte only data. |
1606 | 0 | if (eDataType == GDT_UInt8) |
1607 | 0 | return poGDS->m_poWMSDS->GetRasterBand(nBand)->GetColorInterpretation(); |
1608 | 0 | return GCI_Undefined; |
1609 | 0 | } |
1610 | | |
1611 | | /************************************************************************/ |
1612 | | /* ParseXMLSchema() */ |
1613 | | /************************************************************************/ |
1614 | | |
1615 | | static bool |
1616 | | ParseXMLSchema(const std::string &osURL, |
1617 | | std::vector<std::unique_ptr<OGRFieldDefn>> &apoFields, |
1618 | | OGRwkbGeometryType &eGeomType) |
1619 | 0 | { |
1620 | 0 | CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); |
1621 | |
|
1622 | 0 | std::vector<GMLFeatureClass *> apoClasses; |
1623 | 0 | bool bFullyUnderstood = false; |
1624 | 0 | bool bUseSchemaImports = false; |
1625 | 0 | bool bHaveSchema = GMLParseXSD(osURL.c_str(), bUseSchemaImports, apoClasses, |
1626 | 0 | bFullyUnderstood); |
1627 | 0 | if (bHaveSchema && apoClasses.size() == 1) |
1628 | 0 | { |
1629 | 0 | auto poGMLFeatureClass = apoClasses[0]; |
1630 | 0 | if (poGMLFeatureClass->GetGeometryPropertyCount() == 1 && |
1631 | 0 | poGMLFeatureClass->GetGeometryProperty(0)->GetType() != wkbUnknown) |
1632 | 0 | { |
1633 | 0 | eGeomType = static_cast<OGRwkbGeometryType>( |
1634 | 0 | poGMLFeatureClass->GetGeometryProperty(0)->GetType()); |
1635 | 0 | } |
1636 | |
|
1637 | 0 | const int nPropertyCount = poGMLFeatureClass->GetPropertyCount(); |
1638 | 0 | for (int iField = 0; iField < nPropertyCount; iField++) |
1639 | 0 | { |
1640 | 0 | const auto poProperty = poGMLFeatureClass->GetProperty(iField); |
1641 | 0 | OGRFieldSubType eSubType = OFSTNone; |
1642 | 0 | const OGRFieldType eFType = |
1643 | 0 | GML_GetOGRFieldType(poProperty->GetType(), eSubType); |
1644 | |
|
1645 | 0 | const char *pszName = poProperty->GetName(); |
1646 | 0 | auto poField = std::make_unique<OGRFieldDefn>(pszName, eFType); |
1647 | 0 | poField->SetSubType(eSubType); |
1648 | 0 | apoFields.emplace_back(std::move(poField)); |
1649 | 0 | } |
1650 | 0 | delete poGMLFeatureClass; |
1651 | 0 | return true; |
1652 | 0 | } |
1653 | | |
1654 | 0 | for (auto poFeatureClass : apoClasses) |
1655 | 0 | delete poFeatureClass; |
1656 | |
|
1657 | 0 | return false; |
1658 | 0 | } |
1659 | | |
1660 | | /************************************************************************/ |
1661 | | /* InitWithTilesAPI() */ |
1662 | | /************************************************************************/ |
1663 | | |
1664 | | bool OGCAPIDataset::InitWithTilesAPI(GDALOpenInfo *poOpenInfo, |
1665 | | const CPLString &osTilesURL, bool bIsMap, |
1666 | | double dfXMin, double dfYMin, |
1667 | | double dfXMax, double dfYMax, |
1668 | | bool bBBOXIsInCRS84, |
1669 | | const CPLJSONObject &oJsonCollection) |
1670 | 0 | { |
1671 | 0 | CPLJSONDocument oDoc; |
1672 | 0 | if (!DownloadJSon(osTilesURL.c_str(), oDoc)) |
1673 | 0 | return false; |
1674 | | |
1675 | 0 | auto oTilesets = oDoc.GetRoot()["tilesets"].ToArray(); |
1676 | 0 | if (oTilesets.Size() == 0) |
1677 | 0 | { |
1678 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot find tilesets"); |
1679 | 0 | return false; |
1680 | 0 | } |
1681 | 0 | const char *pszRequiredTileMatrixSet = |
1682 | 0 | CSLFetchNameValue(poOpenInfo->papszOpenOptions, "TILEMATRIXSET"); |
1683 | 0 | const char *pszPreferredTileMatrixSet = CSLFetchNameValue( |
1684 | 0 | poOpenInfo->papszOpenOptions, "PREFERRED_TILEMATRIXSET"); |
1685 | 0 | CPLString osTilesetURL; |
1686 | 0 | for (const auto &oTileset : oTilesets) |
1687 | 0 | { |
1688 | 0 | const auto oTileMatrixSetURI = oTileset.GetString("tileMatrixSetURI"); |
1689 | 0 | const auto oLinks = oTileset.GetArray("links"); |
1690 | 0 | if (bIsMap) |
1691 | 0 | { |
1692 | 0 | if (oTileset.GetString("dataType") != "map") |
1693 | 0 | continue; |
1694 | 0 | } |
1695 | 0 | else |
1696 | 0 | { |
1697 | 0 | if (oTileset.GetString("dataType") != "vector") |
1698 | 0 | continue; |
1699 | 0 | } |
1700 | 0 | if (!oLinks.IsValid()) |
1701 | 0 | { |
1702 | 0 | CPLDebug("OGCAPI", "Missing links for a tileset"); |
1703 | 0 | continue; |
1704 | 0 | } |
1705 | 0 | if (pszRequiredTileMatrixSet != nullptr && |
1706 | 0 | oTileMatrixSetURI.find(pszRequiredTileMatrixSet) == |
1707 | 0 | std::string::npos) |
1708 | 0 | { |
1709 | 0 | continue; |
1710 | 0 | } |
1711 | 0 | CPLString osCandidateTilesetURL; |
1712 | 0 | for (const auto &oLink : oLinks) |
1713 | 0 | { |
1714 | 0 | if (oLink["rel"].ToString() == "self") |
1715 | 0 | { |
1716 | 0 | const auto osType = oLink["type"].ToString(); |
1717 | 0 | if (osType == MEDIA_TYPE_JSON) |
1718 | 0 | { |
1719 | 0 | osCandidateTilesetURL = BuildURL(oLink["href"].ToString()); |
1720 | 0 | break; |
1721 | 0 | } |
1722 | 0 | else if (osType.empty()) |
1723 | 0 | { |
1724 | 0 | osCandidateTilesetURL = BuildURL(oLink["href"].ToString()); |
1725 | 0 | } |
1726 | 0 | } |
1727 | 0 | } |
1728 | 0 | if (pszRequiredTileMatrixSet != nullptr) |
1729 | 0 | { |
1730 | 0 | osTilesetURL = std::move(osCandidateTilesetURL); |
1731 | 0 | } |
1732 | 0 | else if (pszPreferredTileMatrixSet != nullptr && |
1733 | 0 | !osCandidateTilesetURL.empty() && |
1734 | 0 | (oTileMatrixSetURI.find(pszPreferredTileMatrixSet) != |
1735 | 0 | std::string::npos)) |
1736 | 0 | { |
1737 | 0 | osTilesetURL = std::move(osCandidateTilesetURL); |
1738 | 0 | } |
1739 | 0 | else if (oTileMatrixSetURI.find("WorldCRS84Quad") != std::string::npos) |
1740 | 0 | { |
1741 | 0 | osTilesetURL = std::move(osCandidateTilesetURL); |
1742 | 0 | } |
1743 | 0 | else if (osTilesetURL.empty()) |
1744 | 0 | { |
1745 | 0 | osTilesetURL = std::move(osCandidateTilesetURL); |
1746 | 0 | } |
1747 | 0 | } |
1748 | 0 | if (osTilesetURL.empty()) |
1749 | 0 | { |
1750 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot find tilematrixset"); |
1751 | 0 | return false; |
1752 | 0 | } |
1753 | | |
1754 | | // Download and parse selected tileset definition |
1755 | 0 | if (!DownloadJSon(osTilesetURL.c_str(), oDoc)) |
1756 | 0 | return false; |
1757 | | |
1758 | 0 | const auto oLinks = oDoc.GetRoot().GetArray("links"); |
1759 | 0 | if (!oLinks.IsValid()) |
1760 | 0 | { |
1761 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Missing links for tileset"); |
1762 | 0 | return false; |
1763 | 0 | } |
1764 | | |
1765 | | // Key - mime type, Value url |
1766 | 0 | std::map<std::string, std::string> oMapItemUrls; |
1767 | 0 | CPLString osMVT_URL; |
1768 | 0 | CPLString osGEOJSON_URL; |
1769 | 0 | CPLString osTilingSchemeURL; |
1770 | 0 | bool bTilingSchemeURLJson = false; |
1771 | |
|
1772 | 0 | for (const auto &oLink : oLinks) |
1773 | 0 | { |
1774 | 0 | const auto osRel = oLink.GetString("rel"); |
1775 | 0 | const auto osType = oLink.GetString("type"); |
1776 | |
|
1777 | 0 | if (!bTilingSchemeURLJson && |
1778 | 0 | osRel == "http://www.opengis.net/def/rel/ogc/1.0/tiling-scheme") |
1779 | 0 | { |
1780 | 0 | if (osType == MEDIA_TYPE_JSON) |
1781 | 0 | { |
1782 | 0 | bTilingSchemeURLJson = true; |
1783 | 0 | osTilingSchemeURL = BuildURL(oLink["href"].ToString()); |
1784 | 0 | } |
1785 | 0 | else if (osType.empty()) |
1786 | 0 | { |
1787 | 0 | osTilingSchemeURL = BuildURL(oLink["href"].ToString()); |
1788 | 0 | } |
1789 | 0 | } |
1790 | 0 | else if (bIsMap) |
1791 | 0 | { |
1792 | 0 | if (osRel == "item" && !osType.empty()) |
1793 | 0 | { |
1794 | 0 | oMapItemUrls[osType] = BuildURL(oLink["href"].ToString()); |
1795 | 0 | } |
1796 | 0 | else if (osRel == "item") |
1797 | 0 | { |
1798 | | // For lack of additional information assume we are getting some bytes |
1799 | 0 | oMapItemUrls["application/octet-stream"] = |
1800 | 0 | BuildURL(oLink["href"].ToString()); |
1801 | 0 | } |
1802 | 0 | } |
1803 | 0 | else |
1804 | 0 | { |
1805 | 0 | if (osRel == "item" && |
1806 | 0 | osType == "application/vnd.mapbox-vector-tile") |
1807 | 0 | { |
1808 | 0 | osMVT_URL = BuildURL(oLink["href"].ToString()); |
1809 | 0 | } |
1810 | 0 | else if (osRel == "item" && osType == "application/geo+json") |
1811 | 0 | { |
1812 | 0 | osGEOJSON_URL = BuildURL(oLink["href"].ToString()); |
1813 | 0 | } |
1814 | 0 | } |
1815 | 0 | } |
1816 | |
|
1817 | 0 | if (osTilingSchemeURL.empty()) |
1818 | 0 | { |
1819 | 0 | CPLError( |
1820 | 0 | CE_Failure, CPLE_AppDefined, |
1821 | 0 | "Cannot find http://www.opengis.net/def/rel/ogc/1.0/tiling-scheme"); |
1822 | 0 | return false; |
1823 | 0 | } |
1824 | | |
1825 | | // Parse tile matrix set limits. |
1826 | 0 | const auto oTileMatrixSetLimits = |
1827 | 0 | oDoc.GetRoot().GetArray("tileMatrixSetLimits"); |
1828 | |
|
1829 | 0 | struct Limits |
1830 | 0 | { |
1831 | 0 | int minTileRow; |
1832 | 0 | int maxTileRow; |
1833 | 0 | int minTileCol; |
1834 | 0 | int maxTileCol; |
1835 | 0 | }; |
1836 | |
|
1837 | 0 | std::map<CPLString, Limits> oMapTileMatrixSetLimits; |
1838 | 0 | if (CPLTestBool( |
1839 | 0 | CPLGetConfigOption("GDAL_OGCAPI_TILEMATRIXSET_LIMITS", "YES"))) |
1840 | 0 | { |
1841 | 0 | for (const auto &jsonLimit : oTileMatrixSetLimits) |
1842 | 0 | { |
1843 | 0 | const auto osTileMatrix = jsonLimit.GetString("tileMatrix"); |
1844 | 0 | if (!osTileMatrix.empty()) |
1845 | 0 | { |
1846 | 0 | Limits limits; |
1847 | 0 | limits.minTileRow = jsonLimit.GetInteger("minTileRow"); |
1848 | 0 | limits.maxTileRow = jsonLimit.GetInteger("maxTileRow"); |
1849 | 0 | limits.minTileCol = jsonLimit.GetInteger("minTileCol"); |
1850 | 0 | limits.maxTileCol = jsonLimit.GetInteger("maxTileCol"); |
1851 | 0 | if (limits.minTileRow > limits.maxTileRow) |
1852 | 0 | continue; // shouldn't happen on valid data |
1853 | 0 | oMapTileMatrixSetLimits[osTileMatrix] = limits; |
1854 | 0 | } |
1855 | 0 | } |
1856 | 0 | } |
1857 | |
|
1858 | 0 | const std::pair<std::string, std::string> oContentUrlPair = |
1859 | 0 | SelectImageURL(poOpenInfo->papszOpenOptions, oMapItemUrls); |
1860 | 0 | const std::string osContentType = oContentUrlPair.first; |
1861 | 0 | const std::string osRasterURL = oContentUrlPair.second; |
1862 | |
|
1863 | 0 | const CPLString osVectorURL = SelectVectorFormatURL( |
1864 | 0 | poOpenInfo->papszOpenOptions, osMVT_URL, osGEOJSON_URL); |
1865 | 0 | if (osRasterURL.empty() && osVectorURL.empty()) |
1866 | 0 | { |
1867 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1868 | 0 | "Cannot find link to PNG, JPEG, MVT or GeoJSON tiles"); |
1869 | 0 | return false; |
1870 | 0 | } |
1871 | | |
1872 | 0 | for (const char *pszNeedle : {"{tileMatrix}", "{tileRow}", "{tileCol}"}) |
1873 | 0 | { |
1874 | 0 | if (!osRasterURL.empty() && |
1875 | 0 | osRasterURL.find(pszNeedle) == std::string::npos) |
1876 | 0 | { |
1877 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s missing in tile URL %s", |
1878 | 0 | pszNeedle, osRasterURL.c_str()); |
1879 | 0 | return false; |
1880 | 0 | } |
1881 | 0 | if (!osVectorURL.empty() && |
1882 | 0 | osVectorURL.find(pszNeedle) == std::string::npos) |
1883 | 0 | { |
1884 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s missing in tile URL %s", |
1885 | 0 | pszNeedle, osVectorURL.c_str()); |
1886 | 0 | return false; |
1887 | 0 | } |
1888 | 0 | } |
1889 | | |
1890 | | // Download and parse tile matrix set definition |
1891 | 0 | if (!DownloadJSon(osTilingSchemeURL.c_str(), oDoc, nullptr, |
1892 | 0 | MEDIA_TYPE_JSON)) |
1893 | 0 | return false; |
1894 | | |
1895 | 0 | auto tms = gdal::TileMatrixSet::parse(oDoc.SaveAsString().c_str()); |
1896 | 0 | if (tms == nullptr) |
1897 | 0 | return false; |
1898 | | |
1899 | 0 | if (m_oSRS.SetFromUserInput( |
1900 | 0 | tms->crs().c_str(), |
1901 | 0 | OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get()) != |
1902 | 0 | OGRERR_NONE) |
1903 | 0 | return false; |
1904 | 0 | const bool bInvertAxis = m_oSRS.EPSGTreatsAsLatLong() != FALSE || |
1905 | 0 | m_oSRS.EPSGTreatsAsNorthingEasting() != FALSE; |
1906 | 0 | m_oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
1907 | |
|
1908 | 0 | bool bFoundSomething = false; |
1909 | 0 | if (!osVectorURL.empty() && (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0) |
1910 | 0 | { |
1911 | 0 | const auto osVectorType = oJsonCollection.GetString("vectorType"); |
1912 | 0 | OGRwkbGeometryType eGeomType = wkbUnknown; |
1913 | 0 | if (osVectorType == "Points") |
1914 | 0 | eGeomType = wkbPoint; |
1915 | 0 | else if (osVectorType == "Lines") |
1916 | 0 | eGeomType = wkbMultiLineString; |
1917 | 0 | else if (osVectorType == "Polygons") |
1918 | 0 | eGeomType = wkbMultiPolygon; |
1919 | |
|
1920 | 0 | CPLString osXMLSchemaURL; |
1921 | 0 | for (const auto &oLink : oJsonCollection.GetArray("links")) |
1922 | 0 | { |
1923 | 0 | if (oLink["rel"].ToString() == "describedBy" && |
1924 | 0 | oLink["type"].ToString() == "text/xml") |
1925 | 0 | { |
1926 | 0 | osXMLSchemaURL = BuildURL(oLink["href"].ToString()); |
1927 | 0 | } |
1928 | 0 | } |
1929 | |
|
1930 | 0 | std::vector<std::unique_ptr<OGRFieldDefn>> apoFields; |
1931 | 0 | bool bGotSchema = false; |
1932 | 0 | if (!osXMLSchemaURL.empty()) |
1933 | 0 | { |
1934 | 0 | bGotSchema = ParseXMLSchema(osXMLSchemaURL, apoFields, eGeomType); |
1935 | 0 | } |
1936 | |
|
1937 | 0 | for (const auto &tileMatrix : tms->tileMatrixList()) |
1938 | 0 | { |
1939 | 0 | const double dfOriX = |
1940 | 0 | bInvertAxis ? tileMatrix.mTopLeftY : tileMatrix.mTopLeftX; |
1941 | 0 | const double dfOriY = |
1942 | 0 | bInvertAxis ? tileMatrix.mTopLeftX : tileMatrix.mTopLeftY; |
1943 | |
|
1944 | 0 | auto oLimitsIter = oMapTileMatrixSetLimits.find(tileMatrix.mId); |
1945 | 0 | if (!oMapTileMatrixSetLimits.empty() && |
1946 | 0 | oLimitsIter == oMapTileMatrixSetLimits.end()) |
1947 | 0 | { |
1948 | | // Tile matrix level not in known limits |
1949 | 0 | continue; |
1950 | 0 | } |
1951 | 0 | int minCol = std::max( |
1952 | 0 | 0, static_cast<int>((dfXMin - dfOriX) / tileMatrix.mResX / |
1953 | 0 | tileMatrix.mTileWidth)); |
1954 | 0 | int maxCol = |
1955 | 0 | std::min(tileMatrix.mMatrixWidth - 1, |
1956 | 0 | static_cast<int>((dfXMax - dfOriX) / tileMatrix.mResX / |
1957 | 0 | tileMatrix.mTileWidth)); |
1958 | 0 | int minRow = std::max( |
1959 | 0 | 0, static_cast<int>((dfOriY - dfYMax) / tileMatrix.mResY / |
1960 | 0 | tileMatrix.mTileHeight)); |
1961 | 0 | int maxRow = |
1962 | 0 | std::min(tileMatrix.mMatrixHeight - 1, |
1963 | 0 | static_cast<int>((dfOriY - dfYMin) / tileMatrix.mResY / |
1964 | 0 | tileMatrix.mTileHeight)); |
1965 | 0 | if (oLimitsIter != oMapTileMatrixSetLimits.end()) |
1966 | 0 | { |
1967 | | // Take into account tileMatrixSetLimits |
1968 | 0 | minCol = std::max(minCol, oLimitsIter->second.minTileCol); |
1969 | 0 | minRow = std::max(minRow, oLimitsIter->second.minTileRow); |
1970 | 0 | maxCol = std::min(maxCol, oLimitsIter->second.maxTileCol); |
1971 | 0 | maxRow = std::min(maxRow, oLimitsIter->second.maxTileRow); |
1972 | 0 | if (minCol > maxCol || minRow > maxRow) |
1973 | 0 | { |
1974 | 0 | continue; |
1975 | 0 | } |
1976 | 0 | } |
1977 | 0 | auto poLayer = |
1978 | 0 | std::unique_ptr<OGCAPITiledLayer>(new OGCAPITiledLayer( |
1979 | 0 | this, bInvertAxis, osVectorURL, osVectorURL == osMVT_URL, |
1980 | 0 | tileMatrix, eGeomType)); |
1981 | 0 | poLayer->SetMinMaxXY(minCol, minRow, maxCol, maxRow); |
1982 | 0 | poLayer->SetExtent(dfXMin, dfYMin, dfXMax, dfYMax); |
1983 | 0 | if (bGotSchema) |
1984 | 0 | poLayer->SetFields(apoFields); |
1985 | 0 | m_apoLayers.emplace_back(std::move(poLayer)); |
1986 | 0 | } |
1987 | |
|
1988 | 0 | bFoundSomething = true; |
1989 | 0 | } |
1990 | |
|
1991 | 0 | if (!osRasterURL.empty() && (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0) |
1992 | 0 | { |
1993 | 0 | if (bBBOXIsInCRS84) |
1994 | 0 | { |
1995 | | // Reproject the extent if needed |
1996 | 0 | OGRSpatialReference oCRS84; |
1997 | 0 | oCRS84.importFromEPSG(4326); |
1998 | 0 | oCRS84.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
1999 | 0 | auto poCT = std::unique_ptr<OGRCoordinateTransformation>( |
2000 | 0 | OGRCreateCoordinateTransformation(&oCRS84, &m_oSRS)); |
2001 | 0 | if (poCT) |
2002 | 0 | { |
2003 | 0 | poCT->TransformBounds(dfXMin, dfYMin, dfXMax, dfYMax, &dfXMin, |
2004 | 0 | &dfYMin, &dfXMax, &dfYMax, 21); |
2005 | 0 | } |
2006 | 0 | } |
2007 | |
|
2008 | 0 | const bool bCache = CPLTestBool( |
2009 | 0 | CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "CACHE", "YES")); |
2010 | 0 | const int nMaxConnections = atoi(CSLFetchNameValueDef( |
2011 | 0 | poOpenInfo->papszOpenOptions, "MAX_CONNECTIONS", |
2012 | 0 | CPLGetConfigOption("GDAL_WMS_MAX_CONNECTIONS", "5"))); |
2013 | 0 | const char *pszTileMatrix = |
2014 | 0 | CSLFetchNameValue(poOpenInfo->papszOpenOptions, "TILEMATRIX"); |
2015 | |
|
2016 | 0 | int l_nBands = FigureBands(osContentType, osRasterURL); |
2017 | |
|
2018 | 0 | for (const auto &tileMatrix : tms->tileMatrixList()) |
2019 | 0 | { |
2020 | 0 | if (pszTileMatrix && !EQUAL(tileMatrix.mId.c_str(), pszTileMatrix)) |
2021 | 0 | { |
2022 | 0 | continue; |
2023 | 0 | } |
2024 | 0 | if (tileMatrix.mTileWidth == 0 || |
2025 | 0 | tileMatrix.mMatrixWidth > INT_MAX / tileMatrix.mTileWidth || |
2026 | 0 | tileMatrix.mTileHeight == 0 || |
2027 | 0 | tileMatrix.mMatrixHeight > INT_MAX / tileMatrix.mTileHeight) |
2028 | 0 | { |
2029 | | // Too resoluted for GDAL limits |
2030 | 0 | break; |
2031 | 0 | } |
2032 | 0 | auto oLimitsIter = oMapTileMatrixSetLimits.find(tileMatrix.mId); |
2033 | 0 | if (!oMapTileMatrixSetLimits.empty() && |
2034 | 0 | oLimitsIter == oMapTileMatrixSetLimits.end()) |
2035 | 0 | { |
2036 | | // Tile matrix level not in known limits |
2037 | 0 | continue; |
2038 | 0 | } |
2039 | | |
2040 | 0 | if (dfXMax - dfXMin < tileMatrix.mResX || |
2041 | 0 | dfYMax - dfYMin < tileMatrix.mResY) |
2042 | 0 | { |
2043 | | // skip levels for which the extent is smaller than the size |
2044 | | // of one pixel |
2045 | 0 | continue; |
2046 | 0 | } |
2047 | | |
2048 | 0 | CPLString osURL(osRasterURL); |
2049 | 0 | osURL.replaceAll("{tileMatrix}", tileMatrix.mId.c_str()); |
2050 | 0 | osURL.replaceAll("{tileRow}", "${y}"); |
2051 | 0 | osURL.replaceAll("{tileCol}", "${x}"); |
2052 | |
|
2053 | 0 | const double dfOriX = |
2054 | 0 | bInvertAxis ? tileMatrix.mTopLeftY : tileMatrix.mTopLeftX; |
2055 | 0 | const double dfOriY = |
2056 | 0 | bInvertAxis ? tileMatrix.mTopLeftX : tileMatrix.mTopLeftY; |
2057 | |
|
2058 | 0 | const auto CreateWMS_XML = |
2059 | 0 | [=, &osURL, &tileMatrix](int minRow, int rowCount, |
2060 | 0 | int nCoalesce, double &dfStripMinY, |
2061 | 0 | double &dfStripMaxY) |
2062 | 0 | { |
2063 | 0 | int minCol = 0; |
2064 | 0 | int maxCol = tileMatrix.mMatrixWidth - 1; |
2065 | 0 | int maxRow = minRow + rowCount - 1; |
2066 | 0 | double dfStripMinX = |
2067 | 0 | dfOriX + minCol * tileMatrix.mTileWidth * tileMatrix.mResX; |
2068 | 0 | double dfStripMaxX = dfOriX + (maxCol + 1) * |
2069 | 0 | tileMatrix.mTileWidth * |
2070 | 0 | tileMatrix.mResX; |
2071 | 0 | dfStripMaxY = |
2072 | 0 | dfOriY - minRow * tileMatrix.mTileHeight * tileMatrix.mResY; |
2073 | 0 | dfStripMinY = dfOriY - (maxRow + 1) * tileMatrix.mTileHeight * |
2074 | 0 | tileMatrix.mResY; |
2075 | 0 | CPLString osWMS_XML; |
2076 | 0 | char *pszEscapedURL = CPLEscapeString(osURL, -1, CPLES_XML); |
2077 | 0 | osWMS_XML.Printf( |
2078 | 0 | "<GDAL_WMS>" |
2079 | 0 | " <Service name=\"TMS\">" |
2080 | 0 | " <ServerUrl>%s</ServerUrl>" |
2081 | 0 | " <TileXMultiplier>%d</TileXMultiplier>" |
2082 | 0 | " </Service>" |
2083 | 0 | " <DataWindow>" |
2084 | 0 | " <UpperLeftX>%.17g</UpperLeftX>" |
2085 | 0 | " <UpperLeftY>%.17g</UpperLeftY>" |
2086 | 0 | " <LowerRightX>%.17g</LowerRightX>" |
2087 | 0 | " <LowerRightY>%.17g</LowerRightY>" |
2088 | 0 | " <TileLevel>0</TileLevel>" |
2089 | 0 | " <TileY>%d</TileY>" |
2090 | 0 | " <SizeX>%d</SizeX>" |
2091 | 0 | " <SizeY>%d</SizeY>" |
2092 | 0 | " <YOrigin>top</YOrigin>" |
2093 | 0 | " </DataWindow>" |
2094 | 0 | " <BlockSizeX>%d</BlockSizeX>" |
2095 | 0 | " <BlockSizeY>%d</BlockSizeY>" |
2096 | 0 | " <BandsCount>%d</BandsCount>" |
2097 | 0 | " <MaxConnections>%d</MaxConnections>" |
2098 | 0 | " %s" |
2099 | 0 | "</GDAL_WMS>", |
2100 | 0 | pszEscapedURL, nCoalesce, dfStripMinX, dfStripMaxY, |
2101 | 0 | dfStripMaxX, dfStripMinY, minRow, |
2102 | 0 | (maxCol - minCol + 1) / nCoalesce * tileMatrix.mTileWidth, |
2103 | 0 | rowCount * tileMatrix.mTileHeight, tileMatrix.mTileWidth, |
2104 | 0 | tileMatrix.mTileHeight, l_nBands, nMaxConnections, |
2105 | 0 | bCache ? "<Cache />" : ""); |
2106 | 0 | CPLFree(pszEscapedURL); |
2107 | 0 | return osWMS_XML; |
2108 | 0 | }; |
2109 | |
|
2110 | 0 | auto vmwl = tileMatrix.mVariableMatrixWidthList; |
2111 | 0 | if (vmwl.empty()) |
2112 | 0 | { |
2113 | 0 | double dfIgnored1, dfIgnored2; |
2114 | 0 | CPLString osWMS_XML(CreateWMS_XML(0, tileMatrix.mMatrixHeight, |
2115 | 0 | 1, dfIgnored1, dfIgnored2)); |
2116 | 0 | if (osWMS_XML.empty()) |
2117 | 0 | continue; |
2118 | 0 | std::unique_ptr<GDALDataset> poDS(GDALDataset::Open( |
2119 | 0 | osWMS_XML, GDAL_OF_RASTER | GDAL_OF_INTERNAL)); |
2120 | 0 | if (!poDS) |
2121 | 0 | return false; |
2122 | 0 | m_apoDatasetsAssembled.emplace_back(std::move(poDS)); |
2123 | 0 | } |
2124 | 0 | else |
2125 | 0 | { |
2126 | 0 | std::sort(vmwl.begin(), vmwl.end(), |
2127 | 0 | [](const gdal::TileMatrixSet::TileMatrix:: |
2128 | 0 | VariableMatrixWidth &a, |
2129 | 0 | const gdal::TileMatrixSet::TileMatrix:: |
2130 | 0 | VariableMatrixWidth &b) |
2131 | 0 | { return a.mMinTileRow < b.mMinTileRow; }); |
2132 | 0 | std::vector<GDALDatasetH> apoStrippedDS; |
2133 | | // For each variable matrix width, create a separate WMS dataset |
2134 | | // with the correspond strip |
2135 | 0 | for (size_t i = 0; i < vmwl.size(); i++) |
2136 | 0 | { |
2137 | 0 | if (vmwl[i].mCoalesce <= 0 || |
2138 | 0 | (tileMatrix.mMatrixWidth % vmwl[i].mCoalesce) != 0) |
2139 | 0 | { |
2140 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2141 | 0 | "Invalid coalesce factor (%d) w.r.t matrix " |
2142 | 0 | "width (%d)", |
2143 | 0 | vmwl[i].mCoalesce, tileMatrix.mMatrixWidth); |
2144 | 0 | return false; |
2145 | 0 | } |
2146 | 0 | { |
2147 | 0 | double dfStripMinY = 0; |
2148 | 0 | double dfStripMaxY = 0; |
2149 | 0 | CPLString osWMS_XML(CreateWMS_XML( |
2150 | 0 | vmwl[i].mMinTileRow, |
2151 | 0 | vmwl[i].mMaxTileRow - vmwl[i].mMinTileRow + 1, |
2152 | 0 | vmwl[i].mCoalesce, dfStripMinY, dfStripMaxY)); |
2153 | 0 | if (osWMS_XML.empty()) |
2154 | 0 | continue; |
2155 | 0 | if (dfStripMinY < dfYMax && dfStripMaxY > dfYMin) |
2156 | 0 | { |
2157 | 0 | std::unique_ptr<GDALDataset> poDS(GDALDataset::Open( |
2158 | 0 | osWMS_XML, GDAL_OF_RASTER | GDAL_OF_INTERNAL)); |
2159 | 0 | if (!poDS) |
2160 | 0 | return false; |
2161 | 0 | m_apoDatasetsElementary.emplace_back( |
2162 | 0 | std::move(poDS)); |
2163 | 0 | apoStrippedDS.emplace_back(GDALDataset::ToHandle( |
2164 | 0 | m_apoDatasetsElementary.back().get())); |
2165 | 0 | } |
2166 | 0 | } |
2167 | | |
2168 | | // Add a strip for non-coalesced tiles |
2169 | 0 | if (i + 1 < vmwl.size() && |
2170 | 0 | vmwl[i].mMaxTileRow + 1 != vmwl[i + 1].mMinTileRow) |
2171 | 0 | { |
2172 | 0 | double dfStripMinY = 0; |
2173 | 0 | double dfStripMaxY = 0; |
2174 | 0 | CPLString osWMS_XML(CreateWMS_XML( |
2175 | 0 | vmwl[i].mMaxTileRow + 1, |
2176 | 0 | vmwl[i + 1].mMinTileRow - vmwl[i].mMaxTileRow - 1, |
2177 | 0 | 1, dfStripMinY, dfStripMaxY)); |
2178 | 0 | if (osWMS_XML.empty()) |
2179 | 0 | continue; |
2180 | 0 | if (dfStripMinY < dfYMax && dfStripMaxY > dfYMin) |
2181 | 0 | { |
2182 | 0 | std::unique_ptr<GDALDataset> poDS(GDALDataset::Open( |
2183 | 0 | osWMS_XML, GDAL_OF_RASTER | GDAL_OF_INTERNAL)); |
2184 | 0 | if (!poDS) |
2185 | 0 | return false; |
2186 | 0 | m_apoDatasetsElementary.emplace_back( |
2187 | 0 | std::move(poDS)); |
2188 | 0 | apoStrippedDS.emplace_back(GDALDataset::ToHandle( |
2189 | 0 | m_apoDatasetsElementary.back().get())); |
2190 | 0 | } |
2191 | 0 | } |
2192 | 0 | } |
2193 | | |
2194 | 0 | if (apoStrippedDS.empty()) |
2195 | 0 | return false; |
2196 | | |
2197 | | // Assemble the strips in a single VRT |
2198 | 0 | CPLStringList argv; |
2199 | 0 | argv.AddString("-resolution"); |
2200 | 0 | argv.AddString("highest"); |
2201 | 0 | GDALBuildVRTOptions *psOptions = |
2202 | 0 | GDALBuildVRTOptionsNew(argv.List(), nullptr); |
2203 | 0 | GDALDatasetH hAssembledDS = GDALBuildVRT( |
2204 | 0 | "", static_cast<int>(apoStrippedDS.size()), |
2205 | 0 | &apoStrippedDS[0], nullptr, psOptions, nullptr); |
2206 | 0 | GDALBuildVRTOptionsFree(psOptions); |
2207 | 0 | if (hAssembledDS == nullptr) |
2208 | 0 | return false; |
2209 | 0 | m_apoDatasetsAssembled.emplace_back( |
2210 | 0 | GDALDataset::FromHandle(hAssembledDS)); |
2211 | 0 | } |
2212 | | |
2213 | 0 | CPLStringList argv; |
2214 | 0 | argv.AddString("-of"); |
2215 | 0 | argv.AddString("VRT"); |
2216 | 0 | argv.AddString("-projwin"); |
2217 | 0 | argv.AddString(CPLSPrintf("%.17g", dfXMin)); |
2218 | 0 | argv.AddString(CPLSPrintf("%.17g", dfYMax)); |
2219 | 0 | argv.AddString(CPLSPrintf("%.17g", dfXMax)); |
2220 | 0 | argv.AddString(CPLSPrintf("%.17g", dfYMin)); |
2221 | 0 | GDALTranslateOptions *psOptions = |
2222 | 0 | GDALTranslateOptionsNew(argv.List(), nullptr); |
2223 | 0 | GDALDatasetH hCroppedDS = GDALTranslate( |
2224 | 0 | "", GDALDataset::ToHandle(m_apoDatasetsAssembled.back().get()), |
2225 | 0 | psOptions, nullptr); |
2226 | 0 | GDALTranslateOptionsFree(psOptions); |
2227 | 0 | if (hCroppedDS == nullptr) |
2228 | 0 | return false; |
2229 | 0 | m_apoDatasetsCropped.emplace_back( |
2230 | 0 | GDALDataset::FromHandle(hCroppedDS)); |
2231 | |
|
2232 | 0 | if (tileMatrix.mResX <= m_gt[1]) |
2233 | 0 | break; |
2234 | 0 | } |
2235 | 0 | if (!m_apoDatasetsCropped.empty()) |
2236 | 0 | { |
2237 | 0 | std::reverse(std::begin(m_apoDatasetsCropped), |
2238 | 0 | std::end(m_apoDatasetsCropped)); |
2239 | 0 | nRasterXSize = m_apoDatasetsCropped[0]->GetRasterXSize(); |
2240 | 0 | nRasterYSize = m_apoDatasetsCropped[0]->GetRasterYSize(); |
2241 | 0 | m_apoDatasetsCropped[0]->GetGeoTransform(m_gt); |
2242 | |
|
2243 | 0 | for (int i = 1; i <= m_apoDatasetsCropped[0]->GetRasterCount(); i++) |
2244 | 0 | { |
2245 | 0 | SetBand(i, new OGCAPITilesWrapperBand(this, i)); |
2246 | 0 | } |
2247 | 0 | SetMetadataItem("INTERLEAVE", "PIXEL", "IMAGE_STRUCTURE"); |
2248 | |
|
2249 | 0 | bFoundSomething = true; |
2250 | 0 | } |
2251 | 0 | } |
2252 | | |
2253 | 0 | return bFoundSomething; |
2254 | 0 | } |
2255 | | |
2256 | | /************************************************************************/ |
2257 | | /* OGCAPITilesWrapperBand() */ |
2258 | | /************************************************************************/ |
2259 | | |
2260 | | OGCAPITilesWrapperBand::OGCAPITilesWrapperBand(OGCAPIDataset *poDSIn, |
2261 | | int nBandIn) |
2262 | 0 | { |
2263 | 0 | poDS = poDSIn; |
2264 | 0 | nBand = nBandIn; |
2265 | 0 | eDataType = poDSIn->m_apoDatasetsCropped[0] |
2266 | 0 | ->GetRasterBand(nBand) |
2267 | 0 | ->GetRasterDataType(); |
2268 | 0 | poDSIn->m_apoDatasetsCropped[0]->GetRasterBand(nBand)->GetBlockSize( |
2269 | 0 | &nBlockXSize, &nBlockYSize); |
2270 | 0 | } |
2271 | | |
2272 | | /************************************************************************/ |
2273 | | /* IReadBlock() */ |
2274 | | /************************************************************************/ |
2275 | | |
2276 | | CPLErr OGCAPITilesWrapperBand::IReadBlock(int nBlockXOff, int nBlockYOff, |
2277 | | void *pImage) |
2278 | 0 | { |
2279 | 0 | OGCAPIDataset *poGDS = cpl::down_cast<OGCAPIDataset *>(poDS); |
2280 | 0 | return poGDS->m_apoDatasetsCropped[0]->GetRasterBand(nBand)->ReadBlock( |
2281 | 0 | nBlockXOff, nBlockYOff, pImage); |
2282 | 0 | } |
2283 | | |
2284 | | /************************************************************************/ |
2285 | | /* IRasterIO() */ |
2286 | | /************************************************************************/ |
2287 | | |
2288 | | CPLErr OGCAPITilesWrapperBand::IRasterIO( |
2289 | | GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize, |
2290 | | void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType, |
2291 | | GSpacing nPixelSpace, GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg) |
2292 | 0 | { |
2293 | 0 | OGCAPIDataset *poGDS = cpl::down_cast<OGCAPIDataset *>(poDS); |
2294 | |
|
2295 | 0 | if ((nBufXSize < nXSize || nBufYSize < nYSize) && |
2296 | 0 | poGDS->m_apoDatasetsCropped.size() > 1 && eRWFlag == GF_Read) |
2297 | 0 | { |
2298 | 0 | int bTried; |
2299 | 0 | CPLErr eErr = TryOverviewRasterIO( |
2300 | 0 | eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, |
2301 | 0 | eBufType, nPixelSpace, nLineSpace, psExtraArg, &bTried); |
2302 | 0 | if (bTried) |
2303 | 0 | return eErr; |
2304 | 0 | } |
2305 | | |
2306 | 0 | return poGDS->m_apoDatasetsCropped[0]->GetRasterBand(nBand)->RasterIO( |
2307 | 0 | eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, |
2308 | 0 | eBufType, nPixelSpace, nLineSpace, psExtraArg); |
2309 | 0 | } |
2310 | | |
2311 | | /************************************************************************/ |
2312 | | /* GetOverviewCount() */ |
2313 | | /************************************************************************/ |
2314 | | |
2315 | | int OGCAPITilesWrapperBand::GetOverviewCount() |
2316 | 0 | { |
2317 | 0 | OGCAPIDataset *poGDS = cpl::down_cast<OGCAPIDataset *>(poDS); |
2318 | 0 | return static_cast<int>(poGDS->m_apoDatasetsCropped.size() - 1); |
2319 | 0 | } |
2320 | | |
2321 | | /************************************************************************/ |
2322 | | /* GetOverview() */ |
2323 | | /************************************************************************/ |
2324 | | |
2325 | | GDALRasterBand *OGCAPITilesWrapperBand::GetOverview(int nLevel) |
2326 | 0 | { |
2327 | 0 | OGCAPIDataset *poGDS = cpl::down_cast<OGCAPIDataset *>(poDS); |
2328 | 0 | if (nLevel < 0 || nLevel >= GetOverviewCount()) |
2329 | 0 | return nullptr; |
2330 | 0 | return poGDS->m_apoDatasetsCropped[nLevel + 1]->GetRasterBand(nBand); |
2331 | 0 | } |
2332 | | |
2333 | | /************************************************************************/ |
2334 | | /* GetColorInterpretation() */ |
2335 | | /************************************************************************/ |
2336 | | |
2337 | | GDALColorInterp OGCAPITilesWrapperBand::GetColorInterpretation() |
2338 | 0 | { |
2339 | 0 | OGCAPIDataset *poGDS = cpl::down_cast<OGCAPIDataset *>(poDS); |
2340 | 0 | return poGDS->m_apoDatasetsCropped[0] |
2341 | 0 | ->GetRasterBand(nBand) |
2342 | 0 | ->GetColorInterpretation(); |
2343 | 0 | } |
2344 | | |
2345 | | /************************************************************************/ |
2346 | | /* IRasterIO() */ |
2347 | | /************************************************************************/ |
2348 | | |
2349 | | CPLErr OGCAPIDataset::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff, |
2350 | | int nXSize, int nYSize, void *pData, |
2351 | | int nBufXSize, int nBufYSize, |
2352 | | GDALDataType eBufType, int nBandCount, |
2353 | | BANDMAP_TYPE panBandMap, GSpacing nPixelSpace, |
2354 | | GSpacing nLineSpace, GSpacing nBandSpace, |
2355 | | GDALRasterIOExtraArg *psExtraArg) |
2356 | 0 | { |
2357 | 0 | if (!m_apoDatasetsCropped.empty()) |
2358 | 0 | { |
2359 | | // Tiles API |
2360 | 0 | if ((nBufXSize < nXSize || nBufYSize < nYSize) && |
2361 | 0 | m_apoDatasetsCropped.size() > 1 && eRWFlag == GF_Read) |
2362 | 0 | { |
2363 | 0 | int bTried; |
2364 | 0 | CPLErr eErr = TryOverviewRasterIO( |
2365 | 0 | eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, |
2366 | 0 | nBufYSize, eBufType, nBandCount, panBandMap, nPixelSpace, |
2367 | 0 | nLineSpace, nBandSpace, psExtraArg, &bTried); |
2368 | 0 | if (bTried) |
2369 | 0 | return eErr; |
2370 | 0 | } |
2371 | | |
2372 | 0 | return m_apoDatasetsCropped[0]->RasterIO( |
2373 | 0 | eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, nBufXSize, nBufYSize, |
2374 | 0 | eBufType, nBandCount, panBandMap, nPixelSpace, nLineSpace, |
2375 | 0 | nBandSpace, psExtraArg); |
2376 | 0 | } |
2377 | 0 | else if (m_poWMSDS) |
2378 | 0 | { |
2379 | | // Maps API |
2380 | 0 | return m_poWMSDS->RasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, |
2381 | 0 | nBufXSize, nBufYSize, eBufType, nBandCount, |
2382 | 0 | panBandMap, nPixelSpace, nLineSpace, |
2383 | 0 | nBandSpace, psExtraArg); |
2384 | 0 | } |
2385 | | |
2386 | | // Should not be hit |
2387 | 0 | return GDALDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize, pData, |
2388 | 0 | nBufXSize, nBufYSize, eBufType, nBandCount, |
2389 | 0 | panBandMap, nPixelSpace, nLineSpace, |
2390 | 0 | nBandSpace, psExtraArg); |
2391 | 0 | } |
2392 | | |
2393 | | /************************************************************************/ |
2394 | | /* OGCAPITiledLayer() */ |
2395 | | /************************************************************************/ |
2396 | | |
2397 | | OGCAPITiledLayer::OGCAPITiledLayer( |
2398 | | OGCAPIDataset *poDS, bool bInvertAxis, const CPLString &osTileURL, |
2399 | | bool bIsMVT, const gdal::TileMatrixSet::TileMatrix &tileMatrix, |
2400 | | OGRwkbGeometryType eGeomType) |
2401 | 0 | : m_poDS(poDS), m_osTileURL(osTileURL), m_bIsMVT(bIsMVT), |
2402 | 0 | m_oTileMatrix(tileMatrix), m_bInvertAxis(bInvertAxis) |
2403 | 0 | { |
2404 | 0 | m_poFeatureDefn = new OGCAPITiledLayerFeatureDefn( |
2405 | 0 | this, ("Zoom level " + tileMatrix.mId).c_str()); |
2406 | 0 | SetDescription(m_poFeatureDefn->GetName()); |
2407 | 0 | m_poFeatureDefn->SetGeomType(eGeomType); |
2408 | 0 | if (eGeomType != wkbNone) |
2409 | 0 | { |
2410 | 0 | auto poClonedSRS = poDS->m_oSRS.Clone(); |
2411 | 0 | m_poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef(poClonedSRS); |
2412 | 0 | poClonedSRS->Dereference(); |
2413 | 0 | } |
2414 | 0 | m_poFeatureDefn->Reference(); |
2415 | 0 | m_osTileURL.replaceAll("{tileMatrix}", tileMatrix.mId.c_str()); |
2416 | 0 | } |
2417 | | |
2418 | | /************************************************************************/ |
2419 | | /* ~OGCAPITiledLayer() */ |
2420 | | /************************************************************************/ |
2421 | | |
2422 | | OGCAPITiledLayer::~OGCAPITiledLayer() |
2423 | 0 | { |
2424 | 0 | m_poFeatureDefn->InvalidateLayer(); |
2425 | 0 | m_poFeatureDefn->Release(); |
2426 | 0 | } |
2427 | | |
2428 | | /************************************************************************/ |
2429 | | /* GetCoalesceFactorForRow() */ |
2430 | | /************************************************************************/ |
2431 | | |
2432 | | int OGCAPITiledLayer::GetCoalesceFactorForRow(int nRow) const |
2433 | 0 | { |
2434 | 0 | int nCoalesce = 1; |
2435 | 0 | for (const auto &vmw : m_oTileMatrix.mVariableMatrixWidthList) |
2436 | 0 | { |
2437 | 0 | if (nRow >= vmw.mMinTileRow && nRow <= vmw.mMaxTileRow) |
2438 | 0 | { |
2439 | 0 | nCoalesce = vmw.mCoalesce; |
2440 | 0 | break; |
2441 | 0 | } |
2442 | 0 | } |
2443 | 0 | return nCoalesce; |
2444 | 0 | } |
2445 | | |
2446 | | /************************************************************************/ |
2447 | | /* ResetReading() */ |
2448 | | /************************************************************************/ |
2449 | | |
2450 | | void OGCAPITiledLayer::ResetReading() |
2451 | 0 | { |
2452 | 0 | if (m_nCurX == m_nCurMinX && m_nCurY == m_nCurMinY && m_poUnderlyingLayer) |
2453 | 0 | { |
2454 | 0 | m_poUnderlyingLayer->ResetReading(); |
2455 | 0 | } |
2456 | 0 | else |
2457 | 0 | { |
2458 | 0 | m_nCurX = m_nCurMinX; |
2459 | 0 | m_nCurY = m_nCurMinY; |
2460 | 0 | m_poUnderlyingDS.reset(); |
2461 | 0 | m_poUnderlyingLayer = nullptr; |
2462 | 0 | } |
2463 | 0 | } |
2464 | | |
2465 | | /************************************************************************/ |
2466 | | /* OpenTile() */ |
2467 | | /************************************************************************/ |
2468 | | |
2469 | | GDALDataset *OGCAPITiledLayer::OpenTile(int nX, int nY, bool &bEmptyContent) |
2470 | 0 | { |
2471 | 0 | int nCoalesce = GetCoalesceFactorForRow(nY); |
2472 | 0 | if (nCoalesce <= 0) |
2473 | 0 | return nullptr; |
2474 | 0 | nX = (nX / nCoalesce) * nCoalesce; |
2475 | |
|
2476 | 0 | const char *const *papszOpenOptions = nullptr; |
2477 | 0 | CPLString poPrefix; |
2478 | 0 | CPLStringList aosOpenOptions; |
2479 | |
|
2480 | 0 | if (m_bIsMVT) |
2481 | 0 | { |
2482 | 0 | const double dfOriX = |
2483 | 0 | m_bInvertAxis ? m_oTileMatrix.mTopLeftY : m_oTileMatrix.mTopLeftX; |
2484 | 0 | const double dfOriY = |
2485 | 0 | m_bInvertAxis ? m_oTileMatrix.mTopLeftX : m_oTileMatrix.mTopLeftY; |
2486 | 0 | aosOpenOptions.SetNameValue( |
2487 | 0 | "@GEOREF_TOPX", |
2488 | 0 | CPLSPrintf("%.17g", dfOriX + nX * m_oTileMatrix.mResX * |
2489 | 0 | m_oTileMatrix.mTileWidth)); |
2490 | 0 | aosOpenOptions.SetNameValue( |
2491 | 0 | "@GEOREF_TOPY", |
2492 | 0 | CPLSPrintf("%.17g", dfOriY - nY * m_oTileMatrix.mResY * |
2493 | 0 | m_oTileMatrix.mTileHeight)); |
2494 | 0 | aosOpenOptions.SetNameValue( |
2495 | 0 | "@GEOREF_TILEDIMX", |
2496 | 0 | CPLSPrintf("%.17g", nCoalesce * m_oTileMatrix.mResX * |
2497 | 0 | m_oTileMatrix.mTileWidth)); |
2498 | 0 | aosOpenOptions.SetNameValue( |
2499 | 0 | "@GEOREF_TILEDIMY", |
2500 | 0 | CPLSPrintf("%.17g", |
2501 | 0 | m_oTileMatrix.mResY * m_oTileMatrix.mTileWidth)); |
2502 | |
|
2503 | 0 | papszOpenOptions = aosOpenOptions.List(); |
2504 | 0 | poPrefix = "MVT"; |
2505 | 0 | } |
2506 | |
|
2507 | 0 | std::unique_ptr<GDALDataset> dataset = m_poDS->OpenTile( |
2508 | 0 | m_osTileURL, stoi(m_oTileMatrix.mId), nX, nY, bEmptyContent, |
2509 | 0 | GDAL_OF_VECTOR, poPrefix, papszOpenOptions); |
2510 | |
|
2511 | 0 | return dataset.release(); |
2512 | 0 | } |
2513 | | |
2514 | | /************************************************************************/ |
2515 | | /* FinalizeFeatureDefnWithLayer() */ |
2516 | | /************************************************************************/ |
2517 | | |
2518 | | void OGCAPITiledLayer::FinalizeFeatureDefnWithLayer(OGRLayer *poUnderlyingLayer) |
2519 | 0 | { |
2520 | 0 | if (!m_bFeatureDefnEstablished) |
2521 | 0 | { |
2522 | 0 | m_bFeatureDefnEstablished = true; |
2523 | 0 | const auto poSrcFieldDefn = poUnderlyingLayer->GetLayerDefn(); |
2524 | 0 | const int nFieldCount = poSrcFieldDefn->GetFieldCount(); |
2525 | 0 | for (int i = 0; i < nFieldCount; i++) |
2526 | 0 | { |
2527 | 0 | m_poFeatureDefn->AddFieldDefn(poSrcFieldDefn->GetFieldDefn(i)); |
2528 | 0 | } |
2529 | 0 | } |
2530 | 0 | } |
2531 | | |
2532 | | /************************************************************************/ |
2533 | | /* BuildFeature() */ |
2534 | | /************************************************************************/ |
2535 | | |
2536 | | OGRFeature *OGCAPITiledLayer::BuildFeature(OGRFeature *poSrcFeature, int nX, |
2537 | | int nY) |
2538 | 0 | { |
2539 | 0 | int nCoalesce = GetCoalesceFactorForRow(nY); |
2540 | 0 | if (nCoalesce <= 0) |
2541 | 0 | return nullptr; |
2542 | 0 | nX = (nX / nCoalesce) * nCoalesce; |
2543 | |
|
2544 | 0 | OGRFeature *poFeature = new OGRFeature(m_poFeatureDefn); |
2545 | 0 | const GIntBig nFID = nY * m_oTileMatrix.mMatrixWidth + nX + |
2546 | 0 | poSrcFeature->GetFID() * m_oTileMatrix.mMatrixWidth * |
2547 | 0 | m_oTileMatrix.mMatrixHeight; |
2548 | 0 | auto poGeom = poSrcFeature->StealGeometry(); |
2549 | 0 | if (poGeom && m_poFeatureDefn->GetGeomType() != wkbUnknown) |
2550 | 0 | { |
2551 | 0 | poGeom = |
2552 | 0 | OGRGeometryFactory::forceTo(poGeom, m_poFeatureDefn->GetGeomType()); |
2553 | 0 | } |
2554 | 0 | poFeature->SetFrom(poSrcFeature, true); |
2555 | 0 | poFeature->SetFID(nFID); |
2556 | 0 | if (poGeom && m_poFeatureDefn->GetGeomFieldCount() > 0) |
2557 | 0 | { |
2558 | 0 | poGeom->assignSpatialReference( |
2559 | 0 | m_poFeatureDefn->GetGeomFieldDefn(0)->GetSpatialRef()); |
2560 | 0 | } |
2561 | 0 | poFeature->SetGeometryDirectly(poGeom); |
2562 | 0 | delete poSrcFeature; |
2563 | 0 | return poFeature; |
2564 | 0 | } |
2565 | | |
2566 | | /************************************************************************/ |
2567 | | /* IncrementTileIndices() */ |
2568 | | /************************************************************************/ |
2569 | | |
2570 | | bool OGCAPITiledLayer::IncrementTileIndices() |
2571 | 0 | { |
2572 | |
|
2573 | 0 | const int nCoalesce = GetCoalesceFactorForRow(m_nCurY); |
2574 | 0 | if (nCoalesce <= 0) |
2575 | 0 | return false; |
2576 | 0 | if (m_nCurX / nCoalesce < m_nCurMaxX / nCoalesce) |
2577 | 0 | { |
2578 | 0 | m_nCurX += nCoalesce; |
2579 | 0 | } |
2580 | 0 | else if (m_nCurY < m_nCurMaxY) |
2581 | 0 | { |
2582 | 0 | m_nCurX = m_nCurMinX; |
2583 | 0 | m_nCurY++; |
2584 | 0 | } |
2585 | 0 | else |
2586 | 0 | { |
2587 | 0 | m_nCurY = -1; |
2588 | 0 | return false; |
2589 | 0 | } |
2590 | 0 | return true; |
2591 | 0 | } |
2592 | | |
2593 | | /************************************************************************/ |
2594 | | /* GetNextRawFeature() */ |
2595 | | /************************************************************************/ |
2596 | | |
2597 | | OGRFeature *OGCAPITiledLayer::GetNextRawFeature() |
2598 | 0 | { |
2599 | 0 | while (true) |
2600 | 0 | { |
2601 | 0 | if (m_poUnderlyingLayer == nullptr) |
2602 | 0 | { |
2603 | 0 | if (m_nCurY < 0) |
2604 | 0 | { |
2605 | 0 | return nullptr; |
2606 | 0 | } |
2607 | 0 | bool bEmptyContent = false; |
2608 | 0 | m_poUnderlyingDS.reset(OpenTile(m_nCurX, m_nCurY, bEmptyContent)); |
2609 | 0 | if (bEmptyContent) |
2610 | 0 | { |
2611 | 0 | if (!IncrementTileIndices()) |
2612 | 0 | return nullptr; |
2613 | 0 | continue; |
2614 | 0 | } |
2615 | 0 | if (m_poUnderlyingDS == nullptr) |
2616 | 0 | { |
2617 | 0 | return nullptr; |
2618 | 0 | } |
2619 | 0 | m_poUnderlyingLayer = m_poUnderlyingDS->GetLayer(0); |
2620 | 0 | if (m_poUnderlyingLayer == nullptr) |
2621 | 0 | { |
2622 | 0 | return nullptr; |
2623 | 0 | } |
2624 | 0 | FinalizeFeatureDefnWithLayer(m_poUnderlyingLayer); |
2625 | 0 | } |
2626 | | |
2627 | 0 | auto poSrcFeature = m_poUnderlyingLayer->GetNextFeature(); |
2628 | 0 | if (poSrcFeature != nullptr) |
2629 | 0 | { |
2630 | 0 | return BuildFeature(poSrcFeature, m_nCurX, m_nCurY); |
2631 | 0 | } |
2632 | | |
2633 | 0 | m_poUnderlyingDS.reset(); |
2634 | 0 | m_poUnderlyingLayer = nullptr; |
2635 | |
|
2636 | 0 | if (!IncrementTileIndices()) |
2637 | 0 | return nullptr; |
2638 | 0 | } |
2639 | 0 | } |
2640 | | |
2641 | | /************************************************************************/ |
2642 | | /* GetFeature() */ |
2643 | | /************************************************************************/ |
2644 | | |
2645 | | OGRFeature *OGCAPITiledLayer::GetFeature(GIntBig nFID) |
2646 | 0 | { |
2647 | 0 | if (nFID < 0) |
2648 | 0 | return nullptr; |
2649 | 0 | const GIntBig nFIDInTile = |
2650 | 0 | nFID / (m_oTileMatrix.mMatrixWidth * m_oTileMatrix.mMatrixHeight); |
2651 | 0 | const GIntBig nTileID = |
2652 | 0 | nFID % (m_oTileMatrix.mMatrixWidth * m_oTileMatrix.mMatrixHeight); |
2653 | 0 | const int nY = static_cast<int>(nTileID / m_oTileMatrix.mMatrixWidth); |
2654 | 0 | const int nX = static_cast<int>(nTileID % m_oTileMatrix.mMatrixWidth); |
2655 | 0 | bool bEmptyContent = false; |
2656 | 0 | std::unique_ptr<GDALDataset> poUnderlyingDS( |
2657 | 0 | OpenTile(nX, nY, bEmptyContent)); |
2658 | 0 | if (poUnderlyingDS == nullptr) |
2659 | 0 | return nullptr; |
2660 | 0 | OGRLayer *poUnderlyingLayer = poUnderlyingDS->GetLayer(0); |
2661 | 0 | if (poUnderlyingLayer == nullptr) |
2662 | 0 | return nullptr; |
2663 | 0 | FinalizeFeatureDefnWithLayer(poUnderlyingLayer); |
2664 | 0 | OGRFeature *poSrcFeature = poUnderlyingLayer->GetFeature(nFIDInTile); |
2665 | 0 | if (poSrcFeature == nullptr) |
2666 | 0 | return nullptr; |
2667 | 0 | return BuildFeature(poSrcFeature, nX, nY); |
2668 | 0 | } |
2669 | | |
2670 | | /************************************************************************/ |
2671 | | /* EstablishFields() */ |
2672 | | /************************************************************************/ |
2673 | | |
2674 | | void OGCAPITiledLayer::EstablishFields() |
2675 | 0 | { |
2676 | 0 | if (!m_bFeatureDefnEstablished && !m_bEstablishFieldsCalled) |
2677 | 0 | { |
2678 | 0 | m_bEstablishFieldsCalled = true; |
2679 | | |
2680 | | // Try up to 10 requests in order. We could probably remove that |
2681 | | // to use just the fallback logic. |
2682 | 0 | for (int i = 0; i < 10; ++i) |
2683 | 0 | { |
2684 | 0 | bool bEmptyContent = false; |
2685 | 0 | m_poUnderlyingDS.reset(OpenTile(m_nCurX, m_nCurY, bEmptyContent)); |
2686 | 0 | if (bEmptyContent || !m_poUnderlyingDS) |
2687 | 0 | { |
2688 | 0 | if (!IncrementTileIndices()) |
2689 | 0 | break; |
2690 | 0 | continue; |
2691 | 0 | } |
2692 | 0 | m_poUnderlyingLayer = m_poUnderlyingDS->GetLayer(0); |
2693 | 0 | if (m_poUnderlyingLayer) |
2694 | 0 | { |
2695 | 0 | FinalizeFeatureDefnWithLayer(m_poUnderlyingLayer); |
2696 | 0 | break; |
2697 | 0 | } |
2698 | 0 | } |
2699 | |
|
2700 | 0 | if (!m_bFeatureDefnEstablished) |
2701 | 0 | { |
2702 | | // Try to sample at different locations in the extent |
2703 | 0 | for (int j = 0; !m_bFeatureDefnEstablished && j < 3; ++j) |
2704 | 0 | { |
2705 | 0 | m_nCurY = m_nMinY + (2 * j + 1) * (m_nMaxY - m_nMinY) / 6; |
2706 | 0 | for (int i = 0; i < 3; ++i) |
2707 | 0 | { |
2708 | 0 | m_nCurX = m_nMinX + (2 * i + 1) * (m_nMaxX - m_nMinX) / 6; |
2709 | 0 | bool bEmptyContent = false; |
2710 | 0 | m_poUnderlyingDS.reset( |
2711 | 0 | OpenTile(m_nCurX, m_nCurY, bEmptyContent)); |
2712 | 0 | if (bEmptyContent || !m_poUnderlyingDS) |
2713 | 0 | { |
2714 | 0 | continue; |
2715 | 0 | } |
2716 | 0 | m_poUnderlyingLayer = m_poUnderlyingDS->GetLayer(0); |
2717 | 0 | if (m_poUnderlyingLayer) |
2718 | 0 | { |
2719 | 0 | FinalizeFeatureDefnWithLayer(m_poUnderlyingLayer); |
2720 | 0 | break; |
2721 | 0 | } |
2722 | 0 | } |
2723 | 0 | } |
2724 | 0 | } |
2725 | |
|
2726 | 0 | if (!m_bFeatureDefnEstablished) |
2727 | 0 | { |
2728 | 0 | CPLDebug("OGCAPI", "Could not establish feature definition. No " |
2729 | 0 | "valid tile found in sampling done"); |
2730 | 0 | } |
2731 | |
|
2732 | 0 | ResetReading(); |
2733 | 0 | } |
2734 | 0 | } |
2735 | | |
2736 | | /************************************************************************/ |
2737 | | /* SetExtent() */ |
2738 | | /************************************************************************/ |
2739 | | |
2740 | | void OGCAPITiledLayer::SetExtent(double dfXMin, double dfYMin, double dfXMax, |
2741 | | double dfYMax) |
2742 | 0 | { |
2743 | 0 | m_sEnvelope.MinX = dfXMin; |
2744 | 0 | m_sEnvelope.MinY = dfYMin; |
2745 | 0 | m_sEnvelope.MaxX = dfXMax; |
2746 | 0 | m_sEnvelope.MaxY = dfYMax; |
2747 | 0 | } |
2748 | | |
2749 | | /************************************************************************/ |
2750 | | /* IGetExtent() */ |
2751 | | /************************************************************************/ |
2752 | | |
2753 | | OGRErr OGCAPITiledLayer::IGetExtent(int /* iGeomField */, OGREnvelope *psExtent, |
2754 | | bool /* bForce */) |
2755 | 0 | { |
2756 | 0 | *psExtent = m_sEnvelope; |
2757 | 0 | return OGRERR_NONE; |
2758 | 0 | } |
2759 | | |
2760 | | /************************************************************************/ |
2761 | | /* ISetSpatialFilter() */ |
2762 | | /************************************************************************/ |
2763 | | |
2764 | | OGRErr OGCAPITiledLayer::ISetSpatialFilter(int iGeomField, |
2765 | | const OGRGeometry *poGeomIn) |
2766 | 0 | { |
2767 | 0 | const OGRErr eErr = OGRLayer::ISetSpatialFilter(iGeomField, poGeomIn); |
2768 | 0 | if (eErr == OGRERR_NONE) |
2769 | 0 | { |
2770 | 0 | OGREnvelope sEnvelope; |
2771 | 0 | if (m_poFilterGeom != nullptr) |
2772 | 0 | sEnvelope = m_sFilterEnvelope; |
2773 | 0 | else |
2774 | 0 | sEnvelope = m_sEnvelope; |
2775 | |
|
2776 | 0 | const double dfTileDim = m_oTileMatrix.mResX * m_oTileMatrix.mTileWidth; |
2777 | 0 | const double dfOriX = |
2778 | 0 | m_bInvertAxis ? m_oTileMatrix.mTopLeftY : m_oTileMatrix.mTopLeftX; |
2779 | 0 | const double dfOriY = |
2780 | 0 | m_bInvertAxis ? m_oTileMatrix.mTopLeftX : m_oTileMatrix.mTopLeftY; |
2781 | 0 | if (sEnvelope.MinX - dfOriX >= -10 * dfTileDim && |
2782 | 0 | dfOriY - sEnvelope.MinY >= -10 * dfTileDim && |
2783 | 0 | sEnvelope.MaxX - dfOriX <= 10 * dfTileDim && |
2784 | 0 | dfOriY - sEnvelope.MaxY <= 10 * dfTileDim) |
2785 | 0 | { |
2786 | 0 | m_nCurMinX = std::max( |
2787 | 0 | m_nMinX, |
2788 | 0 | static_cast<int>(floor((sEnvelope.MinX - dfOriX) / dfTileDim))); |
2789 | 0 | m_nCurMinY = std::max( |
2790 | 0 | m_nMinY, |
2791 | 0 | static_cast<int>(floor((dfOriY - sEnvelope.MaxY) / dfTileDim))); |
2792 | 0 | m_nCurMaxX = std::min( |
2793 | 0 | m_nMaxX, |
2794 | 0 | static_cast<int>(floor((sEnvelope.MaxX - dfOriX) / dfTileDim))); |
2795 | 0 | m_nCurMaxY = std::min( |
2796 | 0 | m_nMaxY, |
2797 | 0 | static_cast<int>(floor((dfOriY - sEnvelope.MinY) / dfTileDim))); |
2798 | 0 | } |
2799 | 0 | else |
2800 | 0 | { |
2801 | 0 | m_nCurMinX = m_nMinX; |
2802 | 0 | m_nCurMinY = m_nMinY; |
2803 | 0 | m_nCurMaxX = m_nMaxX; |
2804 | 0 | m_nCurMaxY = m_nMaxY; |
2805 | 0 | } |
2806 | |
|
2807 | 0 | ResetReading(); |
2808 | 0 | } |
2809 | 0 | return eErr; |
2810 | 0 | } |
2811 | | |
2812 | | /************************************************************************/ |
2813 | | /* TestCapability() */ |
2814 | | /************************************************************************/ |
2815 | | |
2816 | | int OGCAPITiledLayer::TestCapability(const char *pszCap) const |
2817 | 0 | { |
2818 | 0 | if (EQUAL(pszCap, OLCRandomRead)) |
2819 | 0 | return true; |
2820 | 0 | if (EQUAL(pszCap, OLCFastGetExtent)) |
2821 | 0 | return true; |
2822 | 0 | if (EQUAL(pszCap, OLCStringsAsUTF8)) |
2823 | 0 | return true; |
2824 | 0 | if (EQUAL(pszCap, OLCFastSpatialFilter)) |
2825 | 0 | return true; |
2826 | 0 | return false; |
2827 | 0 | } |
2828 | | |
2829 | | /************************************************************************/ |
2830 | | /* SetMinMaxXY() */ |
2831 | | /************************************************************************/ |
2832 | | |
2833 | | void OGCAPITiledLayer::SetMinMaxXY(int minCol, int minRow, int maxCol, |
2834 | | int maxRow) |
2835 | 0 | { |
2836 | 0 | m_nMinX = minCol; |
2837 | 0 | m_nMinY = minRow; |
2838 | 0 | m_nMaxX = maxCol; |
2839 | 0 | m_nMaxY = maxRow; |
2840 | 0 | m_nCurMinX = m_nMinX; |
2841 | 0 | m_nCurMinY = m_nMinY; |
2842 | 0 | m_nCurMaxX = m_nMaxX; |
2843 | 0 | m_nCurMaxY = m_nMaxY; |
2844 | 0 | ResetReading(); |
2845 | 0 | } |
2846 | | |
2847 | | /************************************************************************/ |
2848 | | /* SetFields() */ |
2849 | | /************************************************************************/ |
2850 | | |
2851 | | void OGCAPITiledLayer::SetFields( |
2852 | | const std::vector<std::unique_ptr<OGRFieldDefn>> &apoFields) |
2853 | 0 | { |
2854 | 0 | m_bFeatureDefnEstablished = true; |
2855 | 0 | for (const auto &poField : apoFields) |
2856 | 0 | { |
2857 | 0 | m_poFeatureDefn->AddFieldDefn(poField.get()); |
2858 | 0 | } |
2859 | 0 | } |
2860 | | |
2861 | | /************************************************************************/ |
2862 | | /* Open() */ |
2863 | | /************************************************************************/ |
2864 | | |
2865 | | GDALDataset *OGCAPIDataset::Open(GDALOpenInfo *poOpenInfo) |
2866 | 68 | { |
2867 | 68 | if (!Identify(poOpenInfo)) |
2868 | 0 | return nullptr; |
2869 | 68 | auto poDS = std::make_unique<OGCAPIDataset>(); |
2870 | 68 | if (STARTS_WITH_CI(poOpenInfo->pszFilename, "OGCAPI:") || |
2871 | 0 | STARTS_WITH(poOpenInfo->pszFilename, "http://") || |
2872 | 0 | STARTS_WITH(poOpenInfo->pszFilename, "https://")) |
2873 | 68 | { |
2874 | 68 | if (!poDS->InitFromURL(poOpenInfo)) |
2875 | 68 | return nullptr; |
2876 | 68 | } |
2877 | 0 | else |
2878 | 0 | { |
2879 | 0 | if (!poDS->InitFromFile(poOpenInfo)) |
2880 | 0 | return nullptr; |
2881 | 0 | } |
2882 | 0 | return poDS.release(); |
2883 | 68 | } |
2884 | | |
2885 | | /************************************************************************/ |
2886 | | /* GDALRegister_OGCAPI() */ |
2887 | | /************************************************************************/ |
2888 | | |
2889 | | void GDALRegister_OGCAPI() |
2890 | | |
2891 | 22 | { |
2892 | 22 | if (GDALGetDriverByName("OGCAPI") != nullptr) |
2893 | 0 | return; |
2894 | | |
2895 | 22 | GDALDriver *poDriver = new GDALDriver(); |
2896 | | |
2897 | 22 | poDriver->SetDescription("OGCAPI"); |
2898 | 22 | poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES"); |
2899 | 22 | poDriver->SetMetadataItem(GDAL_DCAP_VECTOR, "YES"); |
2900 | 22 | poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "OGCAPI"); |
2901 | | |
2902 | 22 | poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES"); |
2903 | | |
2904 | 22 | poDriver->SetMetadataItem( |
2905 | 22 | GDAL_DMD_OPENOPTIONLIST, |
2906 | 22 | "<OpenOptionList>" |
2907 | 22 | " <Option name='API' type='string-select' " |
2908 | 22 | "description='Which API to use to access data' default='AUTO'>" |
2909 | 22 | " <Value>AUTO</Value>" |
2910 | 22 | " <Value>MAP</Value>" |
2911 | 22 | " <Value>TILES</Value>" |
2912 | 22 | " <Value>COVERAGE</Value>" |
2913 | 22 | " <Value>ITEMS</Value>" |
2914 | 22 | " </Option>" |
2915 | 22 | " <Option name='IMAGE_FORMAT' scope='raster' type='string-select' " |
2916 | 22 | "description='Which format to use for pixel acquisition' " |
2917 | 22 | "default='AUTO'>" |
2918 | 22 | " <Value>AUTO</Value>" |
2919 | 22 | " <Value>PNG</Value>" |
2920 | 22 | " <Value>PNG_PREFERRED</Value>" |
2921 | 22 | " <Value>JPEG</Value>" |
2922 | 22 | " <Value>JPEG_PREFERRED</Value>" |
2923 | 22 | " <Value>GEOTIFF</Value>" |
2924 | 22 | " </Option>" |
2925 | 22 | " <Option name='VECTOR_FORMAT' scope='vector' type='string-select' " |
2926 | 22 | "description='Which format to use for vector data acquisition' " |
2927 | 22 | "default='AUTO'>" |
2928 | 22 | " <Value>AUTO</Value>" |
2929 | 22 | " <Value>GEOJSON</Value>" |
2930 | 22 | " <Value>GEOJSON_PREFERRED</Value>" |
2931 | 22 | " <Value>MVT</Value>" |
2932 | 22 | " <Value>MVT_PREFERRED</Value>" |
2933 | 22 | " </Option>" |
2934 | 22 | " <Option name='TILEMATRIXSET' type='string' " |
2935 | 22 | "description='Identifier of the required tile matrix set'/>" |
2936 | 22 | " <Option name='PREFERRED_TILEMATRIXSET' type='string' " |
2937 | 22 | "description='dentifier of the preferred tile matrix set' " |
2938 | 22 | "default='WorldCRS84Quad'/>" |
2939 | 22 | " <Option name='TILEMATRIX' scope='raster' type='string' " |
2940 | 22 | "description='Tile matrix identifier.'/>" |
2941 | 22 | " <Option name='CACHE' scope='raster' type='boolean' " |
2942 | 22 | "description='Whether to enable block/tile caching' default='YES'/>" |
2943 | 22 | " <Option name='MAX_CONNECTIONS' scope='raster' type='int' " |
2944 | 22 | "description='Maximum number of connections' default='5'/>" |
2945 | 22 | " <Option name='MINX' type='float' " |
2946 | 22 | "description='Minimum value (in SRS of TileMatrixSet) of X'/>" |
2947 | 22 | " <Option name='MINY' type='float' " |
2948 | 22 | "description='Minimum value (in SRS of TileMatrixSet) of Y'/>" |
2949 | 22 | " <Option name='MAXX' type='float' " |
2950 | 22 | "description='Maximum value (in SRS of TileMatrixSet) of X'/>" |
2951 | 22 | " <Option name='MAXY' type='float' " |
2952 | 22 | "description='Maximum value (in SRS of TileMatrixSet) of Y'/>" |
2953 | 22 | "</OpenOptionList>"); |
2954 | | |
2955 | 22 | poDriver->pfnIdentify = OGCAPIDataset::Identify; |
2956 | 22 | poDriver->pfnOpen = OGCAPIDataset::Open; |
2957 | | |
2958 | 22 | GetGDALDriverManager()->RegisterDriver(poDriver); |
2959 | 22 | } |