1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Subcommand definitions for API calls from the command line.
10 from typing import Dict, Any, Optional, Type, Mapping
16 from functools import reduce
18 import nominatim_api as napi
19 from nominatim_api.v1.helpers import zoom_to_rank, deduplicate_results
20 from nominatim_api.server.content_types import CONTENT_JSON
21 import nominatim_api.logging as loglib
22 from ..errors import UsageError
23 from .args import NominatimArgs
26 LOG = logging.getLogger()
30 ('amenity', 'name and/or type of POI'),
31 ('street', 'housenumber and street'),
32 ('city', 'city, town or village'),
35 ('country', 'country'),
36 ('postalcode', 'postcode')
41 ('addressdetails', 'Include a breakdown of the address into elements'),
42 ('extratags', ("Include additional information if available "
43 "(e.g. wikipedia link, opening hours)")),
44 ('entrances', 'Include a list of tagged entrance nodes'),
45 ('namedetails', 'Include a list of alternative names')
49 def _add_list_format(parser: argparse.ArgumentParser) -> None:
50 group = parser.add_argument_group('Other options')
51 group.add_argument('--list-formats', action='store_true',
52 help='List supported output formats and exit.')
55 def _add_api_output_arguments(parser: argparse.ArgumentParser) -> None:
56 group = parser.add_argument_group('Output formatting')
57 group.add_argument('--format', type=str, default='jsonv2',
58 help='Format of result (use --list-format to see supported formats)')
59 for name, desc in EXTRADATA_PARAMS:
60 group.add_argument('--' + name, action='store_true', help=desc)
62 group.add_argument('--lang', '--accept-language', metavar='LANGS',
63 help='Preferred language order for presenting search results')
64 group.add_argument('--polygon-output',
65 choices=['geojson', 'kml', 'svg', 'text'],
66 help='Output geometry of results as a GeoJSON, KML, SVG or WKT')
67 group.add_argument('--polygon-threshold', type=float, default=0.0,
69 help=("Simplify output geometry."
70 "Parameter is difference tolerance in degrees."))
73 def _get_geometry_output(args: NominatimArgs) -> napi.GeometryFormat:
74 """ Get the requested geometry output format in a API-compatible
77 if not args.polygon_output:
78 return napi.GeometryFormat.NONE
79 if args.polygon_output == 'geojson':
80 return napi.GeometryFormat.GEOJSON
81 if args.polygon_output == 'kml':
82 return napi.GeometryFormat.KML
83 if args.polygon_output == 'svg':
84 return napi.GeometryFormat.SVG
85 if args.polygon_output == 'text':
86 return napi.GeometryFormat.TEXT
89 return napi.GeometryFormat[args.polygon_output.upper()]
90 except KeyError as exp:
91 raise UsageError(f"Unknown polygon output format '{args.polygon_output}'.") from exp
94 def _get_locales(args: NominatimArgs, default: Optional[str]) -> napi.Locales:
95 """ Get the locales from the language parameter.
98 return napi.Locales.from_accept_languages(args.lang)
100 return napi.Locales.from_accept_languages(default)
102 return napi.Locales()
105 def _get_layers(args: NominatimArgs, default: napi.DataLayer) -> Optional[napi.DataLayer]:
106 """ Get the list of selected layers as a DataLayer enum.
111 return reduce(napi.DataLayer.__or__,
112 (napi.DataLayer[s.upper()] for s in args.layers))
115 def _list_formats(formatter: napi.FormatDispatcher, rtype: Type[Any]) -> int:
116 for fmt in formatter.list_formats(rtype):
124 def _print_output(formatter: napi.FormatDispatcher, result: Any,
125 fmt: str, options: Mapping[str, Any]) -> None:
128 pprint.pprint(result)
130 output = formatter.format_result(result, fmt, options)
131 if formatter.get_content_type(fmt) == CONTENT_JSON:
132 # reformat the result, so it is pretty-printed
134 json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False)
135 except json.decoder.JSONDecodeError as err:
136 # Catch the error here, so that data can be debugged,
137 # when people are developping custom result formatters.
138 LOG.fatal("Parsing json failed: %s\nUnformatted output:\n%s", err, output)
140 sys.stdout.write(output)
141 sys.stdout.write('\n')
146 Execute a search query.
148 This command works exactly the same as if calling the /search endpoint on
149 the web API. See the online documentation for more details on the
151 https://nominatim.org/release-docs/latest/api/Search/
154 def add_args(self, parser: argparse.ArgumentParser) -> None:
155 group = parser.add_argument_group('Query arguments')
156 group.add_argument('--query',
157 help='Free-form query string')
158 for name, desc in STRUCTURED_QUERY:
159 group.add_argument('--' + name, help='Structured query: ' + desc)
161 _add_api_output_arguments(parser)
163 group = parser.add_argument_group('Result limitation')
164 group.add_argument('--countrycodes', metavar='CC,..',
165 help='Limit search results to one or more countries')
166 group.add_argument('--exclude_place_ids', metavar='ID,..',
167 help='List of search object to be excluded')
168 group.add_argument('--limit', type=int, default=10,
169 help='Limit the number of returned results')
170 group.add_argument('--viewbox', metavar='X1,Y1,X2,Y2',
171 help='Preferred area to find search results')
172 group.add_argument('--bounded', action='store_true',
173 help='Strictly restrict results to viewbox area')
174 group.add_argument('--no-dedupe', action='store_false', dest='dedupe',
175 help='Do not remove duplicates from the result list')
176 _add_list_format(parser)
178 def run(self, args: NominatimArgs) -> int:
179 formatter = napi.load_format_dispatcher('v1', args.project_dir)
181 if args.list_formats:
182 return _list_formats(formatter, napi.SearchResults)
184 if args.format in ('debug', 'raw'):
185 loglib.set_log_output('text')
186 elif not formatter.supports_format(napi.SearchResults, args.format):
187 raise UsageError(f"Unsupported format '{args.format}'. "
188 'Use --list-formats to see supported formats.')
191 with napi.NominatimAPI(args.project_dir) as api:
192 params: Dict[str, Any] = {'max_results': args.limit + min(args.limit, 10),
193 'address_details': True, # needed for display name
194 'geometry_output': _get_geometry_output(args),
195 'geometry_simplification': args.polygon_threshold,
196 'countries': args.countrycodes,
197 'excluded': args.exclude_place_ids,
198 'viewbox': args.viewbox,
199 'bounded_viewbox': args.bounded,
200 'entrances': args.entrances,
204 results = api.search(args.query, **params)
206 results = api.search_address(amenity=args.amenity,
211 postalcode=args.postalcode,
212 country=args.country,
214 except napi.UsageError as ex:
215 raise UsageError(ex) from ex
217 locales = _get_locales(args, api.config.DEFAULT_LANGUAGE)
218 locales.localize_results(results)
220 if args.dedupe and len(results) > 1:
221 results = deduplicate_results(results, args.limit)
223 if args.format == 'debug':
224 print(loglib.get_and_disable())
227 _print_output(formatter, results, args.format,
228 {'extratags': args.extratags,
229 'namedetails': args.namedetails,
230 'entrances': args.entrances,
231 'addressdetails': args.addressdetails})
237 Execute API reverse query.
239 This command works exactly the same as if calling the /reverse endpoint on
240 the web API. See the online documentation for more details on the
242 https://nominatim.org/release-docs/latest/api/Reverse/
245 def add_args(self, parser: argparse.ArgumentParser) -> None:
246 group = parser.add_argument_group('Query arguments')
247 group.add_argument('--lat', type=float,
248 help='Latitude of coordinate to look up (in WGS84)')
249 group.add_argument('--lon', type=float,
250 help='Longitude of coordinate to look up (in WGS84)')
251 group.add_argument('--zoom', type=int,
252 help='Level of detail required for the address')
253 group.add_argument('--layer', metavar='LAYER',
254 choices=[n.name.lower() for n in napi.DataLayer if n.name],
255 action='append', required=False, dest='layers',
256 help='OSM id to lookup in format <NRW><id> (may be repeated)')
258 _add_api_output_arguments(parser)
259 _add_list_format(parser)
261 def run(self, args: NominatimArgs) -> int:
262 formatter = napi.load_format_dispatcher('v1', args.project_dir)
264 if args.list_formats:
265 return _list_formats(formatter, napi.ReverseResults)
267 if args.format in ('debug', 'raw'):
268 loglib.set_log_output('text')
269 elif not formatter.supports_format(napi.ReverseResults, args.format):
270 raise UsageError(f"Unsupported format '{args.format}'. "
271 'Use --list-formats to see supported formats.')
273 if args.lat is None or args.lon is None:
274 raise UsageError("lat' and 'lon' parameters are required.")
276 layers = _get_layers(args, napi.DataLayer.ADDRESS | napi.DataLayer.POI)
279 with napi.NominatimAPI(args.project_dir) as api:
280 result = api.reverse(napi.Point(args.lon, args.lat),
281 max_rank=zoom_to_rank(args.zoom or 18),
283 address_details=True, # needed for display name
284 geometry_output=_get_geometry_output(args),
285 geometry_simplification=args.polygon_threshold)
286 except napi.UsageError as ex:
287 raise UsageError(ex) from ex
289 if result is not None:
290 locales = _get_locales(args, api.config.DEFAULT_LANGUAGE)
291 locales.localize_results([result])
293 if args.format == 'debug':
294 print(loglib.get_and_disable())
298 _print_output(formatter, napi.ReverseResults([result]), args.format,
299 {'extratags': args.extratags,
300 'namedetails': args.namedetails,
301 'entrances': args.entrances,
302 'addressdetails': args.addressdetails})
306 LOG.error("Unable to geocode.")
312 Execute API lookup query.
314 This command works exactly the same as if calling the /lookup endpoint on
315 the web API. See the online documentation for more details on the
317 https://nominatim.org/release-docs/latest/api/Lookup/
320 def add_args(self, parser: argparse.ArgumentParser) -> None:
321 group = parser.add_argument_group('Query arguments')
322 group.add_argument('--id', metavar='OSMID',
323 action='append', dest='ids',
324 help='OSM id to lookup in format <NRW><id> (may be repeated)')
326 _add_api_output_arguments(parser)
327 _add_list_format(parser)
329 def run(self, args: NominatimArgs) -> int:
330 formatter = napi.load_format_dispatcher('v1', args.project_dir)
332 if args.list_formats:
333 return _list_formats(formatter, napi.ReverseResults)
335 if args.format in ('debug', 'raw'):
336 loglib.set_log_output('text')
337 elif not formatter.supports_format(napi.ReverseResults, args.format):
338 raise UsageError(f"Unsupported format '{args.format}'. "
339 'Use --list-formats to see supported formats.')
342 raise UsageError("'id' parameter required.")
344 places = [napi.OsmID(o[0], int(o[1:])) for o in args.ids]
347 with napi.NominatimAPI(args.project_dir) as api:
348 results = api.lookup(places,
349 address_details=True, # needed for display name
350 geometry_output=_get_geometry_output(args),
351 geometry_simplification=args.polygon_threshold or 0.0)
352 except napi.UsageError as ex:
353 raise UsageError(ex) from ex
355 locales = _get_locales(args, api.config.DEFAULT_LANGUAGE)
356 locales.localize_results(results)
358 if args.format == 'debug':
359 print(loglib.get_and_disable())
362 _print_output(formatter, results, args.format,
363 {'extratags': args.extratags,
364 'namedetails': args.namedetails,
365 'entrances': args.entrances,
366 'addressdetails': args.addressdetails})
372 Execute API details query.
374 This command works exactly the same as if calling the /details endpoint on
375 the web API. See the online documentation for more details on the
377 https://nominatim.org/release-docs/latest/api/Details/
380 def add_args(self, parser: argparse.ArgumentParser) -> None:
381 group = parser.add_argument_group('Query arguments')
382 group.add_argument('--node', '-n', type=int,
383 help="Look up the OSM node with the given ID.")
384 group.add_argument('--way', '-w', type=int,
385 help="Look up the OSM way with the given ID.")
386 group.add_argument('--relation', '-r', type=int,
387 help="Look up the OSM relation with the given ID.")
388 group.add_argument('--place_id', '-p', type=int,
389 help='Database internal identifier of the OSM object to look up')
390 group.add_argument('--class', dest='object_class',
391 help=("Class type to disambiguated multiple entries "
392 "of the same object."))
394 group = parser.add_argument_group('Output arguments')
395 group.add_argument('--format', type=str, default='json',
396 help='Format of result (use --list-formats to see supported formats)')
397 group.add_argument('--addressdetails', action='store_true',
398 help='Include a breakdown of the address into elements')
399 group.add_argument('--keywords', action='store_true',
400 help='Include a list of name keywords and address keywords')
401 group.add_argument('--linkedplaces', action='store_true',
402 help='Include a details of places that are linked with this one')
403 group.add_argument('--entrances', action='store_true',
404 help='Include a list of tagged entrance nodes')
405 group.add_argument('--hierarchy', action='store_true',
406 help='Include details of places lower in the address hierarchy')
407 group.add_argument('--group_hierarchy', action='store_true',
408 help='Group the places by type')
409 group.add_argument('--polygon_geojson', action='store_true',
410 help='Include geometry of result')
411 group.add_argument('--lang', '--accept-language', metavar='LANGS',
412 help='Preferred language order for presenting search results')
413 _add_list_format(parser)
415 def run(self, args: NominatimArgs) -> int:
416 formatter = napi.load_format_dispatcher('v1', args.project_dir)
418 if args.list_formats:
419 return _list_formats(formatter, napi.DetailedResult)
421 if args.format in ('debug', 'raw'):
422 loglib.set_log_output('text')
423 elif not formatter.supports_format(napi.DetailedResult, args.format):
424 raise UsageError(f"Unsupported format '{args.format}'. "
425 'Use --list-formats to see supported formats.')
429 place = napi.OsmID('N', args.node, args.object_class)
431 place = napi.OsmID('W', args.way, args.object_class)
433 place = napi.OsmID('R', args.relation, args.object_class)
434 elif args.place_id is not None:
435 place = napi.PlaceID(args.place_id)
437 raise UsageError('One of the arguments --node/-n --way/-w '
438 '--relation/-r --place_id/-p is required/')
441 with napi.NominatimAPI(args.project_dir) as api:
442 result = api.details(place,
443 address_details=args.addressdetails,
444 entrances=args.entrances,
445 linked_places=args.linkedplaces,
446 parented_places=args.hierarchy,
447 keywords=args.keywords,
448 geometry_output=(napi.GeometryFormat.GEOJSON
449 if args.polygon_geojson
450 else napi.GeometryFormat.NONE))
451 except napi.UsageError as ex:
452 raise UsageError(ex) from ex
454 if result is not None:
455 locales = _get_locales(args, api.config.DEFAULT_LANGUAGE)
456 locales.localize_results([result])
458 if args.format == 'debug':
459 print(loglib.get_and_disable())
463 _print_output(formatter, result, args.format or 'json',
464 {'group_hierarchy': args.group_hierarchy})
467 LOG.error("Object not found in database.")
473 Execute API status query.
475 This command works exactly the same as if calling the /status endpoint on
476 the web API. See the online documentation for more details on the
478 https://nominatim.org/release-docs/latest/api/Status/
481 def add_args(self, parser: argparse.ArgumentParser) -> None:
482 group = parser.add_argument_group('API parameters')
483 group.add_argument('--format', type=str, default='text',
484 help='Format of result (use --list-formats to see supported formats)')
485 _add_list_format(parser)
487 def run(self, args: NominatimArgs) -> int:
488 formatter = napi.load_format_dispatcher('v1', args.project_dir)
490 if args.list_formats:
491 return _list_formats(formatter, napi.StatusResult)
493 if args.format in ('debug', 'raw'):
494 loglib.set_log_output('text')
495 elif not formatter.supports_format(napi.StatusResult, args.format):
496 raise UsageError(f"Unsupported format '{args.format}'. "
497 'Use --list-formats to see supported formats.')
500 with napi.NominatimAPI(args.project_dir) as api:
501 status = api.status()
502 except napi.UsageError as ex:
503 raise UsageError(ex) from ex
505 if args.format == 'debug':
506 print(loglib.get_and_disable())
509 _print_output(formatter, status, args.format, {})