-
Notifications
You must be signed in to change notification settings - Fork 0
/
qctcollection.cpp
302 lines (240 loc) · 6.62 KB
/
qctcollection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* > qctcollection.cpp
* 1.01 arb Wed Feb 23 10:45:35 GMT 2011 - error check openFilename
* 1.00 arb Tue Jul 20 00:18:47 BST 2010
*/
static const char SCCSid[] = "@(#)qctcollection.1.01 (C) 2010 arb QCT file collection";
/*
* To do:
* Cache results in a file so faster next time.
*/
#include <qapplication.h> // for cursor
#include <qdir.h>
#include <qstring.h>
#include "satlib/dundee.h" // for debugf
#include "osmap/qct.h"
#include "osmap/inpoly.h"
#include "qctcollection.h"
/* -------------------------------------------------------------------------
* This is each individual map in the collection, containing the map outline.
*/
QCTCollectionMap::QCTCollectionMap(const QString &fname)
{
filename = fname;
numpoints = 0;
degreesPerPixel = 0;
// description, chartname, scale should be null already
qct = new QCT();
// pass headeronly=true to prevent reading whole image
if (qct->openFilename(filename, true))
{
// Sample QCT has these fields:
// Title="Ordnance Survey GB Route Planner", Name="OS GB Route Planner 1:1M", Identifier=""
// Title="RIVER TAY", Name="BA1481 RIVER TAY", Identifier="BA1481"
description = qct->getName(); // eg. "BA1481 RIVER TAY"
chartname = qct->getIdentifier(); // eg. "BA1481" or empty
scale = QString::null; // XXX
degreesPerPixel = qct->getDegreesPerPixel(); // XXX slow?
numpoints = qct->getOutlineSize();
if (numpoints > 2)
{
intpoints = new unsigned int[numpoints][2];
int ii;
double lat, lon;
for (ii=0; ii<numpoints; ii++)
{
qct->getOutlinePoint(ii, &lat, &lon);
// Convert all real numbers to positive integers
#define LAT_TO_INT(L) (unsigned int)((L+90.0) * 1e3)
#define LON_TO_INT(L) (unsigned int)((L+180.0) * 1e3)
intpoints[ii][0] = LON_TO_INT(lon);
intpoints[ii][1] = LAT_TO_INT(lat);
}
}
qct->closeFilename();
}
else
{
fprintf(stderr, "Warning: cannot load '%s' into QCT collection\n", (const char*)fname);
}
delete qct;
}
QCTCollectionMap::~QCTCollectionMap()
{
if (numpoints > 2)
delete [] intpoints;
}
QString
QCTCollectionMap::getFilename() const
{
return filename;
}
QString
QCTCollectionMap::getLabel() const
{
return description;
}
QString
QCTCollectionMap::getScale() const
{
return scale;
}
float
QCTCollectionMap::getDegreesPerPixel() const
{
return degreesPerPixel;
}
bool
QCTCollectionMap::containsLatLon(double lat, double lon)
{
int rc, intlat, intlon;
// Need at least 3 points to make a polygon!
if (numpoints < 3)
return false;
// Convert all real numbers to positive integers
intlat = LAT_TO_INT(lat);
intlon = LON_TO_INT(lon);
// Fast check if point within polygon
rc = inpoly(intpoints, numpoints, intlon, intlat);
return (rc ? true : false);
}
/* -------------------------------------------------------------------------
* This is the thread which runs in the background collecting map outlines.
*/
QCTCollectionWorker::QCTCollectionWorker()
{
qctlistptr = 0;
}
QCTCollectionWorker::~QCTCollectionWorker()
{
}
void
QCTCollectionWorker::setPaths(QStringList paths)
{
mapPaths = paths;
}
void
QCTCollectionWorker::setMapList(QPtrList<QCTCollectionMap> *qctlistp)
{
qctlistptr = qctlistp;
}
// Call this when you have an interactive requirement for the results
// it prevents the background thread sleeping nicely so it's faster
void
QCTCollectionWorker::hurryUp()
{
stillBackgroundThread = false;
}
void
QCTCollectionWorker::run()
{
if (qctlistptr == 0 || mapPaths.count() == 0)
return;
// Be nice
stillBackgroundThread = true;
QStringList::Iterator pathiter;
for (pathiter = mapPaths.begin(); pathiter != mapPaths.end(); ++pathiter)
{
debugf(2, " QCTCollection dir %s\n", (const char*)*pathiter);
QDir dir(*pathiter);
QStringList files = dir.entryList("*.qct *.QCT");
QStringList::Iterator fileiter;
for (fileiter = files.begin(); fileiter != files.end(); ++fileiter)
{
debugf(3, " QCTCollection file %s\n", (const char*)*fileiter);
QCTCollectionMap *map = new QCTCollectionMap(*pathiter + "/" + *fileiter);
qctlistptr->append(map);
// The user can tell us the hurry up (when the collection is
// requested before we've finished collecting it) but until
// then we sleep to ensure the foreground thread gets some CPU.
if (stillBackgroundThread)
QThread::msleep(100);
}
}
}
/* -------------------------------------------------------------------------
* This is the collection of maps, this is the object which the user creates.
*/
QCTCollection::QCTCollection(const QString &qctdir)
{
qctlist.setAutoDelete(true);
if (!qctdir.isEmpty())
setPath(qctdir);
}
QCTCollection::~QCTCollection()
{
if (worker.running())
{
worker.terminate();
worker.wait();
}
}
void
QCTCollection::setPath(const QString &path)
{
setPaths(QStringList(path));
}
void
QCTCollection::setPaths(QStringList paths)
{
mapPaths = paths;
collectMaps();
}
void
QCTCollection::collectMaps()
{
qctlist.clear();
// Tell the worker where to look and where to put the results
worker.setPaths(mapPaths);
worker.setMapList(&qctlist);
// Start it running in the background thread
worker.start();
}
QStringList
QCTCollection::mapsListAtLatLon(double lat, double lon)
{
// Collection of maps must have completed before we can continue
QApplication::setOverrideCursor(Qt::waitCursor);
worker.hurryUp();
worker.wait();
QApplication::restoreOverrideCursor();
// Create a table mapping resolution to map name
typedef QMap<float,QString> mapTable_t;
mapTable_t maptable;
// Iterate through all maps in the collection looking for those which
// cover the point of interest and add the map name to a table which is
// indexed by resolution.
debugf(1,"QCTCollection::mapsListAtLatLon query %f %f\n",lat,lon);
for ( QCTCollectionMap *mapiter = qctlist.first(); mapiter; mapiter = qctlist.next() )
{
if (mapiter->containsLatLon(lat, lon))
{
debugf(2," match %s\n", (const char*)mapiter->getFilename());
maptable[mapiter->getDegreesPerPixel()] = mapiter->getLabel();
}
}
// Create a list of map names by extracting them from the table.
// Note the items in the table are automatically sorted by the primary key
// (the resolution) so we can simply pull out the sorted map names
// ordered from close-up to zoomed-out, handy for the menu.
QStringList list;
mapTable_t::Iterator iter;
for ( iter = maptable.begin(); iter != maptable.end(); ++iter )
{
list << iter.data();
}
return(list);
}
QString
QCTCollection::getFilenameForMap(const QString &mapname)
{
QString fn;
for ( QCTCollectionMap *mapiter = qctlist.first(); mapiter; mapiter = qctlist.next() )
{
if (mapiter->getLabel() == mapname)
{
fn = mapiter->getFilename();
break;
}
}
return fn;
}