postgresql - 查询以查找点列与 PostGis 中的点之间的距离

标签 postgresql postgis

我在我的应用程序中使用 PostGis 进行基于位置的计算。在表格中,我在地理类型(点(经纬度))中有一个名为“位置”的列...就像表格中存在的行数一样。 我想通过一个点(Point(lon lat))并检查该点(我通过)和所有行中的位置列之间的距离......如果距离小于 5 m ....它将返回名称的点。如何查询这个。

最佳答案

假设您的数据 srid 是 4326,您要查找的查询是:

SELECT the_geom FROM mytable WHERE ST_DWithin(the_geom,ST_GeomFromEWKT("srid=4326;POINT(lon lat)"), 0.0008);

请注意,ST_DWithin 中的单位 (0.0008) 与投影的单位相同,在 4326 的情况下,它们是度数。如果您的投影数据以米为单位,您将能够使用米。

对于生产应用程序,您应该使用几何类型,速度更快。来自 stackoverflow 上一个 question :

Short Answer: geography is a new data type that supports long range distances measurements. If you use geography -- you don't need to learn much about planar coordinate systems. Geography is generally best if all you care about is measuring distances and lengths and you have data from all over the world. Geometry datatype is an older data type that has many functions supporting it and enjoys great support from third party tools. Its best if you are pretty comfortable with spatial reference systems or you are dealing with localized data where all your data fits in a single spatial reference system (SRID), or you need to do a lot of spatial processing. Refer to Section 8.8, “PostGIS Function Support Matrix” to see what is currently supported and what is not.

关于postgresql - 查询以查找点列与 PostGis 中的点之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8657075/

相关文章:

PostgreSQL:随机选择?

json - 如何在postgresql中解析JSON

临时表中的 PostgreSQL 索引

postgresql - 如何使用 Peewee Models 定义几何字段

ruby-on-rails - 如何转换 Shapefile 的坐标?

sql - 使用 postgres 函数从多个表返回插入计数

database - Go 编程语言的 PostgreSQL 驱动程序

postgresql - 将 Postgresql 8.4 迁移到 9.1

python - 如何在 gitlab-ci.yml 中将 postgis 扩展添加到 postgresql 数据库

c++ - 如何在 Postgresql for C++ 中准备语句和绑定(bind)参数