Open msussman opened 6 years ago
There exist a geoCentroid()
expression in Vega that could potentially be used, but I can't get that to work facing similar errors as documented in https://github.com/vega/vega-lite/issues/5490.
But since you are already using GeoPandas its easier to compute your centroids beforehand and add them using the longitude
and latitude
channels in the mark_text()
mark:
import altair as alt
import geopandas as gpd
gdf = gpd.read_file('https://opendata.arcgis.com/datasets/fcfbf29074e549d8aff9b9c708179291_1.geojson')
gdf.loc[:, 'lon'] = gdf.geometry.centroid.x
gdf.loc[:, 'lat'] = gdf.geometry.centroid.y
gdf.head(2)
OBJECTID | ANC_ID | WEB_URL | NAME | Shape_Length | Shape_Area | geometry | lon | lat | |
---|---|---|---|---|---|---|---|---|---|
0 | 1 | 1C | http://anc.dc.gov/page/advisory-neighborhood-c... | ANC 1C | 5218.954361 | 1.285112e+06 | POLYGON ((-77.04642 38.92598, -77.04655 38.926... | -77.042845 | 38.921928 |
1 | 2 | 1D | http://anc.dc.gov/page/advisory-neighborhood-c... | ANC 1D | 4224.010068 | 9.475922e+05 | POLYGON ((-77.03645 38.93638, -77.03645 38.936... | -77.041738 | 38.932277 |
base = alt.Chart().mark_geoshape().encode(
color=alt.Color('OBJECTID', scale=alt.Scale(scheme='bluegreen'))
)
label = alt.Chart().mark_text().encode(
latitude='lat',
longitude='lon',
text='ANC_ID'
)
alt.layer(base, label, data=gdf)
I'm new to Altair and really like it's functionality, but I'm trying to do something that seems someone obscure in the Altair universe and wanted to check to see if this was even possible.
I've successfully made a chloropeth of DC ANC districts with the code below by appending my statistical fields to a geopandas dataframe and then converting to a JSON to be rendered in the chart. I'd like to be able to add the ANC title to each ANC polygon, but am struggling with how to do so (see commented code in "gen_chloro" function. I've also included a picture of DC with it's ANCs labeled, which is ultimately the layer I'd like to add to my chloropeth. Any help would be greatly appreciated.
Here is my code: