我想为特定地理区域的H3六边形生成 shapefile。特别是,我对分辨率为 6、7 和 9 的湾区感兴趣。如何为覆盖该区域的六边形创建 shapefile?
我是 shapefile 或任何其他地理数据结构的新手。我对 python 和 R 最满意。
我想为特定地理区域的H3六边形生成 shapefile。特别是,我对分辨率为 6、7 和 9 的湾区感兴趣。如何为覆盖该区域的六边形创建 shapefile?
我是 shapefile 或任何其他地理数据结构的新手。我对 python 和 R 最满意。
这里的基本步骤是:
polyfill
方法以所需的分辨率用六边形填充多边形。h3ToGeoBoundary
函数的边界。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));
你会像这样使用脚本:
node bbox-geojson.js | ogr2ogr -f "ESRI Shapefile" bbox-hexagons.shp /vsistdin/
如果您正在寻找解决方案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)
这将返回以下多边形:
在这里接受 John Stud 的问题,因为我遇到了同样的“问题”。在下文中,我将评论如何读取 shapefile,使用 H3 对其进行六边形化,并从中获取 Hexagon 地理数据框(并最终将其保存为 shapefile)。
可重现的例子
让我们为美国获取一个 shapefile,例如这里(我使用“cb_2018_us_state_500k.zip”文件)。
# Imports
import h3
import geopandas as gpd
import matplotlib.pyplot as plt
import pandas as pd
import shapely
from shapely.ops import unary_union
from shapely.geometry import mapping, Polygon
# Read shapefile
gdf = gpd.read_file("data/cb_2018_us_state_500k.shp")
# Get US without territories / Alaska + Hawaii
us = gdf[~gdf.NAME.isin(["Hawaii", "Alaska", "American Samoa",
"United States Virgin Islands", "Guam",
"Commonwealth of the Northern Mariana Islands",
"Puerto Rico"])]
# Plot it
fig, ax = plt.subplots(1,1)
us.plot(ax=ax)
plt.show()
# Convert to EPSG 4326 for compatibility with H3 Hexagons
us = us.to_crs(epsg=4326)
# Get union of the shape (whole US)
union_poly = unary_union(us.geometry)
# Find the hexagons within the shape boundary using PolyFill
hex_list=[]
for n,g in enumerate(union_poly):
if (n+1) % 100 == 0:
print(str(n+1)+"/"+str(len(union_poly)))
temp = mapping(g)
temp['coordinates']=[[[j[1],j[0]] for j in i] for i in temp['coordinates']]
hex_list.extend(h3.polyfill(temp,res=5))
# Create hexagon data frame
us_hex = pd.DataFrame(hex_list,columns=["hex_id"])
# Create hexagon geometry and GeoDataFrame
us_hex['geometry'] = [Polygon(h3.h3_to_geo_boundary(x, geo_json=True)) for x in us_hex["hex_id"]]
us_hex = gpd.GeoDataFrame(us_hex)
# Plot the thing
fig, ax = plt.subplots(1,1)
us_hex.plot(ax=ax, cmap="prism")
plt.show()
上图的分辨率为“5”(https://h3geo.org/docs/core-library/restable/),我建议您也查看其他分辨率,例如 4:
当然,这取决于“缩放级别”,即您是查看整个国家还是仅查看城市等。
而且,当然,要回答原始问题:您可以使用以下方法保存生成的 shapefile
us_hex.to_file("us_hex.shp")
使用单独的多边形和索引名称修改了 @nrabinowitz 的一个:
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, 5, false);
var features = hexagons.map(function toBoundary(hex) {
var coords = h3.h3ToGeoBoundary(hex, true)
var feature = {"type": "Feature",
"properties": {"name": hex},
"geometry": {
"type": "Polygon",
"coordinates": [coords]}};
return feature;
});
console.log(JSON.stringify({
"type": "FeatureCollection",
"features": features}));