1 -- Nominatim themepark theme.
 
   3 -- The Nominatim theme creates a fixed set of import tables for use with
 
   4 -- Nominatim. Creation and object processing are directly controlled by
 
   5 -- the theme. Topics provide preset configurations. You should add exactly
 
   6 -- one topic to your project.
 
   8 -- The theme also exports a number of functions that can be used to configure
 
   9 -- its behaviour. These may be directly called in the style file after
 
  10 -- importing the theme:
 
  12 --      local nominatim = themepark:init_theme('nominatim')
 
  13 --      nominatim.set_main_tags{boundary = 'always'}
 
  15 -- This allows to write your own configuration from scratch. You can also
 
  16 -- use it to customize topics. In that case, first add the topic, then
 
  17 -- change the configuration:
 
  19 --      themepark:add_topic('nominatim/full')
 
  20 --      local nominatim = themepark:init_theme('nominatim')
 
  21 --      nominatim.ignore_tags{'amenity'}
 
  25 local POST_DELETE = nil
 
  26 local MAIN_KEYS = {admin_level = {'delete'}}
 
  27 local PRE_FILTER = {prefix = {}, suffix = {}}
 
  29 local ADDRESS_TAGS = nil
 
  30 local SAVE_EXTRA_MAINS = false
 
  31 local POSTCODE_FALLBACK = true
 
  33 -- This file can also be directly require'd instead of running it under
 
  34 -- the themepark framework. In that case the first parameter is usually
 
  35 -- the module name. Lets check for that, so that further down we can call
 
  36 -- the low-level osm2pgsql functions instead of themepark functions.
 
  38 if type(themepark) ~= 'table' then
 
  42 -- tables required for taginfo
 
  43 module.TAGINFO_MAIN = {keys = {}, delete_tags = {}}
 
  44 module.TAGINFO_NAME_KEYS = {}
 
  45 module.TAGINFO_ADDRESS_KEYS = {}
 
  48 -- The single place table.
 
  49 local place_table_definition = {
 
  51     ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
 
  53         { column = 'class', type = 'text', not_null = true },
 
  54         { column = 'type', type = 'text', not_null = true },
 
  55         { column = 'admin_level', type = 'smallint' },
 
  56         { column = 'name', type = 'hstore' },
 
  57         { column = 'address', type = 'hstore' },
 
  58         { column = 'extratags', type = 'hstore' },
 
  59         { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true },
 
  61     data_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_DATA"),
 
  62     index_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_INDEX"),
 
  69     themepark:add_table(place_table_definition)
 
  70     insert_row = function(columns)
 
  71         themepark:insert('place', columns, {}, {})
 
  74     local place_table = osm2pgsql.define_table(place_table_definition)
 
  75     insert_row = function(columns)
 
  76         place_table:insert(columns)
 
  80 ------------ Geometry functions for relations ---------------------
 
  82 function module.relation_as_multipolygon(o)
 
  83     return o:as_multipolygon()
 
  86 function module.relation_as_multiline(o)
 
  87     return o:as_multilinestring():line_merge()
 
  91 module.RELATION_TYPES = {
 
  92     multipolygon = module.relation_as_multipolygon,
 
  93     boundary = module.relation_as_multipolygon,
 
  94     waterway = module.relation_as_multiline
 
  97 --------- Built-in place transformation functions --------------------------
 
  99 local PlaceTransform = {}
 
 101 -- Special transform meanings which are interpreted elsewhere
 
 102 PlaceTransform.fallback = 'fallback'
 
 103 PlaceTransform.delete = 'delete'
 
 104 PlaceTransform.extra = 'extra'
 
 106 -- always: unconditionally use that place
 
 107 function PlaceTransform.always(place)
 
 111 -- never: unconditionally drop the place
 
 112 function PlaceTransform.never()
 
 116 -- named: use the place if it has a fully-qualified name
 
 117 function PlaceTransform.named(place)
 
 118     if place.has_name then
 
 123 -- named_with_key: use place if there is a name with the main key prefix
 
 124 function PlaceTransform.named_with_key(place, k)
 
 126     local prefix = k .. ':name'
 
 127     for namek, namev in pairs(place.intags) do
 
 128         if namek:sub(1, #prefix) == prefix
 
 129            and (#namek == #prefix
 
 130                 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
 
 131             names[namek:sub(#k + 2)] = namev
 
 135     if next(names) ~= nil then
 
 136         return place:clone{names=names}
 
 140 ----------------- other helper functions -----------------------------
 
 142 local function lookup_prefilter_classification(k, v)
 
 144     local desc = MAIN_KEYS[k]
 
 145     local fullmatch = desc and (desc[v] or desc[1])
 
 146     if fullmatch ~= nil then
 
 150     for slen, slist in pairs(PRE_FILTER.suffix) do
 
 152             local group = slist[k:sub(-slen)]
 
 159     for slen, slist in pairs(PRE_FILTER.prefix) do
 
 161             local group = slist[k:sub(1, slen)]
 
 170 local function merge_filters_into_main(group, keys, tags)
 
 172         for _, key in pairs(keys) do
 
 173             -- ignore suffix and prefix matches
 
 174             if key:sub(1, 1) ~= '*' and key:sub(#key, #key) ~= '*' then
 
 175                 if MAIN_KEYS[key] == nil then
 
 178                 MAIN_KEYS[key][1] = group
 
 184         for key, values in pairs(tags) do
 
 185             if MAIN_KEYS[key] == nil then
 
 188             for _, v in pairs(values) do
 
 189                 MAIN_KEYS[key][v] = group
 
 196 local function remove_group_from_main(group)
 
 197     for key, values in pairs(MAIN_KEYS) do
 
 198         for _, ttype in pairs(values) do
 
 199             if ttype == group then
 
 203         if next(values) == nil then
 
 210 local function add_pre_filter(data)
 
 211     for group, keys in pairs(data) do
 
 212         for _, key in pairs(keys) do
 
 213             local klen = #key - 1
 
 214             if key:sub(1, 1) == '*' then
 
 216                     if PRE_FILTER.suffix[klen] == nil then
 
 217                         PRE_FILTER.suffix[klen] = {}
 
 219                     PRE_FILTER.suffix[klen][key:sub(2)] = group
 
 221             elseif key:sub(#key, #key) == '*' then
 
 222                 if PRE_FILTER.prefix[klen] == nil then
 
 223                     PRE_FILTER.prefix[klen] = {}
 
 225                 PRE_FILTER.prefix[klen][key:sub(1, klen)] = group
 
 231 ------------- Place class ------------------------------------------
 
 234 Place.__index = Place
 
 236 function Place.new(object, geom_func)
 
 237     local self = setmetatable({}, Place)
 
 239     self.geom_func = geom_func
 
 241     self.admin_level = tonumber(self.object.tags.admin_level or 15) or 15
 
 242     if self.admin_level == nil
 
 243        or self.admin_level <= 0 or self.admin_level > 15
 
 244        or math.floor(self.admin_level) ~= self.admin_level then
 
 245         self.admin_level = 15
 
 249     self.has_name = false
 
 256     local has_main_tags = false
 
 257     for k, v in pairs(self.object.tags) do
 
 258         local group = lookup_prefilter_classification(k, v)
 
 259         if group == 'extra' then
 
 260             self.extratags[k] = v
 
 261         elseif group ~= 'delete' then
 
 269     if not has_main_tags then
 
 270         -- no interesting tags, don't bother processing
 
 277 function Place:clean(data)
 
 278     for k, v in pairs(self.intags) do
 
 279         if data.delete ~= nil and data.delete(k, v) then
 
 281         elseif data.extra ~= nil and data.extra(k, v) then
 
 282             self.extratags[k] = v
 
 288 function Place:delete(data)
 
 289     if data.match ~= nil then
 
 290         for k, v in pairs(self.intags) do
 
 291             if data.match(k, v) then
 
 298 function Place:grab_extratags(data)
 
 301     if data.match ~= nil then
 
 302         for k, v in pairs(self.intags) do
 
 303             if data.match(k, v) then
 
 305                 self.extratags[k] = v
 
 314 local function strip_address_prefix(k)
 
 315     if k:sub(1, 5) == 'addr:' then
 
 319     if k:sub(1, 6) == 'is_in:' then
 
 327 function Place:grab_address_parts(data)
 
 330     if data.groups ~= nil then
 
 331         for k, v in pairs(self.intags) do
 
 332             local atype = data.groups(k, v)
 
 335                 if atype == 'main' then
 
 337                     self.address[strip_address_prefix(k)] = v
 
 339                 elseif atype == 'extra' then
 
 340                     self.address[strip_address_prefix(k)] = v
 
 342                     self.address[atype] = v
 
 353 function Place:grab_name_parts(data)
 
 356     if data.groups ~= nil then
 
 357         for k, v in pairs(self.intags) do
 
 358             local atype = data.groups(k, v)
 
 363                 if atype == 'main' then
 
 365                 elseif atype == 'house' then
 
 367                     fallback = {'place', 'house', PlaceTransform.always}
 
 377 function Place:write_place(k, v, mfunc, save_extra_mains)
 
 378     v = v or self.intags[k]
 
 383     local place = mfunc(self, k, v)
 
 385         local res = place:write_row(k, v, save_extra_mains)
 
 386         self.num_entries = self.num_entries + res
 
 393 function Place:write_row(k, v, save_extra_mains)
 
 394     if self.geometry == nil then
 
 395         self.geometry = self.geom_func(self.object)
 
 397     if self.geometry:is_null() then
 
 401     if save_extra_mains ~= nil then
 
 402         for extra_k, extra_v in pairs(self.intags) do
 
 403             if extra_k ~= k and save_extra_mains(extra_k, extra_v) then
 
 404                 self.extratags[extra_k] = extra_v
 
 412         admin_level = self.admin_level,
 
 413         name = next(self.names) and self.names,
 
 414         address = next(self.address) and self.address,
 
 415         extratags = next(self.extratags) and self.extratags,
 
 416         geometry = self.geometry
 
 419     if save_extra_mains then
 
 420         for tk, tv in pairs(self.intags) do
 
 421             if save_extra_mains(tk, tv) then
 
 422                 self.extratags[tk] = nil
 
 431 function Place:clone(data)
 
 432     local cp = setmetatable({}, Place)
 
 433     cp.object = self.object
 
 434     cp.geometry = data.geometry or self.geometry
 
 435     cp.geom_func = self.geom_func
 
 436     cp.intags = data.intags or self.intags
 
 437     cp.admin_level = data.admin_level or self.admin_level
 
 438     cp.names = data.names or self.names
 
 439     cp.address = data.address or self.address
 
 440     cp.extratags = data.extratags or self.extratags
 
 446 function module.tag_match(data)
 
 447     if data == nil or next(data) == nil then
 
 451     local fullmatches = {}
 
 452     local key_prefixes = {}
 
 453     local key_suffixes = {}
 
 455     if data.keys ~= nil then
 
 456         for _, key in pairs(data.keys) do
 
 457             if key:sub(1, 1) == '*' then
 
 459                     if key_suffixes[#key - 1] == nil then
 
 460                         key_suffixes[#key - 1] = {}
 
 462                     key_suffixes[#key - 1][key:sub(2)] = true
 
 464             elseif key:sub(#key, #key) == '*' then
 
 465                 if key_prefixes[#key - 1] == nil then
 
 466                     key_prefixes[#key - 1] = {}
 
 468                 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
 
 470                 fullmatches[key] = true
 
 475     if data.tags ~= nil then
 
 476         for k, vlist in pairs(data.tags) do
 
 477             if fullmatches[k] == nil then
 
 479                 for _, v in pairs(vlist) do
 
 480                     fullmatches[k][v] = true
 
 486     return function (k, v)
 
 487         if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
 
 491         for slen, slist in pairs(key_suffixes) do
 
 492             if #k >= slen and slist[k:sub(-slen)] ~= nil then
 
 497         for slen, slist in pairs(key_prefixes) do
 
 498             if #k >= slen and slist[k:sub(1, slen)] ~= nil then
 
 508 function module.tag_group(data)
 
 509     if data == nil or next(data) == nil then
 
 513     local fullmatches = {}
 
 514     local key_prefixes = {}
 
 515     local key_suffixes = {}
 
 517     for group, tags in pairs(data) do
 
 518         for _, key in pairs(tags) do
 
 519             if key:sub(1, 1) == '*' then
 
 521                     if key_suffixes[#key - 1] == nil then
 
 522                         key_suffixes[#key - 1] = {}
 
 524                     key_suffixes[#key - 1][key:sub(2)] = group
 
 526             elseif key:sub(#key, #key) == '*' then
 
 527                 if key_prefixes[#key - 1] == nil then
 
 528                     key_prefixes[#key - 1] = {}
 
 530                 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
 
 532                 fullmatches[key] = group
 
 537     return function (k, v)
 
 538         local val = fullmatches[k]
 
 543         for slen, slist in pairs(key_suffixes) do
 
 545                 val = slist[k:sub(-slen)]
 
 552         for slen, slist in pairs(key_prefixes) do
 
 554                 val = slist[k:sub(1, slen)]
 
 563 -- Returns prefix part of the keys, and reject suffix matching keys
 
 564 local function process_key(key)
 
 565     if key:sub(1, 1) == '*' then
 
 568     if key:sub(#key, #key) == '*' then
 
 569         return key:sub(1, #key - 2)
 
 574 -- Process functions for all data types
 
 575 function module.process_node(object)
 
 577     local function geom_func(o)
 
 581     module.process_tags(Place.new(object, geom_func))
 
 584 function module.process_way(object)
 
 586     local function geom_func(o)
 
 587         local geom = o:as_polygon()
 
 589         if geom:is_null() then
 
 590             geom = o:as_linestring()
 
 596     module.process_tags(Place.new(object, geom_func))
 
 599 function module.process_relation(object)
 
 600     local geom_func = module.RELATION_TYPES[object.tags.type]
 
 602     if geom_func ~= nil then
 
 603         module.process_tags(Place.new(object, geom_func))
 
 607 -- The process functions are used by default by osm2pgsql.
 
 609     themepark:add_proc('node', module.process_node)
 
 610     themepark:add_proc('way', module.process_way)
 
 611     themepark:add_proc('relation', module.process_relation)
 
 613     osm2pgsql.process_node = module.process_node
 
 614     osm2pgsql.process_way = module.process_way
 
 615     osm2pgsql.process_relation = module.process_relation
 
 618 function module.process_tags(o)
 
 619     if next(o.intags) == nil then
 
 620         return  -- shortcut when pre-filtering has removed all tags
 
 623     -- Exception for boundary/place double tagging
 
 624     if o.intags.boundary == 'administrative' then
 
 625         o:grab_extratags{match = function (k, v)
 
 626             return k == 'place' and v:sub(1,3) ~= 'isl'
 
 631     local fallback = o:grab_name_parts{groups=NAMES}
 
 634     if o:grab_address_parts{groups=ADDRESS_TAGS} > 0 and fallback == nil then
 
 635         fallback = {'place', 'house', PlaceTransform.always}
 
 637     if o.address.country ~= nil and #o.address.country ~= 2 then
 
 638         o.address['country'] = nil
 
 640     if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
 
 641         fallback = {'place', 'postcode', PlaceTransform.always}
 
 644     if o.address.interpolation ~= nil then
 
 645         o:write_place('place', 'houses', PlaceTransform.always, SAVE_EXTRA_MAINS)
 
 649     o:clean{delete = POST_DELETE}
 
 652     for k, v in pairs(o.intags) do
 
 653         local ktable = MAIN_KEYS[k]
 
 655             local ktype = ktable[v] or ktable[1]
 
 656             if type(ktype) == 'function' then
 
 657                 o:write_place(k, v, ktype, SAVE_EXTRA_MAINS)
 
 658             elseif ktype == 'fallback' and o.has_name then
 
 659                 fallback = {k, v, PlaceTransform.named}
 
 664     if fallback ~= nil and o.num_entries == 0 then
 
 665         o:write_place(fallback[1], fallback[2], fallback[3], SAVE_EXTRA_MAINS)
 
 669 --------- Convenience functions for simple style configuration -----------------
 
 671 function module.set_prefilters(data)
 
 672     remove_group_from_main('delete')
 
 673     merge_filters_into_main('delete', data.delete_keys, data.delete_tags)
 
 675     remove_group_from_main('extra')
 
 676     merge_filters_into_main('extra', data.extra_keys, data.extra_tags)
 
 678     PRE_FILTER = {prefix = {}, suffix = {}}
 
 679     add_pre_filter{delete = data.delete_keys, extra = data.extra_keys}
 
 683 function module.ignore_tags(data)
 
 684     merge_filters_into_main('delete', data)
 
 685     add_pre_filter{delete = data}
 
 689 function module.add_for_extratags(data)
 
 690     merge_filters_into_main('extra', data)
 
 691     add_pre_filter{extra = data}
 
 695 function module.set_main_tags(data)
 
 696     for key, values in pairs(MAIN_KEYS) do
 
 697         for _, ttype in pairs(values) do
 
 698             if ttype == 'fallback' or type(ttype) == 'function' then
 
 702         if next(values) == nil then
 
 706     module.add_main_tags(data)
 
 710 function module.add_main_tags(data)
 
 711     for k, v in pairs(data) do
 
 712         if MAIN_KEYS[k] == nil then
 
 715         if type(v) == 'function' then
 
 717         elseif type(v) == 'string' then
 
 718             MAIN_KEYS[k][1] = PlaceTransform[v]
 
 719         elseif type(v) == 'table' then
 
 720             for subk, subv in pairs(v) do
 
 721                 if type(subv) == 'function' then
 
 722                     MAIN_KEYS[k][subk] = subv
 
 724                     MAIN_KEYS[k][subk] = PlaceTransform[subv]
 
 732 function module.set_name_tags(data)
 
 733     NAMES = module.tag_group(data)
 
 735     for _, lst in pairs(data) do
 
 736         for _, k in ipairs(lst) do
 
 737             local key = process_key(k)
 
 739                 module.TAGINFO_NAME_KEYS[key] = true
 
 743     remove_group_from_main('fallback:name')
 
 744     merge_filters_into_main('fallback:name', data.house)
 
 748 function module.set_address_tags(data)
 
 749     if data.postcode_fallback ~= nil then
 
 750         POSTCODE_FALLBACK = data.postcode_fallback
 
 751         data.postcode_fallback = nil
 
 753     ADDRESS_TAGS = module.tag_group(data)
 
 755     for _, lst in pairs(data) do
 
 757             for _, k in ipairs(lst) do
 
 758                 local key = process_key(k)
 
 760                     module.TAGINFO_ADDRESS_KEYS[key] = true
 
 766     remove_group_from_main('fallback:address')
 
 767     remove_group_from_main('fallback:postcode')
 
 768     merge_filters_into_main('fallback:address', data.main)
 
 769     if POSTCODE_FALLBACK then
 
 770         merge_filters_into_main('fallback:postcode', data.postcode)
 
 772     merge_filters_into_main('fallback:address', data.interpolation)
 
 776 function module.set_unused_handling(data)
 
 777     if data.extra_keys == nil and data.extra_tags == nil then
 
 778         POST_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
 
 779         SAVE_EXTRA_MAINS = function() return true end
 
 780     elseif data.delete_keys == nil and data.delete_tags == nil then
 
 782         SAVE_EXTRA_MAINS = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
 
 784         error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
 
 788 function module.set_relation_types(data)
 
 789     module.RELATION_TYPES = {}
 
 791         if v == 'multipolygon' then
 
 792             module.RELATION_TYPES[k] = module.relation_as_multipolygon
 
 793         elseif v == 'multiline' then
 
 794             module.RELATION_TYPES[k] = module.relation_as_multiline