Skip to content

Commit

Permalink
added DC results to '24 cartography files; updated app.py, index.html…
Browse files Browse the repository at this point in the history
…, and prepublish
  • Loading branch information
tonmcg committed Dec 2, 2024
1 parent 407eb39 commit da1b778
Show file tree
Hide file tree
Showing 20 changed files with 33,708 additions and 33,558 deletions.
7 changes: 7 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def fetch_county_data(census_year, county_file):
data = response.text
return data

@app.route('/census/gazetteer/<int:census_year>/dc_sldu', methods=['GET'])
def fetch_dc_sldu_data(census_year):
url = f'https://www2.census.gov/geo/docs/maps-data/data/gazetteer/{census_year}_Gazetteer/{census_year}_Gaz_sldu_national.zip'
response = requests.get(url)
data = BytesIO(response.content)
return data

@app.route('/census/gazetteer/<int:census_year>/ak_sldl', methods=['GET'])
def fetch_ak_sldl_data(census_year):
url = f'https://www2.census.gov/geo/docs/maps-data/data/gazetteer/{census_year}_Gazetteer/{census_year}_Gaz_sldl_national.zip'
Expand Down
2 changes: 1 addition & 1 deletion cartography/2020/counties-albers-500k.topojson

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cartography/2020/districts_counties-albers-500k.topojson

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cartography/2020/us-albers-500k.topojson

Large diffs are not rendered by default.

6,405 changes: 3,206 additions & 3,199 deletions cartography/2020/us_election_results.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24,115 changes: 12,071 additions & 12,044 deletions cartography/2020/us_election_results_dot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion cartography/2020/ward_district_county-albers-500k.topojson

This file was deleted.

2 changes: 1 addition & 1 deletion cartography/2020/wards-albers-500k.topojson
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type":"Topology","arcs":[[[412,289],[-11,68]],[[401,357],[37,9],[14,51],[26,16]],[[478,433],[103,-63],[-109,-150]],[[472,220],[-60,69]],[[100,534],[27,-37],[54,19],[-7,-64],[15,-18]],[[189,434],[3,-44]],[[192,390],[-80,-26],[19,-47]],[[131,317],[-73,23],[-58,57],[100,137]],[[189,434],[73,20]],[[262,454],[26,-30],[-8,-39]],[[280,385],[-23,-6]],[[257,379],[-65,11]],[[280,385],[121,-28]],[[412,289],[-63,-30],[-34,-50]],[[315,209],[-9,39],[-43,48],[60,11],[-44,38],[-22,34]],[[100,534],[65,86],[155,-92]],[[320,528],[-44,-16],[-14,-58]],[[320,528],[158,-95]],[[315,209],[-26,-25]],[[289,184],[-38,58],[-21,-5],[-51,47],[-17,31],[-31,2]],[[472,220],[-161,-220],[-29,124],[7,60]]],"transform":{"scale":[32.074739069854616,35.99815122675218],"translate":[1610776.9640452527,306994.38727301906]},"objects":{"ward":{"type":"GeometryCollection","geometries":[{"arcs":[[0,1,2,3]],"type":"Polygon","id":"11007"},{"arcs":[[4,5,6,7]],"type":"Polygon","id":"11003"},{"arcs":[[8,9,10,11,-6]],"type":"Polygon","id":"11001"},{"arcs":[[-11,12,-1,13,14]],"type":"Polygon","id":"11006"},{"arcs":[[15,16,-9,-5]],"type":"Polygon","id":"11004"},{"arcs":[[-17,17,-2,-13,-10]],"type":"Polygon","id":"11005"},{"arcs":[[-12,-15,18,19,-7]],"type":"Polygon","id":"11002"},{"arcs":[[-19,-14,-4,20]],"type":"Polygon","id":"11008"}]}}}
{"type":"Topology","arcs":[[[303,201],[-7,48]],[[296,249],[56,53]],[[352,302],[76,-44],[-81,-104]],[[347,154],[-44,47]],[[74,372],[60,-13],[5,-57]],[[139,302],[2,-30]],[[141,272],[-59,-19],[15,-32]],[[97,221],[-54,16],[-43,40],[74,95]],[[139,302],[54,15]],[[193,317],[14,-49]],[[207,268],[-17,-4]],[[190,264],[-49,8]],[[207,268],[89,-19]],[[303,201],[-71,-55]],[[232,146],[-38,60],[-4,58]],[[74,372],[47,60],[115,-64]],[[236,368],[-43,-51]],[[236,368],[116,-66]],[[232,146],[-19,-18]],[[213,128],[-94,91],[-22,2]],[[347,154],[-118,-154],[-16,128]]],"transform":{"scale":[43.54070887753629,51.66401333469064],"translate":[1610776.9640452527,306994.38727301906]},"objects":{"ward":{"type":"GeometryCollection","geometries":[{"arcs":[[0,1,2,3]],"type":"Polygon","id":"11007"},{"arcs":[[4,5,6,7]],"type":"Polygon","id":"11003"},{"arcs":[[8,9,10,11,-6]],"type":"Polygon","id":"11001"},{"arcs":[[-11,12,-1,13,14]],"type":"Polygon","id":"11006"},{"arcs":[[15,16,-9,-5]],"type":"Polygon","id":"11004"},{"arcs":[[-17,17,-2,-13,-10]],"type":"Polygon","id":"11005"},{"arcs":[[-12,-15,18,19,-7]],"type":"Polygon","id":"11002"},{"arcs":[[-19,-14,-4,20]],"type":"Polygon","id":"11008"}]}}}

Large diffs are not rendered by default.

6,313 changes: 3,160 additions & 3,153 deletions cartography/2024/build/2024_data.csv

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cartography/2024/counties-albers-500k.topojson

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cartography/2024/districts_counties-albers-500k.topojson

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cartography/2024/us-albers-500k.topojson

Large diffs are not rendered by default.

6,401 changes: 3,204 additions & 3,197 deletions cartography/2024/us_election_results.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23,919 changes: 11,973 additions & 11,946 deletions cartography/2024/us_election_results_dot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion cartography/2024/ward_district_county-albers-500k.topojson

This file was deleted.

2 changes: 1 addition & 1 deletion cartography/2024/wards-albers-500k.topojson
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type":"Topology","arcs":[[[105,531],[28,-37],[58,19],[6,-83]],[[197,430],[4,-42]],[[201,388],[-49,-23],[-14,-50]],[[138,315],[-77,22],[-61,58],[105,136]],[[275,451],[15,58],[46,15]],[[336,524],[166,-93]],[[502,431],[-28,-17],[-14,-51],[-59,-8]],[[401,355],[-65,17],[-26,-16]],[[310,356],[-14,24]],[[296,380],[6,41],[-27,30]],[[253,248],[23,46],[29,22],[5,40]],[[401,355],[13,-58],[18,-10]],[[432,287],[-79,-1],[3,-54],[-53,-49]],[[303,183],[-50,65]],[[105,531],[68,85],[163,-92]],[[275,451],[-78,-21]],[[201,388],[15,-14],[80,6]],[[253,248],[-65,35],[-18,30],[-32,2]],[[502,431],[108,-63],[-115,-149]],[[495,219],[-63,68]],[[495,219],[-169,-219],[-30,123],[7,60]]],"transform":{"scale":[30.54987442555005,36.231905455497326],"translate":[1610776.9640452527,306994.38727301906]},"objects":{"ward":{"type":"GeometryCollection","geometries":[{"arcs":[[0,1,2,3]],"type":"Polygon","id":"11003"},{"arcs":[[4,5,6,7,8,9]],"type":"Polygon","id":"11005"},{"arcs":[[10,-8,11,12,13]],"type":"Polygon","id":"11006"},{"arcs":[[14,-5,15,-1]],"type":"Polygon","id":"11004"},{"arcs":[[16,-9,-11,17,-3]],"type":"Polygon","id":"11002"},{"arcs":[[-7,18,19,-12]],"type":"Polygon","id":"11007"},{"arcs":[[-13,-20,20]],"type":"Polygon","id":"11008"},{"arcs":[[-16,-10,-17,-2]],"type":"Polygon","id":"11001"}]}}}
{"type":"Topology","arcs":[[[76,398],[62,-14],[4,-62]],[[142,322],[4,-31]],[[146,291],[-46,-55]],[[100,236],[-56,17],[-44,43],[76,102]],[[200,339],[43,54]],[[243,393],[120,-70]],[[363,323],[-30,-51],[-42,-5]],[[291,267],[-66,0]],[[225,267],[-10,18]],[[215,285],[-15,54]],[[183,186],[42,81]],[[291,267],[22,-52]],[[313,215],[-57,0],[2,-41],[-38,-37]],[[220,137],[-37,49]],[[76,398],[49,64],[118,-69]],[[200,339],[-58,-17]],[[146,291],[69,-6]],[[183,186],[-83,50]],[[363,323],[79,-47],[-83,-112]],[[359,164],[-46,51]],[[359,164],[-123,-164],[-21,92],[5,45]]],"transform":{"scale":[42.16159140177722,48.309207273996435],"translate":[1610776.964045253,306994.38727301906]},"objects":{"ward":{"type":"GeometryCollection","geometries":[{"arcs":[[0,1,2,3]],"type":"Polygon","id":"11003"},{"arcs":[[4,5,6,7,8,9]],"type":"Polygon","id":"11005"},{"arcs":[[10,-8,11,12,13]],"type":"Polygon","id":"11006"},{"arcs":[[14,-5,15,-1]],"type":"Polygon","id":"11004"},{"arcs":[[16,-9,-11,17,-3]],"type":"Polygon","id":"11002"},{"arcs":[[-7,18,19,-12]],"type":"Polygon","id":"11007"},{"arcs":[[-13,-20,20]],"type":"Polygon","id":"11008"},{"arcs":[[-16,-10,-17,-2]],"type":"Polygon","id":"11001"}]}}}

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions cartography/prepublish
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ for currYr in "${years[@]}"; do
-each 'FID=GEOID' \
-filter-fields 'FID' \
-filter-islands min-area='10km2' \
-simplify 5% \
-simplify 2% \
-proj albersusa \
-o "${currYr}/"wards-albers-500k.topojson \
-i "${build}/cb_${previousYr}_02_sldl_500k.shp" name=district \
Expand All @@ -62,7 +62,7 @@ for currYr in "${years[@]}"; do
-proj albersusa \
-o "${currYr}/"districts-albers-500k.topojson \
-i "${build}/cb_${previousYr}_us_county_500k.shp" name=county \
-filter 'STATEFP !== "02" && STATEFP !== "69" && STATEFP !== "66" && STATEFP !== "78" && STATEFP !== "60" && STATEFP !== "72"' \
-filter 'STATEFP !== "02" && STATEFP !== "11" && STATEFP !== "69" && STATEFP !== "66" && STATEFP !== "78" && STATEFP !== "60" && STATEFP !== "72"' \
-each 'FID=GEOID' \
-filter-fields FID \
-filter-islands min-area='10km2' \
Expand All @@ -72,9 +72,9 @@ for currYr in "${years[@]}"; do
-i "${currYr}/"districts-albers-500k.topojson "${currYr}/"counties-albers-500k.topojson combine-files name=district_county \
-merge-layers \
-o "${currYr}/"districts_counties-albers-500k.topojson \
-i "${currYr}/"districts_counties-albers-500k.topojson "${currYr}/"wards-albers-500k.topojson combine-files name=ward_district_county \
-i "${currYr}/"districts_counties-albers-500k.topojson "${currYr}/"wards-albers-500k.topojson combine-files name=wards_districts_counties \
-merge-layers \
-o "${currYr}/"ward_district_county-albers-500k.topojson \
-o "${currYr}/"wards_districts_counties-albers-500k.topojson \
-i "${build}/cb_${previousYr}_us_state_500k.shp" name=state \
-filter 'STATEFP !== "69" && STATEFP !== "66" && STATEFP !== "78" && STATEFP !== "60" && STATEFP !== "72"' \
-each 'FID=GEOID' \
Expand All @@ -90,11 +90,11 @@ for currYr in "${years[@]}"; do
-simplify 5% \
-proj albersusa \
-o "${currYr}/"nation-albers-500k.topojson \
-o target=district_county,state,nation width='960' height='630' "${currYr}/"us-albers-500k.topojson
-o target=wards_districts_counties,state,nation width='960' height='630' "${currYr}/"us-albers-500k.topojson

# Election Results by County and District Choropleth
mapshaper \
-i "${currYr}/"districts_counties-albers-500k.topojson name=us_district_boundaries \
-i "${currYr}/"wards_districts_counties-albers-500k.topojson name=us_district_boundaries \
-style class="county" stroke="#000000" fill="none" stroke-width="0.1" \
-each 'state_fips=FID.slice(0,2)' \
-dissolve state_fips + name=us_state_boundaries \
Expand All @@ -110,7 +110,7 @@ for currYr in "${years[@]}"; do

# Election Results by County and District Dot Density
mapshaper \
-i "${currYr}/"districts_counties-albers-500k.topojson name=us_district_boundaries \
-i "${currYr}/"wards_districts_counties-albers-500k.topojson name=us_district_boundaries \
-each 'state_fips=FID.slice(0,2)' \
-points inner + name=dots \
-i "${build}/${currYr}_data.csv" string-fields=county_fips name=2024_data \
Expand Down
72 changes: 70 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@
censusCounties,
censusStates
);
// const dcGeographies = await this.getDCGeographies();
// await this.fetchDCResults(dcGeographies);
const akGeographies = await this.getAlaskaGeographies();
await this.fetchAlaskaResults(akGeographies);
await this.fetchElectionResults(censusStates, censusGeographies);
Expand Down Expand Up @@ -540,6 +542,71 @@
// convert back to JSON and return
return dfd.toJSON(censusGeographies);
},
// async getDCGeographies() {

// // Download the zip file content into memory
// const url = `https://us-election-server-2ae36243a2a4.herokuapp.com/census/gazetteer/${this.geographies.censusYear}/dc_sldu`;
// const response = await fetch(url, { method: "GET" });
// const arrayBuffer = await response.arrayBuffer();
// const nationalGazetteerZip = new Uint8Array(arrayBuffer);

// // Open the zip file from the in-memory content
// const zip = await JSZip.loadAsync(nationalGazetteerZip);
// const unzippedFiles = Object.keys(zip.files);

// // Find the tab-delimited text file (assuming it's a .txt file)
// let tabDelimitedFile = null;
// for (const file of unzippedFiles) {
// if (file.endsWith(".txt")) {
// tabDelimitedFile = file;
// break;
// }
// }

// if (!tabDelimitedFile) {
// console.error("No tab-delimited text file found.");
// return;
// }

// // Read the tab-delimited text file content
// const fileContent = await zip.file(tabDelimitedFile).async("string");

// // Parse the tab-delimited text file using d3
// const nationalsldusts = d3.dsvFormat('\t').parse(fileContent, function (d) {
// return {
// c_state_name: "District of Columbia", // Add District of Columbia name to state_name column
// geoid: d['GEOID'],
// c_sldust_name: d['NAME']
// };
// });
// // Convert the national sldusts data to a DataFrame
// const nationalsldusts_df = new dfd.DataFrame(nationalsldusts, {
// index: null,
// columns: ["c_state_name", "geoid", "c_sldust_name"],
// dtypes: ["string", "string", "string"]
// });

// // filter the national sldusts dataframe for District of Columbia and reset the index
// let dcGeographies = nationalsldusts_df.loc({
// rows: nationalsldusts_df["geoid"].map(
// (geo) => geo.startsWith("11")
// ),
// }).resetIndex({ drop: true });

// // Reorder columns
// dcGeographies = dcGeographies.loc({
// columns: ["c_state_name", "geoid", "c_sldust_name"]
// });
// // Log the shape and data types of the resulting DataFrame
// console.log("Shape:", dcGeographies.shape);
// console.log("Data Types:", dcGeographies.ctypes);

// // Print the first 100 rows of the resulting DataFrame
// dcGeographies.head(100).print();

// // convert back to JSON and return
// return dfd.toJSON(dcGeographies);
// },
async getAlaskaGeographies() {

// Download the zip file content into memory
Expand Down Expand Up @@ -818,6 +885,7 @@
console.error("Error fetching Alaska results:", error);
}
},
// async fetchDCResults() { dcGeographies },
async fetchAlaskaResults(akGeographies) {
try {
// Fetch the JSON data
Expand Down Expand Up @@ -1012,7 +1080,7 @@

const countyPath = group
.selectAll(".counties")
.data(topojson.feature(us, us.objects.district_county).features)
.data(topojson.feature(us, us.objects.wards_districts_counties).features)
.enter()
.append("path")
.attr("class", "county")
Expand All @@ -1035,7 +1103,7 @@

const dotDensity = group
.selectAll(".dot")
.data(topojson.feature(us, us.objects.district_county).features)
.data(topojson.feature(us, us.objects.wards_districts_counties).features)
.enter()
.append("circle")
.attr("class", "dot")
Expand Down

0 comments on commit da1b778

Please sign in to comment.