From 556095af04218ed533aedbf0fe6d6c14c6148ee2 Mon Sep 17 00:00:00 2001 From: Lucio Asnaghi Date: Mon, 23 Feb 2009 15:00:25 +0000 Subject: [PATCH] - sqlite.input: added row_offset and row_limit to parameters - sqlite.input: added wkb_format parameter for selecting WKB format (generic/spatialite) - sqlite.input: commented check of the spatial index - wkb.hpp: removed wkqSQLite in favour of wkbSpatiaLite --- include/mapnik/wkb.hpp | 5 +- plugins/input/sqlite/sqlite_datasource.cpp | 61 +++++++++++++++++----- plugins/input/sqlite/sqlite_datasource.hpp | 9 +++- plugins/input/sqlite/sqlite_featureset.cpp | 4 +- plugins/input/sqlite/sqlite_featureset.hpp | 3 ++ plugins/input/sqlite/sqlite_types.hpp | 18 +------ src/wkb.cpp | 7 +-- 7 files changed, 67 insertions(+), 40 deletions(-) diff --git a/include/mapnik/wkb.hpp b/include/mapnik/wkb.hpp index b1d237e94..621d8ec53 100644 --- a/include/mapnik/wkb.hpp +++ b/include/mapnik/wkb.hpp @@ -28,13 +28,13 @@ #include #include #include + namespace mapnik { enum wkbFormat { wkbGeneric=1, - wkbAutodetect=2, - wkbSQLite=3 + wkbSpatiaLite=2 }; class MAPNIK_DECL geometry_utils @@ -52,4 +52,5 @@ namespace mapnik geometry_utils& operator=(const geometry_utils&); }; } + #endif //WKB_HPP diff --git a/plugins/input/sqlite/sqlite_datasource.cpp b/plugins/input/sqlite/sqlite_datasource.cpp index 33e9b0207..64ab7771d 100644 --- a/plugins/input/sqlite/sqlite_datasource.cpp +++ b/plugins/input/sqlite/sqlite_datasource.cpp @@ -61,11 +61,21 @@ sqlite_datasource::sqlite_datasource(parameters const& params) metadata_(*params.get("metadata","")), geometry_field_(*params.get("geometry_field","the_geom")), key_field_(*params.get("key_field","PK_UID")), - desc_(*params.get("type"), *params.get("encoding","utf-8")) + row_offset_(*params_.get("row_offset",0)), + row_limit_(*params_.get("row_limit",0)), + desc_(*params.get("type"), *params.get("encoding","utf-8")), + format_(mapnik::wkbGeneric) { boost::optional file = params.get("file"); if (!file) throw datasource_exception("missing paramater"); + boost::optional wkb = params.get("wkb_format"); + if (wkb) + { + if (*wkb == "spatialite") + format_ = mapnik::wkbSpatiaLite; + } + multiple_geometries_ = *params_.get("multiple_geometries",false); use_spatial_index_ = *params_.get("use_spatial_index",true); @@ -118,29 +128,44 @@ sqlite_datasource::sqlite_datasource(parameters const& params) double ymin = rs->column_double (1); double xmax = rs->column_double (2); double ymax = rs->column_double (3); - + extent_.init (xmin,ymin,xmax,ymax); extent_initialized_ = true; } } +#if 0 if (use_spatial_index_) { - std::ostringstream s; - s << "select count (*) from sqlite_master"; - s << " where name = 'idx_" << table_ << "_" << geometry_field_ << "'"; - boost::scoped_ptr rs (dataset_->execute_query (s.str())); - if (rs->is_valid () && rs->step_next()) { - if (rs->column_integer (0) == 0) + std::ostringstream s; + s << "select spatial_index_enabled from geometry_columns"; + s << " where lower(f_table_name) = lower('" << table_ << "')"; + boost::scoped_ptr rs (dataset_->execute_query (s.str())); + if (rs->is_valid () && rs->step_next()) { -#ifdef MAPNIK_DEBUG - clog << "cannot use the spatial index " << endl; -#endif - use_spatial_index_ = false; + use_spatial_index_ = rs->column_integer (0) == 1; } } + + if (use_spatial_index_) + { + std::ostringstream s; + s << "select count (*) from sqlite_master"; + s << " where lower(name) = lower('idx_" << table_ << "_" << geometry_field_ << "')"; + boost::scoped_ptr rs (dataset_->execute_query (s.str())); + if (rs->is_valid () && rs->step_next()) + { + use_spatial_index_ = rs->column_integer (0) == 1; + } + } + +#ifdef MAPNIK_DEBUG + if (! use_spatial_index_) + clog << "cannot use the spatial index " << endl; +#endif } +#endif { /* @@ -239,13 +264,21 @@ featureset_ptr sqlite_datasource::features(query const& q) const s << " and ymax>=" << e.miny() << " and ymin<=" << e.maxy() << ")"; } + if (row_limit_ > 0) { + s << " limit " << row_limit_; + } + + if (row_offset_ > 0) { + s << " offset " << row_offset_; + } + //#ifdef MAPNIK_DEBUG - std::cerr << "executing sql: " << s.str() << "\n"; + std::cerr << s.str() << "\n"; //#endif boost::shared_ptr rs (dataset_->execute_query (s.str())); - return featureset_ptr (new sqlite_featureset(rs, desc_.get_encoding(), multiple_geometries_)); + return featureset_ptr (new sqlite_featureset(rs, desc_.get_encoding(), format_, multiple_geometries_)); } return featureset_ptr(); diff --git a/plugins/input/sqlite/sqlite_datasource.hpp b/plugins/input/sqlite/sqlite_datasource.hpp index 4c64985dd..fd9a5a6c2 100644 --- a/plugins/input/sqlite/sqlite_datasource.hpp +++ b/plugins/input/sqlite/sqlite_datasource.hpp @@ -28,6 +28,7 @@ #include #include #include +#include // boost #include @@ -53,8 +54,14 @@ class sqlite_datasource : public mapnik::datasource mutable bool extent_initialized_; int type_; sqlite_connection* dataset_; - std::string table_, metadata_, geometry_field_, key_field_; + std::string table_; + std::string metadata_; + std::string geometry_field_; + std::string key_field_; + const int row_offset_; + const int row_limit_; mapnik::layer_descriptor desc_; + mapnik::wkbFormat format_; bool multiple_geometries_; bool use_spatial_index_; }; diff --git a/plugins/input/sqlite/sqlite_featureset.cpp b/plugins/input/sqlite/sqlite_featureset.cpp index fd4694504..6b889b965 100644 --- a/plugins/input/sqlite/sqlite_featureset.cpp +++ b/plugins/input/sqlite/sqlite_featureset.cpp @@ -50,9 +50,11 @@ using mapnik::transcoder; sqlite_featureset::sqlite_featureset(boost::shared_ptr rs, std::string const& encoding, + mapnik::wkbFormat format, bool multiple_geometries) : rs_(rs), tr_(new transcoder(encoding)), + format_(format), multiple_geometries_(multiple_geometries) { } @@ -72,7 +74,7 @@ feature_ptr sqlite_featureset::next() #endif feature_ptr feature(new Feature(feature_id)); - geometry_utils::from_wkb(*feature,data,size,multiple_geometries_,mapnik::wkbGeneric); + geometry_utils::from_wkb(*feature,data,size,multiple_geometries_,format_); for (int i = 2; i < rs_->column_count (); ++i) { diff --git a/plugins/input/sqlite/sqlite_featureset.hpp b/plugins/input/sqlite/sqlite_featureset.hpp index 5f49602c7..0a025f096 100644 --- a/plugins/input/sqlite/sqlite_featureset.hpp +++ b/plugins/input/sqlite/sqlite_featureset.hpp @@ -27,6 +27,7 @@ // mapnik #include #include +#include // boost #include @@ -41,12 +42,14 @@ class sqlite_featureset : public mapnik::Featureset public: sqlite_featureset(boost::shared_ptr rs, std::string const& encoding, + mapnik::wkbFormat format, bool multiple_geometries); virtual ~sqlite_featureset(); mapnik::feature_ptr next(); private: boost::shared_ptr rs_; boost::scoped_ptr tr_; + mapnik::wkbFormat format_; bool multiple_geometries_; }; diff --git a/plugins/input/sqlite/sqlite_types.hpp b/plugins/input/sqlite/sqlite_types.hpp index 763aabcd6..5b64c7a5e 100644 --- a/plugins/input/sqlite/sqlite_types.hpp +++ b/plugins/input/sqlite/sqlite_types.hpp @@ -119,13 +119,13 @@ class sqlite_connection { public: - typedef int (*sqlite_query_callback) (void*, int, char**, char**); - sqlite_connection (const std::string& file) : db_(0) { if (sqlite3_open (file.c_str(), &db_)) throw mapnik::datasource_exception (sqlite3_errmsg (db_)); + + //sqlite3_enable_load_extension(db_, 1); } ~sqlite_connection () @@ -134,20 +134,6 @@ public: sqlite3_close (db_); } - int execute_query_callback (const std::string& sql, sqlite_query_callback callback) - { - char* error_message = 0; - - int rc = sqlite3_exec (db_, sql.c_str(), callback, 0, &error_message); - if (rc != SQLITE_OK) - { - std::clog << error_message << std::endl; - sqlite3_free (error_message); - } - - return rc; - } - sqlite_resultset* execute_query (const std::string& sql) { sqlite3_stmt* stmt = 0; diff --git a/src/wkb.cpp b/src/wkb.cpp index 773025077..a90ffe002 100644 --- a/src/wkb.cpp +++ b/src/wkb.cpp @@ -61,14 +61,9 @@ namespace mapnik pos_(0), format_(format) { - if (format_ == wkbAutodetect) - { - // XXX - todo - } - switch (format_) { - case wkbSQLite: + case wkbSpatiaLite: byteOrder_ = (wkbByteOrder) wkb_[1]; pos_ = 39; break;