]> git.openstreetmap.org Git - nominatim.git/blob - lib-lua/themes/nominatim/init.lua
release 5.2.0.post7
[nominatim.git] / lib-lua / themes / nominatim / init.lua
1 -- Nominatim themepark theme.
2 --
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.
7 --
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:
11 --
12 --      local nominatim = themepark:init_theme('nominatim')
13 --      nominatim.set_main_tags{boundary = 'always'}
14 --
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:
18 --
19 --      themepark:add_topic('nominatim/full')
20 --      local nominatim = themepark:init_theme('nominatim')
21 --      nominatim.ignore_tags{'amenity'}
22
23 local module = {}
24
25 local MAIN_KEYS = {admin_level = {'delete'}}
26 local PRE_FILTER = {prefix = {}, suffix = {}}
27 local NAMES = {}
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
35
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.
40 local themepark = ...
41 if type(themepark) ~= 'table' then
42     themepark = nil
43 end
44
45 -- The place tables carry the raw OSM information.
46 local table_definitions = {
47     place = {
48         ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
49         columns = {
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 },
57         },
58         indexes = {}
59     },
60     place_entrance = {
61         ids = { type = 'node', id_column = 'osm_id' },
62         columns = {
63             { column = 'type', type = 'text', not_null = true },
64             { column = 'extratags', type = 'hstore' },
65             { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true }
66         },
67         indexes = {}
68     },
69     place_postcode = {
70         ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
71         columns = {
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' }
76         },
77         indexes = {
78             { column = 'postcode', method = 'btree' }
79         }
80      }
81 }
82
83 local insert_row = {}
84 local script_path = debug.getinfo(1, "S").source:match("@?(.*/)")
85 local PRESETS = loadfile(script_path .. 'presets.lua')()
86
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")
91
92     if themepark then
93         themepark:add_table(table_definition)
94         insert_row[table_name] = function(columns)
95             themepark:insert(table_name, columns, {}, {})
96         end
97     else
98         local place_table = osm2pgsql.define_table(table_definition)
99         insert_row[table_name] = function(columns)
100             place_table:insert(columns)
101         end
102     end
103 end
104
105 ------------ Geometry functions for relations ---------------------
106
107 function module.relation_as_multipolygon(o)
108     return o:as_multipolygon()
109 end
110
111 function module.relation_as_multiline(o)
112     return o:as_multilinestring():line_merge()
113 end
114
115
116 module.RELATION_TYPES = {
117     multipolygon = module.relation_as_multipolygon,
118     boundary = module.relation_as_multipolygon,
119     waterway = module.relation_as_multiline
120 }
121
122 --------- Built-in place transformation functions --------------------------
123
124 local PlaceTransform = {}
125
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'
131
132 -- always: unconditionally use that place
133 function PlaceTransform.always(place)
134     return place
135 end
136
137 -- never: unconditionally drop the place
138 function PlaceTransform.never()
139     return nil
140 end
141
142 -- named: use the place if it has a fully-qualified name
143 function PlaceTransform.named(place)
144     if place.has_name then
145         return place
146     end
147 end
148
149 -- named_with_key: use place if there is a name with the main key prefix
150 function PlaceTransform.named_with_key(place, k)
151     local names = {}
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
158         end
159     end
160
161     if next(names) ~= nil then
162         return place:clone{names=names}
163     end
164 end
165
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
170         return place
171     end
172
173     local names = {}
174     for k, v in pairs(place.names) do
175         if NAME_FILTER(k, v) == 'house' then
176             names[k] = v
177         end
178     end
179     return place:clone{names=names}
180 end
181
182 ----------------- other helper functions -----------------------------
183
184 local function lookup_prefilter_classification(k, v)
185     -- full matches
186     local desc = MAIN_KEYS[k]
187     local fullmatch = desc and (desc[v] or desc[1])
188     if fullmatch ~= nil then
189         return fullmatch
190     end
191     -- suffixes
192     for slen, slist in pairs(PRE_FILTER.suffix) do
193         if #k >= slen then
194             local group = slist[k:sub(-slen)]
195             if group ~= nil then
196                 return group
197             end
198         end
199     end
200     -- prefixes
201     for slen, slist in pairs(PRE_FILTER.prefix) do
202         if #k >= slen then
203             local group = slist[k:sub(1, slen)]
204             if group ~= nil then
205                 return group
206             end
207         end
208     end
209 end
210
211
212 local function merge_filters_into_main(group, keys, tags)
213     if keys ~= nil then
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
218                     MAIN_KEYS[key] = {}
219                 end
220                 MAIN_KEYS[key][1] = group
221             end
222         end
223     end
224
225     if tags ~= nil then
226         for key, values in pairs(tags) do
227             if MAIN_KEYS[key] == nil then
228                 MAIN_KEYS[key] = {}
229             end
230             for _, v in pairs(values) do
231                 MAIN_KEYS[key][v] = group
232             end
233         end
234     end
235 end
236
237
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
242                 values[ttype] = nil
243             end
244         end
245         if next(values) == nil then
246             MAIN_KEYS[key] = nil
247         end
248     end
249 end
250
251
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
257                 if klen > 0 then
258                     if PRE_FILTER.suffix[klen] == nil then
259                         PRE_FILTER.suffix[klen] = {}
260                     end
261                     PRE_FILTER.suffix[klen][key:sub(2)] = group
262                 end
263             elseif key:sub(#key, #key) == '*' then
264                 if PRE_FILTER.prefix[klen] == nil then
265                     PRE_FILTER.prefix[klen] = {}
266                 end
267                 PRE_FILTER.prefix[klen][key:sub(1, klen)] = group
268             end
269         end
270     end
271 end
272
273 ------------- Place class ------------------------------------------
274
275 local Place = {}
276 Place.__index = Place
277
278 function Place.new(object, geom_func)
279     local self = setmetatable({}, Place)
280     self.object = object
281     self.geom_func = geom_func
282
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
288     end
289
290     self.num_entries = 0
291     self.has_name = false
292     self.names = {}
293     self.address = {}
294     self.extratags = {}
295
296     self.intags = {}
297
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
304             self.intags[k] = v
305             if group ~= nil then
306                 has_main_tags = true
307             end
308         end
309     end
310
311     if not has_main_tags then
312         -- no interesting tags, don't bother processing
313         self.intags = {}
314     end
315
316     return self
317 end
318
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
322             self.intags[k] = nil
323         elseif data.extra ~= nil and data.extra(k, v) then
324             self.extratags[k] = v
325             self.intags[k] = nil
326         end
327     end
328 end
329
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
334                 self.intags[k] = nil
335             end
336         end
337     end
338 end
339
340 function Place:grab_extratags(data)
341     local count = 0
342
343     if data.match ~= nil then
344         for k, v in pairs(self.intags) do
345             if data.match(k, v) then
346                 self.intags[k] = nil
347                 self.extratags[k] = v
348                 count = count + 1
349             end
350         end
351     end
352
353     return count
354 end
355
356 local function strip_address_prefix(k)
357     if k:sub(1, 5) == 'addr:' then
358         return k:sub(6)
359     end
360
361     if k:sub(1, 6) == 'is_in:' then
362         return k:sub(7)
363     end
364
365     return k
366 end
367
368
369 function Place:grab_address_parts(data)
370     local count = 0
371
372     if data.groups ~= nil then
373         for k, v in pairs(self.intags) do
374             local atype = data.groups(k, v)
375
376             if atype ~= nil then
377                 if atype == 'main' then
378                     self.has_name = true
379                     self.address[strip_address_prefix(k)] = v
380                     count = count + 1
381                 elseif atype == 'extra' then
382                     self.address[strip_address_prefix(k)] = v
383                 else
384                     self.address[atype] = v
385                 end
386                 self.intags[k] = nil
387             end
388         end
389     end
390
391     return count
392 end
393
394
395 function Place:grab_name_parts(data)
396     local fallback = nil
397
398     if data.groups ~= nil then
399         for k, v in pairs(self.intags) do
400             local atype = data.groups(k, v)
401
402             if atype ~= nil then
403                 self.names[k] = v
404                 self.intags[k] = nil
405                 if atype == 'main' then
406                     self.has_name = true
407                 elseif atype == 'house' then
408                     self.has_name = true
409                     fallback = {'place', 'house', address_fallback}
410                 end
411             end
412         end
413     end
414
415     return fallback
416 end
417
418
419 function Place:write_place(k, v, mfunc)
420     v = v or self.intags[k]
421     if v == nil then
422         return 0
423     end
424
425     local place = mfunc(self, k, v)
426     if place then
427         local res = place:write_row(k, v)
428         self.num_entries = self.num_entries + res
429         return res
430     end
431
432     return 0
433 end
434
435
436 function Place:geometry_is_valid()
437     if self.geometry == nil then
438         self.geometry = self.geom_func(self.object)
439
440         if self.geometry == nil or self.geometry:is_null() then
441             self.geometry = false
442             return false
443         end
444
445         return true
446     end
447
448     return self.geometry ~= false
449 end
450
451
452 function Place:write_row(k, v)
453     if not self:geometry_is_valid() then
454         return 0
455     end
456
457      local extra = EXTRATAGS_FILTER(self, k, v) or {}
458
459      for tk, tv in pairs(self.object.tags) do
460          if REQUIRED_EXTRATAGS_FILTER(tk, tv) and extra[tk] == nil then
461              extra[tk] = tv
462          end
463      end
464
465      if extra and next(extra) == nil then
466          extra = nil
467      end
468
469     insert_row.place{
470         class = k,
471         type = v,
472         admin_level = self.admin_level,
473         name = next(self.names) and self.names,
474         address = next(self.address) and self.address,
475         extratags = extra,
476         geometry = self.geometry
477     }
478
479     return 1
480 end
481
482
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
493
494     return cp
495 end
496
497
498 function module.tag_match(data)
499     if data == nil or next(data) == nil then
500         return nil
501     end
502
503     local fullmatches = {}
504     local key_prefixes = {}
505     local key_suffixes = {}
506
507     if data.keys ~= nil then
508         for _, key in pairs(data.keys) do
509             if key:sub(1, 1) == '*' then
510                 if #key > 1 then
511                     if key_suffixes[#key - 1] == nil then
512                         key_suffixes[#key - 1] = {}
513                     end
514                     key_suffixes[#key - 1][key:sub(2)] = true
515                 end
516             elseif key:sub(#key, #key) == '*' then
517                 if key_prefixes[#key - 1] == nil then
518                     key_prefixes[#key - 1] = {}
519                 end
520                 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
521             else
522                 fullmatches[key] = true
523             end
524         end
525     end
526
527     if data.tags ~= nil then
528         for k, vlist in pairs(data.tags) do
529             if fullmatches[k] == nil then
530                 fullmatches[k] = {}
531                 for _, v in pairs(vlist) do
532                     fullmatches[k][v] = true
533                 end
534             end
535         end
536     end
537
538     return function (k, v)
539         if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
540             return true
541         end
542
543         for slen, slist in pairs(key_suffixes) do
544             if #k >= slen and slist[k:sub(-slen)] ~= nil then
545                 return true
546             end
547         end
548
549         for slen, slist in pairs(key_prefixes) do
550             if #k >= slen and slist[k:sub(1, slen)] ~= nil then
551                 return true
552             end
553         end
554
555         return false
556     end
557 end
558
559
560 function module.tag_group(data)
561     if data == nil or next(data) == nil then
562         return nil
563     end
564
565     local fullmatches = {}
566     local key_prefixes = {}
567     local key_suffixes = {}
568
569     for group, tags in pairs(data) do
570         for _, key in pairs(tags) do
571             if key:sub(1, 1) == '*' then
572                 if #key > 1 then
573                     if key_suffixes[#key - 1] == nil then
574                         key_suffixes[#key - 1] = {}
575                     end
576                     key_suffixes[#key - 1][key:sub(2)] = group
577                 end
578             elseif key:sub(#key, #key) == '*' then
579                 if key_prefixes[#key - 1] == nil then
580                     key_prefixes[#key - 1] = {}
581                 end
582                 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
583             else
584                 fullmatches[key] = group
585             end
586         end
587     end
588
589     return function (k)
590         local val = fullmatches[k]
591         if val ~= nil then
592             return val
593         end
594
595         for slen, slist in pairs(key_suffixes) do
596             if #k >= slen then
597                 val = slist[k:sub(-slen)]
598                 if val ~= nil then
599                     return val
600                 end
601             end
602         end
603
604         for slen, slist in pairs(key_prefixes) do
605             if #k >= slen then
606                 val = slist[k:sub(1, slen)]
607                 if val ~= nil then
608                     return val
609                 end
610             end
611         end
612     end
613 end
614
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
618         return nil
619     end
620     if key:sub(#key, #key) == '*' then
621         return key:sub(1, #key - 2)
622     end
623     return key
624 end
625
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()
635             }
636         end
637     end
638
639     local function geom_func(o)
640         return o:as_point()
641     end
642
643     module.process_tags(Place.new(object, geom_func))
644 end
645
646 function module.process_way(object)
647
648     local function geom_func(o)
649         local geom = o:as_polygon()
650
651         if geom:is_null() then
652             geom = o:as_linestring()
653             if geom:is_null() or geom:length() > 30 then
654                 return nil
655             end
656         end
657
658         return geom
659     end
660
661     module.process_tags(Place.new(object, geom_func))
662 end
663
664 function module.process_relation(object)
665     local geom_func = module.RELATION_TYPES[object.tags.type]
666
667     if geom_func ~= nil then
668         module.process_tags(Place.new(object, geom_func))
669     end
670 end
671
672 -- The process functions are used by default by osm2pgsql.
673 if themepark then
674     themepark:add_proc('node', module.process_node)
675     themepark:add_proc('way', module.process_way)
676     themepark:add_proc('relation', module.process_relation)
677 else
678     osm2pgsql.process_node = module.process_node
679     osm2pgsql.process_way = module.process_way
680     osm2pgsql.process_relation = module.process_relation
681 end
682
683 function module.process_tags(o)
684     if next(o.intags) == nil then
685         return  -- shortcut when pre-filtering has removed all tags
686     end
687
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'
692         end}
693     end
694
695     -- name keys
696     local fallback = o:grab_name_parts{groups=NAME_FILTER}
697
698     -- address keys
699     if o:grab_address_parts{groups=ADDRESS_FILTER} > 0 and fallback == nil then
700         fallback = {'place', 'house', address_fallback}
701     end
702     if o.address.country ~= nil and #o.address.country ~= 2 then
703         o.address['country'] = nil
704     end
705
706     if o.address.interpolation ~= nil then
707         o:write_place('place', 'houses', PlaceTransform.always)
708         return
709     end
710
711     -- collect main keys
712     local postcode_collect = false
713     for k, v in pairs(o.intags) do
714         local ktable = MAIN_KEYS[k]
715         if ktable then
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
728                     }
729                 end
730             elseif ktype == 'fallback' and o.has_name then
731                 fallback = {k, v, PlaceTransform.named}
732             end
733         end
734     end
735
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()
745             }
746         end
747     end
748 end
749
750 --------- Extratags post-processing functions ---------------
751
752 local function default_extratags_filter(p, k)
753     return p.extratags
754 end
755
756 EXTRATAGS_FILTER = default_extratags_filter
757 REQUIRED_EXTRATAGS_FILTER = module.tag_match(PRESETS.EXTRATAGS)
758
759 --------- Convenience functions for simple style configuration -----------------
760
761 function module.set_prefilters(data)
762     remove_group_from_main('delete')
763     merge_filters_into_main('delete', data.delete_keys, data.delete_tags)
764
765     remove_group_from_main('extra')
766     merge_filters_into_main('extra', data.extra_keys, data.extra_tags)
767
768     PRE_FILTER = {prefix = {}, suffix = {}}
769     add_pre_filter{delete = data.delete_keys, extra = data.extra_keys}
770 end
771
772
773 function module.ignore_keys(data)
774     if type(data) == 'string' then
775         local preset = data
776         data = PRESETS.IGNORE_KEYS[data]
777         if data == nil then
778             error('Unknown preset for ignored keys: ' .. preset)
779         end
780     end
781     merge_filters_into_main('delete', data)
782     add_pre_filter{delete = data}
783 end
784
785
786 function module.add_for_extratags(data)
787     if type(data) == 'string' then
788         local preset = data
789         data = PRESETS.IGNORE_KEYS[data]
790         if data == nil then
791             error('Unknown preset for extratags: ' .. preset)
792         end
793     end
794     merge_filters_into_main('extra', data)
795     add_pre_filter{extra = data}
796 end
797
798
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
803                 values[ttype] = nil
804             end
805         end
806         if next(values) == nil then
807             MAIN_KEYS[key] = nil
808         end
809     end
810     module.modify_main_tags(data)
811 end
812
813
814 function module.modify_main_tags(data)
815     if type(data) == 'string' then
816         local preset = data
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))
821         else
822             data = PRESETS.MAIN_TAGS[data]
823         end
824         if data == nil then
825             error('Unknown preset for main tags: ' .. preset)
826         end
827     end
828
829     for k, v in pairs(data) do
830         if MAIN_KEYS[k] == nil then
831             MAIN_KEYS[k] = {}
832         end
833         if type(v) == 'function' then
834             MAIN_KEYS[k][1] = v
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
841                 else
842                     MAIN_KEYS[k][subk] = PlaceTransform[subv]
843                 end
844             end
845         end
846     end
847 end
848
849
850 function module.modify_name_tags(data)
851     if type(data) == 'string' then
852         local preset = data
853         data = PRESETS.NAME_TAGS[data]
854         if data == nil then
855             error('Unknown preset for name keys: ' .. preset)
856         end
857     end
858
859     for k,v in pairs(data) do
860         if next(v) then
861             NAMES[k] = v
862         else
863             NAMES[k] = nil
864         end
865     end
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)
870     end
871 end
872
873
874 function module.set_name_tags(data)
875     NAMES = {}
876     module.modify_name_tags(data)
877 end
878
879
880 function module.set_address_tags(data)
881     ADDRESS_TAGS = {}
882     module.modify_address_tags(data)
883 end
884
885
886 function module.modify_address_tags(data)
887     if type(data) == 'string' then
888         local preset = data
889         data = PRESETS.ADDRESS_TAGS[data]
890         if data == nil then
891             error('Unknown preset for address keys: ' .. preset)
892         end
893     end
894
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
900         else
901             ADDRESS_TAGS[k] = v
902         end
903     end
904
905     ADDRESS_FILTER = module.tag_group(ADDRESS_TAGS)
906
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)
913     end
914 end
915
916
917 function module.set_address_tags(data)
918     ADDRESS_TAGS_SOURCE = {}
919     module.modify_address_tags(data)
920 end
921
922
923 function module.set_postcode_fallback(enable)
924     if POSTCODE_FALLBACK ~= enable then
925         remove_group_from_main('fallback:postcode')
926         if enable then
927             merge_filters_into_main('fallback:postcode', ADDRESS_TAGS.postcode)
928         end
929     end
930     POSTCODE_FALLBACK = enable
931 end
932
933
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)
942             local extra = {}
943             for kin, vin in pairs(p.intags) do
944                 if kin ~= k and not delfilter(kin, vin) then
945                     extra[kin] = vin
946                 end
947             end
948             if next(extra) == nil then
949                 return p.extratags
950             end
951             for kextra, vextra in pairs(p.extratags) do
952                 extra[kextra] = vextra
953             end
954             return extra
955         end
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)
959             local extra = {}
960             for kin, vin in pairs(p.intags) do
961                 if kin ~= k and incfilter(kin, vin) then
962                     extra[kin] = vin
963                 end
964             end
965             if next(extra) == nil then
966                 return p.extratags
967             end
968             for kextra, vextra in pairs(p.extratags) do
969                 extra[kextra] = vextra
970             end
971             return extra
972         end
973     else
974         error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
975     end
976 end
977
978 function module.set_relation_types(data)
979     module.RELATION_TYPES = {}
980     for k, v in data do
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
985         end
986     end
987 end
988
989 function module.set_entrance_filter(data)
990     if data == nil or type(data) == 'function' then
991         ENTRANCE_FUNCTION = data
992         return nil
993     end
994
995     if type(data) == 'string' then
996         local preset = data
997         data = PRESETS.ENTRANCE_TABLE[data]
998         if data == nil then
999             error('Unknown preset for entrance table: ' .. preset)
1000         end
1001     end
1002
1003     ENTRANCE_FUNCTION = nil
1004
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]}
1012                     end
1013                 end
1014                 return nil
1015             end
1016         else
1017             if data.extra_include ~= nil then
1018                 local tags = {}
1019                 for _, v in pairs(data.extra_include) do
1020                     tags[v] = true
1021                 end
1022                 if data.extra_exclude ~= nil then
1023                     for _, v in pairs(data.extra_exclude) do
1024                         tags[v] = nil
1025                     end
1026                 end
1027                 for _, v in pairs(data.main_tags) do
1028                     tags[v] = nil
1029                 end
1030
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]
1035                             local extra = {}
1036                             for k, v in pairs(tags) do
1037                                 extra[k] = o.tags[k]
1038                             end
1039                             if next(extra) == nil then
1040                                 extra = nil
1041                             end
1042                             return {entrance = entrance, extratags = extra}
1043                         end
1044                     end
1045
1046                     return nil
1047                 end
1048             else
1049                 local notags = {}
1050                 if data.extra_exclude ~= nil then
1051                     for _, v in pairs(data.extra_exclude) do
1052                         notags[v] = 1
1053                     end
1054                 end
1055                 for _, v in pairs(data.main_tags) do
1056                     notags[v] = 1
1057                 end
1058
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]
1063                             local extra = {}
1064                             for k, v in pairs(o.tags) do
1065                                 if notags[k] ~= 1 then
1066                                     extra[k] = v
1067                                 end
1068                             end
1069                             if next(extra) == nil then
1070                                 extra = nil
1071                             end
1072                             return {entrance = entrance, extratags = extra}
1073                         end
1074                     end
1075
1076                     return nil
1077                 end
1078             end
1079         end
1080     end
1081 end
1082
1083 function module.get_taginfo()
1084     return {main = MAIN_KEYS, name = NAMES, address = ADDRESS_TAGS}
1085 end
1086
1087 return module