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
33 local ENTRANCE_FUNCTION = nil
35 -- This file can also be directly require'd instead of running it under
36 -- the themepark framework. In that case the first parameter is usually
37 -- the module name. Lets check for that, so that further down we can call
38 -- the low-level osm2pgsql functions instead of themepark functions.
40 if type(themepark) ~= 'table' then
44 -- The place tables carry the raw OSM information.
45 local table_definitions = {
47 ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
49 { column = 'class', type = 'text', not_null = true },
50 { column = 'type', type = 'text', not_null = true },
51 { column = 'admin_level', type = 'smallint' },
52 { column = 'name', type = 'hstore' },
53 { column = 'address', type = 'hstore' },
54 { column = 'extratags', type = 'hstore' },
55 { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true },
60 ids = { type = 'node', id_column = 'osm_id' },
62 { column = 'type', type = 'text', not_null = true },
63 { column = 'extratags', type = 'hstore' },
64 { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true }
71 local script_path = debug.getinfo(1, "S").source:match("@?(.*/)")
72 local PRESETS = loadfile(script_path .. 'presets.lua')()
74 for table_name, table_definition in pairs(table_definitions) do
75 table_definition.name = table_name
76 table_definition.data_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_DATA")
77 table_definition.index_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_INDEX")
80 themepark:add_table(table_definition)
81 insert_row[table_name] = function(columns)
82 themepark:insert(table_name, columns, {}, {})
85 local place_table = osm2pgsql.define_table(table_definition)
86 insert_row[table_name] = function(columns)
87 place_table:insert(columns)
92 ------------ Geometry functions for relations ---------------------
94 function module.relation_as_multipolygon(o)
95 return o:as_multipolygon()
98 function module.relation_as_multiline(o)
99 return o:as_multilinestring():line_merge()
103 module.RELATION_TYPES = {
104 multipolygon = module.relation_as_multipolygon,
105 boundary = module.relation_as_multipolygon,
106 waterway = module.relation_as_multiline
109 --------- Built-in place transformation functions --------------------------
111 local PlaceTransform = {}
113 -- Special transform meanings which are interpreted elsewhere
114 PlaceTransform.fallback = 'fallback'
115 PlaceTransform.delete = 'delete'
116 PlaceTransform.extra = 'extra'
118 -- always: unconditionally use that place
119 function PlaceTransform.always(place)
123 -- never: unconditionally drop the place
124 function PlaceTransform.never()
128 -- named: use the place if it has a fully-qualified name
129 function PlaceTransform.named(place)
130 if place.has_name then
135 -- named_with_key: use place if there is a name with the main key prefix
136 function PlaceTransform.named_with_key(place, k)
138 local prefix = k .. ':name'
139 for namek, namev in pairs(place.intags) do
140 if namek:sub(1, #prefix) == prefix
141 and (#namek == #prefix
142 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
143 names[namek:sub(#k + 2)] = namev
147 if next(names) ~= nil then
148 return place:clone{names=names}
152 -- Special transform used with address fallbacks: ignore all names
153 -- except for those marked as being part of the address.
154 local function address_fallback(place)
155 if next(place.names) == nil or NAMES.house == nil then
160 for k, v in pairs(place.names) do
161 if NAME_FILTER(k, v) == 'house' then
165 return place:clone{names=names}
168 --------- Built-in extratags transformation functions ---------------
170 local function default_extratags_filter(p, k)
171 -- Default handling is to copy over place tag for boundaries.
172 -- Nominatim needs this.
173 if k ~= 'boundary' or p.intags.place == nil then
177 local extra = { place = p.intags.place }
178 for kin, vin in pairs(p.extratags) do
184 EXTRATAGS_FILTER = default_extratags_filter
186 ----------------- other helper functions -----------------------------
188 local function lookup_prefilter_classification(k, v)
190 local desc = MAIN_KEYS[k]
191 local fullmatch = desc and (desc[v] or desc[1])
192 if fullmatch ~= nil then
196 for slen, slist in pairs(PRE_FILTER.suffix) do
198 local group = slist[k:sub(-slen)]
205 for slen, slist in pairs(PRE_FILTER.prefix) do
207 local group = slist[k:sub(1, slen)]
216 local function merge_filters_into_main(group, keys, tags)
218 for _, key in pairs(keys) do
219 -- ignore suffix and prefix matches
220 if key:sub(1, 1) ~= '*' and key:sub(#key, #key) ~= '*' then
221 if MAIN_KEYS[key] == nil then
224 MAIN_KEYS[key][1] = group
230 for key, values in pairs(tags) do
231 if MAIN_KEYS[key] == nil then
234 for _, v in pairs(values) do
235 MAIN_KEYS[key][v] = group
242 local function remove_group_from_main(group)
243 for key, values in pairs(MAIN_KEYS) do
244 for _, ttype in pairs(values) do
245 if ttype == group then
249 if next(values) == nil then
256 local function add_pre_filter(data)
257 for group, keys in pairs(data) do
258 for _, key in pairs(keys) do
259 local klen = #key - 1
260 if key:sub(1, 1) == '*' then
262 if PRE_FILTER.suffix[klen] == nil then
263 PRE_FILTER.suffix[klen] = {}
265 PRE_FILTER.suffix[klen][key:sub(2)] = group
267 elseif key:sub(#key, #key) == '*' then
268 if PRE_FILTER.prefix[klen] == nil then
269 PRE_FILTER.prefix[klen] = {}
271 PRE_FILTER.prefix[klen][key:sub(1, klen)] = group
277 ------------- Place class ------------------------------------------
280 Place.__index = Place
282 function Place.new(object, geom_func)
283 local self = setmetatable({}, Place)
285 self.geom_func = geom_func
287 self.admin_level = tonumber(self.object.tags.admin_level or 15) or 15
288 if self.admin_level == nil
289 or self.admin_level <= 0 or self.admin_level > 15
290 or math.floor(self.admin_level) ~= self.admin_level then
291 self.admin_level = 15
295 self.has_name = false
302 local has_main_tags = false
303 for k, v in pairs(self.object.tags) do
304 local group = lookup_prefilter_classification(k, v)
305 if group == 'extra' then
306 self.extratags[k] = v
307 elseif group ~= 'delete' then
315 if not has_main_tags then
316 -- no interesting tags, don't bother processing
323 function Place:clean(data)
324 for k, v in pairs(self.intags) do
325 if data.delete ~= nil and data.delete(k, v) then
327 elseif data.extra ~= nil and data.extra(k, v) then
328 self.extratags[k] = v
334 function Place:delete(data)
335 if data.match ~= nil then
336 for k, v in pairs(self.intags) do
337 if data.match(k, v) then
344 function Place:grab_extratags(data)
347 if data.match ~= nil then
348 for k, v in pairs(self.intags) do
349 if data.match(k, v) then
351 self.extratags[k] = v
360 local function strip_address_prefix(k)
361 if k:sub(1, 5) == 'addr:' then
365 if k:sub(1, 6) == 'is_in:' then
373 function Place:grab_address_parts(data)
376 if data.groups ~= nil then
377 for k, v in pairs(self.intags) do
378 local atype = data.groups(k, v)
381 if atype == 'main' then
383 self.address[strip_address_prefix(k)] = v
385 elseif atype == 'extra' then
386 self.address[strip_address_prefix(k)] = v
388 self.address[atype] = v
399 function Place:grab_name_parts(data)
402 if data.groups ~= nil then
403 for k, v in pairs(self.intags) do
404 local atype = data.groups(k, v)
409 if atype == 'main' then
411 elseif atype == 'house' then
413 fallback = {'place', 'house', address_fallback}
423 function Place:write_place(k, v, mfunc)
424 v = v or self.intags[k]
429 local place = mfunc(self, k, v)
431 local res = place:write_row(k, v)
432 self.num_entries = self.num_entries + res
439 function Place:write_row(k, v)
440 if self.geometry == nil then
441 self.geometry = self.geom_func(self.object)
443 if self.geometry == nil or self.geometry:is_null() then
447 local extratags = EXTRATAGS_FILTER(self, k, v)
448 if not (extratags and next(extratags)) then
455 admin_level = self.admin_level,
456 name = next(self.names) and self.names,
457 address = next(self.address) and self.address,
458 extratags = extratags,
459 geometry = self.geometry
466 function Place:clone(data)
467 local cp = setmetatable({}, Place)
468 cp.object = self.object
469 cp.geometry = data.geometry or self.geometry
470 cp.geom_func = self.geom_func
471 cp.intags = data.intags or self.intags
472 cp.admin_level = data.admin_level or self.admin_level
473 cp.names = data.names or self.names
474 cp.address = data.address or self.address
475 cp.extratags = data.extratags or self.extratags
481 function module.tag_match(data)
482 if data == nil or next(data) == nil then
486 local fullmatches = {}
487 local key_prefixes = {}
488 local key_suffixes = {}
490 if data.keys ~= nil then
491 for _, key in pairs(data.keys) do
492 if key:sub(1, 1) == '*' then
494 if key_suffixes[#key - 1] == nil then
495 key_suffixes[#key - 1] = {}
497 key_suffixes[#key - 1][key:sub(2)] = true
499 elseif key:sub(#key, #key) == '*' then
500 if key_prefixes[#key - 1] == nil then
501 key_prefixes[#key - 1] = {}
503 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
505 fullmatches[key] = true
510 if data.tags ~= nil then
511 for k, vlist in pairs(data.tags) do
512 if fullmatches[k] == nil then
514 for _, v in pairs(vlist) do
515 fullmatches[k][v] = true
521 return function (k, v)
522 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
526 for slen, slist in pairs(key_suffixes) do
527 if #k >= slen and slist[k:sub(-slen)] ~= nil then
532 for slen, slist in pairs(key_prefixes) do
533 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
543 function module.tag_group(data)
544 if data == nil or next(data) == nil then
548 local fullmatches = {}
549 local key_prefixes = {}
550 local key_suffixes = {}
552 for group, tags in pairs(data) do
553 for _, key in pairs(tags) do
554 if key:sub(1, 1) == '*' then
556 if key_suffixes[#key - 1] == nil then
557 key_suffixes[#key - 1] = {}
559 key_suffixes[#key - 1][key:sub(2)] = group
561 elseif key:sub(#key, #key) == '*' then
562 if key_prefixes[#key - 1] == nil then
563 key_prefixes[#key - 1] = {}
565 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
567 fullmatches[key] = group
573 local val = fullmatches[k]
578 for slen, slist in pairs(key_suffixes) do
580 val = slist[k:sub(-slen)]
587 for slen, slist in pairs(key_prefixes) do
589 val = slist[k:sub(1, slen)]
598 -- Returns prefix part of the keys, and reject suffix matching keys
599 local function process_key(key)
600 if key:sub(1, 1) == '*' then
603 if key:sub(#key, #key) == '*' then
604 return key:sub(1, #key - 2)
609 -- Process functions for all data types
610 function module.process_node(object)
611 if ENTRANCE_FUNCTION ~= nil then
612 local entrance_info = ENTRANCE_FUNCTION(object)
613 if entrance_info ~= nil then
614 insert_row.place_entrance{
615 type = entrance_info.entrance,
616 extratags = entrance_info.extratags,
617 geometry = object:as_point()
622 local function geom_func(o)
626 module.process_tags(Place.new(object, geom_func))
629 function module.process_way(object)
631 local function geom_func(o)
632 local geom = o:as_polygon()
634 if geom:is_null() then
635 geom = o:as_linestring()
636 if geom:is_null() or geom:length() > 30 then
644 module.process_tags(Place.new(object, geom_func))
647 function module.process_relation(object)
648 local geom_func = module.RELATION_TYPES[object.tags.type]
650 if geom_func ~= nil then
651 module.process_tags(Place.new(object, geom_func))
655 -- The process functions are used by default by osm2pgsql.
657 themepark:add_proc('node', module.process_node)
658 themepark:add_proc('way', module.process_way)
659 themepark:add_proc('relation', module.process_relation)
661 osm2pgsql.process_node = module.process_node
662 osm2pgsql.process_way = module.process_way
663 osm2pgsql.process_relation = module.process_relation
666 function module.process_tags(o)
667 if next(o.intags) == nil then
668 return -- shortcut when pre-filtering has removed all tags
671 -- Exception for boundary/place double tagging
672 if o.intags.boundary == 'administrative' then
673 o:grab_extratags{match = function (k, v)
674 return k == 'place' and v:sub(1,3) ~= 'isl'
679 local fallback = o:grab_name_parts{groups=NAME_FILTER}
682 if o:grab_address_parts{groups=ADDRESS_FILTER} > 0 and fallback == nil then
683 fallback = {'place', 'house', address_fallback}
685 if o.address.country ~= nil and #o.address.country ~= 2 then
686 o.address['country'] = nil
688 if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
689 fallback = {'place', 'postcode', PlaceTransform.always}
692 if o.address.interpolation ~= nil then
693 o:write_place('place', 'houses', PlaceTransform.always)
698 for k, v in pairs(o.intags) do
699 local ktable = MAIN_KEYS[k]
701 local ktype = ktable[v] or ktable[1]
702 if type(ktype) == 'function' then
703 o:write_place(k, v, ktype)
704 elseif ktype == 'fallback' and o.has_name then
705 fallback = {k, v, PlaceTransform.named}
710 if fallback ~= nil and o.num_entries == 0 then
711 o:write_place(fallback[1], fallback[2], fallback[3])
715 --------- Convenience functions for simple style configuration -----------------
717 function module.set_prefilters(data)
718 remove_group_from_main('delete')
719 merge_filters_into_main('delete', data.delete_keys, data.delete_tags)
721 remove_group_from_main('extra')
722 merge_filters_into_main('extra', data.extra_keys, data.extra_tags)
724 PRE_FILTER = {prefix = {}, suffix = {}}
725 add_pre_filter{delete = data.delete_keys, extra = data.extra_keys}
729 function module.ignore_keys(data)
730 if type(data) == 'string' then
732 data = PRESETS.IGNORE_KEYS[data]
734 error('Unknown preset for ignored keys: ' .. preset)
737 merge_filters_into_main('delete', data)
738 add_pre_filter{delete = data}
742 function module.add_for_extratags(data)
743 if type(data) == 'string' then
745 data = PRESETS.EXTRATAGS[data] or PRESETS.IGNORE_KEYS[data]
747 error('Unknown preset for extratags: ' .. preset)
750 merge_filters_into_main('extra', data)
751 add_pre_filter{extra = data}
755 function module.set_main_tags(data)
756 for key, values in pairs(MAIN_KEYS) do
757 for _, ttype in pairs(values) do
758 if ttype == 'fallback' or type(ttype) == 'function' then
762 if next(values) == nil then
766 module.modify_main_tags(data)
770 function module.modify_main_tags(data)
771 if type(data) == 'string' then
773 if data:sub(1, 7) == 'street/' then
774 data = PRESETS.MAIN_TAGS_STREETS[data:sub(8)]
775 elseif data:sub(1, 4) == 'poi/' then
776 data = PRESETS.MAIN_TAGS_POIS(data:sub(5))
778 data = PRESETS.MAIN_TAGS[data]
781 error('Unknown preset for main tags: ' .. preset)
785 for k, v in pairs(data) do
786 if MAIN_KEYS[k] == nil then
789 if type(v) == 'function' then
791 elseif type(v) == 'string' then
792 MAIN_KEYS[k][1] = PlaceTransform[v]
793 elseif type(v) == 'table' then
794 for subk, subv in pairs(v) do
795 if type(subv) == 'function' then
796 MAIN_KEYS[k][subk] = subv
798 MAIN_KEYS[k][subk] = PlaceTransform[subv]
806 function module.modify_name_tags(data)
807 if type(data) == 'string' then
809 data = PRESETS.NAME_TAGS[data]
811 error('Unknown preset for name keys: ' .. preset)
815 for k,v in pairs(data) do
822 NAME_FILTER = module.tag_group(NAMES)
823 remove_group_from_main('fallback:name')
824 if data.house ~= nil then
825 merge_filters_into_main('fallback:name', data.house)
830 function module.set_name_tags(data)
832 module.modify_name_tags(data)
836 function module.set_address_tags(data)
838 module.modify_address_tags(data)
842 function module.modify_address_tags(data)
843 if type(data) == 'string' then
845 data = PRESETS.ADDRESS_TAGS[data]
847 error('Unknown preset for address keys: ' .. preset)
851 for k, v in pairs(data) do
852 if k == 'postcode_fallback' then
853 POSTCODE_FALLBACK = v
854 elseif next(v) == nil then
855 ADDRESS_TAGS[k] = nil
861 ADDRESS_FILTER = module.tag_group(ADDRESS_TAGS)
863 remove_group_from_main('fallback:address')
864 merge_filters_into_main('fallback:address', data.main)
865 merge_filters_into_main('fallback:address', data.interpolation)
866 remove_group_from_main('fallback:postcode')
867 if POSTCODE_FALLBACK then
868 merge_filters_into_main('fallback:postcode', data.postcode)
873 function module.set_address_tags(data)
874 ADDRESS_TAGS_SOURCE = {}
875 module.modify_address_tags(data)
879 function module.set_postcode_fallback(enable)
880 if POSTCODE_FALLBACK ~= enable then
881 remove_group_from_main('fallback:postcode')
883 merge_filters_into_main('fallback:postcode', ADDRESS_TAGS.postcode)
886 POSTCODE_FALLBACK = enable
890 function module.set_unused_handling(data)
891 if type(data) == 'function' then
892 EXTRATAGS_FILTER = data
893 elseif data == nil then
894 EXTRATAGS_FILTER = default_extratags_filter
895 elseif data.extra_keys == nil and data.extra_tags == nil then
896 local delfilter = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
897 EXTRATAGS_FILTER = function (p, k)
899 for kin, vin in pairs(p.intags) do
900 if kin ~= k and not delfilter(kin, vin) then
904 if next(extra) == nil then
907 for kextra, vextra in pairs(p.extratags) do
908 extra[kextra] = vextra
912 elseif data.delete_keys == nil and data.delete_tags == nil then
913 local incfilter = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
914 EXTRATAGS_FILTER = function (p, k)
916 for kin, vin in pairs(p.intags) do
917 if kin ~= k and incfilter(kin, vin) then
921 if next(extra) == nil then
924 for kextra, vextra in pairs(p.extratags) do
925 extra[kextra] = vextra
930 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
934 function module.set_relation_types(data)
935 module.RELATION_TYPES = {}
937 if v == 'multipolygon' then
938 module.RELATION_TYPES[k] = module.relation_as_multipolygon
939 elseif v == 'multiline' then
940 module.RELATION_TYPES[k] = module.relation_as_multiline
945 function module.set_entrance_filter(data)
946 if type(data) == 'string' then
948 data = PRESETS.ENTRACE_TABLE[data]
950 error('Unknown preset for entrance table: ' .. preset)
954 ENTRANCE_FUNCTION = data and data.func
956 if data ~= nil and data.main_tags ~= nil and next(data.main_tags) ~= nil then
957 if data.extra_include ~= nil and next(data.extra_include) == nil then
958 -- shortcut: no extra tags requested
959 ENTRANCE_FUNCTION = function(o)
960 for _, v in ipairs(data.main_tags) do
961 if o.tags[v] ~= nil then
962 return {entrance = o.tags[v]}
968 if data.extra_include ~= nil then
970 for _, v in pairs(data.extra_include) do
973 if data.extra_exclude ~= nil then
974 for _, v in pairs(data.extra_exclude) do
978 for _, v in pairs(data.main_tags) do
982 ENTRANCE_FUNCTION = function(o)
983 for _, v in ipairs(data.main_tags) do
984 if o.tags[v] ~= nil then
985 local entrance = o.tags[v]
987 for k, v in pairs(tags) do
990 if next(extra) == nil then
993 return {entrance = entrance, extratags = extra}
1001 if data.extra_exclude ~= nil then
1002 for _, v in pairs(data.extra_exclude) do
1006 for _, v in pairs(data.main_tags) do
1010 ENTRANCE_FUNCTION = function(o)
1011 for _, v in ipairs(data.main_tags) do
1012 if o.tags[v] ~= nil then
1013 local entrance = o.tags[v]
1015 for k, v in pairs(o.tags) do
1016 if notags[k] ~= 1 then
1020 if next(extra) == nil then
1023 return {entrance = entrance, extratags = extra}
1034 function module.get_taginfo()
1035 return {main = MAIN_KEYS, name = NAMES, address = ADDRESS_TAGS}