ero*_*ros 13 python postgresql postgis geodjango
我正在使用GeoDjango和PostGIS.然后我就如何从我的postgres db表中的给定坐标获取最近的记录而遇到麻烦.
小智 19
这是使用GeoDjango和PostGIS的答案
点坐标必须是GEOSGeometry对象.使用它
from django.contrib.gis.geos import GEOSGeometry
point = GEOSGeometry('POINT(5 23)')
Run Code Online (Sandbox Code Playgroud)
那么,让我们假设你有一个"餐厅"模型和点的坐标.因此,对于最近的餐厅,只需使用:
Restaurants.objects.distance(point).order_by('distance')[0]
Run Code Online (Sandbox Code Playgroud)
PostGIS 2.0的下一个版本以KNN gist为特色,以获得最近邻的功能.例如:
SELECT ST_Distance(geom, 'SRID=26910;POINT(34.5 -23.2)'::geometry) AS d
FROM mypoints
ORDER BY geom <-> 'SRID=26910;POINT(34.5 -23.2)'::geometry LIMIT 1;
Run Code Online (Sandbox Code Playgroud)
该功能在SVN版本中,您现在可以测试/使用它.
我没有使用 GeoDjango 的经验,但在 PostgreSQL/PostGIS 上你有 st_distance(..) 函数。因此,您可以对结果进行排序st_distance(geom_column, your_coordinates) asc并查看最近的行。
如果您有普通坐标(没有 postgis 几何图形),则可以使用 GeometryFromText 函数将坐标转换为点。
这就是您要找的吗?如果没有,请尝试更明确。
| 归档时间: |
|
| 查看次数: |
4615 次 |
| 最近记录: |