Rails application and database to lookup congressional and state legislative districts by latitude and longitude.
Install Postgres and postGIS, set up a database, apply the appropriate functions and populate it.
Install Postgress
sudo apt-get install postgresql-contrib postgresql postgresql-9.1-postgis libpq-dev
Postgres won't let you connect without a password out of the box. Create a new postgres user with a password or configure postgres to trust all connections from localhost and make sure the "postgres" user in postgres doesn't have a password. These instruction assume the later. You can do that by editing /etc/postgresql/8.4/main/pg_hba.conf
and adding a line ABOVE THE DEFAULT RULES like this:
host all postgres 127.0.0.1/32 trust
After you create your database you need to add required functions to your database. Note: You'll need to do the same for your test database - rerun the following w/ DB="congress_test"
DB="congress_development" # or whatever you want to call it
sudo -u postgres createdb -E UTF8 $DB
createlang -h localhost -U postgres plpgsql $DB
psql -h localhost -U postgres -d $DB -f /usr/share/postgresql/9.1/contrib/postgis-1.5/postgis.sql
psql -h localhost -U postgres -d $DB -f /usr/share/postgresql/9.1/contrib/postgis-1.5/spatial_ref_sys.sql
psql -h localhost -U postgres -d $DB -c "select postgis_lib_version();" # to make sure postgis works
Download postgres.app from http://postgresapp.com/. Extract it. Copy it to Applications.
$ sudo cp /Applications/Postgres.app/Contents/MacOS/lib/libjpeg* /usr/local/lib # to make postgis work with postgres.app
Run postgres.app.
$ DB="congress_development"
$ psql -h localhost -c "CREATE DATABASE $DB;"
$ psql -h localhost -d $DB -c "CREATE EXTENSION postgis;"
bundle install
./script/server
Visit http://localhost:3000/. The page should load although nothing will work yet because there's no data in the database. Run the following to load the full collection of shape files from census
rake db:migrate
rake shapefiles_113:prep_sql_files # Downloads files from census, unzip those files and convert with shp2pgsql.
rake shapefiles_113:import # Import sql files into temp tables, normalize naming converions and load into districts table.
To run specs, you need to seed the database with sample polygons. This repo contains sql imports for Rhode Island. To execute run:
rake db:test:prepare
rake spec
District data is imported using the code in lib/tasks/shapefile_procssing.rake
. Run rake -T shapefiles_113
from terminal to view the available tasks. Those tasks execute the basic process for updating district data, as outlined below.
- Download the relevant zipped shape files with the new data - from the U.S. Census.
- Unzip the shape files.
- Use shp2pgsql to convert the shape files to sql that will put it in temporary tables.
- Execute that sql for federal and then state data.
- Normalize the data in the temp tables.
- Copy the temporary data into the final
districts
table. - Delete the temporary tables.
The above steps have been completed for http://congress.mcommons.org. Please review the tasks and code in lib/tasks/shapefile_procssing.rake
Project sponsored by Mobile Commons
Copyright (c) 2008-2013 Mobile Commons See MIT-LICENSE in this directory.