Bladeren bron

Cleanning code. Found simpler maps at http://code.highcharts.com/mapdata/. Removed the city class.

Rafael Arce Nazario 9 jaren geleden
bovenliggende
commit
1719d7e13c
10 gewijzigde bestanden met toevoegingen van 42 en 115 verwijderingen
  1. 0
    6
      city.cpp
  2. 0
    58
      city.h
  3. 23
    20
      country.cpp
  4. 3
    3
      country.h
  5. 0
    1
      data/cityLimitsPR.json
  6. 2
    0
      data/pr-all-all.geo.json
  7. 0
    14
      mainwindow.cpp
  8. 12
    9
      map.cpp
  9. 1
    3
      prMap.pro
  10. 1
    1
      prmap.qrc

+ 0
- 6
city.cpp Bestand weergeven

1
-#include "city.h"
2
-
3
-void City::pushPoint(double x, double y) {
4
-    geometry->push_back(QPointF(x,y));
5
-
6
-}

+ 0
- 58
city.h Bestand weergeven

1
-#ifndef CITY_H
2
-#define CITY_H
3
-
4
-#include <QPointF>
5
-#include <QVector>
6
-
7
-///
8
-/// \brief The City class
9
-///        This is merely a vector of points, i.e. the points that make up the
10
-///        polygon of the city.
11
-
12
-class City {
13
-public:
14
-    QVector<QPointF> *geometry;
15
-    
16
-    /// \fn City()
17
-    /// \~English
18
-    /// \brief default constructor
19
-    /// \~Spanish
20
-    /// \brief default constructor
21
-    ///
22
-    City() {};
23
-
24
-    /// \fn pushPoint(double x, double y)
25
-    /// \~English
26
-    /// \brief push a point to the vector of points
27
-    /// \~Spanish
28
-    /// \brief añadir un punto al final del vector de puntos
29
-    ///
30
-    void pushPoint(double x, double y);
31
-
32
-    /// \fn getSize()
33
-    /// \~English
34
-    /// \brief get the size of the vector that contains the points
35
-    /// \~Spanish
36
-    /// \brief devuelve el tamaño del vector de puntos
37
-    ///
38
-    virtual int getSize() const { return geometry->size(); }
39
-
40
-    /// \fn QVector<QPointF> *getGeometry()
41
-    /// \~English
42
-    /// \brief get a pointer to the vector of points
43
-    /// \~Spanish
44
-    /// \brief devuelve un puntero al vector de puntos
45
-    ///
46
-    QVector<QPointF> *getGeometry() const { return geometry; }
47
-    
48
-
49
-    /// \fn ~City()
50
-    /// \~English
51
-    /// \brief destructor
52
-    /// \~Spanish
53
-    /// \brief destructor
54
-    ///    
55
-    ~City() {delete geometry; }
56
-};
57
-
58
-#endif // CITY_H

+ 23
- 20
country.cpp Bestand weergeven

6
 #include <QJsonDocument>
6
 #include <QJsonDocument>
7
 
7
 
8
 void Country::limits(){
8
 void Country::limits(){
9
-    QMap<QString,City*>::iterator it = Cities.begin();
9
+    QMap<QString,QPolygonF*>::iterator it = Cities.begin();
10
 
10
 
11
-    minX = maxX = Cities.begin().value()->geometry->at(0).x();
12
-    minY = maxY = Cities.begin().value()->geometry->at(0).y();
11
+    minX = maxX = Cities.begin().value()->at(0).x();
12
+    minY = maxY = Cities.begin().value()->at(0).y();
13
 
13
 
14
     for (it = Cities.begin() + 1; it != Cities.end(); ++it) {
14
     for (it = Cities.begin() + 1; it != Cities.end(); ++it) {
15
 
15
 
16
-        City *c = it.value();
16
+        QPolygonF *c = it.value();
17
 
17
 
18
-        for (int i = 0; i < c->getSize(); i++) {
19
-            if (c->geometry->at(i).x() != 0.0) {
20
-            if (c->geometry->at(i).x() > maxX ) maxX = c->geometry->at(i).x();
21
-            if (c->geometry->at(i).y() > maxY ) maxY = c->geometry->at(i).y();
22
-            if (c->geometry->at(i).x() < minX ) minX = c->geometry->at(i).x();
23
-            if (c->geometry->at(i).y() < minY ) minY = c->geometry->at(i).y();
18
+        for (int i = 0; i < c->size(); i++) {
19
+            if (c->at(i).x() != 0.0) {
20
+            if (c->at(i).x() > maxX ) maxX = c->at(i).x();
21
+            if (c->at(i).y() > maxY ) maxY = c->at(i).y();
22
+            if (c->at(i).x() < minX ) minX = c->at(i).x();
23
+            if (c->at(i).y() < minY ) minY = c->at(i).y();
24
             }
24
             }
25
         }
25
         }
26
     }
26
     }
33
     QFile loadFile(fileName);
33
     QFile loadFile(fileName);
34
 
34
 
35
     if (!loadFile.open(QIODevice::ReadOnly)) {
35
     if (!loadFile.open(QIODevice::ReadOnly)) {
36
-        qWarning("Couldn't open save file.");
36
+        qWarning("Couldn't open read file.");
37
         return false;
37
         return false;
38
     }
38
     }
39
 
39
 
40
+    qDebug() << "JSON ok...";
41
+
40
     QByteArray saveData = loadFile.readAll();
42
     QByteArray saveData = loadFile.readAll();
41
     QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
43
     QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
42
-    QJsonArray topLevelArray = loadDoc.array();
43
 
44
 
45
+    QJsonArray featureLevelArray = loadDoc.object()["features"].toArray();
44
     int ctr = 1;
46
     int ctr = 1;
45
-    foreach(QJsonValue obj, topLevelArray) {
46
-        QVector<QPointF> *points = new QVector<QPointF>;
47
+    foreach(QJsonValue obj, featureLevelArray) {
48
+        QPolygonF *poly = new QPolygonF;
47
         QJsonArray geometryArray = obj.toObject()["geometry"].toObject()["coordinates"].toArray();
49
         QJsonArray geometryArray = obj.toObject()["geometry"].toObject()["coordinates"].toArray();
48
         QString polyType = obj.toObject()["geometry"].toObject()["type"].toString();
50
         QString polyType = obj.toObject()["geometry"].toObject()["type"].toString();
51
+        qDebug() << polyType;
49
         if (polyType == "Polygon") {
52
         if (polyType == "Polygon") {
50
             for (int i = 0; i < geometryArray[0].toArray().size(); i++) {
53
             for (int i = 0; i < geometryArray[0].toArray().size(); i++) {
51
-                points->push_back(QPointF(
54
+                poly->push_back(QPointF(
52
                                 geometryArray[0].toArray()[i].toArray()[0].toDouble(),
55
                                 geometryArray[0].toArray()[i].toArray()[0].toDouble(),
53
                                 geometryArray[0].toArray()[i].toArray()[1].toDouble()));
56
                                 geometryArray[0].toArray()[i].toArray()[1].toDouble()));
54
             }
57
             }
58
             for (int i = 0; i <geometryArray.size() ; i++) {
61
             for (int i = 0; i <geometryArray.size() ; i++) {
59
                 // for every point in the city's polygon
62
                 // for every point in the city's polygon
60
                 for (int j = 0; j < geometryArray[i].toArray()[0].toArray().size(); j++) {
63
                 for (int j = 0; j < geometryArray[i].toArray()[0].toArray().size(); j++) {
61
-                    points->push_back(QPointF(
64
+                    poly->push_back(QPointF(
62
                                           geometryArray[i].toArray()[0].toArray()[j].toArray()[0].toDouble(),
65
                                           geometryArray[i].toArray()[0].toArray()[j].toArray()[0].toDouble(),
63
                                           geometryArray[i].toArray()[0].toArray()[j].toArray()[1].toDouble()));
66
                                           geometryArray[i].toArray()[0].toArray()[j].toArray()[1].toDouble()));
64
 
67
 
65
                 }
68
                 }
66
                 // I will push a 0,0 between each polygon of a multipolygon to distinguish
69
                 // I will push a 0,0 between each polygon of a multipolygon to distinguish
67
-                points->push_back(QPointF(0,0));
70
+                poly->push_back(QPointF(0,0));
68
             }
71
             }
69
         }
72
         }
70
 
73
 
71
-        QString cityName = obj.toObject()["properties"].toObject()["city"].toString();
74
+        QString cityName = obj.toObject()["properties"].toObject()["name"].toString();
72
 
75
 
73
         qDebug() << "Storing: " << cityName;
76
         qDebug() << "Storing: " << cityName;
74
-        Cities[cityName] = new City;
75
-        Cities[cityName]->geometry = points;
77
+        Cities[cityName] = poly; //new City;
78
+        //Cities[cityName]->geometry = poly;
76
 
79
 
77
         ctr++;
80
         ctr++;
78
     }
81
     }

+ 3
- 3
country.h Bestand weergeven

2
 #define COUNTRY_H
2
 #define COUNTRY_H
3
 
3
 
4
 #include <QPointF>
4
 #include <QPointF>
5
-#include <city.h>
6
 #include <QMap>
5
 #include <QMap>
7
-
6
+#include <QPolygonF>
8
 
7
 
9
 /// \brief A class to display a map of a country, given the coordinates
8
 /// \brief A class to display a map of a country, given the coordinates
10
 /// that define its limits.
9
 /// that define its limits.
12
 class Country {
11
 class Country {
13
 private:
12
 private:
14
 public:
13
 public:
15
-    QMap<QString,City*> Cities;
14
+    QMap<QString,QPolygonF*> Cities;
16
     double minX, minY, maxX, maxY;
15
     double minX, minY, maxX, maxY;
17
 
16
 
18
     /// \fn Country()
17
     /// \fn Country()
47
 
46
 
48
 };
47
 };
49
 
48
 
49
+
50
 #endif // COUNTRY_H
50
 #endif // COUNTRY_H

+ 0
- 1
data/cityLimitsPR.json
Diff onderdrukt omdat het te groot bestand
Bestand weergeven


+ 2
- 0
data/pr-all-all.geo.json
Diff onderdrukt omdat het te groot bestand
Bestand weergeven


+ 0
- 14
mainwindow.cpp Bestand weergeven

1
 #include "mainwindow.h"
1
 #include "mainwindow.h"
2
 #include "ui_mainwindow.h"
2
 #include "ui_mainwindow.h"
3
-#include <QPainter>
4
-#include <QPen>
5
-#include <QCoreApplication>
6
-//#include <QJsonDocument>
7
-#include <QFile>
8
 #include <QDebug>
3
 #include <QDebug>
9
-//#include <QJsonArray>
10
-//#include <QJsonObject>
11
-#include <QDesktopWidget>
12
-#include <QVector>
13
-#include <QPoint>
14
-#include <string>
15
-#include <QPolygon>
16
-#include <city.h>
17
 #include <country.h>
4
 #include <country.h>
18
-#include <QPointF>
19
 #include <gispoi.h>
5
 #include <gispoi.h>
20
 
6
 
21
 using namespace std;
7
 using namespace std;

+ 12
- 9
map.cpp Bestand weergeven

10
     srand(time(NULL));
10
     srand(time(NULL));
11
 
11
 
12
     // Create country object and read data from the json file
12
     // Create country object and read data from the json file
13
-    myCountry = new Country;  
14
-    myCountry->readInfoFromJSON(":/data/cityLimitsPR.json");
13
+    qDebug() << "Reading the Json...";
14
+    myCountry = new Country;
15
+    myCountry->readInfoFromJSON(":/data/pr-all-all.geo.json");
16
+    qDebug() << "...done, read " << myCountry->Cities.size();
17
+
15
 
18
 
16
     cityColorMap = NULL;
19
     cityColorMap = NULL;
17
     gisLocations = NULL;
20
     gisLocations = NULL;
86
 
89
 
87
 
90
 
88
     int colorCtr = 0;
91
     int colorCtr = 0;
89
-    QMap<QString,City*>::iterator it;
92
+    QMap<QString,QPolygonF*>::iterator it;
90
     unsigned int randColor;
93
     unsigned int randColor;
91
     int cityCounter = 0;
94
     int cityCounter = 0;
92
 
95
 
93
     for (it = myCountry->Cities.begin() ; it != myCountry->Cities.end(); ++it) {
96
     for (it = myCountry->Cities.begin() ; it != myCountry->Cities.end(); ++it) {
94
-        City *c = it.value();
97
+        QPolygonF *c = it.value();
95
 
98
 
96
         int x1 ,y1, x2, y2;
99
         int x1 ,y1, x2, y2;
97
-        x1 = factorX * (c->getGeometry()->at(0).x() - myCountry->minX);
98
-        y1 = height() - factorY*(c->getGeometry()->at(0).y() - myCountry->minY)  ;
100
+        x1 = factorX * (c->at(0).x() - myCountry->minX);
101
+        y1 = height() - factorY*(c->at(0).y() - myCountry->minY)  ;
99
 
102
 
100
-        QPointF p1 = c->getGeometry()->at(0);
103
+        QPointF p1 = c->at(0);
101
         QPointF p2;
104
         QPointF p2;
102
         int ctr = 0;
105
         int ctr = 0;
103
 
106
 
113
 
116
 
114
 
117
 
115
 
118
 
116
-        for(int i = 0; i < c->getSize() + 1; i++) {
117
-            p2 = c->getGeometry()->at((i+1)%c->getSize());
119
+        for(int i = 0; i < c->size() + 1; i++) {
120
+            p2 = c->at((i+1)%c->size());
118
 
121
 
119
             x2 = factorX * (p2.x() - myCountry->minX);
122
             x2 = factorX * (p2.x() - myCountry->minX);
120
             y2 = height() - factorY*(p2.y() - myCountry->minY)  ;
123
             y2 = height() - factorY*(p2.y() - myCountry->minY)  ;

+ 1
- 3
prMap.pro Bestand weergeven

15
 
15
 
16
 SOURCES += main.cpp\
16
 SOURCES += main.cpp\
17
         mainwindow.cpp \
17
         mainwindow.cpp \
18
-    city.cpp \
19
     country.cpp \
18
     country.cpp \
20
     map.cpp \
19
     map.cpp \
21
     gispoi.cpp
20
     gispoi.cpp
22
 
21
 
23
 HEADERS  += mainwindow.h \
22
 HEADERS  += mainwindow.h \
24
-    city.h \
25
     country.h \
23
     country.h \
26
     map.h \
24
     map.h \
27
     gispoi.h
25
     gispoi.h
31
 RESOURCES += \
29
 RESOURCES += \
32
     prmap.qrc
30
     prmap.qrc
33
 
31
 
34
-DEFINES += QT_NO_DEBUG_OUTPUT
32
+#DEFINES += QT_NO_DEBUG_OUTPUT

+ 1
- 1
prmap.qrc Bestand weergeven

1
 <RCC>
1
 <RCC>
2
     <qresource prefix="/">
2
     <qresource prefix="/">
3
-        <file>data/cityLimitsPR.json</file>
3
+        <file>data/pr-all-all.geo.json</file>
4
     </qresource>
4
     </qresource>
5
 </RCC>
5
 </RCC>