GithubHelp home page GithubHelp logo

truthiswill / congress-maps Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aaronpdennis/congress-maps

0.0 1.0 0.0 121 KB

Process for mapping US congressional districts using US Census boundaries data and Mapbox

Home Page: http://aaronpdennis.github.io/congress-maps/example/

License: MIT License

JavaScript 77.97% HTML 22.03%

congress-maps's Introduction

Mapping US Congressional Districts

Follow the steps below to create a web map of United States congressional districts from Census Bureau data using Mapbox. You can also use this to create a lat/lng-to-congressional district API.

You will need an account on Mapbox.com. Then follow the commands below from the Mac OS X or Ubuntu terminal.

Why use Tippecanoe? Using Tippecanoe provides more control over how the geometries are tiled into a map. For comparison, using the Mapbox Studio default upload will not show a zoomed-out full country view of the data because the boundaries are so detailed; the default upload thinks you are only interested in looking closer at the data. Tippecanoe stops oversimplification of the geometry and also specifies a min/max zoom level.

Dependencies:

On OS X, install required dependencies with Homebrew:

brew install tippecanoe gdal node

On Ubuntu, you'll need node:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
nvm install 5.0

and gdal and Tippecanoe, which must be built from sources:

sudo apt-get install gdal-bin libprotobuf-dev protobuf-compiler libsqlite3-dev
git clone https://github.com/mapbox/tippecanoe
cd tippecanoe
make
make install

Setup:

Download this repository and then use npm to install a few more dependencies:

git clone https://github.com/aaronpdennis/congress-maps.git
cd congress-maps
npm install

Creating the map:

To complete these steps, run the commands below. Set MAPBOX_USERNAME to your Mapbox username, MAPBOX_DEFAULT_ACCESS_TOKEN to your Mapbox default access token, and MAPBOX_ACESS_TOKEN to a uploads:write scope access token from your Mapbox account.

# setup Mapbox account name and access tokens
export MAPBOX_USERNAME=<your mapbox username>
export MAPBOX_DEFAULT_ACCESS_TOKEN=<your mapbox default access token>
export MAPBOX_WRITE_SCOPE_ACCESS_TOKEN=<your mapbox write scope access token>

# create directory to store data
mkdir data

# dowload Census boundaries data, unzip the data, and convert it to GeoJSON
wget -P data ftp://ftp2.census.gov/geo/tiger/TIGER2015/CD/tl_2015_us_cd114.zip
unzip data/tl_2015_us_cd114.zip -d ./data/
ogr2ogr -f GeoJSON -t_srs crs:84 data/congressional_districts.geojson data/tl_2015_us_cd114.shp

# run processing on data
node process.js data/congressional_districts.geojson

# create Mapbox vector tiles from data
tippecanoe -o data/cd-114-2015.mbtiles -f -z 12 -Z 0 -B 0 -pS -pp -l districts -n "US Congressional Districts" data/map.geojson

# upload map data to Mapbox.com
node upload.js data/cd-114-2015.mbtiles

# modify mapbox-style-template.json to use your Mapbox account and save as data/mapbox-style.json
sed s/'USER'/"$MAPBOX_USERNAME"/g mapbox-style-template.json > data/mapbox-style.json

Next, go to mapbox.com/studio/styles, then drag-and-drop the data/mapbox-style.json file onto the screen. This should upload the map style to Mapbox.

Check out mapbox.com/studio to see updates on data processing. Once Mapbox is finished processing your upload, you can try it out.

Usage:

Use the files in the example directory as the basis for making a web map with functionality to focus on specific states or districts. To use this example web map, you'll need to change out my map style and default access token for your own. In the index.html file, replace my Mapbox default access token on line 75 and my map style URL on line 79 with your own.

After following the steps above, index.html will be a full page web map of U.S. Congressional districts. Host this file and the two supporting scripts (states.js, bboxes.js) on your website. If you don't want the interactive menu on your map, search through index.html and remove all sections of code that immediately follow the INTERACTIVE MENU line comment labels.

With this web map, you can show specific congressional districts using the URL hash. Set the location hash to state={state abbreviation} to show a specific state and add &district={district number} to specify a district within the state. The hash expects US Census two letter state abbreviations and district number conventions. At Large districts are numbered 00 and all other districts are two character numbers: district=01, district=02, ..., district=15, etc.

See the click handler for an example of how to use the Mapbox API to get the congressional district at a particular lat/lng coordinate.

Examples:

To show districts in the state of Virginia: http://aaronpdennis.github.io/congress-maps/example/#state=VA

To show the 5th district of California: http://aaronpdennis.github.io/congress-maps/example/#state=CA&district=05

congress-maps's People

Contributors

aaronpdennis avatar joshdata avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.