如何使用Postgres找到2公里半径的左长

发布于 2025-02-12 15:16:16 字数 148 浏览 0 评论 0原文

我有一个邮政表,其中存储为点(几何数据类型)。我有兴趣用2公里半径查询给定纬度值的所有行。另外,我希望为此有一个合适的数据类型,目前我将这些值存储为点。但是在一些调查中,我发现这里使用多边形。但是,即使我无法实现结果。

任何一点都可以用合适的GTS功能来实现确切的查询

I have a postgres table with lat-long values stored as point (Geometric data type). I am interested to query all rows with 2km radius for the given lat-long values. Also, I am expecting for a suitable datatype for this, currently I stored these values as POINT. But on some investigation, I found to use POLYGON here. But even though I couldn't able to achieve the results what expected.

Can any one point me the exact query with suitable GTS functions to achieve this

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

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

发布评论

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

评论(2

清欢 2025-02-19 15:16:16

https://postgis.net/works.net/workshops/workshops/postgis-postgis-inpostgis-intro/spatial_ial_ial_relatial_htial_htriphatss.htriphatss.htriationss.hthtphipss.htriationss.html
示例说明:

SELECT name
FROM nyc_streets
WHERE ST_DWithin(
        geom,
        ST_GeomFromText('POINT(583571 4506714)',26918),
        10
      );
  • 第一个“ GEOM”请参阅NYC_Streets中的列名。
  • ST_GEOMFROMTEXT:将文本转换为GEOM。 26918是SRID。
  • 10:10米。

https://postgis.net/workshops/postgis-intro/spatial_relationships.html
example explanation:

SELECT name
FROM nyc_streets
WHERE ST_DWithin(
        geom,
        ST_GeomFromText('POINT(583571 4506714)',26918),
        10
      );
  • The first "geom" refer to the column name in nyc_streets.
  • ST_GeomFromText: transform text to geom. 26918 is srid.
  • 10 : 10 meters.
折戟 2025-02-19 15:16:16

要查询某个半径内的几何形状,您可能想使用 st_dwithin。为了将其与米一起使用,您必须使用具有仪表单位的SRS,例如 epsg:26918在

SELECT *
FROM mytable
WHERE ST_DWithin(ST_MakePoint(1,2)::geography, -- 1=long / 2=lat
                 geom::geography, -- casting the 'geometry' to 'geography'
                 2000); -- 2km radius

​类型,例如polygonlinestring,您可能想使用st_geogfromtext而不是st_makepoint

SELECT *
FROM mytable
WHERE ST_DWithin(ST_GeogFromText('POINT(1 2)'),
                 geom::geography,2000); 

SELECT *
FROM mytable
WHERE  ST_DWithin(ST_GeogFromText('POLYGON((1 1,2 2,3 3,1 1))'),
                  geom::geography,2000); 

请记住,请记住转换的转换几何不仅仅是改变其srid -check st_transform

进一步阅读

To query geometries within a certain radius you might wanna use ST_DWithin. In order to use it with metres you have to either use a SRS that has metre as unit, such as EPSG:26918, or use geography instead of geometry:

SELECT *
FROM mytable
WHERE ST_DWithin(ST_MakePoint(1,2)::geography, -- 1=long / 2=lat
                 geom::geography, -- casting the 'geometry' to 'geography'
                 2000); -- 2km radius

In case you're dealing with different geometry types, such as polygon or linestring, you might wanna use ST_GeogFromText instead of ST_MakePoint:

SELECT *
FROM mytable
WHERE ST_DWithin(ST_GeogFromText('POINT(1 2)'),
                 geom::geography,2000); 

SELECT *
FROM mytable
WHERE  ST_DWithin(ST_GeogFromText('POLYGON((1 1,2 2,3 3,1 1))'),
                  geom::geography,2000); 

Keep in mind that transforming a geometry is much more than just change its SRID - check ST_Transform.

Further reading

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