-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_functions.py
165 lines (142 loc) · 4.53 KB
/
api_functions.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import requests
import json
import geopandas as gpd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import zipfile
import io
import glob
from us import states
_TIGER_URL = "https://www2.census.gov/geo/tiger/"
_RETAIL_SERVICE_URL = ("https://services1.arcgis.com/Hp6G80Pky0om7QvQ/"
"arcgis/rest/services/Retail_Service_Territories/"
"FeatureServer/0/query?")
AVAILABLE_COLUMNS = [
"ID",
"NAME",
"ADDRESS",
"CITY",
"STATE",
"ZIP",
"TELEPHONE",
"TYPE",
"COUNTRY",
"NAICS_CODE",
"NAICS_DESC",
"SOURCE",
"SOURCEDATE",
"VAL_METHOD",
"VAL_DATE",
"WEBSITE",
"REGULATED",
"CNTRL_AREA",
"PLAN_AREA",
"HOLDING_CO",
"SUMMR_PEAK",
"WINTR_PEAK",
"SUMMER_CAP",
"WINTER_CAP",
"NET_GEN",
"PURCHASED",
"NET_EX",
"RETAIL_MWH",
"WSALE_MWH",
"TOTAL_MWH",
"TRANS_MWH",
"CUSTOMERS",
"YEAR",
"Shape__Area",
"Shape__Length"]
RETAIL_SERVICE_COLUMNS = ["CNTRL_AREA",
"PLAN_AREA",
"HOLDING_CO",
"NET_GEN",
"PURCHASED",
"RETAIL_MWH",
"WSALE_MWH",
"TOTAL_MWH",
"TRANS_MWH",
"CUSTOMERS",
"YEAR",
"NET_EX",
"NAME",
"REGULATED",
"STATE",
"ID",
"NAICS_CODE",
"NAICS_DESC"]
def get_tiger_files(year, state_abbr, feature='tract'):
"""
This function retrievs a TIGER shapefile from the United States Census
website.
Parameters
----------
year : int
The shapefile year of interest.
state_abbr : str
The abbreviation for the state of interest.
feature : str, optional
Indicates which TIGER file data feature to extract, by default 'tract'.
"""
try:
state = states.lookup(state_abbr)
assert state, f"{state_abbr} is not a state in the U.S."
except AssertionError as error:
raise error
_FEATURE_FILE = {'tract': f'TRACT/tl_{year}_{state.fips}_tract.zip',
'blockgroup': f'BG/tl_{year}_{state.fips}_bg.zip',
'county': f"COUNTY/tl_{year}_us_county.zip"}
data_route = f"TIGER{year}/{_FEATURE_FILE[feature]}"
geo_df = gpd.read_file(_TIGER_URL + data_route)
return geo_df
def get_retail_service_area(state_name=None,
crs=4326,
columns=RETAIL_SERVICE_COLUMNS):
try:
state = states.lookup(state_name)
assert state_name, f"{state_name} is not a state in the U.S."
except AssertionError as error:
raise error
if columns != RETAIL_SERVICE_COLUMNS:
for col in columns:
if col not in AVAILABLE_COLUMNS:
print(
f"{col} not in available columns. Must be one of:\n {AVAILABLE_COLUMNS}")
raise KeyError
state_field = f"where=STATE%20%3D%20'{state.abbr}'" if state_name else ""
crs_field = f"outSR={crs}"
format_field = f"f=json"
return_fields = f"outFields={','.join(columns)}"
params = "&".join([state_field, return_fields, crs_field, format_field])
return _RETAIL_SERVICE_URL + params
def get_county_fips(state_name, county_name):
"""
This function retrieves the FIPS code for a county given
the name of the county. The `county_name` parameter must
be the name only. It should not have "county" at the end.
Example:
Parameters
----------
state_name : str
The name of the state. E.g., "Kansas" or "Idaho"
county_name : str
The name of the county. E.g., "Cook", "Wyandotte."
Should not include "county." So, "Cook County" would be
incorrect.
Raises
------
error
Assertion error if the state name cannot be found.
"""
try:
state = states.lookup(state_name)
assert state_name, f"{state_name} is not a state in the U.S."
except AssertionError as error:
raise error
counties = pd.read_html(
(f"https://en.wikipedia.org/wiki/"
f"List_of_counties_in_{state_name.capitalize()}"))[1].set_index('County')
county_fips = counties.at[county_name.capitalize() + ' County',
counties.columns[0]]
return county_fips