mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-07 02:24:08 +00:00
switch CLI lookup command to Python implementation
This commit is contained in:
@@ -10,7 +10,7 @@ Hard-coded information about tag catagories.
|
||||
These tables have been copied verbatim from the old PHP code. For future
|
||||
version a more flexible formatting is required.
|
||||
"""
|
||||
from typing import Tuple, Optional, Mapping
|
||||
from typing import Tuple, Optional, Mapping, Union
|
||||
|
||||
import nominatim.api as napi
|
||||
|
||||
@@ -41,7 +41,7 @@ def get_label_tag(category: Tuple[str, str], extratags: Optional[Mapping[str, st
|
||||
return label.lower().replace(' ', '_')
|
||||
|
||||
|
||||
def bbox_from_result(result: napi.ReverseResult) -> napi.Bbox:
|
||||
def bbox_from_result(result: Union[napi.ReverseResult, napi.SearchResult]) -> napi.Bbox:
|
||||
""" Compute a bounding box for the result. For ways and relations
|
||||
a given boundingbox is used. For all other object, a box is computed
|
||||
around the centroid according to dimensions dereived from the
|
||||
|
||||
@@ -198,33 +198,33 @@ def _format_reverse_jsonv2(results: napi.ReverseResults,
|
||||
|
||||
|
||||
@dispatch.format_func(napi.SearchResults, 'xml')
|
||||
def _format_reverse_xml(results: napi.SearchResults, options: Mapping[str, Any]) -> str:
|
||||
def _format_search_xml(results: napi.SearchResults, options: Mapping[str, Any]) -> str:
|
||||
return format_xml.format_base_xml(results,
|
||||
options, False, 'searchresults',
|
||||
{'querystring': 'TODO'})
|
||||
|
||||
|
||||
@dispatch.format_func(napi.SearchResults, 'geojson')
|
||||
def _format_reverse_geojson(results: napi.SearchResults,
|
||||
def _format_search_geojson(results: napi.SearchResults,
|
||||
options: Mapping[str, Any]) -> str:
|
||||
return format_json.format_base_geojson(results, options, False)
|
||||
|
||||
|
||||
@dispatch.format_func(napi.SearchResults, 'geocodejson')
|
||||
def _format_reverse_geocodejson(results: napi.SearchResults,
|
||||
def _format_search_geocodejson(results: napi.SearchResults,
|
||||
options: Mapping[str, Any]) -> str:
|
||||
return format_json.format_base_geocodejson(results, options, False)
|
||||
|
||||
|
||||
@dispatch.format_func(napi.SearchResults, 'json')
|
||||
def _format_reverse_json(results: napi.SearchResults,
|
||||
def _format_search_json(results: napi.SearchResults,
|
||||
options: Mapping[str, Any]) -> str:
|
||||
return format_json.format_base_json(results, options, False,
|
||||
class_label='class')
|
||||
|
||||
|
||||
@dispatch.format_func(napi.SearchResults, 'jsonv2')
|
||||
def _format_reverse_jsonv2(results: napi.SearchResults,
|
||||
def _format_search_jsonv2(results: napi.SearchResults,
|
||||
options: Mapping[str, Any]) -> str:
|
||||
return format_json.format_base_json(results, options, False,
|
||||
class_label='category')
|
||||
|
||||
@@ -7,12 +7,14 @@
|
||||
"""
|
||||
Helper functions for output of results in json formats.
|
||||
"""
|
||||
from typing import Mapping, Any, Optional, Tuple
|
||||
from typing import Mapping, Any, Optional, Tuple, Union
|
||||
|
||||
import nominatim.api as napi
|
||||
import nominatim.api.v1.classtypes as cl
|
||||
from nominatim.utils.json_writer import JsonWriter
|
||||
|
||||
#pylint: disable=too-many-branches
|
||||
|
||||
def _write_osm_id(out: JsonWriter, osm_object: Optional[Tuple[str, int]]) -> None:
|
||||
if osm_object is not None:
|
||||
out.keyval_not_none('osm_type', cl.OSM_TYPE_NAME.get(osm_object[0], None))\
|
||||
@@ -61,7 +63,7 @@ def _write_geocodejson_address(out: JsonWriter,
|
||||
out.keyval('country_code', country_code)
|
||||
|
||||
|
||||
def format_base_json(results: napi.ReverseResults, #pylint: disable=too-many-branches
|
||||
def format_base_json(results: Union[napi.ReverseResults, napi.SearchResults],
|
||||
options: Mapping[str, Any], simple: bool,
|
||||
class_label: str) -> str:
|
||||
""" Return the result list as a simple json string in custom Nominatim format.
|
||||
@@ -141,7 +143,7 @@ def format_base_json(results: napi.ReverseResults, #pylint: disable=too-many-bra
|
||||
return out()
|
||||
|
||||
|
||||
def format_base_geojson(results: napi.ReverseResults,
|
||||
def format_base_geojson(results: Union[napi.ReverseResults, napi.SearchResults],
|
||||
options: Mapping[str, Any],
|
||||
simple: bool) -> str:
|
||||
""" Return the result list as a geojson string.
|
||||
@@ -210,7 +212,7 @@ def format_base_geojson(results: napi.ReverseResults,
|
||||
return out()
|
||||
|
||||
|
||||
def format_base_geocodejson(results: napi.ReverseResults,
|
||||
def format_base_geocodejson(results: Union[napi.ReverseResults, napi.SearchResults],
|
||||
options: Mapping[str, Any], simple: bool) -> str:
|
||||
""" Return the result list as a geocodejson string.
|
||||
"""
|
||||
|
||||
@@ -7,13 +7,15 @@
|
||||
"""
|
||||
Helper functions for output of results in XML format.
|
||||
"""
|
||||
from typing import Mapping, Any, Optional
|
||||
from typing import Mapping, Any, Optional, Union
|
||||
import datetime as dt
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
import nominatim.api as napi
|
||||
import nominatim.api.v1.classtypes as cl
|
||||
|
||||
#pylint: disable=too-many-branches
|
||||
|
||||
def _write_xml_address(root: ET.Element, address: napi.AddressLines,
|
||||
country_code: Optional[str]) -> None:
|
||||
parts = {}
|
||||
@@ -34,7 +36,7 @@ def _write_xml_address(root: ET.Element, address: napi.AddressLines,
|
||||
ET.SubElement(root, 'country_code').text = country_code
|
||||
|
||||
|
||||
def _create_base_entry(result: napi.ReverseResult, #pylint: disable=too-many-branches
|
||||
def _create_base_entry(result: Union[napi.ReverseResult, napi.SearchResult],
|
||||
root: ET.Element, simple: bool,
|
||||
locales: napi.Locales) -> ET.Element:
|
||||
if result.address_rows:
|
||||
@@ -86,7 +88,7 @@ def _create_base_entry(result: napi.ReverseResult, #pylint: disable=too-many-bra
|
||||
return place
|
||||
|
||||
|
||||
def format_base_xml(results: napi.ReverseResults,
|
||||
def format_base_xml(results: Union[napi.ReverseResults, napi.SearchResults],
|
||||
options: Mapping[str, Any],
|
||||
simple: bool, xml_root_tag: str,
|
||||
xml_extra_info: Mapping[str, str]) -> str:
|
||||
|
||||
@@ -227,8 +227,11 @@ class ASGIAdaptor(abc.ABC):
|
||||
|
||||
|
||||
def parse_geometry_details(self, fmt: str) -> napi.LookupDetails:
|
||||
""" Create details strucutre from the supplied geometry parameters.
|
||||
"""
|
||||
details = napi.LookupDetails(address_details=True,
|
||||
geometry_simplification=self.get_float('polygon_threshold', 0.0))
|
||||
geometry_simplification=
|
||||
self.get_float('polygon_threshold', 0.0))
|
||||
numgeoms = 0
|
||||
if self.get_bool('polygon_geojson', False):
|
||||
details.geometry_output |= napi.GeometryFormat.GEOJSON
|
||||
@@ -348,7 +351,7 @@ async def lookup_endpoint(api: napi.NominatimAPIAsync, params: ASGIAdaptor) -> A
|
||||
details = params.parse_geometry_details(fmt)
|
||||
|
||||
places = []
|
||||
for oid in params.get('osm_ids', '').split(','):
|
||||
for oid in (params.get('osm_ids') or '').split(','):
|
||||
oid = oid.strip()
|
||||
if len(oid) > 1 and oid[0] in 'RNWrnw' and oid[1:].isdigit():
|
||||
places.append(napi.OsmID(oid[0], int(oid[1:])))
|
||||
|
||||
Reference in New Issue
Block a user