如何进行通用查询以从 Postgis 几何类型获取点

发布于 2025-01-11 08:41:31 字数 1062 浏览 0 评论 0原文

我的 PostgreSQL 数据库中的一个表中有一个带有 Postgis 扩展名的几何字段,该字段的值具有以下可能性:点、线串和多边形。我想知道是否可以设置一个查询,以便无论点格式如何,我都可以检索要在 WHERE 子句中使用的几何图形中包含的 POINT,以便我可以检索一个与搜索点接近的点的元组。

DDL:

CREATE TABLE public.contribution (
    id serial4 NOT NULL,
    occurrence timestamp(6) NULL,
    risk_damage bool NOT NULL DEFAULT false,
    victims bool NOT NULL DEFAULT false,
    published varchar(1) NOT NULL DEFAULT 'P'::character varying,
    "desc" varchar(500) NULL,
    "local" geometry NOT NULL,
    id_category int4 NOT NULL,
    id_collaborator int4 NULL,
    id_manager int4 NULL,
    created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT contribution_pkey PRIMARY KEY (id)
);

“本地”列支持 POINT、POLYGON 和 LINESTRING,我想进行一个查询,给定坐标的 X 和 Y 值返回具有相关坐标的元组,例如:

Select * from contribution where 10.0000 < local.x < 12000 and 20.0000 < local.y < 22.0000

在这种情况下,我需要知道是否有任何点“局部”几何形状在上述范围内,这就是我所需要的。

I have a geometry field in one of the tables in my PostgreSQL database with the Postgis extension, this field is valued with the following possibilities, POINT, LINESTRING and POLYGON. I wonder if it is possible to set up a query so that regardless of the point format I can retrieve a POINT contained in geometry to be used in a WHERE clause, so that I can retrieve a tuple that has a point close to the searched one.

DDL:

CREATE TABLE public.contribution (
    id serial4 NOT NULL,
    occurrence timestamp(6) NULL,
    risk_damage bool NOT NULL DEFAULT false,
    victims bool NOT NULL DEFAULT false,
    published varchar(1) NOT NULL DEFAULT 'P'::character varying,
    "desc" varchar(500) NULL,
    "local" geometry NOT NULL,
    id_category int4 NOT NULL,
    id_collaborator int4 NULL,
    id_manager int4 NULL,
    created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT contribution_pkey PRIMARY KEY (id)
);

"local" column supports POINT, POLYGON and LINESTRING, I would like to make a query that given a coordinate's X and Y value returns tuples that have the coordinate in question, for example:

Select * from contribution where 10.0000 < local.x < 12000 and 20.0000 < local.y < 22.0000

In this situation I need to know if any point of the "local" geometry is in the above range and this is what I need.

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

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

发布评论

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

评论(1

━╋う一瞬間旳綻放 2025-01-18 08:41:31

不要在 X 和 Y 上使用范围,而是将其转换为多边形并检查此框是否与存储的几何图形相交:

Select * 
from contribution 
where st_intersects(local,
  ST_SetSRID(
   ST_MakeBox2D(
    ST_Point(10, 20),
    ST_Point(12 ,22)),4326);

如果您对 11;21 周围 1 的距离感兴趣,则可以使用 st_dwithin 改为:

Select * 
from contribution 
where st_dwithin(local,
  ST_SetSRID(
   ST_Point(12, 22),
  ,4326),
  1);

Instead of using a range on X and Y, turn it into a polygon and check if this box intersects with the stored geometries:

Select * 
from contribution 
where st_intersects(local,
  ST_SetSRID(
   ST_MakeBox2D(
    ST_Point(10, 20),
    ST_Point(12 ,22)),4326);

And if you were interested in a distance of 1 around 11;21, you could use st_dwithin instead:

Select * 
from contribution 
where st_dwithin(local,
  ST_SetSRID(
   ST_Point(12, 22),
  ,4326),
  1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文