Skip to content

Instantly share code, notes, and snippets.

@mrbitsdcf
Created May 28, 2019 18:53
Show Gist options
  • Save mrbitsdcf/b659f97f0e5823d8fc21c14398e94749 to your computer and use it in GitHub Desktop.
Save mrbitsdcf/b659f97f0e5823d8fc21c14398e94749 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Distances on Globe and Flat Earth
# Instructions:
# - Input each information as asked;
# - Input coordinates in degrees (23.1234);
# - If coordinates are at South or West, use negative numbers (-23.1234);
# - North Pole to Equator based on a Flat Earth with 40.075km equatorial perimeter.
# Earth radius = 6371km rounded, so little diferences are expected if you're considering WGS84 geoid.
# Author: MrBiTs (mrbits@mrbits.com.br)
# CHUPA POMBAIADA!!!
from math import pi, sin, cos, sqrt, radians, atan2
def globe(lat1, long1, lat2, long2):
'''Assume Earth as a sphere'''
dlat = radians(lat2 - lat1)
dlon = radians(long2 - long1)
a = sin(dlat / 2) * sin(dlat / 2) + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon / 2) * sin(dlon / 2)
c = 2 * atan2(sqrt(a), sqrt(1 - a))
d = R * c
return d
def flatland(lat1, long1, lat2, long2):
pi2 = pi / 2
lat1 = radians(lat1)
long1 = radians(long1)
lat2 = radians(lat2)
long2 = radians(long2)
r1 = (1 - lat1 / pi2) * EQ
r2 = (1 - lat2 / pi2) * EQ
x1 = r1 * cos(long1)
y1 = r1 * sin(long1)
x2 = r2 * cos(long2)
y2 = r2 * sin(long2)
dx = x2 - x1
dy = y2 - y1
return sqrt(dx**2 + dy**2)
if __name__ == "__main__":
place = raw_input("Name your places: ")
lat1 = raw_input("Type Latitude at first place in degrees: ")
long1 = raw_input("Type Longitude at first place in degrees: ")
lat2 = raw_input("Type Latitude at second place in degrees: ")
long2 = raw_input("Type Longitude at second place in degrees: ")
EQ = raw_input("Type North Pole to Equator in Flat Earth (or leave it blank to assume 10,007.543398km): ")
R = 6371.0
if not EQ:
EQ = R * pi / 2
LG = globe(
float(lat1),
float(long1),
float(lat2),
float(long2)
)
print "Distance on Globe: {0} km".format(LG)
LF = flatland(
float(lat1),
float(long1),
float(lat2),
float(long2)
)
print "Distance on FE: {0} km".format(LF)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment