Skip to content
Advertisement

How can we parse a JSON file for specific records of county borders and overlay that on a Folium HeatMap?

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

https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json

How can I parse that file for specific records, like ‘Durham’ and ‘Raleigh’ and ‘Charlotte’ together, and plot these on a Folium map? When I run the code below, I have all counties plotted on the map, be because no specific counties are parsed out before mapping.

from folium import GeoJson
geo=r"C:\Users\RShuell\Downloads\gz_2010_us_050_00_500k.json"

file = open(geo)
text = file.read()
m = folium.Map(width="%100",weight="%100")
GeoJson(text).add_to(m)
m

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

Advertisement

Answer

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="%100",weight="%100")
GeoJson(text).add_to(m)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement