Better and flexible weight parsing in measure.lua
This commit is contained in:
parent
d2590989f5
commit
3088dd0342
@ -5,6 +5,7 @@ Measure = {}
|
|||||||
-- measurements conversion constants
|
-- measurements conversion constants
|
||||||
local inch_to_meters = 0.0254
|
local inch_to_meters = 0.0254
|
||||||
local feet_to_inches = 12
|
local feet_to_inches = 12
|
||||||
|
local pound_to_kilograms = 0.45359237
|
||||||
|
|
||||||
--- Parse string as a height in meters.
|
--- Parse string as a height in meters.
|
||||||
--- according to http://wiki.openstreetmap.org/wiki/Key:maxheight
|
--- according to http://wiki.openstreetmap.org/wiki/Key:maxheight
|
||||||
@ -25,33 +26,19 @@ function Measure.parse_value_meters(value)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- according to http://wiki.openstreetmap.org/wiki/Map_Features/Units#Explicit_specifications
|
--- Parse weight value in kilograms.
|
||||||
local tonns_parse_patterns = Sequence {
|
--- according to https://wiki.openstreetmap.org/wiki/Key:maxweight
|
||||||
"%d+",
|
|
||||||
"%d+.%d+",
|
|
||||||
"%d+.%d+ ?t"
|
|
||||||
}
|
|
||||||
|
|
||||||
local kg_parse_patterns = Sequence {
|
|
||||||
"%d+ ?kg"
|
|
||||||
}
|
|
||||||
|
|
||||||
--- Parse weight value in kilograms
|
|
||||||
function Measure.parse_value_kilograms(value)
|
function Measure.parse_value_kilograms(value)
|
||||||
-- try to parse kilograms
|
local n = tonumber(value:gsub(",", "."):match("%d+%.?%d*"))
|
||||||
for i, templ in ipairs(kg_parse_patterns) do
|
if n then
|
||||||
m = string.match(value, templ)
|
if string.match(value, "lbs") then
|
||||||
if m then
|
n = n * pound_to_kilograms
|
||||||
return tonumber(m)
|
elseif string.match(value, "kg") then
|
||||||
end
|
-- n = n
|
||||||
end
|
else -- Default, metric tons
|
||||||
|
n = n * 1000
|
||||||
-- try to parse tonns
|
|
||||||
for i, templ in ipairs(tonns_parse_patterns) do
|
|
||||||
m = string.match(value, templ)
|
|
||||||
if m then
|
|
||||||
return tonumber(m) * 1000
|
|
||||||
end
|
end
|
||||||
|
return n
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user