Added gui options for wireframe, and colour coding
[tecorrec.git] / geo / tcSrtmCache.cpp
blob78e30da52f0bd0768210a256825661508617023d
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 tcSrtmCache.cpp
22 * @brief Manages cache of SRTM data.
25 #include "tcSrtmCache.h"
26 #include "tcSrtmData.h"
28 #include <QString>
29 #include <QStringList>
30 #include <QFile>
32 #include <cstdlib>
35 * Static variables
38 QDir tcSrtmCache::s_dataDirectory("/home/james/cs/pro/data/srtm");
41 * Static functions
44 /// Get a list of datasets.
45 QStringList tcSrtmCache::dataSets()
47 return s_dataDirectory.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
51 * Constructors + destructor
54 /// Primary constructor.
55 tcSrtmCache::tcSrtmCache(const QString& dataSet)
56 : m_dataSet(dataSet)
58 for (int i = 0; i < 360; ++i)
60 for (int j = 0; j < 180; ++j)
62 m_cache[i][j] = 0;
67 /// Destructor.
68 tcSrtmCache::~tcSrtmCache()
70 for (int i = 0; i < 360; ++i)
72 for (int j = 0; j < 180; ++j)
74 if ((tcSrtmData*)0x1 != m_cache[i][j])
76 delete m_cache[i][j];
83 * Main interface
86 /// Load raw SRTM height file.
87 tcSrtmData* tcSrtmCache::loadData(int lon, int lat)
89 tcSrtmData*& cacheItem = cache(lon, lat);
90 if (0 == cacheItem)
92 QString filename = QString("%1/%2/%3%4%5%6.hgt")
93 .arg(s_dataDirectory.path())
94 .arg(m_dataSet)
95 .arg(lat >= 0 ? 'N' : 'S')
96 .arg(abs(lat), 2, 10, QLatin1Char('0'))
97 .arg(lon >= 0 ? 'E' : 'W')
98 .arg(abs(lon), 3, 10, QLatin1Char('0'));
99 QFile file(filename);
100 if (file.open(QIODevice::ReadOnly))
102 cacheItem = new tcSrtmData(lon, lat, file);
104 else
106 // 0x1 represents a previous failed attempt
107 cacheItem = (tcSrtmData*)0x1;
110 if ((tcSrtmData*)0x1 != cacheItem)
112 return cacheItem;
114 else
116 return 0;
120 /// Load raw SRTM height file.
121 tcSrtmData* tcSrtmCache::loadData(const tcGeo& coord)
123 int lon,lat;
124 toIntLonLat(coord, &lon, &lat);
125 return loadData(lon, lat);
128 /** Find some SRTM height data overlapping an area.
129 * @param swCorner Geographical coordinate of south-west corner.
130 * @param neCorner Geographical coordinate of north-east corner.
131 * @returns An arbitrary SRTM data for a chunk in the lon lat range.
133 tcSrtmData* tcSrtmCache::findData(const tcGeo& swCorner, const tcGeo& neCorner)
135 int swLon,swLat;
136 int neLon,neLat;
137 toIntLonLat(swCorner, &swLon, &swLat);
138 toIntLonLat(neCorner, &neLon, &neLat);
139 for (int lon = swLon; lon <= neLon; ++lon)
141 for (int lat = swLat; lat <= neLat; ++lat)
143 tcSrtmData* result = loadData(lon,lat);
144 if (0 != result)
146 return result;
150 return 0;
154 * Private functions
157 /// Convert a geographical coordinate into lon lat ints.
158 void tcSrtmCache::toIntLonLat(const tcGeo& coord, int* const outLon, int* const outLat)
160 int lat = (int)floor(coord.lat() * 180.0/M_PI);
161 int lon = (int)floor(coord.lon() * 180.0/M_PI);
162 lat = lat % 360;
163 if (lat < -90)
165 lat += 360;
167 if (lat >= 90)
169 lat -= 180;
170 lon += 180;
172 lon = lon % 360;
173 if (lon < -180)
175 lon += 360;
177 else if (lon >= 180)
179 lon -= 360;
181 *outLon = lon;
182 *outLat = lat;
185 /// Cache index.
186 tcSrtmData*& tcSrtmCache::cache(int lon, int lat)
188 Q_ASSERT(lon >= -180 && lon < 180);
189 Q_ASSERT(lat >= -90 && lat < 90);
190 return m_cache[180 + lon][90 + lat];