@@ -180,29 +180,32 @@ def run(self, args: NominatimArgs) -> int:
180
180
raise UsageError (f"Unsupported format '{ args .format } '. "
181
181
'Use --list-formats to see supported formats.' )
182
182
183
- api = napi .NominatimAPI (args .project_dir )
184
- params : Dict [str , Any ] = {'max_results' : args .limit + min (args .limit , 10 ),
185
- 'address_details' : True , # needed for display name
186
- 'geometry_output' : _get_geometry_output (args ),
187
- 'geometry_simplification' : args .polygon_threshold ,
188
- 'countries' : args .countrycodes ,
189
- 'excluded' : args .exclude_place_ids ,
190
- 'viewbox' : args .viewbox ,
191
- 'bounded_viewbox' : args .bounded ,
192
- 'locales' : _get_locales (args , api .config .DEFAULT_LANGUAGE )
193
- }
194
-
195
- if args .query :
196
- results = api .search (args .query , ** params )
197
- else :
198
- results = api .search_address (amenity = args .amenity ,
199
- street = args .street ,
200
- city = args .city ,
201
- county = args .county ,
202
- state = args .state ,
203
- postalcode = args .postalcode ,
204
- country = args .country ,
205
- ** params )
183
+ try :
184
+ with napi .NominatimAPI (args .project_dir ) as api :
185
+ params : Dict [str , Any ] = {'max_results' : args .limit + min (args .limit , 10 ),
186
+ 'address_details' : True , # needed for display name
187
+ 'geometry_output' : _get_geometry_output (args ),
188
+ 'geometry_simplification' : args .polygon_threshold ,
189
+ 'countries' : args .countrycodes ,
190
+ 'excluded' : args .exclude_place_ids ,
191
+ 'viewbox' : args .viewbox ,
192
+ 'bounded_viewbox' : args .bounded ,
193
+ 'locales' : _get_locales (args , api .config .DEFAULT_LANGUAGE )
194
+ }
195
+
196
+ if args .query :
197
+ results = api .search (args .query , ** params )
198
+ else :
199
+ results = api .search_address (amenity = args .amenity ,
200
+ street = args .street ,
201
+ city = args .city ,
202
+ county = args .county ,
203
+ state = args .state ,
204
+ postalcode = args .postalcode ,
205
+ country = args .country ,
206
+ ** params )
207
+ except napi .UsageError as ex :
208
+ raise UsageError (ex ) from ex
206
209
207
210
if args .dedupe and len (results ) > 1 :
208
211
results = deduplicate_results (results , args .limit )
@@ -260,14 +263,19 @@ def run(self, args: NominatimArgs) -> int:
260
263
if args .lat is None or args .lon is None :
261
264
raise UsageError ("lat' and 'lon' parameters are required." )
262
265
263
- api = napi .NominatimAPI (args .project_dir )
264
- result = api .reverse (napi .Point (args .lon , args .lat ),
265
- max_rank = zoom_to_rank (args .zoom or 18 ),
266
- layers = _get_layers (args , napi .DataLayer .ADDRESS | napi .DataLayer .POI ),
267
- address_details = True , # needed for display name
268
- geometry_output = _get_geometry_output (args ),
269
- geometry_simplification = args .polygon_threshold ,
270
- locales = _get_locales (args , api .config .DEFAULT_LANGUAGE ))
266
+ layers = _get_layers (args , napi .DataLayer .ADDRESS | napi .DataLayer .POI )
267
+
268
+ try :
269
+ with napi .NominatimAPI (args .project_dir ) as api :
270
+ result = api .reverse (napi .Point (args .lon , args .lat ),
271
+ max_rank = zoom_to_rank (args .zoom or 18 ),
272
+ layers = layers ,
273
+ address_details = True , # needed for display name
274
+ geometry_output = _get_geometry_output (args ),
275
+ geometry_simplification = args .polygon_threshold ,
276
+ locales = _get_locales (args , api .config .DEFAULT_LANGUAGE ))
277
+ except napi .UsageError as ex :
278
+ raise UsageError (ex ) from ex
271
279
272
280
if args .format == 'debug' :
273
281
print (loglib .get_and_disable ())
@@ -323,12 +331,15 @@ def run(self, args: NominatimArgs) -> int:
323
331
324
332
places = [napi .OsmID (o [0 ], int (o [1 :])) for o in args .ids ]
325
333
326
- api = napi .NominatimAPI (args .project_dir )
327
- results = api .lookup (places ,
328
- address_details = True , # needed for display name
329
- geometry_output = _get_geometry_output (args ),
330
- geometry_simplification = args .polygon_threshold or 0.0 ,
331
- locales = _get_locales (args , api .config .DEFAULT_LANGUAGE ))
334
+ try :
335
+ with napi .NominatimAPI (args .project_dir ) as api :
336
+ results = api .lookup (places ,
337
+ address_details = True , # needed for display name
338
+ geometry_output = _get_geometry_output (args ),
339
+ geometry_simplification = args .polygon_threshold or 0.0 ,
340
+ locales = _get_locales (args , api .config .DEFAULT_LANGUAGE ))
341
+ except napi .UsageError as ex :
342
+ raise UsageError (ex ) from ex
332
343
333
344
if args .format == 'debug' :
334
345
print (loglib .get_and_disable ())
@@ -410,17 +421,20 @@ def run(self, args: NominatimArgs) -> int:
410
421
raise UsageError ('One of the arguments --node/-n --way/-w '
411
422
'--relation/-r --place_id/-p is required/' )
412
423
413
- api = napi .NominatimAPI (args .project_dir )
414
- locales = _get_locales (args , api .config .DEFAULT_LANGUAGE )
415
- result = api .details (place ,
416
- address_details = args .addressdetails ,
417
- linked_places = args .linkedplaces ,
418
- parented_places = args .hierarchy ,
419
- keywords = args .keywords ,
420
- geometry_output = napi .GeometryFormat .GEOJSON
421
- if args .polygon_geojson
422
- else napi .GeometryFormat .NONE ,
423
- locales = locales )
424
+ try :
425
+ with napi .NominatimAPI (args .project_dir ) as api :
426
+ locales = _get_locales (args , api .config .DEFAULT_LANGUAGE )
427
+ result = api .details (place ,
428
+ address_details = args .addressdetails ,
429
+ linked_places = args .linkedplaces ,
430
+ parented_places = args .hierarchy ,
431
+ keywords = args .keywords ,
432
+ geometry_output = napi .GeometryFormat .GEOJSON
433
+ if args .polygon_geojson
434
+ else napi .GeometryFormat .NONE ,
435
+ locales = locales )
436
+ except napi .UsageError as ex :
437
+ raise UsageError (ex ) from ex
424
438
425
439
if args .format == 'debug' :
426
440
print (loglib .get_and_disable ())
@@ -465,7 +479,11 @@ def run(self, args: NominatimArgs) -> int:
465
479
raise UsageError (f"Unsupported format '{ args .format } '. "
466
480
'Use --list-formats to see supported formats.' )
467
481
468
- status = napi .NominatimAPI (args .project_dir ).status ()
482
+ try :
483
+ with napi .NominatimAPI (args .project_dir ) as api :
484
+ status = api .status ()
485
+ except napi .UsageError as ex :
486
+ raise UsageError (ex ) from ex
469
487
470
488
if args .format == 'debug' :
471
489
print (loglib .get_and_disable ())
0 commit comments