Skip to content

Instantly share code, notes, and snippets.

@kamermans
Last active March 16, 2020 02:29
Show Gist options
  • Save kamermans/397488317c75b23414100d7e1316e96f to your computer and use it in GitHub Desktop.
Save kamermans/397488317c75b23414100d7e1316e96f to your computer and use it in GitHub Desktop.
COVID-19 Confirmed Cases by US County (from John's Hopkins Dataset)
# COVID-19 by US County
Arizona (6):
Maricopa: 6
Pinal: 2
California (144):
Alameda: 2
Contra Costa: 12
Fresno: 1
Humboldt: 1
Kings: 144
Los Angeles: 14
Madera: 1
Orange: 4
Placer: 7
Riverside: 1
Sacramento: 2
San Benito: 2
San Diego: 3
San Francisco: 9
San Mateo: 2
Santa Clara: 38
Shasta: 1
Sonoma: 3
Yolo: 1
Colorado (15):
Denver: 2
Douglas: 3
El Paso: 1
Summit: 2
Teller: 15
Connecticut (2):
Fairfield: 1
Hartford: 2
District of Columbia (5):
District of Columbia: 5
Florida (15):
Broward: 3
Charlotte: 1
Hillsborough: 2
Lee: 2
Manatee: 2
Okaloosa: 1
Polk: 15
Santa Rosa: 1
Sarasota: 1
Volusia: 1
Georgia (17):
Cherokee: 1
Cobb: 3
Fulton: 5
Jones: 17
Polk: 1
Hawaii (2):
Honolulu: 2
Illinois (12):
Cook: 7
McLean: 12
Indiana (6):
Hendricks: 2
Marion: 6
Iowa (8):
Johnson: 3
Marshall: 8
Kansas (1):
Johnson: 1
Morris: 1
Kentucky (6):
Fayette: 1
Garrard: 6
Harrison: 2
Jefferson: 1
Louisiana (1):
Avoyelles: 1
Jefferson: 1
Maryland (8):
Anne Arundel: 8
Harford: 1
Montgomery: 4
Massachusetts (92):
Berkshire: 1
Middlesex: 92
Norfolk: 6
Plymouth: 1
Suffolk: 8
Minnesota (3):
Benton: 3
Carver: 1
Ramsey: 1
Missouri (1):
Cole: 1
St. Louis: 1
Nebraska (3):
Douglas: 3
Merrick: 3
Nevada (4):
Clark: 2
Nye: 4
Washoe: 2
New Hampshire (4):
Belknap: 4
Grafton: 4
Rockingham: 1
New Jersey (15):
Bergen: 4
Hudson: 1
Middlesex: 15
New York (173):
Delaware: 173
Nassau: 17
New York: 19
Rockland: 4
Saratoga: 2
Suffolk: 1
Ulster: 1
Westchester: 98
North Carolina (7):
Chatham: 1
Randolph: 7
Wake: 1
Ohio (3):
Morrow: 3
Oklahoma (2):
Lincoln: 2
Tulsa: 1
Oregon (15):
Douglas: 1
Jackson: 2
Klamath: 1
Linn: 15
Marion: 1
Umatilla: 1
Washington: 8
Pennsylvania (12):
Delaware: 1
Juniata: 12
Montgomery: 5
Wayne: 1
Rhode Island (3):
Kent: 3
Providence: 3
South Carolina (7):
Charleston: 1
Kershaw: 4
Richland: 7
Spartanburg: 1
Tennessee (7):
Davidson: 1
Rutherford: 7
Shelby: 1
Williamson: 1
Texas (13):
Bell: 13
Bexar: 1
Collin: 1
Fort Bend: 6
Harris: 6
Utah (2):
Davis: 1
Utah: 2
Vermont (1):
Bennington: 1
Washington: 1
Virginia (7):
Fairfax: 2
Fluvanna: 7
Washington (267):
Chelan: 5
Clark: 1
Grant: 1
Jefferson: 1
King: 267
Kittitas: 1
Pierce: 4
Snohomish: 31
Spokane: 1
Wisconsin (3):
Dane: 1
Portage: 3
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"sort"
"strconv"
)
const (
statsURL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv"
fccURL = "https://geo.fcc.gov/api/census/block/find?latitude=%v&longitude=%v&format=json"
colState = 0
colCountry = 1
colLat = 2
colLon = 3
firstDataColumn = 4
)
type FCCData struct {
County FCCCounty
State FCCState
Status string
}
type FCCCounty struct {
Name string
}
type FCCState struct {
Code string
Name string
}
func LookupFCCData(lat, lon float64) *FCCData {
resp, err := http.Get(fmt.Sprintf(fccURL, lat, lon))
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
out := new(FCCData)
err = json.Unmarshal(data, out)
if err != nil {
panic(err)
}
return out
}
func main() {
resp, err := http.Get(statsURL)
if err != nil {
panic(err)
}
defer resp.Body.Close()
r := csv.NewReader(resp.Body)
r.ReuseRecord = true
perStateCounty := map[string]map[string]int{}
perState := map[string]int{}
line := 0
for {
line++
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if line == 1 {
continue
}
if record[colCountry] != "US" {
continue
}
lat, err := strconv.ParseFloat(record[colLat], 64)
if err != nil {
panic(err)
}
lon, err := strconv.ParseFloat(record[colLon], 64)
if err != nil {
panic(err)
}
geo := LookupFCCData(lat, lon)
if geo.State.Name == "" {
continue
}
if _, ok := perState[geo.State.Name]; !ok {
perState[geo.State.Name] = 0
}
if _, ok := perStateCounty[geo.State.Name]; !ok {
perStateCounty[geo.State.Name] = map[string]int{}
}
if _, ok := perStateCounty[geo.State.Name][geo.County.Name]; !ok {
perStateCounty[geo.State.Name][geo.County.Name] = 0
}
// Iterate the record in reverse to find the most recent count
for i := len(record) - 1; i > firstDataColumn; i-- {
v, err := strconv.Atoi(record[i])
if err != nil {
continue
}
if v == 0 {
break
}
perState[geo.State.Name] = v
perStateCounty[geo.State.Name][geo.County.Name] = v
break
}
}
// fmt.Println("# COVID-19 by US State")
// for state, v := range perState {
// fmt.Printf("%s: %v\n", state, v)
// }
// fmt.Println("")
statesLookup := []string{}
countiesLookup := map[string][]string{}
for state, perCounty := range perStateCounty {
if perState[state] == 0 {
continue
}
statesLookup = append(statesLookup, state)
counties := []string{}
for county, v := range perCounty {
if v == 0 {
continue
}
counties = append(counties, county)
}
sort.Strings(counties)
countiesLookup[state] = counties
}
sort.Strings(statesLookup)
fmt.Println("# COVID-19 by US County")
for _, state := range statesLookup {
fmt.Printf("%s (%v):\n", state, perState[state])
for _, county := range countiesLookup[state] {
fmt.Printf(" %s: %v\n", county, perStateCounty[state][county])
}
fmt.Println("")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment