2 Subcommand definitions for API calls from the command line.
6 from nominatim.tools.exec_utils import run_api_script
7 from nominatim.errors import UsageError
9 # Do not repeat documentation of subcommand classes.
10 # pylint: disable=C0111
12 LOG = logging.getLogger()
15 ('street', 'housenumber and street'),
16 ('city', 'city, town or village'),
19 ('country', 'country'),
20 ('postalcode', 'postcode')
24 ('addressdetails', 'Include a breakdown of the address into elements'),
25 ('extratags', ("Include additional information if available "
26 "(e.g. wikipedia link, opening hours)")),
27 ('namedetails', 'Include a list of alternative names')
31 ('addressdetails', 'Include a breakdown of the address into elements'),
32 ('keywords', 'Include a list of name keywords and address keywords'),
33 ('linkedplaces', 'Include a details of places that are linked with this one'),
34 ('hierarchy', 'Include details of places lower in the address hierarchy'),
35 ('group_hierarchy', 'Group the places by type'),
36 ('polygon_geojson', 'Include geometry of result')
39 def _add_api_output_arguments(parser):
40 group = parser.add_argument_group('Output arguments')
41 group.add_argument('--format', default='jsonv2',
42 choices=['xml', 'json', 'jsonv2', 'geojson', 'geocodejson'],
43 help='Format of result')
44 for name, desc in EXTRADATA_PARAMS:
45 group.add_argument('--' + name, action='store_true', help=desc)
47 group.add_argument('--lang', '--accept-language', metavar='LANGS',
48 help='Preferred language order for presenting search results')
49 group.add_argument('--polygon-output',
50 choices=['geojson', 'kml', 'svg', 'text'],
51 help='Output geometry of results as a GeoJSON, KML, SVG or WKT')
52 group.add_argument('--polygon-threshold', type=float, metavar='TOLERANCE',
53 help=("Simplify output geometry."
54 "Parameter is difference tolerance in degrees."))
57 def _run_api(endpoint, args, params):
58 script_file = args.project_dir / 'website' / (endpoint + '.php')
60 if not script_file.exists():
61 LOG.error("Cannot find API script file.\n\n"
62 "Make sure to run 'nominatim' from the project directory \n"
63 "or use the option --project-dir.")
64 raise UsageError("API script not found.")
66 return run_api_script(endpoint, args.project_dir,
67 phpcgi_bin=args.phpcgi_path, params=params)
71 Execute a search query.
73 This command works exactly the same as if calling the /search endpoint on
74 the web API. See the online documentation for more details on the
76 https://nominatim.org/release-docs/latest/api/Search/
81 group = parser.add_argument_group('Query arguments')
82 group.add_argument('--query',
83 help='Free-form query string')
84 for name, desc in STRUCTURED_QUERY:
85 group.add_argument('--' + name, help='Structured query: ' + desc)
87 _add_api_output_arguments(parser)
89 group = parser.add_argument_group('Result limitation')
90 group.add_argument('--countrycodes', metavar='CC,..',
91 help='Limit search results to one or more countries')
92 group.add_argument('--exclude_place_ids', metavar='ID,..',
93 help='List of search object to be excluded')
94 group.add_argument('--limit', type=int,
95 help='Limit the number of returned results')
96 group.add_argument('--viewbox', metavar='X1,Y1,X2,Y2',
97 help='Preferred area to find search results')
98 group.add_argument('--bounded', action='store_true',
99 help='Strictly restrict results to viewbox area')
101 group = parser.add_argument_group('Other arguments')
102 group.add_argument('--no-dedupe', action='store_false', dest='dedupe',
103 help='Do not remove duplicates from the result list')
109 params = dict(q=args.query)
111 params = {k: getattr(args, k) for k, _ in STRUCTURED_QUERY if getattr(args, k)}
113 for param, _ in EXTRADATA_PARAMS:
114 if getattr(args, param):
116 for param in ('format', 'countrycodes', 'exclude_place_ids', 'limit', 'viewbox'):
117 if getattr(args, param):
118 params[param] = getattr(args, param)
120 params['accept-language'] = args.lang
121 if args.polygon_output:
122 params['polygon_' + args.polygon_output] = '1'
123 if args.polygon_threshold:
124 params['polygon_threshold'] = args.polygon_threshold
126 params['bounded'] = '1'
128 params['dedupe'] = '0'
130 return _run_api('search', args, params)
134 Execute API reverse query.
136 This command works exactly the same as if calling the /reverse endpoint on
137 the web API. See the online documentation for more details on the
139 https://nominatim.org/release-docs/latest/api/Reverse/
143 def add_args(parser):
144 group = parser.add_argument_group('Query arguments')
145 group.add_argument('--lat', type=float, required=True,
146 help='Latitude of coordinate to look up (in WGS84)')
147 group.add_argument('--lon', type=float, required=True,
148 help='Longitude of coordinate to look up (in WGS84)')
149 group.add_argument('--zoom', type=int,
150 help='Level of detail required for the address')
152 _add_api_output_arguments(parser)
157 params = dict(lat=args.lat, lon=args.lon, format=args.format)
158 if args.zoom is not None:
159 params['zoom'] = args.zoom
161 for param, _ in EXTRADATA_PARAMS:
162 if getattr(args, param):
165 params['accept-language'] = args.lang
166 if args.polygon_output:
167 params['polygon_' + args.polygon_output] = '1'
168 if args.polygon_threshold:
169 params['polygon_threshold'] = args.polygon_threshold
171 return _run_api('reverse', args, params)
176 Execute API lookup query.
178 This command works exactly the same as if calling the /lookup endpoint on
179 the web API. See the online documentation for more details on the
181 https://nominatim.org/release-docs/latest/api/Lookup/
185 def add_args(parser):
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)
196 params = dict(osm_ids=','.join(args.ids), format=args.format)
198 for param, _ in EXTRADATA_PARAMS:
199 if getattr(args, param):
202 params['accept-language'] = args.lang
203 if args.polygon_output:
204 params['polygon_' + args.polygon_output] = '1'
205 if args.polygon_threshold:
206 params['polygon_threshold'] = args.polygon_threshold
208 return _run_api('lookup', args, params)
213 Execute API details query.
215 This command works exactly the same as if calling the /details endpoint on
216 the web API. See the online documentation for more details on the
218 https://nominatim.org/release-docs/latest/api/Details/
222 def add_args(parser):
223 group = parser.add_argument_group('Query arguments')
224 objs = group.add_mutually_exclusive_group(required=True)
225 objs.add_argument('--node', '-n', type=int,
226 help="Look up the OSM node with the given ID.")
227 objs.add_argument('--way', '-w', type=int,
228 help="Look up the OSM way with the given ID.")
229 objs.add_argument('--relation', '-r', type=int,
230 help="Look up the OSM relation with the given ID.")
231 objs.add_argument('--place_id', '-p', type=int,
232 help='Database internal identifier of the OSM object to look up')
233 group.add_argument('--class', dest='object_class',
234 help=("Class type to disambiguated multiple entries "
235 "of the same object."))
237 group = parser.add_argument_group('Output arguments')
238 for name, desc in DETAILS_SWITCHES:
239 group.add_argument('--' + name, action='store_true', help=desc)
240 group.add_argument('--lang', '--accept-language', metavar='LANGS',
241 help='Preferred language order for presenting search results')
246 params = dict(osmtype='N', osmid=args.node)
248 params = dict(osmtype='W', osmid=args.node)
250 params = dict(osmtype='R', osmid=args.node)
252 params = dict(place_id=args.place_id)
253 if args.object_class:
254 params['class'] = args.object_class
255 for name, _ in DETAILS_SWITCHES:
256 params[name] = '1' if getattr(args, name) else '0'
258 params['accept-language'] = args.lang
260 return _run_api('details', args, params)
265 Execute API status query.
267 This command works exactly the same as if calling the /status endpoint on
268 the web API. See the online documentation for more details on the
270 https://nominatim.org/release-docs/latest/api/Status/
274 def add_args(parser):
275 group = parser.add_argument_group('API parameters')
276 group.add_argument('--format', default='text', choices=['text', 'json'],
277 help='Format of result')
281 return _run_api('status', args, dict(format=args.format))