Add distance_to function to the tile38 namespace in lua.

distance_to(lat_a, lon_a, lat_b, lon_b) returns distance in meters between points A and B.
This commit is contained in:
Alex Roitman 2019-01-04 14:57:00 -08:00
parent 35dbb0cd6f
commit 5e215c247d
2 changed files with 13 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/tidwall/geojson/geo"
"github.com/tidwall/resp" "github.com/tidwall/resp"
"github.com/tidwall/tile38/internal/log" "github.com/tidwall/tile38/internal/log"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
@ -142,12 +143,22 @@ func (pl *lStatePool) New() *lua.LState {
ls.Push(lua.LString(shaSum)) ls.Push(lua.LString(shaSum))
return 1 return 1
} }
distanceTo := func(ls *lua.LState) int {
dt := geo.DistanceTo(
float64(ls.ToNumber(1)),
float64(ls.ToNumber(2)),
float64(ls.ToNumber(3)),
float64(ls.ToNumber(4)))
ls.Push(lua.LNumber(dt))
return 1
}
var exports = map[string]lua.LGFunction{ var exports = map[string]lua.LGFunction{
"call": call, "call": call,
"pcall": pcall, "pcall": pcall,
"error_reply": errorReply, "error_reply": errorReply,
"status_reply": statusReply, "status_reply": statusReply,
"sha1hex": sha1hex, "sha1hex": sha1hex,
"distance_to": distanceTo,
} }
L.SetGlobal("tile38", L.SetFuncs(L.NewTable(), exports)) L.SetGlobal("tile38", L.SetFuncs(L.NewTable(), exports))

View File

@ -24,6 +24,8 @@ func scripts_BASIC_test(mc *mockServer) error {
{"EVAL", "return KEYS[1] .. ' only'", 1, "key1"}, {"key1 only"}, {"EVAL", "return KEYS[1] .. ' only'", 1, "key1"}, {"key1 only"},
{"EVAL", "return KEYS[1] .. ' and ' .. ARGV[1]", 1, "key1", "arg1"}, {"key1 and arg1"}, {"EVAL", "return KEYS[1] .. ' and ' .. ARGV[1]", 1, "key1", "arg1"}, {"key1 and arg1"},
{"EVAL", "return ARGV[1] .. ' and ' .. ARGV[2]", 0, "arg1", "arg2"}, {"arg1 and arg2"}, {"EVAL", "return ARGV[1] .. ' and ' .. ARGV[2]", 0, "arg1", "arg2"}, {"arg1 and arg2"},
{"EVAL", "return tile38.sha1hex('asdf')", 0}, {"3da541559918a808c2402bba5012f6c60b27661c"},
{"EVAL", "return tile38.distance_to(37.7341129, -122.4408378, 37.733, -122.43)", 0}, {"961"},
}) })
} }