在Python绘图'hexbin_mapbox'热图中获取每个hexbin的GPS边界 - 质心GPS点和hexbin每个角的GPS点

问题描述 投票:0回答:2

我使用plotly通过绘制多个位置(使用GPS纬度/经度)以及每个位置的值,在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()
python pandas plotly gps latitude-longitude
2个回答
0
投票

我想我已经明白了。

  • 我可以通过fig.data[0]访问hexbin数据。
  • 可以从“customdata”和“geojson”字段中将数据提取到 geopandas df 中。
  • 然后可以将 geopandas df 转换为 pandas df。

需要的其他代码(除了上面原始帖子中的代码之外)如下:

# Import more dependencies
import geopandas as gpd # Used to extract hexbin data
from shapely.geometry import Polygon # Used to extract hexbin data

# Create GeoPandas df using hexbin data found in fig.data[0]
geo_df = gpd.GeoDataFrame({
         'customdata': fig.data[0]['customdata'].tolist(),
         'id':[item['id'] for item in fig.data[0]['geojson']['features']],
         'geometry':[Polygon(item['geometry']['coordinates'][0]) for item in fig.data[0]['geojson']['features']]
         })

# Convert geo_df to pandas df
df1 = pd.DataFrame(geo_df)

# Display df1
display(df1)

0
投票

您可以提取每个六边形的六个角的坐标以及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

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

>>> hexbins_gdf
                                         coordinates        values                                           geometry                    centroid
0  [[[-96.7889, 32.78215666477984], [-96.78539999...  28833.333333  LINESTRING (-96.78890 32.78216, -96.78540 32.7...  POINT (-96.78890 32.78555)
1  [[[-96.792400000007, 32.777059832108314], [-96...  17500.000000  LINESTRING (-96.79240 32.77706, -96.78890 32.7...  POINT (-96.79240 32.78046)
2  [[[-96.785399999993, 32.7872532054738], [-96.7...  26000.000000  LINESTRING (-96.78540 32.78725, -96.78190 32.7...  POINT (-96.78540 32.79065)
3  [[[-96.785399999993, 32.79744541083471], [-96....  12000.000000  LINESTRING (-96.78540 32.79745, -96.78190 32.7...  POINT (-96.78540 32.80084)
© www.soinside.com 2019 - 2024. All rights reserved.