我的PostgreSQL資料庫中的一個表中有一個幾何欄位,帶有Postgis擴展,該欄位的值具有以下可能性,POINT、LINESTRING 和 POLYGON。我想知道是否可以設定一個查詢,以便無論點格式如何,我都可以檢索幾何中包含的 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”列支持 POINT、POLYGON 和 LINESTRING,我想查詢給定坐標的 X 和 Y 值,回傳具有相關坐標的元組,例如:
Select * from contribution where 10.0000 < local.x < 12000 and 20.0000 < local.y < 22.0000
在這種情況下,我需要知道“區域”幾何的任何點是否在上述范圍內,這就是我需要的。
uj5u.com熱心網友回復:
不要在 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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/437313.html
上一篇:為什么在docker(ubuntu:18.04)中運行長時間運行的postgres查詢需要`psycopg2.connect(...)`中的`keepalives`引數?
下一篇:如果存在執行,我如何使用?
