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: 2
Pima: 1
Pinal: 3
California (145):
Alameda: 3
Contra Costa: 9
Fresno: 1
Humboldt: 1
Los Angeles: 20
Madera: 1
Napa: 1
Orange: 5
Placer: 7
Riverside: 4
Sacramento: 10
San Benito: 2
San Diego: 3
San Francisco: 15
San Mateo: 9
Santa Clara: 43
Santa Cruz: 2
Shasta: 1
Solano: 3
Sonoma: 3
Ventura: 1
Yolo: 1
Colorado (15):
Arapahoe: 2
Denver: 3
Douglas: 3
Eagle: 2
El Paso: 1
Gunnison: 1
Larimer: 2
Summit: 1
Connecticut (1):
Fairfield: 1
District of Columbia (5):
District of Columbia: 5
Florida (16):
Broward: 5
Charlotte: 1
Hillsborough: 2
Lee: 2
Manatee: 2
Okaloosa: 1
Santa Rosa: 1
Volusia: 2
Georgia (16):
Cherokee: 1
Cobb: 4
DeKalb: 2
Fayette: 1
Fulton: 5
Gwinnett: 2
Polk: 1
Hawaii (1):
Honolulu: 1
Illinois (12):
Cook: 11
Kane: 1
Indiana (5):
Boone: 1
Hendricks: 2
Marion: 1
Noble: 1
Iowa (8):
Johnson: 7
Pottawattamie: 1
Kansas (1):
Johnson: 1
Kentucky (6):
Fayette: 2
Floyd: 1
Harrison: 2
Jefferson: 1
Louisiana (1):
Jefferson: 1
Maryland (8):
Harford: 1
Montgomery: 4
Prince George's: 3
Massachusetts (41):
Berkshire: 5
Middlesex: 15
Norfolk: 10
Suffolk: 10
Worcester: 1
Minnesota (2):
Carver: 1
Ramsey: 1
Missouri (1):
St. Louis: 1
Nebraska (3):
Douglas: 3
Nevada (4):
Clark: 2
Washoe: 2
New Hampshire (4):
Grafton: 3
Rockingham: 1
New Jersey (15):
Bergen: 7
Burlington: 2
Camden: 1
Hudson: 1
Monmouth: 2
Passaic: 1
Union: 1
New York (150):
Nassau: 19
New York: 25
Rockland: 4
Saratoga: 2
Suffolk: 1
Ulster: 1
Westchester: 98
North Carolina (7):
Chatham: 1
Wake: 6
Ohio (3):
Cuyahoga: 3
Oklahoma (2):
Tulsa: 2
Oregon (14):
Douglas: 1
Jackson: 2
Klamath: 1
Marion: 1
Umatilla: 1
Washington: 8
Pennsylvania (13):
Adams: 1
Delaware: 1
Monroe: 1
Montgomery: 8
Philadelphia: 1
Wayne: 1
Rhode Island (3):
Providence: 3
South Carolina (7):
Charleston: 1
Kershaw: 5
Spartanburg: 1
Tennessee (3):
Davidson: 1
Shelby: 1
Williamson: 1
Texas (16):
Collin: 3
Fort Bend: 6
Gregg: 1
Harris: 6
Utah (2):
Davis: 1
Weber: 1
Vermont (1):
Bennington: 1
Virginia (9):
Arlington: 1
Fairfax: 4
Loudoun: 1
Norfolk City: 2
Spotsylvania: 1
Washington (162):
Clark: 1
Grant: 1
Jefferson: 1
King: 116
Kitsap: 1
Kittitas: 1
Pierce: 4
Snohomish: 37
Wisconsin (3):
Dane: 2
Pierce: 1
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"sort"
"strconv"
"strings"
)
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
}
if !strings.Contains(record[colState], ",") {
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