Nessuna descrizione

country.cpp 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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,QPolygonF*>::iterator it = Cities.begin();
  9. minX = maxX = Cities.begin().value()->at(0).x();
  10. minY = maxY = Cities.begin().value()->at(0).y();
  11. for (it = Cities.begin() + 1; it != Cities.end(); ++it) {
  12. QPolygonF *c = it.value();
  13. for (int i = 0; i < c->size(); i++) {
  14. if (c->at(i).x() != 0.0) {
  15. if (c->at(i).x() > maxX ) maxX = c->at(i).x();
  16. if (c->at(i).y() > maxY ) maxY = c->at(i).y();
  17. if (c->at(i).x() < minX ) minX = c->at(i).x();
  18. if (c->at(i).y() < minY ) minY = c->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 read file.");
  29. return false;
  30. }
  31. qDebug() << "JSON ok...";
  32. QByteArray saveData = loadFile.readAll();
  33. QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
  34. QJsonArray featureLevelArray = loadDoc.object()["pueblos"].toObject()["features"].toArray();
  35. int ctr = 1;
  36. foreach(QJsonValue obj, featureLevelArray) {
  37. QPolygonF *poly = new QPolygonF;
  38. QJsonArray geometryArray = obj.toObject()["geometry"].toObject()["coordinates"].toArray();
  39. QString polyType = obj.toObject()["geometry"].toObject()["type"].toString();
  40. qDebug() << polyType;
  41. if (polyType == "Polygon") {
  42. for (int i = 0; i < geometryArray[0].toArray().size(); i++) {
  43. poly->push_back(QPointF(
  44. geometryArray[0].toArray()[i].toArray()[0].toDouble(),
  45. geometryArray[0].toArray()[i].toArray()[1].toDouble()));
  46. qDebug() << geometryArray[0].toArray()[i].toArray()[0].toDouble();
  47. }
  48. }
  49. else {
  50. // for every city
  51. for (int i = 0; i <geometryArray.size() ; i++) {
  52. // for every point in the city's polygon
  53. for (int j = 0; j < geometryArray[i].toArray()[0].toArray().size(); j++) {
  54. poly->push_back(QPointF(
  55. geometryArray[i].toArray()[0].toArray()[j].toArray()[0].toDouble(),
  56. geometryArray[i].toArray()[0].toArray()[j].toArray()[1].toDouble()));
  57. }
  58. // I will push a 0,0 between each polygon of a multipolygon to distinguish
  59. poly->push_back(QPointF(0,0));
  60. }
  61. }
  62. QString cityName = obj.toObject()["properties"].toObject()["NAME"].toString();
  63. qDebug() << "Storing: " << cityName;
  64. Cities[cityName] = poly;
  65. ctr++;
  66. }
  67. return true;
  68. }