我想初始化一个单点的路径。我很困惑如何去做这件事。 GeoPoint是我们创建的另一个带有name,lat和long的类。如果我创建一个新的路径,输出应该是[name(lat,long)。任何帮助都会非常感激.Init很混乱,因为我刚开始使用java中的python。
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)]
"""
答案 0 :(得分:1)
路径将是点/位置的有序列表。根据您的描述,您可以将Path
实现为GeoPoint
个对象的列表。据推测,GeoPoint
类提供了有用的方法,例如,计算自身与另一个GeoPoint
对象之间的距离,或者可能在地图上绘制自己。您不希望丢失该功能,因此没有理由(基于您的问题中的信息)将GeoPoint
转换为其他对象只是为了将其存储在列表中。您无需访问GeoPoint
对象的成员即可执行此操作。
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)
样本使用:
>>> 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
答案 1 :(得分:0)
我猜你的Geopoint
类有一些属性需要获取并添加到路径中的列表中。
可能路径也有某种功能可以为它添加更多点数。在init中,self.points = [start_point]
就足以将第一个点添加到列表中。
某些功能可能会添加以下每一点
def add_point(self, point):
self.points += [point]