/home/martin/workspace/OpenStreetNav/src/wayreduction/main.cc
Go to the documentation of this file.
00001 /*
00002  * main.cc
00003  *
00004  *  Created on: Jan 15, 2012
00005  *      Author: martin
00006  */
00007 
00008 #include <string>
00009 #include "../osmdb/osmdb.h"
00010 #include "wayreduction.h"
00011 #include <boost/program_options.hpp>
00012 #include <boost/algorithm/string/regex.hpp>
00013 #include <fstream>
00014 #include "WayNodeFilter.h"
00015 
00016 void action_handler(osmdb::ImportTableAction act, int64_t amount)
00017 {
00018     using osmdb::ImportTableAction;
00019     switch (act)
00020     {
00021         case ImportTableAction::ANALYZE:
00022             std::cout << "Done analyzing" << std::endl;
00023             break;
00024         case ImportTableAction::CLEAR_IMPORT:
00025             std::cout << "Done cleaning up" << std::endl;
00026             break;
00027         case ImportTableAction::CREATE_IMPORT_INDEX:
00028             std::cout << "Created import indexes" << std::endl;
00029             break;
00030         case ImportTableAction::CREATE_IMPORT_PKEY:
00031             std::cout << "Created import primary key" << std::endl;
00032             break;
00033         case ImportTableAction::IMPORT_EDGES:
00034             std::cout << "Imported " << amount << " edges" << std::endl;
00035             break;
00036         case ImportTableAction::IMPORT_WAY_NODE:
00037             std::cout << "Imported " << amount << " way nodes" << std::endl;
00038             break;
00039         default:
00040             break;
00041     }
00042 }
00043 
00044 int reduce(std::string const& dbname, std::string const& base, std::string dest, std::multimap<std::string, std::string> mp)
00045 {
00046     try
00047     {
00048         std::string connstr;
00049         if (dbname != "")
00050             connstr = "dbname=" + dbname;
00051         psql::Database pdb(connstr, true);
00052         psql::Database pdb2(connstr, true);
00053         pdb.set_schema(base);
00054         pdb2.create_schema(dest);
00055         if (base != "")
00056             dest += "," + base;
00057         pdb2.set_schema(dest);
00058         osmdb::OsmDatabase odb(pdb);
00059         osmdb::OsmDatabase odb2(pdb2);
00060         odb2.create_edge_tables();
00061         osmdb::ElementCopy cp(odb2);
00062 
00063         wayred::WayNodeFilter flt;
00064         for (auto it = mp.begin(); it != mp.end(); ++it)
00065             flt.add_important(it->first, it->second);
00066         cp.start_copy();
00067         pdb.begin_transaction();
00068         osmdb::WayLister wl(odb, mp);
00069         std::cout << "Processing ways";
00070         while (!wl.end())
00071         {
00072             std::cout << ".";
00073             auto const& mp = wl.get_current_connected_ways();
00074             auto v = flt.reduce_ways(mp);
00075             for (unsigned int i = 0; i < v.size(); ++i)
00076             {
00077                 cp.insert_way(v[i]);
00078             }
00079             wl.next();
00080         }
00081         std::cout << std::endl;
00082         wl.end();
00083         pdb.commit_transaction();
00084         cp.end_copy();
00085         pdb2.begin_transaction();
00086         osmdb::ImportTableProcessor proc(odb2);
00087         proc.disable_all();
00088         proc.enable(osmdb::ImportTableAction::CLEAR_IMPORT);
00089         proc.enable(osmdb::ImportTableAction::ANALYZE);
00090         proc.enable(osmdb::ImportTableAction::CREATE_IMPORT_INDEX);
00091         proc.enable(osmdb::ImportTableAction::CREATE_IMPORT_PKEY);
00092         proc.enable(osmdb::ImportTableAction::IMPORT_EDGES);
00093         proc.enable(osmdb::ImportTableAction::IMPORT_WAY_NODE);
00094         proc.action_signal.connect(action_handler);
00095         proc.process();
00096         odb2.create_edge_keys_and_indexes();
00097         pdb2.commit_transaction();
00098         std::cout << "Success" << std::endl;
00099     }
00100     catch (std::exception& ex)
00101     {
00102         std::cout << "Error occured" << std::endl << ex.what() << std::endl;
00103         return 1;
00104     }
00105     return 0;
00106 }
00107 
00108 int main (int argc, char** argv)
00109 {
00110     boost::program_options::options_description desc("Allowed options");
00111     desc.add_options()
00112     ("help,h", "print this help")
00113     ("database,d", boost::program_options::value<std::string>(), "postgresql database to operate on")
00114     ("base-schema,b", boost::program_options::value<std::string>(), "base schema name - this needs to exist")
00115     ("dest-schema,s", boost::program_options::value<std::string>(), "destination schema name - this shouldn't exist")
00116     ("important,i", boost::program_options::value<std::string>(), "pipe separated key value pairs to consider e.g. (key1/val1|key2/val2)")
00117     ("important-file,I", boost::program_options::value<std::string>(), "file listing key value pairs to consider (newline separated)");
00118     boost::program_options::variables_map vm;
00119     boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
00120     boost::program_options::notify(vm);
00121     if (vm.count("help"))
00122     {
00123         std::cout << desc << std::endl;
00124         return 0;
00125     }
00126     std::string dbname;
00127     if (vm.count("database"))
00128         dbname = vm["database"].as<std::string>();
00129     std::string base;
00130     if (vm.count("base-schema"))
00131         base = vm["base-schema"].as<std::string>();
00132     if (!vm.count("dest-schema"))
00133     {
00134         std::cout << "You need to specify destination schema" << std::endl << desc << std::endl;
00135     }
00136     std::string dest(vm["dest-schema"].as<std::string>());
00137     std::multimap<std::string, std::string> mp;
00138     boost::regex r("\\|");
00139     boost::regex r2("\\/");
00140     if (vm.count("important"))
00141     {
00142         std::vector<std::string> vect;
00143         boost::algorithm::split_regex(vect, vm["important"].as<std::string>(), r);
00144         for (unsigned int i = 0; i < vect.size(); ++i)
00145         {
00146             std::vector<std::string> v2;
00147             boost::algorithm::split_regex(v2, vect[i], r2);
00148             if (v2.size() != 2)
00149             {
00150                 std::cout << "Error parsing important attributes" << std::endl;
00151                 return 1;
00152             }
00153             mp.insert(std::make_pair(v2[0], v2[1]));
00154         }
00155     }
00156     if (vm.count("important-file"))
00157     {
00158         std::ifstream ifs(vm["important-file"].as<std::string>(), std::ifstream::in);
00159         std::string s;
00160         while (!ifs.eof())
00161         {
00162             ifs >> s;
00163             std::vector<std::string> v2;
00164             boost::algorithm::split_regex(v2, s, r2);
00165             if (v2.size() != 2)
00166             {
00167                 std::cout << "Error parsing important attributes" << std::endl;
00168                 return 1;
00169             }
00170             mp.insert(std::make_pair(v2[0], v2[1]));
00171         }
00172     }
00173     return reduce(dbname, base, dest, mp);
00174 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines