edgi-govdata-archiving / ECHO-Sunrise

An Environmental Enforcement Watch partnership with Sunrise Boston hubs
https://colab.research.google.com/github/edgi-govdata-archiving/ECHO-Sunrise/blob/main/ECHO-Sunrise.ipynb
GNU General Public License v3.0
1 stars 2 forks source link

Implement aggregating and mapping data by selected geography #2

Closed ericnost closed 4 years ago

ericnost commented 4 years ago

Here is some test code for how it might work for counties:

import json
geo = select_region_widget.value.lower()
geo_json_data = json.load(open("ECHO-Geo/ma_"+geo+".geojson"))
echo_data.reset_index(inplace=True) # Reset index to loop and edit values. ECHO data is loaded separately.

County names need to be cleaned up!

for i, row in echo_data.iterrows():
  county = row["FAC_COUNTY"]
  if (type(county) == str and county.endswith( ' COUNTY' )):
    county = county[:-7]
    echo_data.at[i,'FAC_COUNTY'] = county

Even then, there are issues:

att_data = echo_data.groupby("FAC_COUNTY")[["FAC_QTRS_WITH_NC"]].agg("mean") # Aggregate by county. Error: county names are duplicated....
att_data.reset_index(inplace=True) # Reset index to change column names
att_data = test.rename(columns={"FAC_COUNTY": "COUNTY", "FAC_QTRS_WITH_NC": "percent"}) 
att_data= att_data.loc[(att_data["percent"]>0)] # Remove zeros to remove some duplicated county names

Make the map:

q = att_data['percent'].quantile([0, 0.25,0.5,0.75, 1]) # Create a quantile scale. This should put an equal number of geographies in each bin/color.
m = folium.Map()
c = folium.Choropleth(
    geo_data = geo_json_data,
    data = att_data,
    columns=['COUNTY', 'percent'], key_on='feature.properties.COUNTY', # Join the geo data and the attribute data on a key id
    fill_color='OrRd',fill_opacity=0.75,line_weight=.5,nan_fill_opacity=.1, nan_fill_color="white", highlight=True, 
    bins=[q[0.00],q[0.25], q[0.5], q[0.75], q[1.00]],
    legend_name="Average # of Quarters Facilities were in Non-Compliance (out of 12)"
).add_to(m)

c.geojson.add_child(
            folium.features.GeoJsonTooltip(['COUNTY'])
        )

bounds = m.get_bounds()
m.fit_bounds(bounds)

m
ericnost commented 4 years ago

Done, except need the geographies. See #4