]> git.openstreetmap.org Git - nominatim.git/blob - docs/library/Getting-Started.md
preliminary library reference finished
[nominatim.git] / docs / library / Getting-Started.md
1 # Getting Started
2
3 The Nominatim search frontend can directly be used as a Python library in
4 scripts and applications. When you have imported your own Nominatim database,
5 then it is no longer necessary to run a full web service for it and access
6 the database through http requests. With the Nominatim library it is possible
7 to access all search functionality directly from your Python code. There are
8 also less constraints on the kinds of data that can be accessed. The library
9 allows to get access to more detailed information about the objects saved
10 in the database.
11
12 !!! danger
13     The library interface is currently in an experimental stage. There might
14     be some smaller adjustments to the public interface until the next version.
15
16     The library also misses a proper installation routine, so some manipulation
17     of the PYTHONPATH is required. Use is only recommended for advanced Python
18     programmers at the moment.
19
20 ## Installation
21
22 To use the Nominatim library, you need access to a local Nominatim database.
23 Follow the [installation and import instructions](../admin/) to set up your
24 database.
25
26 It is not yet possible to install it in the usual way via pip or inside a
27 virtualenv. To get access to the library you need to set an appropriate
28 PYTHONPATH. With the default installation, the python library can be found
29 under `/usr/local/share/nominatim/lib-python`. If you have installed
30 Nominatim under a different prefix, adapt the `/usr/local/` part accordingly.
31 You can also point the PYTHONPATH to the Nominatim source code.
32
33
34 ### A simple search example
35
36 To query the Nominatim database you need to first set up a connection. This
37 is done by creating an Nominatim API object. This object exposes all the
38 search functions of Nominatim that are also knwon from its web API.
39
40 This code snippet implements a simple search for the town if 'Brugge':
41
42 === "NominatimAPIAsync"
43     ```
44     from pathlib import Path
45     import asyncio
46
47     import nominatim.api as napi
48
49     async def search(query):
50         api = napi.NominatimAPIAsync(Path('.'))
51
52         return await api.search(query)
53
54     results = asyncio.run(search('Brugge'))
55     if not results:
56         print('Cannot find Brugge')
57     else:
58         print(f'Found a place at {results[0].centroid.x},{results[1].centroid.y}')
59     ```
60
61 === "NominatimAPI"
62     ```
63     from pathlib import Path
64
65     import nominatim.api as napi
66
67     api = napi.NominatimAPI(Path('.'))
68
69     results = api.search('Brugge')
70
71     if not results:
72         print('Cannot find Brugge')
73     else:
74         print(f'Found a place at {results[0].centroid.x},{results[1].centroid.y}')
75     ```
76
77 The Nonminatim API comes in two flavours: synchronous and asynchronous.
78 The complete Nominatim library is written so that it can work asynchronously.
79 If you have many requests to make, coroutines can speed up your applications
80 significantly.
81
82 For smaller scripts there is also a sychronous wrapper around the API.
83
84 ### Defining which database to use
85
86