folium icon indicating copy to clipboard operation
folium copied to clipboard

Choropleth without bindings

Open baobobs opened this issue 10 years ago • 4 comments

Is it possible to create a choropleth without binding? The data I intend to use for the choropleth is among the geojson keys (HIMEDCY).

I have the following code:

m.choropleth(geo_path="sqlexport9658.json", columns=['BG10', 'HIMEDCY'], fill_color='PuBu', threshold_scale=[10000, 50000, 100000, 150000])

Both BG10 and HIMEDCY are keys within sqlexport9658.json. I checked to ensure the breaks are within the range in the HIMEDCY field, but the resulting map shows a gray layer over the basemap, rather than the expected choropleth.

baobobs avatar Dec 10 '15 22:12 baobobs

If I understand well, you have a GeoJSON named sqlexport9658 of the form :

{"type" : "FeatureCollection",
"features" : [
    {
        "geomety" : ...
        "properties" : {"BG10" : ... , "HIDEMCY" : ... },
       ...
        },
    ...
    ]
}

and you would like to use colors based on "HIDEMCY" values.

Is that right ? Otherwise, can you provide a sample of data, for the question to be clear, please ?

BibMartin avatar Dec 11 '15 12:12 BibMartin

Apologies for not including that in the prior message.

Attached is the json file, changed to txt per Github requirements.

Here's a snippet (starting from the top):

{ "type": "FeatureCollection", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [ { "type": "Feature", "properties": { "BG10": "270370601011", "HIMEDCY": 65694.000000 }, "geometry": { "type": "Polygon", "coordinates": [ [ [

sqlexport9658.txt

baobobs avatar Dec 11 '15 15:12 baobobs

If you're using v0.1.6 or before, you have to create a DataFrame that binds explicitely BG10 to HIMEDCY. Something like this sould work :

# Open and load the data
d = json.load(open('/home/journois/Téléchargements/sqlexport9658.txt'))

# Create a DataFrame binding BG10->HIMEDCY for each feature
data = pd.DataFrame([[feature['properties']['BG10'],feature['properties']['HIMEDCY']]
             for feature in d['features']], columns=['BG10','HIMEDCY'])

# Create the map
m = folium.Map([44.91,-93.12], zoom_start=12)

# Create the choropleth
m.geo_json(
    geo_str=json.dumps(d),
    key_on='feature.properties.BG10',
    data=data,
    columns=['BG10','HIMEDCY'],
    fill_color='PuBu',
    threshold_scale=[10000, 50000, 100000, 150000],
    )

If you're on the master (coming v0.2.0), you can bind the color on the fly:

# You have to create your own colormap function.
# This has to be simplified in the future.
def PuBu_colormap(x):
    """Colormap binding HIMEDCY to a color of the form "#RRGGBB"."""
    PuBu_colors = folium.utilities.color_brewer('PuBu')
    index = len([y for y in [10000, 50000, 100000, 150000] if y<x])
    return PuBu_colors[index]h

# Create the map
m = folium.Map([44.91,-93.12], zoom_start=12)

# Create the choropleth
folium.GeoJson(
    d,
    style_function=lambda x: {
        'fillColor' : PuBu_colormap(x['properties']['HIMEDCY']),
        'fillOpacity' : 0.9,
        'color' : 'black',
        'weight' : 2
        },
    ).add_to(m)

The advantage of this new approach is that you can control explicitely each style component.

In both cases, the drawback is: you have to load you data into python.

BibMartin avatar Dec 12 '15 09:12 BibMartin

That's great, thanks @BibMartin!

baobobs avatar Dec 14 '15 16:12 baobobs