简体   繁体   English

PostGIS-计算多边形中的点(并在边界内平均其特征)

[英]PostGIS - Count Points in Polygons (and average their features within the boundaries)

I've a table with some points representing buildings : 我有一张桌子,上面有一些点代表建筑物

CREATE TABLE buildings(
  pk serial NOT NULL,
  geom geometry(Point,4326),
  height double precision,
  area double precision,
  perimeter double precision
)

And another table with polylines (most of them closed): 另一个带有折线的表(其中大多数已关闭):

CREATE TABLE regions
(
  pk serial NOT NULL,
  geom geometry(Polygon,4326)
)

I would like to: 我想要:

  1. count the numbers of points inside each regions (buildings_n) 计算每个区域内的点数 (buildings_n)
  2. find the average value of one the features(eg. area) within the regions boundary (area_avg) 查找区域边界 (area_avg) 中一个特征(例如面积)的平均值

Adding the two new columns: 添加两个新列:

ALTER TABLE regions ADD COLUMN buildings_n integer;
ALTER TABLE regions ADD COLUMN area_avg double precision;

How can I do these two queries? 我该如何做这两个查询?

I've tried this one for the point 1, but it fails: 我已经针对第1点尝试过此操作,但失败了:

INSERT INTO  regions (buildings_n)
SELECT  count(b.geom) 
FROM  regions a, buildings b
WHERE st_contains(a.geom,b.geom); 

thank you, 谢谢,

Stefano 斯特凡诺

Region geometry 区域几何

The first problem you have is that ST_Contains with 'polylines' or linestrings only finds the points that are exactly on the linestring's geometry. 您遇到的第一个问题是带有'polylines'或线串的ST_Contains仅找到完全在线串的几何图形上的点。 If you want points within a region represented by a linestring it won't work, especially if these are not closed. 如果要在由线串表示的区域内的点将不起作用,特别是如果这些点未闭合。 See the examples of valid ST_Contains relations here: http://www.postgis.org/docs/ST_Contains.html 在此处查看有效的ST_Contains关系的示例: http : //www.postgis.org/docs/ST_Contains.html

For the spatial relation to work you have to transform the region's geometry to polygons, either beforehand or on the fly in the query. 为了使空间关系正常工作,您必须事先或在查询中即时将区域的几何形状转换为多边形。 For example: 例如:

ST_Contains(ST_MakePolygon(a.geom),b.geom)

See this reference for more info: http://www.postgis.org/docs/ST_MakePolygon.html 有关更多信息,请参见此参考资料: http : //www.postgis.org/docs/ST_MakePolygon.html

Calculate aggregate values 计算合计值

The second problem is that to use the aggregate functions count or average on subsets of the buildings table (and not the entire table) you need to associate the region id with each building... 第二个问题是要对建筑物表的子集(而不是整个表)使用聚合函数计数或平均值,您需要将区域ID与每座建筑物相关联...

SELECT a.pk region_pk, b.pk building_pk, b.area
FROM regions a, buildings b
WHERE ST_Contains(ST_MakePolygon(a.geom),b.geom)

.. and then group your building data by the region they belong to: ..然后按建筑物所属的区域将建筑物数据分组:

SELECT region_pk, count(), avg(area) average
FROM joined_regions_and_buildings
GROUP BY region_pk;

Update new columns 更新新列

The third problem is that you are using INSERT to add values to the newly created columns. 第三个问题是您正在使用INSERT将值添加到新创建的列。 INSERT is for adding new records to a table, UPDATE is used for changing values of existing records in a table. INSERT用于将新记录添加到表中,UPDATE用于更改表中现有记录的值。

Solution

So, all of the points above combined result in the following query: 因此,以上所有点的组合将导致以下查询:

WITH joined_regions_and_buildings AS (
    SELECT a.pk region_pk, b.pk building_pk, b.area
    FROM regions a, buildings b
    WHERE ST_Contains(ST_MakePolygon(a.geom),b.geom)
)
UPDATE regions a
SET buildings_n = b.count, area_avg = b.average
FROM (
    SELECT region_pk, count(), avg(area) average
    FROM joined_regions_and_buildings
    GROUP BY region_pk
    ) b
WHERE a.pk = b.region_pk;

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

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