[英]How to make a generic query to get a point from a Postgis geometry type
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.我的PostgreSQL数据库中的一个表中有一个带有Postgis扩展的几何字段,该字段具有以下可能性:POINT、LINESTRING 和 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.我想知道是否可以设置一个查询,以便无论点格式如何,我都可以检索包含在几何中的 POINT 以用于 WHERE 子句,这样我就可以检索一个元组,该元组的点与搜索到的元组接近.
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: “本地”列支持 POINT、POLYGON 和 LINESTRING,我想查询给定坐标的 X 和 Y 值,返回具有相关坐标的元组,例如:
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.在这种情况下,我需要知道“局部”几何体的任何一点是否在上述范围内,这就是我所需要的。
Instead of using a range on X and Y, turn it into a polygon and check if this box intersects with the stored geometries:不要在 X 和 Y 上使用范围,而是将其转换为多边形并检查此框是否与存储的几何图形相交:
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:如果您对 11 左右的距离感兴趣;21,您可以改用st_dwithin
:
Select *
from contribution
where st_dwithin(local,
ST_SetSRID(
ST_Point(12, 22),
,4326),
1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.