Maps: GeoCoding with Google and Access from Shell
Abstract:
As the world becomes more connected, the ability to map things is easier. There are multiple mapping vendors on the internet (MapQuest, google, yahoo, microsoft.) The maps will normally have the ability to place locations via address or coordinates. Bulk placement of locations normally requires those locations to have been previously turned into coordinates via geocoding. A location can be simply retrieved from the UNIX command line via commands like wget and parsed with nawk.
Access to Geocoding via wget:
The wget command allows for simple access to web based URL's. A geocoding request can be passed in a URL with the values being returned in a JSON format.
sun5150/user$ wget "http://maps.googleapis.com/maps/api/geocode/json?address=55+Lodi+Estate+New+Delhi+110003.+India&sensor=false"
--2013-03-08 00:48:13-- http://maps.googleapis.com/maps/api/geocode/json?address=55+Lodi+Estate+New+Delhi+110003.+India&sensor=false
Resolving maps.googleapis.com... 173.194.77.95, 2607:f8b0:4003:c01::5f
Connecting to maps.googleapis.com|173.194.77.95|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/json]
Saving to: `json?address=55+Lodi+Estate+New+Delhi+110003.+India&sensor=false.1'
[ <=> ] 2,073 --.-K/s in 0s
2013-03-08 00:48:13 (23.5 MB/s) - `json?address=55+Lodi+Estate+New+Delhi+110003.+India&sensor=false.1' saved [2073]
Reviewing the JSON output:
The resulting information is placed in a JSON formatted output file.
sun5150/user$ cat json*Delhi*
{
"results" : [
{
"address_components" : [
{
"long_name" : "Lodi Estate",
"short_name" : "Lodi Estate",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "110003",
"short_name" : "110003",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Lodi Estate, New Delhi, Delhi 110003, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.60010310,
"lng" : 77.22872610
},
"southwest" : {
"lat" : 28.59045690,
"lng" : 77.22100139999999
}
},
"location" : {
"lat" : 28.59474070,
"lng" : 77.22542659999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.60010310,
"lng" : 77.22872610
},
"southwest" : {
"lat" : 28.59045690,
"lng" : 77.22100139999999
}
}
},
"partial_match" : true,
"types" : [ "neighborhood", "political" ]
}
],
"status" : "OK"
}
{
"results" : [
{
"address_components" : [
{
"long_name" : "Lodi Estate",
"short_name" : "Lodi Estate",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "110003",
"short_name" : "110003",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Lodi Estate, New Delhi, Delhi 110003, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.60010310,
"lng" : 77.22872610
},
"southwest" : {
"lat" : 28.59045690,
"lng" : 77.22100139999999
}
},
"location" : {
"lat" : 28.59474070,
"lng" : 77.22542659999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.60010310,
"lng" : 77.22872610
},
"southwest" : {
"lat" : 28.59045690,
"lng" : 77.22100139999999
}
}
},
"partial_match" : true,
"types" : [ "neighborhood", "political" ]
}
],
"status" : "OK"
}
Parsing JSON with nawk:
This can be quickly parsed using nawk with a simple one-liner.
sun5150/user$ nawk '
$1~/\"location\"/ { Tag="Yes" }
(/lat/ || /lng/) && Tag=="Yes" { print $3 }
/location_type/ { Tag="No" }
' json*New*Delhi*
28.59474070,
77.22542659999999