2009-10-08 86 views
0

我有一个Point对象的集合。我想找到任意两点之间最远的距离。想象一个圈出所有这些点。我想知道那个圆的直径。我如何在GeoDjango中做到这一点?GeoDjango任意两点之间的最远距离

编辑:这是我到目前为止有:

>>> r=Route.objects.get(pk=1) 
>>> a=Airport.objects.filter(routebase__route=r) 

>>> # this route represents a few hundred miles flight into mexico and back 
>>> a 
[<Airport: MMDO>, <Airport: KELP>, <Airport: KELP>, <Airport: MMCU>] 

>>> # a multipoint object with all the airports 
>>> mpoint = a.collect() 

>>> # a polygon that represents a ring around the multipoint 
>>> p = mpoint.envelope 

>>> #now I just need to get the diameter of this envelope 
>>> p.length 
19.065994262694986 

???

那是什么单位?这甚至是我追求的价值吗?

EDIT2:好的,我要试试这个另一种方式:

>>> r=Route.objects.get(pk=1) 
>>> a=Airport.objects.filter(routebase__route=r) 

>>> # this route represents a few hundred miles flight into mexico 
>>> a 
[<Airport: MMDO>, <Airport: KELP>, <Airport: MMCU>] 

>>> # a multipoint object with all the airports 
>>> mpoint = a.collect() 

>>> # get the center point of the route polygon and get the 
>>> # distance between each point and the centroid 
>>> # the largest should be the diameter of the ring, right? 
>>> cen = mpoint.centroid 

>>> dist = [] 
>>> for p in mp: 
     dist.append(LineString(p, cen).length) 

>>> dis 
[0.54555421739245946, 
0.61638306853425906, 
0.53442640535933494, 
0.54555421739245946] 

>>> max(dist) 
0.61638306853425906 

?再次,这些单位是什么?

+0

从纯几何观点来看,包围一组点的圆的直径只是最大距离的上限。如果你有三个点,所有的点只相隔0.01度,那么最大距离比所述圆的直径小100倍。 – jva 2009-10-27 15:02:56

回答

2

好吧,我想通了。

def overall_distance(route): 
    a = Airport.objects.filter(routebase__route=route).distinct() 
    mp = a.collect() 
    ct = mp.envelope.centroid 
    na = a.distance(ct) 

    dist = [] 
    for p in na: 
     dist.append(p.distance) 

    diameter = max(dist) * 2 

    return diameter.nm