View Source Geocalc (Geocalc v0.8.5)

Calculate distance, bearing and more between Latitude/Longitude points.

Link to this section Summary

Functions

Calculates how far the point is along a path from from start-point, heading towards end-point.

Calculate the given area surface.

Check if a point is at the border of area.

Check if a point at the center point of area.

Calculates bearing.

Calculates a bounding box around a point with a radius in meters.

Calculates a bounding box for a list of points.

Returns true if the bounding box contains the given point.

Compute distance from the point to great circle defined by start-point and end-point.

Calculates the pair of meridians at which a great circle defined by two points crosses the given latitude.

Converts degrees to radians.

Finds point between start and end points in direction to end point with given distance (in meters).

Calculates distance between 2 points.

Extend the bounds to contain the given bounds.

Compute the geographic center (aka geographic midpoint, center of gravity) for an array of geocoded objects and/or [lat,lon] arrays (can be mixed).

Check if a point is inside area.

Finds intersection point from start points with given bearings.

Returns true if the bounding box intersects the given bounds.

Calculates maximum latitude reached when travelling on a great circle on given bearing from the point (Clairaut's formula). Negate the result for the minimum latitude (in the Southern hemisphere).

Check if a point is outside area.

Returns true if the bounding box overlaps the given bounds.

Converts radians to degrees.

Calculates if a point is within a polygon.

Calculates if a point is within radius of the center of a circle.

Link to this section Types

@type point_or_bearing() :: Geocalc.Point.t() | number()

Link to this section Functions

Link to this function

along_track_distance_to(point, path_start_point, path_end_point)

View Source
@spec along_track_distance_to(Geocalc.Point.t(), Geocalc.Point.t(), Geocalc.Point.t()) ::
  number()

Calculates how far the point is along a path from from start-point, heading towards end-point.

That is, if a perpendicular is drawn from the point to the (great circle) path, the along-track distance is the distance from the start point to where the perpendicular crosses the path.

examples

Examples

iex> berlin = [52.5075419, 13.4251364]
iex> london = [51.5286416, -0.1015987]
iex> paris = [48.8588589, 2.3475569]
iex> Geocalc.along_track_distance_to(berlin, london, paris)
310412.6031976226

Calculate the given area surface.

Returns area surface in square meters.

examples

Examples

iex> area = %Geocalc.Shape.Circle{latitude: 48.856614, longitude: 2.3522219, radius: 1000}
iex> Geocalc.area_size(area)
3141592.653589793
Link to this function

at_area_border?(area, point)

View Source

Check if a point is at the border of area.

Returns true if at border, false if not.

examples

Examples

iex> area = %Geocalc.Shape.Circle{latitude: 48.856614, longitude: 2.3522219, radius: 1000}
iex> point = %{lat: 48.856418, lng: 2.365871}
iex> Geocalc.at_area_border?(area, point)
true
Link to this function

at_center_point?(area, point)

View Source

Check if a point at the center point of area.

Returns true if at center point, false if not

examples

Examples

iex> area = %Geocalc.Shape.Circle{latitude: 48.856614, longitude: 2.3522219, radius: 100}
iex> point = %{lat: 48.856614, lng: 2.3522219}
iex> Geocalc.at_center_point?(area, point)
true
Link to this function

bearing(point_1, point_2)

View Source
@spec bearing(Geocalc.Point.t(), Geocalc.Point.t()) :: number()

Calculates bearing.

Returns radians.

examples

Examples

iex> berlin = {52.5075419, 13.4251364}
iex> paris = {48.8588589, 2.3475569}
iex> Geocalc.bearing(berlin, paris)
-1.9739245359361486
iex> Geocalc.bearing(paris, berlin)
1.0178267866082613

iex> berlin = %{lat: 52.5075419, lon: 13.4251364}
iex> paris = %{latitude: 48.8588589, longitude: 2.3475569}
iex> Geocalc.bearing(berlin, paris)
-1.9739245359361486
Link to this function

bounding_box(point, radius_in_m)

View Source
@spec bounding_box(Geocalc.Point.t(), number()) :: list()

Calculates a bounding box around a point with a radius in meters.

Returns an array with 2 points (list format). The bottom left (southwest) point, and the top-right (northeast) one.

examples

Examples

iex> berlin = [52.5075419, 13.4251364]
iex> radius = 10_000
iex> Geocalc.bounding_box(berlin, radius)
[[52.417520954378574, 13.277235453275123], [52.59756284562143, 13.573037346724874]]
Link to this function

bounding_box_for_points(points)

View Source
@spec bounding_box_for_points(list()) :: list()

Calculates a bounding box for a list of points.

Returns an array with 2 points (list format). The bottom left (southwest) point, and the top-right (northeast) one.

examples

Examples

iex> berlin = [52.5075419, 13.4251364]
iex> london = [51.5286416, -0.1015987]
iex> paris = [48.8588589, 2.3475569]
iex> Geocalc.bounding_box_for_points([berlin, london, paris])
[[48.8588589, -0.1015987], [52.5075419, 13.4251364]]
Link to this function

contains_point?(bounding_box, point)

View Source
@spec contains_point?(list(), Geocalc.Point.t()) :: boolean()

Returns true if the bounding box contains the given point.

examples

Examples

iex> germany = [[47.27, 5.87], [55.1, 15.04]]
iex> berlin = [52.5075419, 13.4251364]
iex> Geocalc.contains_point?(germany, berlin)
true
Link to this function

cross_track_distance_to(point, path_start_point, path_end_point)

View Source
@spec cross_track_distance_to(Geocalc.Point.t(), Geocalc.Point.t(), Geocalc.Point.t()) ::
  number()

Compute distance from the point to great circle defined by start-point and end-point.

Returns distance in meters.

examples

Examples

iex> berlin = [52.5075419, 13.4251364]
iex> london = [51.5286416, -0.1015987]
iex> paris = [48.8588589, 2.3475569]
iex> Geocalc.cross_track_distance_to(berlin, london, paris)
-877680.2992295175
Link to this function

crossing_parallels(point_1, path_2, latitude)

View Source
@spec crossing_parallels(Geocalc.Point.t(), Geocalc.Point.t(), number()) :: tuple()

Calculates the pair of meridians at which a great circle defined by two points crosses the given latitude.

Returns longitudes.

examples

Examples

iex> berlin = [52.5075419, 13.4251364]
iex> paris = [48.8588589, 2.3475569]
iex> Geocalc.crossing_parallels(berlin, paris, 12.3456)
{:ok, 123.179463369946, -39.81144878508576}

iex> point_1 = %{lat: 0, lng: 0}
iex> point_2 = %{lat: -180, lng: -90}
iex> latitude = 45.0
iex> Geocalc.crossing_parallels(point_1, point_2, latitude)
{:error, "Not found"}
Link to this function

degrees_to_radians(degrees)

View Source
@spec degrees_to_radians(number()) :: number()

Converts degrees to radians.

Returns radians.

examples

Examples

iex> Geocalc.degrees_to_radians(143.67156782221554)
2.5075419

iex> Geocalc.degrees_to_radians(-10.735322818996854)
-0.18736672945597435
Link to this function

destination_point(point_1, point_2, distance)

View Source
@spec destination_point(Geocalc.Point.t(), point_or_bearing(), number()) :: tuple()

Finds point between start and end points in direction to end point with given distance (in meters).

Finds point from start point with given distance (in meters) and bearing. Returns array with latitude and longitude.

examples

Examples

Find destination point by bearing:

iex> berlin = [52.5075419, 13.4251364]
iex> paris = [48.8588589, 2.3475569]
iex> bearing = Geocalc.bearing(berlin, paris)
iex> distance = 400_000
iex> Geocalc.destination_point(berlin, bearing, distance)
{:ok, [50.97658022467569, 8.165929595956982]}

iex> zero_point = {0.0, 0.0}
iex> equator_degrees = 90.0
iex> equator_bearing = Geocalc.degrees_to_radians(equator_degrees)
iex> distance = 1_000_000
iex> Geocalc.destination_point(zero_point, equator_bearing, distance)
{:ok, [5.484172965344896e-16, 8.993216059187306]}

iex> berlin = %{lat: 52.5075419, lon: 13.4251364}
iex> bearing = -1.9739245359361486
iex> distance = 100_000
iex> Geocalc.destination_point(berlin, bearing, distance)
{:ok, [52.147030316318904, 12.076990111001148]}

Find destination point by point:

iex> berlin = [52.5075419, 13.4251364]
iex> paris = [48.8588589, 2.3475569]
iex> distance = 250_000
iex> Geocalc.destination_point(berlin, paris, distance)
{:ok, [51.578054644172525, 10.096282782248409]}
Link to this function

distance_between(point_1, point_2)

View Source
@spec distance_between(Geocalc.Point.t(), Geocalc.Point.t()) :: number()

Calculates distance between 2 points.

Returns distance in meters.

examples

Examples

iex> berlin = [Decimal.new("52.5075419"), Decimal.new("13.4251364")]
iex> paris = [48.8588589, 2.3475569]
iex> Geocalc.distance_between(berlin, paris)
878327.4291149472
iex> Geocalc.distance_between(paris, berlin)
878327.4291149472

iex> berlin = %{lat: 52.5075419, lon: 13.4251364}
iex> london = %{lat: Decimal.new("51.5286416"), lng: Decimal.new("-0.1015987")}
iex> paris = %{latitude: 48.8588589, longitude: 2.3475569}
iex> Geocalc.distance_between(berlin, paris)
878327.4291149472
iex> Geocalc.distance_between(paris, london)
344229.88946533133
Link to this function

extend_bounding_box(bounding_box_1, bounding_box_2)

View Source
@spec extend_bounding_box(list(), list()) :: list()

Extend the bounds to contain the given bounds.

Returns an array with 2 points (list format). The bottom left (southwest) point, and the top-right (northeast) one.

examples

Examples

iex> berlin = [52.5075419, 13.4251364]
iex> london = [51.5286416, -0.1015987]
iex> Geocalc.extend_bounding_box([berlin, berlin], [london, london])
[[51.5286416, -0.1015987], [52.5075419, 13.4251364]]
Link to this function

geographic_center(points)

View Source
@spec geographic_center(list()) :: Geocalc.Point.t()

Compute the geographic center (aka geographic midpoint, center of gravity) for an array of geocoded objects and/or [lat,lon] arrays (can be mixed).

Any objects missing coordinates are ignored. Follows the procedure documented at http://www.geomidpoint.com/calculation.html.

examples

Examples

iex> point_1 = [0, 0]
iex> point_2 = [0, 3]
iex> Geocalc.geographic_center([point_1, point_2])
[0.0, 1.5]

Check if a point is inside area.

Returns true if inside area, false if not.

examples

Examples

iex> area = %Geocalc.Shape.Circle{latitude: 48.856614, longitude: 2.3522219, radius: 1000}
iex> point = %{lat: 48.856612, lng: 2.3522217}
iex> Geocalc.in_area?(area, point)
true
Link to this function

intersection_point(point_1, bearing_1, point_2, bearing_2)

View Source
@spec intersection_point(
  Geocalc.Point.t(),
  point_or_bearing(),
  Geocalc.Point.t(),
  point_or_bearing()
) ::
  tuple()

Finds intersection point from start points with given bearings.

Returns array with latitude and longitude. Raise an exception if no intersection point found.

examples

Examples

Find intersection point by bearing:

iex> berlin = [52.5075419, 13.4251364]
iex> berlin_bearing = -2.102
iex> london = [51.5286416, -0.1015987]
iex> london_bearing = 1.502
iex> Geocalc.intersection_point(berlin, berlin_bearing, london, london_bearing)
{:ok, [51.49271112601574, 10.735322818996854]}

iex> berlin = %{lat: 52.5075419, lng: 13.4251364}
iex> bearing = Geocalc.degrees_to_radians(90.0)
iex> Geocalc.intersection_point(berlin, bearing, berlin, bearing)
{:ok, [52.5075419, 13.4251364]}

Find intersection point by point:

iex> berlin = {52.5075419, 13.4251364}
iex> london = {51.5286416, -0.1015987}
iex> paris = {48.8588589, 2.3475569}
iex> Geocalc.intersection_point(berlin, london, paris, london)
{:ok, [51.5286416, -0.10159869999998701]}

Raise exception when no intersection points:

iex> berlin_1 = %{lat: 52.5075419, lng: 13.4251364}
iex> berlin_2 = %{lat: 52.5075419, lng: 13.57}
iex> bearing = Geocalc.degrees_to_radians(90.0)
iex> Geocalc.intersection_point(berlin_1, bearing, berlin_2, bearing)
{:error, "No intersection point found"}
Link to this function

intersects_bounding_box?(bounding_box_1, bounding_box_2)

View Source
@spec intersects_bounding_box?(list(), list()) :: boolean()

Returns true if the bounding box intersects the given bounds.

Two bounds intersect if they have at least one point in common.

examples

Examples

iex> germany = [[47.27, 5.87], [55.1, 15.04]]
iex> poland = [[49.0, 14.12], [55.03, 24.15]]
iex> Geocalc.intersects_bounding_box?(germany, poland)
true
Link to this function

max_latitude(point, bearing)

View Source
@spec max_latitude(Geocalc.Point.t(), number()) :: number()

Calculates maximum latitude reached when travelling on a great circle on given bearing from the point (Clairaut's formula). Negate the result for the minimum latitude (in the Southern hemisphere).

The maximum latitude is independent of longitude; it will be the same for all points on a given latitude.

Returns radians.

examples

Examples

iex> berlin = [52.5075419, 13.4251364]
iex> paris = [48.8588589, 2.3475569]
iex> bearing = Geocalc.bearing(berlin, paris)
iex> Geocalc.max_latitude(berlin, bearing)
55.953467429882835
Link to this function

outside_area?(area, point)

View Source

Check if a point is outside area.

Returns true if outside area, false if not

examples

Examples

iex> area = %Geocalc.Shape.Circle{latitude: 48.856614, longitude: 2.3522219, radius: 10}
iex> point = %{lat: 48.856418, lng: 2.365871}
iex> Geocalc.outside_area?(area, point)
true
Link to this function

overlaps_bounding_box?(bounding_box_1, bounding_box_2)

View Source
@spec overlaps_bounding_box?(list(), list()) :: boolean()

Returns true if the bounding box overlaps the given bounds.

Two bounds overlap if their intersection is an area.

examples

Examples

iex> germany = [[47.27, 5.87], [55.1, 15.04]]
iex> berlin_suburbs = [[52.338261, 13.08835], [52.67551, 13.76116]]
iex> Geocalc.overlaps_bounding_box?(germany, berlin_suburbs)
true
Link to this function

radians_to_degrees(radians)

View Source
@spec radians_to_degrees(number()) :: number()

Converts radians to degrees.

Returns degrees.

examples

Examples

iex> Geocalc.radians_to_degrees(2.5075419)
143.67156782221554

iex> Geocalc.radians_to_degrees(-0.1015987)
-5.821176714015797
@spec within?([Geocalc.Point.t()], Geocalc.Point.t()) :: boolean()

Calculates if a point is within a polygon.

Returns boolean.

examples

Examples

iex> point = [14.952242, 60.1696017]
iex> poly = [[24.950899, 60.169158], [24.953492, 60.169158], [24.953510, 60.170104], [24.950958, 60.169990]]
iex> Geocalc.within?(poly, point)
false

iex> point = [24.952242, 60.1696017]
iex> poly = [[24.950899, 60.169158], [24.953492, 60.169158], [24.953510, 60.170104], [24.950958, 60.169990]]
iex> Geocalc.within?(poly, point)
true

iex> point = [24.976567, 60.1612500]
iex> poly = [[24.950899, 60.169158], [24.953492, 60.169158], [24.953510, 60.170104], [24.950958, 60.169990]]
iex> Geocalc.within?(poly, point)
false
Link to this function

within?(radius, center, point)

View Source
@spec within?(number(), Geocalc.Point.t(), Geocalc.Point.t()) :: boolean()

Calculates if a point is within radius of the center of a circle.

Returns boolean.

examples

Examples

iex> berlin = [52.5075419, 13.4251364]
iex> paris = [48.8588589, 2.3475569]
iex> Geocalc.within?(10, paris, berlin)
false
iex> Geocalc.within?(10, berlin, paris)
false

iex> san_juan = %{lat: 18.4655, lon: 66.1057}
iex> puerto_rico = %{lat: 18.2208, lng: 66.5901}
iex> Geocalc.within?(170_000, puerto_rico, san_juan)
true