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 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #include "pcidsk_exception.h"
32 : #include "core/metadataset.h"
33 :
34 : #include "segment/metadatasegment.h"
35 :
36 : #include <string>
37 :
38 : using namespace PCIDSK;
39 :
40 : /************************************************************************/
41 : /* MetadataSet() */
42 : /************************************************************************/
43 :
44 1969 : MetadataSet::MetadataSet()
45 :
46 :
47 : {
48 1969 : this->file = nullptr;
49 1969 : id = -1;
50 1969 : loaded = false;
51 1969 : }
52 :
53 : /************************************************************************/
54 : /* ~MetadataSet() */
55 : /************************************************************************/
56 :
57 1969 : MetadataSet::~MetadataSet()
58 :
59 : {
60 1969 : }
61 :
62 : /************************************************************************/
63 : /* Initialize() */
64 : /************************************************************************/
65 :
66 1958 : void MetadataSet::Initialize( PCIDSKFile *fileIn, const std::string& groupIn, int idIn )
67 :
68 : {
69 1958 : this->file = fileIn;
70 1958 : this->group = groupIn;
71 1958 : this->id = idIn;
72 1958 : }
73 :
74 : /************************************************************************/
75 : /* Load() */
76 : /************************************************************************/
77 :
78 1507 : void MetadataSet::Load()
79 :
80 : {
81 1507 : if( loaded )
82 0 : return;
83 :
84 : // This legitimately occurs in some situations, such for overview channel
85 : // objects.
86 1507 : if( file == nullptr )
87 : {
88 0 : loaded = true;
89 0 : return;
90 : }
91 :
92 1507 : PCIDSKSegment *seg = file->GetSegment( SEG_SYS , "METADATA");
93 :
94 1507 : if( seg == nullptr )
95 : {
96 1368 : loaded = true;
97 1368 : return;
98 : }
99 :
100 139 : MetadataSegment *md_seg = dynamic_cast<MetadataSegment *>( seg );
101 139 : if( md_seg )
102 139 : md_seg->FetchGroupMetadata( group.c_str(), id, md_set );
103 139 : loaded = true;
104 : }
105 :
106 : /************************************************************************/
107 : /* GetMetadataValue() */
108 : /************************************************************************/
109 :
110 1344 : std::string MetadataSet::GetMetadataValue( const std::string& key )
111 :
112 : {
113 1344 : if( !loaded )
114 1205 : Load();
115 :
116 1344 : if( md_set.count(key) == 0 )
117 1194 : return "";
118 : else
119 150 : return md_set[key];
120 : }
121 :
122 : /************************************************************************/
123 : /* SetMetadataValue() */
124 : /************************************************************************/
125 :
126 71 : void MetadataSet::SetMetadataValue( const std::string& key, const std::string& value )
127 : {
128 71 : if( !loaded )
129 49 : Load();
130 :
131 71 : if( file == nullptr )
132 : {
133 0 : return ThrowPCIDSKException( "Attempt to set metadata on an unassociated MetadataSet, likely an overview channel." );
134 : }
135 :
136 71 : md_set[key] = value;
137 :
138 71 : PCIDSKSegment *seg = file->GetSegment( SEG_SYS , "METADATA");
139 :
140 71 : if( seg == nullptr )
141 : {
142 32 : file->CreateSegment("METADATA",
143 : "Please do not modify this metadata segment.",
144 32 : SEG_SYS, 64); // Create a 32k segment by default.
145 32 : seg = file->GetSegment( SEG_SYS , "METADATA");
146 : }
147 :
148 71 : MetadataSegment *md_seg = dynamic_cast<MetadataSegment *>( seg );
149 71 : if( md_seg )
150 71 : md_seg->SetGroupMetadataValue( group.c_str(), id, key, value );
151 : }
152 :
153 : /************************************************************************/
154 : /* GetMetadataKeys() */
155 : /************************************************************************/
156 :
157 277 : std::vector<std::string> MetadataSet::GetMetadataKeys()
158 : {
159 277 : if( !loaded )
160 253 : Load();
161 :
162 277 : std::vector<std::string> keys;
163 277 : std::map<std::string,std::string>::iterator it;
164 :
165 327 : for( it = md_set.begin(); it != md_set.end(); ++it )
166 : {
167 : // If the value is empty the key value pair will be deleted in the file
168 : // This will ensure that the returned list key are in sync with the file
169 50 : if(!it->second.empty())
170 50 : keys.push_back(it->first);
171 : }
172 :
173 554 : return keys;
174 : }
|