]> git.openstreetmap.org Git - nominatim.git/blob - docs/customize/Import-Styles.md
add documentation for entrance table configuration
[nominatim.git] / docs / customize / Import-Styles.md
1 # Configuring the Import of OSM data
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. Nominatim ships with a few preset
8 configurations for this import, each results in a geocoding database of
9 different detail. The
10 [Import section](../admin/Import.md#filtering-imported-data) explains
11 these default configurations in detail.
12
13 If you want to have more control over which OSM data is added to the database,
14 you can also create your own custom style. Create a new lua style file, put it
15 into your project directory and then set `NOMINATIM_IMPORT_STYLE` to the name
16 of the file. Custom style files can be used to modify the existing preset
17 configurations or to implement your own configuration from scratch.
18
19 The remainder of the page describes how the flex style works and how to
20 customize it.
21
22 ## The `flex-base` lua module
23
24 The core of Nominatim's flex import configuration is the `flex-base` module.
25 It defines the table layout used by Nominatim and provides standard
26 implementations for the import callbacks that help with customizing
27 how OSM tags are used by Nominatim.
28
29 Every custom style must include this module to make sure that the correct
30 tables are created. Thus start your custom style as follows:
31
32 ``` lua
33 local flex = require('flex-base')
34 ```
35
36 ### Using preset configurations
37
38 If you want to start with one of the existing presets, then you can import
39 its settings using the `load_topic()` function:
40
41 ``` lua
42 local flex = require('flex-base')
43
44 flex.load_topic('streets')
45 ```
46
47 The `load_topic` function takes an optional second configuration
48 parameter. The available options are explained in the
49 [themepark section](#using-osm2pgsql-themepark).
50
51 Available topics are: `admin`, `street`, `address`, `full`. These topic
52 correspond to the [import styles](../admin/Import.md#filtering-imported-data)
53 you can choose during import. To start with the 'extratags' style, use the
54 `full` topic with the appropriate config parameter:
55
56 ``` lua
57 flex.load_topic('full', {with_extratags = true})
58 ```
59
60 !!! note
61     You can also directly import the preset style files, e.g.
62     `local flex = require('import-street')`. It is not possible to
63     set extra configuration this way.
64
65 ### How processing works
66
67 When Nominatim processes an OSM object, it looks for four kinds of tags:
68 The _main tags_ classify what kind of place the OSM object represents. One
69 OSM object can have more than one main tag. In such case one database entry
70 is created for each main tag. _Name tags_ represent searchable names of the
71 place. _Address tags_ are used to compute the address information of the place.
72 Address tags are used for searching and for creating a display name of the place.
73 _Extra tags_ are any tags that are not directly related to search but
74 contain interesting additional information. These are just saved in the database
75 and may be returned with the result [on request](../api/Search.md#output-details).
76
77 !!! danger
78     Some tags in the extratags category are used by Nominatim to better
79     classify the place. You want to make sure these are always present
80     in custom styles.
81
82 Configuring the style means deciding which key and/or key/value is used
83 in which category.
84
85 ## Changing the recognized tags
86
87 The flex style offers a number of functions to set the classification of
88 each OSM tag. Most of these functions can also take a preset string instead
89 of a tag description. These presets describe common configurations that
90 are also used in the definition of the predefined styles. This section
91 lists the configuration functions and the accepted presets.
92
93 #### Key match lists
94
95 Some of the following functions take _key match lists_. These lists can
96 contain three kinds of strings to match against tag keys:
97 A string that ends in an asterisk `*` is a prefix match and accordingly matches
98 against any key that starts with the given string (minus the `*`). 
99 A suffix match can be defined similarly with a string that starts with a `*`.
100 Any other string is matched exactly against tag keys.
101
102 ###  Main tags
103
104 `set/modify_main_tags()` allow to define which tags are used as main tags. It
105 takes a lua table parameter which defines for keys and key/value
106 combinations, how they are classified.
107
108 The following classifications are recognized:
109
110 | classification  | meaning |
111 | :-------------- | :------ |
112 | always          | Unconditionally use this tag as a main tag. |
113 | named           | Consider as main tag, when the object has a primary name (see [names](#name-tags) below) |
114 | named_with_key  | Consider as main tag, when the object has a primary name with a domain prefix. For example, if the main tag is  `bridge=yes`, then it will only be added as an extra entry, if there is a tag `bridge:name[:XXX]` for the same object. If this property is set, all names that are not domain-specific are ignored. |
115 | fallback        | Consider as main tag only when no other main tag was found. Fallback always implies `named`, i.e. fallbacks are only tried for objects with primary names. |
116 | delete          | Completely ignore the tag in any further processing |
117 | extra           | Move the tag to extratags and then ignore it for further processing |
118 | `<function>`| Advanced handling, see [below](#advanced-main-tag-handling) |
119
120 Each key in the table parameter defines an OSM tag key. The value may
121 be directly a classification as described above. Then the tag will
122 be considered a main tag for any possible value that is not further defined.
123 To further restrict which values are acceptable, give a table with the
124 permitted values and their kind of main tag. If the table contains a simple
125 value without key, then this is used as default for values that are not listed.
126
127 `set_main_tags()` will completely replace the current main tag configuration
128 with the new configuration. `modify_main_tags()` will merge the new
129 configuration with the existing one. Merging is done at value level.
130 For example, when the current setting is `highway = {'always', primary = 'named'}`,
131 then `set_main_tags{highway = 'delete'}` will result in a rule
132 `highway = {'delete', primary = 'named'}`.
133
134 !!! example
135     ``` lua
136     local flex = require('import-full')
137
138     flex.set_main_tags{
139         boundary = {administrative = 'named'},
140         highway = {'always', street_lamp = 'named', no = 'delete'},
141         landuse = 'fallback'
142     }
143     ```
144
145     In this example an object with a `boundary` tag will only be included
146     when it has a value of `administrative`. Objects with `highway` tags are
147     always included with two exceptions: the troll tag `highway=no` is
148     deleted on the spot. And when the value is `street_lamp` then the object
149     must also have a name, to be included. Finally, if a `landuse` tag is
150     present then it will be used independently of the concrete value when
151     neither boundary nor highway tags were found and the object is named.
152
153 ##### Presets
154
155 | Name   | Description |
156 | :----- | :---------- |
157 | admin  | Basic tag set collecting places and administrative boundaries. This set is needed also to ensure proper address computation and should therefore always be present. You can disable selected place types like `place=locality` after adding this set, if they are not relevant for your use case. |
158 | all_boundaries | Extends the set of recognized boundaries and places to all available ones. |
159 | natural | Tags for natural features like rivers and mountain peaks. |
160 | street/default | Tags for streets. Major streets are always included, minor ones only when they have a name. |
161 | street/car | Tags for all streets that can be used by a motor vehicle. |
162 | street/all | Includes all highway features named and unnamed. |
163 | poi/delete | Adds most POI features with and without name. Some frequent but very domain-specific values are excluded by deleting them. |
164 | poi/extra | Like 'poi/delete' but excluded values are moved to extratags. |
165
166
167 ##### Advanced main tag handling
168
169 The groups described above are in fact only a preset for a filtering function
170 that is used to make the final decision how a pre-selected main tag is entered
171 into Nominatim's internal table. To further customize handling you may also
172 supply your own filtering function.
173
174 The function takes up to three parameters: a Place object of the object
175 being processed, the key of the main tag and the value of the main tag.
176 The function may return one of three values:
177
178 * `nil` or `false` causes the entry to be ignored
179 * the Place object causes the place to be added as is
180 * `Place.copy(names=..., address=..., extratags=...) causes the
181   place to be enter into the database but with name/address/extratags
182   set to the given different values.
183
184 The Place object has some read-only values that can be used to determine
185 the handling:
186
187 * **object** is the original OSM object data handed in by osm2pgsql
188 * **admin_level** is the content of the admin_level tag, parsed into an integer and normalized to a value between 0 and 15
189 * **has_name** is a boolean indicating if the object has a primary name tag
190 * **names** is a table with the collected list of name tags
191 * **address** is a table with the collected list of address tags
192 * **extratags** is a table with the collected list of additional tags to save
193
194 !!! example
195     ``` lua
196     local flex = require('flex-base')
197
198     flex.add_topic('street')
199
200     local function no_sidewalks(place, k, v)
201         if place.object.tags.footway == 'sidewalk' then
202             return false
203         end
204
205         -- default behaviour is to have all footways
206         return place
207     end
208
209     flex.modify_main_tags(highway = {'footway' = no_sidewalks}
210     ```
211     This script adds a custom handler for `highway=footway`. It only includes
212     them in the database, when the object doesn't have a tag `footway=sidewalk`
213     indicating that it is just part of a larger street which should already
214     be indexed. Note that it is not necessary to check the key and value
215     of the main tag because the function is only used for the specific
216     main tag.
217
218
219 ### Ignored tags
220
221 The function `ignore_keys()` sets the `delete` classification for keys.
222 This function takes a _key match list_ so that it is possible to exclude
223 groups of keys.
224
225 Note that full matches always take precedence over suffix matches, which
226 in turn take precedence over prefix matches.
227
228 !!! example
229     ``` lua
230     local flex = require('flex-base')
231
232     flex.add_topic('admin')
233     flex.ignore_keys{'old_name', 'old_name:*'}
234     ```
235
236     This example uses the `admin` preset with the exception that names
237     that are no longer are in current use, are ignored.
238
239 ##### Presets
240
241 | Name     | Description |
242 | :-----   | :---------- |
243 | metatags | Tags with meta information about the OSM tag like source, notes and import sources. |
244 | name     | Non-names that actually describe properties or name parts. These names can throw off search and should always be removed. |
245 | address  | Extra `addr:*` tags that are not useful for Nominatim. |
246
247
248 ### Tags for `extratags`
249
250 The function `add_for_extratags()` sets the `extra` classification for keys.
251 This function takes a
252 _key match list_ so that it is possible to move groups of keys to extratags.
253
254 Note that full matches always take precedence over suffix matches, which
255 in turn take precedence over prefix matches.
256
257 !!! example
258     ``` lua
259     local flex = require('flex-base')
260
261     flex.add_topic('street')
262     flex.add_for_extratags{'surface', 'access', 'vehicle', 'maxspeed'}
263     ```
264
265     This example uses the `street` preset but adds a couple of tags that
266     are of interest about the condition of the street.
267
268 ##### Presets
269
270 | Name     | Description |
271 | :-----   | :---------- |
272 | required | Tags that Nominatim will use for various computations when present in extratags. Always include these. |
273
274 In addition, all [presets from ignored tags](#presets_1) are accepted.
275
276 ### General pre-filtering
277
278 _(deprecated)_ `set_prefilters()` allows to set the `delete` and `extra`
279 classification for main tags.
280
281 This function removes all previously set main tags with `delete` and `extra`
282 classification and then adds the newly defined tags.
283
284 `set_prefilters()` takes a table with four optional fields:
285
286 * __delete_keys__ is a _key match list_ for tags that should be deleted
287 * __delete_tags__ contains a table of tag keys pointing to a list of tag
288   values. Tags with matching key/value pairs are deleted.
289 * __extra_keys__ is a _key match list_ for tags which should be saved into
290   extratags
291 * __extra_tags__ contains a table of tag keys pointing to a list of tag
292   values. Tags with matching key/value pairs are moved to extratags.
293
294 !!! danger "Deprecation warning"
295     Use of this function should be replaced with `modify_main_tags()` to
296     set the data from `delete_tags` and `extra_tags`, with `ignore_keys()`
297     for the `delete_keys` parameter and with `add_for_extratags()` for the
298     `extra_keys` parameter.
299
300 ### Name tags
301
302 `set/modify_name_tags()` allow to define the tags used for naming places. Name tags
303 can only be selected by their keys. The import script distinguishes
304 between primary and auxiliary names. A primary name is the given name of
305 a place. Having a primary name makes a place _named_. This is important
306 for main tags that are only included when a name is present. Auxiliary names
307 are identifiers like references. They may be searched for but should not
308 be included on their own.
309
310 The functions take a table with two optional fields `main` and `extra`.
311 They take _key match lists_ for primary and auxiliary names respectively.
312 A third field `house` can contain tags for names that appear in place of
313 house numbers in addresses. This field can only contain complete key names.
314 'house tags' are special in that they cause the OSM object to be added to
315 the database independently of the presence of other main tags.
316
317 `set_name_tags()` overwrites the current configuration, while
318 `modify_name_tags()` replaces the fields that are given. (Be aware that
319 the fields are replaced as a whole. `main = {'foo_name'}` will cause
320 `foo_name` to become the only recognized primary name. Any previously
321 defined primary names are forgotten.)
322
323 !!! example
324     ``` lua
325     local flex = require('flex-base')
326
327     flex.set_main_tags{highway = {traffic_light = 'named'}}
328     flex.set_name_tags{main = {'name', 'name:*'},
329                        extra = {'ref'}
330                       }
331     ```
332
333     This example creates a search index over traffic lights but will
334     only include those that have a common name and not those which just
335     have some reference ID from the city.
336
337 ##### Presets
338
339 | Name     | Description |
340 | :-----   | :---------- |
341 | core     | Basic set of recognized names for all places. |
342 | address  | Additional names useful when indexing full addresses. |
343 | poi      | Extended set of recognized names for pois. Use on top of the core set. |
344
345 ### Address tags
346
347 `set/modify_address_tags()` defines the tags that will be used to build
348 up the address of an object. Address tags can only be chosen by their key.
349
350 The functions take a table with arbitrary fields, each defining
351 a key list or _key match list_. Some fields have a special meaning:
352
353 | Field     | Type      | Description |
354 | :---------| :-------- | :-----------|
355 | main      | key list  | Tags that make a full address object out of the OSM object. This is usually the house number or variants thereof. If a main address tag appears, then the object will always be included, if necessary with a fallback of `place=house`. If the key has a prefix of `addr:` or `is_in:` this will be stripped. |
356 | extra     | key match list | 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. |
357 | interpolation | key list | Tags that identify address interpolation lines. |
358 | country   | key match list | Tags that may contain the country the place is in. The first found value with a two-letter code will be accepted, all other values are discarded. |
359 | _other_   | key match list | Summary field. If a key matches the key match list, then its value will be added to the address tags with the name of the field as key. If multiple tags match, then an arbitrary one wins. |
360
361 `set_address_tags()` overwrites the current configuration, while
362 `modify_address_tags()` replaces the fields that are given. (Be aware that
363 the fields are replaced as a whole.)
364
365 !!! example
366     ``` lua
367     local flex = require('import-full')
368
369     flex.set_address_tags{
370         main = {'addr:housenumber'},
371         extra = {'addr:*'},
372         postcode = {'postal_code', 'postcode', 'addr:postcode'},
373         country = {'country_code', 'ISO3166-1'}
374     }
375     ```
376
377     In this example all tags which begin with `addr:` will be saved in
378     the address tag list. If one of the tags is `addr:housenumber`, the
379     object will fall back to be entered as a `place=house` in the database
380     unless there is another interested main tag to be found.
381
382     Tags with keys `country_code` and `ISO3166-1` are saved with their
383     value under `country` in the address tag list. The same thing happens
384     to postcodes, they will always be saved under the key `postcode` thus
385     normalizing the multitude of keys that are used in the OSM database.
386
387 ##### Presets
388
389 | Name     | Description |
390 | :-----   | :---------- |
391 | core     | Basic set of tags needed to recognize address relationship for any place. Always include this. |
392 | houses   | Additional set of tags needed to recognize proper addresses |
393
394 ### Handling of unclassified tags
395
396 `set_unused_handling()` defines what to do with tags that remain after all tags
397 have been classified using the functions above. There are two ways in
398 which the function can be used:
399
400 `set_unused_handling(delete_keys = ..., delete_tags = ...)` deletes all
401 keys that match the descriptions in the parameters and moves all remaining
402 tags into the extratags list.
403
404 `set_unused_handling(extra_keys = ..., extra_tags = ...)` moves all tags
405 matching the parameters into the extratags list and then deletes the remaining
406 tags. For the format of the parameters see the description in `set_prefilters()`
407 above.
408
409 When no special handling is set, then unused tags will be discarded with one
410 exception: place tags are kept in extratags for administrative boundaries.
411 When using a custom setting, you should also make sure that the place tag
412 is added for extratags.
413
414 !!! example
415     ``` lua
416     local flex = require('import-full')
417
418     flex.set_address_tags{
419         main = {'addr:housenumber'},
420         extra = {'addr:*', 'tiger:county'}
421     }
422     flex.set_unused_handling{delete_keys = {'tiger:*'}}
423     ```
424
425     In this example all remaining tags except those beginning with `tiger:`
426     are moved to the extratags list. Note that it is not possible to
427     already delete the tiger tags with `set_prefilters()` because that
428     would remove tiger:county before the address tags are processed.
429
430 ## Filling additional tables
431
432 Most of the OSM objects are saved in the main `place` table for further
433 processing. In addition to that, there are some smaller tables that save
434 specialised information. The content of these tables can be customized as
435 well.
436
437 ### Entrance table
438
439 The table `place_entrance` saves information about OSM nodes that represent
440 an entrance. This data is later mingled with buildings and other areas and
441 can be returned [on request](../api/Search.md#output-details). The table
442 saves the type of entrance as well as a set of custom extra tags.
443
444 The function `set_entrance_filter()` can be used to customize the table's
445 content.
446
447 When called without any parameter, then filling the entrance table will be
448 disabled. When called with a preset name, the appropriate preset will be
449 applied.
450
451 To create a custom configuration, call the function
452 with a table with the following fields:
453
454 * __main_tags__ is a list of tags that mark an entrance node. The value of the
455   first tag found in the list will be used as the entrance type.
456 * __extra_include__ is an optional list of tags to be added to the extratags
457   for this entrance. When left out, all tags except for the ones defined
458   in 'main_tags' will be included. To disable saving of extra tags, set
459   this to the empty list.
460 * __extra_exclude__ defines an optional list of tags to drop before including
461   the remaining tags as extratags. Note that the tags defined in 'main_tags'
462   will always be excluded, independently of this setting.
463
464 To have even more fine-grained control over the output, you can also hand
465 in a table with a single field `func` containing a callback for processing
466 entrance information. The callback function receives a single parameter,
467 the [osm2pgsql object](https://osm2pgsql.org/doc/manual.html#processing-callbacks).
468 This object itself must not be modified. The callback should return either
469 `nil` when the object is not an entrance. Or it returns a table with a
470 mandatory `entrance` field containing a string with the type of entrance
471 and an optional `extratags` field with a simple key-value table of extra
472 information.
473
474 ##### Presets
475
476 | Name     | Description |
477 | :-----   | :---------- |
478 | default  | Standard configuration used with `full` and `extratags` styles. |
479
480 ## Customizing osm2pgsql callbacks
481
482 osm2pgsql expects the flex style to implement three callbacks, one process
483 function per OSM type. If you want to implement special handling for
484 certain OSM types, you can override the default implementations provided
485 by the flex-base module.
486
487 ### Enabling additional relation types
488
489 OSM relations can represent very diverse
490 [types of real-world objects](https://wiki.openstreetmap.org/wiki/Key:type). To
491 be able to process them correctly, Nominatim needs to understand how to
492 create a geometry for each type. By default, the script knows how to
493 process relations of type `multipolygon`, `boundary` and `waterway`. All
494 other relation types are ignored.
495
496 To add other types relations, set `RELATION_TYPES` for
497 the type to the kind of geometry that should be created. The following
498 kinds of geometries can be used:
499
500 * __relation_as_multipolygon__ creates a (Multi)Polygon from the ways in
501   the relation. If the ways do not form a valid area, then the object is
502   silently discarded.
503 * __relation_as_multiline__ creates a (Multi)LineString from the ways in
504   the relations. Ways are combined as much as possible without any regards
505   to their order in the relation.
506
507 !!! Example
508     ``` lua
509     local flex = require('import-full')
510
511     flex.RELATION_TYPES['site'] = flex.relation_as_multipolygon
512     ```
513
514     With this line relations of `type=site` will be included in the index
515     according to main tags found. This only works when the site relation
516     resolves to a valid area. Nodes in the site relation are not part of the
517     geometry.
518
519
520 ### Adding additional logic to processing functions
521
522 The default processing functions are also exported by the flex-base module
523 as `process_node`, `process_way` and `process_relation`. These can be used
524 to implement your own processing functions with some additional processing
525 logic.
526
527 !!! Example
528     ``` lua
529     local flex = require('import-full')
530
531     function osm2pgsql.process_relation(object)
532         if object.tags.boundary ~= 'administrative' or object.tags.admin_level ~= '2' then
533           flex.process_relation(object)
534         end
535     end
536     ```
537
538     This example discards all country-level boundaries and uses standard
539     handling for everything else. This can be useful if you want to use
540     your own custom country boundaries.
541
542
543 ### Customizing the main processing function
544
545 !!! danger "Deprecation Warning"
546     The style used to allow overwriting the internal processing function
547     `process_tags()`. While this is currently still possible, it is no longer
548     encouraged and may stop working in future versions. The internal
549     `Place` class should now be considered read-only.
550
551
552 ## Using osm2pgsql-themepark
553
554 The Nominatim osm2pgsql style is designed so that it can also be used as
555 a theme for [osm2pgsql-themepark](https://osm2pgsql.org/themepark/). This
556 makes it easy to combine Nominatim with other projects like
557 [openstreetmap-carto](https://github.com/gravitystorm/openstreetmap-carto)
558 in the same database.
559
560 To set up one of the preset styles, simply include a topic with the same name:
561
562 ```
563 local themepark = require('themepark')
564 themepark:add_topic('nominatim/address')
565 ```
566
567 Themepark topics offer two configuration options:
568
569 * **street_theme** allows to choose one of the sub topics for streets:
570     * _default_ - include all major streets and named minor paths
571     * _car_ - include all streets physically usable by cars
572     * _all_ - include all major streets and minor paths
573 * **with_extratags**, when set to a truthy value, then tags that are
574   not specifically used for address or naming are added to the
575   extratags column
576
577 The customization functions described in the
578 [Changing recognized tags](#changing-the-recognized-tags) section
579 are available from the theme. To access the theme you need to explicitly initialize it.
580
581 !!! Example
582     ``` lua
583     local themepark = require('themepark')
584
585     themepark:add_topic('nominatim/full', {with_extratags = true})
586
587     local flex = themepark:init_theme('nominatim')
588
589     flex.modify_main_tags{'amenity' = {
590                            'waste_basket' = 'delete'}
591                       }
592     ```
593     This example uses the full Nominatim configuration but disables
594     importing waste baskets.
595
596 You may also write a new configuration from scratch. Simply omit including
597 a Nominatim topic and only call the required customization functions.
598
599 Customizing the osm2pgsql processing functions as explained
600 [above](#adding-additional-logic-to-processing-functions) is not possible
601 when running under themepark. Instead include other topics that make the
602 necessary modifications or add an additional processor before including
603 the Nominatim topic.
604
605 !!! Example
606     ``` lua
607     local themepark = require('themepark')
608
609     local function discard_country_boundaries(object)
610         if object.tags.boundary == 'administrative' and object.tags.admin_level == '2' then
611             return 'stop'
612         end
613     end
614
615     themepark:add_proc('relation', discard_country_boundaries)
616     -- Order matters here. The topic needs to be added after the custom callback.
617     themepark:add_topic('nominatim/full', {with_extratags = true})
618     ```
619     Discarding country-level boundaries when running under themepark.
620
621 ## Changing the style of existing databases
622
623 There is usually no issue changing the style of a database that is already
624 imported and now kept up-to-date with change files. Just be aware that any
625 change in the style applies to updates only. If you want to change the data
626 that is already in the database, then a reimport is necessary.