使用 postgreSQL 和 PostGIS 查询空间交集(点 - 列中的多边形)
Query for spatial intersection (point - polygons in column) using postgreSQL and PostGIS
我正在尝试弄清楚如何创建一个查询,该查询要求多边形内的点。
我的设置是安装了 PostGIS 2.2.2 扩展的 postgreSQL 9.5 实例。
从理论上讲,这可能并不像 PostGIS documentation 中显示的那样非常困难,但我无法得到任何结果。
我创建了以下 table:
CREATE TABLE app_db.testTable
(
message_text text,
message_picture text,
message_date timestamp with time zone DEFAULT now(),
message_id uuid NOT NULL DEFAULT uuid_generate_v4(),
message_position app_db.geometry,
message_radius integer,
message_circle app_db.geometry,
message_userid uuid,
CONSTRAINT messages_pkey PRIMARY KEY (message_id)
)
我已经使用以下命令为我的模式启用了 PostGIS:
CREATE EXTENSION postgis SCHEMA app_db;
我在上面提到的 table 中放了一些示例数据集。
到目前为止一切顺利。
不是我正在尝试创建一个查询,它应该询问所有数据,其中定义的点与存储在我的 table 列 app_db 中的一个几何图形相交-> message.circle:
据我所知,ST_Intersects 需要两个几何图形作为参数,但是如何针对完整的列执行查询?
请注意,通常人们将 postgis
安装到 public
and/or 在 search_path
上安装了架构,因此您不需要提供完整路径,例如 app_db.ST_Intersects
。对于下面的示例,我假设 SET search_path TO app_db;
使其更简单。
从message_circle
中的单个多边形到selectmessage_position
中的所有点:
SELECT p.*
FROM testTable p
JOIN testTable c ON c.message_id='the id of the circle'
AND p.message_id <> c.message_id
AND ST_Intersects(p.message_position, c.message_circle);
我不确定 message_circle
是如何制作的,但如果它是从 message_position
和 message_radius
创建的,那么更好的方法是使用 ST_DWithin,它使用距离阈值,而不是更昂贵和不完美的几何分析。
SELECT p.*
FROM testTable p
JOIN testTable c ON c.message_id='the id of the circle'
AND p.message_id <> c.message_id
AND ST_DWithin(p.message_position, c.message_position, c.message_radius);
可以通过在 message_position
上创建 GiST 空间索引来加快速度。
我正在尝试弄清楚如何创建一个查询,该查询要求多边形内的点。 我的设置是安装了 PostGIS 2.2.2 扩展的 postgreSQL 9.5 实例。 从理论上讲,这可能并不像 PostGIS documentation 中显示的那样非常困难,但我无法得到任何结果。
我创建了以下 table:
CREATE TABLE app_db.testTable
(
message_text text,
message_picture text,
message_date timestamp with time zone DEFAULT now(),
message_id uuid NOT NULL DEFAULT uuid_generate_v4(),
message_position app_db.geometry,
message_radius integer,
message_circle app_db.geometry,
message_userid uuid,
CONSTRAINT messages_pkey PRIMARY KEY (message_id)
)
我已经使用以下命令为我的模式启用了 PostGIS:
CREATE EXTENSION postgis SCHEMA app_db;
我在上面提到的 table 中放了一些示例数据集。 到目前为止一切顺利。
不是我正在尝试创建一个查询,它应该询问所有数据,其中定义的点与存储在我的 table 列 app_db 中的一个几何图形相交-> message.circle: 据我所知,ST_Intersects 需要两个几何图形作为参数,但是如何针对完整的列执行查询?
请注意,通常人们将 postgis
安装到 public
and/or 在 search_path
上安装了架构,因此您不需要提供完整路径,例如 app_db.ST_Intersects
。对于下面的示例,我假设 SET search_path TO app_db;
使其更简单。
从message_circle
中的单个多边形到selectmessage_position
中的所有点:
SELECT p.*
FROM testTable p
JOIN testTable c ON c.message_id='the id of the circle'
AND p.message_id <> c.message_id
AND ST_Intersects(p.message_position, c.message_circle);
我不确定 message_circle
是如何制作的,但如果它是从 message_position
和 message_radius
创建的,那么更好的方法是使用 ST_DWithin,它使用距离阈值,而不是更昂贵和不完美的几何分析。
SELECT p.*
FROM testTable p
JOIN testTable c ON c.message_id='the id of the circle'
AND p.message_id <> c.message_id
AND ST_DWithin(p.message_position, c.message_position, c.message_radius);
可以通过在 message_position
上创建 GiST 空间索引来加快速度。