我想實現基於經度和使用Django和Postgresql的鄰近搜索。這是我擁有的模型的抽象版本。Django模型屬性地理距離
class Item(models.Model):
"""docstring for Item"""
uuid = UUIDField(primary_key=True, auto=True)
name = models.CharField(_("name"), max_length=200)
address = models.CharField(_('street address'), max_length=255, blank=True, null=True)
location = models.CharField(_('location'), max_length=40, blank=True, null=True)
@property
def geo_distance(self, location=None):
if location is not None:
cursor = connection.cursor()
cursor.execute("SELECT geo_distance(CAST('%s')) AS POINT, CAST('%s') AS POINT)" %
self.location, location)
return round(float(cursor.fetchone() * 1.621371192237), 2)
我希望能夠做這樣的事情在views.py:
items = Item.objects.filter(name__istartwith="Anything")
results = []
for item in items:
results.append({
'name': item.name,
'distance': item.geo_distance("(11.23123, -12.123213)")
})
我知道這是不是最好的方法。歡迎任何建議。
你可能想用geodjango和geopy來標記它。 – 2009-12-06 00:07:01