[Translation] Generate grid data based on PostGIS3.1

Data statistics and analysis based on grids is a common method. Compared with natural geographical boundaries and administrative boundaries, the use of grids has the following characteristics:

  • The status of each grid is equal, and there is no distinction between upper and lower levels.

  • The area of ​​each grid is equal.

  • The distance between the center points of two adjacent grid cells is equal.

  • Suitable for transforming data from the "spatial" computational domain to the "non-spatial" domain.

There are actually many ways to generate grid data. For example, in the previous PostGIS, you can write a plpgsql function to quickly generate grids according to the starting point, end point, and cell resolution. Since grid data is more commonly used, PostGIS3.1 directly provides ST_SquareGrid () (square grid) and ST_HexagonGrid() (honeycomb grid) two grid methods.

Slightly different, the grid generated in PostGIS3.1 has unified characteristics:

  • The origin of the generated grid is fixed and points to the plane coordinates (0,0). Note: This origin is based on the actual data. To generate a grid, you generally need to specify a geographic range within which to generate the defined grid. If the input geographic range is the epsg:4326 coordinate system, point to latitude and longitude 0 0 , which points to Mercator's 0 0 if the input is epsg:3857.

  • By specifying the size of the grid unit, the geographic coordinates of the grid can be fixed, and only the cell number is used instead of the real geographic boundary.

ST_SquareGrid()

The ST_SquareGrid(size, bounds) method takes Mercator coordinates 0 0 as the origin, declares the grid size with size, and specifies the geographic range with bounds.

Example sql:

SELECT (ST_SquareGrid(400000, ST_Transform(a.geom, 3857))).* FROMadmin a  WHERE name = 'Brazil';

This example obtains the national border of Brazil, converts it to plane Mercator coordinates, takes 0 0 as the boundary, and uses the bbox of the Brazilian national border as the geographic boundary of the grid to generate a series of grids. The results are as follows:

ST_HexagonGrid()

The parameters of the ST_HexagonGrid(size, bounds) method are completely consistent with the ST_SquareGrid(size, bounds) parameter definitions.

Cellular grids are currently very popular, and have some advantages in some specific scenarios in some cartographic expressions and statistical modeling. They should initially appear in the H3 format proposed by Uber:

H3: Uber’s Hexagonal Hierarchical Spatial Index
https://eng.uber.com/h3/

示例sql:

SELECT (ST_HexagonGrid(100000, ST_Transform(a.geom, 3857))).* FROMadmin a  WHEREname = 'Germany';

结果可视化如下:

格网统计

生成格网一般用于进行统计分析然后汇总可视化的,使用PostGIS时,不需要实际生成网格并存储,而是通过生成器动态创建网格然后与其他空间数据做叠加统计分析。

示例:假设有点数据集places,geom是geometry(Point,4326)类型,该数据集有字段pop_max定义每个palces点的最大人口数,根据蜂窝网格统计汇总每个格网的最大人口数:

SELECTsum(pop_max) as pop_max, hexes.geomFROM    ST_HexagonGrid(4.0,        ST_SetSRID(ST_EstimatedExtent('places', 'geom'), 4326)    ) AS hexesINNERJOIN    places AS pON ST_Intersects(p.geom, hexes.geom)GROUP BY hexes.geom;

可视化结果如下:

这种动态统计分析是非常快速的,结果可以通过Postgis的矢量切片函数做成动态矢量切片服务,也可以使用pg_tileserv(https://github.com/crunchydata/pg_tileserv)做动态矢量切片服务,配合MapboxGL等能快速实现成果可视化。

Guess you like

Origin blog.csdn.net/qq_40098572/article/details/129196261