如何进行通用查询以从 Postgis 几何类型获取点
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在 X 和 Y 上使用范围,而是将其转换为多边形并检查此框是否与存储的几何图形相交:
如果您对 11;21 周围 1 的距离感兴趣,则可以使用
st_dwithin 改为:
Instead of using a range on X and Y, turn it into a polygon and check if this box intersects with the stored geometries:
And if you were interested in a distance of 1 around 11;21, you could use
st_dwithin
instead: