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 MAIN_KEYS = {admin_level = {'delete'}}
 
  26 local PRE_FILTER = {prefix = {}, suffix = {}}
 
  28 local NAME_FILTER = nil
 
  29 local ADDRESS_TAGS = {}
 
  30 local ADDRESS_FILTER = nil
 
  31 local EXTRATAGS_FILTER
 
  32 local POSTCODE_FALLBACK = true
 
  34 -- This file can also be directly require'd instead of running it under
 
  35 -- the themepark framework. In that case the first parameter is usually
 
  36 -- the module name. Lets check for that, so that further down we can call
 
  37 -- the low-level osm2pgsql functions instead of themepark functions.
 
  39 if type(themepark) ~= 'table' then
 
  43 -- The single place table.
 
  44 local place_table_definition = {
 
  46     ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
 
  48         { column = 'class', type = 'text', not_null = true },
 
  49         { column = 'type', type = 'text', not_null = true },
 
  50         { column = 'admin_level', type = 'smallint' },
 
  51         { column = 'name', type = 'hstore' },
 
  52         { column = 'address', type = 'hstore' },
 
  53         { column = 'extratags', type = 'hstore' },
 
  54         { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true },
 
  56     data_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_DATA"),
 
  57     index_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_INDEX"),
 
  62 local script_path = debug.getinfo(1, "S").source:match("@?(.*/)")
 
  63 local PRESETS = loadfile(script_path .. 'presets.lua')()
 
  66     themepark:add_table(place_table_definition)
 
  67     insert_row = function(columns)
 
  68         themepark:insert('place', columns, {}, {})
 
  71     local place_table = osm2pgsql.define_table(place_table_definition)
 
  72     insert_row = function(columns)
 
  73         place_table:insert(columns)
 
  77 ------------ Geometry functions for relations ---------------------
 
  79 function module.relation_as_multipolygon(o)
 
  80     return o:as_multipolygon()
 
  83 function module.relation_as_multiline(o)
 
  84     return o:as_multilinestring():line_merge()
 
  88 module.RELATION_TYPES = {
 
  89     multipolygon = module.relation_as_multipolygon,
 
  90     boundary = module.relation_as_multipolygon,
 
  91     waterway = module.relation_as_multiline
 
  94 --------- Built-in place transformation functions --------------------------
 
  96 local PlaceTransform = {}
 
  98 -- Special transform meanings which are interpreted elsewhere
 
  99 PlaceTransform.fallback = 'fallback'
 
 100 PlaceTransform.delete = 'delete'
 
 101 PlaceTransform.extra = 'extra'
 
 103 -- always: unconditionally use that place
 
 104 function PlaceTransform.always(place)
 
 108 -- never: unconditionally drop the place
 
 109 function PlaceTransform.never()
 
 113 -- named: use the place if it has a fully-qualified name
 
 114 function PlaceTransform.named(place)
 
 115     if place.has_name then
 
 120 -- named_with_key: use place if there is a name with the main key prefix
 
 121 function PlaceTransform.named_with_key(place, k)
 
 123     local prefix = k .. ':name'
 
 124     for namek, namev in pairs(place.intags) do
 
 125         if namek:sub(1, #prefix) == prefix
 
 126            and (#namek == #prefix
 
 127                 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
 
 128             names[namek:sub(#k + 2)] = namev
 
 132     if next(names) ~= nil then
 
 133         return place:clone{names=names}
 
 137 -- Special transform used with address fallbacks: ignore all names
 
 138 -- except for those marked as being part of the address.
 
 139 local function address_fallback(place)
 
 140     if next(place.names) == nil or NAMES.house == nil then
 
 145     for k, v in pairs(place.names) do
 
 146         if NAME_FILTER(k, v) == 'house' then
 
 150     return place:clone{names=names}
 
 153 --------- Built-in extratags transformation functions ---------------
 
 155 local function default_extratags_filter(p, k)
 
 156     -- Default handling is to copy over place tag for boundaries.
 
 157     -- Nominatim needs this.
 
 158     if k ~= 'boundary' or p.intags.place == nil then
 
 162     local extra = { place = p.intags.place }
 
 163     for kin, vin in pairs(p.extratags) do
 
 169 EXTRATAGS_FILTER = default_extratags_filter
 
 171 ----------------- other helper functions -----------------------------
 
 173 local function lookup_prefilter_classification(k, v)
 
 175     local desc = MAIN_KEYS[k]
 
 176     local fullmatch = desc and (desc[v] or desc[1])
 
 177     if fullmatch ~= nil then
 
 181     for slen, slist in pairs(PRE_FILTER.suffix) do
 
 183             local group = slist[k:sub(-slen)]
 
 190     for slen, slist in pairs(PRE_FILTER.prefix) do
 
 192             local group = slist[k:sub(1, slen)]
 
 201 local function merge_filters_into_main(group, keys, tags)
 
 203         for _, key in pairs(keys) do
 
 204             -- ignore suffix and prefix matches
 
 205             if key:sub(1, 1) ~= '*' and key:sub(#key, #key) ~= '*' then
 
 206                 if MAIN_KEYS[key] == nil then
 
 209                 MAIN_KEYS[key][1] = group
 
 215         for key, values in pairs(tags) do
 
 216             if MAIN_KEYS[key] == nil then
 
 219             for _, v in pairs(values) do
 
 220                 MAIN_KEYS[key][v] = group
 
 227 local function remove_group_from_main(group)
 
 228     for key, values in pairs(MAIN_KEYS) do
 
 229         for _, ttype in pairs(values) do
 
 230             if ttype == group then
 
 234         if next(values) == nil then
 
 241 local function add_pre_filter(data)
 
 242     for group, keys in pairs(data) do
 
 243         for _, key in pairs(keys) do
 
 244             local klen = #key - 1
 
 245             if key:sub(1, 1) == '*' then
 
 247                     if PRE_FILTER.suffix[klen] == nil then
 
 248                         PRE_FILTER.suffix[klen] = {}
 
 250                     PRE_FILTER.suffix[klen][key:sub(2)] = group
 
 252             elseif key:sub(#key, #key) == '*' then
 
 253                 if PRE_FILTER.prefix[klen] == nil then
 
 254                     PRE_FILTER.prefix[klen] = {}
 
 256                 PRE_FILTER.prefix[klen][key:sub(1, klen)] = group
 
 262 ------------- Place class ------------------------------------------
 
 265 Place.__index = Place
 
 267 function Place.new(object, geom_func)
 
 268     local self = setmetatable({}, Place)
 
 270     self.geom_func = geom_func
 
 272     self.admin_level = tonumber(self.object.tags.admin_level or 15) or 15
 
 273     if self.admin_level == nil
 
 274        or self.admin_level <= 0 or self.admin_level > 15
 
 275        or math.floor(self.admin_level) ~= self.admin_level then
 
 276         self.admin_level = 15
 
 280     self.has_name = false
 
 287     local has_main_tags = false
 
 288     for k, v in pairs(self.object.tags) do
 
 289         local group = lookup_prefilter_classification(k, v)
 
 290         if group == 'extra' then
 
 291             self.extratags[k] = v
 
 292         elseif group ~= 'delete' then
 
 300     if not has_main_tags then
 
 301         -- no interesting tags, don't bother processing
 
 308 function Place:clean(data)
 
 309     for k, v in pairs(self.intags) do
 
 310         if data.delete ~= nil and data.delete(k, v) then
 
 312         elseif data.extra ~= nil and data.extra(k, v) then
 
 313             self.extratags[k] = v
 
 319 function Place:delete(data)
 
 320     if data.match ~= nil then
 
 321         for k, v in pairs(self.intags) do
 
 322             if data.match(k, v) then
 
 329 function Place:grab_extratags(data)
 
 332     if data.match ~= nil then
 
 333         for k, v in pairs(self.intags) do
 
 334             if data.match(k, v) then
 
 336                 self.extratags[k] = v
 
 345 local function strip_address_prefix(k)
 
 346     if k:sub(1, 5) == 'addr:' then
 
 350     if k:sub(1, 6) == 'is_in:' then
 
 358 function Place:grab_address_parts(data)
 
 361     if data.groups ~= nil then
 
 362         for k, v in pairs(self.intags) do
 
 363             local atype = data.groups(k, v)
 
 366                 if atype == 'main' then
 
 368                     self.address[strip_address_prefix(k)] = v
 
 370                 elseif atype == 'extra' then
 
 371                     self.address[strip_address_prefix(k)] = v
 
 373                     self.address[atype] = v
 
 384 function Place:grab_name_parts(data)
 
 387     if data.groups ~= nil then
 
 388         for k, v in pairs(self.intags) do
 
 389             local atype = data.groups(k, v)
 
 394                 if atype == 'main' then
 
 396                 elseif atype == 'house' then
 
 398                     fallback = {'place', 'house', address_fallback}
 
 408 function Place:write_place(k, v, mfunc)
 
 409     v = v or self.intags[k]
 
 414     local place = mfunc(self, k, v)
 
 416         local res = place:write_row(k, v)
 
 417         self.num_entries = self.num_entries + res
 
 424 function Place:write_row(k, v)
 
 425     if self.geometry == nil then
 
 426         self.geometry = self.geom_func(self.object)
 
 428     if self.geometry == nil or self.geometry:is_null() then
 
 432     local extratags = EXTRATAGS_FILTER(self, k, v)
 
 433     if not (extratags and next(extratags)) then
 
 440         admin_level = self.admin_level,
 
 441         name = next(self.names) and self.names,
 
 442         address = next(self.address) and self.address,
 
 443         extratags = extratags,
 
 444         geometry = self.geometry
 
 451 function Place:clone(data)
 
 452     local cp = setmetatable({}, Place)
 
 453     cp.object = self.object
 
 454     cp.geometry = data.geometry or self.geometry
 
 455     cp.geom_func = self.geom_func
 
 456     cp.intags = data.intags or self.intags
 
 457     cp.admin_level = data.admin_level or self.admin_level
 
 458     cp.names = data.names or self.names
 
 459     cp.address = data.address or self.address
 
 460     cp.extratags = data.extratags or self.extratags
 
 466 function module.tag_match(data)
 
 467     if data == nil or next(data) == nil then
 
 471     local fullmatches = {}
 
 472     local key_prefixes = {}
 
 473     local key_suffixes = {}
 
 475     if data.keys ~= nil then
 
 476         for _, key in pairs(data.keys) do
 
 477             if key:sub(1, 1) == '*' then
 
 479                     if key_suffixes[#key - 1] == nil then
 
 480                         key_suffixes[#key - 1] = {}
 
 482                     key_suffixes[#key - 1][key:sub(2)] = true
 
 484             elseif key:sub(#key, #key) == '*' then
 
 485                 if key_prefixes[#key - 1] == nil then
 
 486                     key_prefixes[#key - 1] = {}
 
 488                 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
 
 490                 fullmatches[key] = true
 
 495     if data.tags ~= nil then
 
 496         for k, vlist in pairs(data.tags) do
 
 497             if fullmatches[k] == nil then
 
 499                 for _, v in pairs(vlist) do
 
 500                     fullmatches[k][v] = true
 
 506     return function (k, v)
 
 507         if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
 
 511         for slen, slist in pairs(key_suffixes) do
 
 512             if #k >= slen and slist[k:sub(-slen)] ~= nil then
 
 517         for slen, slist in pairs(key_prefixes) do
 
 518             if #k >= slen and slist[k:sub(1, slen)] ~= nil then
 
 528 function module.tag_group(data)
 
 529     if data == nil or next(data) == nil then
 
 533     local fullmatches = {}
 
 534     local key_prefixes = {}
 
 535     local key_suffixes = {}
 
 537     for group, tags in pairs(data) do
 
 538         for _, key in pairs(tags) do
 
 539             if key:sub(1, 1) == '*' then
 
 541                     if key_suffixes[#key - 1] == nil then
 
 542                         key_suffixes[#key - 1] = {}
 
 544                     key_suffixes[#key - 1][key:sub(2)] = group
 
 546             elseif key:sub(#key, #key) == '*' then
 
 547                 if key_prefixes[#key - 1] == nil then
 
 548                     key_prefixes[#key - 1] = {}
 
 550                 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
 
 552                 fullmatches[key] = group
 
 558         local val = fullmatches[k]
 
 563         for slen, slist in pairs(key_suffixes) do
 
 565                 val = slist[k:sub(-slen)]
 
 572         for slen, slist in pairs(key_prefixes) do
 
 574                 val = slist[k:sub(1, slen)]
 
 583 -- Returns prefix part of the keys, and reject suffix matching keys
 
 584 local function process_key(key)
 
 585     if key:sub(1, 1) == '*' then
 
 588     if key:sub(#key, #key) == '*' then
 
 589         return key:sub(1, #key - 2)
 
 594 -- Process functions for all data types
 
 595 function module.process_node(object)
 
 597     local function geom_func(o)
 
 601     module.process_tags(Place.new(object, geom_func))
 
 604 function module.process_way(object)
 
 606     local function geom_func(o)
 
 607         local geom = o:as_polygon()
 
 609         if geom:is_null() then
 
 610             geom = o:as_linestring()
 
 611             if geom:is_null() or geom:length() > 30 then
 
 619     module.process_tags(Place.new(object, geom_func))
 
 622 function module.process_relation(object)
 
 623     local geom_func = module.RELATION_TYPES[object.tags.type]
 
 625     if geom_func ~= nil then
 
 626         module.process_tags(Place.new(object, geom_func))
 
 630 -- The process functions are used by default by osm2pgsql.
 
 632     themepark:add_proc('node', module.process_node)
 
 633     themepark:add_proc('way', module.process_way)
 
 634     themepark:add_proc('relation', module.process_relation)
 
 636     osm2pgsql.process_node = module.process_node
 
 637     osm2pgsql.process_way = module.process_way
 
 638     osm2pgsql.process_relation = module.process_relation
 
 641 function module.process_tags(o)
 
 642     if next(o.intags) == nil then
 
 643         return  -- shortcut when pre-filtering has removed all tags
 
 646     -- Exception for boundary/place double tagging
 
 647     if o.intags.boundary == 'administrative' then
 
 648         o:grab_extratags{match = function (k, v)
 
 649             return k == 'place' and v:sub(1,3) ~= 'isl'
 
 654     local fallback = o:grab_name_parts{groups=NAME_FILTER}
 
 657     if o:grab_address_parts{groups=ADDRESS_FILTER} > 0 and fallback == nil then
 
 658         fallback = {'place', 'house', address_fallback}
 
 660     if o.address.country ~= nil and #o.address.country ~= 2 then
 
 661         o.address['country'] = nil
 
 663     if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
 
 664         fallback = {'place', 'postcode', PlaceTransform.always}
 
 667     if o.address.interpolation ~= nil then
 
 668         o:write_place('place', 'houses', PlaceTransform.always)
 
 673     for k, v in pairs(o.intags) do
 
 674         local ktable = MAIN_KEYS[k]
 
 676             local ktype = ktable[v] or ktable[1]
 
 677             if type(ktype) == 'function' then
 
 678                 o:write_place(k, v, ktype)
 
 679             elseif ktype == 'fallback' and o.has_name then
 
 680                 fallback = {k, v, PlaceTransform.named}
 
 685     if fallback ~= nil and o.num_entries == 0 then
 
 686         o:write_place(fallback[1], fallback[2], fallback[3])
 
 690 --------- Convenience functions for simple style configuration -----------------
 
 692 function module.set_prefilters(data)
 
 693     remove_group_from_main('delete')
 
 694     merge_filters_into_main('delete', data.delete_keys, data.delete_tags)
 
 696     remove_group_from_main('extra')
 
 697     merge_filters_into_main('extra', data.extra_keys, data.extra_tags)
 
 699     PRE_FILTER = {prefix = {}, suffix = {}}
 
 700     add_pre_filter{delete = data.delete_keys, extra = data.extra_keys}
 
 704 function module.ignore_keys(data)
 
 705     if type(data) == 'string' then
 
 707         data = PRESETS.IGNORE_KEYS[data]
 
 709             error('Unknown preset for ignored keys: ' .. preset)
 
 712     merge_filters_into_main('delete', data)
 
 713     add_pre_filter{delete = data}
 
 717 function module.add_for_extratags(data)
 
 718     if type(data) == 'string' then
 
 720         data = PRESETS.EXTRATAGS[data] or PRESETS.IGNORE_KEYS[data]
 
 722             error('Unknown preset for extratags: ' .. preset)
 
 725     merge_filters_into_main('extra', data)
 
 726     add_pre_filter{extra = data}
 
 730 function module.set_main_tags(data)
 
 731     for key, values in pairs(MAIN_KEYS) do
 
 732         for _, ttype in pairs(values) do
 
 733             if ttype == 'fallback' or type(ttype) == 'function' then
 
 737         if next(values) == nil then
 
 741     module.modify_main_tags(data)
 
 745 function module.modify_main_tags(data)
 
 746     if type(data) == 'string' then
 
 748         if data:sub(1, 7) == 'street/' then
 
 749             data = PRESETS.MAIN_TAGS_STREETS[data:sub(8)]
 
 750         elseif data:sub(1, 4) == 'poi/' then
 
 751             data = PRESETS.MAIN_TAGS_POIS(data:sub(5))
 
 753             data = PRESETS.MAIN_TAGS[data]
 
 756             error('Unknown preset for main tags: ' .. preset)
 
 760     for k, v in pairs(data) do
 
 761         if MAIN_KEYS[k] == nil then
 
 764         if type(v) == 'function' then
 
 766         elseif type(v) == 'string' then
 
 767             MAIN_KEYS[k][1] = PlaceTransform[v]
 
 768         elseif type(v) == 'table' then
 
 769             for subk, subv in pairs(v) do
 
 770                 if type(subv) == 'function' then
 
 771                     MAIN_KEYS[k][subk] = subv
 
 773                     MAIN_KEYS[k][subk] = PlaceTransform[subv]
 
 781 function module.modify_name_tags(data)
 
 782     if type(data) == 'string' then
 
 784         data = PRESETS.NAME_TAGS[data]
 
 786             error('Unknown preset for name keys: ' .. preset)
 
 790     for k,v in pairs(data) do
 
 797     NAME_FILTER = module.tag_group(NAMES)
 
 798     remove_group_from_main('fallback:name')
 
 799     if data.house ~= nil then
 
 800         merge_filters_into_main('fallback:name', data.house)
 
 805 function module.set_name_tags(data)
 
 807     module.modify_name_tags(data)
 
 811 function module.set_address_tags(data)
 
 813     module.modify_address_tags(data)
 
 817 function module.modify_address_tags(data)
 
 818     if type(data) == 'string' then
 
 820         data = PRESETS.ADDRESS_TAGS[data]
 
 822             error('Unknown preset for address keys: ' .. preset)
 
 826     for k, v in pairs(data) do
 
 827         if k == 'postcode_fallback' then
 
 828             POSTCODE_FALLBACK = v
 
 829         elseif next(v) == nil then
 
 830             ADDRESS_TAGS[k] = nil
 
 836     ADDRESS_FILTER = module.tag_group(ADDRESS_TAGS)
 
 838     remove_group_from_main('fallback:address')
 
 839     merge_filters_into_main('fallback:address', data.main)
 
 840     merge_filters_into_main('fallback:address', data.interpolation)
 
 841     remove_group_from_main('fallback:postcode')
 
 842     if POSTCODE_FALLBACK then
 
 843         merge_filters_into_main('fallback:postcode', data.postcode)
 
 848 function module.set_address_tags(data)
 
 849     ADDRESS_TAGS_SOURCE = {}
 
 850     module.modify_address_tags(data)
 
 854 function module.set_postcode_fallback(enable)
 
 855     if POSTCODE_FALLBACK ~= enable then
 
 856         remove_group_from_main('fallback:postcode')
 
 858             merge_filters_into_main('fallback:postcode', ADDRESS_TAGS.postcode)
 
 861     POSTCODE_FALLBACK = enable
 
 865 function module.set_unused_handling(data)
 
 866     if type(data) == 'function' then
 
 867         EXTRATAGS_FILTER = data
 
 868     elseif data == nil then
 
 869         EXTRATAGS_FILTER = default_extratags_filter
 
 870     elseif data.extra_keys == nil and data.extra_tags == nil then
 
 871         local delfilter = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
 
 872         EXTRATAGS_FILTER = function (p, k)
 
 874             for kin, vin in pairs(p.intags) do
 
 875                 if kin ~= k and not delfilter(kin, vin) then
 
 879             if next(extra) == nil then
 
 882             for kextra, vextra in pairs(p.extratags) do
 
 883                 extra[kextra] = vextra
 
 887     elseif data.delete_keys == nil and data.delete_tags == nil then
 
 888         local incfilter = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
 
 889         EXTRATAGS_FILTER = function (p, k)
 
 891             for kin, vin in pairs(p.intags) do
 
 892                 if kin ~= k and incfilter(kin, vin) then
 
 896             if next(extra) == nil then
 
 899             for kextra, vextra in pairs(p.extratags) do
 
 900                 extra[kextra] = vextra
 
 905         error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
 
 909 function module.set_relation_types(data)
 
 910     module.RELATION_TYPES = {}
 
 912         if v == 'multipolygon' then
 
 913             module.RELATION_TYPES[k] = module.relation_as_multipolygon
 
 914         elseif v == 'multiline' then
 
 915             module.RELATION_TYPES[k] = module.relation_as_multiline
 
 921 function module.get_taginfo()
 
 922     return {main = MAIN_KEYS, name = NAMES, address = ADDRESS_TAGS}