Work the Shell - Exploring Lat/Lon with Shell Scripts
(The invocation is substitute/old-pattern/new-pattern/.)
Now we've got what we set out to create initially. Let's try it with yet another address:
$ sh whereis.sh 1313 S. Disneyland Drive, Anaheim CA 33.814413,-117.924424
Yep, that's the parking structure for Disneyland in California.
Now comes the hard part of this, actually. We can get the lat/lon of any address we desire, but calculating the distance between two points is a bit more tricky, as the mathematics involved is rather hairy, because what we're basically going to do is measure relative to the circumference of Earth.
I found a formula in JavaScript on-line as a starting point:
var R = 6371; // kilometers
var dLat = (lat2-lat1);
var dLon = (lon2-lon1);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
In this case, the circumference is R, and it's 6,371km. Because Earth is an oblate spheroid, not a perfect sphere, I expect this will have some small level of error, but let's proceed and see where we get.
To accomplish any sophisticated mathematics in a Linux shell, we're pretty much stuck with bc, but it's plenty powerful enough for this task, even if it's a bit clunky.
As an example, here's how you'd set the value of pi within a bc script:
pi=$(echo "scale=10; 4*a(1)" | bc -l)
The first stumble we have is that bc wants to work with radians, not degrees, but the lat/lon values we're getting are in degrees, so we need to convert them.
But before we do that, here's the intermediate output we seek, as we now need to work with two addresses, not just one:
$ sh farapart.sh \ "1600 pennsylvania ave, washington dc" \ "1313 s. disneyland drive, anaheim, ca" Lat/long for 1600 pennsylvania ave, washington dc = 38.89859, -77.035971 Lat/long for 1313 s. disneyland drive, anaheim, ca = 33.814413, -117.924424
Next month, we'll crack open the script to see how I am working with two addresses at the same time and splitting it into the four variables we'll later need. Then, we'll look at how to use bc to do the math.
Dave Taylor has been involved with UNIX since he first logged in to the on-line network in 1980. That means that, yes, he's coming up to the 30-year mark now. You can find him just about everywhere on-line, but start here: www.DaveTaylorOnline.com. In addition to all his other projects, Dave is now a film critic. You can read his reviews at www.DaveOnFilm.com.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Linux Systems Administrator
- New Products
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Have you tried Boxen? It's a
1 hour 20 min ago - seo services in india
5 hours 51 min ago - For KDE install kio-mtp
5 hours 52 min ago - Evernote is much more...
7 hours 52 min ago - Reply to comment | Linux Journal
16 hours 37 min ago - Dynamic DNS
17 hours 12 min ago - Reply to comment | Linux Journal
18 hours 10 min ago - Reply to comment | Linux Journal
19 hours 47 sec ago - Not free anymore
23 hours 2 min ago - Great
1 day 2 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
error?
When I run this script I get:
whereis: 7: Syntax error: Unterminated quoted string
Here is the script as I have it:
#!/bin/sh url='http://api.maps.yahoo.com/ajax/geocode' args='?appid=onestep&qt=1&id=m&qs=" converter="$url$args" addr="$(echo $* | sed 's/ /+/g')" curl -s "$CONVERTER$ADDR" | \ sed -e 's/.*{\("Lat":[^}]*\).*/\1/' -e 's/"L.."://g' exit 0Here is the command I'm running:
sh whereis 2001 Blake Street, Denver, CO
This seems pretty simple but I cannot see what I'm missing.
Cupla Problems
You've got a couple problems in your script, the one that's giving you the error is here:
You've got a string with mismatched quotes: it starts with a single quote and ends with a double quote, you need this:
or this
The other problem is that the shell is case sensitive, so you can't use "converter" and "CONVERTER" interchangeably, pick one. Same with "addr" and "ADDR". So change:
to
p.s. The first error (the mismatched quotes) was in the original text, it has now been fixed.
Mitch Frazier is an Associate Editor for Linux Journal.
What about bad addresses?
Is there any way to tell if the address you input is invalid? I've tried a few made-up bad addresses and it still seems to give valid lat/lon coordinates.
bad address
That's a "Feature" of yahoo's service. They'll give you their best estimate. If the house number is bad, they'll tell you the location of the street; if the street is bad, they'll tell you the location of the city; if the city is bad, they'll tell you the location of the state....
To tell if the address is bad you could, perhaps, compare the results with the results of a less specific look up.
shell errors
As written, I get only longitude.
when I change the sed to...
cut -d\" -f11,13 | \
I get the expected output
$ cat whereis.sh
#!/bin/bash
URL='http://api.maps.yahoo.com/ajax/geocode'
ARGS='?appid=onestep&qt=1&d=m&qs='
CONVERTER="$URL$ARGS"
ADDR="$(echo $* | sed 's/ /+/g')"
curl -s "$CONVERTER$ADDR" | \
cut -d\" -f11,13 | \
sed 's/[^0-9\.\,\-]//g;s/,$//'
exit 0
$ ./whereis.sh 1313 S Disneyland Drive, Anaheim, CA
33.814413,-117.924424
Response Is Different
The response you're getting is different, the GeoID value is not a quoted value as above. The following would avoid problems related to quotes:
curl -s "$CONVERTER$ADDR" | \ sed -e 's/.*{\("Lat":[^}]*\).*/\1/' -e 's/"L.."://g'Mitch Frazier is an Associate Editor for Linux Journal.