获取Plotly Hexbin Mapbox热图中每个六边形的GPS边界

获取plotly hexbin mapbox热图中每个六边形的gps边界

本文档详细介绍了如何从Plotly的hexbin_mapbox热图中提取每个六边形的信息,包括平均值、中心点GPS坐标以及六个角点的GPS坐标。我们将使用geopandas库处理地理空间数据,并将其转换为pandas DataFrame,方便后续分析和使用。

从Plotly Figure中提取数据

首先,我们需要从Plotly生成的fig对象中提取六边形的坐标和值。这些信息存储在fig.data[0]中。

import pandas as pdimport geopandas as gpdfrom shapely.geometry import LineStringimport numpy as npimport plotly.figure_factory as ff# 示例数据(与问题中的数据相同)gps_coordinates = [[32.7792, -96.7959, 10000],                   [32.7842, -96.7920, 15000],                   [32.8021, -96.7819, 12000],                   [32.7916, -96.7833, 26000],                   [32.7842, -96.7920, 51000],                  [32.7842, -96.7920, 17000],                   [32.7792, -96.7959, 25000],                   [32.7842, -96.7920, 19000],                   [32.7842, -96.7920, 31000],                   [32.7842, -96.7920, 40000]]df = pd.DataFrame(gps_coordinates, columns=['LATITUDE', 'LONGITUDE', 'Value'])fig = ff.create_hexbin_mapbox(      data_frame=df, lat='LATITUDE', lon='LONGITUDE',      nx_hexagon=2,       opacity=0.2,       labels={"color": "Dollar Value"},      color='Value',      agg_func=np.mean,       color_continuous_scale="Jet",      zoom=14,      min_count=1, # This gets rid of boxes for which we have no data      height=900,      width=1600,      show_original_data=True,      original_data_marker=dict(size=5, opacity=0.6, color="deeppink"),      )fig.update_layout(mapbox_style="open-street-map")# 提取坐标和值coordinates = [feature['geometry']['coordinates'] for feature in fig.data[0].geojson['features']]values = fig.data[0]['z']

创建GeoPandas DataFrame

接下来,我们将使用提取的坐标和值创建一个GeoPandas DataFrame。GeoPandas扩展了Pandas的功能,增加了对地理空间数据的支持。

# 创建DataFramehexbins_df = pd.DataFrame({'coordinates': coordinates, 'values': values})# 创建几何对象hexbins_df['geometry'] = hexbins_df['coordinates'].apply(lambda x: LineString(x[0]))# 创建GeoDataFramehexbins_gdf = gpd.GeoDataFrame(hexbins_df, geometry='geometry')

获取六边形的中心点

GeoPandas提供了方便的方法来计算几何对象的中心点。我们可以直接使用centroid属性来获取每个六边形的中心点坐标。

# 计算中心点hexbins_gdf['centroid'] = hexbins_gdf['geometry'].centroid

获取六边形的角点坐标

为了获取每个六边形的角点坐标,我们可以将坐标列表转换为单独的列。由于每个六边形有六个角点,我们需要创建六个新列。

# 提取角点坐标corners_df = hexbins_gdf['coordinates'].apply(lambda x: pd.Series(x[0])).rename(columns=lambda x: f'corner_{x+1}')# 合并角点坐标到DataFramehexbins_df = pd.concat([hexbins_df, corners_df], axis=1).drop(columns='corner_7') # 删除第七个角点,因为它与第一个角点相同

完整代码示例

以下是完整的代码示例,包括数据准备、数据提取、GeoPandas DataFrame创建、中心点计算和角点坐标提取。

import pandas as pdimport geopandas as gpdfrom shapely.geometry import LineStringimport numpy as npimport plotly.figure_factory as ff# 示例数据(与问题中的数据相同)gps_coordinates = [[32.7792, -96.7959, 10000],                   [32.7842, -96.7920, 15000],                   [32.8021, -96.7819, 12000],                   [32.7916, -96.7833, 26000],                   [32.7842, -96.7920, 51000],                  [32.7842, -96.7920, 17000],                   [32.7792, -96.7959, 25000],                   [32.7842, -96.7920, 19000],                   [32.7842, -96.7920, 31000],                   [32.7842, -96.7920, 40000]]df = pd.DataFrame(gps_coordinates, columns=['LATITUDE', 'LONGITUDE', 'Value'])fig = ff.create_hexbin_mapbox(      data_frame=df, lat='LATITUDE', lon='LONGITUDE',      nx_hexagon=2,       opacity=0.2,       labels={"color": "Dollar Value"},      color='Value',      agg_func=np.mean,       color_continuous_scale="Jet",      zoom=14,      min_count=1, # This gets rid of boxes for which we have no data      height=900,      width=1600,      show_original_data=True,      original_data_marker=dict(size=5, opacity=0.6, color="deeppink"),      )fig.update_layout(mapbox_style="open-street-map")# 提取坐标和值coordinates = [feature['geometry']['coordinates'] for feature in fig.data[0].geojson['features']]values = fig.data[0]['z']# 创建DataFramehexbins_df = pd.DataFrame({'coordinates': coordinates, 'values': values})# 创建几何对象hexbins_df['geometry'] = hexbins_df['coordinates'].apply(lambda x: LineString(x[0]))# 创建GeoDataFramehexbins_gdf = gpd.GeoDataFrame(hexbins_df, geometry='geometry')# 计算中心点hexbins_gdf['centroid'] = hexbins_gdf['geometry'].centroid# 提取角点坐标corners_df = hexbins_gdf['coordinates'].apply(lambda x: pd.Series(x[0])).rename(columns=lambda x: f'corner_{x+1}')# 合并角点坐标到DataFramehexbins_df = pd.concat([hexbins_df, corners_df], axis=1).drop(columns='corner_7') # 删除第七个角点,因为它与第一个角点相同# 打印结果print(hexbins_df)

注意事项

依赖库: 确保安装了pandas, geopandas, shapely, plotly。 可以使用pip install pandas geopandas shapely plotly 命令安装。坐标系: GeoPandas DataFrame默认使用WGS 84坐标系(EPSG:4326)。如果需要,可以将其转换为其他坐标系。数据量: 处理大量六边形数据时,性能可能会受到影响。可以考虑使用空间索引等技术来提高性能。六边形方向: shapely.geometry.LineString创建的几何对象可能需要进一步处理,以确保其方向符合预期。

总结

通过使用Plotly、GeoPandas和Shapely,我们可以方便地从hexbin_mapbox热图中提取每个六边形的平均值、中心点坐标和角点坐标,并将其转换为易于分析和使用的DataFrame。这个过程为地理空间数据的分析和可视化提供了强大的工具

以上就是获取Plotly Hexbin Mapbox热图中每个六边形的GPS边界的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1366818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月14日 06:53:40
下一篇 2025年12月14日 06:53:56

相关推荐

发表回复

登录后才能评论
关注微信