VSIDO Community

VSIDO Support => Scripts and How To's => Scripts => Topic started by: hakerdefo on July 11, 2015, 07:14:40 PM

Title: pmdc - calculate distance & travel time between two locations
Post by: hakerdefo on July 11, 2015, 07:14:40 PM
pmdc is a simple bash script that calculates distance & travel time between two locations. Distance & travel time are calculated using the road network (Driving mode in map parlance). Those of you living in the good ol' US of A can even use short names for their states like ca, ny, dc, etc. etc.
Any dependencies? Well only one. And it's simple to install too,
Code: [Select]
sudo apt-get install jq

Ready for the script? Here you go,
Code: [Select]
#!/bin/bash

clear

echo ""
echo -e "pmdc calculates distance & travel time between two locations."
echo ""
echo -e "Distance & travel time are calculated using the road network."
echo ""
echo -e "Press Enter key to continue...."
echo ""
read EnterKey
clear
echo ""
echo -e "Location 1 - From:"
echo ""
echo ""
echo -e "Enter the name of city-town-village"
echo ""
read village
echo ""
echo -e "Enter the name of state-county-province"
echo ""
read province
echo ""
echo -e "Enter the name of country"
echo ""
read country
clear
echo ""
echo -e "Location 2 - To:"
echo ""
echo ""
echo -e "Enter the name of city-town-village"
echo ""
read city
echo ""
echo -e "Enter the name of state-county-province"
echo ""
read state
echo ""
echo -e "Enter the name of country"
echo ""
read name
clear

city_1=$(echo "$village" | sed 's/ \+/+/g')
state_1=$(echo "$province" | sed 's/ \+/+/g')
country_1=$(echo "$country" | sed 's/ \+/+/g')
city_2=$(echo "$city" | sed 's/ \+/+/g')
state_2=$(echo "$state" | sed 's/ \+/+/g')
country_2=$(echo "$name" | sed 's/ \+/+/g')

curl -s --connect-timeout 30 -o /tmp/pmdc.json "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$city_1+$state_1+$country_1&destinations=$city_2+$state_2+$country_2&language=en"

if [ ! -f /tmp/pmdc.json ]; then
echo ""
echo -e "Unable to connect to Internet!"
echo -e "Check your Internet connection!"
echo ""

exit 1

else

addvalid=$(cat /tmp/pmdc.json | grep "NOT_FOUND")

if [[ -n "$addvalid" ]]; then
echo ""
echo -e "No results found!"
echo -e "May be you misspelled a name?!?"
echo ""

exit 1

else

resvalid=$(cat /tmp/pmdc.json | grep "ZERO_RESULTS")

if [[ -n "$resvalid" ]]; then
echo ""
echo -e "Sorry! No distance data is available!"
echo ""

exit 1

else

orgadd=$(cat /tmp/pmdc.json | jq -r '.origin_addresses' | sed -e 's/\"//g' -e 's/\[//g' -e 's/\]//g' -e 's/^[ \t]*//')
desadd=$(cat /tmp/pmdc.json | jq -r '.destination_addresses' | sed -e 's/\"//g' -e 's/\[//g' -e 's/\]//g' -e 's/^[ \t]*//')
distbet=$(cat /tmp/pmdc.json | jq -r '.rows[].elements[].distance.text')
durabet=$(cat /tmp/pmdc.json | jq -r '.rows[].elements[].duration.text')

echo ""
echo -e "From: $orgadd"
echo ""
echo -e "To: $desadd"
echo ""
echo -e "Distance -> $distbet"
echo ""
echo -e "Travel time -> $durabet"
echo ""

rm /tmp/pmdc.json

fi
fi
fi

exit 0


Save this script with the name 'pmdc' somewhere in your $PATH and 'chmod 755' it.
Open terminal and run 'pmdc' whenever you are planning a driving trip and it will give you the exact distance and approximate travel time.
By the way pmdc means Poor Man's Distance Calculator.
Cheers!!!
Title: Re: pmdc - calculate distance & travel time between two locations
Post by: PackRat on July 12, 2015, 06:38:44 PM
Cool.

Works - and I've done the drive a couple times so ~41 hours total time is pretty close.

(http://7.t.imgbox.com/5HRs3js7.jpg) (http://imgbox.com/5HRs3js7)