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.
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.
|Alice, the Turtle of the Modern Age||Mar 07, 2014|
|Using Django and MongoDB to Build a Blog||Mar 05, 2014|
|What virtualization solution do you use/manage at work?||Mar 04, 2014|
|Our Assignment||Mar 04, 2014|
|March 2014 Issue of Linux Journal: 20 Years of Linux Journal||Mar 03, 2014|
|Have Resume - Will Travel||Feb 28, 2014|
- You have to be careful there
1 week 4 days ago
- Wonder when LJ is going to
1 week 4 days ago
- Puerto Rico Free Software
1 week 5 days ago
2 weeks 3 hours ago
- I hate voice commands
2 weeks 17 hours ago
- usabilty --- AGAIN with this nonsense
2 weeks 19 hours ago
- Don't make excuses
2 weeks 22 hours ago
- Sorry to let you know, but
2 weeks 23 hours ago
- Ridiculous statement. Not a
2 weeks 1 day ago
2 weeks 2 days ago