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 (9):
Maricopa: 3
Pima: 1
Pinal: 5
California (175):
Alameda: 5
Calaveras: 2
Contra Costa: 10
Fresno: 3
Humboldt: 1
Los Angeles: 27
Madera: 1
Marin: 1
Napa: 1
Orange: 5
Placer: 7
Riverside: 6
Sacramento: 11
San Benito: 2
San Diego: 3
San Francisco: 14
San Mateo: 15
Santa Clara: 48
Santa Cruz: 2
Shasta: 1
Solano: 3
Sonoma: 3
Stanislaus: 2
Ventura: 1
Yolo: 1
Colorado (27):
Arapahoe: 3
Denver: 6
Douglas: 3
Eagle: 4
El Paso: 1
Gunnison: 5
Jefferson: 3
Larimer: 1
Summit: 1
Connecticut (3):
Fairfield: 2
Litchfield: 1
Delaware (1):
New Castle: 1
District of Columbia (4):
District of Columbia: 4
Florida (24):
Alachua: 1
Broward: 4
Charlotte: 1
Collier: 3
Hillsborough: 3
Lee: 2
Manatee: 2
Nassau: 1
Okaloosa: 1
Pasco: 1
Pinellas: 2
Santa Rosa: 1
Volusia: 2
Georgia (22):
Charlton: 1
Cherokee: 1
Cobb: 7
DeKalb: 2
Fayette: 1
Fulton: 7
Gwinnett: 2
Polk: 1
Hawaii (2):
Honolulu: 2
Illinois (25):
Cook: 22
Kane: 1
Lake: 1
McHenry: 1
Indiana (10):
Boone: 1
Hendricks: 2
Howard: 1
Johnson: 3
Marion: 1
Noble: 1
St. Joseph: 1
Iowa (13):
Johnson: 12
Pottawattamie: 1
Kansas (1):
Johnson: 1
Kentucky (9):
Fayette: 2
Floyd: 1
Harrison: 5
Jefferson: 1
Louisiana (6):
Jefferson: 1
Orleans: 5
Maryland (9):
Harford: 1
Montgomery: 5
Prince George's: 3
Massachusetts (92):
Berkshire: 7
Essex: 1
Middlesex: 41
Norfolk: 22
Suffolk: 20
Worcester: 1
Michigan (2):
Oakland: 1
Wayne: 1
Minnesota (5):
Anoka: 1
Carver: 1
Olmsted: 1
Ramsey: 2
Missouri (1):
St. Louis: 1
Nebraska (5):
Douglas: 4
Knox: 1
Nevada (7):
Clark: 5
Washoe: 2
New Hampshire (5):
Grafton: 3
Rockingham: 2
New Jersey (23):
Bergen: 11
Burlington: 2
Camden: 1
Hudson: 1
Middlesex: 2
Monmouth: 4
Passaic: 1
Union: 1
New Mexico (3):
Bernalillo: 1
Socorro: 2
New York (220):
Nassau: 28
New York: 55
Rockland: 6
Saratoga: 2
Suffolk: 6
Ulster: 2
Westchester: 121
North Carolina (7):
Chatham: 1
Wake: 6
Ohio (4):
Cuyahoga: 3
Stark: 1
Oklahoma (2):
Tulsa: 2
Oregon (19):
Deschutes: 1
Douglas: 1
Jackson: 2
Klamath: 1
Marion: 2
Multnomah: 1
Polk: 1
Umatilla: 2
Washington: 8
Pennsylvania (16):
Adams: 1
Bucks: 1
Delaware: 1
Monroe: 2
Montgomery: 9
Philadelphia: 1
Wayne: 1
Rhode Island (5):
Providence: 5
South Carolina (10):
Charleston: 1
Kershaw: 7
Lancaster: 1
Spartanburg: 1
South Dakota (8):
Beadle: 1
Bon Homme: 1
Charles Mix: 1
Davison: 1
Minnehaha: 3
Pennington: 1
Tennessee (9):
Davidson: 2
Shelby: 1
Sullivan: 1
Williamson: 5
Texas (21):
Collin: 3
Dallas: 2
Fort Bend: 6
Gregg: 1
Harris: 7
Montgomery: 1
Tarrant: 1
Utah (3):
Davis: 1
Summit: 1
Weber: 1
Vermont (1):
Bennington: 1
Virginia (9):
Arlington: 1
Fairfax: 3
Hanover: 1
Loudoun: 1
Norfolk City: 2
Spotsylvania: 1
Washington (282):
Clark: 1
Grant: 1
Island: 1
Jefferson: 1
King: 190
Kitsap: 2
Kittitas: 1
Pierce: 14
Skagit: 1
Snohomish: 68
Thurston: 1
Whatcom: 1
Wisconsin (4):
Dane: 2
Pierce: 2
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