Skip to content

Instantly share code, notes, and snippets.

@TheMatjaz
Created February 26, 2022 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheMatjaz/1cb5b1ee8fb5481735fb0033a0484916 to your computer and use it in GitHub Desktop.
Save TheMatjaz/1cb5b1ee8fb5481735fb0033a0484916 to your computer and use it in GitHub Desktop.
Distance to the horizon in Python
#!/usr/bin/env python3
# coding: utf8
# BSD 3-Clause License
# ===============================================================================
#
# Copyright © 2022, Matjaž Guštin <dev@matjaz.it> <https://matjaz.it>.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of nor the names of its contributors may be used to
# endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS “AS IS”
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Compute how far the horizon is from where you stand."""
import math
import unittest
EARTH_RADIUS_EQUATOR_M = 6_378_137.0
"""Distance from Earth's centre to the surface at the equator in metres.
Often written as ``a``"""
EARTH_RADIUS_POLES_M = 6_356_752.3
"""Distance from Earth's centre to the surface at the poles in metres.
Often written as ``a``"""
def earth_radius(latitude_deg: float = None) -> float:
"""Distance from Earth centre to the surface in metres based on
your latitude (offset from equator).
Source: https://en.wikipedia.org/wiki/Earth_radius, 2022-02-26
Args:
latitude_deg: degrees of offset from the Equator.
``0`` for the Equator, ``+/-90`` for the poles (north or south)
but the sign is discarded. ``None`` or ``NaN`` for the average
radius.
"""
if latitude_deg is None or math.isnan(latitude_deg):
# Arithmetic mean of the polar and equatorial radii
return (2 * EARTH_RADIUS_EQUATOR_M + EARTH_RADIUS_POLES_M) / 3
if latitude_deg < -90 or latitude_deg > 90:
raise ValueError('Latitude must be in [-90, +90] degrees, '
f'{latitude_deg} found instead')
latitude_rad = math.radians(latitude_deg)
sin = math.sin(latitude_rad)
cos = math.cos(latitude_rad)
num = ((EARTH_RADIUS_EQUATOR_M ** 2 * cos) ** 2
+ (EARTH_RADIUS_POLES_M ** 2 * sin) ** 2)
den = ((EARTH_RADIUS_EQUATOR_M * cos) ** 2
+ (EARTH_RADIUS_POLES_M * sin) ** 2)
return math.sqrt(num / den)
def horizon_distance(height_above_surface_m: float,
latitude_deg: float = None,
correct_for_atmosphere: bool = False) -> float:
"""Distance of the visible horizon at the viewer's height.
Source: https://en.wikipedia.org/wiki/Horizon, 2022-02-26
Args:
height_above_surface_m: height of the observer's eyes from Earth's
surface in metres (height above sea level).
latitude_deg: degrees of offset from the Equator of the
observer's position, used to compute the Earth's radius at the
observer's position. ``0`` for the Equator, ``+/-90`` for the poles
(north or south) but the sign is discarded. ``None`` or ``NaN``
(default) will use the average Earth radius..
correct_for_atmosphere: default is False. When True, the Young's
approximate compensation for atmospheric refraction is applied.
This increases the Earth's radius by 1/6, virtually bringing
the horizon further away. Light further than the horizon
is refracted downwards, allowing us to see behind the horizon
to some extend. This correction only works in standard atmospheric
conditions.
Returns:
Distance between observer's eyes and the furthest still-visible
item on the horizon.
"""
radius = earth_radius(latitude_deg)
if correct_for_atmosphere:
radius *= 7 / 6 # Young approximate correction method
return math.sqrt(height_above_surface_m *
(2 * radius + height_above_surface_m))
class TestEarthRadius(unittest.TestCase):
def test_earth_radius_default(self):
self.assertAlmostEqual(6_371_008.8, earth_radius(), places=1)
self.assertAlmostEqual(6_371_008.8, earth_radius(None), places=1)
self.assertAlmostEqual(6_371_008.8, earth_radius(math.nan), places=1)
def test_earth_radius_poles(self):
self.assertAlmostEqual(EARTH_RADIUS_POLES_M, earth_radius(+90))
self.assertAlmostEqual(EARTH_RADIUS_POLES_M, earth_radius(-90))
def test_earth_radius_equator(self):
self.assertAlmostEqual(EARTH_RADIUS_EQUATOR_M, earth_radius(+0))
self.assertAlmostEqual(EARTH_RADIUS_EQUATOR_M, earth_radius(-0))
def test_earth_radius_any(self):
self.assertAlmostEqual(6367489.536800727, earth_radius(+45))
self.assertAlmostEqual(6367489.536800727, earth_radius(-45))
self.assertAlmostEqual(6372824.41677746, earth_radius(+30))
self.assertAlmostEqual(6372824.41677746, earth_radius(-30))
class TestHorizonDistance(unittest.TestCase):
def test_horizon_distance_default(self):
# At 1.7 metres height (average eye height when on surface)
self.assertAlmostEqual(math.sqrt(2 * 6_371_008.8 * 1.7 + 1.7 ** 2),
horizon_distance(1.7), places=3)
# At 100 metres height
self.assertAlmostEqual(math.sqrt(2 * 6_371_008.8 * 100 + 100 ** 2),
horizon_distance(100), places=3)
def test_horizon_distance_known_latitude(self):
# At 1.7 metres height (average eye height when on surface)
self.assertAlmostEqual(
math.sqrt(2 * EARTH_RADIUS_EQUATOR_M * 1.7 + 1.7 ** 2),
horizon_distance(1.7, 0), places=3)
self.assertAlmostEqual(
math.sqrt(2 * EARTH_RADIUS_POLES_M * 1.7 + 1.7 ** 2),
horizon_distance(1.7, -90), places=3)
# At 100 metres height
self.assertAlmostEqual(
math.sqrt(2 * EARTH_RADIUS_EQUATOR_M * 100 + 100 ** 2),
horizon_distance(100, 0), places=3)
self.assertAlmostEqual(
math.sqrt(2 * EARTH_RADIUS_POLES_M * 100 + 100 ** 2),
horizon_distance(100, 90), places=3)
def test_horizon_distance_known_latitude_with_correction(self):
# At 1.7 metres height (average eye height when on surface)
self.assertAlmostEqual(
math.sqrt(2 * EARTH_RADIUS_EQUATOR_M * 7 / 6 * 1.7 + 1.7 ** 2),
horizon_distance(1.7, 0, True), places=3)
self.assertAlmostEqual(
math.sqrt(2 * EARTH_RADIUS_POLES_M * 7 / 6 * 1.7 + 1.7 ** 2),
horizon_distance(1.7, -90, True), places=3)
# At 100 metres height
self.assertAlmostEqual(
math.sqrt(2 * EARTH_RADIUS_EQUATOR_M * 7 / 6 * 100 + 100 ** 2),
horizon_distance(100, 0, True), places=3)
self.assertAlmostEqual(
math.sqrt(2 * EARTH_RADIUS_POLES_M * 7 / 6 * 100 + 100 ** 2),
horizon_distance(100, 90, True), places=3)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment