Merge pull request #3532 from lonvia/refresh-docs

Update library documentation
This commit is contained in:
Sarah Hoffmann 2024-09-11 10:27:09 +02:00 committed by GitHub
commit a66c063314
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 120 additions and 66 deletions

View File

@ -1,5 +1,3 @@
### Nominatim API
!!! Attention !!! Attention
The current version of Nominatim implements two different search frontends: The current version of Nominatim implements two different search frontends:
the old PHP frontend and the new Python frontend. They have a very similar the old PHP frontend and the new Python frontend. They have a very similar

View File

@ -34,3 +34,8 @@ th {
.md-footer__inner { .md-footer__inner {
display: none; display: none;
} }
.headerlink {
filter: grayscale(100%);
font-size: 80%;
}

View File

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

View File

@ -1,16 +1,21 @@
# Getting Started # Getting Started
The Nominatim search frontend can directly be used as a Python library in The Nominatim search frontend is implemented as a Python library and can as
scripts and applications. When you have imported your own Nominatim database, such directly be used in Python scripts and applications. You don't need to
then it is no longer necessary to run a full web service for it and access set up a web frontend and access it through HTTP calls. The library gives
the database through http requests. There are direct access to the Nominatim database through similar search functions as
also less constraints on the kinds of data that can be accessed. The library offered by the web API. In addition, it will give you a more complete and
allows to get access to more detailed information about the objects saved detailed view on the search objects stored in the database.
in the database.
!!! danger !!! warning
The library interface is currently in an experimental stage. There might
be some smaller adjustments to the public interface until the next version. The Nominatim library is used for accessing a local Nominatim database.
It is not meant to be used against web services of Nominatim like the
one on https://nominatim.openstreetmap.org. If you need a Python library
to access these web services, have a look at
[GeoPy](https://geopy.readthedocs.io). Don't forget to consult the
usage policy of the service you want to use before accessing such
a web service.
## Installation ## Installation
@ -19,13 +24,17 @@ Follow the [installation](../admin/Installation.md) and
[import](../admin/Import.md) instructions to set up your database. [import](../admin/Import.md) instructions to set up your database.
The Nominatim frontend library is contained in the Python package `nominatim-api`. The Nominatim frontend library is contained in the Python package `nominatim-api`.
You can install the latest released version directly from pip:
pip install nominatim-api
To install the package from the source tree directly, run: To install the package from the source tree directly, run:
pip install packaging/nominatim-api pip install packaging/nominatim-api
Usually you would want to run this in a virtual environment. Usually you would want to run this in a virtual environment.
### A simple search example ## A simple search example
To query the Nominatim database you need to first set up a connection. This To query the Nominatim database you need to first set up a connection. This
is done by creating an Nominatim API object. This object exposes all the 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':
!!! example !!! example
=== "NominatimAPIAsync" === "NominatimAPIAsync"
``` python ``` python
from pathlib import Path
import asyncio import asyncio
import nominatim_api as napi import nominatim_api as napi
async def search(query): async def search(query):
api = napi.NominatimAPIAsync(Path('.')) async with napi.NominatimAPIAsync() as api:
return await api.search(query)
return await api.search(query)
results = asyncio.run(search('Brugge')) results = asyncio.run(search('Brugge'))
if not results: if not results:
@ -55,13 +62,10 @@ This code snippet implements a simple search for the town of 'Brugge':
=== "NominatimAPI" === "NominatimAPI"
``` python ``` python
from pathlib import Path
import nominatim_api as napi import nominatim_api as napi
api = napi.NominatimAPI(Path('.')) with napi.NominatimAPI() as api:
results = api.search('Brugge')
results = api.search('Brugge')
if not results: if not results:
print('Cannot find Brugge') print('Cannot find Brugge')
@ -84,7 +88,7 @@ implementations. The documentation itself will usually refer only to
available only for the synchronous or asynchronous version, this will be available only for the synchronous or asynchronous version, this will be
explicitly mentioned. explicitly mentioned.
### Defining which database to use ## Defining which database to use
The [Configuration](../admin/Import.md#configuration-setup-in-env) The [Configuration](../admin/Import.md#configuration-setup-in-env)
section explains how Nominatim is configured using the section explains how Nominatim is configured using the
@ -93,76 +97,120 @@ The same configuration mechanism is used with the
Nominatim API library. You should therefore be sure you are familiar with Nominatim API library. You should therefore be sure you are familiar with
the section. the section.
The constructor of the 'Nominatim API class' takes one mandatory parameter: There are three different ways, how configuration options can be set for
the path to the [project directory](../admin/Import.md#creating-the-project-directory). a 'Nominatim API class'. When you have set up your Nominatim database, you
You should have set up this directory as part of the Nominatim import. have normally created a [project directory](../admin/Import.md#creating-the-project-directory)
Any configuration found in the `.env` file in this directory will automatically which stores the various configuration and customization files that Nominatim
used. needs. You may pass the location of the project directory to your
'Nominatim API class' constructor and it will read the .env file in the
You may also configure Nominatim by setting environment variables. directory and set the configuration accordingly. Here is the simple search
Normally, Nominatim will check the operating system environment. This can be example, using the configuration from a pre-defined project directory in
overwritten by giving the constructor a dictionary of configuration parameters. `/srv/nominatim-project`:
Let us look up 'Brugge' in the special database named 'belgium' instead of the
standard 'nominatim' database:
!!! example !!! example
=== "NominatimAPIAsync" === "NominatimAPIAsync"
``` python ``` python
from pathlib import Path
import asyncio import asyncio
import nominatim_api as napi import nominatim_api as napi
async def search(query):
async with napi.NominatimAPIAsync('/srv/nominatim-project') as api:
return await api.search(query)
results = asyncio.run(search('Brugge'))
if not results:
print('Cannot find Brugge')
else:
print(f'Found a place at {results[0].centroid.x},{results[0].centroid.y}')
```
=== "NominatimAPI"
``` python
import nominatim_api as napi
with napi.NominatimAPI('/srv/nominatim-project') as api:
results = api.search('Brugge')
if not results:
print('Cannot find Brugge')
else:
print(f'Found a place at {results[0].centroid.x},{results[0].centroid.y}')
```
You may also configure Nominatim by setting environment variables.
Normally Nominatim will check the operating system environment. Lets
say you want to look up 'Brugge' in the special database named 'belgium' instead of the
standard 'nominatim' database. You can run the example script above like this:
```
NOMINATIM_DATABASE_DSN=pgsql:dbname=belgium python3 example.py
```
The third option to configure the library is to hand in the configuration
parameters into the 'Nominatim API class'. Changing the database would look
like this:
!!! example
=== "NominatimAPIAsync"
``` python
import asyncio
import nominatim_api as napi
config_params = { config_params = {
'NOMINATIM_DATABASE_DSN': 'pgsql:dbname=belgium' 'NOMINATIM_DATABASE_DSN': 'pgsql:dbname=belgium'
} }
async def search(query): async def search(query):
api = napi.NominatimAPIAsync(Path('.'), environ=config_params) async with napi.NominatimAPIAsync(environ=config_params) as api:
return await api.search(query)
return await api.search(query)
results = asyncio.run(search('Brugge')) results = asyncio.run(search('Brugge'))
``` ```
=== "NominatimAPI" === "NominatimAPI"
``` python ``` python
from pathlib import Path
import nominatim_api as napi import nominatim_api as napi
config_params = { config_params = {
'NOMINATIM_DATABASE_DSN': 'pgsql:dbname=belgium' 'NOMINATIM_DATABASE_DSN': 'pgsql:dbname=belgium'
} }
api = napi.NominatimAPI(Path('.'), environ=config_params) with napi.NominatimAPI(environ=config_params) as api:
results = api.search('Brugge')
results = api.search('Brugge')
``` ```
### Presenting results to humans When the `environ` parameter is given, then only configuration variables
from this dictionary will be used. The operating system's environment
variables will be ignored.
All search functions return the raw results from the database. There is no ## Presenting results to humans
full human-readable label. To create such a label, you need two things:
All search functions return full result objects from the database. Such a
result object contains lots of details: names, address information, OSM tags etc.
This gives you lots of flexibility what to do with the results.
One of the most common things to get is some kind of human-readable label
that describes the result in a compact form. Usually this would be the name
of the object and some parts of the address to explain where in the world
it is. To create such a label, you need two things:
* the address details of the place * the address details of the place
* adapt the result to the language you wish to use for display * all names for the label adapted to the language you wish to use for display
Again searching for 'Brugge', this time with a nicely formatted result: Again searching for 'Brugge', this time with a nicely formatted result:
!!! example !!! example
=== "NominatimAPIAsync" === "NominatimAPIAsync"
``` python ``` python
from pathlib import Path
import asyncio import asyncio
import nominatim_api as napi import nominatim_api as napi
async def search(query): async def search(query):
api = napi.NominatimAPIAsync(Path('.')) async with napi.NominatimAPIAsync() as api:
return await api.search(query, address_details=True)
return await api.search(query, address_details=True)
results = asyncio.run(search('Brugge')) results = asyncio.run(search('Brugge'))
@ -174,13 +222,10 @@ Again searching for 'Brugge', this time with a nicely formatted result:
=== "NominatimAPI" === "NominatimAPI"
``` python ``` python
from pathlib import Path
import nominatim_api as napi import nominatim_api as napi
api = napi.NominatimAPI(Path('.')) with napi.NominatimAPI() as api:
results = api.search('Brugge', address_details=True)
results = api.search('Brugge', address_details=True)
locale = napi.Locales(['fr', 'en']) locale = napi.Locales(['fr', 'en'])
for i, result in enumerate(results): for i, result in enumerate(results):
@ -236,7 +281,7 @@ Bruges, Flandre-Occidentale, Flandre, Belgique
This is a fairly simple way to create a human-readable description. The This is a fairly simple way to create a human-readable description. The
place information in `address_rows` contains further information about each place information in `address_rows` contains further information about each
place. For example, which OSM `adlin_level` was used, what category the place place. For example, which OSM `admin_level` was used, what category the place
belongs to or what rank Nominatim has assigned. Use this to adapt the output belongs to or what rank Nominatim has assigned. Use this to adapt the output
to local address formats. to local address formats.

View File

@ -24,12 +24,11 @@ the placex table:
``` ```
import asyncio import asyncio
from pathlib import Path
import sqlalchemy as sa import sqlalchemy as sa
from nominatim_api import NominatimAPIAsync from nominatim_api import NominatimAPIAsync
async def print_table_size(): async def print_table_size():
api = NominatimAPIAsync(Path('.')) api = NominatimAPIAsync()
async with api.begin() as conn: async with api.begin() as conn:
cnt = await conn.scalar(sa.select(sa.func.count()).select_from(conn.t.placex)) cnt = await conn.scalar(sa.select(sa.func.count()).select_from(conn.t.placex))

View File

@ -1,8 +1,12 @@
site_name: Nominatim Manual site_name: Nominatim Manual
theme: theme:
font: false
name: material name: material
features: features:
- navigation.tabs - navigation.tabs
- toc.integrate
plugins:
- privacy
copyright: Copyright © Nominatim developer community copyright: Copyright © Nominatim developer community
docs_dir: docs docs_dir: docs
site_url: https://nominatim.org site_url: https://nominatim.org
@ -68,7 +72,8 @@ markdown_extensions:
alternate_style: true alternate_style: true
- def_list - def_list
- toc: - toc:
permalink: toc_depth: 4
permalink: 🔗
extra_css: [extra.css, styles.css] extra_css: [extra.css, styles.css]
exclude_docs: | exclude_docs: |
mk_install_instructions.py mk_install_instructions.py