-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.py
144 lines (103 loc) · 3.29 KB
/
geo.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
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.15.2
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %%
"""Geography and geometry."""
import pytz
import functools
import geopy
import geopandas
import shapely
import util
# %%
@functools.cache
def _census_block_api(loc):
return util.web_get(
f'https://geo.fcc.gov/api/census/block/find?latitude={loc.lat}&longitude={loc.lon}&censusYear=2020&showall=false&format=json'
).json()
# %%
def city_to_state_code(city):
return _census_block_api(city)['State']['code']
# %%
def city_to_county(city):
"""
Note: Raises KeyError if city is not in the USA.
Return:
Name of the county including the word "County", e.g. "San Francisco County".
"""
return _census_block_api(city)['County']['name']
# %%
@functools.cache
def _timezones_df():
with util.web_get_to_file(
'https://github.com/evansiroky/timezone-boundary-builder/releases/download/2023d/timezones.shapefile.zip',
suffix='.zip',
) as f:
return geopandas.read_file('zip://' + str(f.name))
@util.cache_on_disk
def city_timezone(loc):
rows = _timezones_df()[lambda df: df.geometry.contains(shapely.Point(loc.lonlat))]
if not len(rows):
raise RuntimeError(f'Could not find timezone for {loc}')
return pytz.timezone(rows.iloc[0].tzid)
# %% tags=["active-ipynb"]
# city_timezone(locs.berkeley)
# %%
@util.cache_on_disk
def _nominatim_reverse(latlon):
# We cache to comply with Nominatim's terms of service
# https://operations.osmfoundation.org/policies/nominatim/
geolocator = geopy.Nominatim(user_agent='NestCB')
return geolocator.reverse(latlon)
# %%
def get_country_code(latlon):
return _nominatim_reverse(latlon).raw['address']['country_code']
# %%
class NoStateException(Exception):
pass
def get_state(latlon):
addr = _nominatim_reverse(latlon).raw['address']
if addr['country_code'] != 'us':
raise NoStateException(f'{latlon=} is not in the USA')
return addr['state']
# %% tags=["active-ipynb"]
# get_state((37.85353813271663, -122.29008828881683))
# %%
@functools.cache
def _zipcodes_df():
with util.web_get_to_file(
'https://www2.census.gov/geo/tiger/GENZ2020/shp/cb_2020_us_zcta520_500k.zip',
suffix='.zip',
) as f:
return geopandas.read_file('zip://' + str(f.name))
class NoZipCodeException(Exception):
pass
_NO_ZIP_CODE = '_NO_ZIP_CODE'
@util.cache_on_disk
def _get_zipcode(latlon):
lat, lon = latlon
rows = _zipcodes_df()[lambda df: df.geometry.contains(shapely.Point(lon, lat))]
# Note: We can't directly raise an exception from here because it won't be cached. Instead, we
# return a special value and have a wrapper function that can raise an exception.
if not len(rows):
return _NO_ZIP_CODE
return rows.iloc[0]['NAME20']
def get_zipcode(latlon):
ret = _get_zipcode(latlon)
if ret == _NO_ZIP_CODE:
raise NoZipCodeException(f'Failed to find ZIP code for {latlon}')
return ret
# %% tags=["active-ipynb"]
# get_zipcode((37.8, -122.4))
# %% tags=["active-ipynb"]
# get_zipcode((37.85353813271663, -122.29008828881683))