GeoDjango内置。在一个SQL查询中距离beetwen两点

时间:2016-10-04 18:01:06

标签: python django orm geodjango

我有一项任务是找到离公寓最近的城镇。我想这不是一项艰巨的任务,但我是GeoDjango的新手,所以每个人都会帮忙:) 我只能使用一个SQL查询。当然使用django ORM。

from django.contrib.gis.db.models import MultiPolygonField, PointField
from django.db import models


class City(models.Model):
    geo_area = MultiPolygonField(verbose_name='Area')
    center = PointField(verbose_name='center of City')


class Flat(models.Model):
    geo_point = PointField(verbose_name='Coordinates of apartment')

def distance_from_city(self):
    """
    Method calculating distance to the nearest town. 
    In the case where the coordinates of the apartments are located
    in the middle of the city, it counts the distance to the center 
    of the city (City.center) otherwise has the distance to the border 
    town (City.geo_area) closest to the coordinates of the apartment.
    The distance is returned in kilometers
    """
    pass

0 个答案:

没有答案