Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Purpose: Implementation of the MetadataSet class. This is a container 4 : * for a set of metadata, and used by the file, channel and segment 5 : * classes to manage metadata for themselves. It is not public 6 : * to SDK users. 7 : * 8 : ****************************************************************************** 9 : * Copyright (c) 2009 10 : * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada. 11 : * 12 : * SPDX-License-Identifier: MIT 13 : ****************************************************************************/ 14 : 15 : #include "pcidsk_exception.h" 16 : #include "core/metadataset.h" 17 : 18 : #include "segment/metadatasegment.h" 19 : 20 : #include <string> 21 : 22 : using namespace PCIDSK; 23 : 24 : /************************************************************************/ 25 : /* MetadataSet() */ 26 : /************************************************************************/ 27 : 28 1976 : MetadataSet::MetadataSet() 29 : 30 : 31 : { 32 1976 : this->file = nullptr; 33 1976 : id = -1; 34 1976 : loaded = false; 35 1976 : } 36 : 37 : /************************************************************************/ 38 : /* ~MetadataSet() */ 39 : /************************************************************************/ 40 : 41 1976 : MetadataSet::~MetadataSet() 42 : 43 : { 44 1976 : } 45 : 46 : /************************************************************************/ 47 : /* Initialize() */ 48 : /************************************************************************/ 49 : 50 1965 : void MetadataSet::Initialize( PCIDSKFile *fileIn, const std::string& groupIn, int idIn ) 51 : 52 : { 53 1965 : this->file = fileIn; 54 1965 : this->group = groupIn; 55 1965 : this->id = idIn; 56 1965 : } 57 : 58 : /************************************************************************/ 59 : /* Load() */ 60 : /************************************************************************/ 61 : 62 1510 : void MetadataSet::Load() 63 : 64 : { 65 1510 : if( loaded ) 66 0 : return; 67 : 68 : // This legitimately occurs in some situations, such for overview channel 69 : // objects. 70 1510 : if( file == nullptr ) 71 : { 72 0 : loaded = true; 73 0 : return; 74 : } 75 : 76 1510 : PCIDSKSegment *seg = file->GetSegment( SEG_SYS , "METADATA"); 77 : 78 1510 : if( seg == nullptr ) 79 : { 80 1371 : loaded = true; 81 1371 : return; 82 : } 83 : 84 139 : MetadataSegment *md_seg = dynamic_cast<MetadataSegment *>( seg ); 85 139 : if( md_seg ) 86 139 : md_seg->FetchGroupMetadata( group.c_str(), id, md_set ); 87 139 : loaded = true; 88 : } 89 : 90 : /************************************************************************/ 91 : /* GetMetadataValue() */ 92 : /************************************************************************/ 93 : 94 1345 : std::string MetadataSet::GetMetadataValue( const std::string& key ) 95 : 96 : { 97 1345 : if( !loaded ) 98 1205 : Load(); 99 : 100 1345 : if( md_set.count(key) == 0 ) 101 1195 : return ""; 102 : else 103 150 : return md_set[key]; 104 : } 105 : 106 : /************************************************************************/ 107 : /* SetMetadataValue() */ 108 : /************************************************************************/ 109 : 110 73 : void MetadataSet::SetMetadataValue( const std::string& key, const std::string& value ) 111 : { 112 73 : if( !loaded ) 113 50 : Load(); 114 : 115 73 : if( file == nullptr ) 116 : { 117 0 : return ThrowPCIDSKException( "Attempt to set metadata on an unassociated MetadataSet, likely an overview channel." ); 118 : } 119 : 120 73 : md_set[key] = value; 121 : 122 73 : PCIDSKSegment *seg = file->GetSegment( SEG_SYS , "METADATA"); 123 : 124 73 : if( seg == nullptr ) 125 : { 126 33 : file->CreateSegment("METADATA", 127 : "Please do not modify this metadata segment.", 128 33 : SEG_SYS, 64); // Create a 32k segment by default. 129 33 : seg = file->GetSegment( SEG_SYS , "METADATA"); 130 : } 131 : 132 73 : MetadataSegment *md_seg = dynamic_cast<MetadataSegment *>( seg ); 133 73 : if( md_seg ) 134 73 : md_seg->SetGroupMetadataValue( group.c_str(), id, key, value ); 135 : } 136 : 137 : /************************************************************************/ 138 : /* GetMetadataKeys() */ 139 : /************************************************************************/ 140 : 141 280 : std::vector<std::string> MetadataSet::GetMetadataKeys() 142 : { 143 280 : if( !loaded ) 144 255 : Load(); 145 : 146 280 : std::vector<std::string> keys; 147 280 : std::map<std::string,std::string>::iterator it; 148 : 149 330 : for( it = md_set.begin(); it != md_set.end(); ++it ) 150 : { 151 : // If the value is empty the key value pair will be deleted in the file 152 : // This will ensure that the returned list key are in sync with the file 153 50 : if(!it->second.empty()) 154 50 : keys.push_back(it->first); 155 : } 156 : 157 560 : return keys; 158 : }