Fixing distance calculation issues

This commit is contained in:
Alex Epstein 2017-07-10 20:41:43 -04:00
parent 79bed61d70
commit 05cd4dadf3
1 changed files with 28 additions and 33 deletions

View File

@ -90,10 +90,12 @@ update()
getDirections() getDirections()
{ {
response=$(httpGet "https://www.mapquestapi.com/directions/v2/route?key=$MAPQUEST_API_KEY&from=$1&to=$2&outFormat=json&ambiguities=ignore&routeType=fastest&doReverseGeocode=false&enhancedNarrative=true&avoidTimedConditions=false") response=$(httpGet "https://www.mapquestapi.com/directions/v2/route?key=$MAPQUEST_API_KEY&from=$1&to=$2&outFormat=json&ambiguities=ignore&routeType=fastest&doReverseGeocode=false&enhancedNarrative=false&avoidTimedConditions=false")
firstDirection=$(echo $response | grep -Eo "origNarrative\":\"[a-Z -./0-9]*" | grep -Eo "[^origNarrative\":][a-Z -./0-9]*" ) firstDirection=$(echo $response | grep -Eo "origNarrative\":\"[a-Z -./0-9]*" | grep -Eo "[^origNarrative\":][a-Z -./0-9]*" )
tempDirections=($(echo $response | grep -Eo "\"narrative\":\"[a-Z -./0-9]*")) tempDirections=($(echo $response | grep -Eo "\"narrative\":\"[a-Z -./0-9]*"))
distances=($(echo $response | grep -Eo "distance\":[0-9]*[.][0-9]*" | grep -Eo "[^distance\":][0-9]*[.][0-9]*" )) distances=($(echo $response | grep -Eo "distance\":[0-9]*[.][0-9]*" | grep -Eo "[^distance\":][0-9]*[.][0-9]*" ))
totalDist=${distances[0]}
count="0"
count="-1" count="-1"
temp=${tempDirections[0]} temp=${tempDirections[0]}
for direct in "${tempDirections[@]}" for direct in "${tempDirections[@]}"
@ -112,19 +114,12 @@ do
getDirectionsMap() getDirectionsMap()
{ {
checkImagemagick || exit 1 echo "Generating route map from $unformattedFromLocation to $unformattedToLocation"
echo "Generating route map from $1 to $2"
if [[ "$OSTYPE" == "linux"* ]];then
if command -v display &>/dev/null ;then
mkdir ~/temp || return 1 mkdir ~/temp || return 1
httpGet "https://www.mapquestapi.com/staticmap/v5/map?start=$1&end=$2&size=600,400@2x&key=$MAPQUEST_API_KEY" >> ~/temp/routeImage.png || return 1 httpGet "https://www.mapquestapi.com/staticmap/v5/map?start=$1&end=$2&size=600,400@2x&key=$MAPQUEST_API_KEY" >> ~/temp/routeImage.png || return 1
if [[ "$OSTYPE" == "linux"* ]];then
display ~/temp/routeImage.png > /dev/null || return 1 display ~/temp/routeImage.png > /dev/null || return 1
else
echo "To use the map image features you must install imagemagick"
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then elif [[ "$OSTYPE" == "darwin"* ]]; then
mkdir ~/temp
httpGet "https://www.mapquestapi.com/staticmap/v5/map?start=$1&end=$2&size=600,400@2x&key=$MAPQUEST_API_KEY" >> ~/temp/routeImage.png || return 1
open ~/temp/routeImage.png > /dev/null open ~/temp/routeImage.png > /dev/null
fi fi
rm -rf ~/temp > /dev/null rm -rf ~/temp > /dev/null
@ -133,24 +128,19 @@ getDirectionsMap()
printDirections() printDirections()
{ {
echo echo
totalDist="0" echo "From $unformattedFromLocation to $unformattedToLocation ($totalDist mi)"
for dist in "${distances[@]}"
do
totalDist=$(echo $totalDist + $dist | bc)
done
echo "From $1 to $2 ($totalDist mi)"
echo "===================================================" echo "==================================================="
count=0 count=0
for direct in "${directions[@]}" for direct in "${directions[@]}"
do do
echo -n "$(echo $count + 1 | bc)). " if [ $count -ne 0 ];then echo -n "$(echo $count | bc)). ";fi
if [[ $direct != "" ]];then # sometimes original Narrative is blank if [[ $direct != "" ]];then # sometimes original Narrative is blank
direct="$direct (${distances[$count]} mi)" direct="$direct (${distances[$(echo $count + 1 | bc)]} mi)"
if [ $count -ne 0 ];then echo $direct;fi
count=$(echo $count + 1 | bc ) count=$(echo $count + 1 | bc )
echo $direct
fi fi
done done
echo "Welcome to $2" echo "Welcome to $unformattedToLocation"
echo "===================================================" echo "==================================================="
echo echo
} }
@ -163,18 +153,22 @@ getLocations()
read toLocation read toLocation
unformattedFromLocation=$fromLocation unformattedFromLocation=$fromLocation
unformattedToLocation=$toLocation unformattedToLocation=$toLocation
fromLocation=$(echo $fromLocation | sed s/','/""/g | sed s/' '/"+"/g ) fromLocation=$(echo $fromLocation | sed s/','/"+"/g | sed s/' '/"+"/g )
toLocation=$(echo $toLocation | sed s/','/""/g | sed s/' '/"+"/g ) toLocation=$(echo $toLocation | sed s/','/"+"/g | sed s/' '/"+"/g )
} }
checkImagemagick() checkImagemagick()
{ {
if [[ "$OSTYPE" == "linux"* ]];then
if ! command -v display &>/dev/null ;then if ! command -v display &>/dev/null ;then
echo "Error: you need to install imagemagick to use map features." &>2 echo "Error: you need to install imagemagick to use map features." &>2
return 1 return 1
else else
return 0 return 0
fi fi
else
return 0
fi
} }
@ -183,18 +177,16 @@ getMapLocation()
echo -n "Enter the city or address you want to generate a map for: " echo -n "Enter the city or address you want to generate a map for: "
read mapLocation read mapLocation
echo "Generating static map for $mapLocation" echo "Generating static map for $mapLocation"
mapLocation==$(echo $mapLocation | sed s/','/""/g | sed s/' '/"+"/g ) mapLocation==$(echo $mapLocation | sed s/','/"+"/g | sed s/' '/"+"/g )
} }
getStaticMap() getStaticMap()
{ {
if [[ "$OSTYPE" == "linux"* ]];then
mkdir ~/temp || return 1 mkdir ~/temp || return 1
httpGet "https://www.mapquestapi.com/staticmap/v5/map?key=$MAPQUEST_API_KEY&center=$1&zoom=15&type=hyb&size=600,400@2x" >> ~/temp/mapImage.png || return 1 httpGet "https://www.mapquestapi.com/staticmap/v5/map?key=$MAPQUEST_API_KEY&center=$1&zoom=15&type=hyb&size=600,400@2x" >> ~/temp/mapImage.png || return 1
if [[ "$OSTYPE" == "linux"* ]];then
display ~/temp/mapImage.png > /dev/null || return 1 display ~/temp/mapImage.png > /dev/null || return 1
elif [[ "$OSTYPE" == "darwin"* ]]; then elif [[ "$OSTYPE" == "darwin"* ]]; then
mkdir ~/temp || return 1
httpGet "https://www.mapquestapi.com/staticmap/v5/map?key=$MAPQUEST_API_KEY&center=$1&zoom=15&type=hyb&size=600,400@2x" >> ~/temp/mapImage.png || return 1
open ~/temp/mapImage.png > /dev/null || return 1 open ~/temp/mapImage.png > /dev/null || return 1
fi fi
rm -rf ~/temp > /dev/null || return 1 rm -rf ~/temp > /dev/null || return 1
@ -265,5 +257,8 @@ elif [[ $directionsFlag == "1" ]];then
printDirections $fromLocation $toLocation || exit 1 printDirections $fromLocation $toLocation || exit 1
checkImagemagick || exit 1 checkImagemagick || exit 1
if [[ $directionsMapFlag == "1" ]];then getDirectionsMap $fromLocation $toLocation || exit 1;fi if [[ $directionsMapFlag == "1" ]];then getDirectionsMap $fromLocation $toLocation || exit 1;fi
if [[ $staticMapFlag = "1" ]];then echo "Generating static map for $unformattedFromLocation" && getStaticMap $fromLocation || exit 1 && echo "Generating static map for $unformattedToLocation" && getStaticMap $toLocation || exit 1; fi if [[ $staticMapFlag = "1" ]];then
echo "Generating static map for $unformattedFromLocation" && getStaticMap $fromLocation || exit 1
echo "Generating static map for $unformattedToLocation" && getStaticMap $toLocation || exit 1
fi
fi fi