Skip to content

Instantly share code, notes, and snippets.

@myersjustinc
Last active March 8, 2023 20:39
Show Gist options
  • Save myersjustinc/0682c12b1c5d9177efc6d6c5a9aa2694 to your computer and use it in GitHub Desktop.
Save myersjustinc/0682c12b1c5d9177efc6d6c5a9aa2694 to your computer and use it in GitHub Desktop.
Remove Great Lakes and other water from Congressional districts shapefile

Remove Great Lakes and other water from Congressional districts shapefile

The problem

You want the most up-to-date shapefile of Congressional districts, but you don't want Michigan to be a giant blob.

(The Census Bureau offers some cartographic boundary shapefiles without this problem, but they aren't always the most current.)

Source data

You'll need two source files:

Assumptions

Utilities

You'll need ogr2ogr installed, which is part of GDAL. If you're on a Mac and have Homebrew, brew install gdal should do the trick.

Directories

I'm assuming you'll have downloaded both of those ZIP files to the current directory, and that you'll have unzipped them with their directory structures preserved.

In other words, you should have:

$ tree .
.
├── ne_10m_admin_0_countries_lakes
│   ├── ne_10m_admin_0_countries_lakes.README.html
│   ├── ne_10m_admin_0_countries_lakes.VERSION.txt
│   ├── ne_10m_admin_0_countries_lakes.cpg
│   ├── ne_10m_admin_0_countries_lakes.dbf
│   ├── ne_10m_admin_0_countries_lakes.prj
│   ├── ne_10m_admin_0_countries_lakes.shp
│   └── ne_10m_admin_0_countries_lakes.shx
├── ne_10m_admin_0_countries_lakes.zip
├── tl_2016_us_cd115
│   ├── tl_2016_us_cd115.cpg
│   ├── tl_2016_us_cd115.dbf
│   ├── tl_2016_us_cd115.prj
│   ├── tl_2016_us_cd115.shp
│   ├── tl_2016_us_cd115.shp.ea.iso.xml
│   ├── tl_2016_us_cd115.shp.iso.xml
│   ├── tl_2016_us_cd115.shp.xml
│   └── tl_2016_us_cd115.shx
└── tl_2016_us_cd115.zip

Removing water areas

Extract just the U.S. from the Natural Earth file:

ogr2ogr -where "ADM0_A3='USA'" us_only.shp ne_10m_admin_0_countries_lakes/ne_10m_admin_0_countries_lakes.shp

Use that to clip the original Census shapefile:

ogr2ogr -clipsrc us_only.shp clipped_districts.shp tl_2016_us_cd115/tl_2016_us_cd115.shp

Victory

And then use clipped_districts.shp however you want!

Credits/licenses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment