Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


How can I convert map addresses into latitude longitude?

I'm trying to write a shell script that will accept a street address and output the latitude and longitude of that address, for geocache purposes. How can I do that?


Dave's Answer:

This is a pretty interesting problem because while there are a variety of ways you can ostensibly get your latitude and longitude for a specific address from somewhere like Google Maps, the data isn't always accurate and it's also often pretty darn hard to figure out.

Heck, one of the most common ways to get lat/long from a Google Map is to cut and paste a Javascript snippet into your browser address bar. Ugh.

Fortunately, after much digging around, I found that there's not only a nice API for Yahoo Maps, but that there's a lazy way you can access it via a shell script.

The key piece is the Yahoo Maps URL:

http://api.maps.yahoo.com/ajax/geocode?appid=onestep&qt=1&id=m&qs=

Let's step through how it works, using an address you might have bumped into a few times in the past, and then I'll show how it can be turned into a simple Linux or Unix shell script.

$ lynx -dump 'http://api.maps.yahoo.com/ajax/geocode?appid=onestep&qt=1&id=m&qs=1600+pennsylvania+ave+washington+dc'
  YGeoCode.getMap({"GeoID":"m","GeoAddress":"1600 pennsylvania ave washington
  dc","GeoPoint":{"Lat":38.89859,"Lon":-77.035971},"GeoMID":false,"succes
  s":1},1);

The address is 1600 Pennsylvania Ave, Washington DC and I would like to think that everyone knows who lives there. Note that I'm using "lynx" to dump the result of that URL query to standard output and that the output is split across three lines.

The key data item here is GeoPoint because immediately following are the Lat and Lon values (in this case 38.89859 and -77.035971).

We can experimentally verify this by entering these two points into Google Maps too:

google maps white house

Yup, it's the lat/long for the White House in Washington DC.

Back to the script, though. Using "lynx" (or "get" or "curl") we now have a quick and easy way to convert a street address into a latitude / longitude pair. So there's just stuff to wrap around it. For example, convert a simple address into a URL-friendly address. Pretty easy:

addr="$(echo $1 | sed 's/ /+/g')"

It's a lazy way to accomplish this goal (e.g., turn spaces into "+" symbols), but let's just keep moving and let it ride.

Next up, call the Yahoo API and save just the line that contains the GeoPoint data:

val="$(lynx -dump "$converter$addr" | grep "GeoPoint" )"

Finally, we have the line that contains the lat/long we want. How do we extract just that data? With my favorite Linux command: "cut":

lat="$(echo $val | cut -d: -f3 | cut -d, -f1)"
long="$(echo $val | cut -d: -f4 | cut -d\} -f1)"

Now we've got it! All that's left is to output it:

echo "$long,$lat"

Put all the lines together and you now have a very simple command that does exactly what you seek:

$ sh geomap.sh "1600 pennsylvania ave, washington dc"
38.89859,-77.035971

Hope that helps you out. Oh, and assembling all the discrete lines into a script is a simple task "best left for the reader". :-)


More Useful Shell Script Programming Articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  




Categorized: Shell Script Programming   (Article 8845, Written by )
Tagged: geocache, latitude longitude, linux, shell script, unix
Previous: 28 Smart Ways to Overcome the Recession
Next: How do I create a KML map data file?




Reader Comments To Date: 3

frogola said, on September 30, 2009 9:17 AM:

sweet! thanks dude!

Harsh Shah said, on February 18, 2010 10:32 AM:

Wow.... what a great way to trick google maps... I really love you little sweet code.. that worked out amazingly...

thank you so much for sharing this with us.

Harsh

Peter said, on May 5, 2011 2:44 AM:

confusion:
your first example displays lat to be equal to 38.89859 in one place and then after the line this line says echo "$long,$lat" to be equal to -77.035971 ! So which is it?

Thanks for the script in any case. ;)

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.