Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
refactor: iterate over substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
rush42 committed May 7, 2024
1 parent 9f9e2cf commit 51a5928
Showing 1 changed file with 40 additions and 50 deletions.
90 changes: 40 additions & 50 deletions processing/topics/helper/SanitizeTrafficSign.lua
Original file line number Diff line number Diff line change
@@ -1,62 +1,52 @@
require("IsTermInString")

-- Cleanup spaces where none should be
local function CleanupSpaces(string)
local cleaned = string
if IsTermInString(', ', cleaned) then
cleaned = cleaned.gsub(cleaned, ', ', ',')
end
if IsTermInString('; ', cleaned) then
cleaned = cleaned.gsub(cleaned, '; ', ';')
end
return cleaned
-- Remove all whitespaces after delimeters
local function stripWhitespaces(traffic_sign)
local stripped = string.gsub(traffic_sign, ', ', ',')
stripped = string.gsub(stripped, '; ', ';')
return stripped
end

-- * @desc Cleanup the `traffic_sign=*` tag
-- * @returns string
function SanitizeTrafficSign(traffic_sign_value)
if traffic_sign_value == nil then
--- Cleanup the `traffic_sign=*` tag
--- @param traffic_sign string
--- @returns string
function SanitizeTrafficSign(traffic_sign)
if traffic_sign == nil then
return nil
end

-- Rename
-- Docs: gsub with "^" targets beginning of string
if traffic_sign_value == "no" then
if traffic_sign == "no" or traffic_sign == 'none' then
return "none"
end
if osm2pgsql.has_prefix(traffic_sign_value, 'DE: ') then
return CleanupSpaces(string.gsub(traffic_sign_value, '^DE: ', 'DE:'))
end
if osm2pgsql.has_prefix(traffic_sign_value, 'DE2') then
return CleanupSpaces(string.gsub(traffic_sign_value, '^DE2', 'DE:2'))
end
if osm2pgsql.has_prefix(traffic_sign_value, 'DE1') then
return CleanupSpaces(string.gsub(traffic_sign_value, '^DE1', 'DE:1'))
end
if osm2pgsql.has_prefix(traffic_sign_value, 'D:') then
return CleanupSpaces(string.gsub(traffic_sign_value, '^D:', 'DE:'))
end
if osm2pgsql.has_prefix(traffic_sign_value, 'de:') then
return CleanupSpaces(string.gsub(traffic_sign_value, '^de:', 'DE:'))
end
if osm2pgsql.has_prefix(traffic_sign_value, 'DE.') then
return CleanupSpaces(string.gsub(traffic_sign_value, '^DE.', 'DE:'))
end
if osm2pgsql.has_prefix(traffic_sign_value, '2') then
return CleanupSpaces(string.gsub(traffic_sign_value, '^2', 'DE:2'))
end
if osm2pgsql.has_prefix(traffic_sign_value, '10') then
return CleanupSpaces(string.gsub(traffic_sign_value, '^10', 'DE:10'))
end

-- Allows
if osm2pgsql.has_prefix(traffic_sign_value, 'DE:') then
return CleanupSpaces(traffic_sign_value)
-- Docs: patterns with "^" target beginning of string

-- This is the correct tagging, all traffic signs should start with DE:
if string.find(traffic_sign, '^DE:%S') then
return stripWhitespaces(traffic_sign)
end
if traffic_sign_value == "none" then
return traffic_sign_value

local substitutions = {
['^DE: '] = 'DE:',
['^DE.'] = 'DE:',
['^D:'] = 'DE:',
['^D%.'] = 'DE:',
['^de:'] = 'DE:',
['^DE1'] = 'DE:1',
['^DE2'] = 'DE:2',
['^2'] = 'DE:2',
['^1'] = 'DE:1',
-- These patterns could handle all the above in a more generalized way
-- ['^DE?[:.]%s?'] = 'DE:',
-- ['^de?[:.]%s?'] = 'DE:'
-- ['^DE(%d)'] = 'DE:%1'
-- ['^(%d)'] = 'DE:%1'
}
for pattern, substitude in pairs(substitutions) do
local val, n = string.gsub(traffic_sign, pattern, substitude)
if n > 0 then
-- TODO: add to todo list
return stripWhitespaces(val)
end
end

-- Disallow everything else
-- Discard everything else
return nil
end

0 comments on commit 51a5928

Please sign in to comment.