-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathqmapwidget.cpp
More file actions
409 lines (325 loc) · 12.9 KB
/
qmapwidget.cpp
File metadata and controls
409 lines (325 loc) · 12.9 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "qmapwidget.h"
#include "tileSources/OSMTileSource.h"
#include "CircleObject.h"
#include "mapcoloroverlay.h"
#include "MapGraphicsNetwork.h"
#include <QUrl>
#include <QFile>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QSharedPointer>
#include <QtDebug>
#include <QThread>
#include <QImage>
#include <QTemporaryFile>
#include <QXmlStreamReader>
#include <limits>
// currently not in use
QMapWidget::QMapWidget(MapGraphicsScene *scene, QWidget *parent):MapGraphicsView(scene,parent)
{
this->setMouseTracking(true); // required for tracking mouse movement with mousepress
this->scene = scene;
//Setup some tile sources
QSharedPointer<OSMTileSource> osmTiles(new OSMTileSource(OSMTileSource::OSMTiles), &QObject::deleteLater);
this->setTileSource(osmTiles);
this->setZoomLevel(1);
this->centerOn(103.7500, 1.3667);
addCountryOverlay("United States of America", QColor(Qt::red));
}
QMapWidget::QMapWidget(MapGraphicsScene *scene, QWidget *parent, qreal centerX, qreal centerY, int zoom):MapGraphicsView(scene,parent)
{
// get the MapGraphicsScene object (required to add overlays)
this->scene = scene;
//Setup some tile sources
QSharedPointer<OSMTileSource> osmTiles(new OSMTileSource(OSMTileSource::OSMTiles), &QObject::deleteLater);
this->setTileSource(osmTiles);
this->setZoomLevel(zoom);
this->centerOn(centerX, centerY);
// QList<PolygonObject *> polygons = addCountryOverlay("United States of America", QColor(Qt::darkRed));
// PolygonObject *polygon = polygons.first();
// polygon->setCountry(QString("USA"));
// polygon->updateObjectData(QString("USA"), 12345);
// addCountryOverlay("China", QColor(Qt::red));
// addCountryOverlay("Canada", QColor(Qt::red));
// addCountryOverlay("Australia", QColor(Qt::red));
// //addCountryOverlay("Brasil", QColor(Qt::red));
// //addCountryOverlay("Russia", QColor(Qt::red));
// addCountryOverlay("India", QColor(Qt::red));
// addCountryOverlay("United Kingdom", QColor(Qt::red));
}
void QMapWidget::locateCity(QString cityName)
{
currentZoomLevel = 12;
geocodingCountry(cityName);
}
void QMapWidget::locateCountry(QString countryName)
{
currentZoomLevel = 5;
geocodingCountry(countryName);
}
// protected
void QMapWidget::geocodingCountry(QString countryName)
{
MapGraphicsNetwork * network = MapGraphicsNetwork::getInstance();
QString host;
QString url;
qDebug() << "start";
//Figure out which server to request from based on our desired tile type
host = "http://nominatim.openstreetmap.org/";
url = "/search?q=%1&format=xml&addressdetails=1";
//Build the request
const QString fetchURL = url.arg(QString::fromAscii(QUrl::toPercentEncoding(countryName)));
QNetworkRequest request(QUrl(host + fetchURL));
//Send the request and setupd a signal to ensure we're notified when it finishes
QNetworkReply * reply = network->get(request);
connect(reply,
SIGNAL(finished()),
this,
SLOT(handleNetworkRequestFinished()));
}
// private slots
void QMapWidget::handleNetworkRequestFinished()
{
QObject * sender = QObject::sender();
QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender);
qDebug() << "response";
if (reply == 0)
{
qWarning() << "QNetworkReply cast failure";
return;
}
/*
We can do this here and use reply later in the function because the reply
won't be deleted until execution returns to the event loop.
*/
reply->deleteLater();
//If there was a network error, ignore the reply
if (reply->error() != QNetworkReply::NoError)
{
qDebug() << "Network Error:" << reply->errorString();
return;
}
//qDebug() << reply->readAll();
QXmlStreamReader *xmlReader = new QXmlStreamReader(reply->readAll());
qreal xpos, ypos;
while(!xmlReader->atEnd() && !xmlReader->hasError()) {
// Read next element
QXmlStreamReader::TokenType token = xmlReader->readNext();
//If token is just StartDocument - go to next
if(token == QXmlStreamReader::StartDocument) {
continue;
}
//If token is StartElement - read it
if(token == QXmlStreamReader::StartElement) {
if(xmlReader->name() == "place") {
xpos = xmlReader->attributes().value("lon").toString().toDouble();
ypos = xmlReader->attributes().value("lat").toString().toDouble();
qDebug() << "lng: " << xpos << "lat: " <<ypos;
break;
}
}
}
if(xmlReader->hasError()) {
qDebug("error parse file");
}
if(xpos>-180 && xpos<180 && ypos>-90 && ypos<90) {
setZoomLevel(currentZoomLevel);
this->centerOn(xpos, ypos);
}
//close reader and flush file
xmlReader->clear();
}
QList<PolygonObject *> QMapWidget::addCountryOverlay(QString countryName, QColor color)
{
MapColorOverlay *overlay = new MapColorOverlay(countryName, color);
QList<PolygonObject *> polygons = overlay->PaintCountryToWidget();
for (int i = 0; i < polygons.count(); i++) {
scene->addObject(polygons.at(i)); // add a list of object (some countries has multiple shape entries)
}
// scene->addObject(polygon);
return polygons;
}
void QMapWidget::addRegionOverlay(QPolygonF regionPolygon, QColor color)
{
}
QString QMapWidget::getItemName()
{
return itemName;
}
void QMapWidget::loadHistoryData(const QString fileName, HistoryDataType datatype)
{
Q_UNUSED(datatype)
QFile *xmlFile = new QFile(fileName);
if (!xmlFile->open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug("error open file");
return;
}
qDebug()<<"load history data";
QXmlStreamReader *xmlReader = new QXmlStreamReader(xmlFile);
historyData = QHash< QString, QHash<QString, int> >();
QHash<QString, int> hashValue;
QString countryName;
QString year;
_firstYear = QString("9999");
_lastYear = QString("-9999");
maxDataValue = std::numeric_limits<int>::min();
minDataValue = std::numeric_limits<int>::max();
while(!xmlReader->atEnd() && !xmlReader->hasError()) {
// Read next element
QXmlStreamReader::TokenType token = xmlReader->readNext();
//If token is just StartDocument - go to next
//qDebug() << "xml text: " << xmlReader->name();
if(token == QXmlStreamReader::StartDocument) {
continue;
}
//If token is StartElement - read it
if(token == QXmlStreamReader::StartElement) {
if(xmlReader->name() == "field") {
QString nameValue = xmlReader->attributes().value("name").toString();
if(!nameValue.compare(QString("Country or Area"))){
QString newCountryName = QString(xmlReader->readElementText());
if(newCountryName.compare(countryName)){
hashValue = QHash<QString, int>();
countryName = newCountryName;
}
qDebug() << "country name: " << countryName;
} else if (!nameValue.compare(QString("Item"))){
itemName = QString(xmlReader->readElementText());
qDebug() << "item name: " << itemName;
} else if (!nameValue.compare(QString("Year"))){
year = QString(xmlReader->readElementText());
int yearValue = year.toInt();
qDebug() << "year: " << year;
if (yearValue < _firstYear.toInt()){
_firstYear = year;
}
if (yearValue > _lastYear.toInt()){
_lastYear = year;
}
} else if (!nameValue.compare(QString("Value"))) {
hashValue[year] = static_cast<int>(xmlReader->readElementText().toDouble());
historyData[countryName] = hashValue;
//qDebug() << "Value: " << hashValue[year];
if(hashValue[year]>maxDataValue){
maxDataValue = hashValue[year];
}
if(hashValue[year]<minDataValue && hashValue[year]>=0){
minDataValue = hashValue[year];
}
}
}
}
}
}
// private
void QMapWidget::displayHistoryData()
{
qDebug() << "start history data";
MapGraphicsScene *scene = this->getScene();
QHash< QString, QHash<QString, int> >::iterator i;
for (i = historyData.begin(); i != historyData.end(); ++i){
QString countryName = i.key();
qDebug() << "get country name: " << countryName;
QHash<QString, int> v = i.value();
QHash<QString, int>::iterator j;
for (j = v.begin(); j != v.end(); ++j){
QString currentYear = j.key();
int currentValue = j.value();
qDebug() << "country:" << countryName << "year: " << currentYear << "value: " << currentValue;
if (!currentYear.compare(_firstYear)){
//qDebug() << "current value: " << currentValue;
if (!countryName.compare(QString("China"))){
QList<PolygonObject *> polygons = this->addCountryOverlay(countryName, QColor(Qt::red));
if(polygons.isEmpty()){
continue;
}
PolygonObject *polygon = polygons.first();
polygon->setCountry(countryName);
qDebug() << "add country" << countryName << "population: " << currentValue;
polygon->updateObjectData(countryName, currentValue, maxDataValue, minDataValue);
for (int k = 0; k < polygons.count(); k++) {
scene->addObject(polygons.at(k));
}
scene->addObject(polygon);
}
}
}
}
}
void QMapWidget::getDataForDate(QDate date)
{
}
int QMapWidget::firstYear()
{
return _firstYear.toInt();
}
int QMapWidget::lastYear()
{
return _lastYear.toInt();
}
void QMapWidget::displayHistoryDataForCountry(QString country)
{
QHash<QString, int> currentCountryData = historyData[country];
int firstValue = currentCountryData[_firstYear];
QList<PolygonObject *> polygons = this->addCountryOverlay(country, QColor(Qt::red));
if(polygons.isEmpty()){
return;
}
PolygonObject *polygon = polygons.first();
polygon->setMajorRegion(true);
qDebug() << "add country" << country << "population: " << firstValue;
for (int k = 0; k < polygons.count(); k++) {
polygons.at(k)->setCountry(country);
polygon->updateObjectData(country, firstValue, maxDataValue, minDataValue);
polygons.at(k)->updateColor(firstValue, maxDataValue, minDataValue);
qDebug() << "draw canada";
//scene->addObject(polygons.at(k));
}
//scene->addObject(polygon);
}
void QMapWidget::displayHistoryDataForCountries(QList<QString> countries)
{
QList<QString>::iterator i;
for (i = countries.begin(); i!=countries.end(); ++i) {
QString country = *i;
QHash<QString, int> currentCountryData = historyData[country];
int firstValue = currentCountryData[_firstYear];
QList<PolygonObject *> polygons = this->addCountryOverlay(country, QColor(Qt::red));
if(polygons.isEmpty()){
return;
}
PolygonObject *polygon = polygons.first();
polygon->setMajorRegion(true);
qDebug() << "add country" << country << "population: " << firstValue;
for (int k = 0; k < polygons.count(); k++) {
polygons.at(k)->setCountry(country);
polygon->updateObjectData(country, firstValue, maxDataValue, minDataValue);
polygons.at(k)->updateColor(firstValue, maxDataValue, minDataValue);
//scene->addObject(polygons.at(k));
}
//scene->addObject(polygon);
}
}
void QMapWidget::updateDataForCountry(QString country, int year)
{
MapGraphicsScene *scene = this->getScene();
QHash<QString, int> currentCountryData = historyData[country];
int firstValue = currentCountryData[QString::number(year)];
scene->updateObjectsData(country, firstValue, maxDataValue, minDataValue);
}
void QMapWidget::updateDataForCountries(QList<QString> countries, int year)
{
MapGraphicsScene *scene = this->getScene();
QList<QString>::iterator i;
for (i = countries.begin(); i!=countries.end(); ++i) {
QString country = *i;
QHash<QString, int> currentCountryData = historyData[country];
int firstValue = currentCountryData[QString::number(year)];
scene->updateObjectsData(country, firstValue, maxDataValue, minDataValue);
}
}
void QMapWidget::removeAllCountryOverlay()
{
MapGraphicsScene *scene = this->getScene();
scene->removeAllCountries();
}