Profile: stronger unit parsing
This commit is contained in:
parent
c7b1d0c131
commit
ddf11cc2cc
@ -6,66 +6,38 @@ Measure = {}
|
|||||||
local inch_to_meters = 0.0254
|
local inch_to_meters = 0.0254
|
||||||
local feet_to_inches = 12
|
local feet_to_inches = 12
|
||||||
|
|
||||||
|
--- 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
|
||||||
local meters_parse_patterns = Sequence {
|
function Measure.parse_value_meters(value)
|
||||||
"%d+",
|
local n = tonumber(value:gsub(",", "."):match("%d+%.?%d*"))
|
||||||
"%d+.%d+",
|
if n then
|
||||||
"%d+.%d+ m",
|
inches = value:match("'.*")
|
||||||
"%d+,%d+ m", -- wrong
|
if inches then -- Imperial unit to metric
|
||||||
"%d+.%d+m", -- wrong
|
-- try to parse feets/inch
|
||||||
"%d+,%d+m", -- wrong
|
n = n * feet_to_inches
|
||||||
}
|
local m = tonumber(inches:match("%d+"))
|
||||||
|
if m then
|
||||||
|
n = n + m
|
||||||
|
end
|
||||||
|
n = n * inch_to_meters
|
||||||
|
end
|
||||||
|
return n
|
||||||
|
end
|
||||||
|
|
||||||
local feet_parse_patterns = Sequence {
|
print("Can't parse value: ", value)
|
||||||
"%d+\'%d+\'",
|
end
|
||||||
}
|
|
||||||
|
|
||||||
--- according to http://wiki.openstreetmap.org/wiki/Map_Features/Units#Explicit_specifications
|
--- according to http://wiki.openstreetmap.org/wiki/Map_Features/Units#Explicit_specifications
|
||||||
local tonns_parse_patterns = Sequence {
|
local tonns_parse_patterns = Sequence {
|
||||||
"%d+",
|
"%d+",
|
||||||
"%d+.%d+",
|
"%d+.%d+",
|
||||||
"%d+.%d+ t"
|
"%d+.%d+ ?t"
|
||||||
}
|
}
|
||||||
|
|
||||||
local kg_parse_patterns = Sequence {
|
local kg_parse_patterns = Sequence {
|
||||||
"%d+ kg"
|
"%d+ ?kg"
|
||||||
}
|
}
|
||||||
|
|
||||||
function Measure.convert_feet_to_inches(feet)
|
|
||||||
return feet * feet_to_inches
|
|
||||||
end
|
|
||||||
|
|
||||||
function Measure.convert_inches_to_meters(inches)
|
|
||||||
return inches * inch_to_meters
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Parse string as a height in meters.
|
|
||||||
function Measure.parse_value_meters(value)
|
|
||||||
-- try to parse meters
|
|
||||||
for i, templ in ipairs(meters_parse_patterns) do
|
|
||||||
m = string.match(value, templ)
|
|
||||||
if m then
|
|
||||||
return tonumber(m)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- try to parse feets/inch
|
|
||||||
for i, templ in ipairs(feet_parse_patterns) do
|
|
||||||
m = string.match(value, templ)
|
|
||||||
if m then
|
|
||||||
feet, inch = m
|
|
||||||
feet = tonumber(feet)
|
|
||||||
inch = tonumber(inch)
|
|
||||||
|
|
||||||
inch = inch + feet * feet_to_inches
|
|
||||||
return Measure.convert_inches_to_meters(inch)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
print("Can't parse value: ", value)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Parse weight value in kilograms
|
--- Parse weight value in kilograms
|
||||||
function Measure.parse_value_kilograms(value)
|
function Measure.parse_value_kilograms(value)
|
||||||
-- try to parse kilograms
|
-- try to parse kilograms
|
||||||
@ -91,31 +63,25 @@ end
|
|||||||
|
|
||||||
--- Get maxheight of specified way in meters. If there are no
|
--- Get maxheight of specified way in meters. If there are no
|
||||||
--- max height, then return nil
|
--- max height, then return nil
|
||||||
function Measure.get_max_height(way)
|
function Measure.get_max_height(raw_value)
|
||||||
raw_value = way:get_value_by_key('maxheight')
|
|
||||||
if raw_value then
|
if raw_value then
|
||||||
return Measure.parse_value_meters(raw_value)
|
return Measure.parse_value_meters(raw_value)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO: parse another tags
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Get maxwidth of specified way in meters.
|
--- Get maxwidth of specified way in meters.
|
||||||
function Measure.get_max_width(way)
|
function Measure.get_max_width(raw_value)
|
||||||
raw_value = way:get_value_by_key('maxwidth')
|
|
||||||
if raw_value then
|
if raw_value then
|
||||||
return Measure.parse_value_meters(raw_value)
|
return Measure.parse_value_meters(raw_value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Get maxweight of specified way in kilogramms
|
--- Get maxweight of specified way in kilogramms
|
||||||
function Measure.get_max_weight(way)
|
function Measure.get_max_weight(raw_value)
|
||||||
raw_value = way:get_value_by_key('maxweight')
|
|
||||||
if raw_value then
|
if raw_value then
|
||||||
-- print(way:id(), raw_value)
|
|
||||||
return Measure.parse_value_kilograms(raw_value)
|
return Measure.parse_value_kilograms(raw_value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return Measure;
|
return Measure;
|
||||||
|
Loading…
Reference in New Issue
Block a user