Simple program to read arcinfo ascii files and convert into hgt elevation data (for...
[tecorrec.git] / geo / tcLandsatMetaData.cpp
blob965d9a55bbba957f8935397dbe9cbcacbe779aed
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 /**
21 * @file tcLandsatMetaData.cpp
22 * @brief Landsat metadata file.
25 #include "tcLandsatMetaData.h"
27 #include <QFile>
28 #include <QRegExp>
30 #include <QtDebug>
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*$");
44 while (!file.atEnd())
46 QByteArray line = file.readLine();
47 if (regexp.exactMatch(line))
49 QString name = regexp.cap(1);
50 if ("GROUP" == name)
52 name = regexp.cap(2);
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)
63 return;
65 if ("OBJECT" == name)
67 name = regexp.cap(2);
68 tcLandsatMetaData* newMeta = new tcLandsatMetaData(file);
69 m_objects[name] += newMeta;
71 else if ("END_OBJECT" == name)
73 return;
75 else
77 QVariant value;
78 QString valueStr = regexp.cap(3);
79 if (valueStr.isEmpty())
81 valueStr = regexp.cap(4);
82 // Try turning it into a double
83 bool ok;
84 double d = valueStr.toDouble(&ok);
85 if (ok)
87 value = d;
89 else
91 // perhaps its a list
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)
98 // of strings?
99 if (str.exactMatch(values[i]))
101 varValues += str.cap(1);
103 else
105 // or maybe doubles
106 bool ok;
107 double d = values[i].toDouble(&ok);
108 if (ok)
110 varValues += d;
112 else
114 varValues += values[i];
118 value = varValues;
120 else
122 value = valueStr;
126 else
128 value = valueStr;
130 m_values[name] = value;
133 else if (end.exactMatch(line))
135 return;
137 else if (blank.exactMatch(line))
140 else
142 qDebug() << "Parsing error reading landsat meta data:" << line;
147 /// Destructor.
148 tcLandsatMetaData::~tcLandsatMetaData()
150 foreach (tcLandsatMetaData* group, m_groups)
152 delete group;
154 QStringList objectNames = m_objects.keys();
155 foreach (QString objectName, objectNames)
157 QList<tcLandsatMetaData*>& objects = m_objects[objectName];
158 foreach (tcLandsatMetaData* object, objects)
160 delete object;
166 * Accessors
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();
212 else
214 return 0;
218 /// Access a group.
219 tcLandsatMetaData* tcLandsatMetaData::group(const QString& name) const
221 if (m_groups.contains(name))
223 return m_groups[name];
225 else
227 return 0;
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];
238 else
240 return 0;
244 /// Access a value.
245 QVariant tcLandsatMetaData::value(const QString& name) const
247 if (m_values.contains(name))
249 return m_values[name];
251 else
253 return QVariant();