Skip to content

Instantly share code, notes, and snippets.

@Jeremiah-England
Last active July 31, 2022 00:34
Show Gist options
  • Save Jeremiah-England/901f5a6341ac309b630b75d66e0c6fef to your computer and use it in GitHub Desktop.
Save Jeremiah-England/901f5a6341ac309b630b75d66e0c6fef to your computer and use it in GitHub Desktop.
Download a shapefile will all the US Census Tracts.
import geopandas as gpd
import os
import requests
import shutil
# downloading all the ANSI codes for all the states and DC, skipping the territories
states_list = requests.get('https://www2.census.gov/geo/docs/reference/state.txt').text.split('\n')[1:52]
# states_list should =
'''
['01|AL|Alabama|01779775',
'02|AK|Alaska|01785533',
'04|AZ|Arizona|01779777',
'05|AR|Arkansas|00068085',
'06|CA|California|01779778',
'08|CO|Colorado|01779779',
'09|CT|Connecticut|01779780',
'10|DE|Delaware|01779781',
'11|DC|District of Columbia|01702382',
'12|FL|Florida|00294478',
'13|GA|Georgia|01705317',
'15|HI|Hawaii|01779782',
'16|ID|Idaho|01779783',
'17|IL|Illinois|01779784',
'18|IN|Indiana|00448508',
'19|IA|Iowa|01779785',
'20|KS|Kansas|00481813',
'21|KY|Kentucky|01779786',
'22|LA|Louisiana|01629543',
'23|ME|Maine|01779787',
'24|MD|Maryland|01714934',
'25|MA|Massachusetts|00606926',
'26|MI|Michigan|01779789',
'27|MN|Minnesota|00662849',
'28|MS|Mississippi|01779790',
'29|MO|Missouri|01779791',
'30|MT|Montana|00767982',
'31|NE|Nebraska|01779792',
'32|NV|Nevada|01779793',
'33|NH|New Hampshire|01779794',
'34|NJ|New Jersey|01779795',
'35|NM|New Mexico|00897535',
'36|NY|New York|01779796',
'37|NC|North Carolina|01027616',
'38|ND|North Dakota|01779797',
'39|OH|Ohio|01085497',
'40|OK|Oklahoma|01102857',
'41|OR|Oregon|01155107',
'42|PA|Pennsylvania|01779798',
'44|RI|Rhode Island|01219835',
'45|SC|South Carolina|01779799',
'46|SD|South Dakota|01785534',
'47|TN|Tennessee|01325873',
'48|TX|Texas|01779801',
'49|UT|Utah|01455989',
'50|VT|Vermont|01779802',
'51|VA|Virginia|01779803',
'53|WA|Washington|01779804',
'54|WV|West Virginia|01779805',
'55|WI|Wisconsin|01779806',
'56|WY|Wyoming|01779807']
'''
# looping through the states, pulling the shapefiles from the census bureau website.
directory = 'tracks' # define the directory to house the state shapefiles.
os.mkdir(directory)
for state in states_list:
ids = state.split('|')
url = f'https://www2.census.gov/geo/tiger/GENZ2017/shp/cb_2017_{ids[0]}_tract_500k.zip'
target_path = f'tracts/{"_".join(ids)}.zip'
response = requests.get(url, stream=True)
handle = open(target_path, "wb")
for chunk in response.iter_content(chunk_size=512):
if chunk: # filter out keep-alive new chunks
handle.write(chunk)
handle.close()
# now that the 'tracts' directory is full of all the shapefiles, we need to
# consolidate them into one big dataframe.
gdf = gpd.GeoDataFrame()
for filename in os.listdir(directory):
gdf = gdf.append(gpd.read_file(f'zip://{directory}/{filename}'))
# putting the GeoDataFame into a zip file
def zip_gdf(gdf, zip_name):
os.mkdir(zip_name)
gdf.to_file(f'{zip_name}/{zip_name}.shp')
shutil.make_archive(zip_name, 'zip', zip_name)
shutil.rmtree(zip_name)
zip_gdf(gdf, 'us_census_tracts')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment