Advertisement
Guest User

munca

a guest
Mar 29th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.07 KB | None | 0 0
  1. #ifndef CONVERTER
  2. #define CONVERTER
  3.  
  4. class Converter {
  5. public:
  6.     static double convertDegreeAngleToDouble( double degrees, double minutes, double seconds );
  7.     static double feetToMeters(int feet);
  8.     static double nauticalMilesTOKMeters(int nauticalMiles) {return nauticalMiles*1.852;}
  9.  
  10. };
  11.  
  12. #endif // CONVERTER
  13.  
  14. #ifndef GEOJSONWRITER
  15. #define GEOJSONWRITER
  16.  
  17. #include <string>
  18. #include <fstream>
  19. #include <regex>
  20. #include <vector>
  21. #include <converter.h>
  22. #include <math.h>       /* cos */
  23. #include <iomanip>
  24. #define PI 3.14159265
  25. #define M_PI 3.1415926535897932384626433832795
  26. struct Point{
  27.     double x;
  28.     double y;
  29.     Point() {
  30.  
  31.     }
  32.  
  33.     Point(double X, double Y) {
  34.         this->x = X;
  35.         this->y = Y;
  36.     }
  37.  
  38.     bool operator==(const Point& other) const {
  39.         return (this->x == other.x
  40.                 && this->y == other.y
  41.                 );
  42.     }
  43. };
  44.  
  45. class Polygon {
  46. public:
  47.  
  48.     std::vector<Point> points;
  49.     int hMin;
  50.     int hMax;
  51.  
  52.     Polygon() {
  53.         hMin = 0;
  54.         hMax = 0;
  55.     }
  56.  
  57.     void addPoint(Point point) {
  58.         points.push_back(point);
  59.     }
  60.     Point getPoint(int index) {
  61.         return points[index];
  62.     }
  63.  
  64.     size_t size() {
  65.         return points.size();
  66.     }
  67.  
  68.     void clear() {
  69.         points.clear();
  70.         this->hMax = 0;
  71.         this->hMin = 0;
  72.     }
  73.  
  74.     void setHmax(double h) {
  75.         this->hMax = h;
  76.     }
  77.  
  78.     void setHmin(double h) {
  79.         this->hMin = h;
  80.     }
  81.  
  82. };
  83.  
  84.  
  85. class GeoJSONWriter {
  86. private:
  87.  
  88.     std::ofstream out;
  89.     std::string filenameIn;
  90.     std::string filenameOut;
  91.     std::regex pointCoordinatesRegex{"N[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]E[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] "};
  92.     std::regex circleCoordinatesRegex{"N[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]E[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] "};
  93.     bool isGND(std::string line);
  94.     bool isGNDandFL(std::string line);
  95.     int getGNDandFLh(std::string line);
  96.     int getGNDwithoutFLh(std::string line);
  97.     int notGNDminH(std::string line);
  98.     int notGNDmaxH(std::string line);
  99.     Point fromNeStringToLngLat(std::string coord);
  100.     double getRadius(std::string coord);
  101.     Point changePointWithMeters(Point p, double km);
  102.     double distance(Point x, Point y);
  103.     double deg2rad(double deg)
  104.     {
  105.         return (deg * M_PI / 180.0);
  106.     };
  107.     Polygon drawCircle(double r, Point p);
  108.  
  109. public:
  110.     GeoJSONWriter(std::string filenameIn, std::string filenameOut);
  111.     GeoJSONWriter(std::string filenameOut);
  112.     void writeHeader();
  113.     void writeFooter();
  114.     void readAll();
  115.     void writePoint(Point point);
  116.     void writePolygon(Polygon polygon);
  117.     void writePolygons(std::vector<Polygon> polygons);
  118. };
  119.  
  120.  
  121. #endif // GEOJSONWRITER
  122.  
  123. #include "converter.h"
  124.  
  125. double Converter::convertDegreeAngleToDouble(double degrees, double minutes, double seconds) {
  126.     //Decimal degrees =
  127.     //   whole number of degrees,
  128.     //   plus minutes divided by 60,
  129.     //   plus seconds divided by 3600
  130.     return degrees + (minutes/60) + (seconds/3600);
  131. }
  132.  
  133. double Converter::feetToMeters(int feet) {
  134.     return feet * 0.3048;
  135. }
  136. #include "geojsonwriter.h"
  137. #include <iostream>
  138.  
  139. GeoJSONWriter::GeoJSONWriter(std::string filenameIn, std::string filenameOut) {
  140.     this->filenameIn = filenameIn;
  141.     this->filenameOut = filenameOut;
  142.     this->writeHeader();
  143.     out.open(filenameOut, std::ios_base::app);
  144. }
  145.  
  146. GeoJSONWriter::GeoJSONWriter(std::string filenameOut) {
  147.     this->filenameOut = filenameOut;
  148.     this->writeHeader();
  149.     out.open(filenameOut, std::ios_base::app);
  150. }
  151.  
  152. void GeoJSONWriter::writeHeader() {
  153.     std::ofstream out(filenameOut);
  154.     out << "{\n\"type\": \"FeatureCollection\",\n\"features\": [\n";
  155. }
  156.  
  157. void GeoJSONWriter::writeFooter() {
  158.      // append instead of overwrite
  159.     out << "\n]\n}";
  160. }
  161.  
  162. void GeoJSONWriter::readAll() {
  163.  
  164.     std::ifstream in(filenameIn);
  165.     std::string line;
  166.     std::string coord;
  167.     Polygon polygon;
  168.     std::vector<Polygon> polygons;
  169.     bool start = true;
  170.     while(
  171.     std::getline(in, line)){
  172.         //UR
  173.     if (line.size() < 132) {
  174.         continue;
  175.     }
  176.     if (line[2] != 'U' || line[3] != 'R') {
  177.         continue;
  178.     }
  179.     if (line[30] == 'C' && line[31] == 'E') {
  180.         std::cout << "CE" << std::endl;
  181.     }
  182.  
  183.     if (start == true) {
  184.     if(this->isGND(line)){
  185.         if(this->isGNDandFL(line)) {
  186.             int feet= this->getGNDandFLh(line);
  187.             polygon.setHmax(Converter::feetToMeters(feet));
  188.         }
  189.         else {
  190.             int feet = this->getGNDwithoutFLh(line);
  191.             polygon.setHmax(Converter::feetToMeters(feet));
  192.         }
  193.     }
  194.     else if(line[81] != ' ') {
  195.         int minFeet = this->notGNDminH(line);
  196.         int maxFeet = this->notGNDmaxH(line);
  197.         polygon.setHmin(Converter::feetToMeters(minFeet));
  198.         polygon.setHmax(Converter::feetToMeters(maxFeet));
  199.     }
  200. }
  201.  
  202.     coord.clear();
  203.     std::smatch m;
  204.     if(
  205.     std::regex_search (line,m,pointCoordinatesRegex)){
  206.     for(auto x : m) {
  207.        coord+=x;
  208.     }
  209.     std::cout <<  "Coord = " << coord << std::endl;
  210.    Point p = fromNeStringToLngLat(coord);
  211.    std::cout << p.x << " " << p.y << std::endl;
  212.    polygon.addPoint(p);
  213.    start = false;
  214.     if(line[31] == 'E' && line[30] != 'C') {
  215.         //writePolygon(polygon);
  216.         polygons.push_back(polygon);
  217.         polygon.clear();
  218.         start = true;
  219.     }
  220.     }
  221.  
  222.     if(std::regex_search(line, m, circleCoordinatesRegex)) {
  223.         for(auto x:m) {
  224.             coord+=x;
  225.         }
  226.         Point p = fromNeStringToLngLat(coord);
  227.         std::cout << p.x << " " << p.y << std::endl;
  228.  
  229.        // double radius = Converter::feetToMeters(this->getRadius(coord));
  230.         double radius = Converter::nauticalMilesTOKMeters(this->getRadius(coord));
  231.         std::cout << "radius is" << " " << radius;
  232.         Point p2 = this->changePointWithMeters(p,radius);
  233.           std::cout << std::setprecision(17);
  234.         std::cout << std::endl << "point 1 " << p.x << " " << p.y<<std::endl;
  235.         std::cout << "point 2 " << p2.x << " " << p2.y<<std::endl;
  236.         double dist = this->distance(p, p2);
  237.         std::cout << dist;
  238.          Polygon pol  =drawCircle(dist, p);
  239.         this->writePolygon(pol);
  240.     }
  241.  
  242.    // std::cout << coord;
  243.     }
  244.      writePolygons(polygons);
  245. }
  246.  
  247.  
  248. void GeoJSONWriter::writePoint(Point point) {
  249.     out << "\t[\n\t" << point.x << ",\n\t" << point.y << "\n\t]";
  250. }
  251.  
  252. void GeoJSONWriter::writePolygon(Polygon polygon) {
  253.     out << " {\n \"type\": \"Feature\",\n \"properties\": {";
  254.     out << " \"extrudeMin\" : " << polygon.hMin << ", \"extrudeMax\" : " << polygon.hMax;
  255.  
  256.     out <<"},\n \"geometry\": {\n \"type\": \"Polygon\",\n \"coordinates\": [ \n  [ \n";
  257.     for(size_t i = 0; i < polygon.size(); i++) {
  258.         this->writePoint(polygon.getPoint(i));
  259.         out << ",\n";
  260.  
  261.     }
  262.     this->writePoint(polygon.getPoint(0));
  263.     out << " \t]\n ]\n }\n \n}";
  264. }
  265.  
  266.  
  267. Point GeoJSONWriter::fromNeStringToLngLat(std::string coord) {
  268.     std::string aux;
  269.     aux += coord[1];
  270.     aux += coord[2];
  271.     double aDb = stod(aux);
  272.     aux.clear();
  273.     aux += coord[3];
  274.     aux += coord[4];
  275.     double bDb = stod(aux);
  276.     aux.clear();
  277.     aux += coord[5];
  278.     aux += coord[6];
  279.     aux += ".";
  280.     aux += coord[7];
  281.     aux += coord[8];
  282.     double cDb = stod(aux);
  283.     std::cout << std::endl << aDb << " " << bDb <<" " <<  cDb<< std::endl;
  284.     double lon = Converter::convertDegreeAngleToDouble(aDb, bDb, cDb);
  285.     aux.clear();
  286.     aux+= coord[10]; aux+= coord[11]; aux+= coord[12];
  287.     aDb = stod(aux);
  288.     aux.clear();
  289.     aux+= coord[13]; aux+= coord[14];
  290.     bDb = stod(aux);
  291.     aux.clear();
  292.     aux+= coord[15]; aux+= coord[16]; aux += ".";aux+= coord[17];  aux+= coord[18];
  293.     cDb = stod(aux);
  294.     double lat = Converter::convertDegreeAngleToDouble(aDb, bDb, cDb);
  295.     Point p{lat, lon};
  296.     return p;
  297. }
  298.  
  299. double GeoJSONWriter::getRadius(std::string coord) {
  300.   std::string aux;
  301.   aux += coord[19];
  302.   aux += coord[20];
  303.   aux += coord[21];
  304.   aux += coord[22];
  305.   double radius = stod(aux);
  306.   return radius;
  307.  
  308. }
  309.  
  310.  
  311.  
  312.  void GeoJSONWriter::writePolygons(std::vector<Polygon> polygons) {
  313.      out << std::setprecision(17);
  314.         for(size_t i = 0; i < polygons.size(); i++) {
  315.             this->writePolygon(polygons[i]);
  316.             if (i!=polygons.size() - 1) {
  317.                 out << ",";
  318.             }
  319.         }
  320.  }
  321.  
  322.  bool GeoJSONWriter::isGND(std::string line) {
  323.      if (line[81] == 'G' && line[82] == 'N' && line[83] == 'D') {
  324.          //std::cout << line << std::endl;
  325.          return true;
  326.      }
  327.      return false;
  328.  }
  329.  
  330.  bool GeoJSONWriter::isGNDandFL(std::string line) {
  331.      if (this->isGND(line) && line[87] == 'F' && line[88] == 'L') {
  332.          return true;
  333.      }
  334.      return false;
  335.  }
  336.  
  337. int GeoJSONWriter::getGNDandFLh(std::string line) {
  338.     if (this->isGNDandFL(line) == false) {
  339.         return 0;
  340.     }
  341.     std::string number;
  342.     int position = 89;
  343.     while(line[position] != 'M') {
  344.         number += line[position];
  345.         position++;
  346.     }
  347.     int alt = stoi(number) * 100;
  348.     return alt;
  349. }
  350.  
  351.  
  352. int GeoJSONWriter::getGNDwithoutFLh(std::string line) {
  353.     if (this->isGNDandFL(line) == true) {
  354.         return 0;
  355.     }
  356.     std::string number;
  357.     int position = 87;
  358.     while(line[position] != 'M') {
  359.         number += line[position];
  360.         position++;
  361.     }
  362.     std::cout << number << std::endl;
  363.     int alt = stoi(number);
  364.     return alt;
  365. }
  366.  
  367. int GeoJSONWriter::notGNDminH(std::string line) {
  368.     int multiply = 1;
  369.     std::string number;
  370.     int position = 81;
  371.     if (line[position] == 'F' && line[position + 1] == 'L') {
  372.         multiply = 100;
  373.         position += 2;
  374.     }
  375.     while(line[position] != 'M') {
  376.         number += line[position];
  377.         position++;
  378.     }
  379.     std::cout << number << std::endl;
  380.     int alt = stoi(number);
  381.     return alt * multiply;
  382. }
  383.  
  384. int GeoJSONWriter::notGNDmaxH(std::string line) {
  385.     int position = 81;
  386.     while(line[position] != 'M') {
  387.         position++;
  388.     }
  389.     position++;
  390.     int multiply = 1;
  391.     std::string number;
  392.     if (line[position] == 'F' && line[position + 1] == 'L') {
  393.         multiply = 100;
  394.         position += 2;
  395.     }
  396.     while(line[position] != 'M') {
  397.         number += line[position];
  398.         position++;
  399.     }
  400.     std::cout << number << std::endl;
  401.     int alt = stoi(number);
  402.     return alt * multiply;
  403. }
  404.  
  405. Point GeoJSONWriter::changePointWithMeters(Point p, double km) {
  406.       double R = 6378.1; //#Radius of the Earth
  407.        double brng = 1.57; //#Bearing is 90 degrees converted to radians.
  408.        double d = km; //#Distance in km
  409.  
  410.        double  lat1 = deg2rad(p.x);// #Current lat point converted to radians
  411.           double   lon1 = deg2rad(p.y);
  412.  
  413.        double lat2 = asin(sin(lat1) * cos(d / R) +
  414.            cos(lat1) * sin(d / R) * cos(brng));
  415.  
  416.        double lon2 = lon1 + atan2(sin(brng) * sin(d / R) * cos(lat1),
  417.            cos(d / R) - sin(lat1) * sin(lat2));
  418.  
  419.        lat2 = (lat2*180)/M_PI;
  420.        lon2 = (lon2*180)/M_PI;
  421.  
  422.        std::cout << std::endl << lat2 << " " << lon2 << std::endl;
  423.  
  424.        Point newPoint{lat2, lon2};
  425.        return newPoint;
  426. }
  427.  
  428. double GeoJSONWriter:: distance(Point x, Point y) {
  429.     return sqrt(pow(y.x - x.x, 2) +
  430.          pow(y.y - x.y, 2) * 1.0);
  431. }
  432.  
  433.  
  434. Polygon GeoJSONWriter::drawCircle(double r, Point p) {
  435.     Polygon pol;
  436.     double theta = 0;
  437.     double step = 1;
  438.     double x, y;
  439.     std::ofstream outt("cerc.txt");
  440.  
  441.     for(theta = 0; theta < 360; theta += step) {
  442.         x = p.x + r * cos(PI*theta/180.0);
  443.         y = p.y - r * sin(PI*theta/180.0);
  444.         Point pp{x,y};
  445.         pol.addPoint(pp);
  446.         // outt << std::setprecision(17);
  447.         //outt << "[" << x << ",\n" << y << "],"<<std::endl;
  448.     }
  449.     return pol;
  450. }
  451. #include <iostream>
  452. #include "converter.h"
  453. #include <fstream>
  454. #include <string>
  455. #include <regex>
  456. #include "geojsonwriter.h"
  457.  
  458. int main() {
  459.  
  460.     GeoJSONWriter writer("pisa.txt", "test.geojson");
  461.     writer.readAll();
  462.     writer.writeFooter();
  463.     return 0;
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement