如何在特定区域中为H3六角形生成shapefile

Yin*_*Cao 6 shapefile h3

我想为特定地理区域中的H3六角形生成shapefile 。特别是,我对分辨率为6、7和9的海湾地区感兴趣。如何为覆盖该区域的六边形创建shapefile?

我是shapefile或任何其他地理数据结构的新手。我对python和R最满意。

raf*_*ira 8

如果您正在寻找 中的解决方案R,该h3jsr软件包提供了对 Uber 的 H3 库的访问。可以使用函数h3jsr::polyfill()和来解决您的问题h3jsr::h3_to_polygon

可重现的例子

library(ggplot2)
library(h3jsr)
library(sf)
library(sf)


# read the shapefile of the polygon area you're interested in
  nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)

# projection
  nc <- st_transform(nc, crs = 4326)

# get the unique h3 ids of the hexagons intersecting your polygon at a given resolution
  nc_5 <- polyfill(nc, res = 5, simple = FALSE)

# pass the h3 ids to return the hexagonal grid
  hex_grid5 <- unlist(nc_5$h3_polyfillers) %>% h3_to_polygon(simple = FALSE)
Run Code Online (Sandbox Code Playgroud)

这将返回以下多边形:

在此处输入图片说明 在此处输入图片说明

  • 你有机会把它变成 Python 的 H3 吗?截至今天,我无法用 R 重现。 (3认同)

nra*_*itz 7

基本步骤如下:

  • 取得所需区域的多边形。边界框应该工作良好。
  • 使用该polyfill方法以所需的分辨率用六边形填充多边形。
  • 循环遍历每个六边形,并使用h3ToGeoBoundary函数获得边界。
  • 将这些边界放入GeoJSON文件中
  • 使用类似的转换器ogr2ogr来转换为shapefile。

Python绑定尚未发布,并且我对R绑定不熟悉,但是JavaScript版本可能如下所示:

var h3 = require('h3-js');

var bbox = [
    [-123.308821530582, 38.28055644998254],
    [-121.30037257250085, 38.28055644998254],
    [-121.30037257250085, 37.242722073589164],
    [-123.308821530582, 37.242722073589164]
];

var hexagons = h3.polyfill(bbox, 6, true);

var geojson = {
    type: 'Feature',
    geometry: {
        type: 'MultiPolygon',
        coordinates: hexagons.map(function toBoundary(hex) {
            return [h3.h3ToGeoBoundary(hex, true)];
        })
    }
};

console.log(JSON.stringify(geojson));
Run Code Online (Sandbox Code Playgroud)

并且您将使用如下脚本:

node bbox-geojson.js | ogr2ogr -f "ESRI Shapefile" bbox-hexagons.shp /vsistdin/
Run Code Online (Sandbox Code Playgroud)