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 Mapping, Dict
 
  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
 
  22 # Do not repeat documentation of subcommand classes.
 
  23 # pylint: disable=C0111
 
  25 LOG = logging.getLogger()
 
  28     ('street', 'housenumber and street'),
 
  29     ('city', 'city, town or village'),
 
  32     ('country', 'country'),
 
  33     ('postalcode', 'postcode')
 
  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')
 
  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)
 
  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."))
 
  61 def _run_api(endpoint: str, args: NominatimArgs, params: Mapping[str, object]) -> int:
 
  62     script_file = args.project_dir / 'website' / (endpoint + '.php')
 
  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.")
 
  70     return run_api_script(endpoint, args.project_dir,
 
  71                           phpcgi_bin=args.phpcgi_path, params=params)
 
  75     Execute a search query.
 
  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
 
  80     https://nominatim.org/release-docs/latest/api/Search/
 
  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)
 
  90         _add_api_output_arguments(parser)
 
  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')
 
 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')
 
 109     def run(self, args: NominatimArgs) -> int:
 
 110         params: Dict[str, object]
 
 112             params = dict(q=args.query)
 
 114             params = {k: getattr(args, k) for k, _ in STRUCTURED_QUERY if getattr(args, k)}
 
 116         for param, _ in EXTRADATA_PARAMS:
 
 117             if getattr(args, param):
 
 119         for param in ('format', 'countrycodes', 'exclude_place_ids', 'limit', 'viewbox'):
 
 120             if getattr(args, param):
 
 121                 params[param] = getattr(args, param)
 
 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
 
 129             params['bounded'] = '1'
 
 131             params['dedupe'] = '0'
 
 133         return _run_api('search', args, params)
 
 137     Execute API reverse query.
 
 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
 
 142     https://nominatim.org/release-docs/latest/api/Reverse/
 
 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')
 
 154         _add_api_output_arguments(parser)
 
 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
 
 162         for param, _ in EXTRADATA_PARAMS:
 
 163             if getattr(args, param):
 
 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
 
 172         return _run_api('reverse', args, params)
 
 177     Execute API lookup query.
 
 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
 
 182     https://nominatim.org/release-docs/latest/api/Lookup/
 
 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)')
 
 191         _add_api_output_arguments(parser)
 
 194     def run(self, args: NominatimArgs) -> int:
 
 195         params: Dict[str, object] = dict(osm_ids=','.join(args.ids), format=args.format)
 
 197         for param, _ in EXTRADATA_PARAMS:
 
 198             if getattr(args, param):
 
 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
 
 207         return _run_api('lookup', args, params)
 
 212     Execute API details query.
 
 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
 
 217     https://nominatim.org/release-docs/latest/api/Details/
 
 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."))
 
 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')
 
 252     def run(self, args: NominatimArgs) -> int:
 
 255             place = napi.OsmID('N', args.node, args.object_class)
 
 257             place = napi.OsmID('W', args.way, args.object_class)
 
 259             place = napi.OsmID('R', args.relation, args.object_class)
 
 261             assert args.place_id is not None
 
 262             place = napi.PlaceID(args.place_id)
 
 264         api = napi.NominatimAPI(args.project_dir)
 
 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
 
 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)
 
 278             locales = napi.Locales()
 
 280         result = api.lookup(place, details)
 
 283             output = api_output.format_result(
 
 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')
 
 294         LOG.error("Object not found in database.")
 
 300     Execute API status query.
 
 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
 
 305     https://nominatim.org/release-docs/latest/api/Status/
 
 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')
 
 315     def run(self, args: NominatimArgs) -> int:
 
 316         status = napi.NominatimAPI(args.project_dir).status()
 
 317         print(api_output.format_result(status, args.format, {}))