<jupyter_start><jupyter_text>Creating Cartographic Congressional Districts<jupyter_code>import pandas as pd
import geopandas as gpd
import topojson as tp
import json
import matplotlib.pyplot as plt
import us<jupyter_output><empty_output><jupyter_text>GeoPandas- State data from [```us```](https://github.com/unitedstates/python-us) package.- Only include 50 states + DC  Congressional Districts and States- Congressional district and State Shapefiles [US Census Bureau](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.html).<jupyter_code>cd = gpd.read_file('../cb_2019_us_cd116_5m/cb_2019_us_cd116_5m.shp')
st = gpd.read_file('../cb_2019_us_state_5m/cb_2019_us_state_5m.shp')
cd.head(3)
st.head(3)
# Merge GeoDataFrames on state FIPS code
df = pd.merge(cd, st, on='STATEFP', how='left', suffixes=('_cd', '_state'))
# Only keep 50 states + DC
states = [ state.abbr for state in us.states.STATES ] + ['DC']
df = df.loc[df['STUSPS'].isin(states)]<jupyter_output><empty_output><jupyter_text>Parallels and Geographic Centers for Projections- Create features for Albers projections:    - State Latitude Parallels    - State Lat-Long Centers relative to prime meridian<jupyter_code>df['state_parallels'] = df['geometry_state'].bounds[['miny', 'maxy']].values.tolist()
df['state_parallels'] = df['state_parallels'].map(lambda x: str(x))
df['state_center'] = df['geometry_state'].centroid.map(lambda x: x.bounds[:2])
df['state_center'] = df['state_center'].map(lambda x: str(list(x)))
col_dict = {
    'CD116FP': 'cd116',
    'STUSPS': 'state_abbr',
    'NAME': 'state',
    'geometry_cd': 'geometry'
}
df.rename(columns=col_dict, inplace=True)
df = df[['state', 'state_abbr', 'cd116', 'state_parallels', 'state_center', 'geometry']]
df.head(3)
# Generate TopoJSON files of individual states
states = list(df['state_abbr'].unique())
error = []
for state in states:
    try:
        topo = tp.Topology(data=df.loc[df['state_abbr'] == state])
    except:
        error.append(state)
    topo.to_json(f'./data/{state}.topo.json')<jupyter_output><empty_output><jupyter_text>National Map with States- Downloaded data from composite Albers [TopoJSON GitHub](https://github.com/topojson/us-atlasstates-10m.json).<jupyter_code>usa = gpd.read_file('./data/usa.topo.json')
usa.head(3)
# Map FIPS code to state abbreviation
fips_dict = { state.fips: state.abbr for state in us.states.STATES }
usa['id'] = usa['id'].map(fips_dict)
# Find DC
usa.loc[usa['id'].isna()]
# Add DC
usa.at[27, 'id'] = 'DC'
# TopoJSON state data is mapped to SVG, the top-left corner is origin
usa.plot().invert_yaxis()
topo = tp.Topology(data=usa)
topo.to_json('./data/usa.topo.json')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15762273901808785, 'fraction_numerical': 0.017718715393133997, 'mean_word_length': 5.860759493670886, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '572967', 'license': 'MIT', 'n_tokens_mistral': 1033, 'n_tokens_neox': 990, 'n_words': 201, 'path': 'notebooks/acq_geo.ipynb', 'repo_name': 'wplam107/rep_db', 'size': '2709'}