Plotly Mapbox 中多边形的 Datashader 集成

Man*_*ehn 7 projection python-3.x mapbox plotly datashader

我正在使用plotly的Scattermapbox将地图与由datashader的阴影函数创建的多边形的阴影图像覆盖(基于https://plotly.com/python/datashader/),但投影似乎没有对齐,请参见下图。有什么建议我可以使用plotly的Scattermapbox和datashader来克服这个问题吗?

可重现的例子:

import geopandas as gpd
import plotly.graph_objects as go
import spatialpandas as spd
import datashader as ds
from colorcet import fire
import datashader.transfer_functions as tf

# load data
world = gpd.read_file(
    gpd.datasets.get_path('naturalearth_lowres')
)
# world = world.to_crs(epsg=3857)
# create spatialpandas DataFrame
df_world = spd.GeoDataFrame(world)
# create datashader canvas and aggregate
cvs = ds.Canvas(plot_width=1000, plot_height=1000)
agg = cvs.polygons(df_world, geometry='geometry', agg=ds.mean('pop_est'))
# create shaded image
tf.shade(agg, cmap=fire)
Run Code Online (Sandbox Code Playgroud)

阴影图像

# create shaded image and convert to Python image
img = tf.shade(agg, cmap=fire)[::-1].to_pil()

coords_lat, coords_lon = agg.coords["y"].values, agg.coords["x"].values
# Corners of the image, which need to be passed to mapbox
coordinates = [
    [coords_lon[0], coords_lat[0]],
    [coords_lon[-1], coords_lat[0]],
    [coords_lon[-1], coords_lat[-1]],
    [coords_lon[0], coords_lat[-1]],
]

fig = go.Figure(go.Scattermapbox())
fig.update_layout(
    mapbox_style="open-street-map",
    mapbox_layers=[
        {
            "sourcetype": "image",
            "source": img,
            "coordinates": coordinates,
        }
    ]
)
fig.show()
Run Code Online (Sandbox Code Playgroud)

叠加地图

我读到 Scattermapbox 仅支持墨卡托投影,我发现这很令人困惑,因为plotly 文档中的示例似乎采用长/纬度格式,但我尝试将 GeoDataFrame 的坐标转换为 epsg 3857,请参阅

# world = world.to_crs(epsg=3857)
Run Code Online (Sandbox Code Playgroud)

结果是阴影图像变得不可见。任何帮助将不胜感激。

小智 0

您尝试过 epsg:4326 吗?就我而言,我使用这个并且几何图形放置正确。

另一方面,使用 geopandas 来转换数据帧的几何列,您必须使用参数“inplace=True”。