How do I get the k nearest neighbors for geodjango?
问题 Assume that I have the following model: class Person: id = models.BigAutoField(primary_key=True) name = models.CharField(max_length=150) location = models.PointField() How would I go by obtaining the k nearest neighbors (KNN) by location using geodjango? Would I have to write custom SQL for that? I am using PostgreSQL with PostGIS. 回答1: You can use a raw() sql query to utilize postgis order_by operators: <-> which gets the nearest neighbor using the centers of the bounding boxes to calculate