2009-02-20 10:43:15 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:54:32 +00:00
|
|
|
*
|
2009-02-20 10:43:15 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2015-06-16 10:49:16 +00:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
2009-02-20 10:43:15 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2009-02-22 22:36:50 +00:00
|
|
|
// mapnik
|
2009-02-20 15:58:33 +00:00
|
|
|
#include "pgsql2sqlite.hpp"
|
2009-02-20 10:43:15 +00:00
|
|
|
#include <mapnik/datasource.hpp>
|
|
|
|
#include <mapnik/wkb.hpp>
|
|
|
|
#include "connection_manager.hpp"
|
2009-02-22 22:36:50 +00:00
|
|
|
|
2016-03-10 19:44:44 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2009-02-20 10:43:15 +00:00
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
2016-03-10 19:44:44 +00:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
#include <memory>
|
2009-02-20 10:43:15 +00:00
|
|
|
|
|
|
|
//stl
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2011-04-04 03:48:39 +00:00
|
|
|
#include <exception>
|
2009-02-20 10:43:15 +00:00
|
|
|
|
2009-02-20 15:58:33 +00:00
|
|
|
int main ( int argc, char** argv)
|
2009-02-20 10:43:15 +00:00
|
|
|
{
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
namespace po = boost::program_options;
|
|
|
|
po::options_description desc("Postgresql/PostGIS to SQLite3 converter\n Options");
|
2011-04-04 03:48:39 +00:00
|
|
|
std::string usage = "usage: pgsql2sqlite --dbname db --table planet_osm_line --file osm.sqlite --query \"select * from planet_osm_line\"";
|
2011-11-14 03:54:32 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
|
2011-04-04 03:48:39 +00:00
|
|
|
desc.add_options()
|
|
|
|
("help,?","Display this help screen.")
|
|
|
|
("host,h",po::value<std::string>(),"Allows you to specify connection to a database on a machine other than the default.")
|
|
|
|
("port,p",po::value<std::string>(),"Allows you to specify a database port other than the default.")
|
|
|
|
("user,u",po::value<std::string>(),"Connect to the database as the specified user.")
|
|
|
|
("dbname,d",po::value<std::string>(),"postgresql database name")
|
|
|
|
("password,P",po::value<std::string>(),"Connect to the database with the specified password.")
|
|
|
|
("query,q",po::value<std::string>(),"Name of the table/or query to pass to postmaster")
|
|
|
|
("table,t",po::value<std::string>(),"Name of the output table to create (default: table in query)")
|
|
|
|
("file,f",po::value<std::string>(),"Use this option to specify the name of the file to create.")
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2011-04-04 03:48:39 +00:00
|
|
|
;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2011-04-04 03:48:39 +00:00
|
|
|
//po::positional_options_description p;
|
|
|
|
//p.add("table",1);
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2011-04-04 03:48:39 +00:00
|
|
|
po::variables_map vm;
|
|
|
|
//positional(p)
|
|
|
|
po::store(po::command_line_parser(argc,argv).options(desc).run(),vm);
|
2010-06-02 11:03:30 +00:00
|
|
|
po::notify(vm);
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2011-04-04 03:48:39 +00:00
|
|
|
if (vm.count("help"))
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
|
|
|
std::cout << desc << "\n";
|
2011-04-04 03:48:39 +00:00
|
|
|
std::cout << usage << "\n";
|
2010-06-02 11:03:30 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2011-04-04 03:48:39 +00:00
|
|
|
else if ( !vm.count("dbname") || !vm.count("file") || !vm.count("query") )
|
|
|
|
{
|
|
|
|
std::cout << desc << "\n";
|
|
|
|
std::cout << usage << "\n";
|
|
|
|
std::cout << "Both --dbname, --file and, --query are required\n";
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2011-04-04 03:48:39 +00:00
|
|
|
boost::optional<std::string> host;
|
|
|
|
boost::optional<std::string> port ;
|
|
|
|
boost::optional<std::string> dbname;
|
|
|
|
boost::optional<std::string> user;
|
|
|
|
boost::optional<std::string> password;
|
|
|
|
boost::optional<std::string> connect_timeout("4");
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2011-04-04 03:48:39 +00:00
|
|
|
if (vm.count("host")) host = vm["host"].as<std::string>();
|
|
|
|
if (vm.count("port")) port = vm["port"].as<std::string>();
|
|
|
|
if (vm.count("dbname")) dbname = vm["dbname"].as<std::string>();
|
|
|
|
if (vm.count("user")) user = vm["user"].as<std::string>();
|
|
|
|
if (vm.count("password")) password = vm["password"].as<std::string>();
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2011-04-04 03:48:39 +00:00
|
|
|
ConnectionCreator<Connection> creator(host,port,dbname,user,password,connect_timeout);
|
2011-11-14 03:54:32 +00:00
|
|
|
try
|
2011-04-04 03:48:39 +00:00
|
|
|
{
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<Connection> conn(creator());
|
2011-11-14 03:54:32 +00:00
|
|
|
|
|
|
|
std::string query = vm["query"].as<std::string>();
|
2011-11-13 23:02:29 +00:00
|
|
|
std::string output_table_name = vm.count("table") ? vm["table"].as<std::string>() : mapnik::sql_utils::table_from_sql(query);
|
2011-04-04 03:48:39 +00:00
|
|
|
std::string output_file = vm["file"].as<std::string>();
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2011-04-04 03:48:39 +00:00
|
|
|
std::cout << "output_table : " << output_table_name << "\n";
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2011-04-04 03:48:39 +00:00
|
|
|
mapnik::pgsql2sqlite(conn,query,output_table_name,output_file);
|
|
|
|
}
|
|
|
|
catch (mapnik::datasource_exception & ex)
|
|
|
|
{
|
|
|
|
std::cerr << ex.what() << "\n";
|
|
|
|
}
|
2009-02-22 21:36:34 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2011-04-04 03:48:39 +00:00
|
|
|
catch(std::exception& e) {
|
|
|
|
std::cerr << desc << "\n";
|
|
|
|
std::cout << usage << "\n";
|
|
|
|
std::cerr << e.what() << "\n";
|
|
|
|
return EXIT_FAILURE;
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
return EXIT_SUCCESS;
|
2009-02-20 10:43:15 +00:00
|
|
|
}
|