X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/e1af6a22d357de51c86ac73582beba2b4419227b..105597be056bf9cbf3bb4c15f9bc516479e48ee2:/nominatim/api/v1/format_xml.py diff --git a/nominatim/api/v1/format_xml.py b/nominatim/api/v1/format_xml.py index 3fe3b7fe..c6ea17c0 100644 --- a/nominatim/api/v1/format_xml.py +++ b/nominatim/api/v1/format_xml.py @@ -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,14 +36,8 @@ 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 - root: ET.Element, simple: bool, - locales: napi.Locales) -> ET.Element: - if result.address_rows: - label_parts = result.address_rows.localize(locales) - else: - label_parts = [] - +def _create_base_entry(result: Union[napi.ReverseResult, napi.SearchResult], + root: ET.Element, simple: bool) -> ET.Element: place = ET.SubElement(root, 'result' if simple else 'place') if result.place_id is not None: place.set('place_id', str(result.place_id)) @@ -52,9 +48,9 @@ def _create_base_entry(result: napi.ReverseResult, #pylint: disable=too-many-bra place.set('osm_id', str(result.osm_object[1])) if result.names and 'ref' in result.names: place.set('ref', result.names['ref']) - elif label_parts: + elif result.locale_name: # bug reproduced from PHP - place.set('ref', label_parts[0]) + place.set('ref', result.locale_name) place.set('lat', f"{result.centroid.lat:.7f}") place.set('lon', f"{result.centroid.lon:.7f}") @@ -76,9 +72,9 @@ def _create_base_entry(result: napi.ReverseResult, #pylint: disable=too-many-bra place.set('geojson', result.geometry['geojson']) if simple: - place.text = ', '.join(label_parts) + place.text = result.display_name or '' else: - place.set('display_name', ', '.join(label_parts)) + place.set('display_name', result.display_name or '') place.set('class', result.category[0]) place.set('type', result.category[1]) place.set('importance', str(result.calculated_importance())) @@ -86,15 +82,13 @@ 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: """ Format the result into an XML response. With 'simple' exactly one result will be output, otherwise a list. """ - locales = options.get('locales', napi.Locales()) - root = ET.Element(xml_root_tag) root.set('timestamp', dt.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S +00:00')) root.set('attribution', cl.OSM_ATTRIBUTION) @@ -105,7 +99,7 @@ def format_base_xml(results: napi.ReverseResults, ET.SubElement(root, 'error').text = 'Unable to geocode' for result in results: - place = _create_base_entry(result, root, simple, locales) + place = _create_base_entry(result, root, simple) if not simple and options.get('icon_base_url', None): icon = cl.ICONS.get(result.category)