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 -- tables required for taginfo
44 module.TAGINFO_MAIN = {keys = {}, delete_tags = {}}
45 module.TAGINFO_NAME_KEYS = {}
46 module.TAGINFO_ADDRESS_KEYS = {}
49 -- The single place table.
50 local place_table_definition = {
52 ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
54 { column = 'class', type = 'text', not_null = true },
55 { column = 'type', type = 'text', not_null = true },
56 { column = 'admin_level', type = 'smallint' },
57 { column = 'name', type = 'hstore' },
58 { column = 'address', type = 'hstore' },
59 { column = 'extratags', type = 'hstore' },
60 { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true },
62 data_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_DATA"),
63 index_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_INDEX"),
68 local script_path = debug.getinfo(1, "S").source:match("@?(.*/)")
69 local PRESETS = loadfile(script_path .. 'presets.lua')()
72 themepark:add_table(place_table_definition)
73 insert_row = function(columns)
74 themepark:insert('place', columns, {}, {})
77 local place_table = osm2pgsql.define_table(place_table_definition)
78 insert_row = function(columns)
79 place_table:insert(columns)
83 ------------ Geometry functions for relations ---------------------
85 function module.relation_as_multipolygon(o)
86 return o:as_multipolygon()
89 function module.relation_as_multiline(o)
90 return o:as_multilinestring():line_merge()
94 module.RELATION_TYPES = {
95 multipolygon = module.relation_as_multipolygon,
96 boundary = module.relation_as_multipolygon,
97 waterway = module.relation_as_multiline
100 --------- Built-in place transformation functions --------------------------
102 local PlaceTransform = {}
104 -- Special transform meanings which are interpreted elsewhere
105 PlaceTransform.fallback = 'fallback'
106 PlaceTransform.delete = 'delete'
107 PlaceTransform.extra = 'extra'
109 -- always: unconditionally use that place
110 function PlaceTransform.always(place)
114 -- never: unconditionally drop the place
115 function PlaceTransform.never()
119 -- named: use the place if it has a fully-qualified name
120 function PlaceTransform.named(place)
121 if place.has_name then
126 -- named_with_key: use place if there is a name with the main key prefix
127 function PlaceTransform.named_with_key(place, k)
129 local prefix = k .. ':name'
130 for namek, namev in pairs(place.intags) do
131 if namek:sub(1, #prefix) == prefix
132 and (#namek == #prefix
133 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
134 names[namek:sub(#k + 2)] = namev
138 if next(names) ~= nil then
139 return place:clone{names=names}
143 --------- Built-in extratags transformation functions ---------------
145 local function default_extratags_filter(p, k)
146 -- Default handling is to copy over place tag for boundaries.
147 -- Nominatim needs this.
148 if k ~= 'boundary' or p.intags.place == nil then
152 local extra = { place = p.intags.place }
153 for kin, vin in pairs(p.extratags) do
159 EXTRATAGS_FILTER = default_extratags_filter
161 ----------------- other helper functions -----------------------------
163 local function lookup_prefilter_classification(k, v)
165 local desc = MAIN_KEYS[k]
166 local fullmatch = desc and (desc[v] or desc[1])
167 if fullmatch ~= nil then
171 for slen, slist in pairs(PRE_FILTER.suffix) do
173 local group = slist[k:sub(-slen)]
180 for slen, slist in pairs(PRE_FILTER.prefix) do
182 local group = slist[k:sub(1, slen)]
191 local function merge_filters_into_main(group, keys, tags)
193 for _, key in pairs(keys) do
194 -- ignore suffix and prefix matches
195 if key:sub(1, 1) ~= '*' and key:sub(#key, #key) ~= '*' then
196 if MAIN_KEYS[key] == nil then
199 MAIN_KEYS[key][1] = group
205 for key, values in pairs(tags) do
206 if MAIN_KEYS[key] == nil then
209 for _, v in pairs(values) do
210 MAIN_KEYS[key][v] = group
217 local function remove_group_from_main(group)
218 for key, values in pairs(MAIN_KEYS) do
219 for _, ttype in pairs(values) do
220 if ttype == group then
224 if next(values) == nil then
231 local function add_pre_filter(data)
232 for group, keys in pairs(data) do
233 for _, key in pairs(keys) do
234 local klen = #key - 1
235 if key:sub(1, 1) == '*' then
237 if PRE_FILTER.suffix[klen] == nil then
238 PRE_FILTER.suffix[klen] = {}
240 PRE_FILTER.suffix[klen][key:sub(2)] = group
242 elseif key:sub(#key, #key) == '*' then
243 if PRE_FILTER.prefix[klen] == nil then
244 PRE_FILTER.prefix[klen] = {}
246 PRE_FILTER.prefix[klen][key:sub(1, klen)] = group
252 ------------- Place class ------------------------------------------
255 Place.__index = Place
257 function Place.new(object, geom_func)
258 local self = setmetatable({}, Place)
260 self.geom_func = geom_func
262 self.admin_level = tonumber(self.object.tags.admin_level or 15) or 15
263 if self.admin_level == nil
264 or self.admin_level <= 0 or self.admin_level > 15
265 or math.floor(self.admin_level) ~= self.admin_level then
266 self.admin_level = 15
270 self.has_name = false
277 local has_main_tags = false
278 for k, v in pairs(self.object.tags) do
279 local group = lookup_prefilter_classification(k, v)
280 if group == 'extra' then
281 self.extratags[k] = v
282 elseif group ~= 'delete' then
290 if not has_main_tags then
291 -- no interesting tags, don't bother processing
298 function Place:clean(data)
299 for k, v in pairs(self.intags) do
300 if data.delete ~= nil and data.delete(k, v) then
302 elseif data.extra ~= nil and data.extra(k, v) then
303 self.extratags[k] = v
309 function Place:delete(data)
310 if data.match ~= nil then
311 for k, v in pairs(self.intags) do
312 if data.match(k, v) then
319 function Place:grab_extratags(data)
322 if data.match ~= nil then
323 for k, v in pairs(self.intags) do
324 if data.match(k, v) then
326 self.extratags[k] = v
335 local function strip_address_prefix(k)
336 if k:sub(1, 5) == 'addr:' then
340 if k:sub(1, 6) == 'is_in:' then
348 function Place:grab_address_parts(data)
351 if data.groups ~= nil then
352 for k, v in pairs(self.intags) do
353 local atype = data.groups(k, v)
356 if atype == 'main' then
358 self.address[strip_address_prefix(k)] = v
360 elseif atype == 'extra' then
361 self.address[strip_address_prefix(k)] = v
363 self.address[atype] = v
374 function Place:grab_name_parts(data)
377 if data.groups ~= nil then
378 for k, v in pairs(self.intags) do
379 local atype = data.groups(k, v)
384 if atype == 'main' then
386 elseif atype == 'house' then
388 fallback = {'place', 'house', PlaceTransform.always}
398 function Place:write_place(k, v, mfunc)
399 v = v or self.intags[k]
404 local place = mfunc(self, k, v)
406 local res = place:write_row(k, v)
407 self.num_entries = self.num_entries + res
414 function Place:write_row(k, v)
415 if self.geometry == nil then
416 self.geometry = self.geom_func(self.object)
418 if self.geometry:is_null() then
422 local extratags = EXTRATAGS_FILTER(self, k, v)
423 if not (extratags and next(extratags)) then
430 admin_level = self.admin_level,
431 name = next(self.names) and self.names,
432 address = next(self.address) and self.address,
433 extratags = extratags,
434 geometry = self.geometry
441 function Place:clone(data)
442 local cp = setmetatable({}, Place)
443 cp.object = self.object
444 cp.geometry = data.geometry or self.geometry
445 cp.geom_func = self.geom_func
446 cp.intags = data.intags or self.intags
447 cp.admin_level = data.admin_level or self.admin_level
448 cp.names = data.names or self.names
449 cp.address = data.address or self.address
450 cp.extratags = data.extratags or self.extratags
456 function module.tag_match(data)
457 if data == nil or next(data) == nil then
461 local fullmatches = {}
462 local key_prefixes = {}
463 local key_suffixes = {}
465 if data.keys ~= nil then
466 for _, key in pairs(data.keys) do
467 if key:sub(1, 1) == '*' then
469 if key_suffixes[#key - 1] == nil then
470 key_suffixes[#key - 1] = {}
472 key_suffixes[#key - 1][key:sub(2)] = true
474 elseif key:sub(#key, #key) == '*' then
475 if key_prefixes[#key - 1] == nil then
476 key_prefixes[#key - 1] = {}
478 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
480 fullmatches[key] = true
485 if data.tags ~= nil then
486 for k, vlist in pairs(data.tags) do
487 if fullmatches[k] == nil then
489 for _, v in pairs(vlist) do
490 fullmatches[k][v] = true
496 return function (k, v)
497 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
501 for slen, slist in pairs(key_suffixes) do
502 if #k >= slen and slist[k:sub(-slen)] ~= nil then
507 for slen, slist in pairs(key_prefixes) do
508 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
518 function module.tag_group(data)
519 if data == nil or next(data) == nil then
523 local fullmatches = {}
524 local key_prefixes = {}
525 local key_suffixes = {}
527 for group, tags in pairs(data) do
528 for _, key in pairs(tags) do
529 if key:sub(1, 1) == '*' then
531 if key_suffixes[#key - 1] == nil then
532 key_suffixes[#key - 1] = {}
534 key_suffixes[#key - 1][key:sub(2)] = group
536 elseif key:sub(#key, #key) == '*' then
537 if key_prefixes[#key - 1] == nil then
538 key_prefixes[#key - 1] = {}
540 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
542 fullmatches[key] = group
548 local val = fullmatches[k]
553 for slen, slist in pairs(key_suffixes) do
555 val = slist[k:sub(-slen)]
562 for slen, slist in pairs(key_prefixes) do
564 val = slist[k:sub(1, slen)]
573 -- Returns prefix part of the keys, and reject suffix matching keys
574 local function process_key(key)
575 if key:sub(1, 1) == '*' then
578 if key:sub(#key, #key) == '*' then
579 return key:sub(1, #key - 2)
584 -- Process functions for all data types
585 function module.process_node(object)
587 local function geom_func(o)
591 module.process_tags(Place.new(object, geom_func))
594 function module.process_way(object)
596 local function geom_func(o)
597 local geom = o:as_polygon()
599 if geom:is_null() then
600 geom = o:as_linestring()
606 module.process_tags(Place.new(object, geom_func))
609 function module.process_relation(object)
610 local geom_func = module.RELATION_TYPES[object.tags.type]
612 if geom_func ~= nil then
613 module.process_tags(Place.new(object, geom_func))
617 -- The process functions are used by default by osm2pgsql.
619 themepark:add_proc('node', module.process_node)
620 themepark:add_proc('way', module.process_way)
621 themepark:add_proc('relation', module.process_relation)
623 osm2pgsql.process_node = module.process_node
624 osm2pgsql.process_way = module.process_way
625 osm2pgsql.process_relation = module.process_relation
628 function module.process_tags(o)
629 if next(o.intags) == nil then
630 return -- shortcut when pre-filtering has removed all tags
633 -- Exception for boundary/place double tagging
634 if o.intags.boundary == 'administrative' then
635 o:grab_extratags{match = function (k, v)
636 return k == 'place' and v:sub(1,3) ~= 'isl'
641 local fallback = o:grab_name_parts{groups=NAME_FILTER}
644 if o:grab_address_parts{groups=ADDRESS_FILTER} > 0 and fallback == nil then
645 fallback = {'place', 'house', PlaceTransform.always}
647 if o.address.country ~= nil and #o.address.country ~= 2 then
648 o.address['country'] = nil
650 if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
651 fallback = {'place', 'postcode', PlaceTransform.always}
654 if o.address.interpolation ~= nil then
655 o:write_place('place', 'houses', PlaceTransform.always)
660 for k, v in pairs(o.intags) do
661 local ktable = MAIN_KEYS[k]
663 local ktype = ktable[v] or ktable[1]
664 if type(ktype) == 'function' then
665 o:write_place(k, v, ktype)
666 elseif ktype == 'fallback' and o.has_name then
667 fallback = {k, v, PlaceTransform.named}
672 if fallback ~= nil and o.num_entries == 0 then
673 o:write_place(fallback[1], fallback[2], fallback[3])
677 --------- Convenience functions for simple style configuration -----------------
679 function module.set_prefilters(data)
680 remove_group_from_main('delete')
681 merge_filters_into_main('delete', data.delete_keys, data.delete_tags)
683 remove_group_from_main('extra')
684 merge_filters_into_main('extra', data.extra_keys, data.extra_tags)
686 PRE_FILTER = {prefix = {}, suffix = {}}
687 add_pre_filter{delete = data.delete_keys, extra = data.extra_keys}
691 function module.ignore_keys(data)
692 if type(data) == 'string' then
694 data = PRESETS.IGNORE_KEYS[data]
696 error('Unknown preset for ignored keys: ' .. preset)
699 merge_filters_into_main('delete', data)
700 add_pre_filter{delete = data}
704 function module.add_for_extratags(data)
705 if type(data) == 'string' then
707 data = PRESETS.EXTRATAGS[data] or PRESETS.IGNORE_KEYS[data]
709 error('Unknown preset for extratags: ' .. preset)
712 merge_filters_into_main('extra', data)
713 add_pre_filter{extra = data}
717 function module.set_main_tags(data)
718 for key, values in pairs(MAIN_KEYS) do
719 for _, ttype in pairs(values) do
720 if ttype == 'fallback' or type(ttype) == 'function' then
724 if next(values) == nil then
728 module.modify_main_tags(data)
732 function module.modify_main_tags(data)
733 if type(data) == 'string' then
735 if data:sub(1, 7) == 'street/' then
736 data = PRESETS.MAIN_TAGS_STREETS[data:sub(8)]
737 elseif data:sub(1, 4) == 'poi/' then
738 data = PRESETS.MAIN_TAGS_POIS(data:sub(5))
740 data = PRESETS.MAIN_TAGS[data]
743 error('Unknown preset for main tags: ' .. preset)
747 for k, v in pairs(data) do
748 if MAIN_KEYS[k] == nil then
751 if type(v) == 'function' then
753 elseif type(v) == 'string' then
754 MAIN_KEYS[k][1] = PlaceTransform[v]
755 elseif type(v) == 'table' then
756 for subk, subv in pairs(v) do
757 if type(subv) == 'function' then
758 MAIN_KEYS[k][subk] = subv
760 MAIN_KEYS[k][subk] = PlaceTransform[subv]
768 function module.modify_name_tags(data)
769 if type(data) == 'string' then
771 data = PRESETS.NAME_TAGS[data]
773 error('Unknown preset for name keys: ' .. preset)
777 for k,v in pairs(data) do
784 NAME_FILTER = module.tag_group(NAMES)
785 remove_group_from_main('fallback:name')
786 if data.house ~= nil then
787 merge_filters_into_main('fallback:name', data.house)
792 function module.set_name_tags(data)
794 module.modify_name_tags(data)
798 function module.set_address_tags(data)
800 module.modify_address_tags(data)
804 function module.modify_address_tags(data)
805 if type(data) == 'string' then
807 data = PRESETS.ADDRESS_TAGS[data]
809 error('Unknown preset for address keys: ' .. preset)
813 for k, v in pairs(data) do
814 if k == 'postcode_fallback' then
815 POSTCODE_FALLBACK = v
816 elseif next(v) == nil then
817 ADDRESS_TAGS[k] = nil
823 ADDRESS_FILTER = module.tag_group(ADDRESS_TAGS)
825 remove_group_from_main('fallback:address')
826 merge_filters_into_main('fallback:address', data.main)
827 merge_filters_into_main('fallback:address', data.interpolation)
828 remove_group_from_main('fallback:postcode')
829 if POSTCODE_FALLBACK then
830 merge_filters_into_main('fallback:postcode', data.postcode)
835 function module.set_address_tags(data)
836 ADDRESS_TAGS_SOURCE = {}
837 module.modify_address_tags(data)
841 function module.set_postcode_fallback(enable)
842 if POSTCODE_FALLBACK ~= enable then
843 remove_group_from_main('fallback:postcode')
845 merge_filters_into_main('fallback:postcode', ADDRESS_TAGS.postcode)
848 POSTCODE_FALLBACK = enable
852 function module.set_unused_handling(data)
853 if type(data) == 'function' then
854 EXTRATAGS_FILTER = data
855 elseif data == nil then
856 EXTRATAGS_FILTER = default_extratags_filter
857 elseif data.extra_keys == nil and data.extra_tags == nil then
858 local delfilter = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
859 EXTRATAGS_FILTER = function (p, k)
861 for kin, vin in pairs(p.intags) do
862 if kin ~= k and not delfilter(kin, vin) then
866 if next(extra) == nil then
869 for kextra, vextra in pairs(p.extratags) do
870 extra[kextra] = vextra
874 elseif data.delete_keys == nil and data.delete_tags == nil then
875 local incfilter = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
876 EXTRATAGS_FILTER = function (p, k)
878 for kin, vin in pairs(p.intags) do
879 if kin ~= k and incfilter(kin, vin) then
883 if next(extra) == nil then
886 for kextra, vextra in pairs(p.extratags) do
887 extra[kextra] = vextra
892 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
896 function module.set_relation_types(data)
897 module.RELATION_TYPES = {}
899 if v == 'multipolygon' then
900 module.RELATION_TYPES[k] = module.relation_as_multipolygon
901 elseif v == 'multiline' then
902 module.RELATION_TYPES[k] = module.relation_as_multiline