1+ import geopandas as gpd
2+ from shapely .geometry import Polygon
3+ import csv
4+ from tqdm import tqdm
5+ import pandas as pd
6+ import math
7+ import copy
8+ import argparse
9+
10+
11+ def parse_args ():
12+ parser = argparse .ArgumentParser (description = 'Process Socioeconomic Data for Boston' )
13+ parser .add_argument ('--boston-building-path' , required = True , help = 'File path to the Boston Buildings Inventory dataset' )
14+ parser .add_argument ('--vulnerabilities' , required = True , type = str , nargs = '+' , help = "Socioeconomic columns to process from the Boston buildings inventory" )
15+ parser .add_argument ('--output-path' ,required = True , help = 'Output CSV path' )
16+ args = parser .parse_args ()
17+ return args
18+
19+ def unique_census_tracts (building_inventory_df , census_tract_column_name ):
20+ unique_census_tracts = set ()
21+ unique_building_use_classes = set ()
22+ for index , row in building_inventory_df .iterrows ():
23+ row_census_tract = row [census_tract_column_name ]
24+ if (not math .isnan (row_census_tract )) and (row_census_tract not in unique_census_tracts ):
25+ unique_census_tracts .add (row_census_tract )
26+ return unique_census_tracts
27+
28+ def process_socioecon (unique_ct , vulnerable_pops , boston_df ):
29+ output = {}
30+ non_percent = set ()
31+
32+ for vulnerable in vulnerable_pops :
33+ if 'pop' in vulnerable :
34+ non_percent .add (vulnerable )
35+ for ct in unique_ct :
36+ int_ct = int (ct )
37+ inner_dict = {}
38+ for vulnerable in vulnerable_pops :
39+ inner_dict [vulnerable ] = 0
40+ for pop in vulnerable_pops :
41+ row = boston_df .loc [boston_df ["census_tract_number" ] == int_ct ]
42+ if pop in non_percent :
43+ socio = row .iloc [0 ].loc [pop ]
44+ else :
45+ socio = (row .iloc [0 ].loc [pop ]) / 100
46+
47+ inner_dict [pop ] = socio
48+ row_census_tract_str = str (int (ct ))
49+ row_census_tract_fixed = str (int (row_census_tract_str [5 :9 ])) + "." + row_census_tract_str [- 2 :]
50+ output [row_census_tract_fixed ] = inner_dict
51+ return output
52+
53+
54+
55+ if __name__ == '__main__' :
56+ args = parse_args ()
57+ boston_building_inventory_path = args .boston_building_path
58+ vulnerable_pops = args .vulnerabilities
59+ output_file = args .output_path
60+ boston_building_inventory_df = pd .read_csv (boston_building_inventory_path )
61+
62+ unique_census_tracts = unique_census_tracts (boston_building_inventory_df , "census_tract_number" )
63+ output_dict = process_socioecon (unique_census_tracts , vulnerable_pops , boston_building_inventory_df )
64+
65+ df = pd .DataFrame .from_dict (output_dict )
66+ df = df .transpose ()
67+ df .to_csv (output_file )
68+
0 commit comments