Debian Codegraph @

ogr/ogrsf_frmts/plscenes/ogrplscenesdatav1dataset.cpp

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Project: PlanetLabs scene driver
4 * Purpose: Implements OGRPLScenesDataV1Dataset
5 * Author: Even Rouault, even dot rouault at spatialys.com
6 *
7 ******************************************************************************
8 * Copyright (c) 2016, Planet Labs
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13 #include "cpl_multiproc.h"cxx . . $ `<file>/port/cpl_multiproc.h`/ // CPLSleep()
14
15 #include "ogr_plscenes.h"cxx . . $ `<file>/ogr/ogrsf_frmts/plscenes/ogr_plscenes.h`/
16 #include "ogrlibjsonutils.h"cxx . . $ `<file>/ogr/ogrlibjsonutils.h`/
17 #include <time.h>
18
19 /************************************************************************/
20 /* OGRPLScenesDataV1Dataset() */
21 /************************************************************************/
22
23 OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#OGRPLScenesDataV1Dataset(49f6e7a06ebc5aa8).()
24 : m_bLayerListInitializedcxx . . $ OGRPLScenesDataV1Dataset#m_bLayerListInitialized.(false), m_bMustCleanPersistentcxx . . $ OGRPLScenesDataV1Dataset#m_bMustCleanPersistent.(false),
25 m_nLayerscxx . . $ OGRPLScenesDataV1Dataset#m_nLayers.(0), m_papoLayerscxx . . $ OGRPLScenesDataV1Dataset#m_papoLayers.(nullptr), m_bFollowLinkscxx . . $ OGRPLScenesDataV1Dataset#m_bFollowLinks.(false)
26 {
27 }
28
29 /************************************************************************/
30 /* ~OGRPLScenesDataV1Dataset() */
31 /************************************************************************/
32
33 OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::~cxx . . $ OGRPLScenesDataV1Dataset#`~OGRPLScenesDataV1Dataset`(ced63f7c635d850d).OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#()
34 {
35 for (int i = 0; i < m_nLayerscxx . . $ OGRPLScenesDataV1Dataset#m_nLayers.; i++)
36 delete m_papoLayerscxx . . $ OGRPLScenesDataV1Dataset#m_papoLayers.[i];
37 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(m_papoLayerscxx . . $ OGRPLScenesDataV1Dataset#m_papoLayers.);
38
39 if (m_bMustCleanPersistentcxx . . $ OGRPLScenesDataV1Dataset#m_bMustCleanPersistent.)
40 {
41 char **papszOptions = CSLSetNameValue(nullptr, "CLOSE_PERSISTENT",
42 CPLSPrintf("PLSCENES:%p", this));
43 CPLHTTPDestroyResult(CPLHTTPFetch(m_osBaseURLcxx . . $ OGRPLScenesDataV1Dataset#m_osBaseURL., papszOptions));
44 CSLDestroy(papszOptions);
45 }
46 }
47
48 /************************************************************************/
49 /* GetLayer() */
50 /************************************************************************/
51
52 const OGRLayercxx . . $ OGRLayer# *OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::GetLayercxx . . $ OGRPLScenesDataV1Dataset#GetLayer(3ead67c79a3185c4).(int idx) const
53 {
54 if (idx < 0 || idx >= GetLayerCountcxx . . $ OGRPLScenesDataV1Dataset#GetLayerCount(455f465bc33b4cdf).())
55 return nullptr;
56 return m_papoLayerscxx . . $ OGRPLScenesDataV1Dataset#m_papoLayers.[idx];
57 }
58
59 /************************************************************************/
60 /* GetLayerCount() */
61 /************************************************************************/
62
63 int OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::GetLayerCountcxx . . $ OGRPLScenesDataV1Dataset#GetLayerCount(455f465bc33b4cdf).() const
64 {
65 if (!m_bLayerListInitializedcxx . . $ OGRPLScenesDataV1Dataset#m_bLayerListInitialized.)
66 {
67 m_bLayerListInitializedcxx . . $ OGRPLScenesDataV1Dataset#m_bLayerListInitialized. = true;
68 const_cast<OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset# *>(this)->EstablishLayerListcxx . . $ OGRPLScenesDataV1Dataset#EstablishLayerList(49f6e7a06ebc5aa8).();
69 }
70 return m_nLayerscxx . . $ OGRPLScenesDataV1Dataset#m_nLayers.;
71 }
72
73 /************************************************************************/
74 /* ParseItemType() */
75 /************************************************************************/
76
77 OGRLayercxx . . $ OGRLayer# *OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::ParseItemTypecxx . . $ OGRPLScenesDataV1Dataset#ParseItemType(21b08aed0ec5ab9c).(json_object *poItemType)
78 {
79 if (poItemType == nullptr ||
80 json_object_get_type(poItemType) != json_type_object)
81 return nullptr;
82 json_object *poId = CPL_json_object_object_getcxx . . $ CPL_json_object_object_get(34d28576f59d1ffc).(poItemType, "id");
83 if (poId == nullptr || json_object_get_type(poId) != json_type_string)
84 return nullptr;
85
86 CPLString osDisplayDescription;
87 json_object *poDisplayDescription =
88 CPL_json_object_object_getcxx . . $ CPL_json_object_object_get(34d28576f59d1ffc).(poItemType, "display_description");
89 if (poDisplayDescription != nullptr &&
90 json_object_get_type(poDisplayDescription) == json_type_string)
91 osDisplayDescription = json_object_get_string(poDisplayDescription);
92 CPLString osDisplayName;
93 json_object *poDisplayName =
94 CPL_json_object_object_getcxx . . $ CPL_json_object_object_get(34d28576f59d1ffc).(poItemType, "display_name");
95 if (poDisplayName != nullptr &&
96 json_object_get_type(poDisplayName) == json_type_string)
97 osDisplayName = json_object_get_string(poDisplayName);
98
99 const char *pszId = json_object_get_string(poId);
100
101 // The layer might already exist if GetLayerByName() is called before
102 // GetLayer()/GetLayerCount() is
103
104 // Prevent GetLayerCount() from calling EstablishLayerList()
105 bool bLayerListInitializedBackup = m_bLayerListInitializedcxx . . $ OGRPLScenesDataV1Dataset#m_bLayerListInitialized.;
106 m_bLayerListInitializedcxx . . $ OGRPLScenesDataV1Dataset#m_bLayerListInitialized. = true;
107 OGRLayercxx . . $ OGRLayer# *poExistingLayer = GDALDatasetcxx . . $ GDALDataset#::GetLayerByNamecxx . . $ GDALDataset#GetLayerByName(5ddf27af0841fa5b).(pszId);
108 m_bLayerListInitializedcxx . . $ OGRPLScenesDataV1Dataset#m_bLayerListInitialized. = bLayerListInitializedBackup;
109 if (poExistingLayer != nullptr)
110 return poExistingLayer;
111
112 OGRPLScenesDataV1Layercxx . . $ OGRPLScenesDataV1Layer# *poPLLayer = new OGRPLScenesDataV1Layercxx . . $ OGRPLScenesDataV1Layer#OGRPLScenesDataV1Layer(acd00c2e2ab48cfb).(this, pszId);
113 if (!osDisplayName.empty())
114 poPLLayer->SetMetadataItemcxx . . $ GDALMajorObject#SetMetadataItem(216fc8eb9053a851).("SHORT_DESCRIPTION", osDisplayName.c_str());
115 if (!osDisplayDescription.empty())
116 poPLLayer->SetMetadataItemcxx . . $ GDALMajorObject#SetMetadataItem(216fc8eb9053a851).("DESCRIPTION", osDisplayDescription.c_str());
117 m_papoLayerscxx . . $ OGRPLScenesDataV1Dataset#m_papoLayers. = (OGRPLScenesDataV1Layercxx . . $ OGRPLScenesDataV1Layer# **)CPLRealloc(
118 m_papoLayerscxx . . $ OGRPLScenesDataV1Dataset#m_papoLayers., sizeof(OGRPLScenesDataV1Layercxx . . $ OGRPLScenesDataV1Layer# *) * (m_nLayerscxx . . $ OGRPLScenesDataV1Dataset#m_nLayers. + 1));
119 m_papoLayerscxx . . $ OGRPLScenesDataV1Dataset#m_papoLayers.[m_nLayerscxx . . $ OGRPLScenesDataV1Dataset#m_nLayers.++] = poPLLayer;
120 return poPLLayer;
121 }
122
123 /************************************************************************/
124 /* ParseItemTypes() */
125 /************************************************************************/
126
127 bool OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::ParseItemTypescxx . . $ OGRPLScenesDataV1Dataset#ParseItemTypes(98ad915997256310).(json_object *poObj,
128 CPLString &osNext)
129 {
130 json_object *poItemTypes = CPL_json_object_object_getcxx . . $ CPL_json_object_object_get(34d28576f59d1ffc).(poObj, "item_types");
131 if (poItemTypes == nullptr ||
132 json_object_get_type(poItemTypes) != json_type_array)
133 {
134 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
135 "Missing item_types object, or not of type array");
136 return false;
137 }
138 const auto nCatalogsLength = json_object_array_length(poItemTypes);
139 for (auto i = decltype(nCatalogsLength){0}; i < nCatalogsLength; i++)
140 {
141 json_object *poItemType = json_object_array_get_idx(poItemTypes, i);
142 ParseItemTypecxx . . $ OGRPLScenesDataV1Dataset#ParseItemType(21b08aed0ec5ab9c).(poItemType);
143 }
144
145 // Is there a next page ?
146 osNext = "";
147 json_object *poLinks = CPL_json_object_object_getcxx . . $ CPL_json_object_object_get(34d28576f59d1ffc).(poObj, "_links");
148 if (poLinks && json_object_get_type(poLinks) == json_type_object)
149 {
150 json_object *poNext = CPL_json_object_object_getcxx . . $ CPL_json_object_object_get(34d28576f59d1ffc).(poLinks, "_next");
151 if (poNext && json_object_get_type(poNext) == json_type_string)
152 {
153 osNext = json_object_get_string(poNext);
154 }
155 }
156
157 return true;
158 }
159
160 /************************************************************************/
161 /* EstablishLayerList() */
162 /************************************************************************/
163
164 void OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::EstablishLayerListcxx . . $ OGRPLScenesDataV1Dataset#EstablishLayerList(49f6e7a06ebc5aa8).()
165 {
166 CPLString osURL(m_osNextItemTypesPageURLcxx . . $ OGRPLScenesDataV1Dataset#m_osNextItemTypesPageURL.);
167 m_osNextItemTypesPageURLcxx . . $ OGRPLScenesDataV1Dataset#m_osNextItemTypesPageURL. = "";
168
169 while (!osURL.empty())
170 {
171 json_object *poObj = RunRequestcxx . . $ OGRPLScenesDataV1Dataset#RunRequest(64c91ecc94621f11).(osURL);
172 if (poObj == nullptr)
173 break;
174 if (!ParseItemTypescxx . . $ OGRPLScenesDataV1Dataset#ParseItemTypes(98ad915997256310).(poObj, osURL))
175 {
176 json_object_put(poObj);
177 break;
178 }
179 json_object_put(poObj);
180 }
181 }
182
183 /************************************************************************/
184 /* GetLayerByName() */
185 /************************************************************************/
186
187 OGRLayercxx . . $ OGRLayer# *OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::GetLayerByNamecxx . . $ OGRPLScenesDataV1Dataset#GetLayerByName(5ddf27af0841fa5b).(const char *pszName)
188 {
189 // Prevent GetLayerCount() from calling EstablishLayerList()
190 bool bLayerListInitializedBackup = m_bLayerListInitializedcxx . . $ OGRPLScenesDataV1Dataset#m_bLayerListInitialized.;
191 m_bLayerListInitializedcxx . . $ OGRPLScenesDataV1Dataset#m_bLayerListInitialized. = true;
192 OGRLayercxx . . $ OGRLayer# *poRet = GDALDatasetcxx . . $ GDALDataset#::GetLayerByNamecxx . . $ GDALDataset#GetLayerByName(5ddf27af0841fa5b).(pszName);
193 m_bLayerListInitializedcxx . . $ OGRPLScenesDataV1Dataset#m_bLayerListInitialized. = bLayerListInitializedBackup;
194 if (poRet != nullptr)
195 return poRet;
196
197 CPLString osURL(m_osBaseURLcxx . . $ OGRPLScenesDataV1Dataset#m_osBaseURL. + "item-types/" + pszName);
198 json_object *poObj = RunRequestcxx . . $ OGRPLScenesDataV1Dataset#RunRequest(64c91ecc94621f11).(osURL);
199 if (poObj == nullptr)
200 return nullptr;
201 poRet = ParseItemTypecxx . . $ OGRPLScenesDataV1Dataset#ParseItemType(21b08aed0ec5ab9c).(poObj);
202 json_object_put(poObj);
203 return poRet;
204 }
205
206 /************************************************************************/
207 /* GetBaseHTTPOptions() */
208 /************************************************************************/
209
210 char **OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::GetBaseHTTPOptionscxx . . $ OGRPLScenesDataV1Dataset#GetBaseHTTPOptions(de5739791ae7584).()
211 {
212 m_bMustCleanPersistentcxx . . $ OGRPLScenesDataV1Dataset#m_bMustCleanPersistent. = true;
213
214 char **papszOptions = nullptr;
215 papszOptions =
216 CSLAddString(papszOptions, CPLSPrintf("PERSISTENT=PLSCENES:%p", this));
217 papszOptions = CSLAddString(
218 papszOptions,
219 CPLSPrintf("HEADERS=Authorization: api-key %s", m_osAPIKeycxx . . $ OGRPLScenesDataV1Dataset#m_osAPIKey..c_str()));
220
221 return papszOptions;
222 }
223
224 /************************************************************************/
225 /* RunRequest() */
226 /************************************************************************/
227
228 json_object *OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::RunRequestcxx . . $ OGRPLScenesDataV1Dataset#RunRequest(64c91ecc94621f11).(const char *pszURL,
229 int bQuiet404Error,
230 const char *pszHTTPVerb,
231 bool bExpectJSonReturn,
232 const char *pszPostContent)
233 {
234 char **papszOptions = CSLAddString(GetBaseHTTPOptionscxx . . $ OGRPLScenesDataV1Dataset#GetBaseHTTPOptions(de5739791ae7584).(), nullptr);
235 // We need to set it each time as CURL would reuse the previous value
236 // if reusing the same connection
237 papszOptions = CSLSetNameValue(papszOptions, "CUSTOMREQUEST", pszHTTPVerb);
238 if (pszPostContent != nullptr)
239 {
240 CPLString osHeaders = CSLFetchNameValueDef(papszOptions, "HEADERS", "");
241 if (!osHeaders.empty())
242 osHeaders += "\r\n";
243 osHeaders += "Content-Type: application/json";
244 papszOptions = CSLSetNameValue(papszOptions, "HEADERS", osHeaders);
245 papszOptions =
246 CSLSetNameValue(papszOptions, "POSTFIELDS", pszPostContent);
247 }
248 papszOptions = CSLSetNameValue(papszOptions, "MAX_RETRY", "3");
249 CPLHTTPResult *psResult = nullptr;
250 if (STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(m_osBaseURL, "/vsimem/") && STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(pszURL, "/vsimem/"))
251 {
252 psResult = (CPLHTTPResult *)CPLCalloc(1, sizeof(CPLHTTPResult));
253 vsi_l_offset nDataLengthLarge = 0;
254 CPLString osURL(pszURL);
255 if (osURL.back() == '/')
256 osURL.pop_back();
257 if (pszPostContent != nullptr)
258 {
259 osURL += "&POSTFIELDS=";
260 osURL += pszPostContent;
261 }
262 CPLDebug("PLSCENES", "Fetching %s", osURL.c_str());
263 GBytecxx . . $ GByte# *pabyBuf = VSIGetMemFileBuffer(osURL, &nDataLengthLarge, FALSEcxx . . $ `port/cpl_port.h:1180:9`!);
264 size_t nDataLength = static_cast<size_t>(nDataLengthLarge);
265 if (pabyBuf)
266 {
267 psResult->pabyData = (GBytecxx . . $ GByte# *)VSI_MALLOC_VERBOSEcxx . . $ `port/cpl_vsi.h:375:9`!(1 + nDataLength);
268 if (psResult->pabyData)
269 {
270 memcpy(psResult->pabyData, pabyBuf, nDataLength);
271 psResult->pabyData[nDataLength] = 0;
272 }
273 }
274 else
275 {
276 psResult->pszErrBuf = CPLStrdup(
277 CPLSPrintf("Error 404. Cannot find %s", osURL.c_str()));
278 }
279 }
280 else
281 {
282 if (bQuiet404Error)
283 CPLPushErrorHandler(CPLQuietErrorHandler);
284 psResult = CPLHTTPFetch(pszURL, papszOptions);
285 if (bQuiet404Error)
286 CPLPopErrorHandler();
287 }
288 CSLDestroy(papszOptions);
289
290 if (pszPostContent != nullptr && m_bMustCleanPersistentcxx . . $ OGRPLScenesDataV1Dataset#m_bMustCleanPersistent.)
291 {
292 papszOptions = CSLSetNameValue(nullptr, "CLOSE_PERSISTENT",
293 CPLSPrintf("PLSCENES:%p", this));
294 CPLHTTPDestroyResult(CPLHTTPFetch(m_osBaseURLcxx . . $ OGRPLScenesDataV1Dataset#m_osBaseURL., papszOptions));
295 CSLDestroy(papszOptions);
296 m_bMustCleanPersistentcxx . . $ OGRPLScenesDataV1Dataset#m_bMustCleanPersistent. = false;
297 }
298
299 if (psResult->pszErrBuf != nullptr)
300 {
301 if (!(bQuiet404Error && strstr(psResult->pszErrBuf, "404")))
302 {
303 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "%s",
304 psResult->pabyData ? (const char *)psResult->pabyData
305 : psResult->pszErrBuf);
306 }
307 CPLHTTPDestroyResult(psResult);
308 return nullptr;
309 }
310
311 if (!bExpectJSonReturn &&
312 (psResult->pabyData == nullptr || psResult->nDataLen == 0))
313 {
314 CPLHTTPDestroyResult(psResult);
315 return nullptr;
316 }
317
318 if (psResult->pabyData == nullptr)
319 {
320 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
321 "Empty content returned by server");
322 CPLHTTPDestroyResult(psResult);
323 return nullptr;
324 }
325
326 const char *pszText = reinterpret_cast<const char *>(psResult->pabyData);
327 #ifdef DEBUG_VERBOSE
328 CPLDebug("PLScenes", "%s", pszText);
329 #endif
330
331 json_object *poObj = nullptr;
332 if (!OGRJSonParsecxx . . $ OGRJSonParse(260f20870814049b).(pszText, &poObj, true))
333 {
334 CPLHTTPDestroyResult(psResult);
335 return nullptr;
336 }
337
338 CPLHTTPDestroyResult(psResult);
339
340 if (json_object_get_type(poObj) != json_type_object)
341 {
342 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
343 "Return is not a JSON dictionary");
344 json_object_put(poObj);
345 poObj = nullptr;
346 }
347
348 return poObj;
349 }
350
351 /************************************************************************/
352 /* InsertAPIKeyInURL() */
353 /************************************************************************/
354
355 CPLString OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::InsertAPIKeyInURLcxx . . $ OGRPLScenesDataV1Dataset#InsertAPIKeyInURL(f51f10d9da8cf4bd).(CPLString osURL)
356 {
357 if (STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(osURL, "http://"))
358 {
359 osURL = "http://" + m_osAPIKeycxx . . $ OGRPLScenesDataV1Dataset#m_osAPIKey. + ":@" + osURL.substr(strlen("http://"));
360 }
361 else if (STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(osURL, "https://"))
362 {
363 osURL =
364 "https://" + m_osAPIKeycxx . . $ OGRPLScenesDataV1Dataset#m_osAPIKey. + ":@" + osURL.substr(strlen("https://"));
365 }
366 return osURL;
367 }
368
369 /************************************************************************/
370 /* OpenRasterScene() */
371 /************************************************************************/
372
373 GDALDatasetcxx . . $ GDALDataset# *OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::OpenRasterScenecxx . . $ OGRPLScenesDataV1Dataset#OpenRasterScene(451a808db72b8b0c).(
374 GDALOpenInfocxx . . $ GDALOpenInfo# *poOpenInfo, CPLString osScene, CSLConstListcxx . . $ CSLConstList# papszOptions)
375 {
376 if (!(poOpenInfo->nOpenFlagscxx . . $ GDALOpenInfo#nOpenFlags. & GDAL_OF_RASTERcxx . . $ `gcore/gdal.h:1125:9`!))
377 {
378 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
379 "The scene option must only be used with vector access");
380 return nullptr;
381 }
382
383 int nActivationTimeout = atoicxx . . $ atoi(f5ecf3191ed0cd93).(CSLFetchNameValueDef(
384 poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "ACTIVATION_TIMEOUT", "3600"));
385
386 for (const auto &[pszKey, pszValue] : cpl::IterateNameValue(papszOptions))
387 {
388 if (!EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "api_key") && !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "scene") &&
389 !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "product_type") && !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "asset") &&
390 !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "catalog") && !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "itemtypes") &&
391 !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "version") && !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "follow_links") &&
392 !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "metadata"))
393 {
394 CPLError(CE_Failure, CPLE_NotSupportedcxx . . $ `port/cpl_error.h:118:9`!, "Unsupported option %s",
395 pszKey);
396 return nullptr;
397 }
398 }
399
400 const char *pszCatalog = CSLFetchNameValueDef(
401 papszOptions, "itemtypes",
402 CSLFetchNameValueDef(
403 papszOptions, "catalog",
404 CSLFetchNameValueDef(
405 poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "ITEMTYPES",
406 CSLFetchNameValue(poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "CATALOG"))));
407 if (pszCatalog == nullptr)
408 {
409 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Missing catalog");
410 return nullptr;
411 }
412
413 const char *pszProductType = CSLFetchNameValueDef(
414 papszOptions, "asset",
415 CSLFetchNameValueDef(
416 papszOptions, "product_type",
417 CSLFetchNameValueDef(poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "ASSET",
418 CSLFetchNameValue(poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions.,
419 "PRODUCT_TYPE"))));
420
421 CPLString osRasterURL;
422 osRasterURL = m_osBaseURLcxx . . $ OGRPLScenesDataV1Dataset#m_osBaseURL.;
423 osRasterURL += "item-types/";
424 osRasterURL += pszCatalog;
425 osRasterURL += "/items/";
426 osRasterURL += osScene;
427 osRasterURL += "/assets/";
428
429 time_t nStartTime = time(nullptr);
430 retry:
431 time_t nCurrentTime = time(nullptr);
432 if (nCurrentTime - nStartTime > nActivationTimeout)
433 {
434 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Activation timeout reached");
435 return nullptr;
436 }
437 json_object *poObj = RunRequestcxx . . $ OGRPLScenesDataV1Dataset#RunRequest(64c91ecc94621f11).(osRasterURL);
438 if (poObj == nullptr)
439 return nullptr;
440
441 json_object *poSubObj = CPL_json_object_object_getcxx . . $ CPL_json_object_object_get(34d28576f59d1ffc).(
442 poObj, pszProductType ? pszProductType : "visual");
443 if (poSubObj == nullptr)
444 {
445 if (pszProductType != nullptr && !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszProductType, "LIST"))
446 {
447 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Cannot find asset %s",
448 pszProductType);
449 json_object_put(poObj);
450 }
451 else
452 {
453 json_object_iter it;
454 it.key = nullptr;
455 it.val = nullptr;
456 it.entry = nullptr;
457 char **papszSubdatasets = nullptr;
458 int nSubDataset = 0;
459 json_object_object_foreachCcxx . . $ `port/cpl_json_header.h:34:9`!(poObj, it)
460 {
461 ++nSubDataset;
462 papszSubdatasets = CSLSetNameValue(
463 papszSubdatasets,
464 CPLSPrintf("SUBDATASET_%d_NAME", nSubDataset),
465 CPLSPrintf("Scene=%s of item types %s, asset %s",
466 osScene.c_str(), pszCatalog, it.key));
467 papszSubdatasets = CSLSetNameValue(
468 papszSubdatasets,
469 CPLSPrintf("SUBDATASET_%d_DESC", nSubDataset),
470 CPLSPrintf("PLScenes:version=Data_V1,itemtypes=%s,scene=%s,"
471 "asset=%s",
472 pszCatalog, osScene.c_str(), it.key));
473 }
474 json_object_put(poObj);
475 if (nSubDataset != 0)
476 {
477 GDALDatasetcxx . . $ GDALDataset# *poDS = new OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#();
478 poDS->SetMetadatacxx . . $ GDALMajorObject#SetMetadata(6c7b553169186fed).(papszSubdatasets, "SUBDATASETS");
479 CSLDestroy(papszSubdatasets);
480 return poDS;
481 }
482 }
483 return nullptr;
484 }
485
486 if (json_object_get_type(poSubObj) != json_type_object)
487 {
488 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Cannot find link");
489 json_object_put(poObj);
490 return nullptr;
491 }
492
493 json_object *poPermissions =
494 CPL_json_object_object_getcxx . . $ CPL_json_object_object_get(34d28576f59d1ffc).(poSubObj, "_permissions");
495 if (poPermissions != nullptr)
496 {
497 const char *pszPermissions =
498 json_object_to_json_string_ext(poPermissions, 0);
499 if (pszPermissions && strstr(pszPermissions, "download") == nullptr)
500 {
501 CPLError(CE_Warning, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
502 "You don't have download permissions for this product");
503 }
504 }
505
506 json_object *poLocation = CPL_json_object_object_getcxx . . $ CPL_json_object_object_get(34d28576f59d1ffc).(poSubObj, "location");
507 json_object *poStatus = CPL_json_object_object_getcxx . . $ CPL_json_object_object_get(34d28576f59d1ffc).(poSubObj, "status");
508 bool bActive = false;
509 if (poStatus != nullptr &&
510 json_object_get_type(poStatus) == json_type_string)
511 {
512 const char *pszStatus = json_object_get_string(poStatus);
513 if (EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszStatus, "activating"))
514 {
515 CPLDebug("PLScenes", "The product is in activation. Retrying...");
516 CPLSleep(nActivationTimeout == 1 ? 0.5 : 1.0);
517 poLocation = nullptr;
518 json_object_put(poObj);
519 goto retry;
520 }
521 bActive = EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszStatus, "active");
522 }
523 if (poLocation == nullptr ||
524 json_object_get_type(poLocation) != json_type_string || !bActive)
525 {
526 CPLDebug("PLScenes", "The product isn't activated yet. Activating it");
527 json_object *poActivate =
528 json_ex_get_object_by_pathcxx . . $ json_ex_get_object_by_path(34d28576f59d1ffc).(poSubObj, "_links.activate");
529 if (poActivate == nullptr ||
530 json_object_get_type(poActivate) != json_type_string)
531 {
532 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
533 "Cannot find link to activate scene %s", osScene.c_str());
534 json_object_put(poObj);
535 return nullptr;
536 }
537 CPLString osActivate = json_object_get_string(poActivate);
538 poLocation = nullptr;
539 json_object_put(poObj);
540 poObj = RunRequestcxx . . $ OGRPLScenesDataV1Dataset#RunRequest(64c91ecc94621f11).(osActivate, FALSEcxx . . $ `port/cpl_port.h:1180:9`!, "GET", false);
541 if (poObj != nullptr)
542 json_object_put(poObj);
543 poObj = nullptr;
544 CPLSleep(nActivationTimeout == 1 ? 0.5 : 1.0);
545 goto retry;
546 }
547
548 const char *pszLink = json_object_get_string(poLocation);
549
550 osRasterURL = pszLink ? pszLink : "";
551 json_object_put(poObj);
552 if (osRasterURL.empty())
553 {
554 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Cannot find link to scene %s",
555 osScene.c_str());
556 return nullptr;
557 }
558
559 osRasterURL = InsertAPIKeyInURLcxx . . $ OGRPLScenesDataV1Dataset#InsertAPIKeyInURL(f51f10d9da8cf4bd).(osRasterURL);
560
561 const bool bUseVSICURL =
562 CPLFetchBool(poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "RANDOM_ACCESS", true);
563 if (bUseVSICURL && !(STARTS_WITHcxx . . $ `port/cpl_port.h:541:9`!(m_osBaseURL, "/vsimem/")))
564 {
565 char *pszEscapedURL = CPLEscapeString(osRasterURL, -1, CPLES_URLcxx . . $ `port/cpl_string.h:157:9`!);
566 CPLString osTmpURL("/vsicurl?use_head=no&max_retry=3&empty_dir=yes&use_"
567 "redirect_url_if_no_query_string_params=yes&url=");
568 osTmpURL += pszEscapedURL;
569 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszEscapedURL);
570 CPLDebug("PLSCENES", "URL = %s", osTmpURL.c_str());
571
572 VSIStatBufL sStat;
573 if (VSIStatL(osTmpURL, &sStat) == 0 && sStat.st_size > 0)
574 {
575 osRasterURL = stdcxx . . $ std/::move(osTmpURL);
576 }
577 else
578 {
579 CPLDebug("PLSCENES", "Cannot use random access for that file");
580 }
581 }
582
583 char **papszAllowedDrivers = nullptr;
584 papszAllowedDrivers = CSLAddString(papszAllowedDrivers, "HTTP");
585 papszAllowedDrivers = CSLAddString(papszAllowedDrivers, "GTiff");
586 papszAllowedDrivers = CSLAddString(papszAllowedDrivers, "PNG");
587 papszAllowedDrivers = CSLAddString(papszAllowedDrivers, "JPEG");
588 papszAllowedDrivers = CSLAddString(papszAllowedDrivers, "NITF");
589 papszAllowedDrivers = CSLAddString(papszAllowedDrivers, "JP2KAK");
590 papszAllowedDrivers = CSLAddString(papszAllowedDrivers, "JP2ECW");
591 papszAllowedDrivers = CSLAddString(papszAllowedDrivers, "JP2MrSID");
592 papszAllowedDrivers = CSLAddString(papszAllowedDrivers, "JP2OpenJPEG");
593 GDALDatasetcxx . . $ GDALDataset# *poOutDS = (GDALDatasetcxx . . $ GDALDataset# *)GDALOpenExcxx . . $ GDALOpenEx(5f20ed644b617ec0).(
594 osRasterURL, GDAL_OF_RASTERcxx . . $ `gcore/gdal.h:1125:9`!, papszAllowedDrivers, nullptr, nullptr);
595 CSLDestroy(papszAllowedDrivers);
596 if (poOutDS)
597 {
598 if (CPLFetchBool(
599 papszOptions, "metadata",
600 CPLFetchBool(poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "METADATA", true)))
601 {
602 OGRLayercxx . . $ OGRLayer# *poLayer = GetLayerByNamecxx . . $ OGRPLScenesDataV1Dataset#GetLayerByName(5ddf27af0841fa5b).(pszCatalog);
603 if (poLayer != nullptr)
604 {
605 // Set a dummy name so that PAM goes here
606 CPLPushErrorHandler(CPLQuietErrorHandler);
607
608 stdcxx . . $ std/::string osTmpFilename =
609 VSIMemGenerateHiddenFilename("ogrplscenesDataV1");
610
611 poOutDS->SetDescriptioncxx . . $ GDALMajorObject#SetDescription(85c52e162fed56f9).(osTmpFilename.c_str());
612
613 /* Attach scene metadata. */
614 poLayer->SetAttributeFiltercxx . . $ OGRLayer#SetAttributeFilter(5c4306d8553b3696).(
615 CPLSPrintf("id = '%s'", osScene.c_str()));
616 OGRFeaturecxx . . $ OGRFeature# *poFeat = poLayer->GetNextFeaturecxx . . $ OGRLayer#GetNextFeature(ec9eceddec1d9563).();
617 if (poFeat)
618 {
619 for (int i = 0; i < poFeat->GetFieldCountcxx . . $ OGRFeature#GetFieldCount(455f465bc33b4cdf).(); i++)
620 {
621 if (poFeat->IsFieldSetAndNotNullcxx . . $ OGRFeature#IsFieldSetAndNotNull(4a668f6de69bf818).(i))
622 {
623 const char *pszKey =
624 poFeat->GetFieldDefnRefcxx . . $ OGRFeature#GetFieldDefnRef(53a8093e6b73def).(i)->GetNameRefcxx . . $ OGRFieldDefn#GetNameRef(a9f41ea0e82d88cf).();
625 const char *pszVal = poFeat->GetFieldAsStringcxx . . $ OGRFeature#GetFieldAsString(afbf07f2bc1a9437).(i);
626 if (strncmp(pszKey, "asset_", strlen("asset_")) ==
627 0 ||
628 strstr(pszVal, "https://") != nullptr ||
629 strcmp(pszKey, "columns") == 0 ||
630 strcmp(pszKey, "rows") == 0 ||
631 strcmp(pszKey, "epsg_code") == 0 ||
632 strcmp(pszKey, "origin_x") == 0 ||
633 strcmp(pszKey, "origin_y") == 0 ||
634 strcmp(pszKey, "permissions") == 0 ||
635 strcmp(pszKey, "acquired") ==
636 0 // Redundant with TIFFTAG_DATETIME
637 )
638 {
639 continue;
640 }
641 poOutDS->SetMetadataItemcxx . . $ GDALMajorObject#SetMetadataItem(216fc8eb9053a851).(pszKey, pszVal);
642 }
643 }
644 }
645 delete poFeat;
646
647 poOutDS->FlushCachecxx . . $ GDALDataset#FlushCache(426a86739463d040).(false);
648 VSIUnlink(osTmpFilename.c_str());
649 VSIUnlink(
650 stdcxx . . $ std/::string(osTmpFilename).append(".aux.xml").c_str());
651 CPLPopErrorHandler();
652 CPL_IGNORE_RET_VAL(osTmpFilename);
653 }
654 }
655
656 CPLErrorReset();
657 poOutDS->SetDescriptioncxx . . $ GDALMajorObject#SetDescription(85c52e162fed56f9).(poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename.);
658 }
659 else if (CPLGetLastErrorType() == CE_None)
660 {
661 poObj = RunRequestcxx . . $ OGRPLScenesDataV1Dataset#RunRequest(64c91ecc94621f11).(osRasterURL);
662 if (poObj == nullptr)
663 {
664 CPLError(
665 CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
666 "The generation of the product is in progress. Retry later");
667 }
668 else
669 {
670 CPLError(
671 CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "%s",
672 json_object_to_json_string_ext(poObj, JSON_C_TO_STRING_PRETTYcxx . . $ `<external>/5475662611906901010/json_object.h:58:9`!));
673 json_object_put(poObj);
674 }
675 }
676
677 return poOutDS;
678 }
679
680 /************************************************************************/
681 /* Open() */
682 /************************************************************************/
683
684 GDALDatasetcxx . . $ GDALDataset# *OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#::Opencxx . . $ OGRPLScenesDataV1Dataset#Open(905b7966ba8c1649).(GDALOpenInfocxx . . $ GDALOpenInfo# *poOpenInfo)
685 {
686 OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset# *poDS = new OGRPLScenesDataV1Datasetcxx . . $ OGRPLScenesDataV1Dataset#();
687
688 poDS->m_osBaseURLcxx . . $ OGRPLScenesDataV1Dataset#m_osBaseURL. =
689 CPLGetConfigOption("PL_URL", "https://api.planet.com/data/v1/");
690
691 char **papszOptions = CSLTokenizeStringComplex(
692 poOpenInfo->pszFilenamecxx . . $ GDALOpenInfo#pszFilename. + strlen("PLScenes:"), ",", TRUEcxx . . $ `port/cpl_port.h:1184:9`!, FALSEcxx . . $ `port/cpl_port.h:1180:9`!);
693
694 poDS->m_osAPIKeycxx . . $ OGRPLScenesDataV1Dataset#m_osAPIKey. = CSLFetchNameValueDef(
695 papszOptions, "api_key",
696 CSLFetchNameValueDef(poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "API_KEY",
697 CPLGetConfigOption("PL_API_KEY", "")));
698 if (poDS->m_osAPIKeycxx . . $ OGRPLScenesDataV1Dataset#m_osAPIKey..empty())
699 {
700 CPLError(
701 CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!,
702 "Missing PL_API_KEY configuration option or API_KEY open option");
703 delete poDS;
704 CSLDestroy(papszOptions);
705 return nullptr;
706 }
707
708 poDS->m_bFollowLinkscxx . . $ OGRPLScenesDataV1Dataset#m_bFollowLinks. = CPLTestBool(
709 CSLFetchNameValueDef(papszOptions, "follow_links",
710 CSLFetchNameValueDef(poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions.,
711 "FOLLOW_LINKS", "FALSE")));
712
713 poDS->m_osFiltercxx . . $ OGRPLScenesDataV1Dataset#m_osFilter. = CSLFetchNameValueDef(
714 papszOptions, "filter",
715 CSLFetchNameValueDef(poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "FILTER", ""));
716 poDS->m_osFiltercxx . . $ OGRPLScenesDataV1Dataset#m_osFilter..Trim();
717
718 const char *pszScene = CSLFetchNameValueDef(
719 papszOptions, "scene",
720 CSLFetchNameValue(poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "SCENE"));
721 if (pszScene)
722 {
723 GDALDatasetcxx . . $ GDALDataset# *poRasterDS =
724 poDS->OpenRasterScenecxx . . $ OGRPLScenesDataV1Dataset#OpenRasterScene(451a808db72b8b0c).(poOpenInfo, pszScene, papszOptions);
725 delete poDS;
726 CSLDestroy(papszOptions);
727 return poRasterDS;
728 }
729 else if ((poOpenInfo->nOpenFlagscxx . . $ GDALOpenInfo#nOpenFlags. & GDAL_OF_RASTERcxx . . $ `gcore/gdal.h:1125:9`!) &&
730 !(poOpenInfo->nOpenFlagscxx . . $ GDALOpenInfo#nOpenFlags. & GDAL_OF_VECTORcxx . . $ `gcore/gdal.h:1130:9`!))
731 {
732 CPLError(CE_Failure, CPLE_AppDefinedcxx . . $ `port/cpl_error.h:108:9`!, "Missing scene");
733 delete poDS;
734 CSLDestroy(papszOptions);
735 return nullptr;
736 }
737
738 for (char **papszIter = papszOptions; papszIter && *papszIter; papszIter++)
739 {
740 char *pszKey = nullptr;
741 const char *pszValue = CPLParseNameValue(*papszIter, &pszKey);
742 if (pszValue != nullptr)
743 {
744 if (!EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "api_key") && !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "version") &&
745 !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "catalog") && !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "itemtypes") &&
746 !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "follow_links") && !EQUALcxx . . $ `port/cpl_port.h:532:9`!(pszKey, "filter"))
747 {
748 CPLError(CE_Failure, CPLE_NotSupportedcxx . . $ `port/cpl_error.h:118:9`!,
749 "Unsupported option '%s'", pszKey);
750 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszKey);
751 delete poDS;
752 CSLDestroy(papszOptions);
753 return nullptr;
754 }
755 CPLFreecxx . . $ `port/cpl_conv.h:97:9`!(pszKey);
756 }
757 }
758
759 json_object *poObj =
760 poDS->RunRequestcxx . . $ OGRPLScenesDataV1Dataset#RunRequest(64c91ecc94621f11).((poDS->m_osBaseURLcxx . . $ OGRPLScenesDataV1Dataset#m_osBaseURL. + "item-types/").c_str());
761 if (poObj == nullptr)
762 {
763 delete poDS;
764 CSLDestroy(papszOptions);
765 return nullptr;
766 }
767
768 const char *pszCatalog = CSLFetchNameValueDef(
769 papszOptions, "itemtypes",
770 CSLFetchNameValueDef(
771 papszOptions, "catalog",
772 CSLFetchNameValueDef(
773 poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "ITEMTYPES",
774 CSLFetchNameValue(poOpenInfo->papszOpenOptionscxx . . $ GDALOpenInfo#papszOpenOptions., "CATALOG"))));
775 if (pszCatalog == nullptr)
776 {
777 // Establish (partial if there are other pages) layer list.
778 if (!poDS->ParseItemTypescxx . . $ OGRPLScenesDataV1Dataset#ParseItemTypes(98ad915997256310).(poObj, poDS->m_osNextItemTypesPageURLcxx . . $ OGRPLScenesDataV1Dataset#m_osNextItemTypesPageURL.))
779 {
780 delete poDS;
781 poDS = nullptr;
782 }
783 }
784 else
785 {
786 if (poDS->GetLayerByNamecxx . . $ OGRPLScenesDataV1Dataset#GetLayerByName(5ddf27af0841fa5b).(pszCatalog) == nullptr)
787 {
788 delete poDS;
789 poDS = nullptr;
790 }
791 }
792
793 json_object_put(poObj);
794
795 CSLDestroy(papszOptions);
796
797 if (!(poOpenInfo->nOpenFlagscxx . . $ GDALOpenInfo#nOpenFlags. & GDAL_OF_VECTORcxx . . $ `gcore/gdal.h:1130:9`!))
798 {
799 delete poDS;
800 return nullptr;
801 }
802
803 return poDS;
804 }