-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·150 lines (104 loc) · 5.19 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
import requests
import csv
import json
from time import strftime
today = strftime("%A, %d %b %Y")
settings = {"date": today}
def clean(geo, btype, data):
'''Strips geojson input of unneeded properties and adds new properties looked up in data.'''
out = {"type": "Feature", "geometry": geo["geometry"], "name": geo["properties"]["NAMELSAD"], "properties": {
"STATE_NAME": geo["properties"]["STATE_NAME"],
"NAME": geo["properties"]["NAME"],
"STUSPS": geo["properties"]["STUSPS"]
}}
out['properties']['description'] = data[out['properties']['STATE_NAME']]['counties'][out['properties']['NAME']]['description']
out['properties']['num_report_' + btype] = data[out['properties']['STATE_NAME']]['counties'][out['properties']['NAME']]['num_report_' + btype]
if btype == 'cap':
out['properties']['num_birds_' + btype] = data[out['properties']['STATE_NAME']]['counties'][out['properties']['NAME']]['num_birds_' + btype]
return out
## DOWNLOAD FROM USDA ##
overwrite = True # for overwriting data files
if overwrite:
commercial = requests.get("https://www.aphis.usda.gov/animal_health/data-csv/hpai-commercial-backyard-flocks.csv").text
wild = requests.get("https://www.aphis.usda.gov/animal_health/data-csv/hpai-wild-birds.csv").text
with open("./data/hpai-commercial-backyard-flocks.csv", "w+") as f:
f.write(commercial)
with open("./data/hpai-wild-birds.csv", "w+") as f:
f.write(wild)
## PROCESS COMMERCIAL FLOCK DATA ##
data = {}
with open("./data/hpai-commercial-backyard-flocks.csv", "r") as f:
creader = csv.reader(f, delimiter=',', quotechar='"')
header = next(creader)
for row in creader:
state = row[0]
county = row[1]
ftype = row[3]
fsize = row[4]
if state not in data:
data[state] = {'counties': {}, 'num_birds_cap': 0, 'num_report_cap': 0}
if county not in data[state]['counties']:
data[state]['counties'][county] = {'description': '', 'num_birds_cap': 0, 'num_report_cap': 0}
data[state]['counties'][county]['description'] += ftype + ': ' + fsize + '<br>'
data[state]['counties'][county]['num_birds_cap'] += int(fsize.replace(',', ''))
data[state]['counties'][county]['num_report_cap'] += 1
data[state]['num_birds_cap'] += int(fsize.replace(',', '')) # can also automate this at the end
data[state]['num_report_cap'] += 1
# create set of (state, county) pairs
x = set()
for s in data:
for c in data[s]['counties']:
x.add((s, c))
# print(data[s]['counties'][c]['num_report_cap']) # data for histogram
out = {"type": "FeatureCollection", "name": "HPAI Counties CAPTIVE", "features": [],
"crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::4269"}}
} # geojson to export
with open("./data/cb_2020_us_county_20m.geojson", 'r') as f:
counties = json.loads(f.read())
for county in counties['features']:
if (county['properties']['STATE_NAME'], county['properties']['NAME']) in x:
x.remove((county['properties']['STATE_NAME'], county['properties']['NAME']))
out['features'].append(clean(county, 'cap', data))
print(x) # TODO: fix these counties not being found
with open("./hpai_counties_cap.geojson", "w", encoding="utf-8") as f:
json.dump(out, f)
## PROCESS WILD BIRD DATA ##
data = {}
with open("./data/hpai-wild-birds.csv", "r") as f:
creader = csv.reader(f, delimiter=',', quotechar='"')
header = next(creader)
for row in creader:
state = row[0]
county = row[1]
date = row[2]
ftype = row[4]
if state not in data:
data[state] = {'counties': {}, 'num_report_wild': 0}
if county not in data[state]['counties']:
data[state]['counties'][county] = {'description': '', 'num_report_wild': 0}
data[state]['counties'][county]['description'] += date + ': ' + ftype + '<br>'
data[state]['counties'][county]['num_report_wild'] += 1
data[state]['num_report_wild'] += 1
# create set of (state, county) pairs
x = set()
for s in data:
for c in data[s]['counties']:
x.add((s, c))
# print(data[s]['counties'][c]['num_report_wild']) # data for histogram
out = {"type": "FeatureCollection", "name": "HPAI Counties WILD", "features": [],
"crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::4269"}}
} # geojson to export
with open("./data/cb_2020_us_county_20m.geojson", 'r') as f:
counties = json.loads(f.read())
for county in counties['features']:
if (county['properties']['STATE_NAME'], county['properties']['NAME']) in x:
x.remove((county['properties']['STATE_NAME'], county['properties']['NAME']))
out['features'].append(clean(county, 'wild', data))
print(x) # TODO: fix these counties not being found
with open("./hpai_counties_wild.geojson", "w", encoding="utf-8") as f:
json.dump(out, f)
## Process data for settings
# TODO: get distribution data and calculate classes
with open("./settings.json", "w", encoding="utf-8") as f:
json.dump(settings, f)