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