Permalink
Browse files

Work on Google geocoding emulation, updated version and documentation

  • Loading branch information...
1 parent 5ab4d04 commit 429e94c3b106c6aae1348e9d82740a231928bec5 @petewarden committed Mar 26, 2012
Showing with 227 additions and 6 deletions.
  1. +1 −1 coordinates2politics.rb
  2. +1 −2 docs/serversetup.txt
  3. +2 −0 dstk.xcodeproj/project.pbxproj
  4. +5 −0 dstk_server.rb
  5. +215 −0 emulategoogle.rb
  6. +1 −1 public/scripts/jquery.dstk.js
  7. +1 −1 python/setup.py
  8. +1 −1 views/about.haml
View
@@ -92,7 +92,7 @@ def coordinates2politics(locations, callback=nil)
end
- # Look in the neighborhoods and unemployment tables if we're in the US
+ # Look in the neighborhoods table if we're in the US
if country_code == 'usa'
neighborhood_select = 'SELECT name,city,state FROM "neighborhoods_polygon" WHERE ST_DWithin('+point_string+', way, 0.0001);'
View
@@ -208,10 +208,9 @@ ruby setup.rb config
ruby setup.rb setup
sudo ruby setup.rb install
-cd ~/sources/dstkdata
-git pull origin master
cd ~/sources/dstk
git pull origin master
+./populate_database.rb
touch tmp/restart.txt
@@ -57,6 +57,7 @@
597D8E1213367B6F0042C251 /* dstk_server.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = dstk_server.rb; sourceTree = "<group>"; };
597D8E1313367BA40042C251 /* dstk_config.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = dstk_config.rb; sourceTree = "<group>"; };
59C2BE9713AFEE0700759133 /* test_text2places.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = test_text2places.py; sourceTree = "<group>"; };
+ 59E3237B152013E8008D2635 /* emulategoogle.rb */ = {isa = PBXFileReference; lastKnownFileType = text.script.ruby; path = emulategoogle.rb; sourceTree = "<group>"; };
59F0A27213455C8800098746 /* test_text2times.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = test_text2times.py; sourceTree = "<group>"; };
59F0A27313455E6F00098746 /* text2times */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = text2times; path = python/text2times; sourceTree = "<group>"; };
59F0A286134E961F00098746 /* loadukpostcodes.sql */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = loadukpostcodes.sql; sourceTree = "<group>"; };
@@ -75,6 +76,7 @@
5950FA5D14E51EA600361BF2 /* docs */,
597D8E1313367BA40042C251 /* dstk_config.rb */,
597D8E1213367B6F0042C251 /* dstk_server.rb */,
+ 59E3237B152013E8008D2635 /* emulategoogle.rb */,
5929E11013243ECB00B81D2E /* Gemfile */,
597D8CC7133548CD0042C251 /* genderfromname.rb */,
5929E11913243F0400B81D2E /* geodict_cli.rb */,
View
@@ -37,6 +37,7 @@
require File.join(cwd, 'text2times')
require File.join(cwd, 'street2coordinates')
require File.join(cwd, 'coordinates2politics')
+require File.join(cwd, 'emulategoogle')
enable :run
@@ -1265,3 +1266,7 @@ def boilerpipe(input_html)
make_json(results, callback)
end
+
+get '/maps/api/geocode/:format' do
+ emulate_google_geocoder(params)
+end
View
@@ -0,0 +1,215 @@
+# EmulateGoogle
+#
+# This module takes provides an interface that's compatible with Google's geocoding
+# API. See https://developers.google.com/maps/documentation/geocoding/ for details.
+#
+# Copyright (C) 2012 Pete Warden <pete@petewarden.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+require 'rubygems' if RUBY_VERSION < '1.9'
+
+require 'json'
+
+cwd = File.expand_path(File.dirname(__FILE__))
+require File.join(cwd, 'street2coordinates')
+
+# Emulates the interface to Google's geocoding API.
+def google_geocoder_api_call(params)
+
+ format = params[:format].downcase
+ address = params[:address]
+ latlng = params[:latlng]
+ result = nil
+ if address
+ found = street2coordinates([address])
+ info = found[address]
+ if !info
+ locations = find_locations_in_text('at '+address.capitalize)
+ locations.each_with_index do |location_info, index|
+ found_tokens = location_info[:found_tokens]
+ match_start_index = found_tokens[0][:start_index]
+ match_end_index = found_tokens[found_tokens.length-1][:end_index]
+ location = found_tokens[0]
+ lat = location[:lat].to_f
+ lon = location[:lon].to_f
+ type = location[:type]
+ if type == :CITY
+ bounding_range = 0.1
+ type_name = 'locality'
+ address_components = [
+ {
+ 'long_name' => location[:matched_string],
+ 'short_name' => location[:matched_string],
+ 'types' => [ 'locality', 'political' ],
+ },
+ {
+ 'long_name' => location[:country_code],
+ 'short_name' => location[:country_code],
+ 'types' => [ 'country', 'political' ],
+ },
+ ]
+ else
+ bounding_range = 5.0
+ type_name = 'country'
+ address_components = [
+ {
+ 'long_name' => location[:matched_string],
+ 'short_name' => location[:code],
+ 'types' => [ 'country', 'political' ],
+ },
+ ]
+ end
+ result = {
+ 'address_components' => address_components,
+ 'geometry' => {
+ 'location' => {
+ 'lat' => lat,
+ 'lng' => lon,
+ },
+ 'location_type' => 'APPROXIMATE',
+ 'viewport' => {
+ 'northeast' => {
+ 'lat' => lat + bounding_range,
+ 'lng' => lon + bounding_range,
+ },
+ 'southwest' => {
+ 'lat' => lat - bounding_range,
+ 'lng' => lon - bounding_range,
+ },
+ }
+ },
+ 'types' => [ type_name, 'political' ],
+ }
+ break
+ end
+ else
+ lat = info[:latitude].to_f
+ lon = info[:longitude].to_f
+ street_number = info[:street_number]
+ street_name = info[:street_name]
+ locality = info[:locality]
+ region = info[:region]
+ country_name = info[:country_name]
+ country_code = info[:country_code]
+ address_components = []
+ types = nil
+ if street_number
+ address_components << {
+ 'long_name' => street_number,
+ 'short_name' => street_number,
+ 'types' => ['street_number'],
+ }
+ end
+ if street_name
+ address_components << {
+ 'long_name' => street_name,
+ 'short_name' => street_name,
+ 'types' => ['route'],
+ }
+ if !types
+ types = ['street_address']
+ end
+ end
+ if locality
+ address_components << {
+ 'long_name' => info[:locality],
+ 'short_name' => info[:locality],
+ 'types' => [ 'locality', 'political' ],
+ }
+ if !types
+ types = ['locality', 'political']
+ end
+ end
+ if region
+ address_components << {
+ 'long_name' => region,
+ 'short_name' => region,
+ 'types' => ['administrative_area_level_1', 'political'],
+ }
+ if !types
+ types = ['administrative_area_level_1', 'political']
+ end
+ end
+ if country_name and country_code
+ address_components << {
+ 'long_name' => country_name,
+ 'short_name' => country_code,
+ 'types' => ['country', 'political'],
+ }
+ if !types
+ types = ['country', 'political']
+ end
+ end
+ result = {
+ 'address_components' => address_components,
+ 'formatted_address' => address,
+ 'geometry' => {
+ 'location' => {
+ 'lat' => lat,
+ 'lng' => lon,
+ },
+ 'location_type' => 'ROOFTOP',
+ 'viewport' => {
+ 'northeast' => {
+ 'lat' => lat + 0.001,
+ 'lng' => lon + 0.001,
+ },
+ 'southwest' => {
+ 'lat' => lat - 0.001,
+ 'lng' => lon - 0.001,
+ },
+ }
+ },
+ 'types' => types,
+ }
+ end
+ elsif latlng
+ halt 404, "Reverse geocoding not supported"
+ else
+ halt 404, "Format not found"
+ end
+
+ if !result
+ output = {
+ 'results' => [],
+ 'status' => 'ZERO_RESULTS',
+ }
+ else
+ output = {
+ 'results' => [result],
+ 'status' => 'OK',
+ }
+ end
+
+ output
+end
+
+if __FILE__ == $0
+
+ if ARGV.length < 1
+ address = '2543 Graystone Place, Simi Valley, CA 93065'
+ else
+ address = ARGV[0]
+ end
+
+ params = {
+ :format => 'json',
+ :address => address,
+ }
+
+ result = google_geocoder_api_call(params)
+ $stderr.puts "result=#{JSON.pretty_generate(result)}"
+
+end
@@ -1,6 +1,6 @@
/*
* jQuery Data Science Toolkit Plugin
- * version: 0.35 (2011-04-17)
+ * version: 0.40 (2011-03-23)
*
* Example:
* var dstk = $.DSTK();
View
@@ -1,6 +1,6 @@
from distutils.core import setup
setup(name='dstk',
- version='0.35',
+ version='0.40',
py_modules=['dstk'],
author='Pete Warden',
author_email='pete@petewarden.com',
View
@@ -17,7 +17,7 @@
[Follow me on Twitter](http://twitter.com/petewarden)
- Version 0.35 - April 17th 2011
+ Version 0.40 - March 25th 2012
### Credits

0 comments on commit 429e94c

Please sign in to comment.