Last active
March 16, 2020 02:29
-
-
Save kamermans/397488317c75b23414100d7e1316e96f to your computer and use it in GitHub Desktop.
COVID-19 Confirmed Cases by US County (from John's Hopkins Dataset)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# COVID-19 by US County | |
Arizona (13): | |
Maricopa: 2 | |
Pinal: 2 | |
[unknown]: 9 | |
California (426): | |
Alameda: 2 | |
Contra Costa: 9 | |
Fresno: 1 | |
Humboldt: 1 | |
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 | |
[unknown]: 325 | |
Colorado (131): | |
Denver: 2 | |
Douglas: 3 | |
El Paso: 1 | |
Summit: 1 | |
[unknown]: 124 | |
District of Columbia (16): | |
District of Columbia: 2 | |
[unknown]: 14 | |
Florida (115): | |
Broward: 3 | |
Charlotte: 1 | |
Hillsborough: 2 | |
Lee: 2 | |
Manatee: 2 | |
Okaloosa: 1 | |
Santa Rosa: 1 | |
Volusia: 1 | |
[unknown]: 102 | |
Georgia (99): | |
Cherokee: 1 | |
Cobb: 3 | |
Fulton: 5 | |
Polk: 1 | |
[unknown]: 89 | |
Hawaii (6): | |
Honolulu: 1 | |
[unknown]: 5 | |
Illinois (93): | |
Cook: 7 | |
[unknown]: 86 | |
Indiana (20): | |
Hendricks: 2 | |
Marion: 1 | |
[unknown]: 17 | |
Iowa (18): | |
Johnson: 3 | |
[unknown]: 15 | |
Kansas (8): | |
Johnson: 1 | |
[unknown]: 7 | |
Kentucky (20): | |
Fayette: 1 | |
Harrison: 2 | |
Jefferson: 1 | |
[unknown]: 16 | |
Louisiana (91): | |
Jefferson: 1 | |
[unknown]: 90 | |
Maryland (32): | |
Harford: 1 | |
Montgomery: 4 | |
[unknown]: 27 | |
Massachusetts (164): | |
Berkshire: 1 | |
Middlesex: 7 | |
Norfolk: 6 | |
Suffolk: 8 | |
[unknown]: 142 | |
Minnesota (35): | |
Carver: 1 | |
Ramsey: 1 | |
[unknown]: 33 | |
Missouri (5): | |
St. Louis: 1 | |
[unknown]: 4 | |
Nebraska (17): | |
Douglas: 3 | |
[unknown]: 14 | |
Nevada (24): | |
Clark: 2 | |
Washoe: 2 | |
[unknown]: 20 | |
New Hampshire (13): | |
Grafton: 3 | |
Rockingham: 1 | |
[unknown]: 9 | |
New Jersey (98): | |
Bergen: 4 | |
Hudson: 1 | |
[unknown]: 93 | |
New York (732): | |
Nassau: 17 | |
New York: 19 | |
Rockland: 4 | |
Saratoga: 2 | |
Suffolk: 1 | |
Ulster: 1 | |
Westchester: 98 | |
[unknown]: 590 | |
North Carolina (33): | |
Chatham: 1 | |
Wake: 1 | |
[unknown]: 31 | |
Oklahoma (7): | |
Tulsa: 1 | |
[unknown]: 6 | |
Oregon (36): | |
Douglas: 1 | |
Jackson: 2 | |
Klamath: 1 | |
Marion: 1 | |
Umatilla: 1 | |
Washington: 8 | |
[unknown]: 22 | |
Pennsylvania (66): | |
Delaware: 1 | |
Montgomery: 5 | |
Wayne: 1 | |
[unknown]: 59 | |
Rhode Island (20): | |
Providence: 3 | |
[unknown]: 17 | |
South Carolina (28): | |
Charleston: 1 | |
Kershaw: 1 | |
Spartanburg: 1 | |
[unknown]: 25 | |
Tennessee (39): | |
Davidson: 1 | |
Shelby: 1 | |
Williamson: 1 | |
[unknown]: 36 | |
Texas (72): | |
Collin: 1 | |
Fort Bend: 6 | |
Harris: 6 | |
[unknown]: 59 | |
United States Virgin Islands (1): | |
St. Thomas: 1 | |
Utah (28): | |
Davis: 1 | |
[unknown]: 27 | |
Vermont (8): | |
Bennington: 1 | |
[unknown]: 7 | |
Virginia (45): | |
Fairfax: 2 | |
[unknown]: 43 | |
Washington (643): | |
Clark: 1 | |
Grant: 1 | |
Jefferson: 1 | |
King: 83 | |
Kittitas: 1 | |
Pierce: 4 | |
Snohomish: 31 | |
[unknown]: 521 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | |
perStateOther := 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], ",") { | |
state := record[colState] | |
v := lastValue(record) | |
if _, ok := perStateOther[state]; !ok { | |
perStateOther[state] = 0 | |
} | |
perStateOther[state] += v | |
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 | |
v := lastValue(record) | |
perState[geo.State.Name] += v | |
perStateCounty[geo.State.Name][geo.County.Name] += v | |
} | |
// 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 { | |
noCountySum := 0 | |
if v, ok := perStateOther[state]; ok { | |
noCountySum = v - perState[state] | |
} | |
fmt.Printf("%s (%v):\n", state, perState[state]+noCountySum) | |
for _, county := range countiesLookup[state] { | |
fmt.Printf(" %s: %v\n", county, perStateCounty[state][county]) | |
} | |
if noCountySum > 0 { | |
fmt.Printf(" [unknown]: %v\n", noCountySum) | |
} | |
fmt.Println("") | |
} | |
} | |
func lastValue(record []string) int { | |
for i := len(record) - 1; i > firstDataColumn; i-- { | |
v, err := strconv.Atoi(record[i]) | |
if err != nil { | |
continue | |
} | |
if v == 0 { | |
continue | |
} | |
return v | |
} | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment