2017-12-01 2 views
1

나는 사용자 이벤트를 보여주는 웹 앱에서 작업 중입니다. 아래 코드는 작동하지만 결과가 매우 느립니다. 어쨌든 더 빨리 만들 수 있는지 궁금합니다. 현재 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 :

나는 약간 빠른 것 같다 하나 같이 일한 : 사용되는 모듈의 영업 이익을 사용하지하고자하는 사람

+0

어떤 라인이 오래 걸리는지 알고 있습니까? –

+0

@Atto 어떤 선이 있는지는 모르겠지만 위도와 위도가 주소에 대해 계산 된 지리적 위치를 가진 선을 믿습니다. – 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 

나는 이것을 사용하고자하는 사람이 도움이되기를 바랍니다!