(* 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 = 2, plusBreak = "+", Yind, Xind, tmpNum }, tempA = (latitude + NorthSouthSize) * OLCbase^3; latNum = Floor(tempA); Yind = Floor((tempA - latNum) * 5) * 4; tempB = (longitude + EastWestSize) * OLCbase^3; lngNum = Floor(tempB); Xind = Floor((tempB - lngNum) * 4); For( j = 1, j <= 5, j++, If( j == plusPos, OLCstr = StringJoin(plusBreak, OLCstr), ); tmpNum = Mod(lngNum, OLCbase); lngNum = (lngNum - tmpNum) / OLCbase; OLCstr = StringJoin(StringPart(OLCdigits, tmpNum + 1), OLCstr); tmpNum = Mod(latNum, OLCbase); latNum = (latNum - tmpNum) / OLCbase; OLCstr = StringJoin(StringPart(OLCdigits, tmpNum + 1), OLCstr); ); OLCstr = StringJoin(OLCstr, StringPart(OLCdigits, Yind + Xind + 1) ) );