From 3088dd0342d32172a263019698feb50a3cb263e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Rodrigo?= Date: Wed, 6 Jun 2018 10:53:57 +0200 Subject: [PATCH] Better and flexible weight parsing in measure.lua --- profiles/lib/measure.lua | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/profiles/lib/measure.lua b/profiles/lib/measure.lua index b433fbe9b..88bbcbd74 100644 --- a/profiles/lib/measure.lua +++ b/profiles/lib/measure.lua @@ -5,6 +5,7 @@ Measure = {} -- measurements conversion constants local inch_to_meters = 0.0254 local feet_to_inches = 12 +local pound_to_kilograms = 0.45359237 --- Parse string as a height in meters. --- according to http://wiki.openstreetmap.org/wiki/Key:maxheight @@ -25,33 +26,19 @@ function Measure.parse_value_meters(value) end end ---- according to http://wiki.openstreetmap.org/wiki/Map_Features/Units#Explicit_specifications -local tonns_parse_patterns = Sequence { - "%d+", - "%d+.%d+", - "%d+.%d+ ?t" -} - -local kg_parse_patterns = Sequence { - "%d+ ?kg" -} - ---- Parse weight value in kilograms +--- Parse weight value in kilograms. +--- according to https://wiki.openstreetmap.org/wiki/Key:maxweight function Measure.parse_value_kilograms(value) - -- try to parse kilograms - for i, templ in ipairs(kg_parse_patterns) do - m = string.match(value, templ) - if m then - return tonumber(m) - end - end - - -- 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 + local n = tonumber(value:gsub(",", "."):match("%d+%.?%d*")) + if n then + if string.match(value, "lbs") then + n = n * pound_to_kilograms + elseif string.match(value, "kg") then + -- n = n + else -- Default, metric tons + n = n * 1000 end + return n end end