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 REQUIRED_EXTRATAGS_FILTER
33 local POSTCODE_FALLBACK = true
34 local ENTRANCE_FUNCTION = nil
36 -- This file can also be directly require'd instead of running it under
37 -- the themepark framework. In that case the first parameter is usually
38 -- the module name. Lets check for that, so that further down we can call
39 -- the low-level osm2pgsql functions instead of themepark functions.
41 if type(themepark) ~= 'table' then
45 -- The place tables carry the raw OSM information.
46 local table_definitions = {
48 ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
50 { column = 'class', type = 'text', not_null = true },
51 { column = 'type', type = 'text', not_null = true },
52 { column = 'admin_level', type = 'smallint' },
53 { column = 'name', type = 'hstore' },
54 { column = 'address', type = 'hstore' },
55 { column = 'extratags', type = 'hstore' },
56 { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true },
61 ids = { type = 'node', id_column = 'osm_id' },
63 { column = 'type', type = 'text', not_null = true },
64 { column = 'extratags', type = 'hstore' },
65 { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true }
72 local script_path = debug.getinfo(1, "S").source:match("@?(.*/)")
73 local PRESETS = loadfile(script_path .. 'presets.lua')()
75 for table_name, table_definition in pairs(table_definitions) do
76 table_definition.name = table_name
77 table_definition.data_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_DATA")
78 table_definition.index_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_INDEX")
81 themepark:add_table(table_definition)
82 insert_row[table_name] = function(columns)
83 themepark:insert(table_name, columns, {}, {})
86 local place_table = osm2pgsql.define_table(table_definition)
87 insert_row[table_name] = function(columns)
88 place_table:insert(columns)
93 ------------ Geometry functions for relations ---------------------
95 function module.relation_as_multipolygon(o)
96 return o:as_multipolygon()
99 function module.relation_as_multiline(o)
100 return o:as_multilinestring():line_merge()
104 module.RELATION_TYPES = {
105 multipolygon = module.relation_as_multipolygon,
106 boundary = module.relation_as_multipolygon,
107 waterway = module.relation_as_multiline
110 --------- Built-in place transformation functions --------------------------
112 local PlaceTransform = {}
114 -- Special transform meanings which are interpreted elsewhere
115 PlaceTransform.fallback = 'fallback'
116 PlaceTransform.delete = 'delete'
117 PlaceTransform.extra = 'extra'
119 -- always: unconditionally use that place
120 function PlaceTransform.always(place)
124 -- never: unconditionally drop the place
125 function PlaceTransform.never()
129 -- named: use the place if it has a fully-qualified name
130 function PlaceTransform.named(place)
131 if place.has_name then
136 -- named_with_key: use place if there is a name with the main key prefix
137 function PlaceTransform.named_with_key(place, k)
139 local prefix = k .. ':name'
140 for namek, namev in pairs(place.intags) do
141 if namek:sub(1, #prefix) == prefix
142 and (#namek == #prefix
143 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
144 names[namek:sub(#k + 2)] = namev
148 if next(names) ~= nil then
149 return place:clone{names=names}
153 -- Special transform used with address fallbacks: ignore all names
154 -- except for those marked as being part of the address.
155 local function address_fallback(place)
156 if next(place.names) == nil or NAMES.house == nil then
161 for k, v in pairs(place.names) do
162 if NAME_FILTER(k, v) == 'house' then
166 return place:clone{names=names}
169 ----------------- other helper functions -----------------------------
171 local function lookup_prefilter_classification(k, v)
173 local desc = MAIN_KEYS[k]
174 local fullmatch = desc and (desc[v] or desc[1])
175 if fullmatch ~= nil then
179 for slen, slist in pairs(PRE_FILTER.suffix) do
181 local group = slist[k:sub(-slen)]
188 for slen, slist in pairs(PRE_FILTER.prefix) do
190 local group = slist[k:sub(1, slen)]
199 local function merge_filters_into_main(group, keys, tags)
201 for _, key in pairs(keys) do
202 -- ignore suffix and prefix matches
203 if key:sub(1, 1) ~= '*' and key:sub(#key, #key) ~= '*' then
204 if MAIN_KEYS[key] == nil then
207 MAIN_KEYS[key][1] = group
213 for key, values in pairs(tags) do
214 if MAIN_KEYS[key] == nil then
217 for _, v in pairs(values) do
218 MAIN_KEYS[key][v] = group
225 local function remove_group_from_main(group)
226 for key, values in pairs(MAIN_KEYS) do
227 for _, ttype in pairs(values) do
228 if ttype == group then
232 if next(values) == nil then
239 local function add_pre_filter(data)
240 for group, keys in pairs(data) do
241 for _, key in pairs(keys) do
242 local klen = #key - 1
243 if key:sub(1, 1) == '*' then
245 if PRE_FILTER.suffix[klen] == nil then
246 PRE_FILTER.suffix[klen] = {}
248 PRE_FILTER.suffix[klen][key:sub(2)] = group
250 elseif key:sub(#key, #key) == '*' then
251 if PRE_FILTER.prefix[klen] == nil then
252 PRE_FILTER.prefix[klen] = {}
254 PRE_FILTER.prefix[klen][key:sub(1, klen)] = group
260 ------------- Place class ------------------------------------------
263 Place.__index = Place
265 function Place.new(object, geom_func)
266 local self = setmetatable({}, Place)
268 self.geom_func = geom_func
270 self.admin_level = tonumber(self.object.tags.admin_level or 15) or 15
271 if self.admin_level == nil
272 or self.admin_level <= 0 or self.admin_level > 15
273 or math.floor(self.admin_level) ~= self.admin_level then
274 self.admin_level = 15
278 self.has_name = false
285 local has_main_tags = false
286 for k, v in pairs(self.object.tags) do
287 local group = lookup_prefilter_classification(k, v)
288 if group == 'extra' then
289 self.extratags[k] = v
290 elseif group ~= 'delete' then
298 if not has_main_tags then
299 -- no interesting tags, don't bother processing
306 function Place:clean(data)
307 for k, v in pairs(self.intags) do
308 if data.delete ~= nil and data.delete(k, v) then
310 elseif data.extra ~= nil and data.extra(k, v) then
311 self.extratags[k] = v
317 function Place:delete(data)
318 if data.match ~= nil then
319 for k, v in pairs(self.intags) do
320 if data.match(k, v) then
327 function Place:grab_extratags(data)
330 if data.match ~= nil then
331 for k, v in pairs(self.intags) do
332 if data.match(k, v) then
334 self.extratags[k] = v
343 local function strip_address_prefix(k)
344 if k:sub(1, 5) == 'addr:' then
348 if k:sub(1, 6) == 'is_in:' then
356 function Place:grab_address_parts(data)
359 if data.groups ~= nil then
360 for k, v in pairs(self.intags) do
361 local atype = data.groups(k, v)
364 if atype == 'main' then
366 self.address[strip_address_prefix(k)] = v
368 elseif atype == 'extra' then
369 self.address[strip_address_prefix(k)] = v
371 self.address[atype] = v
382 function Place:grab_name_parts(data)
385 if data.groups ~= nil then
386 for k, v in pairs(self.intags) do
387 local atype = data.groups(k, v)
392 if atype == 'main' then
394 elseif atype == 'house' then
396 fallback = {'place', 'house', address_fallback}
406 function Place:write_place(k, v, mfunc)
407 v = v or self.intags[k]
412 local place = mfunc(self, k, v)
414 local res = place:write_row(k, v)
415 self.num_entries = self.num_entries + res
422 function Place:write_row(k, v)
423 if self.geometry == nil then
424 self.geometry = self.geom_func(self.object)
426 if self.geometry == nil or self.geometry:is_null() then
430 local extra = EXTRATAGS_FILTER(self, k, v) or {}
432 for tk, tv in pairs(self.object.tags) do
433 if REQUIRED_EXTRATAGS_FILTER(tk, tv) and extra[tk] == nil then
438 if extra and next(extra) == nil then
445 admin_level = self.admin_level,
446 name = next(self.names) and self.names,
447 address = next(self.address) and self.address,
449 geometry = self.geometry
456 function Place:clone(data)
457 local cp = setmetatable({}, Place)
458 cp.object = self.object
459 cp.geometry = data.geometry or self.geometry
460 cp.geom_func = self.geom_func
461 cp.intags = data.intags or self.intags
462 cp.admin_level = data.admin_level or self.admin_level
463 cp.names = data.names or self.names
464 cp.address = data.address or self.address
465 cp.extratags = data.extratags or self.extratags
471 function module.tag_match(data)
472 if data == nil or next(data) == nil then
476 local fullmatches = {}
477 local key_prefixes = {}
478 local key_suffixes = {}
480 if data.keys ~= nil then
481 for _, key in pairs(data.keys) do
482 if key:sub(1, 1) == '*' then
484 if key_suffixes[#key - 1] == nil then
485 key_suffixes[#key - 1] = {}
487 key_suffixes[#key - 1][key:sub(2)] = true
489 elseif key:sub(#key, #key) == '*' then
490 if key_prefixes[#key - 1] == nil then
491 key_prefixes[#key - 1] = {}
493 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
495 fullmatches[key] = true
500 if data.tags ~= nil then
501 for k, vlist in pairs(data.tags) do
502 if fullmatches[k] == nil then
504 for _, v in pairs(vlist) do
505 fullmatches[k][v] = true
511 return function (k, v)
512 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
516 for slen, slist in pairs(key_suffixes) do
517 if #k >= slen and slist[k:sub(-slen)] ~= nil then
522 for slen, slist in pairs(key_prefixes) do
523 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
533 function module.tag_group(data)
534 if data == nil or next(data) == nil then
538 local fullmatches = {}
539 local key_prefixes = {}
540 local key_suffixes = {}
542 for group, tags in pairs(data) do
543 for _, key in pairs(tags) do
544 if key:sub(1, 1) == '*' then
546 if key_suffixes[#key - 1] == nil then
547 key_suffixes[#key - 1] = {}
549 key_suffixes[#key - 1][key:sub(2)] = group
551 elseif key:sub(#key, #key) == '*' then
552 if key_prefixes[#key - 1] == nil then
553 key_prefixes[#key - 1] = {}
555 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
557 fullmatches[key] = group
563 local val = fullmatches[k]
568 for slen, slist in pairs(key_suffixes) do
570 val = slist[k:sub(-slen)]
577 for slen, slist in pairs(key_prefixes) do
579 val = slist[k:sub(1, slen)]
588 -- Returns prefix part of the keys, and reject suffix matching keys
589 local function process_key(key)
590 if key:sub(1, 1) == '*' then
593 if key:sub(#key, #key) == '*' then
594 return key:sub(1, #key - 2)
599 -- Process functions for all data types
600 function module.process_node(object)
601 if ENTRANCE_FUNCTION ~= nil then
602 local entrance_info = ENTRANCE_FUNCTION(object)
603 if entrance_info ~= nil then
604 insert_row.place_entrance{
605 type = entrance_info.entrance,
606 extratags = entrance_info.extratags,
607 geometry = object:as_point()
612 local function geom_func(o)
616 module.process_tags(Place.new(object, geom_func))
619 function module.process_way(object)
621 local function geom_func(o)
622 local geom = o:as_polygon()
624 if geom:is_null() then
625 geom = o:as_linestring()
626 if geom:is_null() or geom:length() > 30 then
634 module.process_tags(Place.new(object, geom_func))
637 function module.process_relation(object)
638 local geom_func = module.RELATION_TYPES[object.tags.type]
640 if geom_func ~= nil then
641 module.process_tags(Place.new(object, geom_func))
645 -- The process functions are used by default by osm2pgsql.
647 themepark:add_proc('node', module.process_node)
648 themepark:add_proc('way', module.process_way)
649 themepark:add_proc('relation', module.process_relation)
651 osm2pgsql.process_node = module.process_node
652 osm2pgsql.process_way = module.process_way
653 osm2pgsql.process_relation = module.process_relation
656 function module.process_tags(o)
657 if next(o.intags) == nil then
658 return -- shortcut when pre-filtering has removed all tags
661 -- Exception for boundary/place double tagging
662 if o.intags.boundary == 'administrative' then
663 o:grab_extratags{match = function (k, v)
664 return k == 'place' and v:sub(1,3) ~= 'isl'
669 local fallback = o:grab_name_parts{groups=NAME_FILTER}
672 if o:grab_address_parts{groups=ADDRESS_FILTER} > 0 and fallback == nil then
673 fallback = {'place', 'house', address_fallback}
675 if o.address.country ~= nil and #o.address.country ~= 2 then
676 o.address['country'] = nil
678 if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
679 fallback = {'place', 'postcode', PlaceTransform.always}
682 if o.address.interpolation ~= nil then
683 o:write_place('place', 'houses', PlaceTransform.always)
688 for k, v in pairs(o.intags) do
689 local ktable = MAIN_KEYS[k]
691 local ktype = ktable[v] or ktable[1]
692 if type(ktype) == 'function' then
693 o:write_place(k, v, ktype)
694 elseif ktype == 'fallback' and o.has_name then
695 fallback = {k, v, PlaceTransform.named}
700 if fallback ~= nil and o.num_entries == 0 then
701 o:write_place(fallback[1], fallback[2], fallback[3])
705 --------- Extratags post-processing functions ---------------
707 local function default_extratags_filter(p, k)
711 EXTRATAGS_FILTER = default_extratags_filter
712 REQUIRED_EXTRATAGS_FILTER = module.tag_match(PRESETS.EXTRATAGS)
714 --------- Convenience functions for simple style configuration -----------------
716 function module.set_prefilters(data)
717 remove_group_from_main('delete')
718 merge_filters_into_main('delete', data.delete_keys, data.delete_tags)
720 remove_group_from_main('extra')
721 merge_filters_into_main('extra', data.extra_keys, data.extra_tags)
723 PRE_FILTER = {prefix = {}, suffix = {}}
724 add_pre_filter{delete = data.delete_keys, extra = data.extra_keys}
728 function module.ignore_keys(data)
729 if type(data) == 'string' then
731 data = PRESETS.IGNORE_KEYS[data]
733 error('Unknown preset for ignored keys: ' .. preset)
736 merge_filters_into_main('delete', data)
737 add_pre_filter{delete = data}
741 function module.add_for_extratags(data)
742 if type(data) == 'string' then
744 data = PRESETS.IGNORE_KEYS[data]
746 error('Unknown preset for extratags: ' .. preset)
749 merge_filters_into_main('extra', data)
750 add_pre_filter{extra = data}
754 function module.set_main_tags(data)
755 for key, values in pairs(MAIN_KEYS) do
756 for _, ttype in pairs(values) do
757 if ttype == 'fallback' or type(ttype) == 'function' then
761 if next(values) == nil then
765 module.modify_main_tags(data)
769 function module.modify_main_tags(data)
770 if type(data) == 'string' then
772 if data:sub(1, 7) == 'street/' then
773 data = PRESETS.MAIN_TAGS_STREETS[data:sub(8)]
774 elseif data:sub(1, 4) == 'poi/' then
775 data = PRESETS.MAIN_TAGS_POIS(data:sub(5))
777 data = PRESETS.MAIN_TAGS[data]
780 error('Unknown preset for main tags: ' .. preset)
784 for k, v in pairs(data) do
785 if MAIN_KEYS[k] == nil then
788 if type(v) == 'function' then
790 elseif type(v) == 'string' then
791 MAIN_KEYS[k][1] = PlaceTransform[v]
792 elseif type(v) == 'table' then
793 for subk, subv in pairs(v) do
794 if type(subv) == 'function' then
795 MAIN_KEYS[k][subk] = subv
797 MAIN_KEYS[k][subk] = PlaceTransform[subv]
805 function module.modify_name_tags(data)
806 if type(data) == 'string' then
808 data = PRESETS.NAME_TAGS[data]
810 error('Unknown preset for name keys: ' .. preset)
814 for k,v in pairs(data) do
821 NAME_FILTER = module.tag_group(NAMES)
822 remove_group_from_main('fallback:name')
823 if data.house ~= nil then
824 merge_filters_into_main('fallback:name', data.house)
829 function module.set_name_tags(data)
831 module.modify_name_tags(data)
835 function module.set_address_tags(data)
837 module.modify_address_tags(data)
841 function module.modify_address_tags(data)
842 if type(data) == 'string' then
844 data = PRESETS.ADDRESS_TAGS[data]
846 error('Unknown preset for address keys: ' .. preset)
850 for k, v in pairs(data) do
851 if k == 'postcode_fallback' then
852 POSTCODE_FALLBACK = v
853 elseif next(v) == nil then
854 ADDRESS_TAGS[k] = nil
860 ADDRESS_FILTER = module.tag_group(ADDRESS_TAGS)
862 remove_group_from_main('fallback:address')
863 merge_filters_into_main('fallback:address', data.main)
864 merge_filters_into_main('fallback:address', data.interpolation)
865 remove_group_from_main('fallback:postcode')
866 if POSTCODE_FALLBACK then
867 merge_filters_into_main('fallback:postcode', data.postcode)
872 function module.set_address_tags(data)
873 ADDRESS_TAGS_SOURCE = {}
874 module.modify_address_tags(data)
878 function module.set_postcode_fallback(enable)
879 if POSTCODE_FALLBACK ~= enable then
880 remove_group_from_main('fallback:postcode')
882 merge_filters_into_main('fallback:postcode', ADDRESS_TAGS.postcode)
885 POSTCODE_FALLBACK = enable
889 function module.set_unused_handling(data)
890 if type(data) == 'function' then
891 EXTRATAGS_FILTER = data
892 elseif data == nil then
893 EXTRATAGS_FILTER = default_extratags_filter
894 elseif data.extra_keys == nil and data.extra_tags == nil then
895 local delfilter = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
896 EXTRATAGS_FILTER = function (p, k)
898 for kin, vin in pairs(p.intags) do
899 if kin ~= k and not delfilter(kin, vin) then
903 if next(extra) == nil then
906 for kextra, vextra in pairs(p.extratags) do
907 extra[kextra] = vextra
911 elseif data.delete_keys == nil and data.delete_tags == nil then
912 local incfilter = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
913 EXTRATAGS_FILTER = function (p, k)
915 for kin, vin in pairs(p.intags) do
916 if kin ~= k and incfilter(kin, vin) then
920 if next(extra) == nil then
923 for kextra, vextra in pairs(p.extratags) do
924 extra[kextra] = vextra
929 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
933 function module.set_relation_types(data)
934 module.RELATION_TYPES = {}
936 if v == 'multipolygon' then
937 module.RELATION_TYPES[k] = module.relation_as_multipolygon
938 elseif v == 'multiline' then
939 module.RELATION_TYPES[k] = module.relation_as_multiline
944 function module.set_entrance_filter(data)
945 if data == nil or type(data) == 'function' then
946 ENTRANCE_FUNCTION = data
950 if type(data) == 'string' then
952 data = PRESETS.ENTRANCE_TABLE[data]
954 error('Unknown preset for entrance table: ' .. preset)
958 ENTRANCE_FUNCTION = nil
960 if data.main_tags ~= nil and next(data.main_tags) ~= nil then
961 if data.extra_include ~= nil and next(data.extra_include) == nil then
962 -- shortcut: no extra tags requested
963 ENTRANCE_FUNCTION = function(o)
964 for _, v in ipairs(data.main_tags) do
965 if o.tags[v] ~= nil then
966 return {entrance = o.tags[v]}
972 if data.extra_include ~= nil then
974 for _, v in pairs(data.extra_include) do
977 if data.extra_exclude ~= nil then
978 for _, v in pairs(data.extra_exclude) do
982 for _, v in pairs(data.main_tags) do
986 ENTRANCE_FUNCTION = function(o)
987 for _, v in ipairs(data.main_tags) do
988 if o.tags[v] ~= nil then
989 local entrance = o.tags[v]
991 for k, v in pairs(tags) do
994 if next(extra) == nil then
997 return {entrance = entrance, extratags = extra}
1005 if data.extra_exclude ~= nil then
1006 for _, v in pairs(data.extra_exclude) do
1010 for _, v in pairs(data.main_tags) do
1014 ENTRANCE_FUNCTION = function(o)
1015 for _, v in ipairs(data.main_tags) do
1016 if o.tags[v] ~= nil then
1017 local entrance = o.tags[v]
1019 for k, v in pairs(o.tags) do
1020 if notags[k] ~= 1 then
1024 if next(extra) == nil then
1027 return {entrance = entrance, extratags = extra}
1038 function module.get_taginfo()
1039 return {main = MAIN_KEYS, name = NAMES, address = ADDRESS_TAGS}