Several years ago, I had a school project that required converting some addresses to longitude/latitude coordinates. We then took the coordinates and placed them on a street map for a mobile device application. The addresses came from a “Yellow Pages” like system lookup service which provided a phone number, street address, name, and business category. But the Yellow Pages service didn’t provide a geographic coordinate.
First, I tracked down a free and very useful web service that provides this functionality. The web site GeoCoder.us provides a few different free web services including geocoding on street address, ZIP code, or city and state. GeoCoder.us offers SOAP, XML-RPC, REST-ful RDF, and REST-ful CSV formats.
We’ll use the web service the requires a street address, so we can get a more accurate coordinate.
string street = "1060 west addison street"; string city = "chicago"; string state = "il";
The GeoCoder.us URL requires a specific parameter format. Here we append the street, city, and state to the base URL. Then we create and load the URL into an XML document. By passing the URL to the XmlDocument Load method, it will handle calling and reading the web response.
string geocoderUri = string.Format( "http://rpc.geocoder.us/service/rest?address={0},{1},{2}", street, city, state); XmlDocument geocoderXmlDoc = new XmlDocument(); geocoderXmlDoc.Load(geocoderUri);
Next, we need to add a namespace manager since GeoCoder.us uses the W3.org Basic Geo (WGS84 lat/long) vocabulary. Below is an example of this vocabulary and the actual response that was loaded into our XmlDocument.
<rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <geo:Point rdf:nodeID="aid62658379"> <dc:description>1060 W Addison St, Chicago IL 60613</dc:description> <geo:long>-87.655788</geo:long> <geo:lat>41.947372</geo:lat> </geo:Point> </rdf:RDF>
The namespace manager is needed so that we can select the long and lat XML nodes. Below we select the two XML nodes and parse them.
XmlNamespaceManager nsMgr = new XmlNamespaceManager(geocoderXmlDoc.NameTable); nsMgr.AddNamespace("geo", @"http://www.w3.org/2003/01/geo/wgs84_pos#"); string sLong = geocoderXmlDoc.DocumentElement.SelectSingleNode( @"//geo:long", nsMgr).InnerText; string sLat = geocoderXmlDoc.DocumentElement.SelectSingleNode( @"//geo:lat", nsMgr).InnerText; double latitude = Double.Parse(sLat); double longitude = Double.Parse(sLong); Console.WriteLine("Lat: " + latitude + " Lon: " + longitude);
Finally, the coordinates are printed to the console and appear as:
Lat: 41.947372 Lon: -87.655788
Update: 10/6/2007 Once you have the coordinates, you can perform radius searching. For example, you can query for all records within 5 miles of each other or find the nearest record to another record. Troy DeMonbreun provides an example of how to calculate the distance between two geographic coordinates using a SQL Server UDF (User Defined Function). The GeoCoder.us blog also discusses how to calculate distances using two geographic coordinates.
Update 2: 10/19/2007 Yahoo also provides a very nice Web Service for geocoding at Yahoo! Maps Web Services – GeoCoding API. Yahoo uses the same REST method for the accessing the Web Service, but with separate URL parameters for city, address, and state. Yahoo’s XML response doesn’t use namespaces and won’t require the use of a NamespaceManager. You’ll be able to just use SelectSingleNode(@”//Longitude”) and SelectSingleNode(@”//Latitude”) to collect the geographic coordinates.
Well, it’s actually Troy, not Tony. ;-)
[...] How to convert a street address to longitude and latitude (geocoding) via web services « Jim 2.0’… [...]
I need something that will take a data base and convert all the addresses to coordinate I can use on my GIS application.
Suggestions
You can loop through each database record and then perform the lat/lon lookup discussed in this article. You would just need to query your table, loop through the record set, pass in the values to the above code, get the coordinates, update the coordinate fields of the record, and save the record.
provided you have less than 5000 records in your db because that’s the api call limit sets by yahoo.
You could also try the USC WebGIS geocoder (https://webgis.usc.edu). It is free and offers several API’s, or you can upload a database for online batch processing.
how do you convert an address into latitude and longitude? Is it possible
how do you change latitude and longitude into a address?
@runawaychild,
> how do you convert an address into latitude and
> longitude? Is it possible
It is possible. If you read the blog entry, it describes the process.
Jim,
Great information. Very helpful! Thanks!
John