Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gistrec/cpp-geometry-library
CPP Geometry Library provides utility functions for the computation of geometric data on the surface of the Earth.
https://github.com/gistrec/cpp-geometry-library
cpp distance geometry-library google-maps-api latlng polyline
Last synced: about 1 month ago
JSON representation
CPP Geometry Library provides utility functions for the computation of geometric data on the surface of the Earth.
- Host: GitHub
- URL: https://github.com/gistrec/cpp-geometry-library
- Owner: gistrec
- License: apache-2.0
- Created: 2019-11-13T11:01:04.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-16T12:29:47.000Z (almost 5 years ago)
- Last Synced: 2023-08-09T08:14:20.320Z (over 1 year ago)
- Topics: cpp, distance, geometry-library, google-maps-api, latlng, polyline
- Language: C++
- Homepage:
- Size: 60.5 KB
- Stars: 31
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Geometry Library
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](https://github.com/googlemaps/android-maps-utils/).
## Features
* [Spherical](https://developers.google.com/maps/documentation/javascript/reference#spherical) contains spherical geometry utilities allowing you to compute angles, distances and areas from latitudes and longitudes.
* [Poly](https://developers.google.com/maps/documentation/javascript/reference#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:
```c++
#include
#include#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 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
* [`containsLocation(LatLng point, LatLngList polygon, bool geodesic)`](#containsLocation)
* [`isLocationOnEdge(LatLng point, LatLngList polygon, double tolerance, bool geodesic)`](#isLocationOnEdge)
* [`isLocationOnPath(LatLng point, LatLngList polyline, double tolerance, bool geodesic)`](#isLocationOnPath)
* [`distanceToLine(LatLng point, LatLng start, LatLng end)`](#distanceToLine)### SphericalUtil class
* [`computeHeading(LatLng from, LatLng to)`](#computeHeading)
* [`computeOffset(LatLng from, double distance, double heading)`](#computeOffset)
* [`computeOffsetOrigin(LatLng to, double distance, double heading)`](#computeOffsetOrigin)
* [`interpolate(LatLng from, LatLng to, double fraction)`](#interpolate)
* [`computeDistanceBetween(LatLng from, LatLng to)`](#computeDistanceBetween)
* [`computeLength(LatLngList path)`](#computeLength)
* [`computeArea(LatLngList path)`](#computeArea)
* [`computeSignedArea(LatLngList path)`](#computeSignedArea)## 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, inclusiveUsage example:
```c++
LatLng northPole = {90, 0};LatLng otherPoint = northPole;
```---
`LatLngList` - a series of connected coordinates in an ordered sequence. Any iterable containers.
Usage example:
```c++
std::vector aroundNorthPole = { {89, 0}, {89, 120}, {89, -120} };std::array 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 otherwiseReturn value: `bool` - whether the given point lies inside the specified polygon
```c++
// Around the north pole.
std::vector 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 otherwiseReturn value: `bool` - whether the given point lies on or near the edge of a polygon
```c++
// On equator.
std::vector 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 otherwiseReturn value: `bool` - whether the point lies on or near a polyline
```c++
// On equator.
std::vector 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 segmentReturn value: `double` - the distance in meters (assuming spherical earth)
```c++
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 longitudeReturn value: `double` - the heading in degrees clockwise from north
```c++
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)
```c++
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 northReturn 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
```c++
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
```c++
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 pointReturn value: `double` - the distance, in meters, between two LatLngs
```c++
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
```c++
// List with three points
std::vector 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
```c++
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 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
```c++
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 path = { right, up, front, down, right };
std::vector pathReversed = { right, down, front, up, right };assert(SphericalUtil::computeSignedArea(path) == -SphericalUtil::computeSignedArea(pathReversed));
```---
## Support
[Please open an issue on GitHub](https://github.com/gistrec/cpp-geometry-library/issues)
## License
Geometry Library Google Maps API V3 is released under the MIT License.
See the bundled [LICENSE](https://github.com/alexpechkarev/geometry-library/blob/master/LICENSE) file for details.