Geen omschrijving

country.cpp 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "country.h"
  2. #include <QFile>
  3. #include <QDebug>
  4. #include <QJsonArray>
  5. #include <QJsonObject>
  6. #include <QJsonDocument>
  7. void Country::limits(){
  8. QMap<QString,City*>::iterator it = Cities.begin();
  9. minX = maxX = Cities.begin().value()->geometry->at(0).x;
  10. minY = maxY = Cities.begin().value()->geometry->at(0).y;
  11. for (it = Cities.begin() + 1; it != Cities.end(); ++it) {
  12. City *c = it.value();
  13. for (int i = 0; i < c->getSize(); i++) {
  14. if (c->geometry->at(i).x != 0.0) {
  15. if (c->geometry->at(i).x > maxX ) maxX = c->geometry->at(i).x;
  16. if (c->geometry->at(i).y > maxY ) maxY = c->geometry->at(i).y;
  17. if (c->geometry->at(i).x < minX ) minX = c->geometry->at(i).x;
  18. if (c->geometry->at(i).y < minY ) minY = c->geometry->at(i).y;
  19. }
  20. }
  21. }
  22. qDebug() << minX << minY << maxX << maxY ;
  23. }
  24. unsigned long *colorMap;
  25. bool Country::readInfoFromJSON(const QString &fileName) {
  26. QFile loadFile(fileName);
  27. if (!loadFile.open(QIODevice::ReadOnly)) {
  28. qWarning("Couldn't open save file.");
  29. return false;
  30. }
  31. QByteArray saveData = loadFile.readAll();
  32. QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
  33. QJsonArray topLevelArray = loadDoc.array();
  34. int ctr = 1;
  35. foreach(QJsonValue obj, topLevelArray) {
  36. QVector<DoublePoint> *points = new QVector<DoublePoint>;
  37. QJsonArray geometryArray = obj.toObject()["geometry"].toObject()["coordinates"].toArray();
  38. QString polyType = obj.toObject()["geometry"].toObject()["type"].toString();
  39. if (polyType == "Polygon") {
  40. for (int i = 0; i < geometryArray[0].toArray().size(); i++) {
  41. points->push_back(DoublePoint(
  42. geometryArray[0].toArray()[i].toArray()[0].toDouble(),
  43. geometryArray[0].toArray()[i].toArray()[1].toDouble()));
  44. }
  45. }
  46. else {
  47. // for every city
  48. for (int i = 0; i <geometryArray.size() ; i++) {
  49. // for every point in the city's polygon
  50. for (int j = 0; j < geometryArray[i].toArray()[0].toArray().size(); j++) {
  51. points->push_back(DoublePoint(
  52. geometryArray[i].toArray()[0].toArray()[j].toArray()[0].toDouble(),
  53. geometryArray[i].toArray()[0].toArray()[j].toArray()[1].toDouble()));
  54. }
  55. // I will push a 0,0 between each polygon of a multipolygon to distinguish
  56. points->push_back(DoublePoint(0,0));
  57. }
  58. }
  59. QString cityName = obj.toObject()["properties"].toObject()["city"].toString();
  60. qDebug() << "Storing: " << cityName;
  61. Cities[cityName] = new City;
  62. Cities[cityName]->geometry = points;
  63. ctr++;
  64. }
  65. return true;
  66. }