Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mcommons/legislative-lookup
Browse files Browse the repository at this point in the history
Conflicts:
	README
  • Loading branch information
Mobile Commons committed Dec 14, 2012
2 parents d85d6ba + e68cb69 commit 29fdc19
Show file tree
Hide file tree
Showing 1,595 changed files with 216,939 additions and 160 deletions.
36 changes: 21 additions & 15 deletions README
@@ -1,6 +1,6 @@
=== Project Ligerhorn

- [Code on GitHub]( http://github.com/mcommons/ligerhorn )
- [Code on GitHub](http://github.com/mcommons/ligerhorn)

=== Description

Expand All @@ -10,39 +10,45 @@ Rails application and database to lookup congressional and state legislative dis

Install Postgres and postGIS, set up a database, and populate it.

On ubuntu:
sudo apt-get install postgresql-contrib postgresql postgresql-8.4-postgis
# 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.
DB="legislative_lookup_dev" # or whatever you want to call it
==== Ubuntu 11.04 Start
sudo apt-get install postgresql-contrib postgresql postgresql-8.4-postgis libpq-dev # libpq-dev needed for gem
# 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
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/8.4/contrib/postgis-1.5/postgis.sql
psql -h localhost -U postgres -d $DB -f /usr/share/postgresql/8.4/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
PATH=/var/lib/gems/1.8/bin/:$PATH rake db:migrate # PATH setting needed for old fashioned system wide installation and shouldn't be needed if using rvm

On OSX:
==== OSX 10.7 Start
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="legislative_lookup_dev" # or whatever you want to call it
$ DB="congress_development" # or whatever you want to call it
$ psql -h localhost -c "CREATE DATABASE $DB;"
$ psql -h localhost -d $DB -c "CREATE EXTENSION postgis;"

==== Common Finish
gem install postgres -v '0.7.9.2008.01.28' # this is what's used in production
./script/server
# Visit http://localhost:3000/. The page should load although nothing will work yet because there's no data in the database.
rake db:migrate
# If ruby is installed system-wide (ie not with rvm) and PATH isn't set up properly, you might need to run it with something like:
# PATH=/var/lib/gems/1.8/bin/:$PATH rake db:migrate

=== Authors

- [Nathan Woodhull](mailto:woodhull@gmail.com)
- [Benjamin Stein](mailto:ben@mobilecommons.com)
- [Mal McKay](mailto:mal@mobilecommons.com)
- [Nathan Woodhull](mailto:nathan@mcommons.com)
- [Benjamin Stein](mailto:ben@mcommons.com)
- [Mal McKay](mailto:mal@mcommons.com)
- [Daniel Benamy](mailto:dbenamy@mcommons.com)

Project sponsored by [Mobile Commons]( http://mobilecommons.com/ )
Project sponsored by [Mobile Commons](http://www.mobilecommons.com/)

=== License

Copyright (c) 2009 Mobile Commons
Copyright (c) 2008-2012 Mobile Commons
See MIT-LICENSE in this directory.


14 changes: 7 additions & 7 deletions app/controllers/application.rb
Expand Up @@ -7,15 +7,15 @@ class ApplicationController < ActionController::Base
layout 'layout'
# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery # :secret => 'a721e67c89b39d5e55d6a42e3b5c1fa0'

include ExceptionNotifiable


#protect_from_forgery # :secret => 'a721e67c89b39d5e55d6a42e3b5c1fa0' # liger1 but not liger2 had this line commented out
skip_before_filter :verify_authenticity_token # liger2 but not liger1 had this line added
# One or both of the above lines turns off security features that weren't working nicely with ajax calls.

# See ActionController::Base for details
# Uncomment this to filter the contents of submitted sensitive data parameters
# from your application log (in this case, all fields with names like "password").
# filter_parameter_logging :password



include ExceptionNotifiable

end
44 changes: 42 additions & 2 deletions app/controllers/districts_controller.rb
Expand Up @@ -9,7 +9,6 @@ def lookup
@federal = @districts.find{|d| d.level == 'federal' }
@upper = @districts.find{|d| d.level == 'state_upper' }
@lower = @districts.find{|d| d.level == 'state_lower' }

end
respond_to do | type |
type.js do
Expand All @@ -23,9 +22,35 @@ def lookup
type.xml { render :layout => false}
type.kml { render :layout => false}
type.georss { render :layout => false}
type.json{
args = { :json => districts_to_json }
args[:callback] = params[:callback] if params[:callback]
render args
}

end
end

def polygon
@district = District.find(:first, :conditions =>{ :state => District::STATES[params[:state]], :name => params[:district], :level => params[:level]})

poly = @district.the_geom[0]
@map = GMap.new("map_div")
@map.control_init(:large_map => true, :map_type => true)

@zoom = @map.get_bounds_zoom_level(GLatLngBounds.from_georuby(poly.envelope))
@center = GLatLng.from_georuby(poly.envelope.center)
@polygon = GPolygon.from_georuby(poly,"#000000",0,0.0,"#ff0000",0.3)
end

def mdata
geocoder = Graticule.service(:google).new(Ym4r::GmPlugin::ApiKey.get())
location = geocoder.locate(params[:args])
@districts = District.lookup(location.latitude, location.longitude)
@districts = @districts.find_all{|d| !d.level.blank? }
render :layout => false
end

private
def load_google_map
@map = GMap.new("map_div")
Expand Down Expand Up @@ -56,4 +81,19 @@ def load_google_map
end
end

end
def districts_to_json
hash = {
:lat=>params[:lat],
:lng=>params[:lng]
}
@districts.each do |d|
hash[d.level] = {
:state => d.state_name,
:district => d.name,
:display_name => d.display_name
}
end
hash
end

end
32 changes: 32 additions & 0 deletions app/models/district.rb
@@ -1,3 +1,19 @@
# == Schema Information
#
# Table name: districts
#
# gid :integer not null
# state :string(2)
# cd :string(3)
# lsad :string(2)
# name :string(90)
# lsad_trans :string(50)
# the_geom :geometry multi_polygon, -1
# state_name :string(255)
# level :string(255)
# census_geo_id :string(255)
#

class District < ActiveRecord::Base

def self.lookup(lat, lng)
Expand All @@ -16,6 +32,20 @@ def display_name
end
end

def title
"#{display_name} #{level_name} District"
end

def level_name
LEVELS[level]
end

LEVELS = {
"state_upper" => "State Upper Legislative",
"state_lower" => "State Lower Legislative",
"federal" => "Congressional"
}

FIPS_CODES = {
"01" => "AL",
"02" => "AK",
Expand Down Expand Up @@ -79,4 +109,6 @@ def display_name
"78" => "VI"

}
STATES = FIPS_CODES.invert.freeze
end

9 changes: 9 additions & 0 deletions app/views/districts/_district.html.erb
@@ -0,0 +1,9 @@
<% @districts.each do | d | %>
<% case d.level when 'federal' %>
<span class="federal"> <%= d.display_name %> Congressional District</span>
<% when 'state_upper'%>
<span class="state_upper"> <%= d.display_name %> Upper House District</span>
<% when 'state_lower'%>
<span class="state_lower"> <%= d.display_name %> Lower House District</span>
<% end %>
<% end %>
3 changes: 2 additions & 1 deletion app/views/districts/_icons.html.erb
@@ -1,3 +1,4 @@
<%= link_to(image_tag("/images/georss.gif"), :controller => 'districts', :action => 'lookup', :format => 'georss', :lat => params[:lat], :lng => params[:lng]) %>
<%= link_to(image_tag("/images/ge.gif"), :controller => 'districts', :action => 'lookup', :format => 'kml', :lat => params[:lat], :lng => params[:lng]) %>
<%= link_to(image_tag("/images/xml.jpg"), :controller => 'districts', :action => 'lookup', :format => 'xml', :lat => params[:lat], :lng => params[:lng]) %>
<%= link_to(image_tag("/images/xml.jpg"), :controller => 'districts', :action => 'lookup', :format => 'xml', :lat => params[:lat], :lng => params[:lng]) %>
<%= link_to(image_tag("/images/json.gif"), :controller => 'districts', :action => 'lookup', :format => 'json', :lat => params[:lat], :lng => params[:lng]) %>
15 changes: 8 additions & 7 deletions app/views/districts/lookup.html.erb
Expand Up @@ -16,7 +16,7 @@
} else {
$('lat').value = point.lat();
$('lng').value = point.lng();
$('lat_lng_form').request();
$('lat_lng_form').request({onFailure:function(request){alert('Address not found')}});
}
}
);
Expand Down Expand Up @@ -54,11 +54,12 @@
<%= text_field_tag "lng", params[:lng] %>
</p>
</span>
<span id="icons"></span>
<% end %>
</div>

<%= @map.div %>
<div id="icons"></div>

<div id="notice" style="display:none;"></div>


Expand All @@ -68,14 +69,14 @@
<br/>

<h4>API Documentation</h4>

This web service matches a latitude and longitude with the congressional and state legislative districts
for that location. It is provided free of charge with no warrantee. If you use this service
please cache your responses and behave responsibly. If you plan to send more than a few thousand queries
per day, let us know via the list before you start.
<p>
Mobile Commons Legislative Lookup matches a latitude and longitude with the congressional and state legislative districts for that location. It is provided free of charge with no warranty. If you use this service please cache your responses and behave responsibly. If you plan to send more than a few thousand queries per day, let us know via the list before you start.
<p>
Join our <a href="http://groups.google.com/group/mcommons-congress-api">low volume mailing list</a> for advance notice of API changes or to ask a question.
</p>
<p>
Source code is available under an MIT License at <a href="http://github.com/mcommons/legislative-lookup">http://github.com/mcommons/legislative-lookup</a>.
</p>
<p class="api">
A query for a latitude and longitude:
<a class="api" href="http://congress.mcommons.com/districts/lookup.xml?lat=40.7145248&lng=-74.007073">http://congress.mcommons.com/districts/lookup.xml?lat=40.7145248&amp;lng=-74.007073</a>
Expand Down
1 change: 1 addition & 0 deletions app/views/districts/lookup.xml.builder
Expand Up @@ -5,6 +5,7 @@ xml.response do
if @districts && @districts.any?
@districts.each do | d |
xml.tag!(d.level.intern) do
xml.iddd d.gid
xml.state d.state_name
xml.district d.name
xml.display_name d.display_name
Expand Down
8 changes: 8 additions & 0 deletions app/views/districts/mdata.html.erb
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<response>
<reply>
<text>
<![CDATA[<%= @districts.collect{|d| d.title}.join(', ')%>]]>
</text>
</reply>
</response>
2 changes: 2 additions & 0 deletions app/views/districts/polygon.rjs
@@ -0,0 +1,2 @@
page << @map.add_overlay(@polygon)
page << @map.set_center(@center,@zoom)
20 changes: 13 additions & 7 deletions app/views/layouts/maps.html.erb
Expand Up @@ -14,12 +14,9 @@
<div id="headerSubWrap">
<div id="header">
<ul>
<li><a href="http://mcommons.com/">Our Products</a></li>
<li><a href="http://mcommons.com/our-customers">Our Customers</a></li>

<li><a href="http://mcommons.com/blog">Company Blog</a></li>
<li class="last"><a href="http://mcommons.com/about-us">About Us</a></li>
</ul> <h1><a href="http://mcommons.com/"><strong>Mobile Commons</strong></a></h1>
<li class="last"><a href="http://mobilecommons.com/">By Mobile Commons</a></li>
</ul>
<h1><a href="http://mobilecommons.com/"><strong>Mobile Commons</strong></a></h1>
</div>
<div id="liner">
</div>
Expand All @@ -33,9 +30,18 @@
</div>
<div id="footer">
<p>
Copyright &copy; 2008 Mobile Commons, All rights reserved.<br />
Copyright &copy; 2009 Mobile Commons, All rights reserved.<br />
</p>
</div>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-721713-2");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>
2 changes: 1 addition & 1 deletion config/database.yml
Expand Up @@ -3,7 +3,7 @@ development:
database: congress_development
username: postgres
password:
host: localhost
host: 127.0.0.1

test:
adapter: postgresql
Expand Down
55 changes: 55 additions & 0 deletions config/deploy.rb
@@ -0,0 +1,55 @@
set :application, "congress"
set :user, "deploy"
set :runner, "deploy"
set :scm, "git"
set :repository_host, "dev.mcommons.com"
set :repository, "#{user}@#{repository_host}:/repos/ligerhorn.git"
set :temp_deployment_dir, "/tmp/mcommons_deploy"
set :deploy_to, "/apps/congress"
set :repository_cache, "cached-copy"
set :checked_out_repo, "#{deploy_to}/shared/#{repository_cache}"
set :monit, "/usr/bin/monit"



set :campfire_subdomain, "mcommons"
set :campfire_ssl, true
set :campfire_email, "mse-6@mcommons.com"
set :campfire_password, "Rebaxan"
set :campfire_room, "Mobile Commons"

set :ssh_options, { :paranoid => false, :port => 7822 }

depend :remote, :gem, "mongrel", ">=1.0.1", :roles => :app
depend :remote, :gem, "mongrel_cluster", ">=1.0.1.1", :roles => :app
depend :remote, :gem, "postgres", ">=0.7", :roles => :app

role :app, "congress.mcommons.com"
role :web, "congress.mcommons.com"
role :db, "congress.mcommons.com", :primary => true


namespace :deploy do

desc "Deploy and start the app servers"
task :cold do
update
start
end

desc "Start Mongrel processes on the app server."
task :start , :roles => :app do
sudo "#{monit} -g mongrel start all"
end

desc "Restart the Mongrel processes on the app server by starting and stopping the cluster."
task :restart , :roles => :app do
sudo "#{monit} -g mongrel restart all"
end

desc "Stop the Mongrel processes on the app server."
task :stop , :roles => :app do
sudo "#{monit} -g mongrel stop all"
end
end

1 change: 1 addition & 0 deletions config/environment.rb
Expand Up @@ -29,6 +29,7 @@
# config.gem "aws-s3", :lib => "aws/s3"

#config.gem "geo-ruby"
config.gem "graticule"

# Only load the plugins named here, in the order given. By default, all plugins
# in vendor/plugins are loaded in alphabetical order.
Expand Down

0 comments on commit 29fdc19

Please sign in to comment.