]> git.openstreetmap.org Git - nominatim.git/blob - docs/customize/Import-Styles.md
add documentation for flex style
[nominatim.git] / docs / customize / Import-Styles.md
1 ## Configuring the Import
2
3 In the very first step of a Nominatim import, OSM data is loaded into the
4 database. Nominatim uses [osm2pgsql](https://osm2pgsql.org) for this task.
5 It comes with a [flex style](https://osm2pgsql.org/doc/manual.html#the-flex-output)
6 specifically tailored to filter and convert OSM data into Nominatim's
7 internal data representation.
8
9 There are a number of default configurations for the flex style which
10 result in geocoding databases of different detail. The
11 [Import section](../admin/Import.md#filtering-imported-data) explains
12 these default configurations in detail.
13
14 You can also create your own custom style. Put the style file into your
15 project directory and then set `NOMINATIM_IMPORT_STYLE` to the name of the file.
16 It is always recommended to start with one of the standard styles and customize
17 those. You find the standard styles under the name `import-<stylename>.lua`
18 in the standard Nominatim configuration path (usually `/etc/nominatim` or
19 `/usr/local/etc/nominatim`).
20
21 The remainder of the page describes how the flex style works and how to
22 customize it.
23
24 ### The `flex-base.lua` module
25
26 The core of Nominatim's flex import configuration is the `flex-base` module.
27 It defines the table layout used by Nominatim and provides standard
28 implementations for the import callbacks that make it easy to customize
29 how OSM tags are used by Nominatim.
30
31 Every custom style should include this module to make sure that the correct
32 tables are created. Thus start your custom style as follows:
33
34 ``` lua
35 local flex = require('flex-base')
36
37 ```
38
39 The following sections explain how the module can be customized.
40
41
42 ### Changing the recognized tags
43
44 If you just want to change which OSM tags are recognized during import,
45 then there are a number of convenience functions to set the tag lists used
46 during the processing.
47
48 !!! warning
49     There are no built-in defaults for the tag lists, so all the functions
50     need to be called from your style script to fully process the data.
51     Make sure you start from one of the default style and only modify
52     the data you are interested in.
53
54 Many of the following functions take _key match lists_. These lists can
55 contain three kinds of strings to match against tag keys:
56 A string that ends in an asterisk `*` is a prefix match and accordingly matches
57 against any key that starts with the given string (minus the `*`). 
58 A suffix match can be defined similarly with a string that starts with a `*`.
59 Any other string is matched exactly against tag keys.
60
61
62 #### `set_main_tags()` - principal tags
63
64 If a principal or main tag is found on an OSM object, then the object
65 is included in Nominatim's search index. A single object may also have
66 multiple main tags. In that case, the object will be included multiple
67 times in the index, once for each main tag.
68
69 The flex script distinguishes between four types of main tags:
70
71 * __always__: a main tag that is used unconditionally
72 * __named__: consider this main tag only, if the object has a proper name
73   (a reference is not enough, see below).
74 * __named_with_key__: consider this main tag only, when the object has
75   a proper name with a domain prefix. For example, if the main tag is
76   `bridge=yes`, then it will only be added as an extra row, if there is
77   a tag `bridge:name[:XXX]` for the same object. If this property is set,
78   all other names that are not domain-specific are ignored.
79 * __fallback__: use this main tag only, if there is no other main tag.
80   Fallback always implied `named`, i.e. fallbacks are only tried for
81   named objects.
82
83 The `set_main_tags()` function takes exactly one table parameter which
84 defines the keys and key/value combinations to include and the kind of
85 main tag. Each lua table key defines an OSM tag key. The value may
86 be a string defining the kind of main key as described above. Then the tag will
87 be considered a main tag for any possible value. To further restrict
88 which values are acceptable, give a table with the permitted values
89 and their kind of main tag. If the table contains a simple value without
90 key, then this is used as default for values that are not listed.
91
92 !!! example
93     ``` lua
94     flex.set_main_tags{
95         boundary = {'administrative' = named},
96         highway = {'always', street_lamp = 'named'}
97         landuse = 'fallback',
98     }
99     ```
100
101     In this example an object with a `boundary` tag will only be included
102     when it has a value of `administrative`. Objects with `highway` tags are
103     always included. However when the value is `street_lamp` then the object
104     must have a name, too. With any other value, the object is included
105     independently of the name. Finally, if a `landuse` tag is present then
106     it will be used independely of the concrete value if neither boundary
107     nor highway tags were found and the object is named.
108
109
110 #### `set_prefilters()` - ignoring tags
111
112 Pre-filtering of tags allows to ignore them for any further processing.
113 Thus pre-filtering takes precedence over any other tag processing. This is
114 useful when some specific key/value combinations need to be excluded from
115 processing. When tags are filtered, they may either be deleted completely
116 or moved to `extratags`. Extra tags are saved with the object and returned
117 to the user when requested, but are not used otherwise.
118
119 `set_prefilters()` takes a table with four optional fields:
120
121 * __delete_keys__ is a _key match list_ for tags that should be deleted
122 * __delete_tags__ contains a table of tag keys pointing to a list of tag
123   values. Tags with matching key/value pairs are deleted.
124 * __extra_keys__ is a _key match list_ for tags which should be saved into
125   extratags
126 * __delete_tags__ contains a table of tag keys pointing to a list of tag
127   values. Tags with matching key/value pairs are moved to extratags.
128
129 Key list may contain three kinds of strings:
130 A string that ends in an asterisk `*` is a prefix match and accordingly matches
131 against any key that starts with the given string (minus the `*`). 
132 A suffix match can be defined similarly with a string that starts with a `*`.
133 Any other string is matched exactly against tag keys.
134
135 !!! example
136     ``` lua
137     flex.set_prefilters{
138         delete_keys = {'source', 'source:*'},
139         extra_tags = {'amenity' = {'yes', 'no'}
140     }
141     flex.set_main_tags{
142         amenity = 'always'
143     }
144     ```
145
146     In this example any tags `source` and tags that begin with `source:`  are
147     deleted before any other processing is done. Getting rid of frequent tags
148     this way can speed up the import.
149
150     Tags with `amenity=yes` or `amenity=no` are moved to extratags. Later
151     all tags with an `amenity` key are made a main tag. This effectively means
152     that Nominatim will use all amenity tags except for those with value
153     yes and no.
154
155 #### `set_name_tags()` - defining names
156
157 The flex script distinguishes between two kinds of names:
158
159 * __main__: the primary names make an object fully searchable.
160   Main tags of type _named_ will only cause the object to be included when
161   such a primary name is present. Primary names are usually those found
162   in the `name` tag and its variants.
163 * __extra__: extra names are still added to the search index but they are
164   alone not sufficient to make an object named.
165
166 `set_name_tags()` takes a table with two optional fields `main` and `extra`.
167 They take _key match lists_ for main and extra names respectively.
168
169 !!! example
170     ``` lua
171     flex.set_main_tags{'highway' = {'traffic_light' = 'named'}}
172     flex.set_name_tags{main = {'name', 'name:*'},
173                        extra = {'ref'}
174                       }
175     ```
176
177     This example creates a search index over traffic lights but will
178     only include those that have a common name and not those which just
179     have some reference ID from the city.
180
181 #### `set_address_tags()` - defining address parts
182
183 Address tags will be used to build up the address of an object.
184
185 `set_address_tags()` takes a table with arbitrary fields pointing to
186 _key match lists_. To fields have a special meaning:
187
188 __main__ defines
189 the tags that make a full address object out of the OSM object. This
190 is usually the housenumber or variants thereof. If a main address tag
191 appears, then the object will always be included, if necessary with a
192 fallback of `place=house`. If the key has a prefix of `addr:` or `is_in:`
193 this will be stripped.
194
195 __extra__ defines all supplementary tags for addresses, tags like `addr:street`, `addr:city` etc. If the key has a prefix of `addr:` or `is_in:` this will be stripped.
196
197 All other fields will be handled as summary fields. If a key matches the
198 key match list, then its value will be added to the address tags with the
199 name of the field as key. If multiple tags match, then an arbitrary one
200 wins.
201
202 Country tags are handled slightly special. Only tags with a two-letter code
203 are accepted, all other values are discarded.
204
205 !!! example
206     ``` lua
207     flex.set_address_tags{
208         main = {'addr:housenumber'},
209         extra = {'addr:*'},
210         postcode = {'postal_code', 'postcode', 'addr:postcode'},
211         country = {'country-code', 'ISO3166-1'}
212     }
213     ```
214
215     In this example all tags which begin with `addr:` will be saved in
216     the address tag list. If one of the tags is `addr:housenumber`, the
217     object will fall back to be entered as a `place=house` in the database
218     unless there is another interested main tag to be found.
219
220     Tags with keys `country-code` and `ISO3166-1` are saved with their
221     value under `country` in the address tag list. The same thing happens
222     to postcodes, they will always be saved under the key `postcode` thus
223     normalizing the multitude of keys that are used in the OSM database.
224
225
226 #### `set_unused_handling()` - processing remaining tags
227
228 This function defines what to do with tags that remain after all tags
229 have been classified using the functions above. There are two ways in
230 which the function can be used:
231
232 `set_unused_handling(delete_keys = ..., delete_tags = ...)` deletes all
233 keys that match the descriptions in the parameters and moves all remaining
234 tags into the extratags list.
235 `set_unused_handling(extra_keys = ..., extra_tags = ...)` moves all tags
236 matching the parameters into the extratags list and then deletes the remaining
237 tags. For the format of the parameters see the description in `set_prefilters()`
238 above.
239
240 !!! example
241     ``` lua
242     flex.set_address_tags{
243         main = {'addr:housenumber'},
244         extra = {'addr:*', 'tiger:county'}
245     }
246     flex.set_unused_handling{delete_keys = {'tiger:*'}}
247     ```
248
249     In this example all remaining tags except those beginning with `tiger:`
250     are moved to the extratags list. Note that it is not possible to
251     already delete the tiger tags with `set_prefilters()` because that
252     would remove tiger:county before the address tags are processed.
253
254 ### Customizing osm2pgsql callbacks
255
256 osm2pgsql expects the flex style to implement three callbacks, one process
257 function per OSM type. If you want to implement special handling for
258 certain OSM types, you can override the default implementations provided
259 by the flex-base module.
260
261 #### Changing the relation types to be handled
262
263 The default scripts only allows relations of type `multipolygon`, `boundary`
264 and `waterway`. To add other types relations, set `RELATION_TYPES` for
265 the type to the kind of geometry that should be created. The following
266 kinds of geometries can be used:
267
268 * __relation_as_multipolygon__ creates a (Multi)Polygon from the ways in
269   the relation. If the ways do not form a valid area, then the object is
270   silently discarded.
271 * __relation_as_multiline__ creates a (Mutli)LineString from the ways in
272   the relations. Ways are combined as much as possible without any regards
273   to their order in the relation.
274
275 !!! Example
276     ``` lua
277     flex.RELATION_TYPES['site'] = flex.relation_as_multipolygon
278     ```
279
280     With this line relations of `type=site` will be included in the index
281     according to main tags found. This only works when the site relation
282     resolves to a valid area. Nodes in the site relation are not part of the
283     geometry.
284
285
286 #### Adding additional logic to processing functions
287
288 The default processing functions are also exported by the flex-base module
289 as `process_node`, `process_way` and `process_relation`. These can be used
290 to implement your own processing functions with some additional processing
291 logic.
292
293 !!! Example
294     ``` lua
295     function osm2pgsql.process_relation(object)
296         if object.tags.boundary ~= 'administrative' or object.tags.admin_level ~= '2' then
297           flex.process_relation(object)
298         end
299     end
300     ```
301
302     This example discards all country-level boundaries and uses standard
303     handling for everything else. This can be useful if you want to use
304     your own custom country boundaries.
305
306
307 ### Customizing the main processing function
308
309 The main processing function of the flex style can be found in the function
310 `process_tags`. This function is called for all OSM object kinds and is
311 responsible for filtering the tags and writing out the rows into Postgresql.
312
313 !!! Example
314     ``` lua
315     local original_process_tags = flex.process_tags
316
317     function flex.process_tags(o)
318         if o.object.tags.highway ~= nil and o.object.tags.access == 'no' then
319             return
320         end
321
322         original_process_tags(o)
323     end
324     ```
325
326     This example shows the most simple customization of the process_tags function.
327     It simply adds some additional processing before running the original code.
328     To do that, first save the original function and then overwrite process_tags
329     from the module. In this example all highways which are not accessible
330     by anyone will be ignored.
331
332
333 #### The `Place` class
334
335 The `process_tags` function receives a Lua object of `Place` type which comes
336 with some handy functions to collect the data necessary for geocoding and
337 writing it into the place table. Always use this object to fill the table.
338
339 The Place class has some attributes which you may access read-only:
340
341 * __object__ is the original OSM object data handed in by osm2pgsql
342 * __admin_level__ is the content of the admin_level tag, parsed into an
343   integer and normalized to a value between 0 and 15
344 * __has_name__ is a boolean indicating if the object has a full name
345 * __names__ is a table with the collected list of name tags
346 * __address__ is a table with the collected list of address tags
347 * __extratags__ is a table with the collected list of additional tags to save
348
349 There are a number of functions to fill these fields. All functions expect
350 a table parameter with fields as indicated in the description.
351 Many of these functions expect match functions which are described in detail
352 further below.
353
354 * __delete{match=...}__ removes all tags that match the match function given
355   in _match_.
356 * __grab_extratags{match=...}__ moves all tags that match the match function
357   given in _match_ into extratags. Returns the number of tags moved.
358 * __clean{delete=..., extra=...}__ deletes all tags that match _delete_ and
359   moves the ones that match _extra_  into extratags
360 * __grab_address_parts{groups=...}__ moves matching tags into the address table.
361   _groups_ must be a group match function. Tags of the group `main` and
362   `extra` are added to the address table as is but with `addr:` and `is_in:`
363   prefixes removed from the tag key. All other groups are added with the
364   group name as key and the value from the tag. Multiple values of the same
365   group overwrite each other. The function returns the number of tags saved
366   from the main group.
367 * __grab_main_parts{groups=...}__ moves matching tags into the name table.
368   _groups_ must be a group match function. If a tags of the group `main` is
369   present, the object will be marked as having a name. Tags of group `house`
370   produce a fallback to `place=house`. This fallback is return by the function
371   if present.
372
373 There are two functions to write a row into the place table. Both functions
374 expect the main tag (key and value) for the row and then use the collected
375 information from the name, address, extratags etc. fields to complete the row.
376 They also have a boolean parameter `save_extra_mains` which defines how any
377 unprocessed tags are handled: when True, the tags will be saved as extratags,
378 when False, they will be simply discarded.
379
380 * __write_row(key, value, save_extra_mains)__ creates a new table row from
381   the current state of the Place object.
382 * __write_place(key, value, mtype, save_extra_mains)__ creates a new row
383   conditionally. When value is nil, the function will attempt to look up the
384   value in the object tags. If value is still nil or mtype is nil, the row
385   is ignored. An mtype of `always` will then always write out the row,
386   a mtype of `named` only, when the object has a full name. When mtype
387   is `named_with_key`, the function checks for a domain name, i.e. a name
388   tag prefixed with the name of the main key. Only if at least one is found,
389   the row will be written. The names are replaced with the domain names found.
390
391 #### Match functions
392
393 The Place functions usually expect either a _match function_ or a
394 _group match function_ to find the tags to apply their function to.
395
396 The __match function__ is a Lua function which takes two parameters,
397 key and value, and returns a boolean to indicate that a tag matches. The
398 flex-base module has a convenience function `tag_match()` to create such a
399 function. It takes a table with two optional fields: `keys` takes a key match
400 list (see above), `tags` takes a table with keys that point to a list of
401 possible values, thus defining key/value matches.
402
403 The __group match function__ is a Lua function which also takes two parameters,
404 key and value, and returns a string indicating to which group or type they
405 belong to. The `tag_group()` can be used to create such a function. It expects
406 a table where the group names are the keys and the values are a key match list.
407
408
409
410 ### Using the gazetteer output of osm2pgsql
411
412 Nominatim still allows you to configure the gazetteer output to remain
413 backwards compatible with older imports. It will be automatically used
414 when the style file name ends in `.style`. For documentation of the
415 old import style, please refer to the documentation of older releases
416 of Nominatim. Do not use the gazetteer output for new imports. There is no
417 guarantee that new versions of Nominatim are fully compatible with the
418 gazetteer output.
419
420 ### Changing the Style of Existing Databases
421
422 There is normally no issue changing the style of a database that is already
423 imported and now kept up-to-date with change files. Just be aware that any
424 change in the style applies to updates only. If you want to change the data
425 that is already in the database, then a reimport is necessary.