2012-07-19 3 views
5

私はgeodjangoを使用しており、データベースにポイントのコレクションを持っています。私はこれを使用して、特定のエリア内の点のクエリセットを取得するには:geodjangoを使用してポイントから最低距離のレコードを返す方法は?

queryset = Spot.objects.filter(point__distance_lte=(origin, distance_m)) 

私の質問は、私は、私はそれに合格した時点から一点のみ(最低距離となる点)を返すことができる方法ですか?

EDIT

私は座標を渡すと、彼らとPointオブジェクトを作成したいのですことを言及すべきです。その点を起点として渡し、それに対してフィルタリングします。

from spots.models import * 
from django.contrib.gis.geos import * 

origin = Point(28.011030, -26.029430) 
distance_m = 1000 

queryset = Spot.objects.filter(point__distance_lte=(origin, distance_m)) 
for q in queryset: 
    print q.distance 

このコードスニペットは、私は、このエラーを与える:興味深いことに

Traceback (most recent call last): 
    File "<console>", line 2, in <module> 
AttributeError: 'Spot' object has no attribute 'distance' 

私は次の操作を実行した場合:

origin = Spot.objects.get(name='Montecasino').point 
distance_m = 1000 

for city in Spot.objects.distance(origin): 
    print(city.name, city.distance) 

(u'Design Quarter Parking', Distance(m=677.347841801)) 
(u'Montecasino', Distance(m=0.0)) 
(u'Fourways', Distance(m=1080.67723755)) 

答えて

4

最後に、GeoDjango distance filter with distance value stored within model - queryの手がかりから集められた解決策は、このpostにつながりました。この情報から、私はあなたを集めることができました測定パラメータを遠隔照会で指定する必要があります。 以下のスニペットでは、をDとしてインポートします。その後、クエリで使用します。

ValueError: Tuple required for `distance_lte` lookup type. 

私はソートする方法の例を探していた私は

from spots.models import * 
from django.contrib.gis.geos import * 
from django.contrib.gis.measure import D 

distance_m = 20000 
origin = Point(28.011030, -26.029430) 

closest_spot = Spot.objects.filter(point__distance_lte=(origin, D(m=distance_m))).distance(origin).order_by('distance')[:1][0] 
+0

最後に' [:1] [0] 'がある理由を説明できます – Sevenearths

+1

デバッグする前にこの手順を実行してください:[https://docs.djangoproject.com/en/1.6/ref/contrib/gis/db-api /#互換表]ちょうどポイント_distance_lte Mysqlのためにサポートされていないときに無意識にデバッグの時間を費やした – PKaura

0
queryset = Spot.objects.distance(origin).filter(distance__lte=distance_m) 
point = queryset.order_by('distance')[:1][0] 

https://docs.djangoproject.com/en/dev/ref/contrib/gis/geoquerysets/#distance

を私が試してみました。例えば
+2

あなたの提案を試してみる私はエラーが発生します。 'FieldError:キーワード 'distance'をフィールドに解決できません。選択肢は、author、created_on、id、name、point、slug、updated_on'です。私のモデルには距離フィールドはありません。 – darren

+0

@darrenあなたは 'objects = GeoManager()'を忘れたときに起こるかもしれません。これは 'from django.contrib.gis.db.models.manager import GeoManager'からの設定です。データベースは' 'ENGINE ''を使うことを忘れないでください: django.contrib.gis.db.backends。postgis '、 ' – andi

0

order_by('distance')[:1][0]を使用最低の距離でちょうどポイントを取るために:あなたはそれを指定しない場合 このエラーが発生しますgeodjangoを使用して場所を特定しているので、私のユースケースはこの場所に非常に近いものでした。容認された解決策は機能しましたが、大きなデータセット(140000行以上)のパフォーマンスは非常に悪いものでした。

短編小説:distance_lte関数は、テーブルの各行の原点までの距離を計算する必要があり、ジオインデックスを使用できません。 dwithin機能は、このようなインデックスを使用することができ、それは方法より効率的ですので、制限が行われる前に、実際に前に各行の原点までの距離を計算する必要がないことが表示されます:

origin = Point(28.011030, -26.029430) 
closest_spot = Spot.objects.filter(point__dwithin=(origin, 1)) \ 
    .distance(origin).order_by('distance')[:1][0] 

dwithin機能地理的データの度合いで処理されます(クエリの「1」)。

関連する問題