Home > other >  passing data into a class
passing data into a class

Time:03-12

Im working on my project for my Intro to python class and I have been trying all day to figure out why I cant get my coordinates to pass into this function, and just keep hitting roadblocks.

class GeoPoint:
    def __init__(self, lat=0, lon=0, description= 'TBD'):
        self.__lat = lat
        self.__lon = lon
        self.description = description

    def SetPoint(self,coords):
        self.lat = coords[0]
        self.lon = coords[1]

    def GetPoint(self):
        return self.__lat, self.__lon

    def SetDescription(self, my_description):
        self.description = my_description

    def GetDescription(self):
        return self.description

    # Function to calculate distance
    def distance(self, toPoint):
        R = 3958.8  # Radius of earth in miles
        lat1 = radians(self.lat)
        lon1 = radians(self.lon)
        lat2 = radians(self.lat)
        lon2 = radians(self.lon)
        dlon = lon2 - lon1
        dlat = lat2 - lat1
        a = sin(dlat / 2) ** 2   cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))
        d = R * c
        return d

    Point = property(GetPoint, SetPoint)
    Description = property(GetDescription, SetDescription)

Above is the class that im trying to use and ive already set 2 locations using the Geopoint class. I ask the user for their coordinates and a description of the location. but anytime i print the userpoint it shows the proper coordinates but if i attempt to do anything with them i just get 0 from the distance function. Im thinking my issue is in the SetPoint function and its not passing to that because im not exactly understanding how to pass the info to that.

# Program Code
user_lat = float(input("Please enter your current latitude. "))
user_lon = float(input("Please enter your current longitude. "))
user_description = str(input("Please enter a description of your Location. "))
userpoint = user_lat, user_lon
user_coords = userpoint.Point

abq_distance = ABQ.distance(user_coords)
tuc_distance = TUC.distance(user_coords)

CodePudding user response:

Maybe you have some typos, but I'm not seeing anyplace where you're correctly creating the GeoPoint class with valid init data:

user_coords = userpoint.Point

If this is supposed to be a reference to GeoPoint, then it should be:

user_coords = GeoPoint(*userpoint)
# or
user_coords = GeoPoint(user_lat, user_lon)

CodePudding user response:

Look at the first 4 lines of the distance function. You are calculating the distance between 2 of the same points! The new function would be:

def distance(self, point2):
        R = 3958.8  # Radius of earth in miles
        lat1 = radians(self.lat)
        lon1 = radians(self.lon)
        lat2 = radians(point2[0])
        lon2 = radians(point2[1])
        dlon = lon2 - lon1
        dlat = lat2 - lat1
        a = sin(dlat / 2) ** 2   cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))
        d = R * c
        return d
  • Related