Saturday, April 17, 2010

Oracle database geocoding with Google earth

Recentley someone asked me if I could help with a project where he was needed to gecode addresses and store this information in a Oracle database. For those who do not know what geocoding is, wikipedia has the following to say about it;

"Geocoding is the process of finding associated geographic coordinates (often expressed as latitude and longitude) from other geographic data, such as street addresses, or zip codes (postal codes). With geographic coordinates the features can be mapped and entered into Geographic Information Systems, or the coordinates can be embedded into media such as digital photographs via geotagging."

His original plan was to use a BASH script to download information from Google Earth and grep the needed information from the results from Google. The second step would be to load the data in the database and start using it. However I used a different technique some time ago to do something similar and we came to the conclusion all could be done from the inside the database itself. In Oracle 11G (and other releases) you can use the function HTTPURITYPE to retrieve information from a web address using the HTTP protocol. As google maps is using the HTTP protocol you can also use it to download the needed information. This is working the best if you retrieve the information in a XML format.

Google is providing you the option to download information from the google maps website in a XML format. The XML format used to described the information you are getting from Google is named KML.

"Keyhole Markup Language (KML) is an XML-based language schema for expressing geographic annotation and visualization on existing or future Internet-based, two-dimensional maps and three-dimensional Earth browsers. KML was developed for use with Google Earth, which was originally named Keyhole Earth Viewer. It was created by Keyhole, Inc, which was acquired by Google in 2004. The name "Keyhole" is an homage to the KH reconnaissance satellites, the original eye-in-the-sky military reconnaissance system first launched in 1976. KML is an international standard of the Open Geospatial Consortium. Google Earth was the first program able to view and graphically edit KML files, and other projects such as Marble have also started to develop KML support."

Lets say for example I want to have information about the location of my office I can check the google maps website by entering the following URL,Utrecht,papendorpseweg 100&sie=UTF8 now we do however still have the information in a HTML format while we want to have the coordinates in a XML format. By adding &output=kml to the url we are not served a website we are served a XML file,Utrecht,papendorpseweg 100&sie=UTF8&output=kml . If we look in the file we will find all the information about the coordinates we need. The file looks as below:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="">
<name>Papendorpseweg 100, 3528 Utrecht, The Netherlands</name>
<address>Papendorpseweg 100, 3528 Utrecht, The Netherlands</address>

Now we need to get this information into the database, the first step is to download the file using HTTPURITYPE and after that we will not store the XML file, we will query it in the same statement that downloads the file and extract the information by making use of EXTRACTVALUE. This will result in a example query as below:

EXTRACTVALUE(value(googleData), '/Placemark/name', 'xmlns=""') as "NAME"
,EXTRACTVALUE(value(googleData), '/Placemark/address', 'xmlns=""') as "ADDRESS"
,EXTRACTVALUE(value(googleData), '/Placemark/styleUrl', 'xmlns=""') as "STYLURL"
,EXTRACTVALUE(value(googleData), '/Placemark/Point/coordinates', 'xmlns=""') as "COORDINATES"
,EXTRACTVALUE(value(googleData), '/Placemark/LookAt/longitude', 'xmlns=""') as "LONGITUDE"
,EXTRACTVALUE(value(googleData), '/Placemark/LookAt/latitude', 'xmlns=""') as "LATITUDE"
,EXTRACTVALUE(value(googleData), '/Placemark/LookAt/range', 'xmlns=""') as "RANGE"
,'/kml/Placemark', 'xmlns=""'
) googleData;

This query itself is not very useful for the solution I proposed however if you add this query to a package that will do a lot more like getting a list of addresses and adding the address to the query as a variable you will be able to geocode lots of addresses directly from your database by making use of Google without any outside scripts. One thing you have to remember is that you will need access to outside locations, you might retrieve a error like "ORA-24247: network access denied by access control list (ACL)". In this case you will have to make sure you have set the access control list correct for the user you are using to execute the query. You will need to be able to lookup and . To set the access control list correct you can have a read in this blogpost I have been writing some time ago.

No comments: