From 7d48561a036ea826b92cec889930214755cca61f Mon Sep 17 00:00:00 2001 From: Paul Norman Date: Thu, 2 Mar 2017 19:34:47 -0800 Subject: [PATCH 1/3] Don't use DEFINEs for gazetteer DDL SQL Instead, build them up like the pgsql SQL --- output-gazetteer.cpp | 52 ++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/output-gazetteer.cpp b/output-gazetteer.cpp index 4deb42d97..e60237bf4 100644 --- a/output-gazetteer.cpp +++ b/output-gazetteer.cpp @@ -15,28 +15,8 @@ #include #include -#define CREATE_PLACE_TABLE \ - "CREATE TABLE place (" \ - " osm_type CHAR(1) NOT NULL," \ - " osm_id " POSTGRES_OSMID_TYPE " NOT NULL," \ - " class TEXT NOT NULL," \ - " type TEXT NOT NULL," \ - " name HSTORE," \ - " admin_level INTEGER," \ - " housenumber TEXT," \ - " street TEXT," \ - " addr_place TEXT," \ - " isin TEXT," \ - " postcode TEXT," \ - " country_code VARCHAR(2)," \ - " extratags HSTORE" \ - ") %s %s" - #define ADMINLEVEL_NONE 100 -#define CREATE_PLACE_ID_INDEX \ - "CREATE INDEX place_id_idx ON place USING BTREE (osm_type, osm_id) %s %s" - void place_tag_processor::clear() { // set members to sane defaults @@ -624,18 +604,34 @@ int output_gazetteer_t::start() pgsql_exec(Connection, PGRES_COMMAND_OK, "DROP TABLE IF EXISTS place"); /* Create the new table */ + + std::string sql = "CREATE TABLE place (" + " osm_type CHAR(1) NOT NULL," + " osm_id " POSTGRES_OSMID_TYPE " NOT NULL," + " class TEXT NOT NULL," + " type TEXT NOT NULL," + " name HSTORE," + " admin_level INTEGER," + " housenumber TEXT," + " street TEXT," + " addr_place TEXT," + " isin TEXT," + " postcode TEXT," + " country_code VARCHAR(2)," + " extratags HSTORE" + ")"; if (m_options.tblsmain_data) { - pgsql_exec(Connection, PGRES_COMMAND_OK, - CREATE_PLACE_TABLE, "TABLESPACE", m_options.tblsmain_data->c_str()); - } else { - pgsql_exec(Connection, PGRES_COMMAND_OK, CREATE_PLACE_TABLE, "", ""); + sql += " TABLESPACE " + m_options.tblsmain_data.get(); } + + pgsql_exec_simple(Connection, PGRES_COMMAND_OK, sql); + + std::string index_sql = + "CREATE INDEX place_id_idx ON place USING BTREE (osm_type, osm_id)"; if (m_options.tblsmain_index) { - pgsql_exec(Connection, PGRES_COMMAND_OK, - CREATE_PLACE_ID_INDEX, "TABLESPACE", m_options.tblsmain_index->c_str()); - } else { - pgsql_exec(Connection, PGRES_COMMAND_OK, CREATE_PLACE_ID_INDEX, "", ""); + index_sql += " TABLESPACE " + m_options.tblsmain_index.get(); } + pgsql_exec_simple(Connection, PGRES_COMMAND_OK, index_sql); pgsql_exec(Connection, PGRES_TUPLES_OK, "SELECT AddGeometryColumn('place', 'geometry', %d, 'GEOMETRY', 2)", srid); pgsql_exec(Connection, PGRES_COMMAND_OK, "ALTER TABLE place ALTER COLUMN geometry SET NOT NULL"); From a117445fcbcbfb4c34ce58bf9fe18525a9bdb2db Mon Sep 17 00:00:00 2001 From: Paul Norman Date: Thu, 2 Mar 2017 20:26:47 -0800 Subject: [PATCH 2/3] Use modern geometry types in Gazetteer --- output-gazetteer.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/output-gazetteer.cpp b/output-gazetteer.cpp index e60237bf4..67e02d1ca 100644 --- a/output-gazetteer.cpp +++ b/output-gazetteer.cpp @@ -618,7 +618,8 @@ int output_gazetteer_t::start() " isin TEXT," " postcode TEXT," " country_code VARCHAR(2)," - " extratags HSTORE" + " extratags HSTORE," + + (boost::format(" geometry Geometry(Geometry,%1%) NOT NULL") % srid).str() + ")"; if (m_options.tblsmain_data) { sql += " TABLESPACE " + m_options.tblsmain_data.get(); @@ -632,9 +633,6 @@ int output_gazetteer_t::start() index_sql += " TABLESPACE " + m_options.tblsmain_index.get(); } pgsql_exec_simple(Connection, PGRES_COMMAND_OK, index_sql); - - pgsql_exec(Connection, PGRES_TUPLES_OK, "SELECT AddGeometryColumn('place', 'geometry', %d, 'GEOMETRY', 2)", srid); - pgsql_exec(Connection, PGRES_COMMAND_OK, "ALTER TABLE place ALTER COLUMN geometry SET NOT NULL"); } return 0; From 8b189d34727b185e9e23490bbfa274560752d006 Mon Sep 17 00:00:00 2001 From: Paul Norman Date: Fri, 3 Mar 2017 11:40:27 -0800 Subject: [PATCH 3/3] Reformat output_gazetteer_t::start --- output-gazetteer.cpp | 100 ++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/output-gazetteer.cpp b/output-gazetteer.cpp index 67e02d1ca..768272d77 100644 --- a/output-gazetteer.cpp +++ b/output-gazetteer.cpp @@ -588,56 +588,58 @@ int output_gazetteer_t::connect() { int output_gazetteer_t::start() { - int srid = m_options.projection->target_srs(); - - places.srid_str = (boost::format("SRID=%1%;") % srid).str(); - - if(connect()) - util::exit_nicely(); - - /* Start a transaction */ - pgsql_exec(Connection, PGRES_COMMAND_OK, "BEGIN"); - - /* (Re)create the table unless we are appending */ - if (!m_options.append) { - /* Drop any existing table */ - pgsql_exec(Connection, PGRES_COMMAND_OK, "DROP TABLE IF EXISTS place"); - - /* Create the new table */ - - std::string sql = "CREATE TABLE place (" - " osm_type CHAR(1) NOT NULL," - " osm_id " POSTGRES_OSMID_TYPE " NOT NULL," - " class TEXT NOT NULL," - " type TEXT NOT NULL," - " name HSTORE," - " admin_level INTEGER," - " housenumber TEXT," - " street TEXT," - " addr_place TEXT," - " isin TEXT," - " postcode TEXT," - " country_code VARCHAR(2)," - " extratags HSTORE," + - (boost::format(" geometry Geometry(Geometry,%1%) NOT NULL") % srid).str() + - ")"; - if (m_options.tblsmain_data) { - sql += " TABLESPACE " + m_options.tblsmain_data.get(); - } - - pgsql_exec_simple(Connection, PGRES_COMMAND_OK, sql); - - std::string index_sql = - "CREATE INDEX place_id_idx ON place USING BTREE (osm_type, osm_id)"; - if (m_options.tblsmain_index) { - index_sql += " TABLESPACE " + m_options.tblsmain_index.get(); - } - pgsql_exec_simple(Connection, PGRES_COMMAND_OK, index_sql); - } - - return 0; -} + int srid = m_options.projection->target_srs(); + + places.srid_str = (boost::format("SRID=%1%;") % srid).str(); + + if (connect()) { + util::exit_nicely(); + } + + /* Start a transaction */ + pgsql_exec(Connection, PGRES_COMMAND_OK, "BEGIN"); + + /* (Re)create the table unless we are appending */ + if (!m_options.append) { + /* Drop any existing table */ + pgsql_exec(Connection, PGRES_COMMAND_OK, "DROP TABLE IF EXISTS place"); + + /* Create the new table */ + + std::string sql = + "CREATE TABLE place (" + " osm_type CHAR(1) NOT NULL," + " osm_id " POSTGRES_OSMID_TYPE " NOT NULL," + " class TEXT NOT NULL," + " type TEXT NOT NULL," + " name HSTORE," + " admin_level INTEGER," + " housenumber TEXT," + " street TEXT," + " addr_place TEXT," + " isin TEXT," + " postcode TEXT," + " country_code VARCHAR(2)," + " extratags HSTORE," + + (boost::format(" geometry Geometry(Geometry,%1%) NOT NULL") % srid) + .str() + + ")"; + if (m_options.tblsmain_data) { + sql += " TABLESPACE " + m_options.tblsmain_data.get(); + } + pgsql_exec_simple(Connection, PGRES_COMMAND_OK, sql); + + std::string index_sql = + "CREATE INDEX place_id_idx ON place USING BTREE (osm_type, osm_id)"; + if (m_options.tblsmain_index) { + index_sql += " TABLESPACE " + m_options.tblsmain_index.get(); + } + pgsql_exec_simple(Connection, PGRES_COMMAND_OK, index_sql); + } + + return 0; +} void output_gazetteer_t::stop() {