Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
WGS84Ellipsoid.hpp
Go to the documentation of this file.
1#pragma once
2#include <math.h>
4#include <fishnet/Radians.hpp>
5#include <fishnet/Degrees.hpp>
6#include <fishnet/Angle.hpp>
9#include "gdal/ogr_spatialref.h"
10
11namespace fishnet {
12
18private:
19 static double cos2(double angle) {
20 return pow(cos((math::PI / 180) * angle), 2);
21 }
22
23 static double cos2(math::Degrees angle){
24 return pow(angle.cos(), 2);
25 }
26
27 static double sin2(double angle) {
28 return pow(sin((math::PI / 180) * angle), 2);
29 }
30
31 static double sin2(math::Degrees angle){
32 return pow(angle.sin(), 2);
33 }
34
35 static auto projectToMetric(fishnet::geometry::Shape auto const & shape) {
36 using T = std::remove_cvref_t<decltype(shape)>;
37 auto centroid = shape.centroid();
38 OGRSpatialReference metric;
39 metric.SetAE(centroid.y, centroid.x, 0, 0);
40 metric.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
42 OGRCoordinateTransformation * toMetric = OGRCreateCoordinateTransformation(&spatialReference, &metric);
43 if(toMetric == nullptr || asOGR.raw()->transform(toMetric) != OGRERR_NONE)
44 throw std::runtime_error("Could not transform geometry to metric");
45 OCTDestroyCoordinateTransformation(OGRCoordinateTransformation::ToHandle(toMetric));
46 return std::move(asOGR).narrowTo<T>().value_or_throw();
47 }
48
49 static inline OGRSpatialReference initWGS84(){
50 OGRSpatialReference wgs84 = OGRSpatialReference();
51 wgs84.importFromEPSG(4326);
52 wgs84.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
53 return wgs84;
54 }
55
56public:
57 constexpr static double flattening = 1.0 / 298.257223563; // Flattening of the Earth: https://en.wikipedia.org/wiki/Earth_ellipsoid
58 constexpr static double radiusInMeter = 6378137; //Earth radius in m;
59 static inline OGRSpatialReference spatialReference = initWGS84();
60
71 static double distance(double lambdaA, double phiA, double lambdaB, double phiB, bool exact = true) {
72 math::Degrees F = math::Degrees((phiA + phiB) / 2);
73 math::Degrees G = math::Degrees((phiA - phiB) / 2);
74 math::Degrees l = math::Degrees((lambdaA - lambdaB) / 2);
75 double S = sin2(G) * cos2(l) + cos2(F) * sin2(l);
76 double C = cos2(G) * cos2(l) + sin2(F) * sin2(l);
77 math::Radians w = math::Radians::atan(sqrt(S / C));
78 double distance = 2 * w.getAngleValue() * radiusInMeter;
79 if (not exact) {
80 return distance;
81 } else {
82 double T = sqrt(S * C) / w.getAngleValue();
83 double h1 = (3 * T - 1) / (2 * C);
84 double h2 = (3 * T + 1) / (2 * S);
85 double correction = (1 + flattening * h1 * sin2(F) * cos2(G) -
86 flattening * h2 * cos2(F) * sin2(G));
87 return distance * correction;
88 }
89 }
90
99 static double distance(fishnet::geometry::IPoint auto const & p, fishnet::geometry::IPoint auto const & q,bool exact = true)noexcept{
100 return distance(p.x, p.y, q.x, q.y, exact);
101 }
102
110 static double area(fishnet::geometry::Shape auto const & shape){
111 return projectToMetric(shape).area();
112 }
113};
114}
Utility class for computing distances and areas in metric units for GIS data using the WGS84 Spatial ...
Definition WGS84Ellipsoid.hpp:17
static OGRSpatialReference spatialReference
Definition WGS84Ellipsoid.hpp:59
static double area(fishnet::geometry::Shape auto const &shape)
Definition WGS84Ellipsoid.hpp:110
static constexpr double radiusInMeter
Definition WGS84Ellipsoid.hpp:58
static double distance(double lambdaA, double phiA, double lambdaB, double phiB, bool exact=true)
Definition WGS84Ellipsoid.hpp:71
static double distance(fishnet::geometry::IPoint auto const &p, fishnet::geometry::IPoint auto const &q, bool exact=true) noexcept
Compute the distance between two points in metric units.
Definition WGS84Ellipsoid.hpp:99
static constexpr double flattening
Definition WGS84Ellipsoid.hpp:57
T * raw() const noexcept
Definition OGRAdapterBase.hpp:115
Adapter for an OGRGeometry of statically unknown type, as handed out by a data source.
Definition OGRGeometryAdapter.hpp:83
Degrees implementation of Angle.
Definition Degrees.hpp:11
double sin() const
Definition Degrees.cpp:22
double cos() const
Definition Degrees.cpp:26
Radians implementation of Angle.
Definition Radians.hpp:11
static Radians atan(double tangent)
Definition Radians.cpp:42
Interface for a point.
Definition IPoint.hpp:92
A geometry enclosing an area: a ring, a polygon or a multi-polygon.
Definition IGeometry.hpp:23
static constexpr double PI
Definition Constants.hpp:6
Definition BFSClustering.hpp:5