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(CSLConstList 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 : CSLConstList 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(CSLConstList 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 : CSLConstList 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.xorig, gt.xscale,
1251 58 : gt.xrot, gt.yorig, gt.yrot, gt.yscale);
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.xorig, gt.xscale, gt.xrot, gt.yorig,
1284 44 : gt.yrot, gt.yscale);
1285 : }
1286 0 : catch (const PCIDSKException &ex)
1287 : {
1288 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1289 0 : return CE_Failure;
1290 : }
1291 :
1292 : // If we got anything non-default return it.
1293 44 : if (gt != GDALGeoTransform())
1294 41 : return CE_None;
1295 : }
1296 :
1297 : /* -------------------------------------------------------------------- */
1298 : /* Check for worldfile if we have no other georeferencing. */
1299 : /* -------------------------------------------------------------------- */
1300 3 : if (GDALReadWorldFile(GetDescription(), "pxw", gt.data()))
1301 0 : return CE_None;
1302 :
1303 3 : return GDALPamDataset::GetGeoTransform(gt);
1304 : }
1305 :
1306 : /************************************************************************/
1307 : /* SetSpatialRef() */
1308 : /************************************************************************/
1309 :
1310 58 : CPLErr PCIDSK2Dataset::SetSpatialRef(const OGRSpatialReference *poSRS)
1311 :
1312 : {
1313 58 : PCIDSKGeoref *poGeoref = nullptr;
1314 :
1315 : try
1316 : {
1317 58 : PCIDSKSegment *poGeoSeg = poFile->GetSegment(1);
1318 58 : poGeoref = dynamic_cast<PCIDSKGeoref *>(poGeoSeg);
1319 : }
1320 0 : catch (const PCIDSKException &)
1321 : {
1322 : // I should really check whether this is an expected issue.
1323 : }
1324 :
1325 58 : if (poGeoref == nullptr)
1326 : {
1327 0 : return GDALPamDataset::SetSpatialRef(poSRS);
1328 : }
1329 :
1330 58 : char *pszGeosys = nullptr;
1331 58 : char *pszUnits = nullptr;
1332 58 : double *padfPrjParams = nullptr;
1333 :
1334 58 : if (poSRS == nullptr || poSRS->exportToPCI(&pszGeosys, &pszUnits,
1335 : &padfPrjParams) != OGRERR_NONE)
1336 : {
1337 0 : return GDALPamDataset::SetSpatialRef(poSRS);
1338 : }
1339 :
1340 58 : if (GetAccess() == GA_ReadOnly)
1341 : {
1342 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
1343 : "Unable to set projection on read-only file.");
1344 0 : CPLFree(pszGeosys);
1345 0 : CPLFree(pszUnits);
1346 0 : CPLFree(padfPrjParams);
1347 0 : return CE_Failure;
1348 : }
1349 :
1350 : try
1351 : {
1352 58 : GDALGeoTransform gt;
1353 58 : poGeoref->GetTransform(gt.xorig, gt.xscale, gt.xrot, gt.yorig, gt.yrot,
1354 58 : gt.yscale);
1355 :
1356 58 : poGeoref->WriteSimple(pszGeosys, gt.xorig, gt.xscale, gt.xrot, gt.yorig,
1357 58 : gt.yrot, gt.yscale);
1358 :
1359 116 : std::vector<double> adfPCIParameters;
1360 1044 : for (unsigned int i = 0; i < 17; i++)
1361 986 : adfPCIParameters.push_back(padfPrjParams[i]);
1362 :
1363 58 : if (STARTS_WITH_CI(pszUnits, "FOOT"))
1364 0 : adfPCIParameters.push_back(
1365 0 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_US_FOOT)));
1366 58 : else if (EQUALN(pszUnits, "INTL FOOT", 9))
1367 0 : adfPCIParameters.push_back(
1368 0 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_INTL_FOOT)));
1369 58 : else if (EQUALN(pszUnits, "DEGREE", 6))
1370 50 : adfPCIParameters.push_back(
1371 50 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_DEGREE)));
1372 : else
1373 8 : adfPCIParameters.push_back(
1374 8 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_METER)));
1375 :
1376 58 : poGeoref->WriteParameters(adfPCIParameters);
1377 : }
1378 0 : catch (const PCIDSKException &ex)
1379 : {
1380 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1381 0 : return CE_Failure;
1382 : }
1383 :
1384 58 : CPLFree(pszGeosys);
1385 58 : CPLFree(pszUnits);
1386 58 : CPLFree(padfPrjParams);
1387 :
1388 58 : return CE_None;
1389 : }
1390 :
1391 : /************************************************************************/
1392 : /* GetSpatialRef() */
1393 : /************************************************************************/
1394 :
1395 10 : const OGRSpatialReference *PCIDSK2Dataset::GetSpatialRef() const
1396 : {
1397 10 : return GetSpatialRef(false);
1398 : }
1399 :
1400 6 : const OGRSpatialReference *PCIDSK2Dataset::GetSpatialRefRasterOnly() const
1401 : {
1402 6 : return GetSpatialRef(true);
1403 : }
1404 :
1405 : static thread_local int tlsEnableLayersInGetSpatialRefCounter = 0;
1406 :
1407 16 : const OGRSpatialReference *PCIDSK2Dataset::GetSpatialRef(bool bRasterOnly) const
1408 : {
1409 16 : if (tlsEnableLayersInGetSpatialRefCounter)
1410 3 : return nullptr;
1411 :
1412 13 : if (m_poSRS)
1413 3 : return m_poSRS;
1414 :
1415 10 : PCIDSKGeoref *poGeoref = nullptr;
1416 :
1417 : try
1418 : {
1419 10 : PCIDSKSegment *poGeoSeg = poFile->GetSegment(1);
1420 10 : poGeoref = dynamic_cast<PCIDSKGeoref *>(poGeoSeg);
1421 : }
1422 0 : catch (const PCIDSKException &)
1423 : {
1424 : // I should really check whether this is an expected issue.
1425 : }
1426 :
1427 10 : if (poGeoref == nullptr)
1428 : {
1429 0 : ++tlsEnableLayersInGetSpatialRefCounter;
1430 : const OGRSpatialReference *poRet =
1431 0 : (bRasterOnly) ? GDALPamDataset::GetSpatialRefRasterOnly()
1432 0 : : GDALPamDataset::GetSpatialRef();
1433 0 : --tlsEnableLayersInGetSpatialRefCounter;
1434 0 : return poRet;
1435 : }
1436 :
1437 20 : CPLString osGeosys;
1438 10 : const char *pszUnits = nullptr;
1439 :
1440 20 : std::vector<double> adfParameters;
1441 10 : adfParameters.resize(18);
1442 :
1443 : try
1444 : {
1445 10 : osGeosys = poGeoref->GetGeosys();
1446 10 : adfParameters = poGeoref->GetParameters();
1447 : const UnitCode code =
1448 10 : static_cast<UnitCode>(static_cast<int>(adfParameters[16]));
1449 :
1450 10 : if (code == PCIDSK::UNIT_DEGREE)
1451 0 : pszUnits = "DEGREE";
1452 10 : else if (code == PCIDSK::UNIT_METER)
1453 0 : pszUnits = "METER";
1454 10 : else if (code == PCIDSK::UNIT_US_FOOT)
1455 0 : pszUnits = "FOOT";
1456 10 : else if (code == PCIDSK::UNIT_INTL_FOOT)
1457 0 : pszUnits = "INTL FOOT";
1458 : }
1459 0 : catch (const PCIDSKException &ex)
1460 : {
1461 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1462 : }
1463 :
1464 20 : OGRSpatialReference oSRS;
1465 10 : oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
1466 10 : if (oSRS.importFromPCI(osGeosys, pszUnits, &(adfParameters[0])) ==
1467 : OGRERR_NONE)
1468 : {
1469 7 : m_poSRS = oSRS.Clone();
1470 7 : return m_poSRS;
1471 : }
1472 : else
1473 : {
1474 3 : ++tlsEnableLayersInGetSpatialRefCounter;
1475 : const OGRSpatialReference *poRet =
1476 3 : (bRasterOnly) ? GDALPamDataset::GetSpatialRefRasterOnly()
1477 0 : : GDALPamDataset::GetSpatialRef();
1478 3 : --tlsEnableLayersInGetSpatialRefCounter;
1479 3 : return poRet;
1480 : }
1481 : }
1482 :
1483 : /************************************************************************/
1484 : /* IBuildOverviews() */
1485 : /************************************************************************/
1486 :
1487 3 : CPLErr PCIDSK2Dataset::IBuildOverviews(
1488 : const char *pszResampling, int nOverviews, const int *panOverviewList,
1489 : int nListBands, const int *panBandList, GDALProgressFunc pfnProgress,
1490 : void *pProgressData, CSLConstList papszOptions)
1491 :
1492 : {
1493 : PCIDSK2Band *poBand =
1494 3 : reinterpret_cast<PCIDSK2Band *>(GetRasterBand(panBandList[0]));
1495 :
1496 : /* -------------------------------------------------------------------- */
1497 : /* If RRD overviews requested, then invoke generic handling. */
1498 : /* -------------------------------------------------------------------- */
1499 3 : bool bUseGenericHandling = false;
1500 :
1501 3 : if (CPLTestBool(CPLGetConfigOption("USE_RRD", "NO")))
1502 : {
1503 1 : bUseGenericHandling = true;
1504 : }
1505 :
1506 : /* -------------------------------------------------------------------- */
1507 : /* If we don't have read access, then create the overviews */
1508 : /* externally. */
1509 : /* -------------------------------------------------------------------- */
1510 3 : if (GetAccess() != GA_Update)
1511 : {
1512 1 : CPLDebug("PCIDSK", "File open for read-only accessing, "
1513 : "creating overviews externally.");
1514 :
1515 1 : bUseGenericHandling = true;
1516 : }
1517 :
1518 3 : if (bUseGenericHandling)
1519 : {
1520 2 : if (poBand->GetOverviewCount() != 0)
1521 : {
1522 0 : CPLError(CE_Failure, CPLE_NotSupported,
1523 : "Cannot add external overviews when there are already "
1524 : "internal overviews");
1525 0 : return CE_Failure;
1526 : }
1527 :
1528 2 : return GDALDataset::IBuildOverviews(
1529 : pszResampling, nOverviews, panOverviewList, nListBands, panBandList,
1530 2 : pfnProgress, pProgressData, papszOptions);
1531 : }
1532 :
1533 1 : if (nListBands == 0)
1534 0 : return CE_None;
1535 :
1536 : /* -------------------------------------------------------------------- */
1537 : /* Currently no support for clearing overviews. */
1538 : /* -------------------------------------------------------------------- */
1539 1 : if (nOverviews == 0)
1540 : {
1541 0 : CPLError(CE_Failure, CPLE_AppDefined,
1542 : "PCIDSK2 driver does not currently support clearing existing "
1543 : "overviews. ");
1544 0 : return CE_Failure;
1545 : }
1546 :
1547 : /* -------------------------------------------------------------------- */
1548 : /* Establish which of the overview levels we already have, and */
1549 : /* which are new. We assume that band 1 of the file is */
1550 : /* representative. */
1551 : /* -------------------------------------------------------------------- */
1552 :
1553 1 : int nNewOverviews = 0;
1554 : int *panNewOverviewList =
1555 1 : static_cast<int *>(CPLCalloc(sizeof(int), nOverviews));
1556 2 : std::vector<bool> abFoundOverviewFactor(nOverviews);
1557 2 : for (int i = 0; i < nOverviews && poBand != nullptr; i++)
1558 : {
1559 1 : for (int j = 0; j < poBand->GetOverviewCount(); j++)
1560 : {
1561 0 : GDALRasterBand *poOverview = poBand->GetOverview(j);
1562 :
1563 : int nOvFactor =
1564 0 : GDALComputeOvFactor(poOverview->GetXSize(), poBand->GetXSize(),
1565 : poOverview->GetYSize(), poBand->GetYSize());
1566 :
1567 0 : if (nOvFactor == panOverviewList[i] ||
1568 0 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
1569 : poBand->GetXSize(),
1570 : poBand->GetYSize()))
1571 0 : abFoundOverviewFactor[i] = true;
1572 : }
1573 :
1574 1 : if (!abFoundOverviewFactor[i])
1575 1 : panNewOverviewList[nNewOverviews++] = panOverviewList[i];
1576 : }
1577 :
1578 : /* -------------------------------------------------------------------- */
1579 : /* Create the overviews that are missing. */
1580 : /* -------------------------------------------------------------------- */
1581 2 : for (int i = 0; i < nNewOverviews; i++)
1582 : {
1583 : try
1584 : {
1585 : // conveniently our resampling values mostly match PCIDSK.
1586 2 : poFile->CreateOverviews(nListBands, panBandList,
1587 2 : panNewOverviewList[i], pszResampling);
1588 : }
1589 0 : catch (const PCIDSKException &ex)
1590 : {
1591 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1592 0 : CPLFree(panNewOverviewList);
1593 0 : return CE_Failure;
1594 : }
1595 : }
1596 :
1597 1 : CPLFree(panNewOverviewList);
1598 1 : panNewOverviewList = nullptr;
1599 :
1600 2 : for (int iBand = 0; iBand < nListBands; iBand++)
1601 : {
1602 : poBand =
1603 1 : reinterpret_cast<PCIDSK2Band *>(GetRasterBand(panBandList[iBand]));
1604 1 : reinterpret_cast<PCIDSK2Band *>(poBand)->RefreshOverviewList();
1605 : }
1606 :
1607 : /* -------------------------------------------------------------------- */
1608 : /* Actually generate the overview imagery. */
1609 : /* -------------------------------------------------------------------- */
1610 1 : CPLErr eErr = CE_None;
1611 1 : std::vector<int> anRegenLevels;
1612 :
1613 : GDALRasterBand **papoOverviewBands = reinterpret_cast<GDALRasterBand **>(
1614 1 : CPLCalloc(sizeof(void *), nOverviews));
1615 :
1616 2 : for (int iBand = 0; iBand < nListBands && eErr == CE_None; iBand++)
1617 : {
1618 1 : nNewOverviews = 0;
1619 :
1620 : poBand =
1621 1 : reinterpret_cast<PCIDSK2Band *>(GetRasterBand(panBandList[iBand]));
1622 :
1623 2 : for (int i = 0; i < nOverviews && poBand != nullptr; i++)
1624 : {
1625 1 : for (int j = 0; j < poBand->GetOverviewCount(); j++)
1626 : {
1627 1 : GDALRasterBand *poOverview = poBand->GetOverview(j);
1628 :
1629 1 : int nOvFactor = GDALComputeOvFactor(
1630 : poOverview->GetXSize(), poBand->GetXSize(),
1631 : poOverview->GetYSize(), poBand->GetYSize());
1632 :
1633 1 : if (nOvFactor == panOverviewList[i] ||
1634 0 : nOvFactor == GDALOvLevelAdjust2(panOverviewList[i],
1635 : poBand->GetXSize(),
1636 : poBand->GetYSize()))
1637 : {
1638 1 : papoOverviewBands[nNewOverviews++] = poOverview;
1639 1 : anRegenLevels.push_back(j);
1640 1 : break;
1641 : }
1642 : }
1643 : }
1644 :
1645 1 : if (nNewOverviews > 0)
1646 : {
1647 1 : eErr = GDALRegenerateOverviewsEx(
1648 : (GDALRasterBandH)poBand, nNewOverviews,
1649 : reinterpret_cast<GDALRasterBandH *>(papoOverviewBands),
1650 : pszResampling, pfnProgress, pProgressData, papszOptions);
1651 :
1652 : // Mark the regenerated overviews as valid.
1653 2 : for (int i = 0; i < static_cast<int>(anRegenLevels.size()); i++)
1654 1 : poBand->poChannel->SetOverviewValidity(anRegenLevels[i], true);
1655 : }
1656 : }
1657 :
1658 1 : CPLFree(papoOverviewBands);
1659 :
1660 1 : return eErr;
1661 : }
1662 :
1663 : /************************************************************************/
1664 : /* PCIDSKTypeToGDAL() */
1665 : /************************************************************************/
1666 :
1667 477 : GDALDataType PCIDSK2Dataset::PCIDSKTypeToGDAL(eChanType eType)
1668 : {
1669 477 : switch (eType)
1670 : {
1671 339 : case CHN_8U:
1672 339 : return GDT_UInt8;
1673 :
1674 66 : case CHN_16U:
1675 66 : return GDT_UInt16;
1676 :
1677 18 : case CHN_16S:
1678 18 : return GDT_Int16;
1679 :
1680 18 : case CHN_32R:
1681 18 : return GDT_Float32;
1682 :
1683 0 : case CHN_BIT:
1684 0 : return GDT_UInt8;
1685 :
1686 0 : case CHN_C16U:
1687 0 : return GDT_CInt16;
1688 :
1689 18 : case CHN_C16S:
1690 18 : return GDT_CInt16;
1691 :
1692 18 : case CHN_C32R:
1693 18 : return GDT_CFloat32;
1694 :
1695 0 : default:
1696 0 : return GDT_Unknown;
1697 : }
1698 : }
1699 :
1700 : /************************************************************************/
1701 : /* Open() */
1702 : /************************************************************************/
1703 :
1704 136 : GDALDataset *PCIDSK2Dataset::Open(GDALOpenInfo *poOpenInfo)
1705 : {
1706 136 : if (!PCIDSKDriverIdentify(poOpenInfo))
1707 0 : return nullptr;
1708 :
1709 : /* -------------------------------------------------------------------- */
1710 : /* Try opening the file. */
1711 : /* -------------------------------------------------------------------- */
1712 136 : PCIDSKFile *poFile = nullptr;
1713 : const int nMaxBandCount =
1714 136 : atoi(CPLGetConfigOption("GDAL_MAX_BAND_COUNT", "65536"));
1715 : try
1716 : {
1717 276 : poFile = PCIDSK::Open(poOpenInfo->pszFilename,
1718 136 : poOpenInfo->eAccess == GA_ReadOnly ? "r" : "r+",
1719 : PCIDSK2GetInterfaces(), nMaxBandCount);
1720 135 : if (poFile == nullptr)
1721 : {
1722 0 : CPLError(CE_Failure, CPLE_OpenFailed,
1723 : "Failed to re-open %s within PCIDSK driver.\n",
1724 : poOpenInfo->pszFilename);
1725 0 : return nullptr;
1726 : }
1727 :
1728 : const bool bValidRasterDimensions =
1729 135 : poFile->GetWidth() && poFile->GetHeight();
1730 135 : if (!bValidRasterDimensions &&
1731 0 : (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0 &&
1732 0 : (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) == 0)
1733 : {
1734 0 : delete poFile;
1735 0 : return nullptr;
1736 : }
1737 :
1738 : /* Check if this is a vector-only PCIDSK file and that we are */
1739 : /* opened in raster-only mode */
1740 398 : if (poOpenInfo->eAccess == GA_ReadOnly &&
1741 128 : (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0 &&
1742 118 : (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) == 0 &&
1743 266 : poFile->GetChannels() == 0 &&
1744 138 : poFile->GetSegment(PCIDSK::SEG_VEC, "") != nullptr)
1745 : {
1746 2 : CPLDebug("PCIDSK",
1747 : "This is a vector-only PCIDSK dataset, "
1748 : "but it has been opened in read-only in raster-only mode");
1749 2 : delete poFile;
1750 2 : return nullptr;
1751 : }
1752 : /* Reverse test */
1753 392 : if (poOpenInfo->eAccess == GA_ReadOnly &&
1754 126 : (poOpenInfo->nOpenFlags & GDAL_OF_RASTER) == 0 &&
1755 10 : (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0 &&
1756 259 : poFile->GetChannels() != 0 &&
1757 133 : poFile->GetSegment(PCIDSK::SEG_VEC, "") == nullptr)
1758 : {
1759 0 : CPLDebug("PCIDSK",
1760 : "This is a raster-only PCIDSK dataset, "
1761 : "but it has been opened in read-only in vector-only mode");
1762 0 : delete poFile;
1763 0 : return nullptr;
1764 : }
1765 :
1766 133 : return LLOpen(poOpenInfo->pszFilename, poFile, poOpenInfo->eAccess,
1767 133 : poOpenInfo->GetSiblingFiles());
1768 : }
1769 : /* -------------------------------------------------------------------- */
1770 : /* Trap exceptions. */
1771 : /* -------------------------------------------------------------------- */
1772 1 : catch (const PCIDSKException &ex)
1773 : {
1774 1 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1775 1 : delete poFile;
1776 1 : return nullptr;
1777 : }
1778 0 : catch (...)
1779 : {
1780 0 : CPLError(CE_Failure, CPLE_AppDefined,
1781 : "PCIDSK::Create() failed, unexpected exception.");
1782 0 : delete poFile;
1783 0 : return nullptr;
1784 : }
1785 : }
1786 :
1787 : /************************************************************************/
1788 : /* LLOpen() */
1789 : /* */
1790 : /* Low level variant of open that takes the preexisting */
1791 : /* PCIDSKFile. */
1792 : /************************************************************************/
1793 :
1794 243 : GDALDataset *PCIDSK2Dataset::LLOpen(const char *pszFilename,
1795 : PCIDSK::PCIDSKFile *poFile,
1796 : GDALAccess eAccessIn,
1797 : char **papszSiblingFiles)
1798 :
1799 : {
1800 243 : PCIDSK2Dataset *poDS = new PCIDSK2Dataset();
1801 : /* -------------------------------------------------------------------- */
1802 : /* Create a corresponding GDALDataset. */
1803 : /* -------------------------------------------------------------------- */
1804 243 : poDS->poFile = poFile;
1805 243 : poDS->eAccess = eAccessIn;
1806 243 : poDS->nRasterXSize = poFile->GetWidth();
1807 243 : poDS->nRasterYSize = poFile->GetHeight();
1808 :
1809 : const bool bValidRasterDimensions =
1810 243 : poFile->GetWidth() && poFile->GetHeight();
1811 243 : if (!bValidRasterDimensions)
1812 : {
1813 0 : poDS->nRasterXSize = 512;
1814 0 : poDS->nRasterYSize = 512;
1815 : }
1816 :
1817 : try
1818 : {
1819 :
1820 : /* --------------------------------------------------------------------
1821 : */
1822 : /* Are we specifically PIXEL or BAND interleaving? */
1823 : /* */
1824 : /* We don't set anything for FILE since it is harder to know if */
1825 : /* this is tiled or what the on disk interleaving is. */
1826 : /* --------------------------------------------------------------------
1827 : */
1828 243 : if (EQUAL(poFile->GetInterleaving().c_str(), "PIXEL"))
1829 0 : poDS->SetMetadataItem("IMAGE_STRUCTURE", "PIXEL",
1830 : "IMAGE_STRUCTURE");
1831 243 : else if (EQUAL(poFile->GetInterleaving().c_str(), "BAND"))
1832 215 : poDS->SetMetadataItem("IMAGE_STRUCTURE", "BAND", "IMAGE_STRUCTURE");
1833 :
1834 : /* --------------------------------------------------------------------
1835 : */
1836 : /* Create band objects. */
1837 : /* --------------------------------------------------------------------
1838 : */
1839 476 : for (int iBand = 0;
1840 476 : bValidRasterDimensions && iBand < poFile->GetChannels(); iBand++)
1841 : {
1842 233 : PCIDSKChannel *poChannel = poFile->GetChannel(iBand + 1);
1843 466 : if (poChannel->GetBlockWidth() <= 0 ||
1844 233 : poChannel->GetBlockHeight() <= 0)
1845 : {
1846 0 : delete poDS;
1847 0 : return nullptr;
1848 : }
1849 :
1850 233 : if (PCIDSK2Dataset::PCIDSKTypeToGDAL(poChannel->GetType()) ==
1851 : GDT_Unknown)
1852 : {
1853 0 : continue;
1854 : }
1855 :
1856 233 : poDS->SetBand(poDS->GetRasterCount() + 1,
1857 233 : new PCIDSK2Band(poFile, poChannel));
1858 : }
1859 :
1860 : /* --------------------------------------------------------------------
1861 : */
1862 : /* Create band objects for bitmap segments. */
1863 : /* --------------------------------------------------------------------
1864 : */
1865 243 : int nLastBitmapSegment = 0;
1866 243 : PCIDSKSegment *poBitSeg = nullptr;
1867 :
1868 486 : while (bValidRasterDimensions &&
1869 486 : (poBitSeg = poFile->GetSegment(SEG_BIT, "",
1870 243 : nLastBitmapSegment)) != nullptr)
1871 : {
1872 0 : PCIDSKChannel *poChannel = dynamic_cast<PCIDSKChannel *>(poBitSeg);
1873 0 : if (poChannel == nullptr || poChannel->GetBlockWidth() <= 0 ||
1874 0 : poChannel->GetBlockHeight() <= 0)
1875 : {
1876 0 : delete poDS;
1877 0 : return nullptr;
1878 : }
1879 :
1880 0 : if (PCIDSK2Dataset::PCIDSKTypeToGDAL(poChannel->GetType()) ==
1881 : GDT_Unknown)
1882 : {
1883 0 : continue;
1884 : }
1885 :
1886 0 : poDS->SetBand(poDS->GetRasterCount() + 1,
1887 0 : new PCIDSK2Band(poChannel));
1888 :
1889 0 : nLastBitmapSegment = poBitSeg->GetSegmentNumber();
1890 : }
1891 :
1892 : /* --------------------------------------------------------------------
1893 : */
1894 : /* Create vector layers from vector segments. */
1895 : /* --------------------------------------------------------------------
1896 : */
1897 243 : PCIDSK::PCIDSKSegment *segobj = poFile->GetSegment(PCIDSK::SEG_VEC, "");
1898 338 : for (; segobj != nullptr;
1899 95 : segobj = poFile->GetSegment(PCIDSK::SEG_VEC, "",
1900 95 : segobj->GetSegmentNumber()))
1901 : {
1902 : PCIDSK::PCIDSKVectorSegment *poVecSeg =
1903 95 : dynamic_cast<PCIDSK::PCIDSKVectorSegment *>(segobj);
1904 95 : if (poVecSeg)
1905 95 : poDS->apoLayers.push_back(new OGRPCIDSKLayer(
1906 95 : poDS, segobj, poVecSeg, eAccessIn == GA_Update));
1907 : }
1908 :
1909 : /* --------------------------------------------------------------------
1910 : */
1911 : /* Process RPC segment, if there is one. */
1912 : /* --------------------------------------------------------------------
1913 : */
1914 243 : poDS->ProcessRPC();
1915 :
1916 : /* --------------------------------------------------------------------
1917 : */
1918 : /* Initialize any PAM information. */
1919 : /* --------------------------------------------------------------------
1920 : */
1921 243 : poDS->SetDescription(pszFilename);
1922 243 : poDS->TryLoadXML(papszSiblingFiles);
1923 :
1924 : /* --------------------------------------------------------------------
1925 : */
1926 : /* Open overviews. */
1927 : /* --------------------------------------------------------------------
1928 : */
1929 243 : poDS->oOvManager.Initialize(poDS, pszFilename, papszSiblingFiles);
1930 :
1931 243 : return poDS;
1932 : }
1933 :
1934 : /* -------------------------------------------------------------------- */
1935 : /* Trap exceptions. */
1936 : /* -------------------------------------------------------------------- */
1937 0 : catch (const PCIDSKException &ex)
1938 : {
1939 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
1940 : }
1941 0 : catch (...)
1942 : {
1943 0 : CPLError(CE_Failure, CPLE_AppDefined,
1944 : "PCIDSK SDK Failure in Open(), unexpected exception.");
1945 : }
1946 :
1947 : /* -------------------------------------------------------------------- */
1948 : /* In case of exception, close dataset */
1949 : /* -------------------------------------------------------------------- */
1950 0 : delete poDS;
1951 :
1952 0 : return nullptr;
1953 : }
1954 :
1955 : /************************************************************************/
1956 : /* Create() */
1957 : /************************************************************************/
1958 :
1959 124 : GDALDataset *PCIDSK2Dataset::Create(const char *pszFilename, int nXSize,
1960 : int nYSize, int nBandsIn,
1961 : GDALDataType eType,
1962 : CSLConstList papszParamList)
1963 :
1964 : {
1965 : /* -------------------------------------------------------------------- */
1966 : /* Prepare channel type list. */
1967 : /* -------------------------------------------------------------------- */
1968 248 : std::vector<eChanType> aeChanTypes;
1969 :
1970 124 : if (eType == GDT_Float32)
1971 3 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_32R);
1972 121 : else if (eType == GDT_Int16)
1973 3 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_16S);
1974 118 : else if (eType == GDT_UInt16)
1975 11 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_16U);
1976 107 : else if (eType == GDT_CInt16)
1977 3 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_C16S);
1978 104 : else if (eType == GDT_CFloat32)
1979 3 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_C32R);
1980 : else
1981 101 : aeChanTypes.resize(std::max(1, nBandsIn), CHN_8U);
1982 :
1983 : /* -------------------------------------------------------------------- */
1984 : /* Reformat options. Currently no support for jpeg compression */
1985 : /* quality. */
1986 : /* -------------------------------------------------------------------- */
1987 248 : CPLString osOptions;
1988 124 : const char *pszValue = CSLFetchNameValue(papszParamList, "INTERLEAVING");
1989 124 : if (pszValue == nullptr)
1990 116 : pszValue = "BAND";
1991 :
1992 124 : osOptions = pszValue;
1993 :
1994 124 : if (osOptions == "TILED")
1995 : {
1996 7 : pszValue = CSLFetchNameValue(papszParamList, "TILESIZE");
1997 7 : if (pszValue != nullptr)
1998 6 : osOptions += pszValue;
1999 :
2000 7 : pszValue = CSLFetchNameValue(papszParamList, "COMPRESSION");
2001 7 : if (pszValue != nullptr)
2002 : {
2003 4 : osOptions += " ";
2004 4 : osOptions += pszValue;
2005 : }
2006 :
2007 7 : pszValue = CSLFetchNameValue(papszParamList, "TILEVERSION");
2008 7 : if (pszValue != nullptr)
2009 : {
2010 4 : osOptions += " TILEV";
2011 4 : osOptions += pszValue;
2012 : }
2013 : }
2014 :
2015 : /* -------------------------------------------------------------------- */
2016 : /* Try creation. */
2017 : /* -------------------------------------------------------------------- */
2018 :
2019 : try
2020 : {
2021 124 : if (nBandsIn == 0)
2022 : {
2023 43 : nXSize = 512;
2024 43 : nYSize = 512;
2025 : }
2026 276 : PCIDSKFile *poFile = PCIDSK::Create(pszFilename, nXSize, nYSize,
2027 124 : nBandsIn, &(aeChanTypes[0]),
2028 : osOptions, PCIDSK2GetInterfaces());
2029 :
2030 : /* --------------------------------------------------------------------
2031 : */
2032 : /* Apply band descriptions, if provided as creation options. */
2033 : /* --------------------------------------------------------------------
2034 : */
2035 132 : for (size_t i = 0;
2036 132 : papszParamList != nullptr && papszParamList[i] != nullptr; i++)
2037 : {
2038 22 : if (STARTS_WITH_CI(papszParamList[i], "BANDDESC"))
2039 : {
2040 0 : int nBand = atoi(papszParamList[i] + 8);
2041 0 : const char *pszDescription = strstr(papszParamList[i], "=");
2042 0 : if (pszDescription && nBand > 0 && nBand <= nBandsIn)
2043 : {
2044 0 : poFile->GetChannel(nBand)->SetDescription(pszDescription +
2045 0 : 1);
2046 : }
2047 : }
2048 : }
2049 :
2050 110 : return LLOpen(pszFilename, poFile, GA_Update);
2051 : }
2052 : /* -------------------------------------------------------------------- */
2053 : /* Trap exceptions. */
2054 : /* -------------------------------------------------------------------- */
2055 28 : catch (const PCIDSKException &ex)
2056 : {
2057 14 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
2058 : }
2059 0 : catch (...)
2060 : {
2061 0 : CPLError(CE_Failure, CPLE_AppDefined,
2062 : "PCIDSK::Create() failed, unexpected exception.");
2063 : }
2064 :
2065 14 : return nullptr;
2066 : }
2067 :
2068 : /************************************************************************/
2069 : /* TestCapability() */
2070 : /************************************************************************/
2071 :
2072 79 : int PCIDSK2Dataset::TestCapability(const char *pszCap) const
2073 :
2074 : {
2075 79 : if (EQUAL(pszCap, ODsCCreateLayer))
2076 37 : return eAccess == GA_Update;
2077 42 : if (EQUAL(pszCap, ODsCRandomLayerWrite))
2078 0 : return eAccess == GA_Update;
2079 42 : if (EQUAL(pszCap, ODsCZGeometries))
2080 12 : return TRUE;
2081 :
2082 30 : return FALSE;
2083 : }
2084 :
2085 : /************************************************************************/
2086 : /* GetLayer() */
2087 : /************************************************************************/
2088 :
2089 1655 : const OGRLayer *PCIDSK2Dataset::GetLayer(int iLayer) const
2090 :
2091 : {
2092 1655 : if (iLayer < 0 || iLayer >= static_cast<int>(apoLayers.size()))
2093 2 : return nullptr;
2094 :
2095 1653 : return apoLayers[iLayer];
2096 : }
2097 :
2098 : /************************************************************************/
2099 : /* ICreateLayer() */
2100 : /************************************************************************/
2101 :
2102 1089 : OGRLayer *PCIDSK2Dataset::ICreateLayer(const char *pszLayerName,
2103 : const OGRGeomFieldDefn *poGeomFieldDefn,
2104 : CSLConstList /*papszOptions*/)
2105 : {
2106 : /* -------------------------------------------------------------------- */
2107 : /* Verify we are in update mode. */
2108 : /* -------------------------------------------------------------------- */
2109 1089 : if (eAccess != GA_Update)
2110 : {
2111 0 : CPLError(CE_Failure, CPLE_NoWriteAccess,
2112 : "Data source %s opened read-only.\n"
2113 : "New layer %s cannot be created.\n",
2114 0 : GetDescription(), pszLayerName);
2115 0 : return nullptr;
2116 : }
2117 :
2118 1089 : const auto eType = poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone;
2119 : const auto poSRS =
2120 1089 : poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRef() : nullptr;
2121 :
2122 : /* -------------------------------------------------------------------- */
2123 : /* Figure out what type of layer we need. */
2124 : /* -------------------------------------------------------------------- */
2125 2178 : std::string osLayerType;
2126 :
2127 1089 : switch (wkbFlatten(eType))
2128 : {
2129 10 : case wkbPoint:
2130 10 : osLayerType = "POINTS";
2131 10 : break;
2132 :
2133 10 : case wkbLineString:
2134 10 : osLayerType = "ARCS";
2135 10 : break;
2136 :
2137 6 : case wkbPolygon:
2138 6 : osLayerType = "WHOLE_POLYGONS";
2139 6 : break;
2140 :
2141 7 : case wkbNone:
2142 7 : osLayerType = "TABLE";
2143 7 : break;
2144 :
2145 1056 : default:
2146 1056 : break;
2147 : }
2148 :
2149 : /* -------------------------------------------------------------------- */
2150 : /* Create the segment. */
2151 : /* -------------------------------------------------------------------- */
2152 : int nSegNum;
2153 : try
2154 : {
2155 1093 : nSegNum = poFile->CreateSegment(pszLayerName, "", PCIDSK::SEG_VEC, 0L);
2156 : }
2157 1 : catch (const PCIDSKException &ex)
2158 : {
2159 1 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
2160 1 : return nullptr;
2161 : }
2162 1088 : PCIDSK::PCIDSKSegment *poSeg = poFile->GetSegment(nSegNum);
2163 : PCIDSK::PCIDSKVectorSegment *poVecSeg =
2164 1088 : dynamic_cast<PCIDSK::PCIDSKVectorSegment *>(poSeg);
2165 1088 : if (poVecSeg == nullptr)
2166 0 : return nullptr;
2167 :
2168 1088 : if (osLayerType != "")
2169 33 : poSeg->SetMetadataValue("LAYER_TYPE", osLayerType);
2170 :
2171 : /* -------------------------------------------------------------------- */
2172 : /* Do we need to apply a coordinate system? */
2173 : /* -------------------------------------------------------------------- */
2174 1088 : char *pszGeosys = nullptr;
2175 1088 : char *pszUnits = nullptr;
2176 1088 : double *padfPrjParams = nullptr;
2177 :
2178 1088 : if (poSRS != nullptr && poSRS->exportToPCI(&pszGeosys, &pszUnits,
2179 : &padfPrjParams) == OGRERR_NONE)
2180 : {
2181 : try
2182 : {
2183 6 : std::vector<double> adfPCIParameters;
2184 :
2185 108 : for (int i = 0; i < 17; i++)
2186 102 : adfPCIParameters.push_back(padfPrjParams[i]);
2187 :
2188 6 : if (STARTS_WITH_CI(pszUnits, "FOOT"))
2189 0 : adfPCIParameters.push_back(static_cast<double>(
2190 : static_cast<int>(PCIDSK::UNIT_US_FOOT)));
2191 6 : else if (STARTS_WITH_CI(pszUnits, "INTL FOOT"))
2192 0 : adfPCIParameters.push_back(static_cast<double>(
2193 : static_cast<int>(PCIDSK::UNIT_INTL_FOOT)));
2194 6 : else if (STARTS_WITH_CI(pszUnits, "DEGREE"))
2195 2 : adfPCIParameters.push_back(
2196 2 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_DEGREE)));
2197 : else
2198 4 : adfPCIParameters.push_back(
2199 4 : static_cast<double>(static_cast<int>(PCIDSK::UNIT_METER)));
2200 :
2201 6 : poVecSeg->SetProjection(pszGeosys, adfPCIParameters);
2202 : }
2203 0 : catch (const PCIDSKException &ex)
2204 : {
2205 0 : CPLError(CE_Failure, CPLE_AppDefined, "%s", ex.what());
2206 : }
2207 :
2208 6 : CPLFree(pszGeosys);
2209 6 : CPLFree(pszUnits);
2210 6 : CPLFree(padfPrjParams);
2211 : }
2212 :
2213 : /* -------------------------------------------------------------------- */
2214 : /* Create the layer object. */
2215 : /* -------------------------------------------------------------------- */
2216 :
2217 1088 : apoLayers.push_back(new OGRPCIDSKLayer(this, poSeg, poVecSeg, TRUE));
2218 :
2219 1088 : return apoLayers.back();
2220 : }
2221 :
2222 : /************************************************************************/
2223 : /* GDALRegister_PCIDSK() */
2224 : /************************************************************************/
2225 :
2226 14 : void GDALRegister_PCIDSK()
2227 :
2228 : {
2229 14 : if (GDALGetDriverByName(DRIVER_NAME) != nullptr)
2230 0 : return;
2231 :
2232 14 : GDALDriver *poDriver = new GDALDriver();
2233 14 : PCIDSKDriverSetCommonMetadata(poDriver);
2234 :
2235 14 : poDriver->pfnOpen = PCIDSK2Dataset::Open;
2236 14 : poDriver->pfnCreate = PCIDSK2Dataset::Create;
2237 :
2238 14 : GetGDALDriverManager()->RegisterDriver(poDriver);
2239 : }
|