main
 1# A service that can look up a geo-location from a string
 2# the api this is based on (ERSI) seems fairly robust and can
 3# take landmarks (Stampede Grounds) and street addresses.
 4
 5# work by Chris Desmarais (codeshoulders@gmail.com)
 6
 7require 'rest_client'
 8require 'open-uri'
 9require 'json'
10
11class GeoLocationService
12
13	def self.GetGeoLocation( locationString)
14		return ExtractGeoLocationFromJson( GetGeoLocationData( locationString ) )
15	end
16
17	def self.GetGeoLocationData(locationString)
18		urlAsString =  "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find?f=pjson&text="
19		urlAsString += URI::encode(locationString)
20		return RestClient.get urlAsString	
21	end
22	
23	def self.ExtractGeoLocationFromJson( jsonString)
24		parsed = JSON.parse(jsonString)
25		
26                return nil if parsed["locations"].length == 0
27                
28		# need to handle more than one match
29		x = parsed["locations"][0]["feature"]["geometry"]["x"]
30		y = parsed["locations"][0]["feature"]["geometry"]["y"]
31		return GeoLocation.new( x, y)
32	end
33end
34
35class GeoLocation
36	attr_accessor :x, :y
37	def initialize (x,y)
38		@x = x
39		@y = y
40	end
41end
42