Home > Back-end >  How can we parse a JSON file for specific records of county borders and overlay that on a Folium Hea
How can we parse a JSON file for specific records of county borders and overlay that on a Folium Hea

Time:12-23

I found a JSON file that has borders of US counties, right here.

enter image description here

Finally, how would I overlap a HeatMap on top of the plotted county borders? When I create a Folium HeatMap, it overwrites all the county borders!

import folium
from folium.plugins import HeatMap


max_amount = float(df_2std['Total_Cust_Minutes'].max())

hmap = folium.Map(location=[35.5, -82.5], zoom_start=7, )


hm_wide = HeatMap(list(zip(df_2std.Circuit_Latitude.values, 
                           df_2std.Circuit_Longitude.values, 
                           df_2std.Total_Cust_Minutes.values)),
                   min_opacity=0.2,
                   max_val=max_amount,
                   radius=25, 
                   blur=20, 
                   max_zoom=1, 
                 )

hmap.add_child(hm_wide)

enter image description here

CodePudding user response:

Here's an example. I don't know if their GeoJSON can handle objects or if has to be text, so this code (a) reads in the JSON and converts to an object, (b) filters the objects to keep those in state 037 (which, I think, is North Carolina), then (c) converts that back to a JSON string.

import json
from folium import GeoJson

data = json.load(open('gz_2010_us_050_00_500k.json'))
newdata = { "type": "FeatureCollection", "features": [] }

for row in data['features']:
    if row['properties']["STATE"] == '37':
        newdata['features'].append( row )

text = json.dumps(newdata)

m = folium.Map(width="0",weight="0")
GeoJson(text).add_to(m)
  • Related