如何查询5米范围内的所有数据?

ero*_*ros 3 python postgresql postgis geodjango

我正在使用GeoDjango和PostGIS.然后我在如何查询我的postgres数据库表以获取5米范围内的所有数据时遇到麻烦.

UPDATES1 我正在使用GeoDjango 1.2.7

我从这个网址找到了一些内容https://docs.djangoproject.com/en/dev/ref/contrib/gis/geoquerysets/#std:fieldlookup-distance_lte

Zipcode.objects.filter(poly__distance_lte =(geom,D(*m*= 5)))

但不知道如何准备参数和变量.

  1. 什么是poly_distance_lte?是一个功能?
  2. 什么是geom?是一个变量?怎么创造它?
  3. 什么是D?是一个功能?如果是,mD函数的参数名称?

und*_*ark 10

通常,这种查询的最佳PostGIS函数是ST_DWithin():

如果几何在彼此的指定距离内,则返回true.

例如.所有居住在距离商店#1 1000米范围内的客户:

SELECT customers.* 
FROM customers, shops
WHERE ST_DWithin(customers.the_geog, shops.the_geog, 1000)
  AND shop.id = 1
Run Code Online (Sandbox Code Playgroud)

ST_DWithin将使用您应该创建的空间索引,因此优于ST_Distance.

在Django中似乎有一个名为dwithin的相应过滤器:

返回模型,其中查找几何体与几何体字段的距离在彼此给定的距离内.

Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))
Backend   SQL Equivalent
PostGIS   ST_DWithin(poly, geom, 5)
Run Code Online (Sandbox Code Playgroud)

D(m = 5)返回长度为5米的距离物体

geom是要从中计算Zipcode对象距离的几何体

dwithin()是使用的函数

poly是Zipcode对象的几何属性

z = Zipcode(code=77096, poly='POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))')
Run Code Online (Sandbox Code Playgroud)

  • 用于解释距离与内部的+1并且更快(因为django docs中distance_lte和dwithin的语法和用法是相同的,并且它没有给出任何提示使用的内容) (3认同)