Nenhuma descrição

country.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 (int i = 0; i <geometryArray.size() ; i++) {
  48. for (int j = 0; j < geometryArray[i].toArray()[0].toArray().size(); j++) {
  49. points->push_back(DoublePoint(
  50. geometryArray[i].toArray()[0].toArray()[j].toArray()[0].toDouble(),
  51. geometryArray[i].toArray()[0].toArray()[j].toArray()[1].toDouble()));
  52. }
  53. // I will push a 0,0 between each polygon of a multipolygon to distinguish
  54. points->push_back(DoublePoint(0,0));
  55. }
  56. }
  57. QString cityName = obj.toObject()["properties"].toObject()["city"].toString();
  58. qDebug() << "Storing: " << cityName;
  59. Cities[cityName] = new City;
  60. Cities[cityName]->geometry = points;
  61. ctr++;
  62. }
  63. }