-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.lua
More file actions
142 lines (126 loc) · 3.83 KB
/
convert.lua
File metadata and controls
142 lines (126 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
-- Port of http://earth.motfe.net/coords.php
-- http://lua-users.org/wiki/FileInputOutput
-- Find the length of a file
-- filename: file name
-- returns
-- len: length of file
-- asserts on error
local json_filename = "Plugins/EarthTweaks/places.json"
local fs = {}
function fs.write(filename, contents)
if type(contents) ~= "string" then return end
local fh = assert(io.open(filename, "wb"))
fh:write(contents)
fh:flush()
fh:close()
end
function toDMS(lat, lon)
lat = lat or 0
lon = lon or 0
local function conversion(dd, latOrLon)
local dms = {}
dms.d = math.floor(dd+.5)
dms.m = math.floor(((dd - dms.d) * 60) + .5)
dms.s = (dd - dms.d - dms.m/60) * 3600
dms.pol = nil
if dms.d < 0 then
dms.d = -dms.d
end
if latOrLon == "latitude" then
if dd <= 0 then
dms.pol = "S"
else
dms.pol = "N"
end
elseif latOrLon == "longitude" then
if dd <= 0 then
dms.pol = "W"
else
dms.pol = "E"
end
end
return dms
end
return conversion(lat, "latitude"), conversion(lon, "longitude")
end
function toDD(lat, lon)
-- Soon(tm)
end
function toMCCoords(lat, lon)
local dms_lat, dms_lon = toDMS(lat, lon)
local lat = dms_lat.s + dms_lat.m * 60 + dms_lat.d * 3600
local lon = dms_lon.s + dms_lon.m * 60 + dms_lon.d * 3600
if dms_lat.pol == "N" then
lat = -lat
end
if dms_lon.pol == "W" then
lon = -lon
end
local mc_lat = lat/648000
local mc_lon = lon/324000
local mul_lat = 21600/CONFIG.MAPDIV
local mul_lon = 10800/CONFIG.MAPDIV
mc_lat = math.floor((mc_lat * mul_lat) + 0.5)
mc_lon = math.floor((mc_lon * mul_lon) + 0.5)
return mc_lat, mc_lon
end
function getCoordsFromPlace(lat, lon)
-- AAAARG I HAVE TO CACHE IT!!!!!
local function loadFromCache(json)
local foundLat = nil
local foundLon = nil
for k, v in pairs(json) do
LOG("key "..tostring(k))
LOG("value "..tostring(v))
if v == "lat" then
LOG((k["lat"]) - (lat))
if (k["lat"]) - (lat) < 1 then
foundLat = v
end
elseif v == "lon" then
LOG((k["lon"]) - (lon))
if (k["lon"]) - (lon) < 1 then
foundLon = v
end
end
if foundLat and foundLon then
return assert(k["display_name"]), assert(k["address"]["city"]..', '..string.upper(k["address"]["country_code"])), foundLat, foundLon
end
end
end
local file_r = cFile:ReadWholeFile(json_filename)
local nLat, nLon, name, weather_name
local j_file_r = {}
local _j_file_r = cJson:Parse(file_r)
if type(_j_file_r) == "table" then -- Tasty type checking
j_file_r = _j_file_r
nLat, nLon, name, weather_name = loadFromCache(j_file_r)
LOG("computation done, "..type(name))
else
fs.write(json_filename,'')
end
if not name then
-- Downloading if it's not cached
local url = "https://nominatim.openstreetmap.org/reverse?format=json&lat="..lat.."&lon="..lon
local res, body, jsonBody = cUrlClient:Get(url, function(a_Body, a_Data)
if a_Body then
LOG(a_Body)
return 1, a_Body, cJson:Parse(a_Body)
else
return 0, a_Data
end
end)
fs.write(json_filename, body)
if type(jsonBody) == 'table' then
result = jsonBody["display_name"], jsonBody["address"]["city"]..', '..string.upper(jsonBody["address"]["country_code"])
end
end
return name, weather_name
end
-- Mere testing of the code
local testlat = 10.2532284
local testlon = -67.5926057
local lat, lon = toDMS(testlat, testlon)
local mlat, mlon = toMCCoords(testlat, testlon)
local rlat, rlon, name, weather_name = getCoordsFromPlace(testlat, testlon)
LOG(name)