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