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 tcSrtmCache.cpp
22 * @brief Manages cache of SRTM data.
25 #include "tcSrtmCache.h"
26 #include "tcSrtmData.h"
29 #include <QStringList>
38 QDir
tcSrtmCache::s_dataDirectory("/home/james/cs/pro/data/srtm");
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
)
58 for (int i
= 0; i
< 360; ++i
)
60 for (int j
= 0; j
< 180; ++j
)
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
])
86 /// Load raw SRTM height file.
87 tcSrtmData
* tcSrtmCache::loadData(int lon
, int lat
)
89 tcSrtmData
*& cacheItem
= cache(lon
, lat
);
92 QString filename
= QString("%1/%2/%3%4%5%6.hgt")
93 .arg(s_dataDirectory
.path())
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'));
100 if (file
.open(QIODevice::ReadOnly
))
102 cacheItem
= new tcSrtmData(lon
, lat
, file
);
106 // 0x1 represents a previous failed attempt
107 cacheItem
= (tcSrtmData
*)0x1;
110 if ((tcSrtmData
*)0x1 != cacheItem
)
120 /// Load raw SRTM height file.
121 tcSrtmData
* tcSrtmCache::loadData(const tcGeo
& coord
)
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
)
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
);
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
);
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
];