python - 在Python中获取每个六边形的GPS边界plotly 'hexbin_mapbox'热图 - 质心GPS点和六边形每个角的GPS点

标签 python pandas plotly gps latitude-longitude

我通过绘制多个位置(使用 GPS 纬度/经度)以及每个位置的值,使用plotly 在 Python 中创建了一个十六进制“热图”。请参阅下面的代码,了解示例 df 和 hexbin 图形图。

所需数据

当我将鼠标悬停在每个十六进制上时,我可以看到该十六进制中包含的平均值。但我想要的是一种将每个十六进制的以下信息下载到 pandas df 中的方法:

  • 每个十六进制的平均值(已根据下面的代码计算,但目前只能通过将鼠标悬停在每个十六进制上来访问;我希望能够将其下载到 df 中)
  • 每个六边形的质心 GPS 坐标
  • 六角形每个角的 GPS 坐标(即每个六角形六个角的纬度和经度)

我的问题

如何将上面项目符号中描述的数据下载到 pandas df 中?

代码示例

# Import dependencies
import pandas as pd
import numpy as np
import plotly.figure_factory as ff
import plotly.express as px

# Create a list of GPS coordinates
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]]

# Create a DataFrame with the GPS coordinates
df = pd.DataFrame(gps_coordinates, columns=['LATITUDE', 'LONGITUDE', 'Value'])

# Print the DataFrame
display(df)
# Create figure using 'df_redfin_std_by_year_and_acreage_bin' data
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"),
      )

# Create the map
fig.update_layout(mapbox_style="open-street-map")

fig.show()

最佳答案

您可以提取每个六边形的六个角的坐标以及来自 fig.data[0] 的值。但是,我不确定质心信息存储在图形对象中的位置,但我们可以从这些数据创建一个geopandas数据框,并直接获取几何列的质心属性:

将 geopandas 导入为 gpd 从 shapely.geometry 导入 LineString

coordinates = [feature['geometry']['coordinates'] for feature in fig.data[0].geojson['features']]
values = fig.data[0]['z']
hexbins_df = pd.DataFrame({'coordinates': coordinates, 'values': values}) 
hexbins_df['geometry'] = hexbins_df['coordinates'].apply(lambda x: LineString(x[0]))

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}')
hexbins_df = pd.concat([hexbins_df, corners_df], axis=1).drop(columns='corner_7') # we drop corner_7 since that is the same as the starting corner

生成的 geopandas 数据框看起来像这样:

>>> hexbins_df
                                         coordinates        values  ...                                 corner_5                                 corner_6
0  [[[-96.7889, 32.78215666477984], [-96.78539999...  28833.333333  ...     [-96.792400000007, 32.7872532054738]    [-96.792400000007, 32.78385554412095]
1  [[[-96.792400000007, 32.777059832108314], [-96...  17500.000000  ...  [-96.79590000001399, 32.78215666477984]  [-96.79590000001399, 32.77875880877266]
2  [[[-96.785399999993, 32.7872532054738], [-96.7...  26000.000000  ...            [-96.7889, 32.79234945416662]           [-96.7889, 32.788951987483806]
3  [[[-96.785399999993, 32.79744541083471], [-96....  12000.000000  ...            [-96.7889, 32.80254107545448]            [-96.7889, 32.79914399815894]

[4 rows x 21 columns]

关于python - 在Python中获取每个六边形的GPS边界plotly 'hexbin_mapbox'热图 - 质心GPS点和六边形每个角的GPS点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77561341/

相关文章:

python - pytorch .cuda() 无法将张量获取到 cuda

python - 如何循环时间戳并创建 df

python - Plotly:如何只显示点击的行?

python - 如何使用 bootstrap 设计我的 Plotly Dash 应用程序?

python - 从另一个容器中的服务连接到 rabbitmq docker 容器

python - 如何在 python PDB 中列出当前行?

python - 我怎样才能告诉子进程停止转义我的引号?

python - 如何在 Pandas 中相互划分多个列

python - 如何在python,dataframe中将数据转换为嵌套字典

charts - 为什么这个 XPlot.Plotly 代码不能编译>