Vega-Lite / Altair:如何居中或裁剪欧洲地图?

Rag*_*rok 2 gis data-visualization vega vega-lite altair

我有一些关于欧洲国家的数据。我正在尝试使用 world-110m 数据在 Altair / Vega-Lite 中创建可视化。从技术上讲,一切都很好,除了国家的编码边界还包括遥远的领土,产生了一个可怕的地图,如下所示:

糟糕的欧洲地图

这是我的代码:

countries = alt.topo_feature(data.world_110m.url, 'countries')
source = df.copy()

map = alt.Chart(countries).mark_geoshape(
    stroke='black'
    ).encode(
    color=alt.Color('SomeStat:Q', sort="descending", scale=alt.Scale(
        scheme='inferno', domain=(min_value,max_value)), legend=alt.Legend(title="", tickCount=6))
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'CountryId', ['SomeStat', 'CountryName'])
).project(
    type='mercator'
)
Run Code Online (Sandbox Code Playgroud)

有没有办法裁剪这张地图或将其居中,以便我只看到欧洲而不是世界各地的偏远地区?

或者,我是否应该使用更好的仅包含欧洲的公共数据集?

小智 6

我没有你的df数据集,所以我发布了相当简单的例子。

import altair as alt
from vega_datasets import data

countries = alt.topo_feature(data.world_110m.url, 'countries')

alt.Chart(countries).mark_geoshape(
    fill='#666666',
    stroke='white'
).project(
    type= 'mercator',
    scale= 350,                          # Magnify
    center= [20,50],                     # [lon, lat]
    clipExtent= [[0, 0], [400, 300]],    # [[left, top], [right, bottom]]
).properties(
    title='Europe (Mercator)',
    width=400, height=300
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

scale您可以通过和控制地图视图center及其实际绘图大小(widthheight)。

  • scale:放大参数
  • center:视图的中心点

如果您需要进一步裁剪地图的任何部分,clipExtent可能会很有用。请注意 - 该数组代表像素大小,而不是地理坐标。(在上面的示例中,我设置了它[[0, 0], [400, 300]],以便它保留整个400x300 px视图。