Skip to content

Commit 2683c99

Browse files
committed
Add Maryland data code.
1 parent 0edfc54 commit 2683c99

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

maryland.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import pandas as pd
2+
3+
from api_data import ApiDataInterface
4+
from web_data import ColumnNames, WebDataGenerator, RowDataGetter
5+
6+
WEB_BASE_DIR = 'web'
7+
DATA_BASE_DIR = 'data/maryland'
8+
9+
COLUMNS = ['REPORT_NO', 'REPORT_TYPE', 'HARM_EVENT_DESC1', 'HARM_EVENT_DESC2', 'LATITUDE', 'LONGITUDE', 'YEAR']
10+
11+
HARM = {
12+
'Pedestrian': 'ped',
13+
'Bicycle': 'bike',
14+
}
15+
16+
COLUMN_NAMES = ColumnNames(latitude='LATITUDE', longitude='LONGITUDE', year='YEAR')
17+
18+
19+
class MarylandApiDataInterface(ApiDataInterface):
20+
def convert_to_df(self):
21+
df = pd.read_csv('data/Maryland_Statewide_Vehicle_Crashes.csv')
22+
df.to_pickle('data/maryland.pkl')
23+
24+
def filter_data(self):
25+
df = pd.read_pickle(f'data/maryland.pkl')
26+
for col in df.columns:
27+
df[col] = pd.to_numeric(df[col], errors='ignore')
28+
df = df[COLUMNS]
29+
df = df.loc[(df['REPORT_TYPE'] == 'Fatal Crash') | (df['REPORT_TYPE'] == 'Injury Crash')]
30+
df.to_pickle(f'data/maryland-filtered.pkl')
31+
32+
def read_data(self):
33+
return pd.read_pickle(f'data/maryland-filtered.pkl')
34+
35+
36+
class MarylandRowDataGetter(RowDataGetter):
37+
@staticmethod
38+
def item_id(row):
39+
return row['REPORT_NO']
40+
41+
@staticmethod
42+
def category(row):
43+
return HARM.get(row['HARM_EVENT_DESC1'], 'car')
44+
45+
46+
def generate_web_data():
47+
df = MarylandApiDataInterface().read_data()
48+
49+
web_data_generator = WebDataGenerator(row_data_getter=MarylandRowDataGetter(),
50+
column_names=COLUMN_NAMES,
51+
state='maryland')
52+
web_data_generator.iterate_and_save(df, latlong_interval=2)
53+
54+
55+
if __name__ == '__main__':
56+
# convert_to_df()
57+
# filter_data()
58+
# df = pd.read_pickle(f'data/maryland-filtered.pkl')
59+
print('Generating web data...')
60+
generate_web_data()
61+
print('Done.')

0 commit comments

Comments
 (0)