]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/nominatim.c
8e96f653b46416387c4e228068e66ee0d1297467
[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, "   -t|--threads\t\tNumber of threads to create for indexing.\n");
79     fprintf(stderr, "   -F|--file\t\tfile to use (either to import or export).\n");
80     fprintf(stderr, "   -T|--tagfile\t\tfile containing 'special' tag pairs\n");
81     fprintf(stderr, "                \t(default: partitionedtags.def).\n");
82     fprintf(stderr, "   -h|--help\t\tHelp information.\n");
83     fprintf(stderr, "   -v|--verbose\t\tVerbose output.\n");
84     fprintf(stderr, "\n");
85
86     if (sizeof(int*) == 4) {
87         fprintf(stderr, "\n\nYou are running this on 32bit system - this will not work\n");
88     }
89 }
90
91 int main(int argc, char *argv[])
92 {
93     int long_usage_bool=0;
94     int pass_prompt=0;
95     const char *db = "nominatim";
96     const char *username=NULL;
97     const char *host=NULL;
98     const char *password=NULL;
99     const char *port = "5432";
100     const char *conninfo = NULL;
101     int index = 0;
102     int export = 0;
103     int import = 0;
104     int threads = 1;
105     const char *file = NULL;
106     const char *tagsfile = "partitionedtags.def";
107
108     //import = 1;
109     //structuredinputfile = "out.osms";
110
111     PGconn *conn;
112
113     fprintf(stderr, "nominatim SVN version %s\n\n", VERSION);
114
115     while (1) {
116         int c, option_index = 0;
117         static struct option long_options[] = {
118             {"help",     0, 0, 'h'},
119
120             {"verbose",  0, 0, 'v'},
121
122             {"database", 1, 0, 'd'},
123             {"username", 1, 0, 'U'},
124             {"password", 0, 0, 'W'},
125             {"host",     1, 0, 'H'},
126             {"port",     1, 0, 'P'},
127
128             {"index",  0, 0, 'i'},
129             {"export",  0, 0, 'e'},
130             {"import",  1, 0, 'I'},
131             {"threads",  1, 0, 't'},
132             {"file",  1, 0, 'F'},
133             {"tagsfile",  1, 0, 'T'},
134
135
136             {0, 0, 0, 0}
137         };
138
139         c = getopt_long(argc, argv, "vhd:U:WH:P:ieIt:F:T:", long_options, &option_index);
140         if (c == -1)
141             break;
142
143         switch (c) {
144             case 'v': verbose=1;  break;
145             case 'd': db=optarg;  break;
146             case 'U': username=optarg; break;
147             case 'W': pass_prompt=1; break;
148             case 'H': host=optarg; break;
149             case 'P': port=optarg; break;
150             case 'h': long_usage_bool=1; break;
151             case 'i': index=1; break;
152             case 'e': export=1; break;
153             case 'I': import=1; break;
154             case 't': threads=atoi(optarg); break;
155             case 'F': file=optarg; break;
156             case 'T': tagsfile=optarg; break;
157             case '?':
158             default:
159                 short_usage(argv[0]);
160                 exit(EXIT_FAILURE);
161         }
162     }
163
164     if (long_usage_bool) {
165         long_usage(argv[0]);
166         exit(EXIT_FAILURE);
167     }
168
169     if (threads < 1) threads = 1;
170
171 /*
172     if (argc == optind) {  // No non-switch arguments
173         short_usage(argv[0]);
174         exit(EXIT_FAILURE);
175     }
176 */
177     if (index && import) {
178         fprintf(stderr, "Error: --index and --import options can not be used on the same database!\n");
179         exit(EXIT_FAILURE);
180     }
181
182     if (pass_prompt)
183         password = simple_prompt("Password:", 100, 0);
184     else {
185         password = getenv("PGPASS");
186     }
187
188     // Test the database connection
189     conninfo = build_conninfo(db, username, password, host, port);
190     conn = PQconnectdb(conninfo);
191     if (PQstatus(conn) != CONNECTION_OK) {
192         fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
193         exit(EXIT_FAILURE);
194     }
195     PQfinish(conn);
196
197     if (!index && !export && !import)
198     {
199        fprintf(stderr, "Please select index, export or import.\n");
200         exit(EXIT_FAILURE);
201     }
202     if (index) nominatim_index(0, 30, threads, conninfo, file);
203     if (export) nominatim_export(0, 30, conninfo, file);
204     if (import) nominatim_import(conninfo, tagsfile, file);
205
206     return 0;
207 }