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 1981 : MetadataSet::MetadataSet() 29 : 30 : 31 : { 32 1981 : this->file = nullptr; 33 1981 : id = -1; 34 1981 : loaded = false; 35 1981 : } 36 : 37 : /************************************************************************/ 38 : /* ~MetadataSet() */ 39 : /************************************************************************/ 40 : 41 1981 : MetadataSet::~MetadataSet() 42 : 43 : { 44 1981 : } 45 : 46 : /************************************************************************/ 47 : /* Initialize() */ 48 : /************************************************************************/ 49 : 50 1970 : void MetadataSet::Initialize( PCIDSKFile *fileIn, const std::string& groupIn, int idIn ) 51 : 52 : { 53 1970 : this->file = fileIn; 54 1970 : this->group = groupIn; 55 1970 : this->id = idIn; 56 1970 : } 57 : 58 : /************************************************************************/ 59 : /* Load() */ 60 : /************************************************************************/ 61 : 62 1515 : void MetadataSet::Load() 63 : 64 : { 65 1515 : if( loaded ) 66 0 : return; 67 : 68 : // This legitimately occurs in some situations, such for overview channel 69 : // objects. 70 1515 : if( file == nullptr ) 71 : { 72 0 : loaded = true; 73 0 : return; 74 : } 75 : 76 1515 : PCIDSKSegment *seg = file->GetSegment( SEG_SYS , "METADATA"); 77 : 78 1515 : if( seg == nullptr ) 79 : { 80 1373 : loaded = true; 81 1373 : return; 82 : } 83 : 84 142 : MetadataSegment *md_seg = dynamic_cast<MetadataSegment *>( seg ); 85 142 : if( md_seg ) 86 142 : md_seg->FetchGroupMetadata( group.c_str(), id, md_set ); 87 142 : loaded = true; 88 : } 89 : 90 : /************************************************************************/ 91 : /* GetMetadataValue() */ 92 : /************************************************************************/ 93 : 94 1406 : std::string MetadataSet::GetMetadataValue( const std::string& key ) 95 : 96 : { 97 1406 : if( !loaded ) 98 1207 : Load(); 99 : 100 1406 : if( md_set.count(key) == 0 ) 101 1254 : return ""; 102 : else 103 152 : return md_set[key]; 104 : } 105 : 106 : /************************************************************************/ 107 : /* SetMetadataValue() */ 108 : /************************************************************************/ 109 : 110 75 : void MetadataSet::SetMetadataValue( const std::string& key, const std::string& value ) 111 : { 112 75 : if( !loaded ) 113 51 : Load(); 114 : 115 75 : if( file == nullptr ) 116 : { 117 0 : return ThrowPCIDSKException( "Attempt to set metadata on an unassociated MetadataSet, likely an overview channel." ); 118 : } 119 : 120 75 : md_set[key] = value; 121 : 122 75 : PCIDSKSegment *seg = file->GetSegment( SEG_SYS , "METADATA"); 123 : 124 75 : if( seg == nullptr ) 125 : { 126 34 : file->CreateSegment("METADATA", 127 : "Please do not modify this metadata segment.", 128 34 : SEG_SYS, 64); // Create a 32k segment by default. 129 34 : seg = file->GetSegment( SEG_SYS , "METADATA"); 130 : } 131 : 132 75 : MetadataSegment *md_seg = dynamic_cast<MetadataSegment *>( seg ); 133 75 : if( md_seg ) 134 75 : md_seg->SetGroupMetadataValue( group.c_str(), id, key, value ); 135 : } 136 : 137 : /************************************************************************/ 138 : /* GetMetadataKeys() */ 139 : /************************************************************************/ 140 : 141 282 : std::vector<std::string> MetadataSet::GetMetadataKeys() 142 : { 143 282 : if( !loaded ) 144 257 : Load(); 145 : 146 282 : std::vector<std::string> keys; 147 282 : std::map<std::string,std::string>::iterator it; 148 : 149 332 : 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 564 : return keys; 158 : }