2017-10-13 5 views
0

ポストギスとジオジャンゴを使用して、指定された座標から近くのレストランを表示する機能を実装しました。しかし、私は、kmまたはmの距離を、ユーザーまたは指定された座標から近くにある距離に基づいて見つける必要があります。私は距離に関する質問がSOで尋ねられますが、これは少し異なります。私はレストランのリスト(リストビュー)を表示しています。レストランの詳細ではなく、特定のレストランの場所をIDから表示します。だから私は今レストランのリストビューで各レストランの距離をどのように表示するべきか考えが必要です。各レストランの距離をキロメートルまたはmで表示する

私の考えは、私はここで

from django.contrib.gis.geos import GEOSGeometry 
pnt = GEOSGeometry('SRID=4326;POINT(40.396764 -3.68042)') 
pnt2 = GEOSGeometry('SRID=4326;POINT(48.835797 2.329102 )') 
pnt.distance(pnt2)*100 

を行うことによって距離を計算するためのコンテキストと使用テンプレートフィルタとして(私はURLから渡していていること)緯度とLNG詳細

内のコードで渡す必要があります
def nearby_restaurant_finder(request, current_lat, current_long): 
    from django.contrib.gis.geos import Point 
    from django.contrib.gis.measure import D 

    user_location = Point(float(current_long), float(current_lat)) 
    distance_from_point = {'km': 500} 
    restaurants = Restaurant.gis.filter(
     location__distance_lte=(user_location, D(**distance_from_point))) 
    restaurants = restaurants.distance(user_location).order_by('distance') 
    context = { 
     'restaurants': restaurants 
    } 
    return render(request, 'restaurant/nearby_restaurant.html', context) 


url(r'^nearby_restaurant/(?P<current_lat>-?\d*.\d*)/(?P<current_long>-?\d*.\d*)/$', 
     views.nearby_restaurant_finder, name="nearby-restaurant"), 


{% block page %} 
    {% for restaurant in restaurants %} 
     <h1>Nearby Restaurants are:</h1> 
     <h3>{{ restaurant.name }}</h3> 
     {% empty %} 
     <h3>No Match Found</h3> 
    {% endfor %} 
{% endblock %} 

どのように私は

答えて

1

私はあなたがほとんどそこだと思うそれを行う必要がある上、あなたのアイデアを共有してください。私は、Pythonを使用して距離を計算し、テンプレートをフィルタを作成する代わりに表示します。

私は最初の辞書または類似のリストにコンテキストを更新します:

def calculate_distance(restaurant_location, current_lat, current_long): 
    # this function should return the distance of the restaurant from the user 
    return distance_calculated 

def nearby_restaurant_finder(request, current_lat, current_long): 
    from django.contrib.gis.geos import Point 
    from django.contrib.gis.measure import D 

    user_location = Point(float(current_long), float(current_lat)) 
    distance_from_point = {'km': 500} 
    restaurants = Restaurant.gis.filter(location__distance_lte=(user_location, D(**distance_from_point))) 
    restaurants = restaurants.distance(user_location).order_by('distance') 

    # create a list of dictionaries with results to display 
    ctx_restaurants = [ 
     { 
      'name': restaurant.name, 
      'distance_from_user': calculate_distance(restaurant.location, current_lat, current_long) 
     } 
     for restaurant in restaurants 
    ] 

    # pass results into context 
    context = { 
     'restaurants': ctx_restaurants 
    } 
    return render(request, 'restaurant/nearby_restaurant.html', context) 

その後、私はテーブルのいくつかの並べ替えTDDを使用して

{% block page %} 
    <h1>Nearby Restaurants are:</h1> 
    <table> 
    {% for restaurant in restaurants %} 
     <tr> 
     <td>{{ restaurant.name }}</td> 
     <td>{{ restaurant.distance_from_user}}</td> 
     </tr> 
    {% endfor %} 
    </table> 
{% endblock %} 

にテンプレートでこれをレンダリングします: ので、 calculate_distance()はデカップリングされています。既知の距離を渡すことでテストします。 the testing docs

from django.test import TestCase 
from myapp.views import calculate_distance 

class DistanceTests(TestCase): 
    def setUp(self): 
     self.known_cases = [ 
      {'location': XX1, 'lat': XX1, 'long': XX1, 'expected': XX1}, 
      {'location': XX2, 'lat': XX2, 'long': XX2, 'expected': XX2}, 
     ] 

    def test_calculate_distance(self): 
     for case in self.known_cases: 
      self.assertEquals(
       calculate_distance(case['location'], case['lat'], case['long']), 
       case['expected'] 
      ) 
+0

あなたの解決策にはありがとうございます。私は距離を表示するために次のことをしましたが、これが動作しているかどうかわかりません。どうすればそれを知ることができますか? https://gist.github.com/SanskarSans/fe05e1d28552b9f5cc077e0ffa23a2f4 – Serenity

+0

前のコードの回答と比較してください。あなたがURLを打つとどうなりますか? – zsoobhan

+0

解決策に問題はありません。私はちょうど計算された距離が正しいかどうかを特定するためのヒントを求めています:)。また、距離計算のコードに関する提案はありますか? – Serenity

関連する問題