다음 모델이 있습니다.특정 반경 내에있는 위도와 경도 좌표로 장고 모델을 필터링하는 방법
class Location(models.Model):
name = models.CharField(max_length = 128, blank = True)
address =models.CharField(max_length = 200, blank= True)
latitude = models.DecimalField(max_digits=6, decimal_places=3)
longitude = models.DecimalField(max_digits=6, decimal_places=3)
def __unicode__(self):
return self.name
내 현재 위도 & 경도 인 경우 :
current_lat = 43.648
current_long = 79.404
제가 조사를 좀 해봤 두 위치 사이의 거리 좌표를 계산 Haversine Equation을 가로 질러왔다. 아래는 내가 발견 방정식입니다 : 내가 좋아하는 것
import math
def distance(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
는 10km 반경 내에있는 모든 위치 개체를 반환하기 위해 내가이 모든 위치의 개체를 반환에만 것 같은 방법으로 필터링 할 수있는 방법, 그 이 10km 반경 내에 떨어지는가?
LocationsNearMe = Location.objects.filter(#This is where I am stuck)
내가 그것을 단지 10km 반경 내에 위치 개체를 반환하도록 필터링에 하버 사인 방정식을 구현할 수 어쨌든 있나요?
나는 상세한 답변을 찾고 있습니다. 도움을 감사하십시오.
geodjango를 사용해보고 사용해야합니다. https://docs.djangoproject.com/en/dev/ref/contrib/gis/ – user710907
geodjango를 자세히 살펴보면이 문제에 대한 해결책이 있습니다. b 그것은 geodjango가 필요합니다 : https://gist.github.com/omouse/5623772 –
올바른 방향으로 저를 안내해 주셔서 감사합니다. 솔루션에서 당신은 요점을 지니지 만,이 모델에서는 두 개의 필드로 구분됩니다 : 경도 위도. 답변란에이 사례에 대한 답을 아래에 적어주십시오. – noahandthewhale