Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: PCIDSK Database File
4 : * Purpose: Read/write PCIDSK Database File used by the PCI software, using
5 : * the external PCIDSK library.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
10 : * Copyright (c) 2009-2013, Even Rouault <even dot rouault at spatialys.com>
11 : *
12 : * SPDX-License-Identifier: MIT
13 : ****************************************************************************/
14 :
15 : #include "gdal_frmts.h"
16 : #include "pcidskdataset2.h"
17 : #include "pcidskdrivercore.h"
18 :
19 : #include <algorithm>
20 :
21 : const PCIDSK::PCIDSKInterfaces *PCIDSK2GetInterfaces(void);
22 :
23 : /************************************************************************/
24 : /* ==================================================================== */
25 : /* PCIDSK2Band */
26 : /* ==================================================================== */
27 : /************************************************************************/
28 :
29 : /************************************************************************/
30 : /* PCIDSK2Band() */
31 : /* */
32 : /* This constructor is used for main file channels. */
33 : /************************************************************************/
34 :
35 233 : PCIDSK2Band::PCIDSK2Band(PCIDSKFile *poFileIn, PCIDSKChannel *poChannelIn)
36 :
37 : {
38 233 : Initialize();
39 :
40 233 : poFile = poFileIn;
41 233 : poChannel = poChannelIn;
42 :
43 233 : nBlockXSize = static_cast<int>(poChannel->GetBlockWidth());
44 233 : nBlockYSize = static_cast<int>(poChannel->GetBlockHeight());
45 :
46 233 : eDataType = PCIDSK2Dataset::PCIDSKTypeToGDAL(poChannel->GetType());
47 :
48 233 : if (!STARTS_WITH_CI(poChannel->GetDescription().c_str(),
49 : "Contents Not Specified"))
50 10 : GDALMajorObject::SetDescription(poChannel->GetDescription().c_str());
51 :
52 : /* -------------------------------------------------------------------- */
53 : /* Do we have overviews? */
54 : /* -------------------------------------------------------------------- */
55 233 : RefreshOverviewList();
56 233 : }
57 :
58 : /************************************************************************/
59 : /* PCIDSK2Band() */
60 : /* */
61 : /* This constructor is used for overviews and bitmap segments */
62 : /* as bands. */
63 : /************************************************************************/
64 :
65 11 : PCIDSK2Band::PCIDSK2Band(PCIDSKChannel *poChannelIn)
66 :
67 : {
68 11 : Initialize();
69 :
70 11 : this->poChannel = poChannelIn;
71 :
72 11 : nBand = 1;
73 :
74 11 : nBlockXSize = static_cast<int>(poChannel->GetBlockWidth());
75 11 : nBlockYSize = static_cast<int>(poChannel->GetBlockHeight());
76 :
77 11 : nRasterXSize = static_cast<int>(poChannel->GetWidth());
78 11 : nRasterYSize = static_cast<int>(poChannel->GetHeight());
79 :
80 11 : eDataType = PCIDSK2Dataset::PCIDSKTypeToGDAL(poChannel->GetType());
81 :
82 11 : if (poChannel->GetType() == CHN_BIT)
83 : {
84 0 : PCIDSK2Band::SetMetadataItem("NBITS", "1", "IMAGE_STRUCTURE");
85 :
86 0 : if (!STARTS_WITH_CI(poChannel->GetDescription().c_str(),
87 : "Contents Not Specified"))
88 0 : GDALMajorObject::SetDescription(
89 0 : poChannel->GetDescription().c_str());
90 : }
91 11 : }
92 :
93 : /************************************************************************/
94 : /* Initialize() */
95 : /************************************************************************/
96 :
97 244 : void PCIDSK2Band::Initialize()
98 :
99 : {
100 244 : papszLastMDListValue = nullptr;
101 :
102 244 : poChannel = nullptr;
103 244 : poFile = nullptr;
104 244 : poDS = nullptr;
105 :
106 244 : bCheckedForColorTable = false;
107 244 : poColorTable = nullptr;
108 244 : nPCTSegNumber = -1;
109 :
110 244 : papszCategoryNames = nullptr;
111 244 : }
112 :
113 : /************************************************************************/
114 : /* ~PCIDSK2Band() */
115 : /************************************************************************/
116 :
117 732 : PCIDSK2Band::~PCIDSK2Band()
118 :
119 : {
120 255 : while (!apoOverviews.empty())
121 : {
122 11 : delete apoOverviews.back();
123 11 : apoOverviews.pop_back();
124 : }
125 244 : CSLDestroy(papszLastMDListValue);
126 244 : CSLDestroy(papszCategoryNames);
127 :
128 244 : delete poColorTable;
129 488 : }
130 :
131 : /************************************************************************/
132 : /* SetDescription() */
133 : /************************************************************************/
134 :
135 3 : void PCIDSK2Band::SetDescription(const char *pszDescription)
136 :
137 : {
138 3 : if (GetAccess() == GA_ReadOnly)
139 : {
140 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
141 : "Unable to set description on read-only file.");
142 0 : return;
143 : }
144 :
145 : try
146 : {
147 3 : poChannel->SetDescription(pszDescription);
148 :
149 3 : if (!STARTS_WITH_CI(poChannel->GetDescription().c_str(),
150 : "Contents Not Specified"))
151 3 : GDALMajorObject::SetDescription(
152 6 : poChannel->GetDescription().c_str());
153 : }
154 0 : catch (const PCIDSKException &ex)
155 : {
156 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
157 : }
158 : }
159 :
160 : /************************************************************************/
161 : /* GetCategoryNames() */
162 : /* */
163 : /* Offer category names from Class_*_ metadata. */
164 : /************************************************************************/
165 :
166 4 : char **PCIDSK2Band::GetCategoryNames()
167 :
168 : {
169 : // already scanned?
170 4 : if (papszCategoryNames != nullptr)
171 0 : return papszCategoryNames;
172 :
173 : try
174 : {
175 8 : std::vector<std::string> aosMDKeys = poChannel->GetMetadataKeys();
176 4 : int nClassCount = 0;
177 4 : constexpr int nMaxClasses = 10000;
178 4 : papszCategoryNames = reinterpret_cast<char **>(
179 4 : CPLCalloc(nMaxClasses + 1, sizeof(char *)));
180 :
181 7 : for (size_t i = 0; i < aosMDKeys.size(); i++)
182 : {
183 3 : CPLString osKey = aosMDKeys[i];
184 :
185 : // is this a "Class_n_name" keyword?
186 3 : if (!STARTS_WITH_CI(osKey, "Class_"))
187 3 : continue;
188 :
189 0 : if (!EQUAL(osKey.c_str() + osKey.size() - 5, "_name"))
190 0 : continue;
191 :
192 : // Ignore unreasonable class values.
193 0 : int iClass = atoi(osKey.c_str() + 6);
194 :
195 0 : if (iClass < 0 || iClass > 10000)
196 0 : continue;
197 :
198 : // Fetch the name.
199 0 : CPLString osName = poChannel->GetMetadataValue(osKey);
200 :
201 : // do we need to put in place dummy class names for missing values?
202 0 : if (iClass >= nClassCount)
203 : {
204 0 : while (iClass >= nClassCount)
205 : {
206 0 : papszCategoryNames[nClassCount++] = CPLStrdup("");
207 0 : papszCategoryNames[nClassCount] = nullptr;
208 : }
209 : }
210 :
211 : // Replace target category name.
212 0 : CPLFree(papszCategoryNames[iClass]);
213 0 : papszCategoryNames[iClass] = nullptr;
214 :
215 0 : papszCategoryNames[iClass] = CPLStrdup(osName);
216 : }
217 :
218 4 : if (nClassCount == 0)
219 4 : return GDALPamRasterBand::GetCategoryNames();
220 :
221 0 : return papszCategoryNames;
222 : }
223 0 : catch (const PCIDSKException &ex)
224 : {
225 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
226 0 : return nullptr;
227 : }
228 : }
229 :
230 : /************************************************************************/
231 : /* CheckForColorTable() */
232 : /************************************************************************/
233 :
234 27 : bool PCIDSK2Band::CheckForColorTable()
235 :
236 : {
237 27 : if (bCheckedForColorTable || poFile == nullptr)
238 14 : return true;
239 :
240 13 : bCheckedForColorTable = true;
241 :
242 : try
243 : {
244 : /* --------------------------------------------------------------------
245 : */
246 : /* Try to find an appropriate PCT segment to use. */
247 : /* --------------------------------------------------------------------
248 : */
249 : std::string osDefaultPCT =
250 39 : poChannel->GetMetadataValue("DEFAULT_PCT_REF");
251 13 : PCIDSKSegment *poPCTSeg = nullptr;
252 :
253 : // If there is no metadata, assume a single PCT in a file with only
254 : // one raster band must be intended for it.
255 25 : if (osDefaultPCT.empty() && poDS != nullptr &&
256 12 : poDS->GetRasterCount() == 1)
257 : {
258 12 : poPCTSeg = poFile->GetSegment(SEG_PCT, "");
259 12 : if (poPCTSeg != nullptr &&
260 12 : poFile->GetSegment(SEG_PCT, "", poPCTSeg->GetSegmentNumber()) !=
261 : nullptr)
262 0 : poPCTSeg = nullptr;
263 : }
264 : // Parse default PCT ref assuming an in file reference.
265 2 : else if (!osDefaultPCT.empty() &&
266 1 : strstr(osDefaultPCT.c_str(), "PCT:") != nullptr)
267 : {
268 1 : poPCTSeg = poFile->GetSegment(
269 1 : atoi(strstr(osDefaultPCT.c_str(), "PCT:") + 4));
270 : }
271 :
272 13 : if (poPCTSeg != nullptr)
273 : {
274 1 : poColorTable = new GDALColorTable();
275 : unsigned char abyPCT[768];
276 :
277 1 : PCIDSK_PCT *poPCT = dynamic_cast<PCIDSK_PCT *>(poPCTSeg);
278 1 : if (poPCT)
279 : {
280 1 : nPCTSegNumber = poPCTSeg->GetSegmentNumber();
281 :
282 1 : poPCT->ReadPCT(abyPCT);
283 :
284 257 : for (int i = 0; i < 256; i++)
285 : {
286 : GDALColorEntry sEntry;
287 :
288 256 : sEntry.c1 = abyPCT[256 * 0 + i];
289 256 : sEntry.c2 = abyPCT[256 * 1 + i];
290 256 : sEntry.c3 = abyPCT[256 * 2 + i];
291 256 : sEntry.c4 = 255;
292 256 : poColorTable->SetColorEntry(i, &sEntry);
293 : }
294 : }
295 : }
296 :
297 : /* --------------------------------------------------------------------
298 : */
299 : /* If we did not find an appropriate PCT segment, check for */
300 : /* Class_n color data from which to construct a color table. */
301 : /* --------------------------------------------------------------------
302 : */
303 26 : std::vector<std::string> aosMDKeys = poChannel->GetMetadataKeys();
304 :
305 23 : for (size_t i = 0; i < aosMDKeys.size(); i++)
306 : {
307 10 : CPLString osKey = aosMDKeys[i];
308 :
309 : // is this a "Class_n_name" keyword?
310 :
311 10 : if (!STARTS_WITH_CI(osKey, "Class_"))
312 10 : continue;
313 :
314 0 : if (!EQUAL(osKey.c_str() + osKey.size() - 6, "_Color"))
315 0 : continue;
316 :
317 : // Ignore unreasonable class values.
318 0 : const int iClass = atoi(osKey.c_str() + 6);
319 :
320 0 : if (iClass < 0 || iClass > 10000)
321 0 : continue;
322 :
323 : // Fetch and parse the RGB value "(RGB:red green blue)"
324 0 : CPLString osRGB = poChannel->GetMetadataValue(osKey);
325 :
326 0 : if (!STARTS_WITH_CI(osRGB, "(RGB:"))
327 0 : continue;
328 :
329 : int nRed, nGreen, nBlue;
330 0 : if (sscanf(osRGB.c_str() + 5, "%d %d %d", &nRed, &nGreen, &nBlue) !=
331 : 3)
332 0 : continue;
333 :
334 : // we have an entry - apply to the color table.
335 : GDALColorEntry sEntry;
336 :
337 0 : sEntry.c1 = (short)nRed;
338 0 : sEntry.c2 = (short)nGreen;
339 0 : sEntry.c3 = (short)nBlue;
340 0 : sEntry.c4 = 255;
341 :
342 0 : if (poColorTable == nullptr)
343 : {
344 0 : CPLDebug("PCIDSK",
345 : "Using Class_n_Color metadata for color table.");
346 0 : poColorTable = new GDALColorTable();
347 : }
348 :
349 0 : poColorTable->SetColorEntry(iClass, &sEntry);
350 : }
351 : }
352 0 : catch (const PCIDSKException &ex)
353 : {
354 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
355 0 : return false;
356 : }
357 :
358 13 : return true;
359 : }
360 :
361 : /************************************************************************/
362 : /* GetColorTable() */
363 : /************************************************************************/
364 :
365 10 : GDALColorTable *PCIDSK2Band::GetColorTable()
366 :
367 : {
368 10 : CheckForColorTable();
369 :
370 10 : if (poColorTable)
371 2 : return poColorTable;
372 :
373 8 : return GDALPamRasterBand::GetColorTable();
374 : }
375 :
376 : /************************************************************************/
377 : /* SetColorTable() */
378 : /************************************************************************/
379 :
380 2 : CPLErr PCIDSK2Band::SetColorTable(GDALColorTable *poCT)
381 :
382 : {
383 2 : if (!CheckForColorTable())
384 0 : return CE_Failure;
385 :
386 : // no color tables on overviews.
387 2 : if (poFile == nullptr)
388 0 : return CE_Failure;
389 :
390 2 : if (GetAccess() == GA_ReadOnly)
391 : {
392 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
393 : "Unable to set color table on read-only file.");
394 0 : return CE_Failure;
395 : }
396 :
397 : try
398 : {
399 : /* --------------------------------------------------------------------
400 : */
401 : /* Are we trying to delete the color table? */
402 : /* --------------------------------------------------------------------
403 : */
404 2 : if (poCT == nullptr)
405 : {
406 1 : delete poColorTable;
407 1 : poColorTable = nullptr;
408 :
409 1 : if (nPCTSegNumber != -1)
410 1 : poFile->DeleteSegment(nPCTSegNumber);
411 1 : poChannel->SetMetadataValue("DEFAULT_PCT_REF", "");
412 1 : nPCTSegNumber = -1;
413 :
414 1 : return CE_None;
415 : }
416 :
417 : /* --------------------------------------------------------------------
418 : */
419 : /* Do we need to create the segment? If so, also set the */
420 : /* default pct metadata. */
421 : /* --------------------------------------------------------------------
422 : */
423 1 : if (nPCTSegNumber == -1)
424 : {
425 1 : nPCTSegNumber = poFile->CreateSegment(
426 1 : "PCTTable", "Default Pseudo-Color Table", SEG_PCT, 0);
427 :
428 1 : CPLString osRef;
429 1 : osRef.Printf("gdb:/{PCT:%d}", nPCTSegNumber);
430 1 : poChannel->SetMetadataValue("DEFAULT_PCT_REF", osRef);
431 : }
432 :
433 : /* --------------------------------------------------------------------
434 : */
435 : /* Write out the PCT. */
436 : /* --------------------------------------------------------------------
437 : */
438 1 : const int nColorCount = std::min(256, poCT->GetColorEntryCount());
439 :
440 : unsigned char abyPCT[768];
441 1 : memset(abyPCT, 0, 768);
442 :
443 4 : for (int i = 0; i < nColorCount; i++)
444 : {
445 : GDALColorEntry sEntry;
446 :
447 3 : poCT->GetColorEntryAsRGB(i, &sEntry);
448 3 : abyPCT[256 * 0 + i] = (unsigned char)sEntry.c1;
449 3 : abyPCT[256 * 1 + i] = (unsigned char)sEntry.c2;
450 3 : abyPCT[256 * 2 + i] = (unsigned char)sEntry.c3;
451 : }
452 :
453 : PCIDSK_PCT *poPCT =
454 1 : dynamic_cast<PCIDSK_PCT *>(poFile->GetSegment(nPCTSegNumber));
455 1 : if (poPCT)
456 1 : poPCT->WritePCT(abyPCT);
457 :
458 1 : delete poColorTable;
459 1 : poColorTable = poCT->Clone();
460 : }
461 :
462 : /* -------------------------------------------------------------------- */
463 : /* Trap exceptions. */
464 : /* -------------------------------------------------------------------- */
465 0 : catch (const PCIDSKException &ex)
466 : {
467 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
468 0 : return CE_Failure;
469 : }
470 :
471 1 : return CE_None;
472 : }
473 :
474 : /************************************************************************/
475 : /* GetColorInterpretation() */
476 : /************************************************************************/
477 :
478 15 : GDALColorInterp PCIDSK2Band::GetColorInterpretation()
479 :
480 : {
481 15 : CheckForColorTable();
482 :
483 15 : if (poColorTable != nullptr)
484 1 : return GCI_PaletteIndex;
485 :
486 14 : return GDALPamRasterBand::GetColorInterpretation();
487 : }
488 :
489 : /************************************************************************/
490 : /* RefreshOverviewList() */
491 : /************************************************************************/
492 :
493 234 : void PCIDSK2Band::RefreshOverviewList()
494 :
495 : {
496 : /* -------------------------------------------------------------------- */
497 : /* Clear existing overviews. */
498 : /* -------------------------------------------------------------------- */
499 234 : while (!apoOverviews.empty())
500 : {
501 0 : delete apoOverviews.back();
502 0 : apoOverviews.pop_back();
503 : }
504 :
505 : /* -------------------------------------------------------------------- */
506 : /* Fetch overviews. */
507 : /* -------------------------------------------------------------------- */
508 245 : for (int iOver = 0; iOver < poChannel->GetOverviewCount(); iOver++)
509 : {
510 11 : auto poOvrBand = new PCIDSK2Band(poChannel->GetOverview(iOver));
511 11 : poOvrBand->eAccess = eAccess;
512 11 : apoOverviews.push_back(poOvrBand);
513 : }
514 234 : }
515 :
516 : /************************************************************************/
517 : /* IReadBlock() */
518 : /************************************************************************/
519 :
520 262 : CPLErr PCIDSK2Band::IReadBlock(int iBlockX, int iBlockY, void *pData)
521 :
522 : {
523 : try
524 : {
525 262 : poChannel->ReadBlock(iBlockX + iBlockY * nBlocksPerRow, pData);
526 :
527 : // Do we need to upsample 1bit to 8bit?
528 262 : if (poChannel->GetType() == CHN_BIT)
529 : {
530 0 : GByte *pabyData = reinterpret_cast<GByte *>(pData);
531 :
532 0 : for (int ii = nBlockXSize * nBlockYSize - 1; ii >= 0; ii--)
533 : {
534 0 : if ((pabyData[ii >> 3] & (0x80 >> (ii & 0x7))))
535 0 : pabyData[ii] = 1;
536 : else
537 0 : pabyData[ii] = 0;
538 : }
539 : }
540 :
541 262 : return CE_None;
542 : }
543 0 : catch (const PCIDSKException &ex)
544 : {
545 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
546 0 : return CE_Failure;
547 : }
548 : }
549 :
550 : /************************************************************************/
551 : /* IWriteBlock() */
552 : /************************************************************************/
553 :
554 645 : CPLErr PCIDSK2Band::IWriteBlock(int iBlockX, int iBlockY, void *pData)
555 :
556 : {
557 : try
558 : {
559 645 : poChannel->WriteBlock(iBlockX + iBlockY * nBlocksPerRow, pData);
560 : }
561 0 : catch (const PCIDSKException &ex)
562 : {
563 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
564 0 : return CE_Failure;
565 : }
566 :
567 645 : return CE_None;
568 : }
569 :
570 : /************************************************************************/
571 : /* GetOverviewCount() */
572 : /************************************************************************/
573 :
574 16 : int PCIDSK2Band::GetOverviewCount()
575 :
576 : {
577 16 : if (!apoOverviews.empty())
578 5 : return static_cast<int>(apoOverviews.size());
579 :
580 11 : return GDALPamRasterBand::GetOverviewCount();
581 : }
582 :
583 : /************************************************************************/
584 : /* GetOverview() */
585 : /************************************************************************/
586 :
587 6 : GDALRasterBand *PCIDSK2Band::GetOverview(int iOverview)
588 :
589 : {
590 6 : if (iOverview < 0 || iOverview >= static_cast<int>(apoOverviews.size()))
591 1 : return GDALPamRasterBand::GetOverview(iOverview);
592 :
593 5 : return apoOverviews[iOverview];
594 : }
595 :
596 : /************************************************************************/
597 : /* SetMetadata() */
598 : /************************************************************************/
599 :
600 2 : CPLErr PCIDSK2Band::SetMetadata(char **papszMD, const char *pszDomain)
601 :
602 : {
603 : /* -------------------------------------------------------------------- */
604 : /* PCIDSK only supports metadata in the default domain. */
605 : /* -------------------------------------------------------------------- */
606 2 : if (pszDomain != nullptr && strlen(pszDomain) > 0)
607 0 : return GDALPamRasterBand::SetMetadata(papszMD, pszDomain);
608 :
609 : /* -------------------------------------------------------------------- */
610 : /* Set each item individually. */
611 : /* -------------------------------------------------------------------- */
612 2 : CSLDestroy(papszLastMDListValue);
613 2 : papszLastMDListValue = nullptr;
614 2 : m_oCacheMetadataItem.clear();
615 :
616 2 : if (GetAccess() == GA_ReadOnly)
617 : {
618 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
619 : "Unable to set metadata on read-only file.");
620 0 : return CE_Failure;
621 : }
622 :
623 : try
624 : {
625 5 : for (int iItem = 0; papszMD && papszMD[iItem]; iItem++)
626 : {
627 3 : char *pszItemName = nullptr;
628 :
629 : const char *pszItemValue =
630 3 : CPLParseNameValue(papszMD[iItem], &pszItemName);
631 3 : if (pszItemName != nullptr)
632 : {
633 3 : poChannel->SetMetadataValue(pszItemName, pszItemValue);
634 3 : CPLFree(pszItemName);
635 : }
636 : }
637 : }
638 0 : catch (const PCIDSKException &ex)
639 : {
640 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
641 0 : return CE_Failure;
642 : }
643 :
644 2 : return CE_None;
645 : }
646 :
647 : /************************************************************************/
648 : /* SetMetadataItem() */
649 : /************************************************************************/
650 :
651 5 : CPLErr PCIDSK2Band::SetMetadataItem(const char *pszName, const char *pszValue,
652 : const char *pszDomain)
653 :
654 : {
655 : /* -------------------------------------------------------------------- */
656 : /* PCIDSK only supports metadata in the default domain. */
657 : /* -------------------------------------------------------------------- */
658 5 : if (pszDomain != nullptr && strlen(pszDomain) > 0)
659 1 : return GDALPamRasterBand::SetMetadataItem(pszName, pszValue, pszDomain);
660 :
661 : /* -------------------------------------------------------------------- */
662 : /* Set on the file. */
663 : /* -------------------------------------------------------------------- */
664 4 : CSLDestroy(papszLastMDListValue);
665 4 : papszLastMDListValue = nullptr;
666 4 : m_oCacheMetadataItem.clear();
667 :
668 4 : if (GetAccess() == GA_ReadOnly)
669 : {
670 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
671 : "Unable to set metadata on read-only file.");
672 0 : return CE_Failure;
673 : }
674 :
675 : try
676 : {
677 4 : if (!pszValue)
678 0 : pszValue = "";
679 4 : poChannel->SetMetadataValue(pszName, pszValue);
680 : }
681 0 : catch (const PCIDSKException &ex)
682 : {
683 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
684 0 : return CE_Failure;
685 : }
686 :
687 4 : return CE_None;
688 : }
689 :
690 : /************************************************************************/
691 : /* GetMetadataDomainList() */
692 : /************************************************************************/
693 :
694 0 : char **PCIDSK2Band::GetMetadataDomainList()
695 : {
696 0 : return BuildMetadataDomainList(GDALPamRasterBand::GetMetadataDomainList(),
697 0 : TRUE, "", nullptr);
698 : }
699 :
700 : /************************************************************************/
701 : /* GetMetadataItem() */
702 : /************************************************************************/
703 :
704 34 : const char *PCIDSK2Band::GetMetadataItem(const char *pszName,
705 : const char *pszDomain)
706 :
707 : {
708 : /* -------------------------------------------------------------------- */
709 : /* PCIDSK only supports metadata in the default domain. */
710 : /* -------------------------------------------------------------------- */
711 34 : if (pszDomain != nullptr && strlen(pszDomain) > 0)
712 23 : return GDALPamRasterBand::GetMetadataItem(pszName, pszDomain);
713 :
714 : /* -------------------------------------------------------------------- */
715 : /* Try and fetch (use cached value if available) */
716 : /* -------------------------------------------------------------------- */
717 11 : auto oIter = m_oCacheMetadataItem.find(pszName);
718 11 : if (oIter != m_oCacheMetadataItem.end())
719 : {
720 5 : return oIter->second.empty() ? nullptr : oIter->second.c_str();
721 : }
722 :
723 12 : CPLString osValue;
724 : try
725 : {
726 6 : osValue = poChannel->GetMetadataValue(pszName);
727 : }
728 0 : catch (const PCIDSKException &ex)
729 : {
730 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
731 0 : return nullptr;
732 : }
733 :
734 6 : oIter = m_oCacheMetadataItem
735 6 : .insert(std::pair<std::string, std::string>(pszName, osValue))
736 : .first;
737 6 : return oIter->second.empty() ? nullptr : oIter->second.c_str();
738 : }
739 :
740 : /************************************************************************/
741 : /* GetMetadata() */
742 : /************************************************************************/
743 :
744 6 : char **PCIDSK2Band::GetMetadata(const char *pszDomain)
745 :
746 : {
747 : /* -------------------------------------------------------------------- */
748 : /* PCIDSK only supports metadata in the default domain. */
749 : /* -------------------------------------------------------------------- */
750 6 : if (pszDomain != nullptr && strlen(pszDomain) > 0)
751 1 : return GDALPamRasterBand::GetMetadata(pszDomain);
752 :
753 : /* -------------------------------------------------------------------- */
754 : /* If we have a cached result, just use that. */
755 : /* -------------------------------------------------------------------- */
756 5 : if (papszLastMDListValue != nullptr)
757 0 : return papszLastMDListValue;
758 :
759 : /* -------------------------------------------------------------------- */
760 : /* Fetch and build the list. */
761 : /* -------------------------------------------------------------------- */
762 : try
763 : {
764 10 : std::vector<std::string> aosKeys = poChannel->GetMetadataKeys();
765 :
766 11 : for (unsigned int i = 0; i < aosKeys.size(); i++)
767 : {
768 6 : if (aosKeys[i].c_str()[0] == '_')
769 3 : continue;
770 :
771 6 : papszLastMDListValue = CSLSetNameValue(
772 3 : papszLastMDListValue, aosKeys[i].c_str(),
773 6 : poChannel->GetMetadataValue(aosKeys[i]).c_str());
774 : }
775 : }
776 0 : catch (const PCIDSKException &ex)
777 : {
778 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
779 0 : return nullptr;
780 : }
781 :
782 5 : return papszLastMDListValue;
783 : }
784 :
785 : /************************************************************************/
786 : /* ==================================================================== */
787 : /* PCIDSK2Dataset */
788 : /* ==================================================================== */
789 : /************************************************************************/
790 :
791 : /************************************************************************/
792 : /* PCIDSK2Dataset() */
793 : /************************************************************************/
794 :
795 243 : PCIDSK2Dataset::PCIDSK2Dataset()
796 243 : : papszLastMDListValue(nullptr), poFile(nullptr)
797 : {
798 243 : }
799 :
800 : /************************************************************************/
801 : /* ~PCIDSK2Dataset() */
802 : /************************************************************************/
803 :
804 : // FIXME? is an exception can really be thrown in the destructor, then it is
805 : // very dangerous !
806 : #ifdef _MSC_VER
807 : #pragma warning(push)
808 : #pragma warning(disable : 4702) /* unreachable code */
809 : #endif
810 486 : PCIDSK2Dataset::~PCIDSK2Dataset()
811 : {
812 243 : PCIDSK2Dataset::FlushCache(true);
813 :
814 1426 : while (!apoLayers.empty())
815 : {
816 1183 : delete apoLayers.back();
817 1183 : apoLayers.pop_back();
818 : }
819 :
820 243 : if (m_poSRS)
821 7 : m_poSRS->Release();
822 :
823 : try
824 : {
825 243 : if (poFile != nullptr)
826 243 : delete poFile;
827 : }
828 :
829 : /* -------------------------------------------------------------------- */
830 : /* Trap exceptions. */
831 : /* -------------------------------------------------------------------- */
832 : catch (const PCIDSKException &ex)
833 : {
834 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
835 : }
836 : catch (...)
837 : {
838 : CPLError(CE_Failure, CPLE_AppDefined,
839 : "PCIDSK SDK Failure in Close(), unexpected exception.");
840 : }
841 :
842 243 : CSLDestroy(papszLastMDListValue);
843 486 : }
844 : #ifdef _MSC_VER
845 : #pragma warning(pop)
846 : #endif
847 :
848 : /************************************************************************/
849 : /* GetFileList() */
850 : /************************************************************************/
851 :
852 55 : char **PCIDSK2Dataset::GetFileList()
853 :
854 : {
855 55 : char **papszFileList = GDALPamDataset::GetFileList();
856 110 : CPLString osBaseDir = CPLGetPathSafe(GetDescription());
857 :
858 : try
859 : {
860 75 : for (int nChan = 1; nChan <= poFile->GetChannels(); nChan++)
861 : {
862 20 : PCIDSKChannel *poChannel = poFile->GetChannel(nChan);
863 40 : CPLString osChanFilename;
864 : uint64 image_offset, pixel_offset, line_offset;
865 : bool little_endian;
866 :
867 20 : poChannel->GetChanInfo(osChanFilename, image_offset, pixel_offset,
868 20 : line_offset, little_endian);
869 :
870 20 : if (osChanFilename != "")
871 : {
872 1 : papszFileList = CSLAddString(
873 : papszFileList,
874 2 : CPLProjectRelativeFilenameSafe(osBaseDir, osChanFilename)
875 : .c_str());
876 : }
877 : }
878 :
879 55 : return papszFileList;
880 : }
881 0 : catch (const PCIDSKException &ex)
882 : {
883 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
884 0 : return papszFileList;
885 : }
886 : }
887 :
888 : /************************************************************************/
889 : /* ProcessRPC() */
890 : /************************************************************************/
891 :
892 243 : void PCIDSK2Dataset::ProcessRPC()
893 :
894 : {
895 : /* -------------------------------------------------------------------- */
896 : /* Search all BIN segments looking for an RPC segment. */
897 : /* -------------------------------------------------------------------- */
898 243 : PCIDSKSegment *poSeg = poFile->GetSegment(SEG_BIN, "");
899 243 : PCIDSKRPCSegment *poRPCSeg = nullptr;
900 :
901 243 : while (poSeg != nullptr &&
902 0 : (poRPCSeg = dynamic_cast<PCIDSKRPCSegment *>(poSeg)) == nullptr)
903 :
904 : {
905 0 : poSeg = poFile->GetSegment(SEG_BIN, "", poSeg->GetSegmentNumber());
906 : }
907 :
908 243 : if (poRPCSeg == nullptr)
909 243 : return;
910 :
911 : /* -------------------------------------------------------------------- */
912 : /* Turn RPC segment into GDAL RFC 22 style metadata. */
913 : /* -------------------------------------------------------------------- */
914 : try
915 : {
916 0 : CPLString osValue;
917 : double dfLineOffset, dfLineScale, dfSampOffset, dfSampScale;
918 : double dfLatOffset, dfLatScale, dfLongOffset, dfLongScale,
919 : dfHeightOffset, dfHeightScale;
920 :
921 0 : poRPCSeg->GetRPCTranslationCoeffs(
922 : dfLongOffset, dfLongScale, dfLatOffset, dfLatScale, dfHeightOffset,
923 : dfHeightScale, dfSampOffset, dfSampScale, dfLineOffset,
924 0 : dfLineScale);
925 :
926 0 : osValue.Printf("%.16g", dfLineOffset);
927 0 : GDALPamDataset::SetMetadataItem("LINE_OFF", osValue, "RPC");
928 :
929 0 : osValue.Printf("%.16g", dfLineScale);
930 0 : GDALPamDataset::SetMetadataItem("LINE_SCALE", osValue, "RPC");
931 :
932 0 : osValue.Printf("%.16g", dfSampOffset);
933 0 : GDALPamDataset::SetMetadataItem("SAMP_OFF", osValue, "RPC");
934 :
935 0 : osValue.Printf("%.16g", dfSampScale);
936 0 : GDALPamDataset::SetMetadataItem("SAMP_SCALE", osValue, "RPC");
937 :
938 0 : osValue.Printf("%.16g", dfLongOffset);
939 0 : GDALPamDataset::SetMetadataItem("LONG_OFF", osValue, "RPC");
940 :
941 0 : osValue.Printf("%.16g", dfLongScale);
942 0 : GDALPamDataset::SetMetadataItem("LONG_SCALE", osValue, "RPC");
943 :
944 0 : osValue.Printf("%.16g", dfLatOffset);
945 0 : GDALPamDataset::SetMetadataItem("LAT_OFF", osValue, "RPC");
946 :
947 0 : osValue.Printf("%.16g", dfLatScale);
948 0 : GDALPamDataset::SetMetadataItem("LAT_SCALE", osValue, "RPC");
949 :
950 0 : osValue.Printf("%.16g", dfHeightOffset);
951 0 : GDALPamDataset::SetMetadataItem("HEIGHT_OFF", osValue, "RPC");
952 :
953 0 : osValue.Printf("%.16g", dfHeightScale);
954 0 : GDALPamDataset::SetMetadataItem("HEIGHT_SCALE", osValue, "RPC");
955 :
956 0 : if (poRPCSeg->GetXNumerator().size() != 20 ||
957 0 : poRPCSeg->GetXDenominator().size() != 20 ||
958 0 : poRPCSeg->GetYNumerator().size() != 20 ||
959 0 : poRPCSeg->GetYDenominator().size() != 20)
960 : {
961 0 : GDALPamDataset::SetMetadata(nullptr, "RPC");
962 0 : CPLError(CE_Failure, CPLE_AppDefined,
963 : "Did not get 20 values in the RPC coefficients lists.");
964 0 : return;
965 : }
966 :
967 0 : std::vector<double> adfCoef = poRPCSeg->GetYNumerator();
968 0 : CPLString osCoefList = "";
969 0 : for (int i = 0; i < 20; i++)
970 : {
971 0 : osValue.Printf("%.16g ", adfCoef[i]);
972 0 : osCoefList += osValue;
973 : }
974 0 : GDALPamDataset::SetMetadataItem("LINE_NUM_COEFF", osCoefList, "RPC");
975 :
976 0 : adfCoef = poRPCSeg->GetYDenominator();
977 0 : osCoefList = "";
978 0 : for (int i = 0; i < 20; i++)
979 : {
980 0 : osValue.Printf("%.16g ", adfCoef[i]);
981 0 : osCoefList += osValue;
982 : }
983 0 : GDALPamDataset::SetMetadataItem("LINE_DEN_COEFF", osCoefList, "RPC");
984 :
985 0 : adfCoef = poRPCSeg->GetXNumerator();
986 0 : osCoefList = "";
987 0 : for (int i = 0; i < 20; i++)
988 : {
989 0 : osValue.Printf("%.16g ", adfCoef[i]);
990 0 : osCoefList += osValue;
991 : }
992 0 : GDALPamDataset::SetMetadataItem("SAMP_NUM_COEFF", osCoefList, "RPC");
993 :
994 0 : adfCoef = poRPCSeg->GetXDenominator();
995 0 : osCoefList = "";
996 0 : for (int i = 0; i < 20; i++)
997 : {
998 0 : osValue.Printf("%.16g ", adfCoef[i]);
999 0 : osCoefList += osValue;
1000 : }
1001 0 : GDALPamDataset::SetMetadataItem("SAMP_DEN_COEFF", osCoefList, "RPC");
1002 : }
1003 0 : catch (const PCIDSKException &ex)
1004 : {
1005 0 : GDALPamDataset::SetMetadata(nullptr, "RPC");
1006 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1007 : }
1008 : }
1009 :
1010 : /************************************************************************/
1011 : /* FlushCache() */
1012 : /************************************************************************/
1013 :
1014 255 : CPLErr PCIDSK2Dataset::FlushCache(bool bAtClosing)
1015 :
1016 : {
1017 255 : CPLErr eErr = GDALPamDataset::FlushCache(bAtClosing);
1018 :
1019 255 : if (poFile)
1020 : {
1021 : try
1022 : {
1023 255 : poFile->Synchronize();
1024 : }
1025 0 : catch (const PCIDSKException &ex)
1026 : {
1027 0 : eErr = CE_Failure;
1028 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1029 : }
1030 : }
1031 255 : return eErr;
1032 : }
1033 :
1034 : /************************************************************************/
1035 : /* SetMetadata() */
1036 : /************************************************************************/
1037 :
1038 26 : CPLErr PCIDSK2Dataset::SetMetadata(char **papszMD, const char *pszDomain)
1039 :
1040 : {
1041 : /* -------------------------------------------------------------------- */
1042 : /* PCIDSK only supports metadata in the default domain. */
1043 : /* -------------------------------------------------------------------- */
1044 26 : if (pszDomain != nullptr && strlen(pszDomain) > 0)
1045 0 : return GDALPamDataset::SetMetadata(papszMD, pszDomain);
1046 :
1047 : /* -------------------------------------------------------------------- */
1048 : /* Set each item individually. */
1049 : /* -------------------------------------------------------------------- */
1050 26 : CSLDestroy(papszLastMDListValue);
1051 26 : papszLastMDListValue = nullptr;
1052 26 : m_oCacheMetadataItem.clear();
1053 :
1054 26 : if (GetAccess() == GA_ReadOnly)
1055 : {
1056 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
1057 : "Unable to set metadata on read-only file.");
1058 0 : return CE_Failure;
1059 : }
1060 :
1061 : try
1062 : {
1063 59 : for (int iItem = 0; papszMD && papszMD[iItem]; iItem++)
1064 : {
1065 33 : char *pszItemName = nullptr;
1066 : const char *pszItemValue =
1067 33 : CPLParseNameValue(papszMD[iItem], &pszItemName);
1068 33 : if (pszItemName != nullptr)
1069 : {
1070 17 : poFile->SetMetadataValue(pszItemName, pszItemValue);
1071 17 : CPLFree(pszItemName);
1072 : }
1073 : }
1074 : }
1075 0 : catch (const PCIDSKException &ex)
1076 : {
1077 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1078 0 : return CE_Failure;
1079 : }
1080 :
1081 26 : return CE_None;
1082 : }
1083 :
1084 : /************************************************************************/
1085 : /* SetMetadataItem() */
1086 : /************************************************************************/
1087 :
1088 220 : CPLErr PCIDSK2Dataset::SetMetadataItem(const char *pszName,
1089 : const char *pszValue,
1090 : const char *pszDomain)
1091 :
1092 : {
1093 : /* -------------------------------------------------------------------- */
1094 : /* PCIDSK only supports metadata in the default domain. */
1095 : /* -------------------------------------------------------------------- */
1096 220 : if (pszDomain != nullptr && strlen(pszDomain) > 0)
1097 216 : return GDALPamDataset::SetMetadataItem(pszName, pszValue, pszDomain);
1098 :
1099 : /* -------------------------------------------------------------------- */
1100 : /* Set on the file. */
1101 : /* -------------------------------------------------------------------- */
1102 4 : CSLDestroy(papszLastMDListValue);
1103 4 : papszLastMDListValue = nullptr;
1104 4 : m_oCacheMetadataItem.clear();
1105 :
1106 4 : if (GetAccess() == GA_ReadOnly)
1107 : {
1108 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
1109 : "Unable to set metadata on read-only file.");
1110 0 : return CE_Failure;
1111 : }
1112 :
1113 : try
1114 : {
1115 4 : poFile->SetMetadataValue(pszName, pszValue);
1116 : }
1117 0 : catch (const PCIDSKException &ex)
1118 : {
1119 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1120 0 : return CE_Failure;
1121 : }
1122 :
1123 4 : return CE_None;
1124 : }
1125 :
1126 : /************************************************************************/
1127 : /* GetMetadataDomainList() */
1128 : /************************************************************************/
1129 :
1130 0 : char **PCIDSK2Dataset::GetMetadataDomainList()
1131 : {
1132 0 : return BuildMetadataDomainList(GDALPamDataset::GetMetadataDomainList(),
1133 0 : TRUE, "", nullptr);
1134 : }
1135 :
1136 : /************************************************************************/
1137 : /* GetMetadataItem() */
1138 : /************************************************************************/
1139 :
1140 1238 : const char *PCIDSK2Dataset::GetMetadataItem(const char *pszName,
1141 : const char *pszDomain)
1142 :
1143 : {
1144 : /* -------------------------------------------------------------------- */
1145 : /* PCIDSK only supports metadata in the default domain. */
1146 : /* -------------------------------------------------------------------- */
1147 1238 : if (pszDomain != nullptr && strlen(pszDomain) > 0)
1148 95 : return GDALPamDataset::GetMetadataItem(pszName, pszDomain);
1149 :
1150 : /* -------------------------------------------------------------------- */
1151 : /* Try and fetch (use cached value if available) */
1152 : /* -------------------------------------------------------------------- */
1153 1143 : auto oIter = m_oCacheMetadataItem.find(pszName);
1154 1143 : if (oIter != m_oCacheMetadataItem.end())
1155 : {
1156 1054 : return oIter->second.empty() ? nullptr : oIter->second.c_str();
1157 : }
1158 :
1159 178 : CPLString osValue;
1160 : try
1161 : {
1162 89 : osValue = poFile->GetMetadataValue(pszName);
1163 : }
1164 0 : catch (const PCIDSKException &ex)
1165 : {
1166 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1167 0 : return nullptr;
1168 : }
1169 :
1170 89 : oIter = m_oCacheMetadataItem
1171 89 : .insert(std::pair<std::string, std::string>(pszName, osValue))
1172 : .first;
1173 89 : return oIter->second.empty() ? nullptr : oIter->second.c_str();
1174 : }
1175 :
1176 : /************************************************************************/
1177 : /* GetMetadata() */
1178 : /************************************************************************/
1179 :
1180 52 : char **PCIDSK2Dataset::GetMetadata(const char *pszDomain)
1181 :
1182 : {
1183 : /* -------------------------------------------------------------------- */
1184 : /* PCIDSK only supports metadata in the default domain. */
1185 : /* -------------------------------------------------------------------- */
1186 52 : if (pszDomain != nullptr && strlen(pszDomain) > 0)
1187 25 : return GDALPamDataset::GetMetadata(pszDomain);
1188 :
1189 : /* -------------------------------------------------------------------- */
1190 : /* If we have a cached result, just use that. */
1191 : /* -------------------------------------------------------------------- */
1192 27 : if (papszLastMDListValue != nullptr)
1193 3 : return papszLastMDListValue;
1194 :
1195 : /* -------------------------------------------------------------------- */
1196 : /* Fetch and build the list. */
1197 : /* -------------------------------------------------------------------- */
1198 : try
1199 : {
1200 48 : std::vector<std::string> aosKeys = poFile->GetMetadataKeys();
1201 :
1202 38 : for (unsigned int i = 0; i < aosKeys.size(); i++)
1203 : {
1204 14 : if (aosKeys[i].c_str()[0] == '_')
1205 2 : continue;
1206 :
1207 12 : papszLastMDListValue =
1208 12 : CSLSetNameValue(papszLastMDListValue, aosKeys[i].c_str(),
1209 24 : poFile->GetMetadataValue(aosKeys[i]).c_str());
1210 : }
1211 : }
1212 0 : catch (const PCIDSKException &ex)
1213 : {
1214 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1215 0 : return nullptr;
1216 : }
1217 :
1218 24 : return papszLastMDListValue;
1219 : }
1220 :
1221 : /************************************************************************/
1222 : /* SetGeoTransform() */
1223 : /************************************************************************/
1224 :
1225 58 : CPLErr PCIDSK2Dataset::SetGeoTransform(const GDALGeoTransform >)
1226 : {
1227 58 : PCIDSKGeoref *poGeoref = nullptr;
1228 : try
1229 : {
1230 58 : PCIDSKSegment *poGeoSeg = poFile->GetSegment(1);
1231 58 : poGeoref = dynamic_cast<PCIDSKGeoref *>(poGeoSeg);
1232 : }
1233 0 : catch (const PCIDSKException &)
1234 : {
1235 : // I should really check whether this is an expected issue.
1236 : }
1237 :
1238 58 : if (poGeoref == nullptr)
1239 0 : return GDALPamDataset::SetGeoTransform(gt);
1240 :
1241 58 : if (GetAccess() == GA_ReadOnly)
1242 : {
1243 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
1244 : "Unable to set GeoTransform on read-only file.");
1245 0 : return CE_Failure;
1246 : }
1247 :
1248 : try
1249 : {
1250 58 : poGeoref->WriteSimple(poGeoref->GetGeosys(), gt[0], gt[1], gt[2], gt[3],
1251 58 : gt[4], gt[5]);
1252 : }
1253 0 : catch (const PCIDSKException &ex)
1254 : {
1255 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1256 0 : return CE_Failure;
1257 : }
1258 :
1259 58 : return CE_None;
1260 : }
1261 :
1262 : /************************************************************************/
1263 : /* GetGeoTransform() */
1264 : /************************************************************************/
1265 :
1266 44 : CPLErr PCIDSK2Dataset::GetGeoTransform(GDALGeoTransform >) const
1267 : {
1268 44 : PCIDSKGeoref *poGeoref = nullptr;
1269 : try
1270 : {
1271 44 : PCIDSKSegment *poGeoSeg = poFile->GetSegment(1);
1272 44 : poGeoref = dynamic_cast<PCIDSKGeoref *>(poGeoSeg);
1273 : }
1274 0 : catch (const PCIDSKException &)
1275 : {
1276 : // I should really check whether this is an expected issue.
1277 : }
1278 :
1279 44 : if (poGeoref != nullptr)
1280 : {
1281 : try
1282 : {
1283 44 : poGeoref->GetTransform(gt[0], gt[1], gt[2], gt[3], gt[4], gt[5]);
1284 : }
1285 0 : catch (const PCIDSKException &ex)
1286 : {
1287 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1288 0 : return CE_Failure;
1289 : }
1290 :
1291 : // If we got anything non-default return it.
1292 44 : if (gt != GDALGeoTransform())
1293 41 : return CE_None;
1294 : }
1295 :
1296 : /* -------------------------------------------------------------------- */
1297 : /* Check for worldfile if we have no other georeferencing. */
1298 : /* -------------------------------------------------------------------- */
1299 3 : if (GDALReadWorldFile(GetDescription(), "pxw", gt.data()))
1300 0 : return CE_None;
1301 :
1302 3 : return GDALPamDataset::GetGeoTransform(gt);
1303 : }
1304 :
1305 : /************************************************************************/
1306 : /* SetSpatialRef() */
1307 : /************************************************************************/
1308 :
1309 58 : CPLErr PCIDSK2Dataset::SetSpatialRef(const OGRSpatialReference *poSRS)
1310 :
1311 : {
1312 58 : PCIDSKGeoref *poGeoref = nullptr;
1313 :
1314 : try
1315 : {
1316 58 : PCIDSKSegment *poGeoSeg = poFile->GetSegment(1);
1317 58 : poGeoref = dynamic_cast<PCIDSKGeoref *>(poGeoSeg);
1318 : }
1319 0 : catch (const PCIDSKException &)
1320 : {
1321 : // I should really check whether this is an expected issue.
1322 : }
1323 :
1324 58 : if (poGeoref == nullptr)
1325 : {
1326 0 : return GDALPamDataset::SetSpatialRef(poSRS);
1327 : }
1328 :
1329 58 : char *pszGeosys = nullptr;
1330 58 : char *pszUnits = nullptr;
1331 58 : double *padfPrjParams = nullptr;
1332 :
1333 58 : if (poSRS == nullptr || poSRS->exportToPCI(&pszGeosys, &pszUnits,
1334 : &padfPrjParams) != OGRERR_NONE)
1335 : {
1336 0 : return GDALPamDataset::SetSpatialRef(poSRS);
1337 : }
1338 :
1339 58 : if (GetAccess() == GA_ReadOnly)
1340 : {
1341 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
1342 : "Unable to set projection on read-only file.");
1343 0 : CPLFree(pszGeosys);
1344 0 : CPLFree(pszUnits);
1345 0 : CPLFree(padfPrjParams);
1346 0 : return CE_Failure;
1347 : }
1348 :
1349 : try
1350 : {
1351 : double adfGT[6];
1352 58 : poGeoref->GetTransform(adfGT[0], adfGT[1], adfGT[2], adfGT[3], adfGT[4],
1353 58 : adfGT[5]);
1354 :
1355 58 : poGeoref->WriteSimple(pszGeosys, adfGT[0], adfGT[1], adfGT[2], adfGT[3],
1356 58 : adfGT[4], adfGT[5]);
1357 :
1358 116 : std::vector<double> adfPCIParameters;
1359 1044 : for (unsigned int i = 0; i < 17; i++)
1360 986 : adfPCIParameters.push_back(padfPrjParams[i]);
1361 :
1362 58 : if (STARTS_WITH_CI(pszUnits, "FOOT"))
1363 0 : adfPCIParameters.push_back(
1364 0 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_US_FOOT)));
1365 58 : else if (EQUALN(pszUnits, "INTL FOOT", 9))
1366 0 : adfPCIParameters.push_back(
1367 0 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_INTL_FOOT)));
1368 58 : else if (EQUALN(pszUnits, "DEGREE", 6))
1369 50 : adfPCIParameters.push_back(
1370 50 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_DEGREE)));
1371 : else
1372 8 : adfPCIParameters.push_back(
1373 8 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_METER)));
1374 :
1375 58 : poGeoref->WriteParameters(adfPCIParameters);
1376 : }
1377 0 : catch (const PCIDSKException &ex)
1378 : {
1379 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1380 0 : return CE_Failure;
1381 : }
1382 :
1383 58 : CPLFree(pszGeosys);
1384 58 : CPLFree(pszUnits);
1385 58 : CPLFree(padfPrjParams);
1386 :
1387 58 : return CE_None;
1388 : }
1389 :
1390 : /************************************************************************/
1391 : /* GetSpatialRef() */
1392 : /************************************************************************/
1393 :
1394 10 : const OGRSpatialReference *PCIDSK2Dataset::GetSpatialRef() const
1395 : {
1396 10 : return GetSpatialRef(false);
1397 : }
1398 :
1399 6 : const OGRSpatialReference *PCIDSK2Dataset::GetSpatialRefRasterOnly() const
1400 : {
1401 6 : return GetSpatialRef(true);
1402 : }
1403 :
1404 : static thread_local int tlsEnableLayersInGetSpatialRefCounter = 0;
1405 :
1406 16 : const OGRSpatialReference *PCIDSK2Dataset::GetSpatialRef(bool bRasterOnly) const
1407 : {
1408 16 : if (tlsEnableLayersInGetSpatialRefCounter)
1409 3 : return nullptr;
1410 :
1411 13 : if (m_poSRS)
1412 3 : return m_poSRS;
1413 :
1414 10 : PCIDSKGeoref *poGeoref = nullptr;
1415 :
1416 : try
1417 : {
1418 10 : PCIDSKSegment *poGeoSeg = poFile->GetSegment(1);
1419 10 : poGeoref = dynamic_cast<PCIDSKGeoref *>(poGeoSeg);
1420 : }
1421 0 : catch (const PCIDSKException &)
1422 : {
1423 : // I should really check whether this is an expected issue.
1424 : }
1425 :
1426 10 : if (poGeoref == nullptr)
1427 : {
1428 0 : ++tlsEnableLayersInGetSpatialRefCounter;
1429 : const OGRSpatialReference *poRet =
1430 0 : (bRasterOnly) ? GDALPamDataset::GetSpatialRefRasterOnly()
1431 0 : : GDALPamDataset::GetSpatialRef();
1432 0 : --tlsEnableLayersInGetSpatialRefCounter;
1433 0 : return poRet;
1434 : }
1435 :
1436 20 : CPLString osGeosys;
1437 10 : const char *pszUnits = nullptr;
1438 :
1439 20 : std::vector<double> adfParameters;
1440 10 : adfParameters.resize(18);
1441 :
1442 : try
1443 : {
1444 10 : osGeosys = poGeoref->GetGeosys();
1445 10 : adfParameters = poGeoref->GetParameters();
1446 : const UnitCode code =
1447 10 : static_cast<UnitCode>(static_cast<int>(adfParameters[16]));
1448 :
1449 10 : if (code == PCIDSK::UNIT_DEGREE)
1450 0 : pszUnits = "DEGREE";
1451 10 : else if (code == PCIDSK::UNIT_METER)
1452 0 : pszUnits = "METER";
1453 10 : else if (code == PCIDSK::UNIT_US_FOOT)
1454 0 : pszUnits = "FOOT";
1455 10 : else if (code == PCIDSK::UNIT_INTL_FOOT)
1456 0 : pszUnits = "INTL FOOT";
1457 : }
1458 0 : catch (const PCIDSKException &ex)
1459 : {
1460 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1461 : }
1462 :
1463 20 : OGRSpatialReference oSRS;
1464 10 : oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
1465 10 : if (oSRS.importFromPCI(osGeosys, pszUnits, &(adfParameters[0])) ==
1466 : OGRERR_NONE)
1467 : {
1468 7 : m_poSRS = oSRS.Clone();
1469 7 : return m_poSRS;
1470 : }
1471 : else
1472 : {
1473 3 : ++tlsEnableLayersInGetSpatialRefCounter;
1474 : const OGRSpatialReference *poRet =
1475 3 : (bRasterOnly) ? GDALPamDataset::GetSpatialRefRasterOnly()
1476 0 : : GDALPamDataset::GetSpatialRef();
1477 3 : --tlsEnableLayersInGetSpatialRefCounter;
1478 3 : return poRet;
1479 : }
1480 : }
1481 :
1482 : /************************************************************************/
1483 : /* IBuildOverviews() */
1484 : /************************************************************************/
1485 :
1486 3 : CPLErr PCIDSK2Dataset::IBuildOverviews(
1487 : const char *pszResampling, int nOverviews, const int *panOverviewList,
1488 : int nListBands, const int *panBandList, GDALProgressFunc pfnProgress,
1489 : void *pProgressData, CSLConstList papszOptions)
1490 :
1491 : {
1492 : PCIDSK2Band *poBand =
1493 3 : reinterpret_cast<PCIDSK2Band *>(GetRasterBand(panBandList[0]));
1494 :
1495 : /* -------------------------------------------------------------------- */
1496 : /* If RRD overviews requested, then invoke generic handling. */
1497 : /* -------------------------------------------------------------------- */
1498 3 : bool bUseGenericHandling = false;
1499 :
1500 3 : if (CPLTestBool(CPLGetConfigOption("USE_RRD", "NO")))
1501 : {
1502 1 : bUseGenericHandling = true;
1503 : }
1504 :
1505 : /* -------------------------------------------------------------------- */
1506 : /* If we don't have read access, then create the overviews */
1507 : /* externally. */
1508 : /* -------------------------------------------------------------------- */
1509 3 : if (GetAccess() != GA_Update)
1510 : {
1511 1 : CPLDebug("PCIDSK", "File open for read-only accessing, "
1512 : "creating overviews externally.");
1513 :
1514 1 : bUseGenericHandling = true;
1515 : }
1516 :
1517 3 : if (bUseGenericHandling)
1518 : {
1519 2 : if (poBand->GetOverviewCount() != 0)
1520 : {
1521 0 : CPLError(CE_Failure, CPLE_NotSupported,
1522 : "Cannot add external overviews when there are already "
1523 : "internal overviews");
1524 0 : return CE_Failure;
1525 : }
1526 :
1527 2 : return GDALDataset::IBuildOverviews(
1528 : pszResampling, nOverviews, panOverviewList, nListBands, panBandList,
1529 2 : pfnProgress, pProgressData, papszOptions);
1530 : }
1531 :
1532 1 : if (nListBands == 0)
1533 0 : return CE_None;
1534 :
1535 : /* -------------------------------------------------------------------- */
1536 : /* Currently no support for clearing overviews. */
1537 : /* -------------------------------------------------------------------- */
1538 1 : if (nOverviews == 0)
1539 : {
1540 0 : CPLError(CE_Failure, CPLE_AppDefined,
1541 : "PCIDSK2 driver does not currently support clearing existing "
1542 : "overviews. ");
1543 0 : return CE_Failure;
1544 : }
1545 :
1546 : /* -------------------------------------------------------------------- */
1547 : /* Establish which of the overview levels we already have, and */
1548 : /* which are new. We assume that band 1 of the file is */
1549 : /* representative. */
1550 : /* -------------------------------------------------------------------- */
1551 :
1552 1 : int nNewOverviews = 0;
1553 : int *panNewOverviewList =
1554 1 : static_cast<int *>(CPLCalloc(sizeof(int), nOverviews));
1555 2 : std::vector<bool> abFoundOverviewFactor(nOverviews);
1556 2 : for (int i = 0; i < nOverviews && poBand != nullptr; i++)
1557 : {
1558 1 : for (int j = 0; j < poBand->GetOverviewCount(); j++)
1559 : {
1560 0 : GDALRasterBand *poOverview = poBand->GetOverview(j);
1561 :
1562 : int nOvFactor =
1563 0 : GDALComputeOvFactor(poOverview->GetXSize(), poBand->GetXSize(),
1564 : poOverview->GetYSize(), poBand->GetYSize());
1565 :
1566 0 : if (nOvFactor == panOverviewList[i] ||
1567 0 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
1568 : poBand->GetXSize(),
1569 : poBand->GetYSize()))
1570 0 : abFoundOverviewFactor[i] = true;
1571 : }
1572 :
1573 1 : if (!abFoundOverviewFactor[i])
1574 1 : panNewOverviewList[nNewOverviews++] = panOverviewList[i];
1575 : }
1576 :
1577 : /* -------------------------------------------------------------------- */
1578 : /* Create the overviews that are missing. */
1579 : /* -------------------------------------------------------------------- */
1580 2 : for (int i = 0; i < nNewOverviews; i++)
1581 : {
1582 : try
1583 : {
1584 : // conveniently our resampling values mostly match PCIDSK.
1585 2 : poFile->CreateOverviews(nListBands, panBandList,
1586 2 : panNewOverviewList[i], pszResampling);
1587 : }
1588 0 : catch (const PCIDSKException &ex)
1589 : {
1590 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1591 0 : CPLFree(panNewOverviewList);
1592 0 : return CE_Failure;
1593 : }
1594 : }
1595 :
1596 1 : CPLFree(panNewOverviewList);
1597 1 : panNewOverviewList = nullptr;
1598 :
1599 2 : for (int iBand = 0; iBand < nListBands; iBand++)
1600 : {
1601 : poBand =
1602 1 : reinterpret_cast<PCIDSK2Band *>(GetRasterBand(panBandList[iBand]));
1603 1 : reinterpret_cast<PCIDSK2Band *>(poBand)->RefreshOverviewList();
1604 : }
1605 :
1606 : /* -------------------------------------------------------------------- */
1607 : /* Actually generate the overview imagery. */
1608 : /* -------------------------------------------------------------------- */
1609 1 : CPLErr eErr = CE_None;
1610 1 : std::vector<int> anRegenLevels;
1611 :
1612 : GDALRasterBand **papoOverviewBands = reinterpret_cast<GDALRasterBand **>(
1613 1 : CPLCalloc(sizeof(void *), nOverviews));
1614 :
1615 2 : for (int iBand = 0; iBand < nListBands && eErr == CE_None; iBand++)
1616 : {
1617 1 : nNewOverviews = 0;
1618 :
1619 : poBand =
1620 1 : reinterpret_cast<PCIDSK2Band *>(GetRasterBand(panBandList[iBand]));
1621 :
1622 2 : for (int i = 0; i < nOverviews && poBand != nullptr; i++)
1623 : {
1624 1 : for (int j = 0; j < poBand->GetOverviewCount(); j++)
1625 : {
1626 1 : GDALRasterBand *poOverview = poBand->GetOverview(j);
1627 :
1628 1 : int nOvFactor = GDALComputeOvFactor(
1629 : poOverview->GetXSize(), poBand->GetXSize(),
1630 : poOverview->GetYSize(), poBand->GetYSize());
1631 :
1632 1 : if (nOvFactor == panOverviewList[i] ||
1633 0 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
1634 : poBand->GetXSize(),
1635 : poBand->GetYSize()))
1636 : {
1637 1 : papoOverviewBands[nNewOverviews++] = poOverview;
1638 1 : anRegenLevels.push_back(j);
1639 1 : break;
1640 : }
1641 : }
1642 : }
1643 :
1644 1 : if (nNewOverviews > 0)
1645 : {
1646 1 : eErr = GDALRegenerateOverviewsEx(
1647 : (GDALRasterBandH)poBand, nNewOverviews,
1648 : reinterpret_cast<GDALRasterBandH *>(papoOverviewBands),
1649 : pszResampling, pfnProgress, pProgressData, papszOptions);
1650 :
1651 : // Mark the regenerated overviews as valid.
1652 2 : for (int i = 0; i < static_cast<int>(anRegenLevels.size()); i++)
1653 1 : poBand->poChannel->SetOverviewValidity(anRegenLevels[i], true);
1654 : }
1655 : }
1656 :
1657 1 : CPLFree(papoOverviewBands);
1658 :
1659 1 : return eErr;
1660 : }
1661 :
1662 : /************************************************************************/
1663 : /* PCIDSKTypeToGDAL() */
1664 : /************************************************************************/
1665 :
1666 477 : GDALDataType PCIDSK2Dataset::PCIDSKTypeToGDAL(eChanType eType)
1667 : {
1668 477 : switch (eType)
1669 : {
1670 339 : case CHN_8U:
1671 339 : return GDT_Byte;
1672 :
1673 66 : case CHN_16U:
1674 66 : return GDT_UInt16;
1675 :
1676 18 : case CHN_16S:
1677 18 : return GDT_Int16;
1678 :
1679 18 : case CHN_32R:
1680 18 : return GDT_Float32;
1681 :
1682 0 : case CHN_BIT:
1683 0 : return GDT_Byte;
1684 :
1685 0 : case CHN_C16U:
1686 0 : return GDT_CInt16;
1687 :
1688 18 : case CHN_C16S:
1689 18 : return GDT_CInt16;
1690 :
1691 18 : case CHN_C32R:
1692 18 : return GDT_CFloat32;
1693 :
1694 0 : default:
1695 0 : return GDT_Unknown;
1696 : }
1697 : }
1698 :
1699 : /************************************************************************/
1700 : /* Open() */
1701 : /************************************************************************/
1702 :
1703 136 : GDALDataset *PCIDSK2Dataset::Open(GDALOpenInfo *poOpenInfo)
1704 : {
1705 136 : if (!PCIDSKDriverIdentify(poOpenInfo))
1706 0 : return nullptr;
1707 :
1708 : /* -------------------------------------------------------------------- */
1709 : /* Try opening the file. */
1710 : /* -------------------------------------------------------------------- */
1711 136 : PCIDSKFile *poFile = nullptr;
1712 : const int nMaxBandCount =
1713 136 : atoi(CPLGetConfigOption("GDAL_MAX_BAND_COUNT", "65536"));
1714 : try
1715 : {
1716 276 : poFile = PCIDSK::Open(poOpenInfo->pszFilename,
1717 136 : poOpenInfo->eAccess == GA_ReadOnly ? "r" : "r+",
1718 : PCIDSK2GetInterfaces(), nMaxBandCount);
1719 135 : if (poFile == nullptr)
1720 : {
1721 0 : CPLError(CE_Failure, CPLE_OpenFailed,
1722 : "Failed to re-open %s within PCIDSK driver.\n",
1723 : poOpenInfo->pszFilename);
1724 0 : return nullptr;
1725 : }
1726 :
1727 : const bool bValidRasterDimensions =
1728 135 : poFile->GetWidth() && poFile->GetHeight();
1729 135 : if (!bValidRasterDimensions &&
1730 0 : (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0 &&
1731 0 : (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) == 0)
1732 : {
1733 0 : delete poFile;
1734 0 : return nullptr;
1735 : }
1736 :
1737 : /* Check if this is a vector-only PCIDSK file and that we are */
1738 : /* opened in raster-only mode */
1739 398 : if (poOpenInfo->eAccess == GA_ReadOnly &&
1740 128 : (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0 &&
1741 118 : (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) == 0 &&
1742 266 : poFile->GetChannels() == 0 &&
1743 138 : poFile->GetSegment(PCIDSK::SEG_VEC, "") != nullptr)
1744 : {
1745 2 : CPLDebug("PCIDSK",
1746 : "This is a vector-only PCIDSK dataset, "
1747 : "but it has been opened in read-only in raster-only mode");
1748 2 : delete poFile;
1749 2 : return nullptr;
1750 : }
1751 : /* Reverse test */
1752 392 : if (poOpenInfo->eAccess == GA_ReadOnly &&
1753 126 : (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) == 0 &&
1754 10 : (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0 &&
1755 259 : poFile->GetChannels() != 0 &&
1756 133 : poFile->GetSegment(PCIDSK::SEG_VEC, "") == nullptr)
1757 : {
1758 0 : CPLDebug("PCIDSK",
1759 : "This is a raster-only PCIDSK dataset, "
1760 : "but it has been opened in read-only in vector-only mode");
1761 0 : delete poFile;
1762 0 : return nullptr;
1763 : }
1764 :
1765 133 : return LLOpen(poOpenInfo->pszFilename, poFile, poOpenInfo->eAccess,
1766 133 : poOpenInfo->GetSiblingFiles());
1767 : }
1768 : /* -------------------------------------------------------------------- */
1769 : /* Trap exceptions. */
1770 : /* -------------------------------------------------------------------- */
1771 1 : catch (const PCIDSKException &ex)
1772 : {
1773 1 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1774 1 : delete poFile;
1775 1 : return nullptr;
1776 : }
1777 0 : catch (...)
1778 : {
1779 0 : CPLError(CE_Failure, CPLE_AppDefined,
1780 : "PCIDSK::Create() failed, unexpected exception.");
1781 0 : delete poFile;
1782 0 : return nullptr;
1783 : }
1784 : }
1785 :
1786 : /************************************************************************/
1787 : /* LLOpen() */
1788 : /* */
1789 : /* Low level variant of open that takes the preexisting */
1790 : /* PCIDSKFile. */
1791 : /************************************************************************/
1792 :
1793 243 : GDALDataset *PCIDSK2Dataset::LLOpen(const char *pszFilename,
1794 : PCIDSK::PCIDSKFile *poFile,
1795 : GDALAccess eAccessIn,
1796 : char **papszSiblingFiles)
1797 :
1798 : {
1799 243 : PCIDSK2Dataset *poDS = new PCIDSK2Dataset();
1800 : /* -------------------------------------------------------------------- */
1801 : /* Create a corresponding GDALDataset. */
1802 : /* -------------------------------------------------------------------- */
1803 243 : poDS->poFile = poFile;
1804 243 : poDS->eAccess = eAccessIn;
1805 243 : poDS->nRasterXSize = poFile->GetWidth();
1806 243 : poDS->nRasterYSize = poFile->GetHeight();
1807 :
1808 : const bool bValidRasterDimensions =
1809 243 : poFile->GetWidth() && poFile->GetHeight();
1810 243 : if (!bValidRasterDimensions)
1811 : {
1812 0 : poDS->nRasterXSize = 512;
1813 0 : poDS->nRasterYSize = 512;
1814 : }
1815 :
1816 : try
1817 : {
1818 :
1819 : /* --------------------------------------------------------------------
1820 : */
1821 : /* Are we specifically PIXEL or BAND interleaving? */
1822 : /* */
1823 : /* We don't set anything for FILE since it is harder to know if */
1824 : /* this is tiled or what the on disk interleaving is. */
1825 : /* --------------------------------------------------------------------
1826 : */
1827 243 : if (EQUAL(poFile->GetInterleaving().c_str(), "PIXEL"))
1828 0 : poDS->SetMetadataItem("IMAGE_STRUCTURE", "PIXEL",
1829 : "IMAGE_STRUCTURE");
1830 243 : else if (EQUAL(poFile->GetInterleaving().c_str(), "BAND"))
1831 215 : poDS->SetMetadataItem("IMAGE_STRUCTURE", "BAND", "IMAGE_STRUCTURE");
1832 :
1833 : /* --------------------------------------------------------------------
1834 : */
1835 : /* Create band objects. */
1836 : /* --------------------------------------------------------------------
1837 : */
1838 476 : for (int iBand = 0;
1839 476 : bValidRasterDimensions && iBand < poFile->GetChannels(); iBand++)
1840 : {
1841 233 : PCIDSKChannel *poChannel = poFile->GetChannel(iBand + 1);
1842 466 : if (poChannel->GetBlockWidth() <= 0 ||
1843 233 : poChannel->GetBlockHeight() <= 0)
1844 : {
1845 0 : delete poDS;
1846 0 : return nullptr;
1847 : }
1848 :
1849 233 : if (PCIDSK2Dataset::PCIDSKTypeToGDAL(poChannel->GetType()) ==
1850 : GDT_Unknown)
1851 : {
1852 0 : continue;
1853 : }
1854 :
1855 233 : poDS->SetBand(poDS->GetRasterCount() + 1,
1856 233 : new PCIDSK2Band(poFile, poChannel));
1857 : }
1858 :
1859 : /* --------------------------------------------------------------------
1860 : */
1861 : /* Create band objects for bitmap segments. */
1862 : /* --------------------------------------------------------------------
1863 : */
1864 243 : int nLastBitmapSegment = 0;
1865 243 : PCIDSKSegment *poBitSeg = nullptr;
1866 :
1867 486 : while (bValidRasterDimensions &&
1868 486 : (poBitSeg = poFile->GetSegment(SEG_BIT, "",
1869 243 : nLastBitmapSegment)) != nullptr)
1870 : {
1871 0 : PCIDSKChannel *poChannel = dynamic_cast<PCIDSKChannel *>(poBitSeg);
1872 0 : if (poChannel == nullptr || poChannel->GetBlockWidth() <= 0 ||
1873 0 : poChannel->GetBlockHeight() <= 0)
1874 : {
1875 0 : delete poDS;
1876 0 : return nullptr;
1877 : }
1878 :
1879 0 : if (PCIDSK2Dataset::PCIDSKTypeToGDAL(poChannel->GetType()) ==
1880 : GDT_Unknown)
1881 : {
1882 0 : continue;
1883 : }
1884 :
1885 0 : poDS->SetBand(poDS->GetRasterCount() + 1,
1886 0 : new PCIDSK2Band(poChannel));
1887 :
1888 0 : nLastBitmapSegment = poBitSeg->GetSegmentNumber();
1889 : }
1890 :
1891 : /* --------------------------------------------------------------------
1892 : */
1893 : /* Create vector layers from vector segments. */
1894 : /* --------------------------------------------------------------------
1895 : */
1896 243 : PCIDSK::PCIDSKSegment *segobj = poFile->GetSegment(PCIDSK::SEG_VEC, "");
1897 338 : for (; segobj != nullptr;
1898 95 : segobj = poFile->GetSegment(PCIDSK::SEG_VEC, "",
1899 95 : segobj->GetSegmentNumber()))
1900 : {
1901 : PCIDSK::PCIDSKVectorSegment *poVecSeg =
1902 95 : dynamic_cast<PCIDSK::PCIDSKVectorSegment *>(segobj);
1903 95 : if (poVecSeg)
1904 95 : poDS->apoLayers.push_back(new OGRPCIDSKLayer(
1905 95 : poDS, segobj, poVecSeg, eAccessIn == GA_Update));
1906 : }
1907 :
1908 : /* --------------------------------------------------------------------
1909 : */
1910 : /* Process RPC segment, if there is one. */
1911 : /* --------------------------------------------------------------------
1912 : */
1913 243 : poDS->ProcessRPC();
1914 :
1915 : /* --------------------------------------------------------------------
1916 : */
1917 : /* Initialize any PAM information. */
1918 : /* --------------------------------------------------------------------
1919 : */
1920 243 : poDS->SetDescription(pszFilename);
1921 243 : poDS->TryLoadXML(papszSiblingFiles);
1922 :
1923 : /* --------------------------------------------------------------------
1924 : */
1925 : /* Open overviews. */
1926 : /* --------------------------------------------------------------------
1927 : */
1928 243 : poDS->oOvManager.Initialize(poDS, pszFilename, papszSiblingFiles);
1929 :
1930 243 : return poDS;
1931 : }
1932 :
1933 : /* -------------------------------------------------------------------- */
1934 : /* Trap exceptions. */
1935 : /* -------------------------------------------------------------------- */
1936 0 : catch (const PCIDSKException &ex)
1937 : {
1938 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1939 : }
1940 0 : catch (...)
1941 : {
1942 0 : CPLError(CE_Failure, CPLE_AppDefined,
1943 : "PCIDSK SDK Failure in Open(), unexpected exception.");
1944 : }
1945 :
1946 : /* -------------------------------------------------------------------- */
1947 : /* In case of exception, close dataset */
1948 : /* -------------------------------------------------------------------- */
1949 0 : delete poDS;
1950 :
1951 0 : return nullptr;
1952 : }
1953 :
1954 : /************************************************************************/
1955 : /* Create() */
1956 : /************************************************************************/
1957 :
1958 124 : GDALDataset *PCIDSK2Dataset::Create(const char *pszFilename, int nXSize,
1959 : int nYSize, int nBandsIn,
1960 : GDALDataType eType, char **papszParamList)
1961 :
1962 : {
1963 : /* -------------------------------------------------------------------- */
1964 : /* Prepare channel type list. */
1965 : /* -------------------------------------------------------------------- */
1966 248 : std::vector<eChanType> aeChanTypes;
1967 :
1968 124 : if (eType == GDT_Float32)
1969 3 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_32R);
1970 121 : else if (eType == GDT_Int16)
1971 3 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_16S);
1972 118 : else if (eType == GDT_UInt16)
1973 11 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_16U);
1974 107 : else if (eType == GDT_CInt16)
1975 3 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_C16S);
1976 104 : else if (eType == GDT_CFloat32)
1977 3 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_C32R);
1978 : else
1979 101 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_8U);
1980 :
1981 : /* -------------------------------------------------------------------- */
1982 : /* Reformat options. Currently no support for jpeg compression */
1983 : /* quality. */
1984 : /* -------------------------------------------------------------------- */
1985 248 : CPLString osOptions;
1986 124 : const char *pszValue = CSLFetchNameValue(papszParamList, "INTERLEAVING");
1987 124 : if (pszValue == nullptr)
1988 116 : pszValue = "BAND";
1989 :
1990 124 : osOptions = pszValue;
1991 :
1992 124 : if (osOptions == "TILED")
1993 : {
1994 7 : pszValue = CSLFetchNameValue(papszParamList, "TILESIZE");
1995 7 : if (pszValue != nullptr)
1996 6 : osOptions += pszValue;
1997 :
1998 7 : pszValue = CSLFetchNameValue(papszParamList, "COMPRESSION");
1999 7 : if (pszValue != nullptr)
2000 : {
2001 4 : osOptions += " ";
2002 4 : osOptions += pszValue;
2003 : }
2004 :
2005 7 : pszValue = CSLFetchNameValue(papszParamList, "TILEVERSION");
2006 7 : if (pszValue != nullptr)
2007 : {
2008 4 : osOptions += " TILEV";
2009 4 : osOptions += pszValue;
2010 : }
2011 : }
2012 :
2013 : /* -------------------------------------------------------------------- */
2014 : /* Try creation. */
2015 : /* -------------------------------------------------------------------- */
2016 :
2017 : try
2018 : {
2019 124 : if (nBandsIn == 0)
2020 : {
2021 43 : nXSize = 512;
2022 43 : nYSize = 512;
2023 : }
2024 276 : PCIDSKFile *poFile = PCIDSK::Create(pszFilename, nXSize, nYSize,
2025 124 : nBandsIn, &(aeChanTypes[0]),
2026 : osOptions, PCIDSK2GetInterfaces());
2027 :
2028 : /* --------------------------------------------------------------------
2029 : */
2030 : /* Apply band descriptions, if provided as creation options. */
2031 : /* --------------------------------------------------------------------
2032 : */
2033 132 : for (size_t i = 0;
2034 132 : papszParamList != nullptr && papszParamList[i] != nullptr; i++)
2035 : {
2036 22 : if (STARTS_WITH_CI(papszParamList[i], "BANDDESC"))
2037 : {
2038 0 : int nBand = atoi(papszParamList[i] + 8);
2039 0 : const char *pszDescription = strstr(papszParamList[i], "=");
2040 0 : if (pszDescription && nBand > 0 && nBand <= nBandsIn)
2041 : {
2042 0 : poFile->GetChannel(nBand)->SetDescription(pszDescription +
2043 0 : 1);
2044 : }
2045 : }
2046 : }
2047 :
2048 110 : return LLOpen(pszFilename, poFile, GA_Update);
2049 : }
2050 : /* -------------------------------------------------------------------- */
2051 : /* Trap exceptions. */
2052 : /* -------------------------------------------------------------------- */
2053 28 : catch (const PCIDSKException &ex)
2054 : {
2055 14 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
2056 : }
2057 0 : catch (...)
2058 : {
2059 0 : CPLError(CE_Failure, CPLE_AppDefined,
2060 : "PCIDSK::Create() failed, unexpected exception.");
2061 : }
2062 :
2063 14 : return nullptr;
2064 : }
2065 :
2066 : /************************************************************************/
2067 : /* TestCapability() */
2068 : /************************************************************************/
2069 :
2070 79 : int PCIDSK2Dataset::TestCapability(const char *pszCap) const
2071 :
2072 : {
2073 79 : if (EQUAL(pszCap, ODsCCreateLayer))
2074 37 : return eAccess == GA_Update;
2075 42 : if (EQUAL(pszCap, ODsCRandomLayerWrite))
2076 0 : return eAccess == GA_Update;
2077 42 : if (EQUAL(pszCap, ODsCZGeometries))
2078 12 : return TRUE;
2079 :
2080 30 : return FALSE;
2081 : }
2082 :
2083 : /************************************************************************/
2084 : /* GetLayer() */
2085 : /************************************************************************/
2086 :
2087 1655 : const OGRLayer *PCIDSK2Dataset::GetLayer(int iLayer) const
2088 :
2089 : {
2090 1655 : if (iLayer < 0 || iLayer >= static_cast<int>(apoLayers.size()))
2091 2 : return nullptr;
2092 :
2093 1653 : return apoLayers[iLayer];
2094 : }
2095 :
2096 : /************************************************************************/
2097 : /* ICreateLayer() */
2098 : /************************************************************************/
2099 :
2100 1089 : OGRLayer *PCIDSK2Dataset::ICreateLayer(const char *pszLayerName,
2101 : const OGRGeomFieldDefn *poGeomFieldDefn,
2102 : CSLConstList /*papszOptions*/)
2103 : {
2104 : /* -------------------------------------------------------------------- */
2105 : /* Verify we are in update mode. */
2106 : /* -------------------------------------------------------------------- */
2107 1089 : if (eAccess != GA_Update)
2108 : {
2109 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
2110 : "Data source %s opened read-only.\n"
2111 : "New layer %s cannot be created.\n",
2112 0 : GetDescription(), pszLayerName);
2113 0 : return nullptr;
2114 : }
2115 :
2116 1089 : const auto eType = poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone;
2117 : const auto poSRS =
2118 1089 : poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRef() : nullptr;
2119 :
2120 : /* -------------------------------------------------------------------- */
2121 : /* Figure out what type of layer we need. */
2122 : /* -------------------------------------------------------------------- */
2123 2178 : std::string osLayerType;
2124 :
2125 1089 : switch (wkbFlatten(eType))
2126 : {
2127 10 : case wkbPoint:
2128 10 : osLayerType = "POINTS";
2129 10 : break;
2130 :
2131 10 : case wkbLineString:
2132 10 : osLayerType = "ARCS";
2133 10 : break;
2134 :
2135 6 : case wkbPolygon:
2136 6 : osLayerType = "WHOLE_POLYGONS";
2137 6 : break;
2138 :
2139 7 : case wkbNone:
2140 7 : osLayerType = "TABLE";
2141 7 : break;
2142 :
2143 1056 : default:
2144 1056 : break;
2145 : }
2146 :
2147 : /* -------------------------------------------------------------------- */
2148 : /* Create the segment. */
2149 : /* -------------------------------------------------------------------- */
2150 : int nSegNum;
2151 : try
2152 : {
2153 1093 : nSegNum = poFile->CreateSegment(pszLayerName, "", PCIDSK::SEG_VEC, 0L);
2154 : }
2155 1 : catch (const PCIDSKException &ex)
2156 : {
2157 1 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
2158 1 : return nullptr;
2159 : }
2160 1088 : PCIDSK::PCIDSKSegment *poSeg = poFile->GetSegment(nSegNum);
2161 : PCIDSK::PCIDSKVectorSegment *poVecSeg =
2162 1088 : dynamic_cast<PCIDSK::PCIDSKVectorSegment *>(poSeg);
2163 1088 : if (poVecSeg == nullptr)
2164 0 : return nullptr;
2165 :
2166 1088 : if (osLayerType != "")
2167 33 : poSeg->SetMetadataValue("LAYER_TYPE", osLayerType);
2168 :
2169 : /* -------------------------------------------------------------------- */
2170 : /* Do we need to apply a coordinate system? */
2171 : /* -------------------------------------------------------------------- */
2172 1088 : char *pszGeosys = nullptr;
2173 1088 : char *pszUnits = nullptr;
2174 1088 : double *padfPrjParams = nullptr;
2175 :
2176 1088 : if (poSRS != nullptr && poSRS->exportToPCI(&pszGeosys, &pszUnits,
2177 : &padfPrjParams) == OGRERR_NONE)
2178 : {
2179 : try
2180 : {
2181 6 : std::vector<double> adfPCIParameters;
2182 :
2183 108 : for (int i = 0; i < 17; i++)
2184 102 : adfPCIParameters.push_back(padfPrjParams[i]);
2185 :
2186 6 : if (STARTS_WITH_CI(pszUnits, "FOOT"))
2187 0 : adfPCIParameters.push_back(static_cast<double>(
2188 : static_cast<int>(PCIDSK::UNIT_US_FOOT)));
2189 6 : else if (STARTS_WITH_CI(pszUnits, "INTL FOOT"))
2190 0 : adfPCIParameters.push_back(static_cast<double>(
2191 : static_cast<int>(PCIDSK::UNIT_INTL_FOOT)));
2192 6 : else if (STARTS_WITH_CI(pszUnits, "DEGREE"))
2193 2 : adfPCIParameters.push_back(
2194 2 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_DEGREE)));
2195 : else
2196 4 : adfPCIParameters.push_back(
2197 4 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_METER)));
2198 :
2199 6 : poVecSeg->SetProjection(pszGeosys, adfPCIParameters);
2200 : }
2201 0 : catch (const PCIDSKException &ex)
2202 : {
2203 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
2204 : }
2205 :
2206 6 : CPLFree(pszGeosys);
2207 6 : CPLFree(pszUnits);
2208 6 : CPLFree(padfPrjParams);
2209 : }
2210 :
2211 : /* -------------------------------------------------------------------- */
2212 : /* Create the layer object. */
2213 : /* -------------------------------------------------------------------- */
2214 :
2215 1088 : apoLayers.push_back(new OGRPCIDSKLayer(this, poSeg, poVecSeg, TRUE));
2216 :
2217 1088 : return apoLayers.back();
2218 : }
2219 :
2220 : /************************************************************************/
2221 : /* GDALRegister_PCIDSK() */
2222 : /************************************************************************/
2223 :
2224 14 : void GDALRegister_PCIDSK()
2225 :
2226 : {
2227 14 : if (GDALGetDriverByName(DRIVER_NAME) != nullptr)
2228 0 : return;
2229 :
2230 14 : GDALDriver *poDriver = new GDALDriver();
2231 14 : PCIDSKDriverSetCommonMetadata(poDriver);
2232 :
2233 14 : poDriver->pfnOpen = PCIDSK2Dataset::Open;
2234 14 : poDriver->pfnCreate = PCIDSK2Dataset::Create;
2235 :
2236 14 : GetGDALDriverManager()->RegisterDriver(poDriver);
2237 : }
|