Python Forum
distance between 2 points
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
distance between 2 points
#1
This is a small part of a bigger project I'm working on to Pythonize (a real word I just made up) the formulas in Jean Meeus' book "Astronomical Algorithms" copywrite 1991, pages 80 and 81 and calculates the geodesic distance between two points on the earths surface. The two co-ordinates referenced at the end are, per Mr. Meeus for the Observatoire de Paris (France) and the US Naval Observatory (Washington DC) with an accuracy of 50 meters. Comments and improvements are welcome.

#! /usr/bin/python3
# -*- coding: utf-8 -*-

from math import atan, cos, pi, sin, sqrt


def calc_geo_dist(lon1, lat1, lon2, lat2):
    a = 6378.137    # in km, semi-major axis
    # b = 6356.752    # polar radius in km, rounded  approx 6356752.314140347 meters per GRS 80
    rad = pi/180
    flat = 1/298.257    # ok with nasa    actual 0.003352810681183637418
    e = ((lon1 - lon2) / 2) * rad
    f = ((lat1+lat2) / 2) * rad
    g = ((lat1-lat2) / 2) * rad
    s = sin(g) * sin(g) * cos(e) * cos(e) + cos(f) * cos(f) * sin(e) * sin(e)
    c = cos(g) * cos(g) * cos(e) * cos(e) + sin(f) * sin(f) * sin(e) * sin(e)
    w = atan(sqrt(s/c))
    r = sqrt(s * c) / w
    d = 2 * w * a
    h1 = (3 * r - 1) / (2 * c)
    h2 = (3 * r + 1) / (2 * s)
    s1 = d * (1 + flat * h1 * sin(f) * sin(f) * cos(g) * cos(g) - flat * h2 * cos(f) * cos(f) * sin(g) * sin(g))
    return s1

while True:
    try:
        my_long1 = float(input('\nEnter longitude in decimal degrees: '))
        if -180 <= my_long1 <= 180:
            break
        else:
            print('\n WARNING: Value must be between -180 and 180 ')
    except ValueError as err:
        print('\n ERROR: Enter value between -180 and 180 {}'.format(err))

while True:
    try:
        my_lat1 = float(input('Enter latitude in decimal degrees: '))
        if -90 <= my_lat1 <= 90:
            break
        else:
            print('\n WARNING: Value must be between -90 and 90 ')
    except ValueError:
        print('\n ERROR: Enter value between -90 and 90 ')

distance = input('\nDo you require the distance between two locations (y or n)? ')
if distance == 'y':
    my_long2 = float(input('For distance, enter second longitude in decimal degrees: '))
    my_lat2 = float(input('Enter second latitude in decimal degrees: '))

if distance == 'y':
    dbp = calc_geo_dist(my_long1, my_lat1, my_long2, my_lat2)
    print('The distance between the two points = {} km'.format(str(dbp)))
# lon1 = -2.3372222, lat1 = 48.8363889; lon2 = 77.0655556, lat 2 = 38.9213889. Should be 6181.63 km
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
#2
The single char variable names are totally incomprehensible.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
#3
Here's a site for great circle calculation of distance between two points:
http://www.movable-type.co.uk/scripts/gis-faq-5.1.html
#4
(Oct-04-2016, 10:09 PM)wavic Wrote: The single char variable names are totally incomprehensible.

True, but I kept them to preserve the actual formulas as written in the book
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
#5
(Oct-05-2016, 12:23 AM)sparkz_alot Wrote:
(Oct-04-2016, 10:09 PM)wavic Wrote: The single char variable names are totally incomprehensible.

True, but I kept them to preserve the actual formulas as written in the book

i code like that a lot, even without a book. i just pretend there is a book.

really, for small parts of code i use single letter variables:

for a in args:

    print( a )
something is inserting an empty line in there.  i removed it and it came back. WTF?

the single character variables is probably from my old Math/Fortran days.  i remember the COBOL people complaining about that.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
#6
sparkz_alot that is awesome! I used Meuus' book a couple of times, but never thought about Pythonizing™ the code, so well done =)) I will follow your efforts if you will be updating us ;)
#7
(Oct-05-2016, 05:36 AM)Skaperen Wrote:
(Oct-05-2016, 12:23 AM)sparkz_alot Wrote:
(Oct-04-2016, 10:09 PM)wavic Wrote: The single char variable names are totally incomprehensible.

True, but I kept them to preserve the actual formulas as written in the book

i code like that a lot, even without a book. i just pretend there is a book.

really, for small parts of code i use single letter variables:

for a in args:

    print( a )
something is inserting an empty line in there.  i removed it and it came back. WTF?

the single character variables is probably from my old Math/Fortran days.  i remember the COBOL people complaining about that.

lol, I remember those days also as well as the good old 'goto' statement...I miss that :-)

In this case though, I admit I have no knowledge of astronomy other than the sun rises in the east and sets in the west :) . For those that have that expertise, apparently the variables have a specific meaning, much like mathematicians, physicists, etc have their own notations.

(Oct-05-2016, 06:19 AM)j.crater Wrote: sparkz_alot that is awesome! I used Meuus' book a couple of times, but never thought about Pythonizing™ the code, so well done =)) I will follow your efforts if you will be updating us ;)

Actually, I have a number of them converted and I'm slowly (and I do mean slowly) working on the more difficult ones. Difficult in the sense that they rely on databases, some rather large, that I have to recreate by hand, which is quite mind numbing :wall: . Right now, all the formulas are in two files plus the database files, though I suppose I could break out the less complicated ones and post them here. Be warned though, wavic, there are a lot of single character variables :D
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
#8
sparkz_alot

Did you happen to tinker with astrodynamics too? And if yes, have you tested the Python SGP4 implementation?
#9
(Oct-05-2016, 02:44 PM)j.crater Wrote: sparkz_alot

Did you happen to tinker with astrodynamics too? And if yes, have you tested the Python SGP4 implementation?

No I have not, though it does look interesting. The only 'real' astronomy library I installed was
pyephem by Brandon Mills, and that was (and is) only to compare my Meuus results with a more modern system. I will say, this undertaking has sparked in interest in astronomy and cosmology that I did not have before, even though it only started as a way to better understand and learn Python :D .

Will definitely check out your suggestion, thanks
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
#10
The code is small no problem at all with this one char vars. The problem is with me. I don't understand what each of the assignments is doing.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020