]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/api.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / nominatim / clicmd / api.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Subcommand definitions for API calls from the command line.
9 """
10 from typing import Mapping, Dict
11 import argparse
12 import logging
13 import json
14 import sys
15
16 from nominatim.tools.exec_utils import run_api_script
17 from nominatim.errors import UsageError
18 from nominatim.clicmd.args import NominatimArgs
19 import nominatim.api as napi
20 import nominatim.api.v1 as api_output
21
22 # Do not repeat documentation of subcommand classes.
23 # pylint: disable=C0111
24
25 LOG = logging.getLogger()
26
27 STRUCTURED_QUERY = (
28     ('street', 'housenumber and street'),
29     ('city', 'city, town or village'),
30     ('county', 'county'),
31     ('state', 'state'),
32     ('country', 'country'),
33     ('postalcode', 'postcode')
34 )
35
36 EXTRADATA_PARAMS = (
37     ('addressdetails', 'Include a breakdown of the address into elements'),
38     ('extratags', ("Include additional information if available "
39                    "(e.g. wikipedia link, opening hours)")),
40     ('namedetails', 'Include a list of alternative names')
41 )
42
43 def _add_api_output_arguments(parser: argparse.ArgumentParser) -> None:
44     group = parser.add_argument_group('Output arguments')
45     group.add_argument('--format', default='jsonv2',
46                        choices=['xml', 'json', 'jsonv2', 'geojson', 'geocodejson'],
47                        help='Format of result')
48     for name, desc in EXTRADATA_PARAMS:
49         group.add_argument('--' + name, action='store_true', help=desc)
50
51     group.add_argument('--lang', '--accept-language', metavar='LANGS',
52                        help='Preferred language order for presenting search results')
53     group.add_argument('--polygon-output',
54                        choices=['geojson', 'kml', 'svg', 'text'],
55                        help='Output geometry of results as a GeoJSON, KML, SVG or WKT')
56     group.add_argument('--polygon-threshold', type=float, metavar='TOLERANCE',
57                        help=("Simplify output geometry."
58                              "Parameter is difference tolerance in degrees."))
59
60
61 def _run_api(endpoint: str, args: NominatimArgs, params: Mapping[str, object]) -> int:
62     script_file = args.project_dir / 'website' / (endpoint + '.php')
63
64     if not script_file.exists():
65         LOG.error("Cannot find API script file.\n\n"
66                   "Make sure to run 'nominatim' from the project directory \n"
67                   "or use the option --project-dir.")
68         raise UsageError("API script not found.")
69
70     return run_api_script(endpoint, args.project_dir,
71                           phpcgi_bin=args.phpcgi_path, params=params)
72
73 class APISearch:
74     """\
75     Execute a search query.
76
77     This command works exactly the same as if calling the /search endpoint on
78     the web API. See the online documentation for more details on the
79     various parameters:
80     https://nominatim.org/release-docs/latest/api/Search/
81     """
82
83     def add_args(self, parser: argparse.ArgumentParser) -> None:
84         group = parser.add_argument_group('Query arguments')
85         group.add_argument('--query',
86                            help='Free-form query string')
87         for name, desc in STRUCTURED_QUERY:
88             group.add_argument('--' + name, help='Structured query: ' + desc)
89
90         _add_api_output_arguments(parser)
91
92         group = parser.add_argument_group('Result limitation')
93         group.add_argument('--countrycodes', metavar='CC,..',
94                            help='Limit search results to one or more countries')
95         group.add_argument('--exclude_place_ids', metavar='ID,..',
96                            help='List of search object to be excluded')
97         group.add_argument('--limit', type=int,
98                            help='Limit the number of returned results')
99         group.add_argument('--viewbox', metavar='X1,Y1,X2,Y2',
100                            help='Preferred area to find search results')
101         group.add_argument('--bounded', action='store_true',
102                            help='Strictly restrict results to viewbox area')
103
104         group = parser.add_argument_group('Other arguments')
105         group.add_argument('--no-dedupe', action='store_false', dest='dedupe',
106                            help='Do not remove duplicates from the result list')
107
108
109     def run(self, args: NominatimArgs) -> int:
110         params: Dict[str, object]
111         if args.query:
112             params = dict(q=args.query)
113         else:
114             params = {k: getattr(args, k) for k, _ in STRUCTURED_QUERY if getattr(args, k)}
115
116         for param, _ in EXTRADATA_PARAMS:
117             if getattr(args, param):
118                 params[param] = '1'
119         for param in ('format', 'countrycodes', 'exclude_place_ids', 'limit', 'viewbox'):
120             if getattr(args, param):
121                 params[param] = getattr(args, param)
122         if args.lang:
123             params['accept-language'] = args.lang
124         if args.polygon_output:
125             params['polygon_' + args.polygon_output] = '1'
126         if args.polygon_threshold:
127             params['polygon_threshold'] = args.polygon_threshold
128         if args.bounded:
129             params['bounded'] = '1'
130         if not args.dedupe:
131             params['dedupe'] = '0'
132
133         return _run_api('search', args, params)
134
135 class APIReverse:
136     """\
137     Execute API reverse query.
138
139     This command works exactly the same as if calling the /reverse endpoint on
140     the web API. See the online documentation for more details on the
141     various parameters:
142     https://nominatim.org/release-docs/latest/api/Reverse/
143     """
144
145     def add_args(self, parser: argparse.ArgumentParser) -> None:
146         group = parser.add_argument_group('Query arguments')
147         group.add_argument('--lat', type=float, required=True,
148                            help='Latitude of coordinate to look up (in WGS84)')
149         group.add_argument('--lon', type=float, required=True,
150                            help='Longitude of coordinate to look up (in WGS84)')
151         group.add_argument('--zoom', type=int,
152                            help='Level of detail required for the address')
153
154         _add_api_output_arguments(parser)
155
156
157     def run(self, args: NominatimArgs) -> int:
158         params = dict(lat=args.lat, lon=args.lon, format=args.format)
159         if args.zoom is not None:
160             params['zoom'] = args.zoom
161
162         for param, _ in EXTRADATA_PARAMS:
163             if getattr(args, param):
164                 params[param] = '1'
165         if args.lang:
166             params['accept-language'] = args.lang
167         if args.polygon_output:
168             params['polygon_' + args.polygon_output] = '1'
169         if args.polygon_threshold:
170             params['polygon_threshold'] = args.polygon_threshold
171
172         return _run_api('reverse', args, params)
173
174
175 class APILookup:
176     """\
177     Execute API lookup query.
178
179     This command works exactly the same as if calling the /lookup endpoint on
180     the web API. See the online documentation for more details on the
181     various parameters:
182     https://nominatim.org/release-docs/latest/api/Lookup/
183     """
184
185     def add_args(self, parser: argparse.ArgumentParser) -> None:
186         group = parser.add_argument_group('Query arguments')
187         group.add_argument('--id', metavar='OSMID',
188                            action='append', required=True, dest='ids',
189                            help='OSM id to lookup in format <NRW><id> (may be repeated)')
190
191         _add_api_output_arguments(parser)
192
193
194     def run(self, args: NominatimArgs) -> int:
195         params: Dict[str, object] = dict(osm_ids=','.join(args.ids), format=args.format)
196
197         for param, _ in EXTRADATA_PARAMS:
198             if getattr(args, param):
199                 params[param] = '1'
200         if args.lang:
201             params['accept-language'] = args.lang
202         if args.polygon_output:
203             params['polygon_' + args.polygon_output] = '1'
204         if args.polygon_threshold:
205             params['polygon_threshold'] = args.polygon_threshold
206
207         return _run_api('lookup', args, params)
208
209
210 class APIDetails:
211     """\
212     Execute API details query.
213
214     This command works exactly the same as if calling the /details endpoint on
215     the web API. See the online documentation for more details on the
216     various parameters:
217     https://nominatim.org/release-docs/latest/api/Details/
218     """
219
220     def add_args(self, parser: argparse.ArgumentParser) -> None:
221         group = parser.add_argument_group('Query arguments')
222         objs = group.add_mutually_exclusive_group(required=True)
223         objs.add_argument('--node', '-n', type=int,
224                           help="Look up the OSM node with the given ID.")
225         objs.add_argument('--way', '-w', type=int,
226                           help="Look up the OSM way with the given ID.")
227         objs.add_argument('--relation', '-r', type=int,
228                           help="Look up the OSM relation with the given ID.")
229         objs.add_argument('--place_id', '-p', type=int,
230                           help='Database internal identifier of the OSM object to look up')
231         group.add_argument('--class', dest='object_class',
232                            help=("Class type to disambiguated multiple entries "
233                                  "of the same object."))
234
235         group = parser.add_argument_group('Output arguments')
236         group.add_argument('--addressdetails', action='store_true',
237                            help='Include a breakdown of the address into elements')
238         group.add_argument('--keywords', action='store_true',
239                            help='Include a list of name keywords and address keywords')
240         group.add_argument('--linkedplaces', action='store_true',
241                            help='Include a details of places that are linked with this one')
242         group.add_argument('--hierarchy', action='store_true',
243                            help='Include details of places lower in the address hierarchy')
244         group.add_argument('--group_hierarchy', action='store_true',
245                            help='Group the places by type')
246         group.add_argument('--polygon_geojson', action='store_true',
247                            help='Include geometry of result')
248         group.add_argument('--lang', '--accept-language', metavar='LANGS',
249                            help='Preferred language order for presenting search results')
250
251
252     def run(self, args: NominatimArgs) -> int:
253         place: napi.PlaceRef
254         if args.node:
255             place = napi.OsmID('N', args.node, args.object_class)
256         elif args.way:
257             place = napi.OsmID('W', args.way, args.object_class)
258         elif args.relation:
259             place = napi.OsmID('R', args.relation, args.object_class)
260         else:
261             assert args.place_id is not None
262             place = napi.PlaceID(args.place_id)
263
264         api = napi.NominatimAPI(args.project_dir)
265
266         details = napi.LookupDetails(address_details=args.addressdetails,
267                                      linked_places=args.linkedplaces,
268                                      parented_places=args.hierarchy,
269                                      keywords=args.keywords)
270         if args.polygon_geojson:
271             details.geometry_output = napi.GeometryFormat.GEOJSON
272
273         if args.lang:
274             locales = napi.Locales.from_accept_languages(args.lang)
275         elif api.config.DEFAULT_LANGUAGE:
276             locales = napi.Locales.from_accept_languages(api.config.DEFAULT_LANGUAGE)
277         else:
278             locales = napi.Locales()
279
280         result = api.lookup(place, details)
281
282         if result:
283             output = api_output.format_result(
284                         result,
285                         'details-json',
286                         {'locales': locales,
287                          'group_hierarchy': args.group_hierarchy})
288             # reformat the result, so it is pretty-printed
289             json.dump(json.loads(output), sys.stdout, indent=4)
290             sys.stdout.write('\n')
291
292             return 0
293
294         LOG.error("Object not found in database.")
295         return 42
296
297
298 class APIStatus:
299     """
300     Execute API status query.
301
302     This command works exactly the same as if calling the /status endpoint on
303     the web API. See the online documentation for more details on the
304     various parameters:
305     https://nominatim.org/release-docs/latest/api/Status/
306     """
307
308     def add_args(self, parser: argparse.ArgumentParser) -> None:
309         formats = api_output.list_formats(napi.StatusResult)
310         group = parser.add_argument_group('API parameters')
311         group.add_argument('--format', default=formats[0], choices=formats,
312                            help='Format of result')
313
314
315     def run(self, args: NominatimArgs) -> int:
316         status = napi.NominatimAPI(args.project_dir).status()
317         print(api_output.format_result(status, args.format, {}))
318         return 0