From af60cf93f732ecc15e465e551adf72042d806229 Mon Sep 17 00:00:00 2001 From: Chandler Swift Date: Thu, 29 Feb 2024 17:24:33 -0600 Subject: [PATCH] Initial commit --- data/.gitignore | 2 + get_data.py | 45 +++++++++++++++++ index.html | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 data/.gitignore create mode 100644 get_data.py create mode 100644 index.html diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/data/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/get_data.py b/get_data.py new file mode 100644 index 0000000..aa41f38 --- /dev/null +++ b/get_data.py @@ -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) diff --git a/index.html b/index.html new file mode 100644 index 0000000..3b166c1 --- /dev/null +++ b/index.html @@ -0,0 +1,126 @@ + + + + + + Name All the Cities + + + +
+

Name All the Cities

+
+ +
+ + +
+
+ + + +
+ + +