-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmap_tiger.py
53 lines (40 loc) · 1.43 KB
/
map_tiger.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
#%%
import os
import pandas as pd
import geopandas as gpd
from dotenv import load_dotenv
import match.helpers as helpers
load_dotenv()
DATA_PATH = os.environ["WSB_STAGING_PATH"]
EPSG = os.environ["WSB_EPSG"]
# Bring in the FIPS -> State Abbr crosswalk
state_cw = (pd
.read_csv("../crosswalks/state_fips_to_abbr.csv", dtype="str")
.set_index("code"))
#%%
tiger = gpd.read_file(os.path.join(DATA_PATH, "tiger_places_clean.gpkg"))
# Ensure strings with leading zeros
tiger["statefp"] = tiger["statefp"].astype("int").astype("str").str.zfill(2)
# Augment with state code
tiger = (tiger
.join(state_cw, on="statefp", how="left"))
# TODO - It would be nice to also know county, zip code, etc.,
# but it doesn't seem like we can get this from the data as it stands.
# Might need a lookup table.
#%%
df = gpd.GeoDataFrame().assign(
source_system_id = tiger["geoid"],
source_system = "tiger",
contributor_id = "tiger." + tiger["geoid"],
master_key = "UNK-tiger." + tiger["geoid"],
name = tiger["name"],
state = tiger["state"],
population_served_count = tiger["population"].astype(pd.Int64Dtype()),
geometry = tiger["geometry"],
centroid_lat = tiger["intptlat"],
centroid_lon = tiger["intptlon"],
centroid_quality = "CALCULATED FROM GEOMETRY",
geometry_source_detail = "2020 Census"
)
#%%
helpers.load_to_postgis("tiger", df)