1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
21 * @file tcLandsatMetaData.cpp
22 * @brief Landsat metadata file.
25 #include "tcLandsatMetaData.h"
33 * Constructors + destructor
36 /// Load metadata from a file.
37 tcLandsatMetaData::tcLandsatMetaData(QFile
& file
)
39 static QRegExp
regexp("^\\s*(\\w+)\\s*=\\s*(\"(.*)\"|(\\S(.*\\S)?))\\s*$");
40 static QRegExp
end("^\\s*END\\s*$");
41 static QRegExp
blank("^\\s*$");
42 static QRegExp
strlist("^\\s*\\((.*)\\)\\s*$");
43 static QRegExp
str("^\\s*\"([^\"]*)\"\\s*$");
46 QByteArray line
= file
.readLine();
47 if (regexp
.exactMatch(line
))
49 QString name
= regexp
.cap(1);
53 if (m_groups
.contains(name
))
55 delete m_groups
[name
];
56 qDebug() << "Landsat meta data has conflicting groups:" << line
;
58 tcLandsatMetaData
* newMeta
= new tcLandsatMetaData(file
);
59 m_groups
[name
] = newMeta
;
61 else if ("END_GROUP" == name
)
68 tcLandsatMetaData
* newMeta
= new tcLandsatMetaData(file
);
69 m_objects
[name
] += newMeta
;
71 else if ("END_OBJECT" == name
)
78 QString valueStr
= regexp
.cap(3);
79 if (valueStr
.isEmpty())
81 valueStr
= regexp
.cap(4);
82 // Try turning it into a double
84 double d
= valueStr
.toDouble(&ok
);
92 if (strlist
.exactMatch(valueStr
))
94 QStringList values
= strlist
.cap(1).split(",");
95 QList
<QVariant
> varValues
;
96 for (int i
= 0; i
< values
.size(); ++i
)
99 if (str
.exactMatch(values
[i
]))
101 varValues
+= str
.cap(1);
107 double d
= values
[i
].toDouble(&ok
);
114 varValues
+= values
[i
];
130 m_values
[name
] = value
;
133 else if (end
.exactMatch(line
))
137 else if (blank
.exactMatch(line
))
142 qDebug() << "Parsing error reading landsat meta data:" << line
;
148 tcLandsatMetaData::~tcLandsatMetaData()
150 foreach (tcLandsatMetaData
* group
, m_groups
)
154 QStringList objectNames
= m_objects
.keys();
155 foreach (QString objectName
, objectNames
)
157 QList
<tcLandsatMetaData
*>& objects
= m_objects
[objectName
];
158 foreach (tcLandsatMetaData
* object
, objects
)
169 /// Get the list of group names.
170 QStringList
tcLandsatMetaData::groupNames() const
172 return m_groups
.keys();
175 /// Get the list of object names.
176 QStringList
tcLandsatMetaData::objectNames() const
178 return m_objects
.keys();
181 /// Get the list of value names.
182 QStringList
tcLandsatMetaData::valueNames() const
184 return m_values
.keys();
187 /// Find whether a certain group exists.
188 bool tcLandsatMetaData::hasGroup(const QString
& name
) const
190 return m_groups
.contains(name
);
193 /// Find whether a certain object exists.
194 bool tcLandsatMetaData::hasObject(const QString
& name
) const
196 return m_groups
.contains(name
);
199 /// Find whether a certain value exists.
200 bool tcLandsatMetaData::hasValue(const QString
& name
) const
202 return m_values
.contains(name
);
205 /// Get the number of objects with a particular name.
206 int tcLandsatMetaData::numObjects(const QString
& name
) const
208 if (m_objects
.contains(name
))
210 return m_objects
[name
].size();
219 tcLandsatMetaData
* tcLandsatMetaData::group(const QString
& name
) const
221 if (m_groups
.contains(name
))
223 return m_groups
[name
];
231 /// Access an object.
232 tcLandsatMetaData
* tcLandsatMetaData::object(const QString
& name
, int index
) const
234 if (m_objects
.contains(name
) && index
>= 0 && index
< m_objects
[name
].size())
236 return m_objects
[name
][index
];
245 QVariant
tcLandsatMetaData::value(const QString
& name
) const
247 if (m_values
.contains(name
))
249 return m_values
[name
];