具有覆盖世界的六边形网格坐标的表 [英] Table with coordinates of a hexagonal grid that covers the world
问题描述
寻找PostGIS中的实现方式,以生成一个覆盖整个星球的六边形网格,以便汇总每个六边形上的数据.
Looking for an implementation in PostGIS for generating a hexagonal grid that covers the the whole planet in order to aggregate data over each hexagon.
任何指向正确方向的指针都会有很大帮助!
Any pointer in the right direction would be of great help!
最终产品: -包含覆盖整个世界的六边形网格中每个六边形的中心点的表. -六边形的面积固定
End product: - A table containing the center point for each hexagon in a hexagonal grid that covers the whole world. - The hexagons have a fixed area
推荐答案
Some time ago I adapted a function
to generate hexagons that might be exactly what you're looking for. It takes the parameters cell width, and the coordinates for southwest and northeast corners, and generates a hexagonal grid.
CREATE OR REPLACE FUNCTION create_hexagons(width FLOAT, xmin FLOAT, ymin FLOAT, xmax FLOAT, ymax FLOAT)
RETURNS TABLE (_gid INTEGER, _geom GEOMETRY) AS $$
DECLARE
b FLOAT := width/2;
a FLOAT := b/2;
c FLOAT := 2*a;
height FLOAT := 2*a+c;
ncol FLOAT := ceil(abs(xmax-xmin)/width);
nrow FLOAT := ceil(abs(ymax-ymin)/height);
polygon_string VARCHAR := 'POLYGON((' ||
0 || ' ' || 0 || ' , ' || b || ' ' || a || ' , ' || b || ' ' || a+c || ' , ' || 0 || ' ' || a+c+a || ' , ' ||
-1*b || ' ' || a+c || ' , ' || -1*b || ' ' || a || ' , ' || 0 || ' ' || 0 || '))';
BEGIN
CREATE TEMPORARY TABLE tmp (gid serial NOT NULL PRIMARY KEY,geom GEOMETRY(POLYGON)) ON COMMIT DROP;
INSERT INTO tmp (geom)
SELECT ST_Translate(geom, x_series*(2*a+c)+xmin, y_series*(2*(c+a))+ymin)
FROM generate_series(0, ncol::INT, 1) AS x_series,
generate_series(0, nrow::INT,1 ) AS y_series,
(SELECT polygon_string::GEOMETRY AS geom
UNION
SELECT ST_Translate(polygon_string::GEOMETRY, b, a + c) AS geom) AS two_hex;
ALTER TABLE tmp ALTER COLUMN geom TYPE GEOMETRY(POLYGON, 4326) USING ST_SetSRID(geom, 4326);
RETURN QUERY (SELECT gid, geom FROM tmp);
END;
$$ LANGUAGE plpgsql;
此函数返回一个包含列_gid
和_geom
的表,分别包含每个六边形的标识符和几何形状.
This function returns a table with the columns _gid
and _geom
, containing an identifier and the geometry for each hexagon, respectively.
CREATE TABLE t AS
SELECT * FROM create_hexagons(1.0, -180, -90, 180, 45)
使用这些参数,该函数将生成一个网格,该网格具有覆盖整个世界的 98192 个六边形:
With these parameters, the function generates a grid with 98192 hexagons covering the whole world:
再靠近一点,以便您可以看到网格:
Here a bit closer, so that you can see the grid:
如果您只想覆盖土地,则可以使用 ST_Intersects
:
If you're only interested in covering land, you can create a subset of these hexagons based on a geometry of your choice using ST_Intersects
:
CREATE TABLE t_overlap AS
SELECT t._gid,t._geom FROM t,world
WHERE ST_Intersects(world.geom,t._geom)
此查询将创建一个包含 35911 六边形的网格的子集,该六边形与世界地图中的几何图形相交:
This query will create a subset with a grid containing 35911 hexagons, which intersect with the geometries from the world map:
此答案中使用的世界地图可以下载为shapefile here
.
The world map used in this answer can be downloaded as shapefile here
.
最终产品:-包含每个六边形的中心点的表格 覆盖整个世界的六角形网格. -六边形有一个 固定区域
End product: - A table containing the center point for each hexagon in a hexagonal grid that covers the whole world. - The hexagons have a fixed area
为每个六边形生成质心也不是什么大事(请参见 ST_Centroid
):
Generating the centroids for each hexagon isn't a big deal either (see ST_Centroid
):
CREATE TABLE t_overlap_centroid AS
SELECT ST_Centroid(_geom) FROM t_overlap;
这篇关于具有覆盖世界的六边形网格坐标的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!