简体   繁体   English

过滤到Altair的等值线地图的州级别

[英]Filtering to State Level for Choropleth Maps in Altair

I've been playing around with Altair's map functionality. 我一直在玩Altair的地图功能。 I'm very easily able to build US maps with state and county boundaries now. 我现在很容易建立州和县界的美国地图。 What I'm stuck on is filtering the maps down to a lower level. 我坚持的是将地图过滤到较低的水平。 For instance, if I wanted to build a map of only the state of Georgia with the county boundaries, how would I do that? 例如,如果我想建立一个只有乔治亚州与县界的地图,我该怎么做?

I have a solution, but it's a bad solution. 我有一个解决方案,但这是一个糟糕的解决方案。 Curious if there's a better way. 好奇,如果有更好的方法。 Here is my code: 这是我的代码:

states_data = alt.topo_feature(data.us_10m.url, "states")
counties = alt.topo_feature(data.us_10m.url, 'counties')

states = alt.Chart(states_data).mark_geoshape(
        stroke='black',
        strokeWidth=1
    ).transform_filter((alt.datum.id == 13))

cobb = alt.Chart(counties).mark_geoshape(
        stroke='black',
        strokeWidth=1
    ).transform_filter((alt.datum.id == 13067))

fulton = alt.Chart(counties).mark_geoshape(
        stroke='black',
        strokeWidth=1
    ).transform_filter((alt.datum.id == 13121))

dekalb = alt.Chart(counties).mark_geoshape(
        stroke='black',
        strokeWidth=1
    ).transform_filter((alt.datum.id == 13089))

states + cobb + fulton + dekalb

This code gives me this result: 这段代码给了我这个结果:

在此输入图像描述

I'm using the very common Albers USA data to create the state boundaries and county boundaries. 我正在使用非常常见的Albers USA数据来创建州边界和县界。 I've used "states" to project the state of Georgia and then I used "cobb", "fulton", and "dekalb" to project 3 different metro Atlanta counties on top of that. 我用“州”来投影乔治亚州,然后我用“cobb”,“fulton”和“dekalb”来预测3个不同的亚特兰大都市县。

This works, but it's extremely inefficient, and it would be a pain a huge pain to do this for all 159 counties in the state. 这是有效的,但效率非常低,而且对于该州所有159个县来说,这将是一个巨大的痛苦。 Is there an easier way to filter the counties than the one I'm using? 有没有比我使用的更简单的过滤县的方法? Or some good automated way to read in all 159 counties without 1,000+ lines of code!? 或者在没有超过1000行代码的情况下,在所有159个县中阅读一些良好的自动化方式!?

Edit: Also for the record, I tried doing the counties and then filtering by state, but that didn't work. 编辑:同样为了记录,我尝试做县,然后按州过滤,但这不起作用。 Code is below: 代码如下:

states = alt.Chart(states_data).mark_geoshape(
        stroke='black',
        strokeWidth=1
    ).transform_filter((alt.datum.id == 13))

counties = alt.Chart(counties).mark_geoshape(
        stroke='black',
        strokeWidth=1
    ).project('albersUsa')

states + counties

That code seems to just do the full US county map. 该代码似乎只是完整的美国县地图。

在此输入图像描述

A bit strange way. 有点奇怪的方式。

County id code starts with state id. 县id代码以state id开头。 With simple js trick you can extract it. 使用简单的js技巧,您可以提取它。

counties = alt.topo_feature(data.us_10m.url, 'counties')

map_georgia =(
    alt.Chart(data = counties)
    .mark_geoshape(
        stroke='black',
        strokeWidth=1
    )
    .transform_calculate(state_id = "(datum.id / 1000)|0")
    .transform_filter((alt.datum.state_id)==13)
)

map_georgia

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM