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 }
70 ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
72 { column = 'postcode', type = 'text', not_null = true },
73 { column = 'country_code', type = 'text' },
74 { column = 'centroid', type = 'point', projection = 'WGS84', not_null = true },
75 { column = 'geometry', type = 'geometry', projection = 'WGS84' }
78 { column = 'postcode', method = 'btree' }
84 local script_path = debug.getinfo(1, "S").source:match("@?(.*/)")
85 local PRESETS = loadfile(script_path .. 'presets.lua')()
87 for table_name, table_definition in pairs(table_definitions) do
88 table_definition.name = table_name
89 table_definition.data_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_DATA")
90 table_definition.index_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_INDEX")
93 themepark:add_table(table_definition)
94 insert_row[table_name] = function(columns)
95 themepark:insert(table_name, columns, {}, {})
98 local place_table = osm2pgsql.define_table(table_definition)
99 insert_row[table_name] = function(columns)
100 place_table:insert(columns)
105 ------------ Geometry functions for relations ---------------------
107 function module.relation_as_multipolygon(o)
108 return o:as_multipolygon()
111 function module.relation_as_multiline(o)
112 return o:as_multilinestring():line_merge()
116 module.RELATION_TYPES = {
117 multipolygon = module.relation_as_multipolygon,
118 boundary = module.relation_as_multipolygon,
119 waterway = module.relation_as_multiline
122 --------- Built-in place transformation functions --------------------------
124 local PlaceTransform = {}
126 -- Special transform meanings which are interpreted elsewhere
127 PlaceTransform.fallback = 'fallback'
128 PlaceTransform.postcode_area = 'postcode_area'
129 PlaceTransform.delete = 'delete'
130 PlaceTransform.extra = 'extra'
132 -- always: unconditionally use that place
133 function PlaceTransform.always(place)
137 -- never: unconditionally drop the place
138 function PlaceTransform.never()
142 -- named: use the place if it has a fully-qualified name
143 function PlaceTransform.named(place)
144 if place.has_name then
149 -- named_with_key: use place if there is a name with the main key prefix
150 function PlaceTransform.named_with_key(place, k)
152 local prefix = k .. ':name'
153 for namek, namev in pairs(place.intags) do
154 if namek:sub(1, #prefix) == prefix
155 and (#namek == #prefix
156 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
157 names[namek:sub(#k + 2)] = namev
161 if next(names) ~= nil then
162 return place:clone{names=names}
166 -- Special transform used with address fallbacks: ignore all names
167 -- except for those marked as being part of the address.
168 local function address_fallback(place)
169 if next(place.names) == nil or NAMES.house == nil then
174 for k, v in pairs(place.names) do
175 if NAME_FILTER(k, v) == 'house' then
179 return place:clone{names=names}
182 ----------------- other helper functions -----------------------------
184 local function lookup_prefilter_classification(k, v)
186 local desc = MAIN_KEYS[k]
187 local fullmatch = desc and (desc[v] or desc[1])
188 if fullmatch ~= nil then
192 for slen, slist in pairs(PRE_FILTER.suffix) do
194 local group = slist[k:sub(-slen)]
201 for slen, slist in pairs(PRE_FILTER.prefix) do
203 local group = slist[k:sub(1, slen)]
212 local function merge_filters_into_main(group, keys, tags)
214 for _, key in pairs(keys) do
215 -- ignore suffix and prefix matches
216 if key:sub(1, 1) ~= '*' and key:sub(#key, #key) ~= '*' then
217 if MAIN_KEYS[key] == nil then
220 MAIN_KEYS[key][1] = group
226 for key, values in pairs(tags) do
227 if MAIN_KEYS[key] == nil then
230 for _, v in pairs(values) do
231 MAIN_KEYS[key][v] = group
238 local function remove_group_from_main(group)
239 for key, values in pairs(MAIN_KEYS) do
240 for _, ttype in pairs(values) do
241 if ttype == group then
245 if next(values) == nil then
252 local function add_pre_filter(data)
253 for group, keys in pairs(data) do
254 for _, key in pairs(keys) do
255 local klen = #key - 1
256 if key:sub(1, 1) == '*' then
258 if PRE_FILTER.suffix[klen] == nil then
259 PRE_FILTER.suffix[klen] = {}
261 PRE_FILTER.suffix[klen][key:sub(2)] = group
263 elseif key:sub(#key, #key) == '*' then
264 if PRE_FILTER.prefix[klen] == nil then
265 PRE_FILTER.prefix[klen] = {}
267 PRE_FILTER.prefix[klen][key:sub(1, klen)] = group
273 ------------- Place class ------------------------------------------
276 Place.__index = Place
278 function Place.new(object, geom_func)
279 local self = setmetatable({}, Place)
281 self.geom_func = geom_func
283 self.admin_level = tonumber(self.object.tags.admin_level or 15) or 15
284 if self.admin_level == nil
285 or self.admin_level <= 0 or self.admin_level > 15
286 or math.floor(self.admin_level) ~= self.admin_level then
287 self.admin_level = 15
291 self.has_name = false
298 local has_main_tags = false
299 for k, v in pairs(self.object.tags) do
300 local group = lookup_prefilter_classification(k, v)
301 if group == 'extra' then
302 self.extratags[k] = v
303 elseif group ~= 'delete' then
311 if not has_main_tags then
312 -- no interesting tags, don't bother processing
319 function Place:clean(data)
320 for k, v in pairs(self.intags) do
321 if data.delete ~= nil and data.delete(k, v) then
323 elseif data.extra ~= nil and data.extra(k, v) then
324 self.extratags[k] = v
330 function Place:delete(data)
331 if data.match ~= nil then
332 for k, v in pairs(self.intags) do
333 if data.match(k, v) then
340 function Place:grab_extratags(data)
343 if data.match ~= nil then
344 for k, v in pairs(self.intags) do
345 if data.match(k, v) then
347 self.extratags[k] = v
356 local function strip_address_prefix(k)
357 if k:sub(1, 5) == 'addr:' then
361 if k:sub(1, 6) == 'is_in:' then
369 function Place:grab_address_parts(data)
372 if data.groups ~= nil then
373 for k, v in pairs(self.intags) do
374 local atype = data.groups(k, v)
377 if atype == 'main' then
379 self.address[strip_address_prefix(k)] = v
381 elseif atype == 'extra' then
382 self.address[strip_address_prefix(k)] = v
384 self.address[atype] = v
395 function Place:grab_name_parts(data)
398 if data.groups ~= nil then
399 for k, v in pairs(self.intags) do
400 local atype = data.groups(k, v)
405 if atype == 'main' then
407 elseif atype == 'house' then
409 fallback = {'place', 'house', address_fallback}
419 function Place:write_place(k, v, mfunc)
420 v = v or self.intags[k]
425 local place = mfunc(self, k, v)
427 local res = place:write_row(k, v)
428 self.num_entries = self.num_entries + res
436 function Place:geometry_is_valid()
437 if self.geometry == nil then
438 self.geometry = self.geom_func(self.object)
440 if self.geometry == nil or self.geometry:is_null() then
441 self.geometry = false
448 return self.geometry ~= false
452 function Place:write_row(k, v)
453 if not self:geometry_is_valid() then
457 local extra = EXTRATAGS_FILTER(self, k, v) or {}
459 for tk, tv in pairs(self.object.tags) do
460 if REQUIRED_EXTRATAGS_FILTER(tk, tv) and extra[tk] == nil then
465 if extra and next(extra) == nil then
472 admin_level = self.admin_level,
473 name = next(self.names) and self.names,
474 address = next(self.address) and self.address,
476 geometry = self.geometry
483 function Place:clone(data)
484 local cp = setmetatable({}, Place)
485 cp.object = self.object
486 cp.geometry = data.geometry or self.geometry
487 cp.geom_func = self.geom_func
488 cp.intags = data.intags or self.intags
489 cp.admin_level = data.admin_level or self.admin_level
490 cp.names = data.names or self.names
491 cp.address = data.address or self.address
492 cp.extratags = data.extratags or self.extratags
498 function module.tag_match(data)
499 if data == nil or next(data) == nil then
503 local fullmatches = {}
504 local key_prefixes = {}
505 local key_suffixes = {}
507 if data.keys ~= nil then
508 for _, key in pairs(data.keys) do
509 if key:sub(1, 1) == '*' then
511 if key_suffixes[#key - 1] == nil then
512 key_suffixes[#key - 1] = {}
514 key_suffixes[#key - 1][key:sub(2)] = true
516 elseif key:sub(#key, #key) == '*' then
517 if key_prefixes[#key - 1] == nil then
518 key_prefixes[#key - 1] = {}
520 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
522 fullmatches[key] = true
527 if data.tags ~= nil then
528 for k, vlist in pairs(data.tags) do
529 if fullmatches[k] == nil then
531 for _, v in pairs(vlist) do
532 fullmatches[k][v] = true
538 return function (k, v)
539 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
543 for slen, slist in pairs(key_suffixes) do
544 if #k >= slen and slist[k:sub(-slen)] ~= nil then
549 for slen, slist in pairs(key_prefixes) do
550 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
560 function module.tag_group(data)
561 if data == nil or next(data) == nil then
565 local fullmatches = {}
566 local key_prefixes = {}
567 local key_suffixes = {}
569 for group, tags in pairs(data) do
570 for _, key in pairs(tags) do
571 if key:sub(1, 1) == '*' then
573 if key_suffixes[#key - 1] == nil then
574 key_suffixes[#key - 1] = {}
576 key_suffixes[#key - 1][key:sub(2)] = group
578 elseif key:sub(#key, #key) == '*' then
579 if key_prefixes[#key - 1] == nil then
580 key_prefixes[#key - 1] = {}
582 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
584 fullmatches[key] = group
590 local val = fullmatches[k]
595 for slen, slist in pairs(key_suffixes) do
597 val = slist[k:sub(-slen)]
604 for slen, slist in pairs(key_prefixes) do
606 val = slist[k:sub(1, slen)]
615 -- Returns prefix part of the keys, and reject suffix matching keys
616 local function process_key(key)
617 if key:sub(1, 1) == '*' then
620 if key:sub(#key, #key) == '*' then
621 return key:sub(1, #key - 2)
626 -- Process functions for all data types
627 function module.process_node(object)
628 if ENTRANCE_FUNCTION ~= nil then
629 local entrance_info = ENTRANCE_FUNCTION(object)
630 if entrance_info ~= nil then
631 insert_row.place_entrance{
632 type = entrance_info.entrance,
633 extratags = entrance_info.extratags,
634 geometry = object:as_point()
639 local function geom_func(o)
643 module.process_tags(Place.new(object, geom_func))
646 function module.process_way(object)
648 local function geom_func(o)
649 local geom = o:as_polygon()
651 if geom:is_null() then
652 geom = o:as_linestring()
653 if geom:is_null() or geom:length() > 30 then
661 module.process_tags(Place.new(object, geom_func))
664 function module.process_relation(object)
665 local geom_func = module.RELATION_TYPES[object.tags.type]
667 if geom_func ~= nil then
668 module.process_tags(Place.new(object, geom_func))
672 -- The process functions are used by default by osm2pgsql.
674 themepark:add_proc('node', module.process_node)
675 themepark:add_proc('way', module.process_way)
676 themepark:add_proc('relation', module.process_relation)
678 osm2pgsql.process_node = module.process_node
679 osm2pgsql.process_way = module.process_way
680 osm2pgsql.process_relation = module.process_relation
683 function module.process_tags(o)
684 if next(o.intags) == nil then
685 return -- shortcut when pre-filtering has removed all tags
688 -- Exception for boundary/place double tagging
689 if o.intags.boundary == 'administrative' then
690 o:grab_extratags{match = function (k, v)
691 return k == 'place' and v:sub(1,3) ~= 'isl'
696 local fallback = o:grab_name_parts{groups=NAME_FILTER}
699 if o:grab_address_parts{groups=ADDRESS_FILTER} > 0 and fallback == nil then
700 fallback = {'place', 'house', address_fallback}
702 if o.address.country ~= nil and #o.address.country ~= 2 then
703 o.address['country'] = nil
706 if o.address.interpolation ~= nil then
707 o:write_place('place', 'houses', PlaceTransform.always)
712 local postcode_collect = false
713 for k, v in pairs(o.intags) do
714 local ktable = MAIN_KEYS[k]
716 local ktype = ktable[v] or ktable[1]
717 if type(ktype) == 'function' then
718 o:write_place(k, v, ktype)
719 elseif ktype == 'postcode_area' then
720 postcode_collect = true
721 if o.object.type == 'relation'
722 and o.address.postcode ~= nil
723 and o:geometry_is_valid() then
724 insert_row.place_postcode{
725 postcode = o.address.postcode,
726 centroid = o.geometry:centroid(),
727 geometry = o.geometry
730 elseif ktype == 'fallback' and o.has_name then
731 fallback = {k, v, PlaceTransform.named}
736 if o.num_entries == 0 then
737 if fallback ~= nil then
738 o:write_place(fallback[1], fallback[2], fallback[3])
739 elseif POSTCODE_FALLBACK and not postcode_collect
740 and o.address.postcode ~= nil
741 and o:geometry_is_valid() then
742 insert_row.place_postcode{
743 postcode = o.address.postcode,
744 centroid = o.geometry:centroid()
750 --------- Extratags post-processing functions ---------------
752 local function default_extratags_filter(p, k)
756 EXTRATAGS_FILTER = default_extratags_filter
757 REQUIRED_EXTRATAGS_FILTER = module.tag_match(PRESETS.EXTRATAGS)
759 --------- Convenience functions for simple style configuration -----------------
761 function module.set_prefilters(data)
762 remove_group_from_main('delete')
763 merge_filters_into_main('delete', data.delete_keys, data.delete_tags)
765 remove_group_from_main('extra')
766 merge_filters_into_main('extra', data.extra_keys, data.extra_tags)
768 PRE_FILTER = {prefix = {}, suffix = {}}
769 add_pre_filter{delete = data.delete_keys, extra = data.extra_keys}
773 function module.ignore_keys(data)
774 if type(data) == 'string' then
776 data = PRESETS.IGNORE_KEYS[data]
778 error('Unknown preset for ignored keys: ' .. preset)
781 merge_filters_into_main('delete', data)
782 add_pre_filter{delete = data}
786 function module.add_for_extratags(data)
787 if type(data) == 'string' then
789 data = PRESETS.IGNORE_KEYS[data]
791 error('Unknown preset for extratags: ' .. preset)
794 merge_filters_into_main('extra', data)
795 add_pre_filter{extra = data}
799 function module.set_main_tags(data)
800 for key, values in pairs(MAIN_KEYS) do
801 for _, ttype in pairs(values) do
802 if ttype == 'fallback' or type(ttype) == 'function' then
806 if next(values) == nil then
810 module.modify_main_tags(data)
814 function module.modify_main_tags(data)
815 if type(data) == 'string' then
817 if data:sub(1, 7) == 'street/' then
818 data = PRESETS.MAIN_TAGS_STREETS[data:sub(8)]
819 elseif data:sub(1, 4) == 'poi/' then
820 data = PRESETS.MAIN_TAGS_POIS(data:sub(5))
822 data = PRESETS.MAIN_TAGS[data]
825 error('Unknown preset for main tags: ' .. preset)
829 for k, v in pairs(data) do
830 if MAIN_KEYS[k] == nil then
833 if type(v) == 'function' then
835 elseif type(v) == 'string' then
836 MAIN_KEYS[k][1] = PlaceTransform[v]
837 elseif type(v) == 'table' then
838 for subk, subv in pairs(v) do
839 if type(subv) == 'function' then
840 MAIN_KEYS[k][subk] = subv
842 MAIN_KEYS[k][subk] = PlaceTransform[subv]
850 function module.modify_name_tags(data)
851 if type(data) == 'string' then
853 data = PRESETS.NAME_TAGS[data]
855 error('Unknown preset for name keys: ' .. preset)
859 for k,v in pairs(data) do
866 NAME_FILTER = module.tag_group(NAMES)
867 remove_group_from_main('fallback:name')
868 if data.house ~= nil then
869 merge_filters_into_main('fallback:name', data.house)
874 function module.set_name_tags(data)
876 module.modify_name_tags(data)
880 function module.set_address_tags(data)
882 module.modify_address_tags(data)
886 function module.modify_address_tags(data)
887 if type(data) == 'string' then
889 data = PRESETS.ADDRESS_TAGS[data]
891 error('Unknown preset for address keys: ' .. preset)
895 for k, v in pairs(data) do
896 if k == 'postcode_fallback' then
897 POSTCODE_FALLBACK = v
898 elseif next(v) == nil then
899 ADDRESS_TAGS[k] = nil
905 ADDRESS_FILTER = module.tag_group(ADDRESS_TAGS)
907 remove_group_from_main('fallback:address')
908 merge_filters_into_main('fallback:address', data.main)
909 merge_filters_into_main('fallback:address', data.interpolation)
910 remove_group_from_main('fallback:postcode')
911 if POSTCODE_FALLBACK then
912 merge_filters_into_main('fallback:postcode', data.postcode)
917 function module.set_address_tags(data)
918 ADDRESS_TAGS_SOURCE = {}
919 module.modify_address_tags(data)
923 function module.set_postcode_fallback(enable)
924 if POSTCODE_FALLBACK ~= enable then
925 remove_group_from_main('fallback:postcode')
927 merge_filters_into_main('fallback:postcode', ADDRESS_TAGS.postcode)
930 POSTCODE_FALLBACK = enable
934 function module.set_unused_handling(data)
935 if type(data) == 'function' then
936 EXTRATAGS_FILTER = data
937 elseif data == nil then
938 EXTRATAGS_FILTER = default_extratags_filter
939 elseif data.extra_keys == nil and data.extra_tags == nil then
940 local delfilter = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
941 EXTRATAGS_FILTER = function (p, k)
943 for kin, vin in pairs(p.intags) do
944 if kin ~= k and not delfilter(kin, vin) then
948 if next(extra) == nil then
951 for kextra, vextra in pairs(p.extratags) do
952 extra[kextra] = vextra
956 elseif data.delete_keys == nil and data.delete_tags == nil then
957 local incfilter = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
958 EXTRATAGS_FILTER = function (p, k)
960 for kin, vin in pairs(p.intags) do
961 if kin ~= k and incfilter(kin, vin) then
965 if next(extra) == nil then
968 for kextra, vextra in pairs(p.extratags) do
969 extra[kextra] = vextra
974 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
978 function module.set_relation_types(data)
979 module.RELATION_TYPES = {}
981 if v == 'multipolygon' then
982 module.RELATION_TYPES[k] = module.relation_as_multipolygon
983 elseif v == 'multiline' then
984 module.RELATION_TYPES[k] = module.relation_as_multiline
989 function module.set_entrance_filter(data)
990 if data == nil or type(data) == 'function' then
991 ENTRANCE_FUNCTION = data
995 if type(data) == 'string' then
997 data = PRESETS.ENTRANCE_TABLE[data]
999 error('Unknown preset for entrance table: ' .. preset)
1003 ENTRANCE_FUNCTION = nil
1005 if data.main_tags ~= nil and next(data.main_tags) ~= nil then
1006 if data.extra_include ~= nil and next(data.extra_include) == nil then
1007 -- shortcut: no extra tags requested
1008 ENTRANCE_FUNCTION = function(o)
1009 for _, v in ipairs(data.main_tags) do
1010 if o.tags[v] ~= nil then
1011 return {entrance = o.tags[v]}
1017 if data.extra_include ~= nil then
1019 for _, v in pairs(data.extra_include) do
1022 if data.extra_exclude ~= nil then
1023 for _, v in pairs(data.extra_exclude) do
1027 for _, v in pairs(data.main_tags) do
1031 ENTRANCE_FUNCTION = function(o)
1032 for _, v in ipairs(data.main_tags) do
1033 if o.tags[v] ~= nil then
1034 local entrance = o.tags[v]
1036 for k, v in pairs(tags) do
1037 extra[k] = o.tags[k]
1039 if next(extra) == nil then
1042 return {entrance = entrance, extratags = extra}
1050 if data.extra_exclude ~= nil then
1051 for _, v in pairs(data.extra_exclude) do
1055 for _, v in pairs(data.main_tags) do
1059 ENTRANCE_FUNCTION = function(o)
1060 for _, v in ipairs(data.main_tags) do
1061 if o.tags[v] ~= nil then
1062 local entrance = o.tags[v]
1064 for k, v in pairs(o.tags) do
1065 if notags[k] ~= 1 then
1069 if next(extra) == nil then
1072 return {entrance = entrance, extratags = extra}
1083 function module.get_taginfo()
1084 return {main = MAIN_KEYS, name = NAMES, address = ADDRESS_TAGS}