Normally you may access this web services API directly with HTTP REST request, and get a response in common formats like xml, json, kml.
For example you may geocode with the Google geocoder an address like this one: “1071 5th Avenue, New York, NY” with a request like this one:
In this case we are querying the Google geocoder web service to get a response in json via the output parameter in the query string. This is the json response we get back from the web service:
Note that the response includes the main result (the coordinates of the geocoded point) and other important attributes, like - in the case of Google - the administrative area name, the sub administrative area name, the country name and so on.
The other web services works in a manner very similiar to Google. For example this is how the same address is geocoded by GeoNames (we will use the name place here - GeoNames is a database of geographic names: “Solomon Guggenheim Museum, NY”)
If we make this request to the web services:
And this is the result (we still have a json output - note that there in this case are many results!):
If you need to invoke these web services in your code and you are using a Python, there is an excellent API that let you query all these web services in the same manner without messing your code with xml/json request/response to them: this API is called GeoPy.
Let’s see how it is easy to work with it. First I create a virtualenv in my Ubuntu 9.04 with Python 2.5 and geopy 0.93. GeoPy at this time doesn’t support Python > 2.5, and I will use the GeoPy from a branch, because that is the only one that at this time supporting reverse geocoding. If you do not need reverse geocoding you may use the stable version.
GeoPy needs SimpleJson and BeautifulSoup, so we install them in the virtualenv:
Now let’s activate the virtualenv againg and time to test this API.
I will geocode the address with Google and after I get the point coordinates I will reverse geocode that point:
Using the API with different web services is just very similiar, there is a geocoder class for each web service: this is how to interact with Yahoo:
One thing that is useful with these Geocoding Web Services is the possibility to get the accuracy of the geocoded address: Google calls this “Accuracy”, Yahoo calls this “Precision”. You have seen the value of this parameter in the answers from the web services. Unluckily this value is still not accessible from GeoPy, but looks like it will be soon implemented.