简体   繁体   English

使用postgreSQL和PostGIS查询空间相交(点-列中的多边形)

[英]Query for spatial intersection (point - polygons in column) using postgreSQL and PostGIS

I'm trying to figure out how to create a query, which asks for points within a polygon. 我试图弄清楚如何创建查询,该查询要求在多边形内的点。 My setup is a postgreSQL 9.5 instance with a installed PostGIS 2.2.2 extension. 我的设置是带有已安装PostGIS 2.2.2扩展名的PostgreSQL 9.5实例。 Theoretically this might not be really difficult as it is shown in the PostGIS documentation , but I am not able to get any result. 从理论上讲,这可能并不那么困难,如PostGIS文档中所示,但我无法获得任何结果。

I've created the following 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)
)

I've enabled PostGIS with the following command for my schemata: 我已使用以下命令为架构启用PostGIS:

CREATE EXTENSION postgis SCHEMA app_db;

And I've put some example datasets in the above mentioned table. 我在上述表格中放置了一些示例数据集。 So far so good. 到现在为止还挺好。

Not I'm trying to create a query, which should ask for all data, where the defined point intersects one of the geometries stored in my table column app_db -> message.circle : As far as I've understood ST_Intersects needs two geometries as parameters, but how do I perform a query in regard to a complete column? 不是,我试图创建一个查询,该查询应查询所有数据,其中定义的点与存储在我的表列app_db-> message.circle中的几何图形之一相交:据我了解,ST_Intersects需要两个几何图形,即参数,但是如何执行关于完整列的查询?

Note that normally folks install postgis into public and/or have the installed schema on the search_path so you don't need to provide full paths like app_db.ST_Intersects . 请注意,通常人们将postgis安装到public并且/或者在search_path上具有已安装的架构,因此您不需要提供完整的路径,例如app_db.ST_Intersects For the examples below, I'm assuming SET search_path TO app_db; 对于下面的示例,我假设将SET search_path TO app_db; to make it simpler. 使它更简单。

To select all the points in message_position from a single polygon in message_circle : 要选择所有的点message_position从一个多边形message_circle

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);

I'm not sure how message_circle was made, but if it was created from message_position and message_radius , then a better way is to use ST_DWithin , which uses a distance threshold, rather than a more expensive and imperfect geometry analysis. 我不确定message_circle制作方式,但是如果它是从message_positionmessage_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);

It can be sped up by creating a GiST spatial index on message_position . 可以通过在message_position上创建GiST空间索引来加快它的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM