我有一项任务是找到离公寓最近的城镇。我想这不是一项艰巨的任务,但我是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