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
16 import nominatim_api as napi
17 import nominatim_api.v1 as api_output
18 from nominatim_api.v1.helpers import zoom_to_rank, deduplicate_results
19 from nominatim_api.v1.format import dispatch as formatting
20 import nominatim_api.logging as loglib
21 from .args import NominatimArgs
23 # Do not repeat documentation of subcommand classes.
24 # pylint: disable=C0111
26 LOG = logging.getLogger()
29 ('amenity', 'name and/or type of POI'),
30 ('street', 'housenumber and street'),
31 ('city', 'city, town or village'),
34 ('country', 'country'),
35 ('postalcode', 'postcode')
39 ('addressdetails', 'Include a breakdown of the address into elements'),
40 ('extratags', ("Include additional information if available "
41 "(e.g. wikipedia link, opening hours)")),
42 ('namedetails', 'Include a list of alternative names')
45 def _add_api_output_arguments(parser: argparse.ArgumentParser) -> None:
46 group = parser.add_argument_group('Output arguments')
47 group.add_argument('--format', default='jsonv2',
48 choices=formatting.list_formats(napi.SearchResults) + ['debug'],
49 help='Format of result')
50 for name, desc in EXTRADATA_PARAMS:
51 group.add_argument('--' + name, action='store_true', help=desc)
53 group.add_argument('--lang', '--accept-language', metavar='LANGS',
54 help='Preferred language order for presenting search results')
55 group.add_argument('--polygon-output',
56 choices=['geojson', 'kml', 'svg', 'text'],
57 help='Output geometry of results as a GeoJSON, KML, SVG or WKT')
58 group.add_argument('--polygon-threshold', type=float, default = 0.0,
60 help=("Simplify output geometry."
61 "Parameter is difference tolerance in degrees."))
64 def _get_geometry_output(args) -> napi.GeometryFormat:
65 """ Get the requested geometry output format in a API-compatible
68 if not args.polygon_output:
69 return napi.GeometryFormat.NONE
70 if args.polygon_output == 'geojson':
71 return napi.GeometryFormat.GEOJSON
72 if args.polygon_output == 'kml':
73 return napi.GeometryFormat.KML
74 if args.polygon_output == 'svg':
75 return napi.GeometryFormat.SVG
76 if args.polygon_output == 'text':
77 return napi.GeometryFormat.TEXT
80 return napi.GeometryFormat[args.polygon_output.upper()]
81 except KeyError as exp:
82 raise UsageError(f"Unknown polygon output format '{args.polygon_output}'.") from exp
85 def _get_locales(args, default: Optional[str]) -> napi.Locales:
86 """ Get the locales from the language parameter.
89 return napi.Locales.from_accept_languages(args.lang)
91 return napi.Locales.from_accept_languages(default)
96 def _get_layers(args, default: napi.DataLayer) -> Optional[napi.DataLayer]:
97 """ Get the list of selected layers as a DataLayer enum.
102 return reduce(napi.DataLayer.__or__,
103 (napi.DataLayer[s.upper()] for s in args.layers))
108 Execute a search query.
110 This command works exactly the same as if calling the /search endpoint on
111 the web API. See the online documentation for more details on the
113 https://nominatim.org/release-docs/latest/api/Search/
116 def add_args(self, parser: argparse.ArgumentParser) -> None:
117 group = parser.add_argument_group('Query arguments')
118 group.add_argument('--query',
119 help='Free-form query string')
120 for name, desc in STRUCTURED_QUERY:
121 group.add_argument('--' + name, help='Structured query: ' + desc)
123 _add_api_output_arguments(parser)
125 group = parser.add_argument_group('Result limitation')
126 group.add_argument('--countrycodes', metavar='CC,..',
127 help='Limit search results to one or more countries')
128 group.add_argument('--exclude_place_ids', metavar='ID,..',
129 help='List of search object to be excluded')
130 group.add_argument('--limit', type=int, default=10,
131 help='Limit the number of returned results')
132 group.add_argument('--viewbox', metavar='X1,Y1,X2,Y2',
133 help='Preferred area to find search results')
134 group.add_argument('--bounded', action='store_true',
135 help='Strictly restrict results to viewbox area')
137 group = parser.add_argument_group('Other arguments')
138 group.add_argument('--no-dedupe', action='store_false', dest='dedupe',
139 help='Do not remove duplicates from the result list')
142 def run(self, args: NominatimArgs) -> int:
143 if args.format == 'debug':
144 loglib.set_log_output('text')
146 api = napi.NominatimAPI(args.project_dir)
148 params: Dict[str, Any] = {'max_results': args.limit + min(args.limit, 10),
149 'address_details': True, # needed for display name
150 'geometry_output': _get_geometry_output(args),
151 'geometry_simplification': args.polygon_threshold,
152 'countries': args.countrycodes,
153 'excluded': args.exclude_place_ids,
154 'viewbox': args.viewbox,
155 'bounded_viewbox': args.bounded,
156 'locales': _get_locales(args, api.config.DEFAULT_LANGUAGE)
160 results = api.search(args.query, **params)
162 results = api.search_address(amenity=args.amenity,
167 postalcode=args.postalcode,
168 country=args.country,
171 if args.dedupe and len(results) > 1:
172 results = deduplicate_results(results, args.limit)
174 if args.format == 'debug':
175 print(loglib.get_and_disable())
178 output = api_output.format_result(
181 {'extratags': args.extratags,
182 'namedetails': args.namedetails,
183 'addressdetails': args.addressdetails})
184 if args.format != 'xml':
185 # reformat the result, so it is pretty-printed
186 json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False)
188 sys.stdout.write(output)
189 sys.stdout.write('\n')
196 Execute API reverse query.
198 This command works exactly the same as if calling the /reverse endpoint on
199 the web API. See the online documentation for more details on the
201 https://nominatim.org/release-docs/latest/api/Reverse/
204 def add_args(self, parser: argparse.ArgumentParser) -> None:
205 group = parser.add_argument_group('Query arguments')
206 group.add_argument('--lat', type=float, required=True,
207 help='Latitude of coordinate to look up (in WGS84)')
208 group.add_argument('--lon', type=float, required=True,
209 help='Longitude of coordinate to look up (in WGS84)')
210 group.add_argument('--zoom', type=int,
211 help='Level of detail required for the address')
212 group.add_argument('--layer', metavar='LAYER',
213 choices=[n.name.lower() for n in napi.DataLayer if n.name],
214 action='append', required=False, dest='layers',
215 help='OSM id to lookup in format <NRW><id> (may be repeated)')
217 _add_api_output_arguments(parser)
220 def run(self, args: NominatimArgs) -> int:
221 if args.format == 'debug':
222 loglib.set_log_output('text')
224 api = napi.NominatimAPI(args.project_dir)
226 result = api.reverse(napi.Point(args.lon, args.lat),
227 max_rank=zoom_to_rank(args.zoom or 18),
228 layers=_get_layers(args, napi.DataLayer.ADDRESS | napi.DataLayer.POI),
229 address_details=True, # needed for display name
230 geometry_output=_get_geometry_output(args),
231 geometry_simplification=args.polygon_threshold,
232 locales=_get_locales(args, api.config.DEFAULT_LANGUAGE))
234 if args.format == 'debug':
235 print(loglib.get_and_disable())
239 output = api_output.format_result(
240 napi.ReverseResults([result]),
242 {'extratags': args.extratags,
243 'namedetails': args.namedetails,
244 'addressdetails': args.addressdetails})
245 if args.format != 'xml':
246 # reformat the result, so it is pretty-printed
247 json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False)
249 sys.stdout.write(output)
250 sys.stdout.write('\n')
254 LOG.error("Unable to geocode.")
261 Execute API lookup query.
263 This command works exactly the same as if calling the /lookup endpoint on
264 the web API. See the online documentation for more details on the
266 https://nominatim.org/release-docs/latest/api/Lookup/
269 def add_args(self, parser: argparse.ArgumentParser) -> None:
270 group = parser.add_argument_group('Query arguments')
271 group.add_argument('--id', metavar='OSMID',
272 action='append', required=True, dest='ids',
273 help='OSM id to lookup in format <NRW><id> (may be repeated)')
275 _add_api_output_arguments(parser)
278 def run(self, args: NominatimArgs) -> int:
279 if args.format == 'debug':
280 loglib.set_log_output('text')
282 api = napi.NominatimAPI(args.project_dir)
284 if args.format == 'debug':
285 print(loglib.get_and_disable())
288 places = [napi.OsmID(o[0], int(o[1:])) for o in args.ids]
290 results = api.lookup(places,
291 address_details=True, # needed for display name
292 geometry_output=_get_geometry_output(args),
293 geometry_simplification=args.polygon_threshold or 0.0,
294 locales=_get_locales(args, api.config.DEFAULT_LANGUAGE))
296 output = api_output.format_result(
299 {'extratags': args.extratags,
300 'namedetails': args.namedetails,
301 'addressdetails': args.addressdetails})
302 if args.format != 'xml':
303 # reformat the result, so it is pretty-printed
304 json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False)
306 sys.stdout.write(output)
307 sys.stdout.write('\n')
314 Execute API details query.
316 This command works exactly the same as if calling the /details endpoint on
317 the web API. See the online documentation for more details on the
319 https://nominatim.org/release-docs/latest/api/Details/
322 def add_args(self, parser: argparse.ArgumentParser) -> None:
323 group = parser.add_argument_group('Query arguments')
324 objs = group.add_mutually_exclusive_group(required=True)
325 objs.add_argument('--node', '-n', type=int,
326 help="Look up the OSM node with the given ID.")
327 objs.add_argument('--way', '-w', type=int,
328 help="Look up the OSM way with the given ID.")
329 objs.add_argument('--relation', '-r', type=int,
330 help="Look up the OSM relation with the given ID.")
331 objs.add_argument('--place_id', '-p', type=int,
332 help='Database internal identifier of the OSM object to look up')
333 group.add_argument('--class', dest='object_class',
334 help=("Class type to disambiguated multiple entries "
335 "of the same object."))
337 group = parser.add_argument_group('Output arguments')
338 group.add_argument('--addressdetails', action='store_true',
339 help='Include a breakdown of the address into elements')
340 group.add_argument('--keywords', action='store_true',
341 help='Include a list of name keywords and address keywords')
342 group.add_argument('--linkedplaces', action='store_true',
343 help='Include a details of places that are linked with this one')
344 group.add_argument('--hierarchy', action='store_true',
345 help='Include details of places lower in the address hierarchy')
346 group.add_argument('--group_hierarchy', action='store_true',
347 help='Group the places by type')
348 group.add_argument('--polygon_geojson', action='store_true',
349 help='Include geometry of result')
350 group.add_argument('--lang', '--accept-language', metavar='LANGS',
351 help='Preferred language order for presenting search results')
354 def run(self, args: NominatimArgs) -> int:
357 place = napi.OsmID('N', args.node, args.object_class)
359 place = napi.OsmID('W', args.way, args.object_class)
361 place = napi.OsmID('R', args.relation, args.object_class)
363 assert args.place_id is not None
364 place = napi.PlaceID(args.place_id)
366 api = napi.NominatimAPI(args.project_dir)
368 locales = _get_locales(args, api.config.DEFAULT_LANGUAGE)
369 result = api.details(place,
370 address_details=args.addressdetails,
371 linked_places=args.linkedplaces,
372 parented_places=args.hierarchy,
373 keywords=args.keywords,
374 geometry_output=napi.GeometryFormat.GEOJSON
375 if args.polygon_geojson
376 else napi.GeometryFormat.NONE,
381 output = api_output.format_result(
385 'group_hierarchy': args.group_hierarchy})
386 # reformat the result, so it is pretty-printed
387 json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False)
388 sys.stdout.write('\n')
392 LOG.error("Object not found in database.")
398 Execute API status query.
400 This command works exactly the same as if calling the /status endpoint on
401 the web API. See the online documentation for more details on the
403 https://nominatim.org/release-docs/latest/api/Status/
406 def add_args(self, parser: argparse.ArgumentParser) -> None:
407 formats = api_output.list_formats(napi.StatusResult)
408 group = parser.add_argument_group('API parameters')
409 group.add_argument('--format', default=formats[0], choices=formats,
410 help='Format of result')
413 def run(self, args: NominatimArgs) -> int:
414 status = napi.NominatimAPI(args.project_dir).status()
415 print(api_output.format_result(status, args.format, {}))