如何在python中使用uber h3将shapefile / geojson转换为六边形? tl;博士完整说明

如何解决如何在python中使用uber h3将shapefile / geojson转换为六边形? tl;博士完整说明

我想在我的地理地图上创建六边形,并希望保留shapefile / geojson指定的数字边界。 如何使用uber的h3 python库实现该功能?

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

解决方法

tl;博士

为方便起见,您可以使用 H3-Pandas

import geopandas as gpd
import h3pandas

# Prepare data
gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf = gdf.loc[gdf['continent'].eq('Africa')]
gdf['gdp_md_per_capita'] = gdf['gdp_md_est'].div(gdf['pop_est'])
resolution = 4

# Resample to H3 cells
gdf.h3.polyfill_resample(resolution)

africa-hexagons

完整说明

这样的用例正是我编写 H3-Pandas 的原因,它是 H3 与 Pandas 和 GeoPandas 的集成。

让我们使用 naturalearth_lowres 中包含的 GeoPandas 数据集来演示用法。

import geopandas as gpd
import h3pandas

path_shapefile = gpd.datasets.get_path('naturalearth_lowres')
gdf = gpd.read_file(path_shapefile)

我们还要创建一个用于绘图的数字列。

gdf = gdf.loc[gdf['continent'].eq('Africa')]
gdf['gdp_md_per_capita'] = gdf['gdp_md_est'].div(gdf['pop_est'])
ax = gdf.plot(figsize=(15,15),column='gdp_md_per_capita',cmap='RdBu')
ax.axis('off')

africa

我们将使用 H3 分辨率 4。有关详细信息,请参阅 H3 resolution table

resolution = 4  

我们可以使用函数 polyfill 添加 H3 六边形。此方法添加质心落入每个多边形的 H3 单元格列表。

gdf_h3 = gdf.h3.polyfill(resolution)
print(gdf_h3['h3_polyfill'].head(3))

1     [846aca7ffffffff,8496b5dffffffff,847b691ffff...
2     [84551a9ffffffff,84551cdffffffff,8455239ffff...
11    [846af51ffffffff,8496e63ffffffff,846a803ffff...
Name: h3_polyfill,dtype: object

如果我们想水平展开值(以与 H3 单元格一样多的行结束),我们可以使用参数 explode

gdf_h3 = gdf.h3.polyfill(resolution,explode=True)
print(gdf_h3.head(3))

    pop_est continent      name iso_a3  gdp_md_est  \
1  53950935    Africa  Tanzania    TZA    150600.0   
1  53950935    Africa  Tanzania    TZA    150600.0   
1  53950935    Africa  Tanzania    TZA    150600.0   

                                            geometry  gdp_md_per_capita  \
1  POLYGON ((33.90371 -0.95000,34.07262 -1.05982...           0.002791   
1  POLYGON ((33.90371 -0.95000,34.07262 -1.05982...           0.002791   

       h3_polyfill  
1  846aca7ffffffff  
1  8496b5dffffffff  
1  847b691ffffffff  

然后我们可以使用方法h3_to_geo_boundary 来获得 H3 单元格的几何形状。它期望索引已经具有 H3 单元格 id。

gdf_h3 = gdf_h3.set_index('h3_polyfill').h3.h3_to_geo_boundary()

我们现在可以绘制结果

ax = gdf_h3.plot(figsize=(15,cmap='RdBu')
ax.axis('off')

africa-hexagons

H3-Pandas 实际上具有一次性执行所有这些的便利功能:polyfill_resample

gdf_h3 = gdf.h3.polyfill_resample(resolution)
ax = gdf_h3.plot(figsize=(15,cmap='RdBu')
ax.axis('off')

africa-hexagons

,

h3-py不知道如何直接使用shapefile数据,但是听起来您可以使用https://github.com/GeospatialPython/pyshp之类的库将shapefile数据转换为GeoJSON,然后使用{{1} }转换为H3六角形的集合。

有很多选项可以绘制边界以及H3六角形。例如,您可以使用pydeck及其GeoJsonLayerH3HexagonLayer层。

如果绘图软件不能直接使用H3六角形,则可以使用h3.polyfill()h3.h3_to_geo_boundary()之类的功能将其转换为其他格式。

,

将 GeoJSON 转换为 Uber 的 h3 非常简单。

附上下面使用的示例代码片段和 GeoJSON:

GeoJSON

{"type": "FeatureCollection","features": [{"type": "Feature","properties": {},"geometry": {"type": "Polygon","coordinates": [[[77.5250244140625,13.00857192009273],[77.51266479492188,12.971103764892034],[77.52777099609375,12.94099133483504],[77.57171630859375,12.907528813968185],[77.60604858398438,12.914890953258695],[77.662353515625,12.928276105253065],[77.69874572753906,12.961066692801282],[77.65823364257812,13.00990996390651],[77.58956909179688,13.04469656691112],[77.53944396972656,13.038007215169166],[77.5250244140625,13.00857192009273]]]}}]}

代码:

from h3converter import h3converter

geojson_raw = open("sampleJson.geojson",)
geojson = json.load(geojson_raw)

h3_list = []
h3_resolution = 7
for feature in geojson["features"]:
    feature_h3 = h3converter.polyfill(feature["geometry"],h3_resolution)
    h3_list.append(feature_h3)
    print("H3's created => ",len(feature_h3))

print(h3_list)

回复:

[{'8761892edffffff','8760145b0ffffff','87618925effffff','87618925bffffff','87618924affffff','876189256ffffff','8760145b1ffffff','8761892eaffffff','8761892eeffffff','876189253ffffff','876189259ffffff','8760145b2ffffff','876014586ffffff','8760145b4ffffff','8761892e1ffffff','8760145a2ffffff','8761892ecffffff','876189251ffffff','8760145a4ffffff','8761892e5ffffff','87618925affffff','8761892e9ffffff','8761892cdffffff','876189250ffffff','87618925dffffff','8760145b6ffffff','876014595ffffff','876189252ffffff','8761892ebffffff','8760145a3ffffff','8760145a6ffffff','876014584ffffff','876189258ffffff','8760145b5ffffff','8760145b3ffffff','876014594ffffff','8761892c9ffffff','87618925cffffff','8760145a0ffffff','8761892e8ffffff'}]

使用的包: https://pypi.org/project/h3converter/

您也可以使用上述包将 h3 转换为 GeoJSON。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-