2015-02-03 20 views
1

나는 상점 목록이 들어있는 간단한 geodjango app을하려고하는데, 주소를 입력하면 가장 가까운 상점이 사용자 위치에 반환됩니다.Django + SpatiaLite + srid + distance queries

나는 PostgreSQL과 Postgis를 사용한 튜토리얼을 사용하고 있지만 SQLite와 SpatiaLite를 사용하고자한다. (나중에 SQLite를 사용하는 다른 응용 프로그램에서 사용하고 싶다. 그래서 내가 엉망이 될지 모르겠다. PostgreSQL로 바꾸려고하면 보통 Python, SQLite, PostgreSQL과 함께 작업하지 않습니다.

쿼리를 만들고 거리를 계산할 때 문제가 있습니다. 내 models.py에서

내가 가진 :

from django.contrib.gis.db import models as gis_models 
from django.contrib.gis import geos 
from django.db import models 

class Shop(models.Model): 
    name = models.CharField(max_length=200) 
    address = models.CharField(max_length=100) 
    city = models.CharField(max_length=50) 
    location = gis_models.PointField(u"longitude/latitude", geography=True, blank=True, null=True) 
    gis = gis_models.GeoManager() 
    objects = models.Manager() 

그리고 views.py에 내가 가진 기능 :

def get_shops(longitude, latitude): 
    current_point = geos.fromstr("POINT(%s %s)" % (longitude, latitude)) 
    distance_from_point = {'km': 10} 
    shops = models.Shop.gis.filter(location__distance_lte=(current_point, measure.D(**distance_from_point))) 
    shops = shops.distance(current_point).order_by('distance') 
    return shops.distance(current_point) 

내가 얻을 오류 :

SQLite does not support linear distance calculations on geodetic coordinate systems.

SRID에 대해 읽었습니다. 내 모델을 변경하십시오. 하지만 어떻게 쓰는지 모르겠습니다. 그리고 정렬 된 데이터를 얻는 방법에 문제가있을 가능성이 있습니다.

답변

0

여러분의 models.py에 문제가 있다고 생각합니다. 모델 관리자는 여전히 기본 모델 관리자입니다. 모델은 다음과 유사해야합니다.

from django.contrib.gis.db import models 
from django.contrib.gis import geos 


class Shop(models.Model): 
    name = models.CharField(max_length=200) 
    address = models.CharField(max_length=100) 
    city = models.CharField(max_length=50) 
    location = models.PointField(u"longitude/latitude", geography=True, blank=True, null=True) 
    objects = models.GeoManager() 

그래서이 모델은 geodjango 모델 - 관리자와 동료입니다. 또한 다른 소스에서 modelfield를 가져올 필요가 없습니다.

다른 문제 SQLite does not support linear distance calculations on geodetic coordinate systems srid를 4326 (WGS84)과 같은 것으로 설정하면 제거 할 수 있습니다. 좀 더 자세한 정보는 here입니다. 또는 삼각 측량을 기반으로 거리 계산을 구현하십시오. 자세한 설명은 this을 참조하십시오.

+0

나는 두 명의 관리자가 있으며 그들은 일합니다. 나는 변경했다 : PointField (srid = 4326, blank = True, null = True) : 상점을 추가 할 수 있지만 PointField (u "경도/위도", geography = True, 빈 = True, null = True) 여전히 선형 거리 계산을 지원하지 않습니다. SRID를 3857로 변경하면 거리별로 정렬되지만 추가는하지 않습니다. – ajgoralczyk

+0

내 실수. 나는이 라인'models.Shop.gis.filter (location_distance_lte = (current_point, measure.D (** distance_from_point))')를 보면서 올바른 필터를 쿼리했다. 그래서 문제는 모델이 아닙니다. 내 대답을 업데이트합니다. –