No Description

country.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. void Country::topLeft(QString &st, DoublePoint &p ) {
  25. QMap<QString,City*>::iterator it = Cities.begin();
  26. st = it.key();
  27. p.x = it.value()->getGeometry()->at(0).x;
  28. p.y = it.value()->getGeometry()->at(0).y;
  29. qDebug() << p.x << ". " << p.y;
  30. for (it = Cities.begin(); it != Cities.end(); ++it) {
  31. QVector<DoublePoint> *geometry = it.value()->getGeometry();
  32. for (int i = 0; i < geometry->size(); i++) {
  33. if (geometry->at(i).x > p.x && geometry->at(i).y < p.y) {
  34. p.x = geometry->at(i).x;
  35. p.y = geometry->at(i).y;
  36. st = it.key();
  37. }
  38. }
  39. }
  40. }
  41. unsigned long *colorMap;
  42. bool Country::readInfoFromJSON(QString fileName) {
  43. QFile loadFile(fileName);
  44. if (!loadFile.open(QIODevice::ReadOnly)) {
  45. qWarning("Couldn't open save file.");
  46. return false;
  47. }
  48. QByteArray saveData = loadFile.readAll();
  49. QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
  50. QJsonArray topLevelArray = loadDoc.array();
  51. int ctr = 1;
  52. foreach(QJsonValue obj, topLevelArray) {
  53. QVector<DoublePoint> *points = new QVector<DoublePoint>;
  54. QJsonArray geometryArray = obj.toObject()["geometry"].toObject()["coordinates"].toArray();
  55. QString polyType = obj.toObject()["geometry"].toObject()["type"].toString();
  56. if (polyType == "Polygon") {
  57. for (int i = 0; i < geometryArray[0].toArray().size(); i++) {
  58. points->push_back(DoublePoint(
  59. geometryArray[0].toArray()[i].toArray()[0].toDouble(),
  60. geometryArray[0].toArray()[i].toArray()[1].toDouble()));
  61. }
  62. }
  63. else {
  64. for (int i = 0; i <geometryArray.size() ; i++) {
  65. for (int j = 0; j < geometryArray[i].toArray()[0].toArray().size(); j++) {
  66. points->push_back(DoublePoint(
  67. geometryArray[i].toArray()[0].toArray()[j].toArray()[0].toDouble(),
  68. geometryArray[i].toArray()[0].toArray()[j].toArray()[1].toDouble()));
  69. }
  70. // I will push a 0,0 between each polygon of a multipolygon to distinguish
  71. points->push_back(DoublePoint(0,0));
  72. }
  73. }
  74. QString cityName = obj.toObject()["properties"].toObject()["city"].toString();
  75. qDebug() << "Storing: " << cityName;
  76. Cities[cityName] = new City;
  77. Cities[cityName]->geometry = points;
  78. ctr++;
  79. }
  80. }