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, 50 West Wilmot Street, Richmond Hill, Ont, 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 290 : MetadataSet::MetadataSet()
45 :
46 :
47 : {
48 290 : this->file = NULL;
49 290 : id = -1;
50 290 : loaded = false;
51 290 : }
52 :
53 : /************************************************************************/
54 : /* ~MetadataSet() */
55 : /************************************************************************/
56 :
57 290 : MetadataSet::~MetadataSet()
58 :
59 : {
60 290 : }
61 :
62 : /************************************************************************/
63 : /* Initialize() */
64 : /************************************************************************/
65 :
66 287 : void MetadataSet::Initialize( PCIDSKFile *file, const std::string& group, int id )
67 :
68 : {
69 287 : this->file = file;
70 287 : this->group = group;
71 287 : this->id = id;
72 287 : }
73 :
74 : /************************************************************************/
75 : /* Load() */
76 : /************************************************************************/
77 :
78 89 : void MetadataSet::Load()
79 :
80 : {
81 89 : if( loaded )
82 0 : return;
83 :
84 : // This legitimately occurs in some situations, such for overview channel
85 : // objects.
86 89 : if( file == NULL )
87 : {
88 0 : loaded = true;
89 0 : return;
90 : }
91 :
92 89 : PCIDSKSegment *seg = file->GetSegment( SEG_SYS , "METADATA");
93 :
94 89 : if( seg == NULL )
95 : {
96 78 : loaded = true;
97 78 : return;
98 : }
99 :
100 11 : MetadataSegment *md_seg = dynamic_cast<MetadataSegment *>( seg );
101 :
102 11 : md_seg->FetchMetadata( group.c_str(), id, md_set );
103 11 : loaded = true;
104 : }
105 :
106 : /************************************************************************/
107 : /* GetMetadataValue() */
108 : /************************************************************************/
109 :
110 34 : std::string MetadataSet::GetMetadataValue( const std::string& key )
111 :
112 : {
113 34 : if( !loaded )
114 0 : Load();
115 :
116 34 : if( md_set.count(key) == 0 )
117 23 : return "";
118 : else
119 11 : return md_set[key];
120 : }
121 :
122 : /************************************************************************/
123 : /* SetMetadataValue() */
124 : /************************************************************************/
125 :
126 24 : void MetadataSet::SetMetadataValue( const std::string& key, const std::string& value )
127 :
128 : {
129 24 : if( !loaded )
130 17 : Load();
131 :
132 24 : if( file == NULL )
133 : {
134 0 : ThrowPCIDSKException( "Attempt to set metadata on an unassociated MetadataSet, likely an overview channel." );
135 : }
136 :
137 24 : md_set[key] = value;
138 :
139 24 : PCIDSKSegment *seg = file->GetSegment( SEG_SYS , "METADATA");
140 :
141 24 : if( seg == NULL )
142 : {
143 : file->CreateSegment( "METADATA",
144 : "Please do not modify this metadata segment.",
145 17 : SEG_SYS, 0 );
146 34 : seg = file->GetSegment( SEG_SYS , "METADATA");
147 : }
148 :
149 24 : MetadataSegment *md_seg = dynamic_cast<MetadataSegment *>( seg );
150 :
151 24 : md_seg->SetMetadataValue( group.c_str(), id, key, value );
152 24 : }
153 :
154 : /************************************************************************/
155 : /* GetMetadataKeys() */
156 : /************************************************************************/
157 :
158 74 : std::vector<std::string> MetadataSet::GetMetadataKeys()
159 :
160 : {
161 74 : if( !loaded )
162 72 : Load();
163 :
164 74 : std::vector<std::string> keys;
165 74 : std::map<std::string,std::string>::iterator it;
166 :
167 92 : for( it = md_set.begin(); it != md_set.end(); it++ )
168 : {
169 18 : keys.push_back( (*it).first );
170 : }
171 :
172 0 : return keys;
173 : }
174 :
175 :
|