2 Command-line interface to the Nominatim functions for import, update,
3 database administration and querying.
9 from pathlib import Path
11 from nominatim.config import Configuration
12 from nominatim.tools.exec_utils import run_legacy_script, run_php_server
13 from nominatim.errors import UsageError
14 from nominatim import clicmd
15 from nominatim.clicmd.args import NominatimArgs
16 from nominatim.tools import tiger_data
18 LOG = logging.getLogger()
21 class CommandlineParser:
22 """ Wraps some of the common functions for parsing the command line
23 and setting up subcommands.
25 def __init__(self, prog, description):
26 self.parser = argparse.ArgumentParser(
28 description=description,
29 formatter_class=argparse.RawDescriptionHelpFormatter)
31 self.subs = self.parser.add_subparsers(title='available commands',
34 # Arguments added to every sub-command
35 self.default_args = argparse.ArgumentParser(add_help=False)
36 group = self.default_args.add_argument_group('Default arguments')
37 group.add_argument('-h', '--help', action='help',
38 help='Show this help message and exit')
39 group.add_argument('-q', '--quiet', action='store_const', const=0,
40 dest='verbose', default=1,
41 help='Print only error messages')
42 group.add_argument('-v', '--verbose', action='count', default=1,
43 help='Increase verboseness of output')
44 group.add_argument('--project-dir', metavar='DIR', default='.',
45 help='Base directory of the Nominatim installation (default:.)')
46 group.add_argument('-j', '--threads', metavar='NUM', type=int,
47 help='Number of parallel threads to use')
50 def add_subcommand(self, name, cmd):
51 """ Add a subcommand to the parser. The subcommand must be a class
52 with a function add_args() that adds the parameters for the
53 subcommand and a run() function that executes the command.
55 parser = self.subs.add_parser(name, parents=[self.default_args],
56 help=cmd.__doc__.split('\n', 1)[0],
57 description=cmd.__doc__,
58 formatter_class=argparse.RawDescriptionHelpFormatter,
60 parser.set_defaults(command=cmd)
63 def run(self, **kwargs):
64 """ Parse the command line arguments of the program and execute the
65 appropriate subcommand.
67 args = NominatimArgs()
68 self.parser.parse_args(args=kwargs.get('cli_args'), namespace=args)
70 if args.subcommand is None:
71 self.parser.print_help()
74 for arg in ('module_dir', 'osm2pgsql_path', 'phplib_dir', 'sqllib_dir',
75 'data_dir', 'config_dir', 'phpcgi_path'):
76 setattr(args, arg, Path(kwargs[arg]))
77 args.project_dir = Path(args.project_dir).resolve()
79 if 'cli_args' not in kwargs:
80 logging.basicConfig(stream=sys.stderr,
81 format='%(asctime)s: %(message)s',
82 datefmt='%Y-%m-%d %H:%M:%S',
83 level=max(4 - args.verbose, 1) * 10)
85 args.config = Configuration(args.project_dir, args.config_dir,
86 environ=kwargs.get('environ', os.environ))
88 log = logging.getLogger()
89 log.warning('Using project directory: %s', str(args.project_dir))
92 return args.command.run(args)
93 except UsageError as exception:
94 if log.isEnabledFor(logging.DEBUG):
95 raise # use Python's exception printing
96 log.fatal('FATAL: %s', exception)
98 # If we get here, then execution has failed in some way.
102 ##### Subcommand classes
104 # Each class needs to implement two functions: add_args() adds the CLI parameters
105 # for the subfunction, run() executes the subcommand.
107 # The class documentation doubles as the help text for the command. The
108 # first line is also used in the summary when calling the program without
111 # No need to document the functions each time.
112 # pylint: disable=C0111
113 # Using non-top-level imports to make pyosmium optional for replication only.
114 # pylint: disable=E0012,C0415
117 Add additional data from a file or an online source.
119 Data is only imported, not indexed. You need to call `nominatim-update index`
120 to complete the process.
124 def add_args(parser):
125 group_name = parser.add_argument_group('Source')
126 group = group_name.add_mutually_exclusive_group(required=True)
127 group.add_argument('--file', metavar='FILE',
128 help='Import data from an OSM file')
129 group.add_argument('--diff', metavar='FILE',
130 help='Import data from an OSM diff file')
131 group.add_argument('--node', metavar='ID', type=int,
132 help='Import a single node from the API')
133 group.add_argument('--way', metavar='ID', type=int,
134 help='Import a single way from the API')
135 group.add_argument('--relation', metavar='ID', type=int,
136 help='Import a single relation from the API')
137 group.add_argument('--tiger-data', metavar='DIR',
138 help='Add housenumbers from the US TIGER census database.')
139 group = parser.add_argument_group('Extra arguments')
140 group.add_argument('--use-main-api', action='store_true',
141 help='Use OSM API instead of Overpass to download objects')
146 return tiger_data.add_tiger_data(args.config.get_libpq_dsn(),
152 params = ['update.php']
154 params.extend(('--import-file', args.file))
156 params.extend(('--import-diff', args.diff))
158 params.extend(('--import-node', args.node))
160 params.extend(('--import-way', args.way))
162 params.extend(('--import-relation', args.relation))
163 if args.use_main_api:
164 params.append('--use-main-api')
165 return run_legacy_script(*params, nominatim_env=args)
170 Export addresses as CSV file from the database.
174 def add_args(parser):
175 group = parser.add_argument_group('Output arguments')
176 group.add_argument('--output-type', default='street',
177 choices=('continent', 'country', 'state', 'county',
178 'city', 'suburb', 'street', 'path'),
179 help='Type of places to output (default: street)')
180 group.add_argument('--output-format',
181 default='street;suburb;city;county;state;country',
182 help=("Semicolon-separated list of address types "
183 "(see --output-type). Multiple ranks can be "
184 "merged into one column by simply using a "
185 "comma-separated list."))
186 group.add_argument('--output-all-postcodes', action='store_true',
187 help=("List all postcodes for address instead of "
188 "just the most likely one"))
189 group.add_argument('--language',
190 help=("Preferred language for output "
191 "(use local name, if omitted)"))
192 group = parser.add_argument_group('Filter arguments')
193 group.add_argument('--restrict-to-country', metavar='COUNTRY_CODE',
194 help='Export only objects within country')
195 group.add_argument('--restrict-to-osm-node', metavar='ID', type=int,
196 help='Export only children of this OSM node')
197 group.add_argument('--restrict-to-osm-way', metavar='ID', type=int,
198 help='Export only children of this OSM way')
199 group.add_argument('--restrict-to-osm-relation', metavar='ID', type=int,
200 help='Export only children of this OSM relation')
205 params = ['export.php',
206 '--output-type', args.output_type,
207 '--output-format', args.output_format]
208 if args.output_all_postcodes:
209 params.append('--output-all-postcodes')
211 params.extend(('--language', args.language))
212 if args.restrict_to_country:
213 params.extend(('--restrict-to-country', args.restrict_to_country))
214 if args.restrict_to_osm_node:
215 params.extend(('--restrict-to-osm-node', args.restrict_to_osm_node))
216 if args.restrict_to_osm_way:
217 params.extend(('--restrict-to-osm-way', args.restrict_to_osm_way))
218 if args.restrict_to_osm_relation:
219 params.extend(('--restrict-to-osm-relation', args.restrict_to_osm_relation))
221 return run_legacy_script(*params, nominatim_env=args)
226 Start a simple web server for serving the API.
228 This command starts the built-in PHP webserver to serve the website
229 from the current project directory. This webserver is only suitable
230 for testing and develop. Do not use it in production setups!
232 By the default, the webserver can be accessed at: http://127.0.0.1:8088
236 def add_args(parser):
237 group = parser.add_argument_group('Server arguments')
238 group.add_argument('--server', default='127.0.0.1:8088',
239 help='The address the server will listen to.')
243 run_php_server(args.server, args.project_dir / 'website')
245 def get_set_parser(**kwargs):
247 Initializes the parser and adds various subcommands for
250 parser = CommandlineParser('nominatim', nominatim.__doc__)
252 parser.add_subcommand('import', clicmd.SetupAll)
253 parser.add_subcommand('freeze', clicmd.SetupFreeze)
254 parser.add_subcommand('replication', clicmd.UpdateReplication)
256 parser.add_subcommand('special-phrases', clicmd.ImportSpecialPhrases)
258 parser.add_subcommand('add-data', UpdateAddData)
259 parser.add_subcommand('index', clicmd.UpdateIndex)
260 parser.add_subcommand('refresh', clicmd.UpdateRefresh)
262 parser.add_subcommand('admin', clicmd.AdminFuncs)
264 parser.add_subcommand('export', QueryExport)
265 parser.add_subcommand('serve', AdminServe)
267 if kwargs.get('phpcgi_path'):
268 parser.add_subcommand('search', clicmd.APISearch)
269 parser.add_subcommand('reverse', clicmd.APIReverse)
270 parser.add_subcommand('lookup', clicmd.APILookup)
271 parser.add_subcommand('details', clicmd.APIDetails)
272 parser.add_subcommand('status', clicmd.APIStatus)
274 parser.parser.epilog = 'php-cgi not found. Query commands not available.'
276 parser.add_subcommand('transition', clicmd.AdminTransition)
281 def nominatim(**kwargs):
283 Command-line tools for importing, updating, administrating and
284 querying the Nominatim database.
286 parser = get_set_parser(**kwargs)
288 return parser.run(**kwargs)