2 #-----------------------------------------------------------------------------
 
   3 # nominatim - [description]
 
   4 #-----------------------------------------------------------------------------
 
   5 # Copyright 2010, Brian Quinion
 
   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.
 
  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.
 
  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 #-----------------------------------------------------------------------------
 
  37 #include "nominatim.h"
 
  38 #include "postgresql.h"
 
  46 void exit_nicely(void)
 
  48     fprintf(stderr, "Error occurred, cleaning up\n");
 
  52 void short_usage(char *arg0)
 
  54     const char *name = basename(arg0);
 
  56     fprintf(stderr, "Usage error. For further information see:\n");
 
  57     fprintf(stderr, "\t%s -h|--help\n", name);
 
  60 static void long_usage(char *arg0)
 
  62     const char *name = basename(arg0);
 
  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");
 
  88     if (sizeof(int*) == 4)
 
  90         fprintf(stderr, "\n\nYou are running this on 32bit system - this will not work\n");
 
  94 int main(int argc, char *argv[])
 
  96     int long_usage_bool=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;
 
 110     const char *file = NULL;
 
 111     const char *tagsfile = "partitionedtags.def";
 
 114     //structuredinputfile = "out.osms";
 
 118     fprintf(stderr, "nominatim version %s\n\n", NOMINATIM_VERSION);
 
 122         int c, option_index = 0;
 
 123         static struct option long_options[] =
 
 127             {"verbose",  0, 0, 'v'},
 
 129             {"database", 1, 0, 'd'},
 
 130             {"username", 1, 0, 'U'},
 
 131             {"password", 0, 0, 'W'},
 
 135             {"index",  0, 0, 'i'},
 
 136             {"export",  0, 0, 'e'},
 
 137             {"import",  1, 0, 'I'},
 
 138             {"threads",  1, 0, 't'},
 
 140             {"tagsfile",  1, 0, 'T'},
 
 142             {"minrank",  1, 0, 'r'},
 
 143             {"maxrank",  1, 0, 'R'},
 
 150         c = getopt_long(argc, argv, "vhd:U:WH:P:ieIt:F:T:r:R:", long_options, &option_index);
 
 187             threads=atoi(optarg);
 
 190             minrank=atoi(optarg);
 
 193             maxrank=atoi(optarg);
 
 203             short_usage(argv[0]);
 
 214     if (threads < 1) threads = 1;
 
 217         if (argc == optind) {  // No non-switch arguments
 
 218             short_usage(argv[0]);
 
 224         fprintf(stderr, "Error: --index and --import options can not be used on the same database!\n");
 
 229         password = simple_prompt("Password:", 100, 0);
 
 232         password = getenv("PGPASS");
 
 235     // Test the database connection
 
 236     conninfo = build_conninfo(db, username, password, host, port);
 
 237     conn = PQconnectdb(conninfo);
 
 238     if (PQstatus(conn) != CONNECTION_OK)
 
 240         fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
 
 245     if (!index && !export && !import)
 
 247         fprintf(stderr, "Please select index, export or import.\n");
 
 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);