Kooky geo

From awesome

Jump to: navigation, search

This little script lets you calculate the distance (in meter) between two locations. Screenshot

[edit] Function code

You will need the lua socket library http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/

 local http = require("socket.http")
 local print = print
 local math = math
 local string = string
 local tonumber = tonumber
 local sin = math.sin
 local cos = math.cos
 local asin = math.asin
 local sqrt = math.sqrt
 local min = math.min
 local abs = math.abs
 local pi = math.pi
 
 module("geo")
 local googleKey
 
 function distance(from, to)
  local distance = 0
  local radius = 6367000
  local radian = pi / 180
  local deltaLatitude = sin(radian * (from.latitude - to.latitude) /2)
  local deltaLongitude = sin(radian * (from.longitude - to.longitude) / 2)
  
  local circleDistance = 2 * asin(min(1, sqrt(deltaLatitude * deltaLatitude + 
     cos(radian * from.latitude) * cos(radian * to.latitude) * deltaLongitude * deltaLongitude)))
  distance = abs(radius * circleDistance)
  return distance
 end
 
 function point(address)
  local point = {}
  point.accuracy = 0
  point.latitude = 0.0
  point.longitude = 0.0
  address = string.gsub(address, "([^A-Za-z0-9_])", function(c)
   return string.format("%%%02x", string.byte(c))
   end)
  local request = "http://maps.google.com/maps/geo?output=csv&q="..address.."&key="
  if googleKey then
   request = request..googleKey
  end
 
  local c, err, h = http.request(request)
  if c then
   err, point.accuracy, point.latitude, point.longitude = c:match("(.*),(.*),(.*),(.*)")
  end
  
  point.accuracy = tonumber(point.accuracy)
  point.latitude = tonumber(point.latitude)
  point.longitude = tonumber(point.longitude)
 
  return point
 end


[edit] Usage

After that you can set your own location in your rc.lua

 require("geo")
 
 location = geo.point("Your Street and City")

And bind a key so you have a prompt where you can enter any location you wanna move or something and the script will show you the distance and any other nessary information about the location

 table.insert(globalkeys, key({ modkey }, "F6", function ()
   awful.prompt.run({ prompt = "Address: " },
     promptbox[mouse.screen],
     function(h)
       local point = geo.point(h)
       naughty.notify({ 
         title = h,
         text = string.format('%s', "monospace 6",
           "Latitude: "..point.latitude.."\n"..
           "Longitude: "..point.longitude.."\n"..
           "Accuracy: "..point.accuracy.."\n"..
           "Distance: "..geo.distance(location, point).."m"), 
         hover_timeout = 3, 
         timeout = 10,
       })
 end)
 end))
Personal tools