-
Notifications
You must be signed in to change notification settings - Fork 177
US Census Bureau shapefiles example #1496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| .DS_Store | ||
| /dist/ | ||
| node_modules/ | ||
| yarn-error.log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [Framework examples →](../) | ||
|
|
||
| # U.S. Census Bureau data loader | ||
|
|
||
| View live: <https://observablehq.observablehq.cloud/framework-example-loader-census/> | ||
|
|
||
| This Observable Framework example demonstrates how to write a bash data loader that downloads high-resolution shapefiles from the US Census Bureau, and converts them into a web-friendly format. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export default { | ||
| root: "src" | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "type": "module", | ||
| "private": true, | ||
| "scripts": { | ||
| "clean": "rimraf src/.observablehq/cache", | ||
| "build": "rimraf dist && observable build", | ||
| "dev": "observable preview", | ||
| "deploy": "observable deploy", | ||
| "observable": "observable" | ||
| }, | ||
| "dependencies": { | ||
| "@observablehq/framework": "latest", | ||
| "shapefile": "^0.6.6", | ||
| "topojson-client": "^3.1.0", | ||
| "topojson-server": "^3.0.1", | ||
| "topojson-simplify": "^3.0.3" | ||
| }, | ||
| "devDependencies": { | ||
| "rimraf": "^5.0.5" | ||
| }, | ||
| "engines": { | ||
| "node": ">=18" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /.observablehq/cache/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Download the ZIP archive from the Census Bureau (if needed). | ||
| if [ ! -f src/.observablehq/cache/cb_2023_06_cousub_500k.zip ]; then | ||
| curl -o src/.observablehq/cache/cb_2023_06_cousub_500k.zip 'https://www2.census.gov/geo/tiger/GENZ2023/shp/cb_2023_06_cousub_500k.zip' | ||
| fi | ||
|
|
||
| # Unzip the ZIP archive to extract the shapefile. | ||
| unzip -oqd src/.observablehq/cache src/.observablehq/cache/cb_2023_06_cousub_500k.zip | ||
|
|
||
| # Convert the shapefile to TopoJSON, simplify, and merge counties. | ||
| shp2json --encoding utf-8 -n src/.observablehq/cache/cb_2023_06_cousub_500k.shp > src/.observablehq/cache/cb_2023_06_cousub_500k.json | ||
|
|
||
| geo2topo -q 1e5 -n counties=src/.observablehq/cache/cb_2023_06_cousub_500k.json \ | ||
| | toposimplify -f -s 1e-7 \ | ||
| | topomerge state=counties | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Census boundaries | ||
|
|
||
| Here’s how to fetch high-resolution shapefiles from the [U.S. Census Bureau](https://www.census.gov/geographies/mapping-files/time-series/geo/cartographic-boundary.html) and convert them into a web-friendly format in a bash data loader. | ||
|
|
||
| First, you’ll need to install a few packages: | ||
|
|
||
| ```sh | ||
| npm install shapefile topojson-client topojson-server topojson-simplify | ||
| ``` | ||
|
|
||
| Next, here’s a bash script, `ca.json.sh`: | ||
|
|
||
| ```bash | ||
| # Download the ZIP archive from the Census Bureau (if needed). | ||
| if [ ! -f src/.observablehq/cache/cb_2023_06_cousub_500k.zip ]; then | ||
| curl -o src/.observablehq/cache/cb_2023_06_cousub_500k.zip 'https://www2.census.gov/geo/tiger/GENZ2023/shp/cb_2023_06_cousub_500k.zip' | ||
| fi | ||
|
|
||
| # Unzip the ZIP archive to extract the shapefile. | ||
| unzip -oqd src/.observablehq/cache src/.observablehq/cache/cb_2023_06_cousub_500k.zip | ||
|
|
||
| # Convert the shapefile to GeoJSON, then to TopoJSON, simplify, and merge counties. | ||
| shp2json --encoding utf-8 -n src/.observablehq/cache/cb_2023_06_cousub_500k.shp > src/.observablehq/cache/cb_2023_06_cousub_500k.json | ||
Fil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Fil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| geo2topo -q 1e5 -n counties=src/.observablehq/cache/cb_2023_06_cousub_500k.json \ | ||
Fil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| | toposimplify -f -s 1e-7 \ | ||
| | topomerge state=counties | ||
| ``` | ||
|
|
||
| The census.gov URL comes from the Census Bureau page linked above. Here “06” is the state of California’s FIPS code, and “cousub” means county subdivisions. | ||
|
|
||
| And here’s the result displayed with Plot: | ||
|
|
||
| ```js echo | ||
| Plot.plot({ | ||
| width: 640, | ||
| height: 720, | ||
| projection: { | ||
| type: "conic-conformal", | ||
| parallels: [37 + 4 / 60, 38 + 26 / 60], | ||
| rotate: [120 + 30 / 60, 0], | ||
| domain: castate | ||
| }, | ||
| marks: [ | ||
| Plot.geo(castate), | ||
| Plot.geo(cacounties, {strokeOpacity: 0.2}) | ||
| ] | ||
| }) | ||
| ``` | ||
|
|
||
| ```js echo | ||
| const ca = FileAttachment("data/ca.json").json(); | ||
| ``` | ||
|
|
||
| ```js echo | ||
| const castate = topojson.feature(ca, ca.objects.state); | ||
| const cacounties = topojson.mesh(ca, ca.objects.counties, (a, b) => a !== b); | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.