2017-12-01 11 views
1

私は近くのユーザーイベントを表示するWebアプリケーションで作業しています。私は動作する以下のコードがありますが、結果を与えるのが非常に遅いです。 とにかくそれをもっと速くすることができるかどうか疑問に思っていました。現在、5つのイベントの距離を計算するだけで約3秒かかります。ここに私のコードスニペットがあります。計算距離が非常に遅い

@app.route('/events') 
def events(): 

    events = Post.query.filter_by(event=True, trivia=False, approved=True, featured=False).order_by(Post.datetime.asc()).all() 

    geolocator = Nominatim() 

    if current_user.state_1 != None: 
     res_state = current_user.state_1 
    else: 
     res_state = ',' 

    user_city = geolocator.geocode(current_user.city_1 + ' ' + res_state + ' ' + current_user.residence) 

    user_city = (user_city.latitude, user_city.longitude) 

    events_near = [] 

    for event in events: 
     if event.address_2 != None: 
      address_2 = event.address_2+',' 
     else: 
      address_2 = ',' 

     if event.state != None: 
      state = event.state+',' 
     else: 
      state = ',' 

     if event.zip_code != None: 
      zip_code = event.zip_code+'.' 
     else: 
      zip_code = '.' 

     location = geolocator.geocode(event.address_1+',' + ' ' + address_2 + ' ' + event.city+',' + ' ' + state + ' ' + zip_code + ' ' + event.country) 
     location = (location.latitude, location.longitude) 

     distance = geopy.distance.vincenty(user_city, location).miles 


     if distance < dist: 
      events_near.append(event) 

     return render_template('events.html', events_near=events_near) 

助けてください。ありがとう。 pygeocoder:私は少し速いと思われるものに取り組んできました

:使用されるモジュールのOPを使用しないように見ている誰かのために

+0

どのような回線が長くかかるのかご存知ですか? –

+0

@Atto私は実際にどちらの線がわかりませんが、地理的位置がlatとlngがアドレスのために計算されているものを信じています。 – aharding378

+0

'user_city = geolocator.geocode(current_user.city_1 + '' + res_state + '+ current_user.residence)行の変数はどのようなものになるのかの例を挙げることができますか? –

答えて

1

。サンプルコードはこうです:

from pygeocoder import Geocoder 
result = Geocoder.geocode("4207 N Washington Ave, Douglas, AZ 85607") 
coords = result.coordinates 
print(coords) # outputs the (lat, long) of the address as a tuple 

私はこれを利用したいと思っている人が助けてくれることを願っています!