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: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()
616 module.process_tags(Place.new(object, geom_func))
619 function module.process_relation(object)
620 local geom_func = module.RELATION_TYPES[object.tags.type]
622 if geom_func ~= nil then
623 module.process_tags(Place.new(object, geom_func))
627 -- The process functions are used by default by osm2pgsql.
629 themepark:add_proc('node', module.process_node)
630 themepark:add_proc('way', module.process_way)
631 themepark:add_proc('relation', module.process_relation)
633 osm2pgsql.process_node = module.process_node
634 osm2pgsql.process_way = module.process_way
635 osm2pgsql.process_relation = module.process_relation
638 function module.process_tags(o)
639 if next(o.intags) == nil then
640 return -- shortcut when pre-filtering has removed all tags
643 -- Exception for boundary/place double tagging
644 if o.intags.boundary == 'administrative' then
645 o:grab_extratags{match = function (k, v)
646 return k == 'place' and v:sub(1,3) ~= 'isl'
651 local fallback = o:grab_name_parts{groups=NAME_FILTER}
654 if o:grab_address_parts{groups=ADDRESS_FILTER} > 0 and fallback == nil then
655 fallback = {'place', 'house', address_fallback}
657 if o.address.country ~= nil and #o.address.country ~= 2 then
658 o.address['country'] = nil
660 if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
661 fallback = {'place', 'postcode', PlaceTransform.always}
664 if o.address.interpolation ~= nil then
665 o:write_place('place', 'houses', PlaceTransform.always)
670 for k, v in pairs(o.intags) do
671 local ktable = MAIN_KEYS[k]
673 local ktype = ktable[v] or ktable[1]
674 if type(ktype) == 'function' then
675 o:write_place(k, v, ktype)
676 elseif ktype == 'fallback' and o.has_name then
677 fallback = {k, v, PlaceTransform.named}
682 if fallback ~= nil and o.num_entries == 0 then
683 o:write_place(fallback[1], fallback[2], fallback[3])
687 --------- Convenience functions for simple style configuration -----------------
689 function module.set_prefilters(data)
690 remove_group_from_main('delete')
691 merge_filters_into_main('delete', data.delete_keys, data.delete_tags)
693 remove_group_from_main('extra')
694 merge_filters_into_main('extra', data.extra_keys, data.extra_tags)
696 PRE_FILTER = {prefix = {}, suffix = {}}
697 add_pre_filter{delete = data.delete_keys, extra = data.extra_keys}
701 function module.ignore_keys(data)
702 if type(data) == 'string' then
704 data = PRESETS.IGNORE_KEYS[data]
706 error('Unknown preset for ignored keys: ' .. preset)
709 merge_filters_into_main('delete', data)
710 add_pre_filter{delete = data}
714 function module.add_for_extratags(data)
715 if type(data) == 'string' then
717 data = PRESETS.EXTRATAGS[data] or PRESETS.IGNORE_KEYS[data]
719 error('Unknown preset for extratags: ' .. preset)
722 merge_filters_into_main('extra', data)
723 add_pre_filter{extra = data}
727 function module.set_main_tags(data)
728 for key, values in pairs(MAIN_KEYS) do
729 for _, ttype in pairs(values) do
730 if ttype == 'fallback' or type(ttype) == 'function' then
734 if next(values) == nil then
738 module.modify_main_tags(data)
742 function module.modify_main_tags(data)
743 if type(data) == 'string' then
745 if data:sub(1, 7) == 'street/' then
746 data = PRESETS.MAIN_TAGS_STREETS[data:sub(8)]
747 elseif data:sub(1, 4) == 'poi/' then
748 data = PRESETS.MAIN_TAGS_POIS(data:sub(5))
750 data = PRESETS.MAIN_TAGS[data]
753 error('Unknown preset for main tags: ' .. preset)
757 for k, v in pairs(data) do
758 if MAIN_KEYS[k] == nil then
761 if type(v) == 'function' then
763 elseif type(v) == 'string' then
764 MAIN_KEYS[k][1] = PlaceTransform[v]
765 elseif type(v) == 'table' then
766 for subk, subv in pairs(v) do
767 if type(subv) == 'function' then
768 MAIN_KEYS[k][subk] = subv
770 MAIN_KEYS[k][subk] = PlaceTransform[subv]
778 function module.modify_name_tags(data)
779 if type(data) == 'string' then
781 data = PRESETS.NAME_TAGS[data]
783 error('Unknown preset for name keys: ' .. preset)
787 for k,v in pairs(data) do
794 NAME_FILTER = module.tag_group(NAMES)
795 remove_group_from_main('fallback:name')
796 if data.house ~= nil then
797 merge_filters_into_main('fallback:name', data.house)
802 function module.set_name_tags(data)
804 module.modify_name_tags(data)
808 function module.set_address_tags(data)
810 module.modify_address_tags(data)
814 function module.modify_address_tags(data)
815 if type(data) == 'string' then
817 data = PRESETS.ADDRESS_TAGS[data]
819 error('Unknown preset for address keys: ' .. preset)
823 for k, v in pairs(data) do
824 if k == 'postcode_fallback' then
825 POSTCODE_FALLBACK = v
826 elseif next(v) == nil then
827 ADDRESS_TAGS[k] = nil
833 ADDRESS_FILTER = module.tag_group(ADDRESS_TAGS)
835 remove_group_from_main('fallback:address')
836 merge_filters_into_main('fallback:address', data.main)
837 merge_filters_into_main('fallback:address', data.interpolation)
838 remove_group_from_main('fallback:postcode')
839 if POSTCODE_FALLBACK then
840 merge_filters_into_main('fallback:postcode', data.postcode)
845 function module.set_address_tags(data)
846 ADDRESS_TAGS_SOURCE = {}
847 module.modify_address_tags(data)
851 function module.set_postcode_fallback(enable)
852 if POSTCODE_FALLBACK ~= enable then
853 remove_group_from_main('fallback:postcode')
855 merge_filters_into_main('fallback:postcode', ADDRESS_TAGS.postcode)
858 POSTCODE_FALLBACK = enable
862 function module.set_unused_handling(data)
863 if type(data) == 'function' then
864 EXTRATAGS_FILTER = data
865 elseif data == nil then
866 EXTRATAGS_FILTER = default_extratags_filter
867 elseif data.extra_keys == nil and data.extra_tags == nil then
868 local delfilter = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
869 EXTRATAGS_FILTER = function (p, k)
871 for kin, vin in pairs(p.intags) do
872 if kin ~= k and not delfilter(kin, vin) then
876 if next(extra) == nil then
879 for kextra, vextra in pairs(p.extratags) do
880 extra[kextra] = vextra
884 elseif data.delete_keys == nil and data.delete_tags == nil then
885 local incfilter = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
886 EXTRATAGS_FILTER = function (p, k)
888 for kin, vin in pairs(p.intags) do
889 if kin ~= k and incfilter(kin, vin) then
893 if next(extra) == nil then
896 for kextra, vextra in pairs(p.extratags) do
897 extra[kextra] = vextra
902 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
906 function module.set_relation_types(data)
907 module.RELATION_TYPES = {}
909 if v == 'multipolygon' then
910 module.RELATION_TYPES[k] = module.relation_as_multipolygon
911 elseif v == 'multiline' then
912 module.RELATION_TYPES[k] = module.relation_as_multiline
918 function module.get_taginfo()
919 return {main = MAIN_KEYS, name = NAMES, address = ADDRESS_TAGS}