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 Subcommand definitions for API calls from the command line.
10 from typing import Dict, Any
16 from nominatim.clicmd.args import NominatimArgs
17 import nominatim.api as napi
18 import nominatim.api.v1 as api_output
19 from nominatim.api.v1.helpers import zoom_to_rank, deduplicate_results
20 from nominatim.api.v1.format import dispatch as formatting
21 import nominatim.api.logging as loglib
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."))
66 Execute a search query.
68 This command works exactly the same as if calling the /search endpoint on
69 the web API. See the online documentation for more details on the
71 https://nominatim.org/release-docs/latest/api/Search/
74 def add_args(self, parser: argparse.ArgumentParser) -> None:
75 group = parser.add_argument_group('Query arguments')
76 group.add_argument('--query',
77 help='Free-form query string')
78 for name, desc in STRUCTURED_QUERY:
79 group.add_argument('--' + name, help='Structured query: ' + desc)
81 _add_api_output_arguments(parser)
83 group = parser.add_argument_group('Result limitation')
84 group.add_argument('--countrycodes', metavar='CC,..',
85 help='Limit search results to one or more countries')
86 group.add_argument('--exclude_place_ids', metavar='ID,..',
87 help='List of search object to be excluded')
88 group.add_argument('--limit', type=int, default=10,
89 help='Limit the number of returned results')
90 group.add_argument('--viewbox', metavar='X1,Y1,X2,Y2',
91 help='Preferred area to find search results')
92 group.add_argument('--bounded', action='store_true',
93 help='Strictly restrict results to viewbox area')
95 group = parser.add_argument_group('Other arguments')
96 group.add_argument('--no-dedupe', action='store_false', dest='dedupe',
97 help='Do not remove duplicates from the result list')
100 def run(self, args: NominatimArgs) -> int:
101 if args.format == 'debug':
102 loglib.set_log_output('text')
104 api = napi.NominatimAPI(args.project_dir)
106 params: Dict[str, Any] = {'max_results': args.limit + min(args.limit, 10),
107 'address_details': True, # needed for display name
108 'geometry_output': args.get_geometry_output(),
109 'geometry_simplification': args.polygon_threshold,
110 'countries': args.countrycodes,
111 'excluded': args.exclude_place_ids,
112 'viewbox': args.viewbox,
113 'bounded_viewbox': args.bounded,
114 'locales': args.get_locales(api.config.DEFAULT_LANGUAGE)
118 results = api.search(args.query, **params)
120 results = api.search_address(amenity=args.amenity,
125 postalcode=args.postalcode,
126 country=args.country,
129 if args.dedupe and len(results) > 1:
130 results = deduplicate_results(results, args.limit)
132 if args.format == 'debug':
133 print(loglib.get_and_disable())
136 output = api_output.format_result(
139 {'extratags': args.extratags,
140 'namedetails': args.namedetails,
141 'addressdetails': args.addressdetails})
142 if args.format != 'xml':
143 # reformat the result, so it is pretty-printed
144 json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False)
146 sys.stdout.write(output)
147 sys.stdout.write('\n')
154 Execute API reverse query.
156 This command works exactly the same as if calling the /reverse endpoint on
157 the web API. See the online documentation for more details on the
159 https://nominatim.org/release-docs/latest/api/Reverse/
162 def add_args(self, parser: argparse.ArgumentParser) -> None:
163 group = parser.add_argument_group('Query arguments')
164 group.add_argument('--lat', type=float, required=True,
165 help='Latitude of coordinate to look up (in WGS84)')
166 group.add_argument('--lon', type=float, required=True,
167 help='Longitude of coordinate to look up (in WGS84)')
168 group.add_argument('--zoom', type=int,
169 help='Level of detail required for the address')
170 group.add_argument('--layer', metavar='LAYER',
171 choices=[n.name.lower() for n in napi.DataLayer if n.name],
172 action='append', required=False, dest='layers',
173 help='OSM id to lookup in format <NRW><id> (may be repeated)')
175 _add_api_output_arguments(parser)
178 def run(self, args: NominatimArgs) -> int:
179 if args.format == 'debug':
180 loglib.set_log_output('text')
182 api = napi.NominatimAPI(args.project_dir)
184 result = api.reverse(napi.Point(args.lon, args.lat),
185 max_rank=zoom_to_rank(args.zoom or 18),
186 layers=args.get_layers(napi.DataLayer.ADDRESS | napi.DataLayer.POI),
187 address_details=True, # needed for display name
188 geometry_output=args.get_geometry_output(),
189 geometry_simplification=args.polygon_threshold,
190 locales=args.get_locales(api.config.DEFAULT_LANGUAGE))
192 if args.format == 'debug':
193 print(loglib.get_and_disable())
197 output = api_output.format_result(
198 napi.ReverseResults([result]),
200 {'extratags': args.extratags,
201 'namedetails': args.namedetails,
202 'addressdetails': args.addressdetails})
203 if args.format != 'xml':
204 # reformat the result, so it is pretty-printed
205 json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False)
207 sys.stdout.write(output)
208 sys.stdout.write('\n')
212 LOG.error("Unable to geocode.")
219 Execute API lookup query.
221 This command works exactly the same as if calling the /lookup endpoint on
222 the web API. See the online documentation for more details on the
224 https://nominatim.org/release-docs/latest/api/Lookup/
227 def add_args(self, parser: argparse.ArgumentParser) -> None:
228 group = parser.add_argument_group('Query arguments')
229 group.add_argument('--id', metavar='OSMID',
230 action='append', required=True, dest='ids',
231 help='OSM id to lookup in format <NRW><id> (may be repeated)')
233 _add_api_output_arguments(parser)
236 def run(self, args: NominatimArgs) -> int:
237 if args.format == 'debug':
238 loglib.set_log_output('text')
240 api = napi.NominatimAPI(args.project_dir)
242 if args.format == 'debug':
243 print(loglib.get_and_disable())
246 places = [napi.OsmID(o[0], int(o[1:])) for o in args.ids]
248 results = api.lookup(places,
249 address_details=True, # needed for display name
250 geometry_output=args.get_geometry_output(),
251 geometry_simplification=args.polygon_threshold or 0.0,
252 locales=args.get_locales(api.config.DEFAULT_LANGUAGE))
254 output = api_output.format_result(
257 {'extratags': args.extratags,
258 'namedetails': args.namedetails,
259 'addressdetails': args.addressdetails})
260 if args.format != 'xml':
261 # reformat the result, so it is pretty-printed
262 json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False)
264 sys.stdout.write(output)
265 sys.stdout.write('\n')
272 Execute API details query.
274 This command works exactly the same as if calling the /details endpoint on
275 the web API. See the online documentation for more details on the
277 https://nominatim.org/release-docs/latest/api/Details/
280 def add_args(self, parser: argparse.ArgumentParser) -> None:
281 group = parser.add_argument_group('Query arguments')
282 objs = group.add_mutually_exclusive_group(required=True)
283 objs.add_argument('--node', '-n', type=int,
284 help="Look up the OSM node with the given ID.")
285 objs.add_argument('--way', '-w', type=int,
286 help="Look up the OSM way with the given ID.")
287 objs.add_argument('--relation', '-r', type=int,
288 help="Look up the OSM relation with the given ID.")
289 objs.add_argument('--place_id', '-p', type=int,
290 help='Database internal identifier of the OSM object to look up')
291 group.add_argument('--class', dest='object_class',
292 help=("Class type to disambiguated multiple entries "
293 "of the same object."))
295 group = parser.add_argument_group('Output arguments')
296 group.add_argument('--addressdetails', action='store_true',
297 help='Include a breakdown of the address into elements')
298 group.add_argument('--keywords', action='store_true',
299 help='Include a list of name keywords and address keywords')
300 group.add_argument('--linkedplaces', action='store_true',
301 help='Include a details of places that are linked with this one')
302 group.add_argument('--hierarchy', action='store_true',
303 help='Include details of places lower in the address hierarchy')
304 group.add_argument('--group_hierarchy', action='store_true',
305 help='Group the places by type')
306 group.add_argument('--polygon_geojson', action='store_true',
307 help='Include geometry of result')
308 group.add_argument('--lang', '--accept-language', metavar='LANGS',
309 help='Preferred language order for presenting search results')
312 def run(self, args: NominatimArgs) -> int:
315 place = napi.OsmID('N', args.node, args.object_class)
317 place = napi.OsmID('W', args.way, args.object_class)
319 place = napi.OsmID('R', args.relation, args.object_class)
321 assert args.place_id is not None
322 place = napi.PlaceID(args.place_id)
324 api = napi.NominatimAPI(args.project_dir)
326 locales = args.get_locales(api.config.DEFAULT_LANGUAGE)
327 result = api.details(place,
328 address_details=args.addressdetails,
329 linked_places=args.linkedplaces,
330 parented_places=args.hierarchy,
331 keywords=args.keywords,
332 geometry_output=napi.GeometryFormat.GEOJSON
333 if args.polygon_geojson
334 else napi.GeometryFormat.NONE,
339 output = api_output.format_result(
343 'group_hierarchy': args.group_hierarchy})
344 # reformat the result, so it is pretty-printed
345 json.dump(json.loads(output), sys.stdout, indent=4, ensure_ascii=False)
346 sys.stdout.write('\n')
350 LOG.error("Object not found in database.")
356 Execute API status query.
358 This command works exactly the same as if calling the /status endpoint on
359 the web API. See the online documentation for more details on the
361 https://nominatim.org/release-docs/latest/api/Status/
364 def add_args(self, parser: argparse.ArgumentParser) -> None:
365 formats = api_output.list_formats(napi.StatusResult)
366 group = parser.add_argument_group('API parameters')
367 group.add_argument('--format', default=formats[0], choices=formats,
368 help='Format of result')
371 def run(self, args: NominatimArgs) -> int:
372 status = napi.NominatimAPI(args.project_dir).status()
373 print(api_output.format_result(status, args.format, {}))