Skip to content

Commit a66c063

Browse files
authored
Merge pull request #3532 from lonvia/refresh-docs
Update library documentation
2 parents 3e6be0b + fe0ade8 commit a66c063

File tree

6 files changed

+119
-65
lines changed

6 files changed

+119
-65
lines changed

docs/api/Overview.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
### Nominatim API
2-
31
!!! Attention
42
The current version of Nominatim implements two different search frontends:
53
the old PHP frontend and the new Python frontend. They have a very similar

docs/extra.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ th {
3434
.md-footer__inner {
3535
display: none;
3636
}
37+
38+
.headerlink {
39+
filter: grayscale(100%);
40+
font-size: 80%;
41+
}

docs/library/Configuration.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Configuration
22

33
When using Nominatim through the library, it can be configured in exactly
4-
the same way as when running as a service. This means that you should have
5-
created a [project directory](../admin/Import.md#creating-the-project-directory)
6-
which contains all files belonging to the Nominatim instance. It can also contain
7-
an `.env` file with configuration options. Setting configuration parameters
8-
via environment variables works as well.
4+
the same way as when running as a service. You may instantiate the library
5+
against the [project directory](../admin/Import.md#creating-the-project-directory)
6+
of your Nominatim installation. It contains all files belonging to the
7+
Nominatim instance. This may include an `.env` file with configuration options.
8+
Setting configuration parameters via environment variables works as well.
9+
Alternatively to using the operating system's environment, a set of
10+
configuration parameters may also be passed to the Nomiantim API object.
911

1012
Configuration options are resolved in the following order:
1113

docs/library/Getting-Started.md

Lines changed: 100 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
# Getting Started
22

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.
1419

1520
## Installation
1621

@@ -19,13 +24,17 @@ Follow the [installation](../admin/Installation.md) and
1924
[import](../admin/Import.md) instructions to set up your database.
2025

2126
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+
2231
To install the package from the source tree directly, run:
2332

2433
pip install packaging/nominatim-api
2534

2635
Usually you would want to run this in a virtual environment.
2736

28-
### A simple search example
37+
## A simple search example
2938

3039
To query the Nominatim database you need to first set up a connection. This
3140
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':
3645
!!! example
3746
=== "NominatimAPIAsync"
3847
``` python
39-
from pathlib import Path
4048
import asyncio
4149

4250
import nominatim_api as napi
4351

4452
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)
4855

4956
results = asyncio.run(search('Brugge'))
5057
if not results:
@@ -55,13 +62,10 @@ This code snippet implements a simple search for the town of 'Brugge':
5562

5663
=== "NominatimAPI"
5764
``` python
58-
from pathlib import Path
59-
6065
import nominatim_api as napi
6166

62-
api = napi.NominatimAPI(Path('.'))
63-
64-
results = api.search('Brugge')
67+
with napi.NominatimAPI() as api:
68+
results = api.search('Brugge')
6569

6670
if not results:
6771
print('Cannot find Brugge')
@@ -84,7 +88,7 @@ implementations. The documentation itself will usually refer only to
8488
available only for the synchronous or asynchronous version, this will be
8589
explicitly mentioned.
8690

87-
### Defining which database to use
91+
## Defining which database to use
8892

8993
The [Configuration](../admin/Import.md#configuration-setup-in-env)
9094
section explains how Nominatim is configured using the
@@ -93,76 +97,120 @@ The same configuration mechanism is used with the
9397
Nominatim API library. You should therefore be sure you are familiar with
9498
the section.
9599

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+
101141

102142
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+
```
105150

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:
108154

109155
!!! example
110156
=== "NominatimAPIAsync"
111157
``` python
112-
from pathlib import Path
113158
import asyncio
114-
115159
import nominatim_api as napi
116160

117161
config_params = {
118162
'NOMINATIM_DATABASE_DSN': 'pgsql:dbname=belgium'
119163
}
120164

121165
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)
125168

126169
results = asyncio.run(search('Brugge'))
127170
```
128171

129172
=== "NominatimAPI"
130173
``` python
131-
from pathlib import Path
132-
133174
import nominatim_api as napi
134175

135176
config_params = {
136177
'NOMINATIM_DATABASE_DSN': 'pgsql:dbname=belgium'
137178
}
138179

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')
142182
```
143183

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.
145187

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:
148198

149199
* 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
151201

152202
Again searching for 'Brugge', this time with a nicely formatted result:
153203

154204
!!! example
155205
=== "NominatimAPIAsync"
156206
``` python
157-
from pathlib import Path
158207
import asyncio
159208

160209
import nominatim_api as napi
161210

162211
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)
166214

167215
results = asyncio.run(search('Brugge'))
168216

@@ -174,13 +222,10 @@ Again searching for 'Brugge', this time with a nicely formatted result:
174222

175223
=== "NominatimAPI"
176224
``` python
177-
from pathlib import Path
178-
179225
import nominatim_api as napi
180226

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)
184229

185230
locale = napi.Locales(['fr', 'en'])
186231
for i, result in enumerate(results):
@@ -236,7 +281,7 @@ Bruges, Flandre-Occidentale, Flandre, Belgique
236281

237282
This is a fairly simple way to create a human-readable description. The
238283
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
240285
belongs to or what rank Nominatim has assigned. Use this to adapt the output
241286
to local address formats.
242287

docs/library/Low-Level-DB-Access.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ the placex table:
2424

2525
```
2626
import asyncio
27-
from pathlib import Path
2827
import sqlalchemy as sa
2928
from nominatim_api import NominatimAPIAsync
3029
3130
async def print_table_size():
32-
api = NominatimAPIAsync(Path('.'))
31+
api = NominatimAPIAsync()
3332
3433
async with api.begin() as conn:
3534
cnt = await conn.scalar(sa.select(sa.func.count()).select_from(conn.t.placex))

mkdocs.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
site_name: Nominatim Manual
22
theme:
3+
font: false
34
name: material
45
features:
56
- navigation.tabs
7+
- toc.integrate
8+
plugins:
9+
- privacy
610
copyright: Copyright © Nominatim developer community
711
docs_dir: docs
812
site_url: https://nominatim.org
@@ -68,7 +72,8 @@ markdown_extensions:
6872
alternate_style: true
6973
- def_list
7074
- toc:
71-
permalink:
75+
toc_depth: 4
76+
permalink: 🔗
7277
extra_css: [extra.css, styles.css]
7378
exclude_docs: |
7479
mk_install_instructions.py

0 commit comments

Comments
 (0)