Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fleshed out coordinates2demographics

  • Loading branch information...
commit 525b240815edf7c0eb36dd124cc818336fcb2572 1 parent 11358ed
Pete Warden authored
Showing with 100 additions and 12 deletions.
  1. +53 −12 coordinates2demographics.rb
  2. +47 −0 dstk_server.rb
65 coordinates2demographics.rb
View
@@ -19,32 +19,67 @@
require 'rubygems' if RUBY_VERSION < '1.9'
+require 'json'
+
cwd = File.expand_path(File.dirname(__FILE__))
require File.join(cwd, 'geodict_lib')
require File.join(cwd, 'dstk_config')
+require File.join(cwd, 'coordinates2politics')
-TypeToFriendly = {
- 'admin2' => 'country',
- 'admin4' => 'state',
- 'admin6' => 'county',
- 'admin5' => 'city',
- 'admin8' => 'city'
+ValueTypeToName = {
+ '03' => :unemployment_rate,
+ '04' => :unemployment,
+ '05' => :employment,
+ '06' => :labor_force,
}
# Takes an array of coordinates as input, and looks up what political areas they lie
# within
def coordinates2demographics(locations, callback=nil)
- politics = coordinates2politics(locations)
+ politics_results = coordinates2politics(locations)
conn = PGconn.connect(DSTKConfig::HOST, DSTKConfig::PORT, '', '', DSTKConfig::REVERSE_GEO_DATABASE, DSTKConfig::USER, DSTKConfig::PASSWORD)
result = []
- politics.each do |politic|
+ politics_results.each do |politics_result|
+
+ location = politics_result[:location]
+ politics = politics_result[:politics]
+ demographics = []
+
+ code_for_type = {}
+ politics.each do |area|
+ type = area[:type]
+ code = area[:code]
+ code_for_type[type] = code
+ end
+
+ country_code = code_for_type['admin2']
+ state_and_county_code = code_for_type['admin6']
+ if country_code == 'usa' and state_and_county_code
+ state_code, county_code = state_and_county_code.split('_', 2)
+ unemployment_select = "SELECT * FROM us_county_unemployment WHERE state_code='#{state_code}' AND county_code='#{county_code}';"
+ unemployment_hashes = select_as_hashes(conn, unemployment_select)
+ if unemployment_hashes
+ county_unemployment = {}
+ ValueTypeToName.each do |value_type, value_name| county_unemployment[value_name] = [] end
+ unemployment_hashes.each do |unemployment_hash|
+ year = unemployment_hash['year']
+ month = unemployment_hash['month']
+ value_type = unemployment_hash['value_type']
+ value = unemployment_hash['value']
+ value_name = ValueTypeToName[value_type]
+ county_unemployment[value_name] = [year, month, value]
+ end
+ demographics[:county_unemployment] = county_unemployment
+ end
+ end
result.push({
:location => location,
- :politics => output
+ :politics => politics,
+ :demographics => demographics
})
end
@@ -53,6 +88,12 @@ def coordinates2demographics(locations, callback=nil)
end
-#text = open('../cruftstripper/test_data/inputs/cnn.com.html').read()
-#output = text2people(text)
-#puts output.inspect
+
+if __FILE__ == $0
+
+ locations = [ {:latitude => 37.769456, :longitude => -122.429128} ]
+ $stderr.puts "locations=#{JSON.pretty_generate(locations)}"
+ demographics = coordinates2demographics(locations)
+ $stderr.puts "demographics=#{JSON.pretty_generate(demographics)}"
+
+end
47 dstk_server.rb
View
@@ -1084,6 +1084,53 @@ def boilerpipe(input_html)
end
+# The POST interface for the location to demographic information lookup
+post '/coordinates2demographics' do
+ begin
+ # Pull in the raw data in the body of the request
+ locations_string = request.env['rack.input'].read
+
+ if !locations_string
+ fatal_error('You need to place the latitude/longitude coordinates as a JSON-encoded array inside the POST body',
+ 'json', 500, nil)
+ end
+
+ locations_list = locations_list_from_string(locations_string)
+
+ result = coordinates2demographics(locations_list)
+
+ make_json(result)
+
+ rescue
+ fatal_error('coordinates2demographics error: '+$!.inspect + $@.inspect, 'json', 500)
+ end
+
+end
+
+# The GET interface for the location to demographic information lookup
+get '/coordinates2demographics/*' do
+
+ callback = params[:callback]
+
+# begin
+ locations_string = params['splat'][0]
+ if !locations_string
+ fatal_error('You need to place the latitude/longitude coordinates as a JSON-encoded array as part of the URL',
+ 'json', 500, callback)
+ end
+
+ locations_list = locations_list_from_string(locations_string, callback)
+
+ result = coordinates2demographics(locations_list, callback)
+
+ make_json(result, callback)
+
+# rescue
+# fatal_error('coordinates2politics error: '+$!.inspect + $@.inspect, 'json', 500, callback)
+# end
+
+end
+
# The interface used to convert a pdf/word/excel/image file into text
post '/file2text' do
Please sign in to comment.
Something went wrong with that request. Please try again.