Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL
4 : * Purpose: Planetary drivers
5 : * Author: Even Rouault
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2023, Even Rouault
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "pdsdrivercore.h"
14 :
15 : #include "nasakeywordhandler.h"
16 :
17 : /************************************************************************/
18 : /* GetVICARLabelOffsetFromPDS3() */
19 : /************************************************************************/
20 :
21 5 : vsi_l_offset GetVICARLabelOffsetFromPDS3(const char *pszHdr, VSILFILE *fp,
22 : std::string &osVICARHeader)
23 : {
24 5 : const char *pszPDSVersionID = strstr(pszHdr, "PDS_VERSION_ID");
25 5 : int nOffset = 0;
26 5 : if (pszPDSVersionID)
27 5 : nOffset = static_cast<int>(pszPDSVersionID - pszHdr);
28 :
29 10 : NASAKeywordHandler oKeywords;
30 5 : if (oKeywords.Ingest(fp, nOffset))
31 : {
32 : const int nRecordBytes =
33 5 : atoi(oKeywords.GetKeyword("RECORD_BYTES", "0"));
34 : const int nImageHeader =
35 5 : atoi(oKeywords.GetKeyword("^IMAGE_HEADER", "0"));
36 5 : if (nRecordBytes > 0 && nImageHeader > 0)
37 : {
38 5 : const auto nImgHeaderOffset =
39 5 : static_cast<vsi_l_offset>(nImageHeader - 1) * nRecordBytes;
40 5 : osVICARHeader.resize(1024);
41 : size_t nMemb;
42 5 : if (VSIFSeekL(fp, nImgHeaderOffset, SEEK_SET) == 0 &&
43 5 : (nMemb = VSIFReadL(&osVICARHeader[0], 1, osVICARHeader.size(),
44 10 : fp)) != 0 &&
45 5 : osVICARHeader.find("LBLSIZE") != std::string::npos)
46 : {
47 5 : osVICARHeader.resize(nMemb);
48 5 : return nImgHeaderOffset;
49 : }
50 : }
51 : }
52 0 : return 0;
53 : }
54 :
55 : /************************************************************************/
56 : /* PDSDriverIdentify() */
57 : /************************************************************************/
58 :
59 54163 : int PDSDriverIdentify(GDALOpenInfo *poOpenInfo)
60 :
61 : {
62 54163 : if (poOpenInfo->pabyHeader == nullptr || poOpenInfo->fpL == nullptr)
63 48620 : return FALSE;
64 :
65 5543 : const char *pszHdr = reinterpret_cast<char *>(poOpenInfo->pabyHeader);
66 5543 : if (strstr(pszHdr, "PDS_VERSION_ID") == nullptr &&
67 5466 : strstr(pszHdr, "ODL_VERSION_ID") == nullptr)
68 : {
69 5466 : return FALSE;
70 : }
71 :
72 : // Some PDS3 images include a VICAR header pointed by ^IMAGE_HEADER.
73 : // If the user sets GDAL_TRY_PDS3_WITH_VICAR=YES, then we will gracefully
74 : // hand over the file to the VICAR dataset.
75 154 : std::string unused;
76 77 : if (CPLTestBool(CPLGetConfigOption("GDAL_TRY_PDS3_WITH_VICAR", "NO")) &&
77 78 : !STARTS_WITH(poOpenInfo->pszFilename, "/vsisubfile/") &&
78 1 : GetVICARLabelOffsetFromPDS3(pszHdr, poOpenInfo->fpL, unused) > 0)
79 : {
80 1 : CPLDebug("PDS3", "File is detected to have a VICAR header. "
81 : "Handing it over to the VICAR driver");
82 1 : return FALSE;
83 : }
84 :
85 76 : return TRUE;
86 : }
87 :
88 : /************************************************************************/
89 : /* PDSDriverSetCommonMetadata() */
90 : /************************************************************************/
91 :
92 1293 : void PDSDriverSetCommonMetadata(GDALDriver *poDriver)
93 : {
94 1293 : poDriver->SetDescription(PDS_DRIVER_NAME);
95 1293 : poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
96 1293 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "NASA Planetary Data System");
97 1293 : poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/pds.html");
98 1293 : poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
99 :
100 1293 : poDriver->pfnIdentify = PDSDriverIdentify;
101 1293 : poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES");
102 1293 : }
103 :
104 : /************************************************************************/
105 : /* PDS4DriverIdentify() */
106 : /************************************************************************/
107 :
108 64219 : int PDS4DriverIdentify(GDALOpenInfo *poOpenInfo)
109 : {
110 64219 : if (STARTS_WITH_CI(poOpenInfo->pszFilename, "PDS4:"))
111 30 : return TRUE;
112 64189 : if (poOpenInfo->nHeaderBytes == 0)
113 51134 : return FALSE;
114 :
115 13055 : const auto HasProductSomethingRootElement = [](const char *pszStr)
116 : {
117 25664 : return strstr(pszStr, "Product_Observational") != nullptr ||
118 25664 : strstr(pszStr, "Product_Ancillary") != nullptr ||
119 25664 : strstr(pszStr, "Product_Collection") != nullptr;
120 : };
121 13055 : const auto HasPDS4Schema = [](const char *pszStr)
122 13055 : { return strstr(pszStr, "://pds.nasa.gov/pds4/pds/v1") != nullptr; };
123 :
124 13055 : for (int i = 0; i < 2; ++i)
125 : {
126 13055 : const char *pszHeader =
127 : reinterpret_cast<const char *>(poOpenInfo->pabyHeader);
128 13055 : int nMatches = 0;
129 13055 : if (HasProductSomethingRootElement(pszHeader))
130 448 : nMatches++;
131 13055 : if (HasPDS4Schema(pszHeader))
132 448 : nMatches++;
133 13055 : if (nMatches == 2)
134 : {
135 448 : return TRUE;
136 : }
137 12607 : if (i == 0)
138 : {
139 12607 : if (nMatches == 0 || poOpenInfo->nHeaderBytes >= 8192)
140 : break;
141 : // If we have found one of the 2 matching elements to identify
142 : // PDS4 products, but have only ingested the default 1024 bytes,
143 : // then try to ingest more.
144 0 : poOpenInfo->TryToIngest(8192);
145 : }
146 : }
147 12607 : return FALSE;
148 : }
149 :
150 : /************************************************************************/
151 : /* PDS4DriverSetCommonMetadata() */
152 : /************************************************************************/
153 :
154 1293 : void PDS4DriverSetCommonMetadata(GDALDriver *poDriver)
155 : {
156 1293 : poDriver->SetDescription(PDS4_DRIVER_NAME);
157 1293 : poDriver->SetMetadataItem(GDAL_DCAP_VECTOR, "YES");
158 1293 : poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
159 1293 : poDriver->SetMetadataItem(GDAL_DCAP_CREATE_LAYER, "YES");
160 1293 : poDriver->SetMetadataItem(GDAL_DCAP_CREATE_FIELD, "YES");
161 1293 : poDriver->SetMetadataItem(GDAL_DCAP_DELETE_FIELD, "YES");
162 1293 : poDriver->SetMetadataItem(GDAL_DCAP_REORDER_FIELDS, "YES");
163 1293 : poDriver->SetMetadataItem(GDAL_DMD_ALTER_FIELD_DEFN_FLAGS,
164 1293 : "Name Type WidthPrecision");
165 1293 : poDriver->SetMetadataItem(GDAL_DCAP_Z_GEOMETRIES, "YES");
166 :
167 1293 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME,
168 1293 : "NASA Planetary Data System 4");
169 1293 : poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/pds4.html");
170 1293 : poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "xml");
171 1293 : poDriver->SetMetadataItem(GDAL_DMD_CREATIONDATATYPES,
172 : "Byte Int8 UInt16 Int16 UInt32 Int32 Float32 "
173 1293 : "Float64 CFloat32 CFloat64");
174 1293 : poDriver->SetMetadataItem(GDAL_DMD_OPENOPTIONLIST, "<OpenOptionList/>");
175 1293 : poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
176 1293 : poDriver->SetMetadataItem(GDAL_DMD_SUBDATASETS, "YES");
177 1293 : poDriver->SetMetadataItem(GDAL_DMD_SUPPORTED_SQL_DIALECTS, "OGRSQL SQLITE");
178 :
179 1293 : poDriver->SetMetadataItem(
180 : GDAL_DMD_OPENOPTIONLIST,
181 : "<OpenOptionList>"
182 : " <Option name='LAT' type='string' scope='vector' description="
183 : "'Name of a field containing a Latitude value' default='Latitude'/>"
184 : " <Option name='LONG' type='string' scope='vector' description="
185 : "'Name of a field containing a Longitude value' default='Longitude'/>"
186 : " <Option name='ALT' type='string' scope='vector' description="
187 : "'Name of a field containing a Altitude value' default='Altitude'/>"
188 : " <Option name='WKT' type='string' scope='vector' description="
189 : "'Name of a field containing a geometry encoded in the WKT format' "
190 : "default='WKT'/>"
191 : " <Option name='KEEP_GEOM_COLUMNS' scope='vector' type='boolean' "
192 : "description="
193 : "'whether to add original x/y/geometry columns as regular fields.' "
194 : "default='NO' />"
195 1293 : "</OpenOptionList>");
196 :
197 1293 : poDriver->SetMetadataItem(
198 : GDAL_DMD_CREATIONOPTIONLIST,
199 : "<CreationOptionList>"
200 : " <Option name='IMAGE_FILENAME' type='string' scope='raster' "
201 : "description="
202 : "'Image filename'/>"
203 : " <Option name='IMAGE_EXTENSION' type='string' scope='raster' "
204 : "description="
205 : "'Extension of the binary raw/geotiff file'/>"
206 : " <Option name='CREATE_LABEL_ONLY' scope='raster' type='boolean' "
207 : "description="
208 : "'whether to create only the XML label when converting from an "
209 : "existing raw format.' default='NO' />"
210 : " <Option name='IMAGE_FORMAT' type='string-select' scope='raster' "
211 : "description='Format of the image file' default='RAW'>"
212 : " <Value>RAW</Value>"
213 : " <Value>GEOTIFF</Value>"
214 : " </Option>"
215 : #ifdef notdef
216 : " <Option name='GEOTIFF_OPTIONS' type='string' scope='raster' "
217 : "description='Comma separated list of KEY=VALUE tuples to forward "
218 : "to the GeoTIFF driver'/>"
219 : #endif
220 : " <Option name='INTERLEAVE' type='string-select' scope='raster' "
221 : "description="
222 : "'Pixel organization' default='BSQ'>"
223 : " <Value>BSQ</Value>"
224 : " <Value>BIP</Value>"
225 : " <Value>BIL</Value>"
226 : " </Option>"
227 : " <Option name='VAR_*' type='string' scope='raster,vector' "
228 : "description="
229 : "'Value to substitute to a variable in the template'/>"
230 : " <Option name='TEMPLATE' type='string' scope='raster,vector' "
231 : "description="
232 : "'.xml template to use'/>"
233 : " <Option name='USE_SRC_LABEL' type='boolean' scope='raster' "
234 : "description='Whether to use source label in PDS4 to PDS4 conversions' "
235 : "default='YES'/>"
236 : " <Option name='LATITUDE_TYPE' type='string-select' "
237 : "scope='raster,vector' "
238 : "description='Value of latitude_type' default='Planetocentric'>"
239 : " <Value>Planetocentric</Value>"
240 : " <Value>Planetographic</Value>"
241 : " </Option>"
242 : " <Option name='LONGITUDE_DIRECTION' type='string-select' "
243 : "scope='raster,vector' "
244 : "description='Value of longitude_direction' "
245 : "default='Positive East'>"
246 : " <Value>Positive East</Value>"
247 : " <Value>Positive West</Value>"
248 : " </Option>"
249 : " <Option name='RADII' type='string' scope='raster,vector' "
250 : "description='Value of form "
251 : "semi_major_radius,semi_minor_radius to override the ones of the SRS'/>"
252 : " <Option name='ARRAY_TYPE' type='string-select' scope='raster' "
253 : "description='Name of the "
254 : "Array XML element' default='Array_3D_Image'>"
255 : " <Value>Array</Value>"
256 : " <Value>Array_2D</Value>"
257 : " <Value>Array_2D_Image</Value>"
258 : " <Value>Array_2D_Map</Value>"
259 : " <Value>Array_2D_Spectrum</Value>"
260 : " <Value>Array_3D</Value>"
261 : " <Value>Array_3D_Image</Value>"
262 : " <Value>Array_3D_Movie</Value>"
263 : " <Value>Array_3D_Spectrum</Value>"
264 : " </Option>"
265 : " <Option name='ARRAY_IDENTIFIER' type='string' scope='raster' "
266 : "description='Identifier to put in the Array element'/>"
267 : " <Option name='UNIT' type='string' scope='raster' "
268 : "description='Name of the unit of the array elements'/>"
269 : " <Option name='BOUNDING_DEGREES' type='string' scope='raster,vector' "
270 : "description='Manually set bounding box with the syntax "
271 : "west_lon,south_lat,east_lon,north_lat'/>"
272 1293 : "</CreationOptionList>");
273 :
274 1293 : poDriver->SetMetadataItem(
275 : GDAL_DS_LAYER_CREATIONOPTIONLIST,
276 : "<LayerCreationOptionList>"
277 : " <Option name='TABLE_TYPE' type='string-select' description='Type of "
278 : "table' default='DELIMITED'>"
279 : " <Value>DELIMITED</Value>"
280 : " <Value>CHARACTER</Value>"
281 : " <Value>BINARY</Value>"
282 : " </Option>"
283 : " <Option name='LINE_ENDING' type='string-select' description="
284 : "'end-of-line sequence. Only applies for "
285 : "TABLE_TYPE=DELIMITED/CHARACTER' "
286 : "default='CRLF'>"
287 : " <Value>CRLF</Value>"
288 : " <Value>LF</Value>"
289 : " </Option>"
290 : " <Option name='GEOM_COLUMNS' type='string-select' description='How "
291 : "geometry is encoded' default='AUTO'>"
292 : " <Value>AUTO</Value>"
293 : " <Value>WKT</Value>"
294 : " <Value>LONG_LAT</Value>"
295 : " </Option>"
296 : " <Option name='CREATE_VRT' type='boolean' description='Whether to "
297 : "generate "
298 : "a OGR VRT file. Only applies for TABLE_TYPE=DELIMITED' default='YES'/>"
299 : " <Option name='LAT' type='string' description="
300 : "'Name of a field containing a Latitude value' default='Latitude'/>"
301 : " <Option name='LONG' type='string' description="
302 : "'Name of a field containing a Longitude value' default='Longitude'/>"
303 : " <Option name='ALT' type='string' description="
304 : "'Name of a field containing a Altitude value' default='Altitude'/>"
305 : " <Option name='WKT' type='string' description="
306 : "'Name of a field containing a WKT value' default='WKT'/>"
307 : " <Option name='SAME_DIRECTORY' type='boolean' description="
308 : "'Whether table files should be created in the same "
309 : "directory, or in a subdirectory' default='NO'/>"
310 1293 : "</LayerCreationOptionList>");
311 :
312 1293 : poDriver->SetMetadataItem(
313 : GDAL_DMD_CREATIONFIELDDATATYPES,
314 1293 : "Integer Integer64 Real String Date DateTime Time");
315 1293 : poDriver->SetMetadataItem(GDAL_DMD_CREATIONFIELDDATASUBTYPES, "Boolean");
316 :
317 1293 : poDriver->pfnIdentify = PDS4DriverIdentify;
318 1293 : poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES");
319 1293 : poDriver->SetMetadataItem(GDAL_DCAP_CREATE, "YES");
320 1293 : poDriver->SetMetadataItem(GDAL_DCAP_CREATECOPY, "YES");
321 1293 : }
322 :
323 : /************************************************************************/
324 : /* ISIS2DriverIdentify() */
325 : /************************************************************************/
326 :
327 54253 : int ISIS2DriverIdentify(GDALOpenInfo *poOpenInfo)
328 : {
329 54253 : if (poOpenInfo->pabyHeader == nullptr)
330 48597 : return FALSE;
331 :
332 5656 : if (strstr((const char *)poOpenInfo->pabyHeader, "^QUBE") == nullptr)
333 5567 : return FALSE;
334 :
335 89 : return TRUE;
336 : }
337 :
338 : /************************************************************************/
339 : /* ISIS2DriverSetCommonMetadata() */
340 : /************************************************************************/
341 :
342 1293 : void ISIS2DriverSetCommonMetadata(GDALDriver *poDriver)
343 : {
344 1293 : poDriver->SetDescription(ISIS2_DRIVER_NAME);
345 1293 : poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
346 1293 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME,
347 1293 : "USGS Astrogeology ISIS cube (Version 2)");
348 1293 : poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/isis2.html");
349 1293 : poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
350 1293 : poDriver->SetMetadataItem(GDAL_DMD_CREATIONDATATYPES,
351 1293 : "Byte Int16 UInt16 Float32 Float64");
352 :
353 1293 : poDriver->SetMetadataItem(
354 : GDAL_DMD_CREATIONOPTIONLIST,
355 : "<CreationOptionList>\n"
356 : " <Option name='LABELING_METHOD' type='string-select' "
357 : "default='ATTACHED'>\n"
358 : " <Value>ATTACHED</Value>"
359 : " <Value>DETACHED</Value>"
360 : " </Option>"
361 : " <Option name='IMAGE_EXTENSION' type='string' default='cub'/>\n"
362 1293 : "</CreationOptionList>\n");
363 :
364 1293 : poDriver->pfnIdentify = ISIS2DriverIdentify;
365 1293 : poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES");
366 1293 : poDriver->SetMetadataItem(GDAL_DCAP_CREATE, "YES");
367 1293 : }
368 :
369 : /************************************************************************/
370 : /* ISIS3DriverIdentify() */
371 : /************************************************************************/
372 :
373 54779 : int ISIS3DriverIdentify(GDALOpenInfo *poOpenInfo)
374 : {
375 54779 : if (poOpenInfo->fpL != nullptr && poOpenInfo->pabyHeader != nullptr &&
376 6089 : strstr((const char *)poOpenInfo->pabyHeader, "IsisCube") != nullptr)
377 531 : return TRUE;
378 :
379 54248 : return FALSE;
380 : }
381 :
382 : /************************************************************************/
383 : /* ISIS3DriverSetCommonMetadata() */
384 : /************************************************************************/
385 :
386 1293 : void ISIS3DriverSetCommonMetadata(GDALDriver *poDriver)
387 : {
388 1293 : poDriver->SetDescription(ISIS3_DRIVER_NAME);
389 1293 : poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
390 1293 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME,
391 1293 : "USGS Astrogeology ISIS cube (Version 3)");
392 1293 : poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/isis3.html");
393 1293 : poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
394 1293 : poDriver->SetMetadataItem(GDAL_DMD_EXTENSIONS, "lbl cub");
395 1293 : poDriver->SetMetadataItem(GDAL_DMD_CREATIONDATATYPES,
396 1293 : "Byte UInt16 Int16 Float32");
397 1293 : poDriver->SetMetadataItem(GDAL_DMD_OPENOPTIONLIST, "<OpenOptionList/>");
398 1293 : poDriver->SetMetadataItem(
399 : GDAL_DMD_CREATIONOPTIONLIST,
400 : "<CreationOptionList>"
401 : " <Option name='DATA_LOCATION' type='string-select' "
402 : "description='Location of pixel data' default='LABEL'>"
403 : " <Value>LABEL</Value>"
404 : " <Value>EXTERNAL</Value>"
405 : " <Value>GEOTIFF</Value>"
406 : " </Option>"
407 : " <Option name='GEOTIFF_AS_REGULAR_EXTERNAL' type='boolean' "
408 : "description='Whether the GeoTIFF file, if uncompressed, should be "
409 : "registered as a regular raw file' default='YES'/>"
410 : " <Option name='GEOTIFF_OPTIONS' type='string' "
411 : "description='Comma separated list of KEY=VALUE tuples to forward "
412 : "to the GeoTIFF driver'/>"
413 : " <Option name='EXTERNAL_FILENAME' type='string' "
414 : "description='Override default external filename. "
415 : "Only for DATA_LOCATION=EXTERNAL or GEOTIFF'/>"
416 : " <Option name='TILED' type='boolean' "
417 : "description='Whether the pixel data should be tiled' default='NO'/>"
418 : " <Option name='BLOCKXSIZE' type='int' "
419 : "description='Tile width' default='256'/>"
420 : " <Option name='BLOCKYSIZE' type='int' "
421 : "description='Tile height' default='256'/>"
422 : " <Option name='COMMENT' type='string' "
423 : "description='Comment to add into the label'/>"
424 : " <Option name='LATITUDE_TYPE' type='string-select' "
425 : "description='Value of Mapping.LatitudeType' default='Planetocentric'>"
426 : " <Value>Planetocentric</Value>"
427 : " <Value>Planetographic</Value>"
428 : " </Option>"
429 : " <Option name='LONGITUDE_DIRECTION' type='string-select' "
430 : "description='Value of Mapping.LongitudeDirection' "
431 : "default='PositiveEast'>"
432 : " <Value>PositiveEast</Value>"
433 : " <Value>PositiveWest</Value>"
434 : " </Option>"
435 : " <Option name='TARGET_NAME' type='string' description='Value of "
436 : "Mapping.TargetName'/>"
437 : " <Option name='FORCE_360' type='boolean' "
438 : "description='Whether to force longitudes in [0,360] range' "
439 : "default='NO'/>"
440 : " <Option name='WRITE_BOUNDING_DEGREES' type='boolean' "
441 : "description='Whether to write Min/MaximumLong/Latitude values' "
442 : "default='YES'/>"
443 : " <Option name='BOUNDING_DEGREES' type='string' "
444 : "description='Manually set bounding box with the syntax "
445 : "min_long,min_lat,max_long,max_lat'/>"
446 : " <Option name='USE_SRC_LABEL' type='boolean' "
447 : "description='Whether to use source label in ISIS3 to ISIS3 "
448 : "conversions' "
449 : "default='YES'/>"
450 : " <Option name='USE_SRC_MAPPING' type='boolean' "
451 : "description='Whether to use Mapping group from source label in "
452 : "ISIS3 to ISIS3 conversions' "
453 : "default='NO'/>"
454 : " <Option name='USE_SRC_HISTORY' type='boolean' "
455 : "description='Whether to use content pointed by the History object in "
456 : "ISIS3 to ISIS3 conversions' "
457 : "default='YES'/>"
458 : " <Option name='ADD_GDAL_HISTORY' type='boolean' "
459 : "description='Whether to add GDAL specific history in the content "
460 : "pointed "
461 : "by the History object in "
462 : "ISIS3 to ISIS3 conversions' "
463 : "default='YES'/>"
464 : " <Option name='GDAL_HISTORY' type='string' "
465 : "description='Manually defined GDAL history. Must be formatted as "
466 : "ISIS3 "
467 : "PDL. If not specified, it is automatically composed.'/>"
468 1293 : "</CreationOptionList>");
469 :
470 1293 : poDriver->pfnIdentify = ISIS3DriverIdentify;
471 1293 : poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES");
472 1293 : poDriver->SetMetadataItem(GDAL_DCAP_CREATE, "YES");
473 1293 : poDriver->SetMetadataItem(GDAL_DCAP_CREATECOPY, "YES");
474 1293 : }
475 :
476 : /************************************************************************/
477 : /* VICARGetLabelOffset() */
478 : /************************************************************************/
479 :
480 63852 : vsi_l_offset VICARGetLabelOffset(GDALOpenInfo *poOpenInfo)
481 :
482 : {
483 63852 : if (poOpenInfo->pabyHeader == nullptr || poOpenInfo->fpL == nullptr)
484 51109 : return static_cast<vsi_l_offset>(-1);
485 :
486 5463 : const auto HasFoundVICARKeywords = [](const char *pszHeader)
487 : {
488 5693 : return strstr(pszHeader, "LBLSIZE") != nullptr &&
489 230 : strstr(pszHeader, "FORMAT") != nullptr &&
490 230 : strstr(pszHeader, "NL") != nullptr &&
491 5923 : strstr(pszHeader, "NS") != nullptr &&
492 5693 : strstr(pszHeader, "NB") != nullptr;
493 : };
494 :
495 25486 : std::string osHeader;
496 12743 : const char *pszHeader =
497 : reinterpret_cast<const char *>(poOpenInfo->pabyHeader);
498 : // Some PDS3 images include a VICAR header pointed by ^IMAGE_HEADER.
499 : // If the user sets GDAL_TRY_PDS3_WITH_VICAR=YES, then we will gracefully
500 : // hand over the file to the VICAR dataset.
501 12743 : vsi_l_offset nOffset = 0;
502 : const bool bTryPDS3WithVicar =
503 12746 : CPLTestBool(CPLGetConfigOption("GDAL_TRY_PDS3_WITH_VICAR", "NO")) &&
504 3 : !STARTS_WITH(poOpenInfo->pszFilename, "/vsisubfile/");
505 12743 : if (bTryPDS3WithVicar && (nOffset = GetVICARLabelOffsetFromPDS3(
506 : pszHeader, poOpenInfo->fpL, osHeader)) > 0)
507 : {
508 2 : pszHeader = osHeader.c_str();
509 : }
510 :
511 12743 : if ((poOpenInfo->nOpenFlags & GDAL_OF_RASTER) == 0 &&
512 7286 : (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0)
513 : {
514 : // If opening in vector-only mode, then check when have NBB != 0
515 7284 : const char *pszNBB = strstr(pszHeader, "NBB");
516 7284 : if (pszNBB == nullptr)
517 7281 : return static_cast<vsi_l_offset>(-1);
518 3 : const char *pszEqualSign = strchr(pszNBB, '=');
519 3 : if (pszEqualSign == nullptr)
520 0 : return static_cast<vsi_l_offset>(-1);
521 3 : if (atoi(pszEqualSign + 1) == 0)
522 1 : return static_cast<vsi_l_offset>(-1);
523 : }
524 :
525 5461 : if (HasFoundVICARKeywords(pszHeader))
526 : {
527 : // If we find VICAR keywords, but the file starts with PDS_VERSION_ID,
528 : // it might be a PDS3 image that includes a VICAR header. Check if
529 : // this is the case.
530 228 : if (nOffset == 0 && STARTS_WITH(pszHeader, "PDS_VERSION_ID"))
531 : {
532 4 : if (!bTryPDS3WithVicar &&
533 2 : (!GDALGetDriverByName("PDS") ||
534 2 : poOpenInfo->IsSingleAllowedDriver("VICAR")))
535 : {
536 2 : const auto nOffset2 = GetVICARLabelOffsetFromPDS3(
537 : pszHeader, poOpenInfo->fpL, osHeader);
538 2 : if (nOffset2 > 0 && HasFoundVICARKeywords(osHeader.c_str()))
539 : {
540 2 : return nOffset2;
541 : }
542 : }
543 : }
544 226 : return nOffset;
545 : }
546 5233 : return static_cast<vsi_l_offset>(-1);
547 : }
548 :
549 : /************************************************************************/
550 : /* VICARDriverIdentify() */
551 : /************************************************************************/
552 :
553 63739 : static int VICARDriverIdentify(GDALOpenInfo *poOpenInfo)
554 : {
555 63739 : return VICARGetLabelOffset(poOpenInfo) != static_cast<vsi_l_offset>(-1);
556 : }
557 :
558 : /************************************************************************/
559 : /* VICARDriverSetCommonMetadata() */
560 : /************************************************************************/
561 :
562 1293 : void VICARDriverSetCommonMetadata(GDALDriver *poDriver)
563 : {
564 1293 : poDriver->SetDescription(VICAR_DRIVER_NAME);
565 1293 : poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
566 1293 : poDriver->SetMetadataItem(GDAL_DCAP_VECTOR, "YES");
567 1293 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "MIPL VICAR file");
568 1293 : poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/vicar.html");
569 1293 : poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
570 1293 : poDriver->SetMetadataItem(GDAL_DMD_CREATIONDATATYPES,
571 1293 : "Byte Int16 Int32 Float32 Float64 CFloat32");
572 1293 : poDriver->SetMetadataItem(
573 : GDAL_DMD_CREATIONOPTIONLIST,
574 : "<CreationOptionList>"
575 : " <Option name='GEOREF_FORMAT' type='string-select' "
576 : "description='How to encode georeferencing information' "
577 : "default='MIPL'>"
578 : " <Value>MIPL</Value>"
579 : " <Value>GEOTIFF</Value>"
580 : " </Option>"
581 : " <Option name='COORDINATE_SYSTEM_NAME' type='string-select' "
582 : "description='Value of MAP.COORDINATE_SYSTEM_NAME' "
583 : "default='PLANETOCENTRIC'>"
584 : " <Value>PLANETOCENTRIC</Value>"
585 : " <Value>PLANETOGRAPHIC</Value>"
586 : " </Option>"
587 : " <Option name='POSITIVE_LONGITUDE_DIRECTION' type='string-select' "
588 : "description='Value of MAP.POSITIVE_LONGITUDE_DIRECTION' "
589 : "default='EAST'>"
590 : " <Value>EAST</Value>"
591 : " <Value>WEST</Value>"
592 : " </Option>"
593 : " <Option name='TARGET_NAME' type='string' description='Value of "
594 : "MAP.TARGET_NAME'/>"
595 : " <Option name='USE_SRC_LABEL' type='boolean' "
596 : "description='Whether to use source label in VICAR to VICAR "
597 : "conversions' "
598 : "default='YES'/>"
599 : " <Option name='USE_SRC_MAP' type='boolean' "
600 : "description='Whether to use MAP property from source label in "
601 : "VICAR to VICAR conversions' "
602 : "default='NO'/>"
603 : " <Option name='LABEL' type='string' "
604 : "description='Label to use, either as a JSON string or a filename "
605 : "containing one'/>"
606 : " <Option name='COMPRESS' type='string-select' "
607 : "description='Compression method' default='NONE'>"
608 : " <Value>NONE</Value>"
609 : " <Value>BASIC</Value>"
610 : " <Value>BASIC2</Value>"
611 : " </Option>"
612 1293 : "</CreationOptionList>");
613 :
614 1293 : poDriver->pfnIdentify = VICARDriverIdentify;
615 1293 : poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES");
616 1293 : poDriver->SetMetadataItem(GDAL_DCAP_CREATE, "YES");
617 1293 : poDriver->SetMetadataItem(GDAL_DCAP_CREATECOPY, "YES");
618 1293 : }
619 :
620 : /************************************************************************/
621 : /* DeclareDeferredPDSPlugin() */
622 : /************************************************************************/
623 :
624 : #ifdef PLUGIN_FILENAME
625 : void DeclareDeferredPDSPlugin()
626 : {
627 : if (GDALGetDriverByName(PDS_DRIVER_NAME) != nullptr)
628 : {
629 : return;
630 : }
631 : {
632 : auto poDriver = new GDALPluginDriverProxy(PLUGIN_FILENAME);
633 : #ifdef PLUGIN_INSTALLATION_MESSAGE
634 : poDriver->SetMetadataItem(GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE,
635 : PLUGIN_INSTALLATION_MESSAGE);
636 : #endif
637 : PDSDriverSetCommonMetadata(poDriver);
638 : GetGDALDriverManager()->DeclareDeferredPluginDriver(poDriver);
639 : }
640 : {
641 : auto poDriver = new GDALPluginDriverProxy(PLUGIN_FILENAME);
642 : #ifdef PLUGIN_INSTALLATION_MESSAGE
643 : poDriver->SetMetadataItem(GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE,
644 : PLUGIN_INSTALLATION_MESSAGE);
645 : #endif
646 : PDS4DriverSetCommonMetadata(poDriver);
647 : GetGDALDriverManager()->DeclareDeferredPluginDriver(poDriver);
648 : }
649 : {
650 : auto poDriver = new GDALPluginDriverProxy(PLUGIN_FILENAME);
651 : #ifdef PLUGIN_INSTALLATION_MESSAGE
652 : poDriver->SetMetadataItem(GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE,
653 : PLUGIN_INSTALLATION_MESSAGE);
654 : #endif
655 : ISIS2DriverSetCommonMetadata(poDriver);
656 : GetGDALDriverManager()->DeclareDeferredPluginDriver(poDriver);
657 : }
658 : {
659 : auto poDriver = new GDALPluginDriverProxy(PLUGIN_FILENAME);
660 : #ifdef PLUGIN_INSTALLATION_MESSAGE
661 : poDriver->SetMetadataItem(GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE,
662 : PLUGIN_INSTALLATION_MESSAGE);
663 : #endif
664 : ISIS3DriverSetCommonMetadata(poDriver);
665 : GetGDALDriverManager()->DeclareDeferredPluginDriver(poDriver);
666 : }
667 : {
668 : auto poDriver = new GDALPluginDriverProxy(PLUGIN_FILENAME);
669 : #ifdef PLUGIN_INSTALLATION_MESSAGE
670 : poDriver->SetMetadataItem(GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE,
671 : PLUGIN_INSTALLATION_MESSAGE);
672 : #endif
673 : VICARDriverSetCommonMetadata(poDriver);
674 : GetGDALDriverManager()->DeclareDeferredPluginDriver(poDriver);
675 : }
676 : }
677 : #endif
|