商城首页欢迎来到中国正版软件门户

您的位置:首页 >如何获取Plotly Hexbin Mapbox六边形边界坐标

如何获取Plotly Hexbin Mapbox六边形边界坐标

  发布于2025-08-11 阅读(0)

扫一扫,手机访问

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

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

从Plotly Figure中提取数据

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

import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString
import numpy as np
import 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的功能,增加了对地理空间数据的支持。

# 创建DataFrame
hexbins_df = pd.DataFrame({'coordinates': coordinates, 'values': values})

# 创建几何对象
hexbins_df['geometry'] = hexbins_df['coordinates'].apply(lambda x: LineString(x[0]))

# 创建GeoDataFrame
hexbins_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}')

# 合并角点坐标到DataFrame
hexbins_df = pd.concat([hexbins_df, corners_df], axis=1).drop(columns='corner_7') # 删除第七个角点,因为它与第一个角点相同

完整代码示例

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

import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString
import numpy as np
import 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']

# 创建DataFrame
hexbins_df = pd.DataFrame({'coordinates': coordinates, 'values': values})

# 创建几何对象
hexbins_df['geometry'] = hexbins_df['coordinates'].apply(lambda x: LineString(x[0]))

# 创建GeoDataFrame
hexbins_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}')

# 合并角点坐标到DataFrame
hexbins_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。这个过程为地理空间数据的分析和可视化提供了强大的工具。

本文转载于:互联网 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注