Ben Fuqua
Ben Fuqua

Reputation: 85

My lines won't plot correctly in altair.chart.mark_geoshape

I need to plot the rivers in idaho from this website

When I load it into geopandas and try to plot it through altair.Chart().mark_geoshape() my graph comes up with a bunch of random lines and they aren't plotting as expected. I don't know what is going on because I am new when it comes to geospatial data.

I followed the pattern from this example https://altair-viz.github.io/gallery/london_tube.html but I wasn't able to plot the lines.

Any thoughts or how I can do this would be of great help! Below is the code I am using. Thank you!

import altair as alt
import geopandas as gpd
from vega_datasets import data

states = alt.topo_feature(data.us_10m.url,'states')
hydro = gpd.read_file('drive/MyDrive/data_cse350/hyd250/hyd250.shp')

rivers = hydro.loc[hydro.FEAT_NAME.isin(['Snake River','Henrys Fork'])]

rchart = alt.Chart(rivers).mark_geoshape().encode(color = 'FEAT_NAME')

idaho = alt.Chart(states).mark_geoshape(fill = 'white',stroke = 'black').project(
    'albersUsa'
    ).transform_calculate(state_id = "(datum.id)"
    ).transform_filter((alt.datum.state_id)==16)

rchart+idaho```

If you can solve this, that would be great! Thank you for your help! I have already spent waaaayyy too many hours on getting this to work. 

Upvotes: 1

Views: 488

Answers (1)

r-beginners
r-beginners

Reputation: 35135

The reason it was not drawn was that the geopandas data coordinate system was different, so it needed to be converted to a format that represented latitude and longitude. My experience with this kind of thing is limited, so I had to figure it out by hand. I actually referenced a map of Idaho to make sure it matched.

import altair as alt
import geopandas as gpd
from vega_datasets import data

states = alt.topo_feature(data.us_10m.url,'states')
hydro = gpd.read_file('./data/hyd250/hyd250.shp')
hydro_trans = hydro.to_crs(epsg=4612)
rivers = hydro_trans.loc[hydro.FEAT_NAME.isin(['Snake River','Henrys Fork'])]

rchart = alt.Chart(rivers).mark_geoshape(
    filled=False,
    strokeWidth=2
).encode(
    color='FEAT_NAME'
).properties(
    width=600,
    height=300
)

idaho = alt.Chart(states).mark_geoshape(
    fill=None,
    stroke='black'
).project(
    'albersUsa'
    ).transform_calculate(
    state_id = "(datum.id)"
    ).transform_filter(
    (alt.datum.state_id)==16
)

rchart+idaho

enter image description here

Upvotes: 2

Related Questions