0

I would like to init a path that is a single point. I am confused on how to go about doing this. A GeoPoint is another class we created that takes a name, lat, and long. If I create a new Path, the output should be [name(lat,long]. Any help would be greatly appreciated. Init is confusing as I have just started using python from java.

class Path :
    def __init__ (self,start_point) :
        """
        Creates a path with a single point.
        >>> Path(GeoPoint('p0',10,12))
        [p0(10,12)]
        >>> Path(GeoPoint('p3',3,1))
        [p3(3,1)]
        """
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • What is confusing to you? The concept of a class? The arguments in init? The task itself? – RedX Feb 19 '15 at 07:14
  • The task itself is rather confusing to me. I do not understand how to make path create a list with the name, latitude, and longitude from the point. – Connor Russell Feb 19 '15 at 07:17

2 Answers2

1

A path would be an ordered list of points/locations. From your description it makes sense that you would implement Path as a list of GeoPoint objects. Presumably the GeoPoint class provides useful methods, for example, to calculate the distance between itself and another GeoPoint object, or perhaps to plot itself on a map. You don't want to lose that functionality, so there is no reason (based on the info in your question) to convert a GeoPoint into some other object just to store it in a list. You won't need to access the members of the GeoPoint object to do this.

from math import radians, cos, sin, asin, sqrt

def haversine(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees).
    Taken from http://stackoverflow.com/a/4913653/21945
    """
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])

    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles
    return c * r

class GeoPoint(object):
    def __init__(self, name, latitude, longitude):
        self.name = name
        self.latitude = latitude
        self.longitude = longitude

    def __unicode__(self):
        return u'{}({},{})'.format(self.name, self.latitude, self.longitude)

    def __repr__(self):
        return self.__unicode__().encode('utf-8')

    def distance_to(self, to):
        return haversine(self.longitude, self.latitude,
                         to.longitude, to.latitude)

class Path(list):
    def __init__(self, *points):
        """ 
        Basically Path is a builtin list, but one that can be initialised with 
        multiple arguments.

        """ 
        super(Path, self).__init__(points)

Sample usage:

>>> sydney = GeoPoint('Sydney', -33.8600, 151.2094)
>>> sydney
Sydney(-33.86,151.2094)
>>> hawaii = GeoPoint('Hawaii', 21.3114, -157.7964)
>>> los_angeles = GeoPoint('Los Angeles', 34.05, -118.25)
>>> new_york = GeoPoint('New York', 40.7127, -74.0059)
>>> london = GeoPoint('London', 51.5072, -0.1275)
>>> dubai = GeoPoint('Dubai', 24.9500, 55.3333)

>>> single_point = Path(sydney)
>>> single_point
[Sydney(-33.86,151.2094)]

>>> world_trip = Path(sydney, hawaii, los_angeles, new_york)
>>> print world_trip
[Sydney(-33.86,151.2094), Hawaii(21.3114,-157.7964), Los Angeles(34.05,-118.25), New York(40.7127,-74.0059)]
>>> world_trip.extend(Path(london, dubai))    # using standard list methods
>>> world_trip.append(sydney)
>>> print world_trip
[Sydney(-33.86,151.2094), Hawaii(21.3114,-157.7964), Los Angeles(34.05,-118.25), New York(40.7127,-74.0059), London(51.5072,-0.1275), Dubai(24.95,55.3333), Sydney(-33.86,151.2094)]

>>> for a, b in zip(world_trip, world_trip[1:]):
...     print '{0.name} to {1.name} : {2:.2f} km'.format(a, b, a.distance_to(b))
Sydney to Hawaii : 8165.01 km
Hawaii to Los Angeles : 4110.87 km
Los Angeles to New York : 3933.91 km
New York to London : 5566.75 km
London to Dubai : 5495.09 km
Dubai to Sydney : 12022.22 km
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • My path init should create a list with a start_point in in it. How would I go about init without adding list to Path (class Path(list)? How do I init Path as a list with a single point? The start point that was given? – Connor Russell Feb 19 '15 at 15:59
  • Just pass one point as shown in the sample usage: `single_point = Path(sydney)`. This creates a `Path` with a single point in it as shown when it is printed. You don't need to explicitly create a list in `Path` because `Path` _is_ a list. – mhawke Feb 19 '15 at 22:08
0

I'm guessing your Geopoint class has some properties that you need to get and add to a list in path.

Probably path also has some kind of function to add more points to it. In init maybe self.points = [start_point] is enough to add the first point to a list.

Every following point is probably added by some function

def add_point(self, point):
  self.points += [point]
RedX
  • 14,749
  • 1
  • 53
  • 76
  • Then maybe also change `__init__` to `__init__(self, *points)` with a `for p in points: self.add_point(p)`. He could then init a `Path` with one (or none, or more) `GeoPoints` as desired. – Nick T Feb 19 '15 at 07:27
  • In the creation of a GeoPoint object, o = GeoPoint('a', 10,12), However GeoPoint is a different class. I need to extract the information from the point and insert it into a list in this format. ['a',(10,12)]. I am just slightly confused because I cannot access a GeoPoint objects parameters. – Connor Russell Feb 19 '15 at 07:30