Skip to content

Commit

Permalink
pgRouting#43 and pgRouting#75 support for schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
robe2 committed Aug 9, 2015
1 parent c550120 commit e3478bf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
72 changes: 37 additions & 35 deletions src/Export2DB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Export2DB::Export2DB(const po::variables_map &vm)
+ " user=" + vm["user"].as<std::string>()
+ " dbname=" + vm["dbname"].as<std::string>()
+ " port=" + vm["db_port"].as<std::string>() ),
tables_schema( vm["schema"].as<std::string>() ),
tables_prefix( vm["prefix"].as<std::string>() ),
tables_suffix( vm["suffix"].as<std::string>() ) {

Expand Down Expand Up @@ -175,7 +176,7 @@ bool Export2DB::createTempTable (const std::string &table_description,
*/
bool Export2DB::createTable(const std::string &table_description,
const std::string &table,
const std::string &table,
const std::string &constraint) const {
std::string sql =
"CREATE TABLE " + table + "(" // + schema + "." + prefix etc
Expand All @@ -192,11 +193,12 @@ bool Export2DB::createTable(const std::string &table_description,


void Export2DB::addGeometry(
const std::string &table,
const std::string &schema, const std::string &table,
const std::string &geometry_type) const {
std::cout << " Adding Geometry: ";
/** PostGIS requires the schema to be specified as separate arg if not default user's schema **/
std::string sql =
+ " SELECT AddGeometryColumn('"
+ " SELECT AddGeometryColumn(" + ( schema == "" ? "" : "'" + schema + "' ," ) + " '"
+ table + "',"
+ "'the_geom', 4326, '" + geometry_type + "',2 );";

Expand Down Expand Up @@ -227,37 +229,37 @@ void Export2DB::addTempGeometry(
///////////////////////
void Export2DB::createTables() const{
// the following are particular of the file tables
if (createTable( create_vertices, full_table_name("ways") + "_vertices_pgr" ))
addGeometry( full_table_name("ways") + "_vertices_pgr" , "POINT" );
if (createTable( create_ways, full_table_name("ways") ))
addGeometry( full_table_name("ways"), "LINESTRING" );
createTable( create_relations_ways, full_table_name("relations_ways") );
if (createTable( create_vertices, full_schema_table_name("ways") + "_vertices_pgr" ))
addGeometry(default_tables_schema(), full_table_name("ways") + "_vertices_pgr" , "POINT" );
if (createTable( create_ways, full_schema_table_name("ways") ))
addGeometry( default_tables_schema(), full_table_name("ways"), "LINESTRING" );
createTable( create_relations_ways, full_schema_table_name("relations_ways") );

// the following are general tables
if ( createTable( create_nodes,
"osm_nodes",
(default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_nodes",
", CONSTRAINT node_id UNIQUE(osm_id)") )
addGeometry( "osm_nodes", "POINT" );
createTable( create_relations, "osm_relations" );
createTable( create_way_tag, "osm_way_tags" );
createTable( create_types, "osm_way_types" );
createTable( create_classes, "osm_way_classes" );
addGeometry(default_tables_schema(), "osm_nodes", "POINT" );
createTable( create_relations, (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_relations" );
createTable( create_way_tag, (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_way_tags" );
createTable( create_types, (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_way_types" );
createTable( create_classes, (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_way_classes" );
}



void Export2DB::dropTable(const std::string &table) const {
std::string drop_tables( "DROP TABLE IF EXIST " + table);
std::string drop_tables( "DROP TABLE IF EXISTS " + table);
PGresult *result = PQexec(mycon, drop_tables.c_str());
PQclear(result);
}



void Export2DB::dropTables() const {
dropTable(full_table_name("ways"));
dropTable(full_table_name("ways") + "_vertices_pgr");
dropTable(full_table_name("relations_ways"));
dropTable(full_schema_table_name("ways"));
dropTable(full_schema_table_name("ways") + "_vertices_pgr");
dropTable(full_schema_table_name("relations_ways"));

// we are not deleting general tables osm_
}
Expand Down Expand Up @@ -304,9 +306,9 @@ void Export2DB::exportNodes(const std::map<long long, Node*> &nodes) const {
std::string insert_into_nodes(
" WITH data AS ( "
" SELECT a.* "
" FROM __nodes_temp a LEFT JOIN osm_nodes b USING (osm_id) WHERE (b.osm_id IS NULL))"
" FROM __nodes_temp a LEFT JOIN " + (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_nodes b USING (osm_id) WHERE (b.osm_id IS NULL))"

" INSERT INTO osm_nodes"
" INSERT INTO " + (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_nodes"
"( " + nodes_columns + " ) "
" (SELECT " + nodes_columns + " FROM data); ");
q_result = PQexec(mycon, insert_into_nodes.c_str());
Expand Down Expand Up @@ -424,7 +426,7 @@ std::cout << row_data << "\n";
std::string insert_into_relations(
" WITH data AS ( "
" SELECT a.* "
" FROM __relations_temp a LEFT JOIN osm_relations b USING (relation_id, type_id)"
" FROM __relations_temp a LEFT JOIN " + (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_relations b USING (relation_id, type_id)"
" WHERE (b.relation_id IS NULL OR b.type_id IS NULL)"

" INSERT INTO osm_relations"
Expand Down Expand Up @@ -464,10 +466,10 @@ void Export2DB::exportRelationsWays(const std::vector<Relation*> &relations, Con
std::string insert_into_relations_ways(
" WITH data AS ( "
" SELECT a.* "
" FROM __relations_ways_temp a LEFT JOIN " + full_table_name("relations_ways") + " b USING (relation_id, way_id)"
" FROM __relations_ways_temp a LEFT JOIN " + full_schema_table_name("relations_ways") + " b USING (relation_id, way_id)"
" WHERE (b.relation_id IS NULL OR b.way_id IS NULL))"

" INSERT INTO " + full_table_name("relations_ways") +
" INSERT INTO " + full_schema_table_name("relations_ways") +
" SELECT * FROM data; ");
q_result = PQexec(mycon, insert_into_relations_ways.c_str());
std::cout << " Inserted: " << PQcmdTuples(q_result) << "way's relations\n" ;
Expand Down Expand Up @@ -519,10 +521,10 @@ void Export2DB::exportTags(const std::vector<Way*> &ways, Configuration *config)
std::string insert_into_tags(
" WITH data AS ( "
" SELECT a.class_id, a.way_id "
" FROM __way_tag_temp a LEFT JOIN osm_way_tags b USING ( class_id, way_id ) "
" FROM __way_tag_temp a LEFT JOIN " + (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_way_tags b USING ( class_id, way_id ) "
" WHERE ( b.class_id IS NULL OR b.way_id IS NULL))"

" INSERT INTO osm_way_tags "
" INSERT INTO " + (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_way_tags "
" SELECT * FROM data; ");

q_result = PQexec(mycon, insert_into_tags.c_str());
Expand Down Expand Up @@ -628,32 +630,32 @@ void Export2DB::exportWays(const std::vector<Way*> &ways, Configuration *config)

std::cout << "Deleting duplicated ways from temporary table\n";
std::string delete_from_temp(
" DELETE FROM __ways_temp a USING " + full_table_name("ways") + " b where a.the_geom = b.the_geom;");
" DELETE FROM __ways_temp a USING " + full_schema_table_name("ways") + " b where a.the_geom = b.the_geom;");
q_result = PQexec(mycon, delete_from_temp.c_str());
std::cout << " Deleted " << PQcmdTuples(q_result) << " duplicated ways from temporary table\n";
PQclear(q_result);

std::cout << "Updating columns on temporary table\n";
fill_source_target( "__ways_temp" , full_table_name("ways") + "_vertices_pgr");
fill_source_target( "__ways_temp" , full_schema_table_name("ways") + "_vertices_pgr");

std::cout << "Inserting new vertices in the vertex table\n";
fill_vertices_table( "__ways_temp" , full_table_name("ways") + "_vertices_pgr");
fill_vertices_table( "__ways_temp" , full_schema_table_name("ways") + "_vertices_pgr");

std::cout << "Updating source, target n temporary table\n";
fill_source_target( "__ways_temp" , full_table_name("ways") + "_vertices_pgr");
fill_source_target( "__ways_temp" , full_schema_table_name("ways") + "_vertices_pgr");


std::string insert_into_ways(
#if 0
" WITH data AS ( "
" SELECT a.* "
// insert only if the geometry is different
" FROM __ways_temp a LEFT JOIN " + full_table_name("ways") + " b USING (the_geom) "
" FROM __ways_temp a LEFT JOIN " + full_schema_table_name("ways") + " b USING (the_geom) "
// " FROM __ways_temp a LEFT JOIN " + full_table_name("ways") + " b USING (source_osm, target_osm, the_geom) "
// " WHERE (b.source_osm IS NULL OR b.target_osm IS NULL OR b.the_geom IS NULL))"
" WHERE ( b.the_geom IS NULL ))"
#endif
" INSERT INTO " + full_table_name("ways") +
" INSERT INTO " + full_schema_table_name("ways") +
"( " + ways_columns + ", source, target, length_m, cost_s, reverse_cost_s ) "
" (SELECT " + ways_columns + ", source, target, length_m, cost_s, reverse_cost_s FROM __ways_temp); ");

Expand Down Expand Up @@ -698,10 +700,10 @@ void Export2DB::exportTypes(const std::map<std::string, Type*> &types) const
std::string insert_into_types(
" WITH data AS ( "
" SELECT a.* "
" FROM __way_types_temp a LEFT JOIN osm_way_types b USING ( type_id ) "
" FROM __way_types_temp a LEFT JOIN " + (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_way_types b USING ( type_id ) "
" WHERE (b.type_id IS NULL))"

" INSERT INTO osm_way_types ( type_id, name ) "
" INSERT INTO " + (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_way_types ( type_id, name ) "
" (SELECT * FROM data); ");

q_result = PQexec(mycon, insert_into_types.c_str());
Expand Down Expand Up @@ -752,10 +754,10 @@ void Export2DB::exportClasses(const std::map<std::string, Type*> &types) const
std::string insert_into_classes(
" WITH data AS ( "
" SELECT a.* "
" FROM __classes_temp a LEFT JOIN osm_way_classes b USING ( class_id ) "
" FROM __classes_temp a LEFT JOIN " + (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_way_classes b USING ( class_id ) "
" WHERE (b.class_id IS NULL))"

" INSERT INTO osm_way_classes "
" INSERT INTO " + (default_tables_schema() == "" ? "" : default_tables_schema() + ".") + "osm_way_classes "
" SELECT * FROM data; ");

q_result = PQexec(mycon, insert_into_classes.c_str());
Expand Down
11 changes: 9 additions & 2 deletions src/Export2DB.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,24 @@ class Export2DB
const std::string &constraint = std::string("")) const;
void addTempGeometry( const std::string &table,
const std::string &geometry_type) const;
void addGeometry( const std::string &table,
void addGeometry( const std::string &schema, const std::string &table,
const std::string &geometry_type) const;
inline std::string full_table_name(const std::string &table) const {
inline std::string full_table_name(const std::string &table) const {
return tables_prefix + table + tables_suffix;
}
inline std::string full_schema_table_name(const std::string &table) const {
return (tables_schema == "" ? "" : tables_schema + ".") + tables_prefix + table + tables_suffix;
}
inline std::string default_tables_schema() const {
return tables_schema;
}
void fill_vertices_table(const std::string &table, const std::string &vertices_tab) const;
void fill_source_target(const std::string &table, const std::string &vertices_tab) const;

private:
PGconn *mycon;
std::string conninf;
std::string tables_schema;
std::string tables_prefix;
std::string tables_suffix;

Expand Down
2 changes: 2 additions & 0 deletions src/prog_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void get_option_description(po::options_description &od_desc) {
// general
("file,f", po::value<string>()->required(), "Name of your osm file (Required).")
("conf,c", po::value<string>()->required()->default_value("/usr/share/osm2pgrouting/mapconfig.xml"), "Name of your configuration xml file.")
("schema", po::value<string>()->default_value(""), "Database schema to put tables. If left blank, defaults to default schema dictated by Postgresql search_path.")
("prefix", po::value<string>()->default_value("planet_"), "Prefix added at the beginning of table names.")
("suffix", po::value<string>()->default_value(""), "Suffix added at the end of table names.")
("skipnodes,s", po::value<bool>()->default_value(true), "When ture: don't import the node table.")
Expand Down Expand Up @@ -97,6 +98,7 @@ int process_command_line(
std::cout << "dbname = " << vm["dbname"].as<std::string>() << "\n";
std::cout << "user = " << vm["user"].as<std::string>() << "\n";
std::cout << "passwd = " << vm["passwd"].as<string>() << "\n";
std::cout << "schema= " << vm["schema"].as<string>() << "\n";
std::cout << "prefix = " << vm["prefix"].as<string>() << "\n";
std::cout << "suffix = " << vm["suffix"].as<string>() << "\n";
std::cout << (vm["clean"].as<bool>()? "C" : "Don't c") << "lean tables\n";
Expand Down

0 comments on commit e3478bf

Please sign in to comment.