(* Calculate Open Location Code (OLC co-ordinates). Calculations from *) (* https://en.wikipedia.org/wiki/Open_Location_Code *) southMostLat = -80; northMostLat = 84 ; EastWestSize = 180 ; NorthSouthSize = 90 ; convertLatLng(latitude_, longitude_) := Module( {OLCstr}, (* error checking *) If( Not(And(NumberQ(latitude), NumberQ(longitude) ) ) , Return("Not a number"), ); If( latitude < southMostLat , Return("Latitude is too small"), ); If( latitude > northMostLat , Return("Latitude is too big"), ); If( longitude < -EastWestSize , Return("East longitude is too small"), ); If( longitude >= EastWestSize , Return("West longitude is too big"), ); OLCstr = convertLatLngToOLC(latitude, longitude) ); convertLatLngToOLC(latitude_, longitude_) := Module( {OLCdigits = "23456789CFGHJMPQRVWX", OLCstr = "", j, OLCbase = 20, latIndex, lngIndex, tempA, tempB, plusPos = 5, plusBreak = "+", Yind, Xind }, tempA = (latitude + NorthSouthSize) * OLCbase^3; tempB = (longitude + EastWestSize) * OLCbase^3; latIndex = IntegerDigits(Floor(tempA), OLCbase); lngIndex = IntegerDigits(Floor(tempB), OLCbase); For( j = 1, j <= 5, j++, If( j == plusPos, OLCstr = StringJoin(OLCstr, plusBreak), ); OLCstr = StringJoin(OLCstr, StringPart(OLCdigits, latIndex[[j]] + 1) ); OLCstr = StringJoin(OLCstr, StringPart(OLCdigits, lngIndex[[j]] + 1) ); ); Yind = Floor((tempA - Floor(tempA)) * 5) * 4; Xind = Floor((tempB - Floor(tempB)) * 4); OLCstr = StringJoin(OLCstr, StringPart(OLCdigits, Yind + Xind + 1) ) );