GeoUtilities.h 2.53 KB
Newer Older
1
#pragma once
2 3 4 5 6

#include <QPointF>
#include <QGeoCoordinate>
#include <QVector3D>
#include <QGeoCoordinate>
7
#include <QtMath>
8

9
#include "PolygonCalculus.h"
10 11


12 13
namespace GeoUtilities {

14 15
    typedef QList<QVector3D>        QVector3DList;
    typedef QList<QPointF>          QPointFList;
16
    typedef QVector<QPointF>        QPointFVector;
17
    typedef QList<QGeoCoordinate>   QGeoList;
18
    typedef QVector<QGeoCoordinate> QGeoVector;
19

Valentin Platzgummer's avatar
Valentin Platzgummer committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    const double earthRadius = 6378137; // meter    

    template <class GeoPoint1, class GeoPoint2, class Point2D>
    void toGeo(const Point2D &in, const GeoPoint1 &origin, GeoPoint2 &out){
        double x = in.x();
        double y = in.y();

        double lat = origin.latitude();
        double lon = origin.longitude();

        //qWarning("%lf %lf %lf %lf", x, y, lat, lon);

        // this could (unlikely) be a problem, replace with different coordinate transformation
        if (!qFuzzyCompare(lat, M_PI_2)) {
            out.setLatitude(qAtan(y/earthRadius)*180/M_PI + lat);
            out.setLongitude(qAtan(x/cos(lat/180*M_PI)/earthRadius)*180/M_PI + lon);
            out.setAltitude(origin.altitude());
        }
        return;
    }

    template <class GeoPointList, class GeoPoint, class Point2DList>
    void toGeoList(const Point2DList &in, const GeoPoint &origin, GeoPointList &out){
        out.reserve(in.size());

        typedef typename GeoPointList::value_type GeoPoint2;
        GeoPoint2 p;
        for ( auto c : in) {
            toGeo(c, origin, p);
            out.append(p);
        }
    }

    template <class GeoPoint1, class GeoPoint2, class Point2D>
    void toCartesian(const GeoPoint1 &in, const GeoPoint2 &origin, Point2D &out)
    {
        double lat = in.latitude()/180*M_PI;
        double lon = in.longitude()/180*M_PI;

        double latO = origin.latitude()/180*M_PI;
        double lonO = origin.longitude()/180*M_PI;

        double dlon = lon-lonO;
        double dlat = lat-latO;


        if (!qFuzzyCompare(dlon, M_PI_2) && !qFuzzyCompare(dlat, M_PI_2))  {
            out.setX(qTan(dlon)*earthRadius*qCos(latO));
            out.setY(qTan(dlat)*earthRadius);

        }

        return;
    }

    template <class GeoPointList, class GeoPoint, class Point2DList>
    void toCartesianList(const GeoPointList &in, const GeoPoint &origin, Point2DList &out){
        out.reserve(in.size());

        typedef typename Point2DList::value_type Point2D;
        Point2D p;
        for ( auto c : in) {
            toCartesian(c, origin, p);
            out.append(p);
        }

        return;
    }
88
}
89