X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/ef1b52eee517d826c7c0e664f9a539b8ceffaaf6..043d52821cba22ca8b430be7c178275325bb2f93:/docs/library/Getting-Started.md diff --git a/docs/library/Getting-Started.md b/docs/library/Getting-Started.md index 77724e67..1f5b2baa 100644 --- a/docs/library/Getting-Started.md +++ b/docs/library/Getting-Started.md @@ -3,8 +3,7 @@ The Nominatim search frontend can directly be used as a Python library in scripts and applications. When you have imported your own Nominatim database, then it is no longer necessary to run a full web service for it and access -the database through http requests. With the Nominatim library it is possible -to access all search functionality directly from your Python code. There are +the database through http requests. There are also less constraints on the kinds of data that can be accessed. The library allows to get access to more detailed information about the objects saved in the database. @@ -13,22 +12,18 @@ in the database. The library interface is currently in an experimental stage. There might be some smaller adjustments to the public interface until the next version. - The library also misses a proper installation routine, so some manipulation - of the PYTHONPATH is required. Use is only recommended for advanced Python - programmers at the moment. - ## Installation To use the Nominatim library, you need access to a local Nominatim database. -Follow the [installation and import instructions](../admin/) to set up your -database. +Follow the [installation](../admin/Installation.md) and +[import](../admin/Import.md) instructions to set up your database. + +The Nominatim frontend library is contained in the Python package `nominatim-api`. +To install the package from the source tree directly, run: + + pip install packaging/nominatim-api -It is not yet possible to install it in the usual way via pip or inside a -virtualenv. To get access to the library you need to set an appropriate -PYTHONPATH. With the default installation, the python library can be found -under `/usr/local/share/nominatim/lib-python`. If you have installed -Nominatim under a different prefix, adapt the `/usr/local/` part accordingly. -You can also point the PYTHONPATH to the Nominatim source code. +Usually you would want to run this in a virtual environment. ### A simple search example @@ -36,7 +31,7 @@ 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 search functions of Nominatim that are also known from its web API. -This code snippet implements a simple search for the town if 'Brugge': +This code snippet implements a simple search for the town of 'Brugge': !!! example === "NominatimAPIAsync" @@ -44,7 +39,7 @@ This code snippet implements a simple search for the town if 'Brugge': from pathlib import Path import asyncio - import nominatim.api as napi + import nominatim_api as napi async def search(query): api = napi.NominatimAPIAsync(Path('.')) @@ -55,14 +50,14 @@ This code snippet implements a simple search for the town if 'Brugge': if not results: print('Cannot find Brugge') else: - print(f'Found a place at {results[0].centroid.x},{results[1].centroid.y}') + print(f'Found a place at {results[0].centroid.x},{results[0].centroid.y}') ``` === "NominatimAPI" ``` python from pathlib import Path - import nominatim.api as napi + import nominatim_api as napi api = napi.NominatimAPI(Path('.')) @@ -71,7 +66,7 @@ This code snippet implements a simple search for the town if 'Brugge': if not results: print('Cannot find Brugge') else: - print(f'Found a place at {results[0].centroid.x},{results[1].centroid.y}') + print(f'Found a place at {results[0].centroid.x},{results[0].centroid.y}') ``` The Nominatim library is designed around @@ -83,8 +78,8 @@ significantly. For smaller scripts there is also a synchronous wrapper around the API. By using `NominatimAPI`, you get exactly the same interface using classic functions. -The examples in this chapter will always show how work with both of the -implementations. The documentation itself will refer usually only to +The examples in this chapter will always show-case both +implementations. The documentation itself will usually refer only to 'Nominatim API class' when both flavours are meant. If a functionality is available only for the synchronous or asynchronous version, this will be explicitly mentioned. @@ -104,7 +99,7 @@ You should have set up this directory as part of the Nominatim import. Any configuration found in the `.env` file in this directory will automatically used. -The second way to configure your Nominatim setup is through environment variables. +You may also configure Nominatim by setting environment variables. Normally, Nominatim will check the operating system environment. This can be overwritten by giving the constructor a dictionary of configuration parameters. @@ -117,7 +112,7 @@ standard 'nominatim' database: from pathlib import Path import asyncio - import nominatim.api as napi + import nominatim_api as napi config_params = { 'NOMINATIM_DATABASE_DSN': 'pgsql:dbname=belgium' @@ -135,7 +130,7 @@ standard 'nominatim' database: ``` python from pathlib import Path - import nominatim.api as napi + import nominatim_api as napi config_params = { 'NOMINATIM_DATABASE_DSN': 'pgsql:dbname=belgium' @@ -162,7 +157,7 @@ Again searching for 'Brugge', this time with a nicely formatted result: from pathlib import Path import asyncio - import nominatim.api as napi + import nominatim_api as napi async def search(query): api = napi.NominatimAPIAsync(Path('.')) @@ -181,7 +176,7 @@ Again searching for 'Brugge', this time with a nicely formatted result: ``` python from pathlib import Path - import nominatim.api as napi + import nominatim_api as napi api = napi.NominatimAPI(Path('.')) @@ -209,8 +204,8 @@ results should be presented. As with the names in the result itself, the places in `address_rows` contain all possible name translation for each row. The library has a helper class `Locale` which helps extracting a name of a -place in the preferred language. It gets a list of language code in the -order of preference. So +place in the preferred language. It takes a single parameter with a list +of language codes in the order of preference. So ``` python locale = napi.Locale(['fr', 'en']) @@ -220,7 +215,7 @@ creates a helper class that returns the name preferably in French. If that is not possible, it tries English and eventually falls back to the default `name` or `ref`. -The Locale object can be applied to a name dictionary to return the best-matching +The `Locale` object can be applied to a name dictionary to return the best-matching name out of it: ``` python