Python Forum
Distance between two locations when their latitude and longitudes are given - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Distance between two locations when their latitude and longitudes are given (/thread-21073.html)



Distance between two locations when their latitude and longitudes are given - dcardonaa - Sep-12-2019

Hello! I'm trying to run this program and it works until Ellip_earth_proj_plane function.

print('This is a program to find distance between two locations when their latitudes and logitudes are given. Please avoid entering negative numbers, just enter a cardinal direction at the end of the coordenate.')
lat1_degrees=float(input('Please enter Latitud degrees of the first point:', ))
lat1_minutes=float(input('Please enter Latitud minutes of the first point:', ))
lat1_seconds=float(input('Please enter Latitud seconds of the first point:', ))
lat1_cardinal_direction=(input('Please enter Latitud cardinal direction of the first point:', ))



lon1_degrees=float(input('Please enter Longitud degrees of the first point:', ))
lon1_minutes=float(input('Please enter Longitud minutes of the first point:', ))
lon1_seconds=float(input('Please enter Longitud seconds of the first point:', ))
lon1_cardinal_direction=(input('Please enter Longitud cardinal direction of the first point:', ))

lat2_degrees=float(input('Please enter Latitud degrees of the second point:', ))
lat2_minutes=float(input('Please enter Latitud minutes of the second point:', ))
lat2_seconds=float(input('Please enter Latitud seconds of the second point:', ))
lat2_cardinal_direction=(input('Please enter Latitud cardinal direction of the second point:', ))

lon2_degrees=float(input('Please enter Longitud degrees of the second point:', ))
lon2_minutes=float(input('Please enter Longitud minutes of the second point:', ))
lon2_seconds=float(input('Please enter Longitud seconds of the second point:', ))
lon2_cardinal_direction=(input('Please enter Longitud cardinal direction of the second point:', ))

degrees=[lat1_degrees, lon1_degrees, lat2_degrees, lon2_degrees]
minutes=[lat1_minutes, lon1_minutes, lat2_minutes, lon2_minutes] 
seconds=[lat1_seconds, lon1_seconds, lat2_seconds, lat2_seconds]

import math
    
class Distance_Geogr_Points:
    def __init__(self):
        self.lat1=lat1
        self.lon1=lon1
        self.lat2=lat2
        self.lon2=lon2
        self.point1=point1
        self.point2=point2
        self.deltaphi=deltaphi
        self.deltalambda=deltalambda
        self.phimedium=phimedium
        self.K1=K1
        self.K2=K2
            
    def ConvertToDegrees (self, degrees, minutes, seconds):
        if lat1_cardinal_direction=='N' or lat1_cardinal_direction=='n':
            lat1=round((degrees[0]+(minutes[0]/60)+(seconds[0]/3600)),6)
        else:
            lat1=-round((degrees[0]+(minutes[0]/60)+(seconds[0]/3600)),6)
        if lon1_cardinal_direction=='E' or lon1_cardinal_direction=='e':
            lon1=round((degrees[1]+(minutes[1]/60)+(seconds[1]/3600)),6)
        else:
            lon1=-round((degrees[1]+(minutes[1]/60)+(seconds[1]/3600)),6)
        if lat2_cardinal_direction=='N' or lat2_cardinal_direction=='n':
            lat2=round((degrees[2]+(minutes[2]/60)+(seconds[2]/3600)),6)
        else:
            lat2=-round((degrees[2]+(minutes[2]/60)+(seconds[2]/3600)),6)
        if lon1_cardinal_direction=='E' or lon1_cardinal_direction=='e':
            lon2=round((grades[3]+(minutes[3]/60)+(seconds[3]/3600)),6)
        else:
            lon2=-round((degrees[3]+(minutes[3]/60)+(seconds[3]/3600)),6)
        point1=(lat1,lon1)
        point2=(lat2,lon2)
        return point1, point2
    
    #Ellipsoidal Earth projected to a plane: The Federal Communications Commission prescribes the following formulae for 
    #distances not exceeding 475 kilometres (295 mi):
    def Ellip_earth_proj_plane(self, deltaphi, deltalambda, phimedium, K1, K2):
        deltaphi=math.radians(lat2-lat1)
        deltalambda=math.radians(lon2-lon1)
        phimedium=math.radians((lat1+lat2)/2)
        K1=111.13209-(0.56605*math.cos(2*phimedium)+0.00120*math.cos(4*phimedium)
        K2=111.41513*math.cos(phimedium)-0.09455*math.cos(3*phimedium)+0.00012*math.cos(5*phimedium)
        Distance=math.sqrt(((K1*deltaphi)**2)+((K2*deltalambda)**2))
        return (phimedium,'Distance between point 1 and 2 is:', Distance, 'km') 
                     
d=Distance_Geogr_Points()
d.Ellip_earth_proj_plane(deltaphi, deltalambda, phimedium, K1, K2)
I haven't being able to see what is the error.
Error:
File "<ipython-input-65-9b2017f900ad>", line 45 K2=111.41513*math.cos(phimedium)-0.09455*math.cos(3*phimedium)+0.00012*math.cos(5*phimedium) ^ SyntaxError: invalid syntax
I tested and it worked until ConvertToGrades. If someone can help me, I would really appreciate it!


RE: Distance between two locations when their latitude and longitudes are given - ibreeden - Sep-12-2019

Inspect the line K1=... Count the brackets. There should be just as many opening as closing brackets.


RE: Distance between two locations when their latitude and longitudes are given - dcardonaa - Sep-12-2019

Thanks! That helped.. but then I found another error..
Error:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-68-f294b8d435f6> in <module> 47 return (phimedium,'Distance between point 1 and 2 is:', Distance, 'km') 48 ---> 49 d=Distance_Geogr_Points() 50 d.Ellip_earth_proj_plane(deltaphi, deltalambda, phimedium, K1, K2) <ipython-input-68-f294b8d435f6> in __init__(self) 9 self.point1=point1 10 self.point2=point2 ---> 11 self.deltaphi=deltaphi 12 self.deltalambda=deltalambda 13 self.phimedium=phimedium NameError: name 'deltaphi' is not defined



RE: Distance between two locations when their latitude and longitudes are given - jefsummers - Sep-12-2019

Where do you believe you defined deltaphi before the __init__?


RE: Distance between two locations when their latitude and longitudes are given - ThomasL - Sep-12-2019

Have a look here https://docs.python.org/2/tutorial/classes.html
Point 9.3.5. Class and Instance Variables