Quellcode durchsuchen

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

Rafael Arce Nazario vor 9 Jahren
Ursprung
Commit
1719d7e13c
10 geänderte Dateien mit 42 neuen und 115 gelöschten Zeilen
  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 Datei anzeigen

@@ -1,6 +0,0 @@
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 Datei anzeigen

@@ -1,58 +0,0 @@
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 Datei anzeigen

@@ -6,21 +6,21 @@
6 6
 #include <QJsonDocument>
7 7
 
8 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 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,22 +33,25 @@ bool Country::readInfoFromJSON(const QString &fileName) {
33 33
     QFile loadFile(fileName);
34 34
 
35 35
     if (!loadFile.open(QIODevice::ReadOnly)) {
36
-        qWarning("Couldn't open save file.");
36
+        qWarning("Couldn't open read file.");
37 37
         return false;
38 38
     }
39 39
 
40
+    qDebug() << "JSON ok...";
41
+
40 42
     QByteArray saveData = loadFile.readAll();
41 43
     QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
42
-    QJsonArray topLevelArray = loadDoc.array();
43 44
 
45
+    QJsonArray featureLevelArray = loadDoc.object()["features"].toArray();
44 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 49
         QJsonArray geometryArray = obj.toObject()["geometry"].toObject()["coordinates"].toArray();
48 50
         QString polyType = obj.toObject()["geometry"].toObject()["type"].toString();
51
+        qDebug() << polyType;
49 52
         if (polyType == "Polygon") {
50 53
             for (int i = 0; i < geometryArray[0].toArray().size(); i++) {
51
-                points->push_back(QPointF(
54
+                poly->push_back(QPointF(
52 55
                                 geometryArray[0].toArray()[i].toArray()[0].toDouble(),
53 56
                                 geometryArray[0].toArray()[i].toArray()[1].toDouble()));
54 57
             }
@@ -58,21 +61,21 @@ bool Country::readInfoFromJSON(const QString &fileName) {
58 61
             for (int i = 0; i <geometryArray.size() ; i++) {
59 62
                 // for every point in the city's polygon
60 63
                 for (int j = 0; j < geometryArray[i].toArray()[0].toArray().size(); j++) {
61
-                    points->push_back(QPointF(
64
+                    poly->push_back(QPointF(
62 65
                                           geometryArray[i].toArray()[0].toArray()[j].toArray()[0].toDouble(),
63 66
                                           geometryArray[i].toArray()[0].toArray()[j].toArray()[1].toDouble()));
64 67
 
65 68
                 }
66 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 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 80
         ctr++;
78 81
     }

+ 3
- 3
country.h Datei anzeigen

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

+ 0
- 1
data/cityLimitsPR.json
Datei-Diff unterdrückt, da er zu groß ist
Datei anzeigen


+ 2
- 0
data/pr-all-all.geo.json
Datei-Diff unterdrückt, da er zu groß ist
Datei anzeigen


+ 0
- 14
mainwindow.cpp Datei anzeigen

@@ -1,21 +1,7 @@
1 1
 #include "mainwindow.h"
2 2
 #include "ui_mainwindow.h"
3
-#include <QPainter>
4
-#include <QPen>
5
-#include <QCoreApplication>
6
-//#include <QJsonDocument>
7
-#include <QFile>
8 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 4
 #include <country.h>
18
-#include <QPointF>
19 5
 #include <gispoi.h>
20 6
 
21 7
 using namespace std;

+ 12
- 9
map.cpp Datei anzeigen

@@ -10,8 +10,11 @@ Map::Map(QWidget *parent) :
10 10
     srand(time(NULL));
11 11
 
12 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 19
     cityColorMap = NULL;
17 20
     gisLocations = NULL;
@@ -86,18 +89,18 @@ void Map::paintEvent(QPaintEvent *) {
86 89
 
87 90
 
88 91
     int colorCtr = 0;
89
-    QMap<QString,City*>::iterator it;
92
+    QMap<QString,QPolygonF*>::iterator it;
90 93
     unsigned int randColor;
91 94
     int cityCounter = 0;
92 95
 
93 96
     for (it = myCountry->Cities.begin() ; it != myCountry->Cities.end(); ++it) {
94
-        City *c = it.value();
97
+        QPolygonF *c = it.value();
95 98
 
96 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 104
         QPointF p2;
102 105
         int ctr = 0;
103 106
 
@@ -113,8 +116,8 @@ void Map::paintEvent(QPaintEvent *) {
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 122
             x2 = factorX * (p2.x() - myCountry->minX);
120 123
             y2 = height() - factorY*(p2.y() - myCountry->minY)  ;

+ 1
- 3
prMap.pro Datei anzeigen

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

+ 1
- 1
prmap.qrc Datei anzeigen

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