Thursday, 9 February 2012

Geo Spatial data and the google maps API

Ok so here's the first of many blog posts that I will be capturing thoughts and research around my project, so apologies for those who read this regularly its going to get busy and I guess over time it will also become quite technical.

I have been carrying out some research on geo information as it is going to be fairly critical for my project, firstly for user registration and location management and secondly for identifying things that are close to a given latitude and longitude. In particular I have spent some time digging around the Google Maps API, documentation for which can be found here http://code.google.com/apis/maps/documentation/geocoding/.

This API allows you to pass a partial or fully constructed address and get a fully geo located JSON or XML response with the address modelled including and missing attributes. An example for Brick Lane in London is outlined below.

http://maps.googleapis.com/maps/api/geocode/json?address=1+Brick+Lane,London&sensor=false

As you can see this isn't exactly a complete address, but look at the JSON response below that documents the full geocode information


{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1",
               "short_name" : "1",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Brick Ln",
               "short_name" : "Brick Ln",
               "types" : [ "route" ]
            },
            {
               "long_name" : "London",
               "short_name" : "London",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "London Borough of Tower Hamlets",
               "short_name" : "London Borough of Tower Hamlets",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Greater London",
               "short_name" : "Gt Lon",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "United Kingdom",
               "short_name" : "GB",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "E1 6PU",
               "short_name" : "E1 6PU",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "E1",
               "short_name" : "E1",
               "types" : [ "postal_code_prefix", "postal_code" ]
            }
         ],
         "formatted_address" : "1 Brick Ln, London Borough of Tower Hamlets, London E1 6PU, UK",
         "geometry" : {
            "location" : {
               "lat" : 51.5173560,
               "lng" : -0.0709070
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 51.51870498029150,
                  "lng" : -0.06955801970849797
               },
               "southwest" : {
                  "lat" : 51.51600701970850,
                  "lng" : -0.07225598029150203
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}



Not only do I now have a fully modelled address that I can easily parse into a Javascript or Java object, I also have the lat/long information that I can use to base geo spatial searching e.g. show me all locations in my database that live within 5 miles of point x, I think it is pretty incredible!

From my research this can be done in any database using the Haversine formula, one solution for MySQL specifically is documented here http://qnatech.wordpress.com/2011/07/06/how-to-use-geo-spatial-with-mysql/


When playing around with example addresses I noticed that if the street name is unique enough all you need to provide is the house number / street and you get the same level of detail which is great if your working with malformed user data.


No comments: