ooRexx logo
   1: #!/usr/bin/env rexx
   2: /*----------------------------------------------------------------- */
   3: /* Demonstrates the use of the geoLoc class                         */
   4: /*----------------------------------------------------------------- */
   5: /*                                                                  */
   6: /* Originally by Ruurd J. Idenburg                                  */
   7: /*                                                                  */
   8: /* No copyright, no licence, no guarantees or warrantees, be it     */
   9: /* explicit, implicit or whatever. Usage is totally and completely  */
  10: /* at the users own risk, the author shall not be liable for any    */ 
  11: /* damages whatsoever, for any reason whatsoever.                   */
  12: /*                                                                  */
  13: /* Please keep this comment block intact when modifying this code   */
  14: /* and add a note with date and a description.                      */
  15: /*                                                                  */
  16: /* ---------------------------------------------------------------- */
  17: /* 2013/12/08 - Initial version                                     */
  18: /* 2020/02/15 - Changes due to Google restrictions - RJI            */
  19: /* 2020/02/22 - Switching to ooRexx 5.00 beta to make use of        */
  20: /*              the address instruction "with" extension -RJI       */
  21: /* 2021/05/20 - Updated shebang line above                          */
  22: /* ---------------------------------------------------------------- */
  23: 
  24: /* 2020/02/15 
  25:  Google requires an apikey nowadays and also requires https instead
  26:  of http, so I'm not using xhr anymore but command line url now,
  27:  "curl", which is more or less standard on linux and can be 
  28:  downloaded from https://curl.haxx.se for windows and also for many
  29:  other systems.
  30:  I myself use Mint 19.3 since Windows 7 is not supported anymore.
  31:  So you need your own "apikey" and "curl" and ooRexx 5.00 beta. 
  32: */
  33: 
  34: jsonArray = .array~new
  35: address system with output using (jsonArray)
  36: 
  37: /* 2020/02/22
  38:  The Google api_Key is set as an environment variable at logon time
  39: */
  40: apiKey = Value("GOOGLE_APIKEY", ,"ENVIRONMENT")
  41: 
  42: begin = 'Wimbledonpark 193, Amstelveen, Nederland'
  43: end = 'Silversant, Noorddammerlaan, Amstelveen, Nederland'
  44: 
  45: say
  46: say 'Asking Google the walking route from:' begin
  47: say '                                  to:' end 
  48: say 'in JSON format'  
  49: say
  50: begin = begin~changestr(' ','%20')
  51: end = end~changestr(' ','%20')
  52:  
  53: "curl -s 'https://maps.googleapis.com/maps/api/directions/json?origin="begin"&destination="end"&mode=walking&language=nl&key="apiKey"'"
  54: 
  55: json = jsonArray~toString('c')
  56: 
  57: say 'Transforming JSON to an ooRexx Directory'
  58: jsonRoute = .json~new()~fromJson(json)
  59: say 'Retrieving route steps from ooRexx Directory, while storing steps in an Array'
  60: say
  61: googleSteps = .array~new
  62: routes = jsonRoute['routes']~items
  63: do r=1 to routes
  64:   route = jsonRoute['routes'][r]
  65:   legs = route['legs']~items
  66:   do l=1 to legs
  67:     leg = route['legs'][l]
  68:     steps = leg['steps']~items
  69:     say 'Dist  - Start Location       - End Location         - Encoded Path' 
  70:     do s=1 to steps
  71:       step = leg['steps'][s]
  72:       stepDist = (step['distance']['value']/1000)~format(,3) -- in meters
  73:       stepStart = step['start_location']['lat']~format(,7)','step['start_location']['lng']~format(,7)
  74:       stepEnd = step['end_location']['lat']~format(,7)','step['end_location']['lng']~format(,7)
  75:       stepPoly = step['polyline']['points']
  76:       stepItem = stepDist '-' stepStart '-' stepEnd '-' stepPoly
  77:       googleSteps~append(stepItem)
  78:       say stepItem
  79:     end
  80:   end
  81: end
  82: say
  83: say "Comparing Google's route distance with ours. Ours is probably shorter,"
  84: say "because Google also uses it's encoded polyline points in the calculation"
  85: say "and we don't. But our 2 calculations should be the same, as the geoPath"
  86: say "'distance' method uses the geoLoc 'distanceFrom' method."
  87: say
  88: say "Using 'decodeGString.rex' to decode the encoded polyline into latitude/longitude pairs"
  89: say "in each step of the route directions should yield a distance that should be equal to"
  90: say "Google's distance."
  91: say
  92: googleDist = 0
  93: ourDist = 0
  94: loopDist = 0~format(,3)
  95: path = .geoPath~new
  96: do s=1 to googleSteps~items
  97:   parse value googleSteps[s] with dist ' - ' loc1 ' - ' loc2 ' - ' poly
  98:   googleDist += dist
  99:   parse var loc1 lat ',' lon
 100:   loc1 = .geoLoc~new(lat,lon)
 101:   parse var loc2 lat ',' lon
 102:   loc2 = .geoLoc~new(lat,lon)
 103:   tempDist = loc1~distanceFrom(loc2)
 104:   ourDist += tempDist
 105:   path~append(loc1)
 106:   say loopDist '-' loc1~latitude','loc1~longitude
 107:   loopDist = tempDist
 108: end
 109: path~append(loc2)
 110: say tempDist '-' loc2~latitude','loc2~longitude
 111: pathDist = path~distance -- should be equal to ourDist
 112: say 'Google:' googleDist '- We(should be equal to next):' ourDist '- Us via geoPath:' pathDist 
 113: 
 114: ::requires 'geoloc.cls'
 115: ::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.