1
1
# Getting Started
2
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. There are
7
- also less constraints on the kinds of data that can be accessed. The library
8
- allows to get access to more detailed information about the objects saved
9
- in the database.
10
-
11
- !!! danger
12
- The library interface is currently in an experimental stage. There might
13
- be some smaller adjustments to the public interface until the next version.
3
+ The Nominatim search frontend is implemented as a Python library and can as
4
+ such directly be used in Python scripts and applications. You don't need to
5
+ set up a web frontend and access it through HTTP calls. The library gives
6
+ direct access to the Nominatim database through similar search functions as
7
+ offered by the web API. In addition, it will give you a more complete and
8
+ detailed view on the search objects stored in the database.
9
+
10
+ !!! warning
11
+
12
+ The Nominatim library is used for accessing a local Nominatim database.
13
+ It is not meant to be used against web services of Nominatim like the
14
+ one on https://nominatim.openstreetmap.org. If you need a Python library
15
+ to access these web services, have a look at
16
+ [GeoPy](https://geopy.readthedocs.io). Don't forget to consult the
17
+ usage policy of the service you want to use before accessing such
18
+ a web service.
14
19
15
20
## Installation
16
21
@@ -19,13 +24,17 @@ Follow the [installation](../admin/Installation.md) and
19
24
[ import] ( ../admin/Import.md ) instructions to set up your database.
20
25
21
26
The Nominatim frontend library is contained in the Python package ` nominatim-api ` .
27
+ You can install the latest released version directly from pip:
28
+
29
+ pip install nominatim-api
30
+
22
31
To install the package from the source tree directly, run:
23
32
24
33
pip install packaging/nominatim-api
25
34
26
35
Usually you would want to run this in a virtual environment.
27
36
28
- ### A simple search example
37
+ ## A simple search example
29
38
30
39
To query the Nominatim database you need to first set up a connection. This
31
40
is done by creating an Nominatim API object. This object exposes all the
@@ -36,15 +45,13 @@ This code snippet implements a simple search for the town of 'Brugge':
36
45
!!! example
37
46
=== "NominatimAPIAsync"
38
47
``` python
39
- from pathlib import Path
40
48
import asyncio
41
49
42
50
import nominatim_api as napi
43
51
44
52
async def search(query):
45
- api = napi.NominatimAPIAsync(Path('.'))
46
-
47
- return await api.search(query)
53
+ async with napi.NominatimAPIAsync() as api:
54
+ return await api.search(query)
48
55
49
56
results = asyncio.run(search('Brugge'))
50
57
if not results:
@@ -55,13 +62,10 @@ This code snippet implements a simple search for the town of 'Brugge':
55
62
56
63
=== "NominatimAPI"
57
64
``` python
58
- from pathlib import Path
59
-
60
65
import nominatim_api as napi
61
66
62
- api = napi.NominatimAPI(Path('.'))
63
-
64
- results = api.search('Brugge')
67
+ with napi.NominatimAPI() as api:
68
+ results = api.search('Brugge')
65
69
66
70
if not results:
67
71
print('Cannot find Brugge')
@@ -84,7 +88,7 @@ implementations. The documentation itself will usually refer only to
84
88
available only for the synchronous or asynchronous version, this will be
85
89
explicitly mentioned.
86
90
87
- ### Defining which database to use
91
+ ## Defining which database to use
88
92
89
93
The [ Configuration] ( ../admin/Import.md#configuration-setup-in-env )
90
94
section explains how Nominatim is configured using the
@@ -93,76 +97,120 @@ The same configuration mechanism is used with the
93
97
Nominatim API library. You should therefore be sure you are familiar with
94
98
the section.
95
99
96
- The constructor of the 'Nominatim API class' takes one mandatory parameter:
97
- the path to the [ project directory] ( ../admin/Import.md#creating-the-project-directory ) .
98
- You should have set up this directory as part of the Nominatim import.
99
- Any configuration found in the ` .env ` file in this directory will automatically
100
- used.
100
+ There are three different ways, how configuration options can be set for
101
+ a 'Nominatim API class'. When you have set up your Nominatim database, you
102
+ have normally created a [ project directory] ( ../admin/Import.md#creating-the-project-directory )
103
+ which stores the various configuration and customization files that Nominatim
104
+ needs. You may pass the location of the project directory to your
105
+ 'Nominatim API class' constructor and it will read the .env file in the
106
+ directory and set the configuration accordingly. Here is the simple search
107
+ example, using the configuration from a pre-defined project directory in
108
+ ` /srv/nominatim-project ` :
109
+
110
+ !!! example
111
+ === "NominatimAPIAsync"
112
+ ``` python
113
+ import asyncio
114
+
115
+ import nominatim_api as napi
116
+
117
+ async def search(query):
118
+ async with napi.NominatimAPIAsync('/srv/nominatim-project') as api:
119
+ return await api.search(query)
120
+
121
+ results = asyncio.run(search('Brugge'))
122
+ if not results:
123
+ print('Cannot find Brugge')
124
+ else:
125
+ print(f'Found a place at {results[0].centroid.x},{results[0].centroid.y}')
126
+ ```
127
+
128
+ === "NominatimAPI"
129
+ ``` python
130
+ import nominatim_api as napi
131
+
132
+ with napi.NominatimAPI('/srv/nominatim-project') as api:
133
+ results = api.search('Brugge')
134
+
135
+ if not results:
136
+ print('Cannot find Brugge')
137
+ else:
138
+ print(f'Found a place at {results[0].centroid.x},{results[0].centroid.y}')
139
+ ```
140
+
101
141
102
142
You may also configure Nominatim by setting environment variables.
103
- Normally, Nominatim will check the operating system environment. This can be
104
- overwritten by giving the constructor a dictionary of configuration parameters.
143
+ Normally Nominatim will check the operating system environment. Lets
144
+ say you want to look up 'Brugge' in the special database named 'belgium' instead of the
145
+ standard 'nominatim' database. You can run the example script above like this:
146
+
147
+ ```
148
+ NOMINATIM_DATABASE_DSN=pgsql:dbname=belgium python3 example.py
149
+ ```
105
150
106
- Let us look up 'Brugge' in the special database named 'belgium' instead of the
107
- standard 'nominatim' database:
151
+ The third option to configure the library is to hand in the configuration
152
+ parameters into the 'Nominatim API class'. Changing the database would look
153
+ like this:
108
154
109
155
!!! example
110
156
=== "NominatimAPIAsync"
111
157
``` python
112
- from pathlib import Path
113
158
import asyncio
114
-
115
159
import nominatim_api as napi
116
160
117
161
config_params = {
118
162
'NOMINATIM_DATABASE_DSN': 'pgsql:dbname=belgium'
119
163
}
120
164
121
165
async def search(query):
122
- api = napi.NominatimAPIAsync(Path('.'), environ=config_params)
123
-
124
- return await api.search(query)
166
+ async with napi.NominatimAPIAsync(environ=config_params) as api:
167
+ return await api.search(query)
125
168
126
169
results = asyncio.run(search('Brugge'))
127
170
```
128
171
129
172
=== "NominatimAPI"
130
173
``` python
131
- from pathlib import Path
132
-
133
174
import nominatim_api as napi
134
175
135
176
config_params = {
136
177
'NOMINATIM_DATABASE_DSN': 'pgsql:dbname=belgium'
137
178
}
138
179
139
- api = napi.NominatimAPI(Path('.'), environ=config_params)
140
-
141
- results = api.search('Brugge')
180
+ with napi.NominatimAPI(environ=config_params) as api:
181
+ results = api.search('Brugge')
142
182
```
143
183
144
- ### Presenting results to humans
184
+ When the ` environ ` parameter is given, then only configuration variables
185
+ from this dictionary will be used. The operating system's environment
186
+ variables will be ignored.
145
187
146
- All search functions return the raw results from the database. There is no
147
- full human-readable label. To create such a label, you need two things:
188
+ ## Presenting results to humans
189
+
190
+ All search functions return full result objects from the database. Such a
191
+ result object contains lots of details: names, address information, OSM tags etc.
192
+ This gives you lots of flexibility what to do with the results.
193
+
194
+ One of the most common things to get is some kind of human-readable label
195
+ that describes the result in a compact form. Usually this would be the name
196
+ of the object and some parts of the address to explain where in the world
197
+ it is. To create such a label, you need two things:
148
198
149
199
* the address details of the place
150
- * adapt the result to the language you wish to use for display
200
+ * all names for the label adapted to the language you wish to use for display
151
201
152
202
Again searching for 'Brugge', this time with a nicely formatted result:
153
203
154
204
!!! example
155
205
=== "NominatimAPIAsync"
156
206
``` python
157
- from pathlib import Path
158
207
import asyncio
159
208
160
209
import nominatim_api as napi
161
210
162
211
async def search(query):
163
- api = napi.NominatimAPIAsync(Path('.'))
164
-
165
- return await api.search(query, address_details=True)
212
+ async with napi.NominatimAPIAsync() as api:
213
+ return await api.search(query, address_details=True)
166
214
167
215
results = asyncio.run(search('Brugge'))
168
216
@@ -174,13 +222,10 @@ Again searching for 'Brugge', this time with a nicely formatted result:
174
222
175
223
=== "NominatimAPI"
176
224
``` python
177
- from pathlib import Path
178
-
179
225
import nominatim_api as napi
180
226
181
- api = napi.NominatimAPI(Path('.'))
182
-
183
- results = api.search('Brugge', address_details=True)
227
+ with napi.NominatimAPI() as api:
228
+ results = api.search('Brugge', address_details=True)
184
229
185
230
locale = napi.Locales(['fr', 'en'])
186
231
for i, result in enumerate(results):
@@ -236,7 +281,7 @@ Bruges, Flandre-Occidentale, Flandre, Belgique
236
281
237
282
This is a fairly simple way to create a human-readable description. The
238
283
place information in ` address_rows ` contains further information about each
239
- place. For example, which OSM ` adlin_level ` was used, what category the place
284
+ place. For example, which OSM ` admin_level ` was used, what category the place
240
285
belongs to or what rank Nominatim has assigned. Use this to adapt the output
241
286
to local address formats.
242
287
0 commit comments