1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Implementation of classes for API access via libraries.
10 from typing import Mapping, Optional, Any, AsyncIterator, Dict, Sequence, List, Tuple
14 from pathlib import Path
16 import sqlalchemy as sa
17 import sqlalchemy.ext.asyncio as sa_asyncio
19 from nominatim.errors import UsageError
20 from nominatim.db.sqlalchemy_schema import SearchTables
21 from nominatim.db.async_core_library import PGCORE_LIB, PGCORE_ERROR
22 from nominatim.config import Configuration
23 from nominatim.api.connection import SearchConnection
24 from nominatim.api.status import get_status, StatusResult
25 from nominatim.api.lookup import get_detailed_place, get_simple_place
26 from nominatim.api.reverse import ReverseGeocoder
27 from nominatim.api.search import ForwardGeocoder, Phrase, PhraseType, make_query_analyzer
28 import nominatim.api.types as ntyp
29 from nominatim.api.results import DetailedResult, ReverseResult, SearchResults
32 class NominatimAPIAsync: #pylint: disable=too-many-instance-attributes
33 """ The main frontend to the Nominatim database implements the
34 functions for lookup, forward and reverse geocoding using
35 asynchronous functions.
37 This class shares most of the functions with its synchronous
38 version. There are some additional functions or parameters,
39 which are documented below.
41 def __init__(self, project_dir: Path,
42 environ: Optional[Mapping[str, str]] = None,
43 loop: Optional[asyncio.AbstractEventLoop] = None) -> None:
44 """ Initiate a new frontend object with synchronous API functions.
47 project_dir: Path to the
48 [project directory](../admin/Import.md#creating-the-project-directory)
49 of the local Nominatim installation.
50 environ: Mapping of [configuration parameters](../customize/Settings.md).
51 When set, replaces any configuration via environment variables.
52 Settings in this mapping also have precedence over any
53 parameters found in the `.env` file of the project directory.
54 loop: The asyncio event loop that will be used when calling
55 functions. Only needed, when a custom event loop is used
56 and the Python version is 3.9 or earlier.
58 self.config = Configuration(project_dir, environ)
59 self.query_timeout = self.config.get_int('QUERY_TIMEOUT') \
60 if self.config.QUERY_TIMEOUT else None
61 self.reverse_restrict_to_country_area = self.config.get_bool('SEARCH_WITHIN_COUNTRIES')
62 self.server_version = 0
64 if sys.version_info >= (3, 10):
65 self._engine_lock = asyncio.Lock()
67 self._engine_lock = asyncio.Lock(loop=loop) # pylint: disable=unexpected-keyword-arg
68 self._engine: Optional[sa_asyncio.AsyncEngine] = None
69 self._tables: Optional[SearchTables] = None
70 self._property_cache: Dict[str, Any] = {'DB:server_version': 0}
73 async def setup_database(self) -> None:
74 """ Set up the SQL engine and connections.
76 This function will be implicitly called when the database is
77 accessed for the first time. You may also call it explicitly to
78 avoid that the first call is delayed by the setup.
80 async with self._engine_lock:
84 extra_args: Dict[str, Any] = {'future': True,
85 'echo': self.config.get_bool('DEBUG_SQL')}
87 is_sqlite = self.config.DATABASE_DSN.startswith('sqlite:')
90 params = dict((p.split('=', 1)
91 for p in self.config.DATABASE_DSN[7:].split(';')))
92 dburl = sa.engine.URL.create('sqlite+aiosqlite',
93 database=params.get('dbname'))
96 dsn = self.config.get_database_params()
97 query = {k: v for k, v in dsn.items()
98 if k not in ('user', 'password', 'dbname', 'host', 'port')}
100 dburl = sa.engine.URL.create(
101 f'postgresql+{PGCORE_LIB}',
102 database=dsn.get('dbname'),
103 username=dsn.get('user'),
104 password=dsn.get('password'),
105 host=dsn.get('host'),
106 port=int(dsn['port']) if 'port' in dsn else None,
108 extra_args['max_overflow'] = 0
109 extra_args['pool_size'] = self.config.get_int('API_POOL_SIZE')
111 engine = sa_asyncio.create_async_engine(dburl, **extra_args)
114 async with engine.begin() as conn:
115 result = await conn.scalar(sa.text('SHOW server_version_num'))
116 server_version = int(result)
117 except (PGCORE_ERROR, sa.exc.OperationalError):
120 if server_version >= 110000 and not is_sqlite:
121 @sa.event.listens_for(engine.sync_engine, "connect")
122 def _on_connect(dbapi_con: Any, _: Any) -> None:
123 cursor = dbapi_con.cursor()
124 cursor.execute("SET jit_above_cost TO '-1'")
125 cursor.execute("SET max_parallel_workers_per_gather TO '0'")
126 # Make sure that all connections get the new settings
130 @sa.event.listens_for(engine.sync_engine, "connect")
131 def _on_sqlite_connect(dbapi_con: Any, _: Any) -> None:
132 dbapi_con.run_async(lambda conn: conn.enable_load_extension(True))
133 cursor = dbapi_con.cursor()
134 cursor.execute("SELECT load_extension('mod_spatialite')")
135 cursor.execute('SELECT SetDecimalPrecision(7)')
136 dbapi_con.run_async(lambda conn: conn.enable_load_extension(False))
138 self._property_cache['DB:server_version'] = server_version
140 self._tables = SearchTables(sa.MetaData(), engine.name) # pylint: disable=no-member
141 self._engine = engine
144 async def close(self) -> None:
145 """ Close all active connections to the database. The NominatimAPIAsync
146 object remains usable after closing. If a new API functions is
147 called, new connections are created.
149 if self._engine is not None:
150 await self._engine.dispose()
153 @contextlib.asynccontextmanager
154 async def begin(self) -> AsyncIterator[SearchConnection]:
155 """ Create a new connection with automatic transaction handling.
157 This function may be used to get low-level access to the database.
158 Refer to the documentation of SQLAlchemy for details how to use
159 the connection object.
161 if self._engine is None:
162 await self.setup_database()
164 assert self._engine is not None
165 assert self._tables is not None
167 async with self._engine.begin() as conn:
168 yield SearchConnection(conn, self._tables, self._property_cache)
171 async def status(self) -> StatusResult:
172 """ Return the status of the database.
175 async with self.begin() as conn:
176 conn.set_query_timeout(self.query_timeout)
177 status = await get_status(conn)
178 except (PGCORE_ERROR, sa.exc.OperationalError):
179 return StatusResult(700, 'Database connection failed')
184 async def details(self, place: ntyp.PlaceRef, **params: Any) -> Optional[DetailedResult]:
185 """ Get detailed information about a place in the database.
187 Returns None if there is no entry under the given ID.
189 details = ntyp.LookupDetails.from_kwargs(params)
190 async with self.begin() as conn:
191 conn.set_query_timeout(self.query_timeout)
193 await make_query_analyzer(conn)
194 return await get_detailed_place(conn, place, details)
197 async def lookup(self, places: Sequence[ntyp.PlaceRef], **params: Any) -> SearchResults:
198 """ Get simple information about a list of places.
200 Returns a list of place information for all IDs that were found.
202 details = ntyp.LookupDetails.from_kwargs(params)
203 async with self.begin() as conn:
204 conn.set_query_timeout(self.query_timeout)
206 await make_query_analyzer(conn)
207 return SearchResults(filter(None,
208 [await get_simple_place(conn, p, details) for p in places]))
211 async def reverse(self, coord: ntyp.AnyPoint, **params: Any) -> Optional[ReverseResult]:
212 """ Find a place by its coordinates. Also known as reverse geocoding.
214 Returns the closest result that can be found or None if
215 no place matches the given criteria.
217 # The following negation handles NaN correctly. Don't change.
218 if not abs(coord[0]) <= 180 or not abs(coord[1]) <= 90:
219 # There are no results to be expected outside valid coordinates.
222 details = ntyp.ReverseDetails.from_kwargs(params)
223 async with self.begin() as conn:
224 conn.set_query_timeout(self.query_timeout)
226 await make_query_analyzer(conn)
227 geocoder = ReverseGeocoder(conn, details,
228 self.reverse_restrict_to_country_area)
229 return await geocoder.lookup(coord)
232 async def search(self, query: str, **params: Any) -> SearchResults:
233 """ Find a place by free-text search. Also known as forward geocoding.
235 query = query.strip()
237 raise UsageError('Nothing to search for.')
239 async with self.begin() as conn:
240 conn.set_query_timeout(self.query_timeout)
241 geocoder = ForwardGeocoder(conn, ntyp.SearchDetails.from_kwargs(params),
242 self.config.get_int('REQUEST_TIMEOUT') \
243 if self.config.REQUEST_TIMEOUT else None)
244 phrases = [Phrase(PhraseType.NONE, p.strip()) for p in query.split(',')]
245 return await geocoder.lookup(phrases)
248 # pylint: disable=too-many-arguments,too-many-branches
249 async def search_address(self, amenity: Optional[str] = None,
250 street: Optional[str] = None,
251 city: Optional[str] = None,
252 county: Optional[str] = None,
253 state: Optional[str] = None,
254 country: Optional[str] = None,
255 postalcode: Optional[str] = None,
256 **params: Any) -> SearchResults:
257 """ Find an address using structured search.
259 async with self.begin() as conn:
260 conn.set_query_timeout(self.query_timeout)
261 details = ntyp.SearchDetails.from_kwargs(params)
263 phrases: List[Phrase] = []
266 phrases.append(Phrase(PhraseType.AMENITY, amenity))
268 phrases.append(Phrase(PhraseType.STREET, street))
270 phrases.append(Phrase(PhraseType.CITY, city))
272 phrases.append(Phrase(PhraseType.COUNTY, county))
274 phrases.append(Phrase(PhraseType.STATE, state))
276 phrases.append(Phrase(PhraseType.POSTCODE, postalcode))
278 phrases.append(Phrase(PhraseType.COUNTRY, country))
281 raise UsageError('Nothing to search for.')
283 if amenity or street:
284 details.restrict_min_max_rank(26, 30)
286 details.restrict_min_max_rank(13, 25)
288 details.restrict_min_max_rank(10, 12)
290 details.restrict_min_max_rank(5, 9)
292 details.restrict_min_max_rank(5, 11)
294 details.restrict_min_max_rank(4, 4)
296 if 'layers' not in params:
297 details.layers = ntyp.DataLayer.ADDRESS
299 details.layers |= ntyp.DataLayer.POI
301 geocoder = ForwardGeocoder(conn, details,
302 self.config.get_int('REQUEST_TIMEOUT') \
303 if self.config.REQUEST_TIMEOUT else None)
304 return await geocoder.lookup(phrases)
307 async def search_category(self, categories: List[Tuple[str, str]],
308 near_query: Optional[str] = None,
309 **params: Any) -> SearchResults:
310 """ Find an object of a certain category near another place.
311 The near place may either be given as an unstructured search
312 query in itself or as coordinates.
315 return SearchResults()
317 details = ntyp.SearchDetails.from_kwargs(params)
318 async with self.begin() as conn:
319 conn.set_query_timeout(self.query_timeout)
321 phrases = [Phrase(PhraseType.NONE, p) for p in near_query.split(',')]
325 await make_query_analyzer(conn)
327 geocoder = ForwardGeocoder(conn, details,
328 self.config.get_int('REQUEST_TIMEOUT') \
329 if self.config.REQUEST_TIMEOUT else None)
330 return await geocoder.lookup_pois(categories, phrases)
335 """ This class provides a thin synchronous wrapper around the asynchronous
336 Nominatim functions. It creates its own event loop and runs each
337 synchronous function call to completion using that loop.
340 def __init__(self, project_dir: Path,
341 environ: Optional[Mapping[str, str]] = None) -> None:
342 """ Initiate a new frontend object with synchronous API functions.
345 project_dir: Path to the
346 [project directory](../admin/Import.md#creating-the-project-directory)
347 of the local Nominatim installation.
348 environ: Mapping of [configuration parameters](../customize/Settings.md).
349 When set, replaces any configuration via environment variables.
350 Settings in this mapping also have precedence over any
351 parameters found in the `.env` file of the project directory.
353 self._loop = asyncio.new_event_loop()
354 self._async_api = NominatimAPIAsync(project_dir, environ, loop=self._loop)
357 def close(self) -> None:
358 """ Close all active connections to the database.
360 This function also closes the asynchronous worker loop making
361 the NominatimAPI object unusuable.
363 self._loop.run_until_complete(self._async_api.close())
368 def config(self) -> Configuration:
369 """ Provide read-only access to the [configuration](#Configuration)
372 return self._async_api.config
374 def status(self) -> StatusResult:
375 """ Return the status of the database as a dataclass object
376 with the fields described below.
379 status(int): A status code as described on the status page.
380 message(str): Either 'OK' or a human-readable message of the
382 software_version(tuple): A tuple with the version of the
383 Nominatim library consisting of (major, minor, patch, db-patch)
385 database_version(tuple): A tuple with the version of the library
386 which was used for the import or last migration.
387 Also consists of (major, minor, patch, db-patch).
388 data_updated(datetime): Timestamp with the age of the data.
390 return self._loop.run_until_complete(self._async_api.status())
393 def details(self, place: ntyp.PlaceRef, **params: Any) -> Optional[DetailedResult]:
394 """ Get detailed information about a place in the database.
396 The result is a dataclass object with the fields described below
397 or `None` if the place could not be found in the database.
400 place: Description of the place to look up. See
401 [Place identification](Input-Parameter-Types.md#place-identification)
402 for the various ways to reference a place.
405 geometry_output (enum): Add the full geometry of the place to the result.
406 Multiple formats may be selected. Note that geometries can become
407 quite large. (Default: none)
408 geometry_simplification (float): Simplification factor to use on
409 the geometries before returning them. The factor expresses
410 the tolerance in degrees from which the geometry may differ.
411 Topology is preserved. (Default: 0.0)
412 address_details (bool): Add detailed information about the places
413 that make up the address of the requested object. (Default: False)
414 linked_places (bool): Add detailed information about the places
415 that link to the result. (Default: False)
416 parented_places (bool): Add detailed information about all places
417 for which the requested object is a parent, i.e. all places for
418 which the object provides the address details.
419 Only POI places can have parents. (Default: False)
420 keywords (bool): Add detailed information about the search terms
424 source_table (enum): Data source of the place. See below for possible values.
425 category (tuple): A tuple of two strings with the primary OSM tag
427 centroid (Point): Point position of the place.
428 place_id (Optional[int]): Internal ID of the place. This ID may differ
429 for the same place between different installations.
430 parent_place_id (Optional(int]): Internal ID of the parent of this
431 place. Only meaning full for POI-like objects (places with a
433 linked_place_id (Optional[int]): Internal ID of the place this object
434 linkes to. When this ID is set then there is no guarantee that
435 the rest of the result information is complete.
436 admin_level (int): Value of the `admin_level` OSM tag. Only meaningful
437 for administrative boundary objects.
438 indexed_date (datetime): Timestamp when the place was last updated.
439 osm_object (Optional[tuple]): OSM type and ID of the place, if available.
440 names (Optional[dict]): Dictionary of names of the place. Keys are
441 usually the corresponding OSM tag keys.
442 address (Optional[dict]): Dictionary of address parts directly
443 attributed to the place. Keys are usually the corresponding
444 OSM tag keys with the `addr:` prefix removed.
445 extratags (Optional[dict]): Dictionary of additional attributes for
446 the place. Usually OSM tag keys and values.
447 housenumber (Optional[str]): House number of the place, normalised
448 for lookup. To get the house number in its original spelling,
449 use `address['housenumber']`.
450 postcode (Optional[str]): Computed postcode for the place. To get
451 directly attributed postcodes, use `address['postcode']` instead.
452 wikipedia (Optional[str]): Reference to a wikipedia site for the place.
453 The string has the format <language code>:<wikipedia title>.
454 rank_address (int): [Address rank](../customize/Ranking.md#address-rank).
455 rank_search (int): [Search rank](../customize/Ranking.md#search-rank).
456 importance (Optional[float]): Relative importance of the place. This is a measure
457 how likely the place will be searched for.
458 country_code (Optional[str]): Country the feature is in as
459 ISO 3166-1 alpha-2 country code.
460 address_rows (Optional[AddressLines]): List of places that make up the
461 computed address. `None` when `address_details` parameter was False.
462 linked_rows (Optional[AddressLines]): List of places that link to the object.
463 `None` when `linked_places` parameter was False.
464 parented_rows (Optional[AddressLines]): List of direct children of the place.
465 `None` when `parented_places` parameter was False.
466 name_keywords (Optional[WordInfos]): List of search words for the name of
467 the place. `None` when `keywords` parameter is set to False.
468 address_keywords (Optional[WordInfos]): List of search word for the address of
469 the place. `None` when `keywords` parameter is set to False.
470 geometry (dict): Dictionary containing the full geometry of the place
471 in the formats requested in the `geometry_output` parameter.
473 return self._loop.run_until_complete(self._async_api.details(place, **params))
476 def lookup(self, places: Sequence[ntyp.PlaceRef], **params: Any) -> SearchResults:
477 """ Get simple information about a list of places.
479 Returns a list of place information for all IDs that were found.
480 Each result is a dataclass with the fields detailed below.
483 places: List of descriptions of the place to look up. See
484 [Place identification](Input-Parameter-Types.md#place-identification)
485 for the various ways to reference a place.
488 geometry_output (enum): Add the full geometry of the place to the result.
489 Multiple formats may be selected. Note that geometries can become
490 quite large. (Default: none)
491 geometry_simplification (float): Simplification factor to use on
492 the geometries before returning them. The factor expresses
493 the tolerance in degrees from which the geometry may differ.
494 Topology is preserved. (Default: 0.0)
495 address_details (bool): Add detailed information about the places
496 that make up the address of the requested object. (Default: False)
497 linked_places (bool): Add detailed information about the places
498 that link to the result. (Default: False)
499 parented_places (bool): Add detailed information about all places
500 for which the requested object is a parent, i.e. all places for
501 which the object provides the address details.
502 Only POI places can have parents. (Default: False)
503 keywords (bool): Add detailed information about the search terms
507 source_table (enum): Data source of the place. See below for possible values.
508 category (tuple): A tuple of two strings with the primary OSM tag
510 centroid (Point): Point position of the place.
511 place_id (Optional[int]): Internal ID of the place. This ID may differ
512 for the same place between different installations.
513 osm_object (Optional[tuple]): OSM type and ID of the place, if available.
514 names (Optional[dict]): Dictionary of names of the place. Keys are
515 usually the corresponding OSM tag keys.
516 address (Optional[dict]): Dictionary of address parts directly
517 attributed to the place. Keys are usually the corresponding
518 OSM tag keys with the `addr:` prefix removed.
519 extratags (Optional[dict]): Dictionary of additional attributes for
520 the place. Usually OSM tag keys and values.
521 housenumber (Optional[str]): House number of the place, normalised
522 for lookup. To get the house number in its original spelling,
523 use `address['housenumber']`.
524 postcode (Optional[str]): Computed postcode for the place. To get
525 directly attributed postcodes, use `address['postcode']` instead.
526 wikipedia (Optional[str]): Reference to a wikipedia site for the place.
527 The string has the format <language code>:<wikipedia title>.
528 rank_address (int): [Address rank](../customize/Ranking.md#address-rank).
529 rank_search (int): [Search rank](../customize/Ranking.md#search-rank).
530 importance (Optional[float]): Relative importance of the place. This is a measure
531 how likely the place will be searched for.
532 country_code (Optional[str]): Country the feature is in as
533 ISO 3166-1 alpha-2 country code.
534 address_rows (Optional[AddressLines]): List of places that make up the
535 computed address. `None` when `address_details` parameter was False.
536 linked_rows (Optional[AddressLines]): List of places that link to the object.
537 `None` when `linked_places` parameter was False.
538 parented_rows (Optional[AddressLines]): List of direct children of the place.
539 `None` when `parented_places` parameter was False.
540 name_keywords (Optional[WordInfos]): List of search words for the name of
541 the place. `None` when `keywords` parameter is set to False.
542 address_keywords (Optional[WordInfos]): List of search word for the address of
543 the place. `None` when `keywords` parameter is set to False.
544 bbox (Bbox): Bounding box of the full geometry of the place.
545 If the place is a single point, then the size of the bounding
546 box is guessed according to the type of place.
547 geometry (dict): Dictionary containing the full geometry of the place
548 in the formats requested in the `geometry_output` parameter.
550 return self._loop.run_until_complete(self._async_api.lookup(places, **params))
553 def reverse(self, coord: ntyp.AnyPoint, **params: Any) -> Optional[ReverseResult]:
554 """ Find a place by its coordinates. Also known as reverse geocoding.
556 Returns the closest result that can be found or `None` if
557 no place matches the given criteria. The result is a dataclass
558 with the fields as detailed below.
561 coord: Coordinate to lookup the place for as a Point
562 or a tuple (x, y). Must be in WGS84 projection.
565 max_rank (int): Highest address rank to return. Can be used to
566 restrict search to streets or settlements.
567 layers (enum): Defines the kind of data to take into account.
568 See description of layers below. (Default: addresses and POIs)
569 geometry_output (enum): Add the full geometry of the place to the result.
570 Multiple formats may be selected. Note that geometries can become
571 quite large. (Default: none)
572 geometry_simplification (float): Simplification factor to use on
573 the geometries before returning them. The factor expresses
574 the tolerance in degrees from which the geometry may differ.
575 Topology is preserved. (Default: 0.0)
576 address_details (bool): Add detailed information about the places
577 that make up the address of the requested object. (Default: False)
578 linked_places (bool): Add detailed information about the places
579 that link to the result. (Default: False)
580 parented_places (bool): Add detailed information about all places
581 for which the requested object is a parent, i.e. all places for
582 which the object provides the address details.
583 Only POI places can have parents. (Default: False)
584 keywords (bool): Add detailed information about the search terms
588 source_table (enum): Data source of the place. See below for possible values.
589 category (tuple): A tuple of two strings with the primary OSM tag
591 centroid (Point): Point position of the place.
592 place_id (Optional[int]): Internal ID of the place. This ID may differ
593 for the same place between different installations.
594 osm_object (Optional[tuple]): OSM type and ID of the place, if available.
595 names (Optional[dict]): Dictionary of names of the place. Keys are
596 usually the corresponding OSM tag keys.
597 address (Optional[dict]): Dictionary of address parts directly
598 attributed to the place. Keys are usually the corresponding
599 OSM tag keys with the `addr:` prefix removed.
600 extratags (Optional[dict]): Dictionary of additional attributes for
601 the place. Usually OSM tag keys and values.
602 housenumber (Optional[str]): House number of the place, normalised
603 for lookup. To get the house number in its original spelling,
604 use `address['housenumber']`.
605 postcode (Optional[str]): Computed postcode for the place. To get
606 directly attributed postcodes, use `address['postcode']` instead.
607 wikipedia (Optional[str]): Reference to a wikipedia site for the place.
608 The string has the format <language code>:<wikipedia title>.
609 rank_address (int): [Address rank](../customize/Ranking.md#address-rank).
610 rank_search (int): [Search rank](../customize/Ranking.md#search-rank).
611 importance (Optional[float]): Relative importance of the place. This is a measure
612 how likely the place will be searched for.
613 country_code (Optional[str]): Country the feature is in as
614 ISO 3166-1 alpha-2 country code.
615 address_rows (Optional[AddressLines]): List of places that make up the
616 computed address. `None` when `address_details` parameter was False.
617 linked_rows (Optional[AddressLines]): List of places that link to the object.
618 `None` when `linked_places` parameter was False.
619 parented_rows (Optional[AddressLines]): List of direct children of the place.
620 `None` when `parented_places` parameter was False.
621 name_keywords (Optional[WordInfos]): List of search words for the name of
622 the place. `None` when `keywords` parameter is set to False.
623 address_keywords (Optional[WordInfos]): List of search word for the address of
624 the place. `None` when `keywords` parameter is set to False.
625 bbox (Bbox): Bounding box of the full geometry of the place.
626 If the place is a single point, then the size of the bounding
627 box is guessed according to the type of place.
628 geometry (dict): Dictionary containing the full geometry of the place
629 in the formats requested in the `geometry_output` parameter.
630 distance (Optional[float]): Distance in degree from the input point.
632 return self._loop.run_until_complete(self._async_api.reverse(coord, **params))
635 def search(self, query: str, **params: Any) -> SearchResults:
636 """ Find a place by free-text search. Also known as forward geocoding.
639 query: Free-form text query searching for a place.
642 max_results (int): Maximum number of results to return. The
643 actual number of results may be less. (Default: 10)
644 min_rank (int): Lowest permissible rank for the result.
645 For addressable places this is the minimum
646 [address rank](../customize/Ranking.md#address-rank). For all
647 other places the [search rank](../customize/Ranking.md#search-rank)
649 max_rank (int): Highest permissible rank for the result. See min_rank above.
650 layers (enum): Defines the kind of data to take into account.
651 See [layers section](Input-Parameter-Types.md#layers) for details.
652 (Default: addresses and POIs)
653 countries (list[str]): Restrict search to countries with the given
654 ISO 3166-1 alpha-2 country code. An empty list (the default)
655 disables this filter.
656 excluded (list[int]): A list of internal IDs of places to exclude
658 viewbox (Optional[Bbox]): Bounding box of an area to focus search on.
659 bounded_viewbox (bool): Consider the bounding box given in `viewbox`
660 as a filter and return only results within the bounding box.
661 near (Optional[Point]): Focus search around the given point and
662 return results ordered by distance to the given point.
663 near_radius (Optional[float]): Restrict results to results within
664 the given distance in degrees of `near` point. Ignored, when
666 categories (list[tuple]): Restrict search to places of the given
667 categories. The category is the main OSM tag assigned to each
668 place. An empty list (the default) disables this filter.
669 geometry_output (enum): Add the full geometry of the place to the result.
670 Multiple formats may be selected. Note that geometries can become
671 quite large. (Default: none)
672 geometry_simplification (float): Simplification factor to use on
673 the geometries before returning them. The factor expresses
674 the tolerance in degrees from which the geometry may differ.
675 Topology is preserved. (Default: 0.0)
676 address_details (bool): Add detailed information about the places
677 that make up the address of the requested object. (Default: False)
678 linked_places (bool): Add detailed information about the places
679 that link to the result. (Default: False)
680 parented_places (bool): Add detailed information about all places
681 for which the requested object is a parent, i.e. all places for
682 which the object provides the address details.
683 Only POI places can have parents. (Default: False)
684 keywords (bool): Add detailed information about the search terms
688 source_table (enum): Data source of the place. See below for possible values.
689 category (tuple): A tuple of two strings with the primary OSM tag
691 centroid (Point): Point position of the place.
692 place_id (Optional[int]): Internal ID of the place. This ID may differ
693 for the same place between different installations.
694 osm_object (Optional[tuple]): OSM type and ID of the place, if available.
695 names (Optional[dict]): Dictionary of names of the place. Keys are
696 usually the corresponding OSM tag keys.
697 address (Optional[dict]): Dictionary of address parts directly
698 attributed to the place. Keys are usually the corresponding
699 OSM tag keys with the `addr:` prefix removed.
700 extratags (Optional[dict]): Dictionary of additional attributes for
701 the place. Usually OSM tag keys and values.
702 housenumber (Optional[str]): House number of the place, normalised
703 for lookup. To get the house number in its original spelling,
704 use `address['housenumber']`.
705 postcode (Optional[str]): Computed postcode for the place. To get
706 directly attributed postcodes, use `address['postcode']` instead.
707 wikipedia (Optional[str]): Reference to a wikipedia site for the place.
708 The string has the format <language code>:<wikipedia title>.
709 rank_address (int): [Address rank](../customize/Ranking.md#address-rank).
710 rank_search (int): [Search rank](../customize/Ranking.md#search-rank).
711 importance (Optional[float]): Relative importance of the place. This is a measure
712 how likely the place will be searched for.
713 country_code (Optional[str]): Country the feature is in as
714 ISO 3166-1 alpha-2 country code.
715 address_rows (Optional[AddressLines]): List of places that make up the
716 computed address. `None` when `address_details` parameter was False.
717 linked_rows (Optional[AddressLines]): List of places that link to the object.
718 `None` when `linked_places` parameter was False.
719 parented_rows (Optional[AddressLines]): List of direct children of the place.
720 `None` when `parented_places` parameter was False.
721 name_keywords (Optional[WordInfos]): List of search words for the name of
722 the place. `None` when `keywords` parameter is set to False.
723 address_keywords (Optional[WordInfos]): List of search word for the address of
724 the place. `None` when `keywords` parameter is set to False.
725 bbox (Bbox): Bounding box of the full geometry of the place.
726 If the place is a single point, then the size of the bounding
727 box is guessed according to the type of place.
728 geometry (dict): Dictionary containing the full geometry of the place
729 in the formats requested in the `geometry_output` parameter.
731 return self._loop.run_until_complete(
732 self._async_api.search(query, **params))
735 # pylint: disable=too-many-arguments
736 def search_address(self, amenity: Optional[str] = None,
737 street: Optional[str] = None,
738 city: Optional[str] = None,
739 county: Optional[str] = None,
740 state: Optional[str] = None,
741 country: Optional[str] = None,
742 postalcode: Optional[str] = None,
743 **params: Any) -> SearchResults:
744 """ Find an address using structured search.
747 amenity: Name of a POI.
748 street: Street and optionally housenumber of the address. If the address
749 does not have a street, then the place the housenumber references to.
750 city: Postal city of the address.
751 county: County equivalent of the address. Does not exist in all
753 state: State or province of the address.
754 country: Country with its full name or its ISO 3166-1 alpha-2 country code.
755 Do not use together with the country_code filter.
756 postalcode: Post code or ZIP for the place.
759 max_results (int): Maximum number of results to return. The
760 actual number of results may be less. (Default: 10)
761 min_rank (int): Lowest permissible rank for the result.
762 For addressable places this is the minimum
763 [address rank](../customize/Ranking.md#address-rank). For all
764 other places the [search rank](../customize/Ranking.md#search-rank)
766 max_rank (int): Highest permissible rank for the result. See min_rank above.
767 layers (enum): Defines the kind of data to take into account.
768 See [layers section](Input-Parameter-Types.md#layers) for details.
769 (Default: addresses and POIs)
770 countries (list[str]): Restrict search to countries with the given
771 ISO 3166-1 alpha-2 country code. An empty list (the default)
772 disables this filter. Do not use, when the country parameter
774 excluded (list[int]): A list of internal IDs of places to exclude
776 viewbox (Optional[Bbox]): Bounding box of an area to focus search on.
777 bounded_viewbox (bool): Consider the bounding box given in `viewbox`
778 as a filter and return only results within the bounding box.
779 near (Optional[Point]): Focus search around the given point and
780 return results ordered by distance to the given point.
781 near_radius (Optional[float]): Restrict results to results within
782 the given distance in degrees of `near` point. Ignored, when
784 categories (list[tuple]): Restrict search to places of the given
785 categories. The category is the main OSM tag assigned to each
786 place. An empty list (the default) disables this filter.
787 geometry_output (enum): Add the full geometry of the place to the result.
788 Multiple formats may be selected. Note that geometries can become
789 quite large. (Default: none)
790 geometry_simplification (float): Simplification factor to use on
791 the geometries before returning them. The factor expresses
792 the tolerance in degrees from which the geometry may differ.
793 Topology is preserved. (Default: 0.0)
794 address_details (bool): Add detailed information about the places
795 that make up the address of the requested object. (Default: False)
796 linked_places (bool): Add detailed information about the places
797 that link to the result. (Default: False)
798 parented_places (bool): Add detailed information about all places
799 for which the requested object is a parent, i.e. all places for
800 which the object provides the address details.
801 Only POI places can have parents. (Default: False)
802 keywords (bool): Add detailed information about the search terms
806 source_table (enum): Data source of the place. See below for possible values.
807 category (tuple): A tuple of two strings with the primary OSM tag
809 centroid (Point): Point position of the place.
810 place_id (Optional[int]): Internal ID of the place. This ID may differ
811 for the same place between different installations.
812 osm_object (Optional[tuple]): OSM type and ID of the place, if available.
813 names (Optional[dict]): Dictionary of names of the place. Keys are
814 usually the corresponding OSM tag keys.
815 address (Optional[dict]): Dictionary of address parts directly
816 attributed to the place. Keys are usually the corresponding
817 OSM tag keys with the `addr:` prefix removed.
818 extratags (Optional[dict]): Dictionary of additional attributes for
819 the place. Usually OSM tag keys and values.
820 housenumber (Optional[str]): House number of the place, normalised
821 for lookup. To get the house number in its original spelling,
822 use `address['housenumber']`.
823 postcode (Optional[str]): Computed postcode for the place. To get
824 directly attributed postcodes, use `address['postcode']` instead.
825 wikipedia (Optional[str]): Reference to a wikipedia site for the place.
826 The string has the format <language code>:<wikipedia title>.
827 rank_address (int): [Address rank](../customize/Ranking.md#address-rank).
828 rank_search (int): [Search rank](../customize/Ranking.md#search-rank).
829 importance (Optional[float]): Relative importance of the place. This is a measure
830 how likely the place will be searched for.
831 country_code (Optional[str]): Country the feature is in as
832 ISO 3166-1 alpha-2 country code.
833 address_rows (Optional[AddressLines]): List of places that make up the
834 computed address. `None` when `address_details` parameter was False.
835 linked_rows (Optional[AddressLines]): List of places that link to the object.
836 `None` when `linked_places` parameter was False.
837 parented_rows (Optional[AddressLines]): List of direct children of the place.
838 `None` when `parented_places` parameter was False.
839 name_keywords (Optional[WordInfos]): List of search words for the name of
840 the place. `None` when `keywords` parameter is set to False.
841 address_keywords (Optional[WordInfos]): List of search word for the address of
842 the place. `None` when `keywords` parameter is set to False.
843 bbox (Bbox): Bounding box of the full geometry of the place.
844 If the place is a single point, then the size of the bounding
845 box is guessed according to the type of place.
846 geometry (dict): Dictionary containing the full geometry of the place
847 in the formats requested in the `geometry_output` parameter.
849 return self._loop.run_until_complete(
850 self._async_api.search_address(amenity, street, city, county,
851 state, country, postalcode, **params))
854 def search_category(self, categories: List[Tuple[str, str]],
855 near_query: Optional[str] = None,
856 **params: Any) -> SearchResults:
857 """ Find an object of a certain category near another place.
859 The near place may either be given as an unstructured search
860 query in itself or as a geographic area through the
861 viewbox or near parameters.
864 categories: Restrict search to places of the given
865 categories. The category is the main OSM tag assigned to each
867 near_query: Optional free-text query to define the are to
871 max_results (int): Maximum number of results to return. The
872 actual number of results may be less. (Default: 10)
873 min_rank (int): Lowest permissible rank for the result.
874 For addressable places this is the minimum
875 [address rank](../customize/Ranking.md#address-rank). For all
876 other places the [search rank](../customize/Ranking.md#search-rank)
878 max_rank (int): Highest permissible rank for the result. See min_rank above.
879 layers (enum): Defines the kind of data to take into account.
880 See [layers section](Input-Parameter-Types.md#layers) for details.
881 (Default: addresses and POIs)
882 countries (list[str]): Restrict search to countries with the given
883 ISO 3166-1 alpha-2 country code. An empty list (the default)
884 disables this filter.
885 excluded (list[int]): A list of internal IDs of places to exclude
887 viewbox (Optional[Bbox]): Bounding box of an area to focus search on.
888 bounded_viewbox (bool): Consider the bounding box given in `viewbox`
889 as a filter and return only results within the bounding box.
890 near (Optional[Point]): Focus search around the given point and
891 return results ordered by distance to the given point.
892 near_radius (Optional[float]): Restrict results to results within
893 the given distance in degrees of `near` point. Ignored, when
895 geometry_output (enum): Add the full geometry of the place to the result.
896 Multiple formats may be selected. Note that geometries can become
897 quite large. (Default: none)
898 geometry_simplification (float): Simplification factor to use on
899 the geometries before returning them. The factor expresses
900 the tolerance in degrees from which the geometry may differ.
901 Topology is preserved. (Default: 0.0)
902 address_details (bool): Add detailed information about the places
903 that make up the address of the requested object. (Default: False)
904 linked_places (bool): Add detailed information about the places
905 that link to the result. (Default: False)
906 parented_places (bool): Add detailed information about all places
907 for which the requested object is a parent, i.e. all places for
908 which the object provides the address details.
909 Only POI places can have parents. (Default: False)
910 keywords (bool): Add detailed information about the search terms
914 source_table (enum): Data source of the place. See below for possible values.
915 category (tuple): A tuple of two strings with the primary OSM tag
917 centroid (Point): Point position of the place.
918 place_id (Optional[int]): Internal ID of the place. This ID may differ
919 for the same place between different installations.
920 osm_object (Optional[tuple]): OSM type and ID of the place, if available.
921 names (Optional[dict]): Dictionary of names of the place. Keys are
922 usually the corresponding OSM tag keys.
923 address (Optional[dict]): Dictionary of address parts directly
924 attributed to the place. Keys are usually the corresponding
925 OSM tag keys with the `addr:` prefix removed.
926 extratags (Optional[dict]): Dictionary of additional attributes for
927 the place. Usually OSM tag keys and values.
928 housenumber (Optional[str]): House number of the place, normalised
929 for lookup. To get the house number in its original spelling,
930 use `address['housenumber']`.
931 postcode (Optional[str]): Computed postcode for the place. To get
932 directly attributed postcodes, use `address['postcode']` instead.
933 wikipedia (Optional[str]): Reference to a wikipedia site for the place.
934 The string has the format <language code>:<wikipedia title>.
935 rank_address (int): [Address rank](../customize/Ranking.md#address-rank).
936 rank_search (int): [Search rank](../customize/Ranking.md#search-rank).
937 importance (Optional[float]): Relative importance of the place. This is a measure
938 how likely the place will be searched for.
939 country_code (Optional[str]): Country the feature is in as
940 ISO 3166-1 alpha-2 country code.
941 address_rows (Optional[AddressLines]): List of places that make up the
942 computed address. `None` when `address_details` parameter was False.
943 linked_rows (Optional[AddressLines]): List of places that link to the object.
944 `None` when `linked_places` parameter was False.
945 parented_rows (Optional[AddressLines]): List of direct children of the place.
946 `None` when `parented_places` parameter was False.
947 name_keywords (Optional[WordInfos]): List of search words for the name of
948 the place. `None` when `keywords` parameter is set to False.
949 address_keywords (Optional[WordInfos]): List of search word for the address of
950 the place. `None` when `keywords` parameter is set to False.
951 bbox (Bbox): Bounding box of the full geometry of the place.
952 If the place is a single point, then the size of the bounding
953 box is guessed according to the type of place.
954 geometry (dict): Dictionary containing the full geometry of the place
955 in the formats requested in the `geometry_output` parameter.
957 return self._loop.run_until_complete(
958 self._async_api.search_category(categories, near_query, **params))