Geocoding, REXML and the Missing Method

Posted by Frederic Jean Mon, 20 Feb 2006 02:35:48 GMT

Scott Davis introduces the audience of his Real World Mapping presentation to the geocoder.us web site, which allows anyone to transform US based addresses to longitude and latitude coordinates. There are many options beyond the web based UI, including SOAP, XML-RPC, and RESTful web services. We will be using the RESTful RDF service for this demonstration.

The first step is to find (and URL encode) an address. For this demonstration, we will be using "Mission & Valencia Sts, San Francisco CA" as the address. You will see the following XML document if you point a browser at http://rpc.geocoder.us/service/rest?address=Mission+%26+Valencia+Sts%2C+San+Francisco+CA:

              1
              2
              3
              4
              5
              6
              7
              
<rdf:RDF>
                <geo:Point rdf:nodeID="aid06646235">
                  <dc:description>Mission St and Valencia St, San Francisco CA 94110</dc:description>
                  <geo:long>-122.420082</geo:long>
                  <geo:lat>37.74533</geo:lat>
                </geo:Point>
              </rdf:RDF>

This is technically an RDF document. It can still be parsed as a regular XML document though. Let's define a Location class:

              1
              2
              3
              4
              5
              6
              7
              8
              9
              10
              11
              12
              13
              14
              15
              16
              17
              18
              19
              20
              21
              22
              23
              24
              25
              
require 'rexml/document'
              require 'net/http'
              
              class Location
                require REXML
              
                def initialize(address)
                  geocoder = Net::HTTP.new("rpc.geocode.us", 80)
                  query = "/service/rest?address=#{address}"
                  query.gsub!(/ /, "+")
                  @entry = Document.new geocoder.get(query).body
                end
                
                def long
                  @entry.elements["//geo:long"].text
                end
                
                def lat
                  @entry.elements["//geo:lat"].text
                end
                
                def desc
                  @entry.elements["//dc:description"].text
                end
              end

From there, you can create a Location object and get the coordinates:

              1
              2
              3
              
location = Location.new "Mission St and Valencia St, San Francisco CA 94110"
              location.lat
              location.long

From there, you can use the location in a mapping application (such as a Google Maps mashup for example...)

Comments

Leave a response

Comments


(leave url/email »)