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 --------- Built-in extratags transformation functions ---------------
139 local function default_extratags_filter(p, k)
140 -- Default handling is to copy over place tag for boundaries.
141 -- Nominatim needs this.
142 if k ~= 'boundary' or p.intags.place == nil then
146 local extra = { place = p.intags.place }
147 for kin, vin in pairs(p.extratags) do
153 EXTRATAGS_FILTER = default_extratags_filter
155 ----------------- other helper functions -----------------------------
157 local function lookup_prefilter_classification(k, v)
159 local desc = MAIN_KEYS[k]
160 local fullmatch = desc and (desc[v] or desc[1])
161 if fullmatch ~= nil then
165 for slen, slist in pairs(PRE_FILTER.suffix) do
167 local group = slist[k:sub(-slen)]
174 for slen, slist in pairs(PRE_FILTER.prefix) do
176 local group = slist[k:sub(1, slen)]
185 local function merge_filters_into_main(group, keys, tags)
187 for _, key in pairs(keys) do
188 -- ignore suffix and prefix matches
189 if key:sub(1, 1) ~= '*' and key:sub(#key, #key) ~= '*' then
190 if MAIN_KEYS[key] == nil then
193 MAIN_KEYS[key][1] = group
199 for key, values in pairs(tags) do
200 if MAIN_KEYS[key] == nil then
203 for _, v in pairs(values) do
204 MAIN_KEYS[key][v] = group
211 local function remove_group_from_main(group)
212 for key, values in pairs(MAIN_KEYS) do
213 for _, ttype in pairs(values) do
214 if ttype == group then
218 if next(values) == nil then
225 local function add_pre_filter(data)
226 for group, keys in pairs(data) do
227 for _, key in pairs(keys) do
228 local klen = #key - 1
229 if key:sub(1, 1) == '*' then
231 if PRE_FILTER.suffix[klen] == nil then
232 PRE_FILTER.suffix[klen] = {}
234 PRE_FILTER.suffix[klen][key:sub(2)] = group
236 elseif key:sub(#key, #key) == '*' then
237 if PRE_FILTER.prefix[klen] == nil then
238 PRE_FILTER.prefix[klen] = {}
240 PRE_FILTER.prefix[klen][key:sub(1, klen)] = group
246 ------------- Place class ------------------------------------------
249 Place.__index = Place
251 function Place.new(object, geom_func)
252 local self = setmetatable({}, Place)
254 self.geom_func = geom_func
256 self.admin_level = tonumber(self.object.tags.admin_level or 15) or 15
257 if self.admin_level == nil
258 or self.admin_level <= 0 or self.admin_level > 15
259 or math.floor(self.admin_level) ~= self.admin_level then
260 self.admin_level = 15
264 self.has_name = false
271 local has_main_tags = false
272 for k, v in pairs(self.object.tags) do
273 local group = lookup_prefilter_classification(k, v)
274 if group == 'extra' then
275 self.extratags[k] = v
276 elseif group ~= 'delete' then
284 if not has_main_tags then
285 -- no interesting tags, don't bother processing
292 function Place:clean(data)
293 for k, v in pairs(self.intags) do
294 if data.delete ~= nil and data.delete(k, v) then
296 elseif data.extra ~= nil and data.extra(k, v) then
297 self.extratags[k] = v
303 function Place:delete(data)
304 if data.match ~= nil then
305 for k, v in pairs(self.intags) do
306 if data.match(k, v) then
313 function Place:grab_extratags(data)
316 if data.match ~= nil then
317 for k, v in pairs(self.intags) do
318 if data.match(k, v) then
320 self.extratags[k] = v
329 local function strip_address_prefix(k)
330 if k:sub(1, 5) == 'addr:' then
334 if k:sub(1, 6) == 'is_in:' then
342 function Place:grab_address_parts(data)
345 if data.groups ~= nil then
346 for k, v in pairs(self.intags) do
347 local atype = data.groups(k, v)
350 if atype == 'main' then
352 self.address[strip_address_prefix(k)] = v
354 elseif atype == 'extra' then
355 self.address[strip_address_prefix(k)] = v
357 self.address[atype] = v
368 function Place:grab_name_parts(data)
371 if data.groups ~= nil then
372 for k, v in pairs(self.intags) do
373 local atype = data.groups(k, v)
378 if atype == 'main' then
380 elseif atype == 'house' then
382 fallback = {'place', 'house', PlaceTransform.always}
392 function Place:write_place(k, v, mfunc)
393 v = v or self.intags[k]
398 local place = mfunc(self, k, v)
400 local res = place:write_row(k, v)
401 self.num_entries = self.num_entries + res
408 function Place:write_row(k, v)
409 if self.geometry == nil then
410 self.geometry = self.geom_func(self.object)
412 if self.geometry:is_null() then
416 local extratags = EXTRATAGS_FILTER(self, k, v)
417 if not (extratags and next(extratags)) then
424 admin_level = self.admin_level,
425 name = next(self.names) and self.names,
426 address = next(self.address) and self.address,
427 extratags = extratags,
428 geometry = self.geometry
435 function Place:clone(data)
436 local cp = setmetatable({}, Place)
437 cp.object = self.object
438 cp.geometry = data.geometry or self.geometry
439 cp.geom_func = self.geom_func
440 cp.intags = data.intags or self.intags
441 cp.admin_level = data.admin_level or self.admin_level
442 cp.names = data.names or self.names
443 cp.address = data.address or self.address
444 cp.extratags = data.extratags or self.extratags
450 function module.tag_match(data)
451 if data == nil or next(data) == nil then
455 local fullmatches = {}
456 local key_prefixes = {}
457 local key_suffixes = {}
459 if data.keys ~= nil then
460 for _, key in pairs(data.keys) do
461 if key:sub(1, 1) == '*' then
463 if key_suffixes[#key - 1] == nil then
464 key_suffixes[#key - 1] = {}
466 key_suffixes[#key - 1][key:sub(2)] = true
468 elseif key:sub(#key, #key) == '*' then
469 if key_prefixes[#key - 1] == nil then
470 key_prefixes[#key - 1] = {}
472 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
474 fullmatches[key] = true
479 if data.tags ~= nil then
480 for k, vlist in pairs(data.tags) do
481 if fullmatches[k] == nil then
483 for _, v in pairs(vlist) do
484 fullmatches[k][v] = true
490 return function (k, v)
491 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
495 for slen, slist in pairs(key_suffixes) do
496 if #k >= slen and slist[k:sub(-slen)] ~= nil then
501 for slen, slist in pairs(key_prefixes) do
502 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
512 function module.tag_group(data)
513 if data == nil or next(data) == nil then
517 local fullmatches = {}
518 local key_prefixes = {}
519 local key_suffixes = {}
521 for group, tags in pairs(data) do
522 for _, key in pairs(tags) do
523 if key:sub(1, 1) == '*' then
525 if key_suffixes[#key - 1] == nil then
526 key_suffixes[#key - 1] = {}
528 key_suffixes[#key - 1][key:sub(2)] = group
530 elseif key:sub(#key, #key) == '*' then
531 if key_prefixes[#key - 1] == nil then
532 key_prefixes[#key - 1] = {}
534 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
536 fullmatches[key] = group
542 local val = fullmatches[k]
547 for slen, slist in pairs(key_suffixes) do
549 val = slist[k:sub(-slen)]
556 for slen, slist in pairs(key_prefixes) do
558 val = slist[k:sub(1, slen)]
567 -- Returns prefix part of the keys, and reject suffix matching keys
568 local function process_key(key)
569 if key:sub(1, 1) == '*' then
572 if key:sub(#key, #key) == '*' then
573 return key:sub(1, #key - 2)
578 -- Process functions for all data types
579 function module.process_node(object)
581 local function geom_func(o)
585 module.process_tags(Place.new(object, geom_func))
588 function module.process_way(object)
590 local function geom_func(o)
591 local geom = o:as_polygon()
593 if geom:is_null() then
594 geom = o:as_linestring()
600 module.process_tags(Place.new(object, geom_func))
603 function module.process_relation(object)
604 local geom_func = module.RELATION_TYPES[object.tags.type]
606 if geom_func ~= nil then
607 module.process_tags(Place.new(object, geom_func))
611 -- The process functions are used by default by osm2pgsql.
613 themepark:add_proc('node', module.process_node)
614 themepark:add_proc('way', module.process_way)
615 themepark:add_proc('relation', module.process_relation)
617 osm2pgsql.process_node = module.process_node
618 osm2pgsql.process_way = module.process_way
619 osm2pgsql.process_relation = module.process_relation
622 function module.process_tags(o)
623 if next(o.intags) == nil then
624 return -- shortcut when pre-filtering has removed all tags
627 -- Exception for boundary/place double tagging
628 if o.intags.boundary == 'administrative' then
629 o:grab_extratags{match = function (k, v)
630 return k == 'place' and v:sub(1,3) ~= 'isl'
635 local fallback = o:grab_name_parts{groups=NAME_FILTER}
638 if o:grab_address_parts{groups=ADDRESS_FILTER} > 0 and fallback == nil then
639 fallback = {'place', 'house', PlaceTransform.always}
641 if o.address.country ~= nil and #o.address.country ~= 2 then
642 o.address['country'] = nil
644 if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
645 fallback = {'place', 'postcode', PlaceTransform.always}
648 if o.address.interpolation ~= nil then
649 o:write_place('place', 'houses', PlaceTransform.always)
654 for k, v in pairs(o.intags) do
655 local ktable = MAIN_KEYS[k]
657 local ktype = ktable[v] or ktable[1]
658 if type(ktype) == 'function' then
659 o:write_place(k, v, ktype)
660 elseif ktype == 'fallback' and o.has_name then
661 fallback = {k, v, PlaceTransform.named}
666 if fallback ~= nil and o.num_entries == 0 then
667 o:write_place(fallback[1], fallback[2], fallback[3])
671 --------- Convenience functions for simple style configuration -----------------
673 function module.set_prefilters(data)
674 remove_group_from_main('delete')
675 merge_filters_into_main('delete', data.delete_keys, data.delete_tags)
677 remove_group_from_main('extra')
678 merge_filters_into_main('extra', data.extra_keys, data.extra_tags)
680 PRE_FILTER = {prefix = {}, suffix = {}}
681 add_pre_filter{delete = data.delete_keys, extra = data.extra_keys}
685 function module.ignore_keys(data)
686 if type(data) == 'string' then
688 data = PRESETS.IGNORE_KEYS[data]
690 error('Unknown preset for ignored keys: ' .. preset)
693 merge_filters_into_main('delete', data)
694 add_pre_filter{delete = data}
698 function module.add_for_extratags(data)
699 if type(data) == 'string' then
701 data = PRESETS.EXTRATAGS[data] or PRESETS.IGNORE_KEYS[data]
703 error('Unknown preset for extratags: ' .. preset)
706 merge_filters_into_main('extra', data)
707 add_pre_filter{extra = data}
711 function module.set_main_tags(data)
712 for key, values in pairs(MAIN_KEYS) do
713 for _, ttype in pairs(values) do
714 if ttype == 'fallback' or type(ttype) == 'function' then
718 if next(values) == nil then
722 module.modify_main_tags(data)
726 function module.modify_main_tags(data)
727 if type(data) == 'string' then
729 if data:sub(1, 7) == 'street/' then
730 data = PRESETS.MAIN_TAGS_STREETS[data:sub(8)]
731 elseif data:sub(1, 4) == 'poi/' then
732 data = PRESETS.MAIN_TAGS_POIS(data:sub(5))
734 data = PRESETS.MAIN_TAGS[data]
737 error('Unknown preset for main tags: ' .. preset)
741 for k, v in pairs(data) do
742 if MAIN_KEYS[k] == nil then
745 if type(v) == 'function' then
747 elseif type(v) == 'string' then
748 MAIN_KEYS[k][1] = PlaceTransform[v]
749 elseif type(v) == 'table' then
750 for subk, subv in pairs(v) do
751 if type(subv) == 'function' then
752 MAIN_KEYS[k][subk] = subv
754 MAIN_KEYS[k][subk] = PlaceTransform[subv]
762 function module.modify_name_tags(data)
763 if type(data) == 'string' then
765 data = PRESETS.NAME_TAGS[data]
767 error('Unknown preset for name keys: ' .. preset)
771 for k,v in pairs(data) do
778 NAME_FILTER = module.tag_group(NAMES)
779 remove_group_from_main('fallback:name')
780 if data.house ~= nil then
781 merge_filters_into_main('fallback:name', data.house)
786 function module.set_name_tags(data)
788 module.modify_name_tags(data)
792 function module.set_address_tags(data)
794 module.modify_address_tags(data)
798 function module.modify_address_tags(data)
799 if type(data) == 'string' then
801 data = PRESETS.ADDRESS_TAGS[data]
803 error('Unknown preset for address keys: ' .. preset)
807 for k, v in pairs(data) do
808 if k == 'postcode_fallback' then
809 POSTCODE_FALLBACK = v
810 elseif next(v) == nil then
811 ADDRESS_TAGS[k] = nil
817 ADDRESS_FILTER = module.tag_group(ADDRESS_TAGS)
819 remove_group_from_main('fallback:address')
820 merge_filters_into_main('fallback:address', data.main)
821 merge_filters_into_main('fallback:address', data.interpolation)
822 remove_group_from_main('fallback:postcode')
823 if POSTCODE_FALLBACK then
824 merge_filters_into_main('fallback:postcode', data.postcode)
829 function module.set_address_tags(data)
830 ADDRESS_TAGS_SOURCE = {}
831 module.modify_address_tags(data)
835 function module.set_postcode_fallback(enable)
836 if POSTCODE_FALLBACK ~= enable then
837 remove_group_from_main('fallback:postcode')
839 merge_filters_into_main('fallback:postcode', ADDRESS_TAGS.postcode)
842 POSTCODE_FALLBACK = enable
846 function module.set_unused_handling(data)
847 if type(data) == 'function' then
848 EXTRATAGS_FILTER = data
849 elseif data == nil then
850 EXTRATAGS_FILTER = default_extratags_filter
851 elseif data.extra_keys == nil and data.extra_tags == nil then
852 local delfilter = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
853 EXTRATAGS_FILTER = function (p, k)
855 for kin, vin in pairs(p.intags) do
856 if kin ~= k and not delfilter(kin, vin) then
860 if next(extra) == nil then
863 for kextra, vextra in pairs(p.extratags) do
864 extra[kextra] = vextra
868 elseif data.delete_keys == nil and data.delete_tags == nil then
869 local incfilter = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
870 EXTRATAGS_FILTER = function (p, k)
872 for kin, vin in pairs(p.intags) do
873 if kin ~= k and incfilter(kin, vin) then
877 if next(extra) == nil then
880 for kextra, vextra in pairs(p.extratags) do
881 extra[kextra] = vextra
886 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
890 function module.set_relation_types(data)
891 module.RELATION_TYPES = {}
893 if v == 'multipolygon' then
894 module.RELATION_TYPES[k] = module.relation_as_multipolygon
895 elseif v == 'multiline' then
896 module.RELATION_TYPES[k] = module.relation_as_multiline
902 function module.get_taginfo()
903 return {main = MAIN_KEYS, name = NAMES, address = ADDRESS_TAGS}