Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef CONVERTER
- #define CONVERTER
- class Converter {
- public:
- static double convertDegreeAngleToDouble( double degrees, double minutes, double seconds );
- static double feetToMeters(int feet);
- static double nauticalMilesTOKMeters(int nauticalMiles) {return nauticalMiles*1.852;}
- };
- #endif // CONVERTER
- #ifndef GEOJSONWRITER
- #define GEOJSONWRITER
- #include <string>
- #include <fstream>
- #include <regex>
- #include <vector>
- #include <converter.h>
- #include <math.h> /* cos */
- #include <iomanip>
- #define PI 3.14159265
- #define M_PI 3.1415926535897932384626433832795
- struct Point{
- double x;
- double y;
- Point() {
- }
- Point(double X, double Y) {
- this->x = X;
- this->y = Y;
- }
- bool operator==(const Point& other) const {
- return (this->x == other.x
- && this->y == other.y
- );
- }
- };
- class Polygon {
- public:
- std::vector<Point> points;
- int hMin;
- int hMax;
- Polygon() {
- hMin = 0;
- hMax = 0;
- }
- void addPoint(Point point) {
- points.push_back(point);
- }
- Point getPoint(int index) {
- return points[index];
- }
- size_t size() {
- return points.size();
- }
- void clear() {
- points.clear();
- this->hMax = 0;
- this->hMin = 0;
- }
- void setHmax(double h) {
- this->hMax = h;
- }
- void setHmin(double h) {
- this->hMin = h;
- }
- };
- class GeoJSONWriter {
- private:
- std::ofstream out;
- std::string filenameIn;
- std::string filenameOut;
- 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] "};
- 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] "};
- bool isGND(std::string line);
- bool isGNDandFL(std::string line);
- int getGNDandFLh(std::string line);
- int getGNDwithoutFLh(std::string line);
- int notGNDminH(std::string line);
- int notGNDmaxH(std::string line);
- Point fromNeStringToLngLat(std::string coord);
- double getRadius(std::string coord);
- Point changePointWithMeters(Point p, double km);
- double distance(Point x, Point y);
- double deg2rad(double deg)
- {
- return (deg * M_PI / 180.0);
- };
- Polygon drawCircle(double r, Point p);
- public:
- GeoJSONWriter(std::string filenameIn, std::string filenameOut);
- GeoJSONWriter(std::string filenameOut);
- void writeHeader();
- void writeFooter();
- void readAll();
- void writePoint(Point point);
- void writePolygon(Polygon polygon);
- void writePolygons(std::vector<Polygon> polygons);
- };
- #endif // GEOJSONWRITER
- #include "converter.h"
- double Converter::convertDegreeAngleToDouble(double degrees, double minutes, double seconds) {
- //Decimal degrees =
- // whole number of degrees,
- // plus minutes divided by 60,
- // plus seconds divided by 3600
- return degrees + (minutes/60) + (seconds/3600);
- }
- double Converter::feetToMeters(int feet) {
- return feet * 0.3048;
- }
- #include "geojsonwriter.h"
- #include <iostream>
- GeoJSONWriter::GeoJSONWriter(std::string filenameIn, std::string filenameOut) {
- this->filenameIn = filenameIn;
- this->filenameOut = filenameOut;
- this->writeHeader();
- out.open(filenameOut, std::ios_base::app);
- }
- GeoJSONWriter::GeoJSONWriter(std::string filenameOut) {
- this->filenameOut = filenameOut;
- this->writeHeader();
- out.open(filenameOut, std::ios_base::app);
- }
- void GeoJSONWriter::writeHeader() {
- std::ofstream out(filenameOut);
- out << "{\n\"type\": \"FeatureCollection\",\n\"features\": [\n";
- }
- void GeoJSONWriter::writeFooter() {
- // append instead of overwrite
- out << "\n]\n}";
- }
- void GeoJSONWriter::readAll() {
- std::ifstream in(filenameIn);
- std::string line;
- std::string coord;
- Polygon polygon;
- std::vector<Polygon> polygons;
- bool start = true;
- while(
- std::getline(in, line)){
- //UR
- if (line.size() < 132) {
- continue;
- }
- if (line[2] != 'U' || line[3] != 'R') {
- continue;
- }
- if (line[30] == 'C' && line[31] == 'E') {
- std::cout << "CE" << std::endl;
- }
- if (start == true) {
- if(this->isGND(line)){
- if(this->isGNDandFL(line)) {
- int feet= this->getGNDandFLh(line);
- polygon.setHmax(Converter::feetToMeters(feet));
- }
- else {
- int feet = this->getGNDwithoutFLh(line);
- polygon.setHmax(Converter::feetToMeters(feet));
- }
- }
- else if(line[81] != ' ') {
- int minFeet = this->notGNDminH(line);
- int maxFeet = this->notGNDmaxH(line);
- polygon.setHmin(Converter::feetToMeters(minFeet));
- polygon.setHmax(Converter::feetToMeters(maxFeet));
- }
- }
- coord.clear();
- std::smatch m;
- if(
- std::regex_search (line,m,pointCoordinatesRegex)){
- for(auto x : m) {
- coord+=x;
- }
- std::cout << "Coord = " << coord << std::endl;
- Point p = fromNeStringToLngLat(coord);
- std::cout << p.x << " " << p.y << std::endl;
- polygon.addPoint(p);
- start = false;
- if(line[31] == 'E' && line[30] != 'C') {
- //writePolygon(polygon);
- polygons.push_back(polygon);
- polygon.clear();
- start = true;
- }
- }
- if(std::regex_search(line, m, circleCoordinatesRegex)) {
- for(auto x:m) {
- coord+=x;
- }
- Point p = fromNeStringToLngLat(coord);
- std::cout << p.x << " " << p.y << std::endl;
- // double radius = Converter::feetToMeters(this->getRadius(coord));
- double radius = Converter::nauticalMilesTOKMeters(this->getRadius(coord));
- std::cout << "radius is" << " " << radius;
- Point p2 = this->changePointWithMeters(p,radius);
- std::cout << std::setprecision(17);
- std::cout << std::endl << "point 1 " << p.x << " " << p.y<<std::endl;
- std::cout << "point 2 " << p2.x << " " << p2.y<<std::endl;
- double dist = this->distance(p, p2);
- std::cout << dist;
- Polygon pol =drawCircle(dist, p);
- this->writePolygon(pol);
- }
- // std::cout << coord;
- }
- writePolygons(polygons);
- }
- void GeoJSONWriter::writePoint(Point point) {
- out << "\t[\n\t" << point.x << ",\n\t" << point.y << "\n\t]";
- }
- void GeoJSONWriter::writePolygon(Polygon polygon) {
- out << " {\n \"type\": \"Feature\",\n \"properties\": {";
- out << " \"extrudeMin\" : " << polygon.hMin << ", \"extrudeMax\" : " << polygon.hMax;
- out <<"},\n \"geometry\": {\n \"type\": \"Polygon\",\n \"coordinates\": [ \n [ \n";
- for(size_t i = 0; i < polygon.size(); i++) {
- this->writePoint(polygon.getPoint(i));
- out << ",\n";
- }
- this->writePoint(polygon.getPoint(0));
- out << " \t]\n ]\n }\n \n}";
- }
- Point GeoJSONWriter::fromNeStringToLngLat(std::string coord) {
- std::string aux;
- aux += coord[1];
- aux += coord[2];
- double aDb = stod(aux);
- aux.clear();
- aux += coord[3];
- aux += coord[4];
- double bDb = stod(aux);
- aux.clear();
- aux += coord[5];
- aux += coord[6];
- aux += ".";
- aux += coord[7];
- aux += coord[8];
- double cDb = stod(aux);
- std::cout << std::endl << aDb << " " << bDb <<" " << cDb<< std::endl;
- double lon = Converter::convertDegreeAngleToDouble(aDb, bDb, cDb);
- aux.clear();
- aux+= coord[10]; aux+= coord[11]; aux+= coord[12];
- aDb = stod(aux);
- aux.clear();
- aux+= coord[13]; aux+= coord[14];
- bDb = stod(aux);
- aux.clear();
- aux+= coord[15]; aux+= coord[16]; aux += ".";aux+= coord[17]; aux+= coord[18];
- cDb = stod(aux);
- double lat = Converter::convertDegreeAngleToDouble(aDb, bDb, cDb);
- Point p{lat, lon};
- return p;
- }
- double GeoJSONWriter::getRadius(std::string coord) {
- std::string aux;
- aux += coord[19];
- aux += coord[20];
- aux += coord[21];
- aux += coord[22];
- double radius = stod(aux);
- return radius;
- }
- void GeoJSONWriter::writePolygons(std::vector<Polygon> polygons) {
- out << std::setprecision(17);
- for(size_t i = 0; i < polygons.size(); i++) {
- this->writePolygon(polygons[i]);
- if (i!=polygons.size() - 1) {
- out << ",";
- }
- }
- }
- bool GeoJSONWriter::isGND(std::string line) {
- if (line[81] == 'G' && line[82] == 'N' && line[83] == 'D') {
- //std::cout << line << std::endl;
- return true;
- }
- return false;
- }
- bool GeoJSONWriter::isGNDandFL(std::string line) {
- if (this->isGND(line) && line[87] == 'F' && line[88] == 'L') {
- return true;
- }
- return false;
- }
- int GeoJSONWriter::getGNDandFLh(std::string line) {
- if (this->isGNDandFL(line) == false) {
- return 0;
- }
- std::string number;
- int position = 89;
- while(line[position] != 'M') {
- number += line[position];
- position++;
- }
- int alt = stoi(number) * 100;
- return alt;
- }
- int GeoJSONWriter::getGNDwithoutFLh(std::string line) {
- if (this->isGNDandFL(line) == true) {
- return 0;
- }
- std::string number;
- int position = 87;
- while(line[position] != 'M') {
- number += line[position];
- position++;
- }
- std::cout << number << std::endl;
- int alt = stoi(number);
- return alt;
- }
- int GeoJSONWriter::notGNDminH(std::string line) {
- int multiply = 1;
- std::string number;
- int position = 81;
- if (line[position] == 'F' && line[position + 1] == 'L') {
- multiply = 100;
- position += 2;
- }
- while(line[position] != 'M') {
- number += line[position];
- position++;
- }
- std::cout << number << std::endl;
- int alt = stoi(number);
- return alt * multiply;
- }
- int GeoJSONWriter::notGNDmaxH(std::string line) {
- int position = 81;
- while(line[position] != 'M') {
- position++;
- }
- position++;
- int multiply = 1;
- std::string number;
- if (line[position] == 'F' && line[position + 1] == 'L') {
- multiply = 100;
- position += 2;
- }
- while(line[position] != 'M') {
- number += line[position];
- position++;
- }
- std::cout << number << std::endl;
- int alt = stoi(number);
- return alt * multiply;
- }
- Point GeoJSONWriter::changePointWithMeters(Point p, double km) {
- double R = 6378.1; //#Radius of the Earth
- double brng = 1.57; //#Bearing is 90 degrees converted to radians.
- double d = km; //#Distance in km
- double lat1 = deg2rad(p.x);// #Current lat point converted to radians
- double lon1 = deg2rad(p.y);
- double lat2 = asin(sin(lat1) * cos(d / R) +
- cos(lat1) * sin(d / R) * cos(brng));
- double lon2 = lon1 + atan2(sin(brng) * sin(d / R) * cos(lat1),
- cos(d / R) - sin(lat1) * sin(lat2));
- lat2 = (lat2*180)/M_PI;
- lon2 = (lon2*180)/M_PI;
- std::cout << std::endl << lat2 << " " << lon2 << std::endl;
- Point newPoint{lat2, lon2};
- return newPoint;
- }
- double GeoJSONWriter:: distance(Point x, Point y) {
- return sqrt(pow(y.x - x.x, 2) +
- pow(y.y - x.y, 2) * 1.0);
- }
- Polygon GeoJSONWriter::drawCircle(double r, Point p) {
- Polygon pol;
- double theta = 0;
- double step = 1;
- double x, y;
- std::ofstream outt("cerc.txt");
- for(theta = 0; theta < 360; theta += step) {
- x = p.x + r * cos(PI*theta/180.0);
- y = p.y - r * sin(PI*theta/180.0);
- Point pp{x,y};
- pol.addPoint(pp);
- // outt << std::setprecision(17);
- //outt << "[" << x << ",\n" << y << "],"<<std::endl;
- }
- return pol;
- }
- #include <iostream>
- #include "converter.h"
- #include <fstream>
- #include <string>
- #include <regex>
- #include "geojsonwriter.h"
- int main() {
- GeoJSONWriter writer("pisa.txt", "test.geojson");
- writer.readAll();
- writer.writeFooter();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement