Initial commit
This commit is contained in:
commit
af60cf93f7
3 changed files with 173 additions and 0 deletions
45
get_data.py
Normal file
45
get_data.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import requests
|
||||
import csv
|
||||
import json
|
||||
|
||||
# https://www2.census.gov/programs-surveys/popest/technical-documentation/file-layouts/2020-2022/SUB-EST2022.pdf
|
||||
INCORPORATED_PLACE = "162"
|
||||
|
||||
res = requests.get("https://www2.census.gov/programs-surveys/popest/datasets/2020-2022/cities/totals/sub-est2022.csv")
|
||||
res.raise_for_status()
|
||||
|
||||
cities_by_state = {}
|
||||
for line in csv.DictReader(res.content.decode('utf-8-sig').split('\n')):
|
||||
if line['SUMLEV'] != INCORPORATED_PLACE:
|
||||
continue
|
||||
|
||||
if not line['STNAME'] in cities_by_state:
|
||||
cities_by_state[line['STNAME']] = []
|
||||
|
||||
cities_by_state[line['STNAME']].append({
|
||||
"name": " ".join(line['NAME'].split(" ")[:-1]), # Remove "city" or "town" from the end
|
||||
"pop": int(line['POPESTIMATE2022']),
|
||||
})
|
||||
|
||||
for state, cities in cities_by_state.items():
|
||||
cities.sort(key=lambda i: i["pop"], reverse=True)
|
||||
|
||||
with open(f"data/{state}.json", 'w') as f:
|
||||
f.write(json.dumps(cities))
|
||||
|
||||
with open(f"data/states.json", 'w') as f:
|
||||
f.write(json.dumps(list(cities_by_state.keys())))
|
||||
|
||||
# ----- MAP -----
|
||||
|
||||
import subprocess
|
||||
|
||||
CMD="""
|
||||
curl --silent --remote-name https://www2.census.gov/geo/tiger/GENZ2022/shp/cb_2022_us_state_20m.zip
|
||||
unzip -q -o cb_2022_us_state_20m.zip
|
||||
ogr2ogr -f GeoJSON data/states.geojson cb_2022_us_state_20m.shp
|
||||
sed -i '/^"crs":/d' data/states.geojson
|
||||
rm cb_2022_us_state_20m.*
|
||||
"""
|
||||
|
||||
subprocess.run(CMD, shell=True)
|
||||
Loading…
Add table
Add a link
Reference in a new issue