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

发布于 2024-12-23 04:57:21 字数 152 浏览 2 评论 0原文

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

I am using PostGis for Location based calculations in my Application. In a Table i have a column called 'location' in geography type(Point(lon lat))...Like this number of rows present in the Table.
I want to pass a point(Point(lon lat)) and check distance between this point(i passed) and location column in all rows....and if distance is less than 5 m....it will return the name of the point.How to query this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

层林尽染 2024-12-30 04:57:21

假设您的数据 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 上一个问题

简短回答:地理是一种支持长范围的新数据类型
距离测量。如果你使用地理——你不需要
了解有关平面坐标系的更多信息。地理一般是
如果您只关心测量距离和长度并且您是最好的
有来自世界各地的数据。几何数据类型是较旧的数据
具有多种功能支持并享有大力支持的类型
来自第三方工具。如果你很舒服的话那就最好了
空间参考系统或者您正在处理本地化数据
您的所有数据都适合单个空间参考系统 (SRID),或者您
需要做大量的空间处理。请参阅第 8.8 节“PostGIS
功能支持矩阵”,查看当前支持哪些功能以及哪些功能
不是。

Assuming that your srid of your data is 4326 the query you are looking for is:

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

Note that the units(0.0008) in ST_DWithin are in the same units of your projection, in the 4326 case they are degrees. If your projection data is in meters, you will be able to use meters.

For a production application you should use geometry types, is faster. From a stackoverflow previous 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.

っ〆星空下的拥抱 2024-12-30 04:57:21

太好了,谢谢。它在数据库中运行良好。我有以下来自 PHP 的代码..它返回类似,查询失败:
$locationresult=pg_query($con,"SELECT id,name FROM gps.locationnames WHERE ST_DWithin(location,ST_GeographyFromText('POINT(lon lat)'),500)") or die ('查询失败:'.pg_last_error($con ));

这里有什么问题..

Great.Thank You. It works fine in database. I have following code from PHP..it returs like, Query Failed:
$locationresult=pg_query($con,"SELECT id,name FROM gps.locationnames WHERE ST_DWithin(location,ST_GeographyFromText('POINT(lon lat)'),500)") or die ('Query Failed:'.pg_last_error($con));

What is the problem here..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文