Skip to content

gistrec/cpp-geometry-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Geometry Library

Build status Code quality Release Supported platforms License

C++ Geometry Library provides utility functions for the computation of geometric data on the surface of the Earth. Code ported from Google Maps Android API.

Features

  • Spherical contains spherical geometry utilities allowing you to compute angles, distances and areas from latitudes and longitudes.
  • Poly utility functions for computations involving polygons and polylines.

Usage

You just need to include SphericalUtil.hpp or PolyUtil.hpp

Here is an example of using this library:

#include <iostream>
#include <vector>

#include "SphericalUtil.hpp"

int main() {
    LatLng up    = { 90.0,    0.0 };
    LatLng down  = {-90.0,    0.0 };
    LatLng front = {  0.0,    0.0 };
    LatLng right = {  0.0,   90.0 };

    auto angle = SphericalUtil::computeAngleBetween(up, right); // 90
    std::cout << "The angle between up and right is " << rad2deg(angle) << " degrees" << std::endl;

    auto distance = SphericalUtil::computeDistanceBetween(up, down); // 2.00151e+07
    std::cout << "The distance between up and down is " << distance << " meters" << std::endl;

    std::vector<LatLng> points = { front, up, right };

    auto length = SphericalUtil::computeLength(points); // 2.00151e+07
    std::cout << "The length between front, up and right is " << length << " meters" << std::endl;

    auto area = SphericalUtil::computeArea(points); // 6.37582e+13
    std::cout << "The area between front, up and right is " << area << " square meters" << std::endl;

    return 0;
}

Available methods

PolyUtil class

SphericalUtil class

Classes description

LatLng - a point in geographical coordinates: latitude and longitude.

  • Latitude ranges between -90 and 90 degrees, inclusive
  • Longitude ranges between -180 and 180 degrees, inclusive

Usage example:

LatLng northPole = {90, 0};

LatLng otherPoint = northPole;

LatLngList - a series of connected coordinates in an ordered sequence. Any iterable containers.

Usage example:

std::vector<LatLng> aroundNorthPole = { {89, 0}, {89, 120}, {89, -120} };

std::array<LatLng, 1U> northPole = { {90, 0} };

Functions description

PolyUtil functions

PolyUtil::containsLocation(const LatLng& point, const LatLngList& polygon, bool geodesic = false) - Computes whether the given point lies inside the specified polygon

  • point - a point in geographical coordinates: latitude and longitude
  • polygon - a series of connected coordinates in an ordered sequence
  • geodesic - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise

Return value: bool - whether the given point lies inside the specified polygon

// Around the north pole.
std::vector<LatLng> aroundNorthPole = { {89, 0}, {89, 120}, {89, -120} };

std::cout << PolyUtil::containsLocation(LatLng(90, 0), aroundNorthPole);  // true
std::cout << PolyUtil::containsLocation(LatLng(-90, 0), aroundNorthPole); // false

PolyUtil::isLocationOnEdge(const LatLng& point, const LatLngList& polygon, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true) - Computes whether the given point lies on or near to a polyline, or the edge of a polygon, within a specified tolerance. Returns true when the difference between the latitude and longitude of the supplied point, and the closest point on the edge, is less than the tolerance. The tolerance defaults to 0.1 meters.

  • point - a point in geographical coordinates: latitude and longitude
  • polygon - a series of connected coordinates in an ordered sequence
  • tolerance - tolerance value in meters
  • geodesic - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise

Return value: bool - whether the given point lies on or near the edge of a polygon

// On equator.
std::vector<LatLng> equator = { {0, 90}, {0, 180} };

double small = 5e-7; // Half the default tolerance.
double big   = 2e-6; // Double the default tolerance.

std::cout << PolyUtil::isLocationOnEdge(LatLng(0, 90 - small), equator); // true
std::cout << PolyUtil::isLocationOnEdge(LatLng(0, 90 - big),   equator); // false

PolyUtil::isLocationOnPath(const LatLng& point, const LatLngList& polyline, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true) - Computes whether the given point lies on or near a polyline, within a specified tolerance in meters. The polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise. The polyline is not closed -- the closing segment between the first point and the last point is not included.

  • point - a point in geographical coordinates: latitude and longitude
  • polygon - a series of connected coordinates in an ordered sequence
  • tolerance - tolerance value in meters
  • geodesic - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise

Return value: bool - whether the point lies on or near a polyline

// On equator.
std::vector<LatLng> equator = { {0, 90}, {0, 180} };

double small = 5e-7; // Half the default tolerance.
double big   = 2e-6; // Double the default tolerance.

std::cout << PolyUtil::isLocationOnPath(LatLng(0, 90 - small), equator); // true
std::cout << PolyUtil::isLocationOnPath(LatLng(0, 90 - big),   equator); // false

PolyUtil::distanceToLine(const LatLng& p, const LatLng& start, const LatLng& end) - Computes the distance on the sphere between the point p and the line segment start to end.

  • point - the point to be measured
  • start - the beginning of the line segment
  • end - the end of the line segment

Return value: double - the distance in meters (assuming spherical earth)

LatLng startLine(28.05359, -82.41632);
LatLng endLine(28.05310, -82.41634);
LatLng point(28.05342, -82.41594);

std::cout << PolyUtil::distanceToLine(point, startLine, endLine); // 37.947946

SphericalUtil functions

SphericalUtil::computeHeading(const LatLng& from, const LatLng& to) - Returns the heading from one LatLng to another LatLng. Headings are expressed in degrees clockwise from North within the range [-180,180).

  • from - a point in geographical coordinates: latitude and longitude
  • to - a point in geographical coordinates: latitude and longitude

Return value: double - the heading in degrees clockwise from north

LatLng front(0,  0);
LatLng right(0, 90);

std::cout << SphericalUtil::computeHeading(right, front); // -90
std::cout << SphericalUtil::computeHeading(front, right); // +90

SphericalUtil::computeOffset(const LatLng& from, double distance, double heading) - Returns the LatLng resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north).

  • from - the LatLng from which to start.
  • distance - the distance to travel.
  • heading - the heading in degrees clockwise from north.

Return value: LatLng - resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north)

LatLng front(0, 0);

auto up    = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2,   0); // LatLng( 90,    0)
auto down  = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2, 180); // LatLng(-90,    0)
auto left  = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2, -90); // LatLng(  0,  -90)
auto right = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2,  90); // LatLng(  0,   90)
auto back  = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS,      90); // LatLng(  0, -180)

SphericalUtil::computeOffsetOrigin(const LatLng& to, double distance, double heading) - Returns the location of origin when provided with a LatLng destination, meters travelled and original heading. Headings are expressed in degrees clockwise from North.

  • from - the destination LatLng
  • distance - the distance travelled, in meters.
  • heading - the heading in degrees clockwise from north

Return value: LatLng - the location of origin when provided with a LatLng destination, meters travelled and original heading. Headings are expressed in degrees clockwise from North

LatLng front(0, 0);

assert(front == SphericalUtil::computeOffsetOrigin(front, 0, 0));

assert(front == SphericalUtil::computeOffsetOrigin(LatLng(  0,  45), M_PI * MathUtil::EARTH_RADIUS / 4,  90));
assert(front == SphericalUtil::computeOffsetOrigin(LatLng(  0, -45), M_PI * MathUtil::EARTH_RADIUS / 4, -90));
assert(front == SphericalUtil::computeOffsetOrigin(LatLng( 45,   0), M_PI * MathUtil::EARTH_RADIUS / 4,   0));
assert(front == SphericalUtil::computeOffsetOrigin(LatLng(-45,   0), M_PI * MathUtil::EARTH_RADIUS / 4, 190));

SphericalUtil::interpolate(const LatLng& from, const LatLng& to, double fraction) - Returns the LatLng which lies the given fraction of the way between the origin LatLng and the destination LatLng.

  • from - the LatLng from which to start.
  • to - the LatLng toward which to travel.
  • fraction - a fraction of the distance to travel.

Return value: LatLng - point which lies the given fraction of the way between the origin LatLng and the destination LatLng

LatLng up(90, 0);
LatLng front(0, 0);

assert(LatLng(1,  0) == SphericalUtil::interpolate(front, up,  1 / 90.0));
assert(LatLng(1,  0) == SphericalUtil::interpolate(up, front, 89 / 90.0));
assert(LatLng(89, 0) == SphericalUtil::interpolate(front, up, 89 / 90.0));
assert(LatLng(89, 0) == SphericalUtil::interpolate(up, front,  1 / 90.0));

SphericalUtil::computeDistanceBetween(const LatLng& from, const LatLng& to) - Returns the distance, in meters, between two LatLngs.

  • from - the first point
  • to - the second point

Return value: double - the distance, in meters, between two LatLngs

LatLng up(90, 0);
LatLng down(-90, 0);

std:cout << SphericalUtil::computeDistanceBetween(up, down); // MathUtil::EARTH_RADIUS

SphericalUtil::computeLength(const LatLngList& path) - Returns the length of the given path, in meters, on Earth

  • path - a series of connected coordinates in an ordered sequence. Any iterable containers.

Return valuse: double - the length of the given path, in meters, on Earth

// List with three points
std::vector<LatLng> latLngs2 = { {0, 0}, {90, 0}, {0, 90} };

std::cout << SphericalUtil::computeLength(latLngs2); // M_PI * MathUtil::EARTH_RADIUS

SphericalUtil::computeArea(const LatLngList& path) - Returns the area of a closed path on Earth.

  • path - a closed path. Any iterable containers.

Return value: double - the area of a closed path on Earth

LatLng up    = { 90.0,  0.0 };
LatLng down  = {-90.0,  0.0 };
LatLng front = {  0.0,  0.0 };
LatLng right = {  0.0, 90.0 };

std::vector<LatLng> path = { right, down, front, up, right };

std::cout << SphericalUtil::computeArea(second); // M_PI * MathUtil::EARTH_RADIUS * MathUtil::EARTH_RADIUS

SphericalUtil::computeSignedArea(const LatLngList& path) - Returns the signed area of a closed path on Earth. The sign of the area may be used to determine the orientation of the path. "inside" is the surface that does not contain the South Pole.

  • path - a closed path. Any iterable containers.

Return value: double - the loop's area in square meters

LatLng up    = { 90.0,    0.0 };
LatLng down  = {-90.0,    0.0 };
LatLng front = {  0.0,    0.0 };
LatLng right = {  0.0,   90.0 };

std::vector<LatLng> path         = { right,   up, front, down, right };
std::vector<LatLng> pathReversed = { right, down, front,   up, right };

assert(SphericalUtil::computeSignedArea(path) == -SphericalUtil::computeSignedArea(pathReversed));

Support

Please open an issue on GitHub

License

Geometry Library Google Maps API V3 is released under the MIT License. See the bundled LICENSE file for details.

About

CPP Geometry Library provides utility functions for the computation of geometric data on the surface of the Earth.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published