]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/nominatim.c
move checkModilePresence to class, delete own debug echo
[nominatim.git] / nominatim / nominatim.c
1 /*
2 #-----------------------------------------------------------------------------
3 # nominatim - [description]
4 #-----------------------------------------------------------------------------
5 # Copyright 2010, Brian Quinion
6 # Based on osm2pgsql
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 #-----------------------------------------------------------------------------
22 */
23
24 #define _GNU_SOURCE
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <getopt.h>
31 #include <libgen.h>
32 #include <pthread.h>
33 #include <time.h>
34
35 #include <libpq-fe.h>
36
37 #include "nominatim.h"
38 #include "postgresql.h"
39 #include "sprompt.h"
40 #include "index.h"
41 #include "export.h"
42 #include "import.h"
43
44 int verbose;
45
46 void exit_nicely(void)
47 {
48     fprintf(stderr, "Error occurred, cleaning up\n");
49     exit(1);
50 }
51
52 void short_usage(char *arg0)
53 {
54     const char *name = basename(arg0);
55
56     fprintf(stderr, "Usage error. For further information see:\n");
57     fprintf(stderr, "\t%s -h|--help\n", name);
58 }
59
60 static void long_usage(char *arg0)
61 {
62     const char *name = basename(arg0);
63
64     fprintf(stderr, "Usage:\n");
65     fprintf(stderr, "\t%s [options] planet.osms\n", name);
66     fprintf(stderr, "\nThis will import the structured osm data into a PostgreSQL database\n");
67     fprintf(stderr, "suitable for nominatim search engine\n");
68     fprintf(stderr, "\nOptions:\n");
69     fprintf(stderr, "   -d|--database\tThe name of the PostgreSQL database to connect\n");
70     fprintf(stderr, "                \tto (default: nominatim).\n");
71     fprintf(stderr, "   -U|--username\tPostgresql user name.\n");
72     fprintf(stderr, "   -W|--password\tForce password prompt.\n");
73     fprintf(stderr, "   -H|--host\t\tDatabase server hostname or socket location.\n");
74     fprintf(stderr, "   -P|--port\t\tDatabase server port.\n");
75     fprintf(stderr, "   -i|--index\t\tIndex the database.\n");
76     fprintf(stderr, "   -e|--export\t\tGenerate a structured file.\n");
77     fprintf(stderr, "   -I|--import\t\tImport a structured file.\n");
78     fprintf(stderr, "   -r|--minrank\t\tMinimum / starting rank. (default: 0))\n");
79     fprintf(stderr, "   -R|--maxrank\t\tMaximum / finishing rank. (default: 30)\n");
80     fprintf(stderr, "   -t|--threads\t\tNumber of threads to create for indexing.\n");
81     fprintf(stderr, "   -F|--file\t\tfile to use (either to import or export).\n");
82     fprintf(stderr, "   -T|--tagfile\t\tfile containing 'special' tag pairs\n");
83     fprintf(stderr, "                \t(default: partitionedtags.def).\n");
84     fprintf(stderr, "   -h|--help\t\tHelp information.\n");
85     fprintf(stderr, "   -v|--verbose\t\tVerbose output.\n");
86     fprintf(stderr, "\n");
87
88     if (sizeof(int*) == 4)
89     {
90         fprintf(stderr, "\n\nYou are running this on 32bit system - this will not work\n");
91     }
92 }
93
94 int main(int argc, char *argv[])
95 {
96     int long_usage_bool=0;
97     int pass_prompt=0;
98     const char *db = "nominatim";
99     const char *username=NULL;
100     const char *host=NULL;
101     const char *password=NULL;
102     const char *port = "5432";
103     const char *conninfo = NULL;
104     int index = 0;
105     int export = 0;
106     int import = 0;
107     int minrank = 0;
108     int maxrank = 30;
109     int threads = 1;
110     const char *file = NULL;
111     const char *tagsfile = "partitionedtags.def";
112
113     //import = 1;
114     //structuredinputfile = "out.osms";
115
116     PGconn *conn;
117
118     fprintf(stderr, "nominatim version %s\n\n", NOMINATIM_VERSION);
119
120     while (1)
121     {
122         int c, option_index = 0;
123         static struct option long_options[] =
124         {
125             {"help",     0, 0, 'h'},
126
127             {"verbose",  0, 0, 'v'},
128
129             {"database", 1, 0, 'd'},
130             {"username", 1, 0, 'U'},
131             {"password", 0, 0, 'W'},
132             {"host",     1, 0, 'H'},
133             {"port",     1, 0, 'P'},
134
135             {"index",  0, 0, 'i'},
136             {"export",  0, 0, 'e'},
137             {"import",  1, 0, 'I'},
138             {"threads",  1, 0, 't'},
139             {"file",  1, 0, 'F'},
140             {"tagsfile",  1, 0, 'T'},
141
142             {"minrank",  1, 0, 'r'},
143             {"maxrank",  1, 0, 'R'},
144
145
146
147             {0, 0, 0, 0}
148         };
149
150         c = getopt_long(argc, argv, "vhd:U:WH:P:ieIt:F:T:r:R:", long_options, &option_index);
151         if (c == -1)
152             break;
153
154         switch (c)
155         {
156         case 'v':
157             verbose=1;
158             break;
159         case 'd':
160             db=optarg;
161             break;
162         case 'U':
163             username=optarg;
164             break;
165         case 'W':
166             pass_prompt=1;
167             break;
168         case 'H':
169             host=optarg;
170             break;
171         case 'P':
172             port=optarg;
173             break;
174         case 'h':
175             long_usage_bool=1;
176             break;
177         case 'i':
178             index=1;
179             break;
180         case 'e':
181             export=1;
182             break;
183         case 'I':
184             import=1;
185             break;
186         case 't':
187             threads=atoi(optarg);
188             break;
189         case 'r':
190             minrank=atoi(optarg);
191             break;
192         case 'R':
193             maxrank=atoi(optarg);
194             break;
195         case 'F':
196             file=optarg;
197             break;
198         case 'T':
199             tagsfile=optarg;
200             break;
201         case '?':
202         default:
203             short_usage(argv[0]);
204             exit(EXIT_FAILURE);
205         }
206     }
207
208     if (long_usage_bool)
209     {
210         long_usage(argv[0]);
211         exit(EXIT_FAILURE);
212     }
213
214     if (threads < 1) threads = 1;
215
216     /*
217         if (argc == optind) {  // No non-switch arguments
218             short_usage(argv[0]);
219             exit(EXIT_FAILURE);
220         }
221     */
222     if (index && import)
223     {
224         fprintf(stderr, "Error: --index and --import options can not be used on the same database!\n");
225         exit(EXIT_FAILURE);
226     }
227
228     if (pass_prompt)
229         password = simple_prompt("Password:", 100, 0);
230     else
231     {
232         password = getenv("PGPASS");
233     }
234
235     // Test the database connection
236     conninfo = build_conninfo(db, username, password, host, port);
237     conn = PQconnectdb(conninfo);
238     if (PQstatus(conn) != CONNECTION_OK)
239     {
240         fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
241         exit(EXIT_FAILURE);
242     }
243     PQfinish(conn);
244
245     if (!index && !export && !import)
246     {
247         fprintf(stderr, "Please select index, export or import.\n");
248         exit(EXIT_FAILURE);
249     }
250     if (index) nominatim_index(minrank, maxrank, threads, conninfo, file);
251     if (export) nominatim_export(minrank, maxrank, conninfo, file);
252     if (import) nominatim_import(conninfo, tagsfile, file);
253
254     return 0;
255 }