-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-fips.py
More file actions
executable file
·64 lines (49 loc) · 1.52 KB
/
add-fips.py
File metadata and controls
executable file
·64 lines (49 loc) · 1.52 KB
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
#!/usr/bin/env python3
import sys, time, requests
DATASET = "https://geo.fcc.gov/api/census/block/find"
CACHE = "fips-cache.tsv"
SLEEP = 0.1
mem_cache = {}
with open(CACHE) as fh:
for line in fh:
lat, lon, fips = map(float, line.rstrip().split("\t"))
mem_cache[(lat, lon)] = fips
def local_lookup(lat, lon):
return mem_cache.get((lat, lon), None)
def local_save(lat, lon, fips):
with open(CACHE, "a") as fh:
fh.write("\t".join(map(str, [lat, lon, fips])) + "\n")
mem_cache[(lat, lon)] = fips
def fips(lat, lon):
val = local_lookup(lat, lon)
if val is not None:
return val
resp = requests.get(
"%s?latitude=%s&longitude=%s&showall=true&format=json" % (DATASET, lat, lon)
)
time.sleep(SLEEP)
assert 200 <= resp.status_code < 300
data = resp.json()
assert data["status"] == "OK", data["statusMessage"]
val = data["County"]["FIPS"]
if val == "" or val is None:
val = float("NaN")
else:
val = float(val)
local_save(lat, lon, val)
return val
def main():
tsv = iter(sys.stdin)
header = next(tsv).rstrip().split("\t")
col = dict((v, k) for k, v in enumerate(header))
print("\t".join(header + ["fips"]))
while True:
try:
line = next(tsv).rstrip().split("\t")
except StopIteration:
break
lat, lon = (float(line[col["lat"]]), float(line[col["lon"]]))
val = fips(lat, lon)
print("\t".join(line + [str(val)]))
if __name__ == "__main__":
main()