|
| 1 | +## PostgreSQL Polygon Averaging in PostGIS - 多边形叠加统计 |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2020-04-12 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , postgis , 多边形 , 叠加 , 统计 |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | +https://info.crunchydata.com/blog/polygon-averaging-in-postgis |
| 16 | + |
| 17 | +Let's say we received polygons from 10 users. If we used ST_Intersection on those polygons, the remaining polygon would only represent the points included in all 10 polygons. If we used ST_Union, the output would represent the points included in at least 1 polygon. |
| 18 | + |
| 19 | +Can anyone recommend a way to output a polygon that represents the points that are in n polygons, where n is greater than 1 and less than the total number of polygons (10 in this case)? |
| 20 | + |
| 21 | +这个问题是要知道在多个多边形叠加后, 在多边形内取一个点, 这个点到底被多少个多边形覆盖到了, 切出一个个的面, 每个面有多少个多边形叠加. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +``` |
| 26 | +WITH |
| 27 | +edges AS |
| 28 | + ( SELECT |
| 29 | + (ST_Dump( |
| 30 | + ST_UnaryUnion( |
| 31 | + ST_Collect( |
| 32 | + ST_ExteriorRing(p.geom))))).geom |
| 33 | + FROM polygons p ) |
| 34 | +``` |
| 35 | + |
| 36 | +This requires a few functions: |
| 37 | + |
| 38 | +- [ST_ExteriorRing](https://postgis.net/docs/ST_ExteriorRing.html) to get the polygon boundary, |
| 39 | +- [ST_UnaryUnion](https://postgis.net/docs/ST_UnaryUnion.html) to mash all the rings together and break them at crossing points, and S |
| 40 | +- [ST_Dump](https://postgis.net/docs/ST_Dump.html) to convert the output of the union (a multi-linestring) back into one row per geometry. |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +``` |
| 45 | +parts AS ( |
| 46 | + SELECT (ST_Dump(ST_Polygonize(geom))).geom FROM edges |
| 47 | +) |
| 48 | + |
| 49 | + |
| 50 | +parts_count AS ( |
| 51 | + SELECT parts.geom, count(*) |
| 52 | + FROM parts |
| 53 | + JOIN polygons p |
| 54 | + ON ST_Intersects(p.geom, ST_PointOnSurface(parts.geom)) |
| 55 | + GROUP BY parts.geom |
| 56 | +) |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +``` |
| 62 | +SELECT ST_Union(geom) AS geom, 4 as count |
| 63 | +FROM parts_count |
| 64 | +WHERE count > 4 |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +``` |
| 70 | +CREATE TABLE average AS |
| 71 | +WITH |
| 72 | +edges AS ( |
| 73 | + SELECT (ST_Dump(ST_UnaryUnion(ST_Collect(ST_ExteriorRing(p.geom))))).geom |
| 74 | + FROM polygons2 p |
| 75 | +), |
| 76 | +parts AS ( |
| 77 | + SELECT (ST_Dump(ST_Polygonize(geom))).geom FROM edges |
| 78 | +), |
| 79 | +parts_count AS ( |
| 80 | + SELECT parts.geom, count(*) |
| 81 | + FROM parts |
| 82 | + JOIN polygons2 p |
| 83 | + ON ST_Intersects(p.geom, ST_PointOnSurface(parts.geom)) |
| 84 | + GROUP BY parts.geom |
| 85 | +) |
| 86 | +SELECT ST_Union(geom) AS geom, 4 as count |
| 87 | +FROM parts_count |
| 88 | +WHERE count > 4 |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +#### [免费领取阿里云RDS PostgreSQL实例、ECS虚拟机](https://www.aliyun.com/database/postgresqlactivity "57258f76c37864c6e6d23383d05714ea") |
| 95 | + |
| 96 | + |
| 97 | +#### [digoal's PostgreSQL文章入口](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae") |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
0 commit comments