我正在使用 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)
# 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()
我读到 Scattermapbox 仅支持墨卡托投影,我发现这令人困惑,因为 plotly 文档中的示例似乎是 long/lat 格式,但我尝试将 GeoDataFrame 的坐标转换为 epsg 3857,请参阅
# world = world.to_crs(epsg=3857)
结果是阴影图像变得不可见。任何帮助将不胜感激。