question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integration with geopandas geometries

See original GitHub issue

geopandas is a package that reads many type of geometric datasets, including geojson, topojson and shapefiles (albeit through fiona) and parses it into a pandas dataframe, where the geometry is parsed as a separate column.

It would be great if this is geometry type is recognized in Altair to make a map easily.

Problems might arise with projections as Vega doesn’t support all EPSG projections, but EPSG:4326 equals to Mercator and that is supported.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

29reactions
mattijncommented, Feb 25, 2019

Got it working 😃

import altair as alt
import geopandas as gpd
import pandas as pd
import json
%matplotlib inline
Load two datasets
counties = r'/Users/mattijnvanhoek/Desktop/us-10m.json'
unemp_data = r'/Users/mattijnvanhoek/Desktop/unemployment.tsv'
df = pd.read_csv(unemp_data, sep='\t')
# make sure you have Fiona <= 1.8a2 or >= 1.8.5
# since the versions in between did not include the `TopoJSON` driver
gdf = gpd.read_file(counties, driver='TopoJSON')
gdf.id = gdf.id.astype(int)
Apply inner-join on GeoDataFrame and DataFrame (gdf should be on ‘left’ side and df on the right to maintain geometry properties in the resulting joined GeoDataFrame)
gdf_merged = gdf.merge(df, left_on='id', right_on='id', how='inner')
Plot the GeoDataFrame using matplotlib and print the head(). The rate column is joined to the DataFrame.
gdf_merged.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x119e79128>

output_7_1

gdf_merged.head()
<div>
geometry id rate
0 () 22051 .065
1 (POLYGON ((-90.1077214366575 30.19168413151698... 22051 .065
2 (POLYGON ((-120.8536146368232 49.0001146177235... 53073 .078
3 POLYGON ((-106.1123837970986 48.99904031068445... 30105 .046
4 POLYGON ((-114.0698488011574 48.99904031068445... 30029 .088
</div>
Prepare GeoDataFrame for Altair
# dump as json
json_gdf = gdf_merged.to_json()
# load as a GeoJSON object.
json_features = json.loads(json_gdf)
Make the Choropleth Map
# parse variable `features` from json_features to `alt.Data`
data_geo = alt.Data(values=json_features['features'])

# plot map, where variables ares nested within `properties`, 
alt.Chart(data_geo).mark_geoshape(
    fill='lightgray',
    stroke='white'
).properties(
    projection={'type': 'albersUsa'},
    width=700,
    height=400
).encode(
    color='properties.rate:Q')

choropleth

👍

5reactions
iliatimofeevcommented, May 3, 2018

I think it’s little bit simpler

import altair as alt
import pandas as pd
import geopandas as gpd

alt.renderers.enable('notebook')


world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world  = world[world.continent!='Antarctica'] # do not display Antarctica

data  = alt.InlineData(values = world.to_json(), #geopandas to geojson string
                       # root object type is "FeatureCollection" but we need its features
                       format = alt.DataFormat(property='features',type='json')) 
alt.Chart(data).mark_geoshape(
).encode( 
    color='properties.pop_est:Q', # DataFrame fields are accessible through a "properties" object 
    tooltip='properties.name:N'
).properties( 
    projection={"type":'mercator'},
    width=500,
    height=300
)

visualization 1

But it will crush if we add Timestamp type field to DataFrame. To to avoid crashing it could be sanitized by alt.InlineData(values = alt.utils.core.sanitize_dataframe(world).to_json(),

In general case will be great to support any object with geo_interface that is widely supported by python GIS libraries. I suggest to have a special class for this case something like: data = alt.GeoData(world). I could make a PR, if it is needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Integration with geopandas to plot shapely geometries #608
One way would be to change plot to accept geopandas type objects, look for the 'geometry' column, and plot the 'Shapely' geometries. Currently, ......
Read more >
GIS with Geopandas - Practical Data Science
Measuring Spatial Relationships: Geopandas is deeply integrated with libraries for geometric manipulations, making it easy to do things like measure the ...
Read more >
Integrating with GeoPandas | VerticaPy
123. pop_est. Int Abc. continent. Varchar(32) Abc. country. Varchar(82) 1 140 Seven seas (open ocean) Fr. S. Antarctic Lands 2 2931 South America Falkland Is. 3...
Read more >
Geometric Manipulations — GeoPandas 0.12.2+0.gefcb367 ...
Returns a GeoSeries of geometries representing all points within a given ... Transform the geometries of the GeoSeries using an affine transformation matrix....
Read more >
Using GeoPandas for Spatial Visualization | by Paul Torres
... descartes — provides a nicer integration of Shapely geometry objects with Matplotlib. It's not needed every time but I import it just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found