ooRexx logo
#!/usr/bin/env rexx
/*----------------------------------------------------------------- */
/* Demonstrates the use of the geoLoc class                         */
/*----------------------------------------------------------------- */
/*                                                                  */
/* Originally by Ruurd J. Idenburg                                  */
/*                                                                  */
/* No copyright, no licence, no guarantees or warrantees, be it     */
/* explicit, implicit or whatever. Usage is totally and completely  */
/* at the users own risk, the author shall not be liable for any    */
/* damages whatsoever, for any reason whatsoever.                   */
/*                                                                  */
/* Please keep this comment block intact when modifying this code   */
/* and add a note with date and a description.                      */
/*                                                                  */
/* ---------------------------------------------------------------- */
/* 2013/12/08 - Initial version                                     */
/* 2020/02/15 - Changes due to Google restrictions - RJI            */
/* 2020/02/22 - Switching to ooRexx 5.00 beta to make use of        */
/*              the address instruction "with" extension -RJI       */
/* 2021/05/20 - Updated shebang line above                          */
/* ---------------------------------------------------------------- */

/* 2020/02/15
 Google requires an apikey nowadays and also requires https instead
 of http, so I'm not using xhr anymore but command line url now,
 "curl", which is more or less standard on linux and can be
 downloaded from https://curl.haxx.se for windows and also for many
 other systems.
 I myself use Mint 19.3 since Windows 7 is not supported anymore.
 So you need your own "apikey" and "curl" and ooRexx 5.00 beta.
*/


jsonArray = .array~new
address system with output using (jsonArray)

/* 2020/02/22
 The Google api_Key is set as an environment variable at logon time
*/

apiKey = Value("GOOGLE_APIKEY", ,"ENVIRONMENT")

begin = 'Wimbledonpark 193, Amstelveen, Nederland'
end = 'Silversant, Noorddammerlaan, Amstelveen, Nederland'

say
say 'Asking Google the walking route from:' begin
say '                                  to:' end
say 'in JSON format'  
say
begin = begin~changestr(' ','%20')
end = end~changestr(' ','%20')
 
"curl -s 'https://maps.googleapis.com/maps/api/directions/json?origin="begin"&destination="end"&mode=walking&language=nl&key="apiKey"'"

json = jsonArray~toString('c')

say 'Transforming JSON to an ooRexx Directory'
jsonRoute = .json~new()~fromJson(json)
say 'Retrieving route steps from ooRexx Directory, while storing steps in an Array'
say
googleSteps = .array~new
routes = jsonRoute['routes']~items
do r=1 to routes
  route = jsonRoute['routes'][r]
  legs = route['legs']~items
  do l=1 to legs
    leg = route['legs'][l]
    steps = leg['steps']~items
    say 'Dist  - Start Location       - End Location         - Encoded Path'
    do s=1 to steps
      step = leg['steps'][s]
      stepDist = (step['distance']['value']/1000)~format(,3) -- in meters
      stepStart = step['start_location']['lat']~format(,7)','step['start_location']['lng']~format(,7)
      stepEnd = step['end_location']['lat']~format(,7)','step['end_location']['lng']~format(,7)
      stepPoly = step['polyline']['points']
      stepItem = stepDist '-' stepStart '-' stepEnd '-' stepPoly
      googleSteps~append(stepItem)
      say stepItem
    end
  end
end
say
say "Comparing Google's route distance with ours. Ours is probably shorter,"
say "because Google also uses it's encoded polyline points in the calculation"
say "and we don't. But our 2 calculations should be the same, as the geoPath"
say "'distance' method uses the geoLoc 'distanceFrom' method."
say
say "Using 'decodeGString.rex' to decode the encoded polyline into latitude/longitude pairs"
say "in each step of the route directions should yield a distance that should be equal to"
say "Google's distance."
say
googleDist = 0
ourDist = 0
loopDist = 0~format(,3)
path = .geoPath~new
do s=1 to googleSteps~items
  parse value googleSteps[s] with dist ' - ' loc1 ' - ' loc2 ' - ' poly
  googleDist += dist
  parse var loc1 lat ',' lon
  loc1 = .geoLoc~new(lat,lon)
  parse var loc2 lat ',' lon
  loc2 = .geoLoc~new(lat,lon)
  tempDist = loc1~distanceFrom(loc2)
  ourDist += tempDist
  path~append(loc1)
  say loopDist '-' loc1~latitude','loc1~longitude
  loopDist = tempDist
end
path~append(loc2)
say tempDist '-' loc2~latitude','loc2~longitude
pathDist = path~distance -- should be equal to ourDist
say 'Google:' googleDist '- We(should be equal to next):' ourDist '- Us via geoPath:' pathDist

::requires 'geoloc.cls'
::requires 'json.cls'
 
If you feel inclined to make corrections, suggestions etc., please mail me any.
All content © Ruurd Idenburg, 2007–, except where marked otherwise. All rights reserved. This page is primarily for non-commercial use only. The Idenburg website records no personal information and sets no ‘cookies’. This site is hosted on a VPS(Virtual Private System) rented from Transip.nl, a Dutch company, falling under Dutch (privacy) laws (I think).

This page updated on by Ruurd Idenburg.