- 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
This commit is contained in:
Lucio Asnaghi 2009-02-23 15:00:25 +00:00
parent 484ab6cc1c
commit 556095af04
7 changed files with 67 additions and 40 deletions

View file

@ -28,13 +28,13 @@
#include <mapnik/geometry.hpp>
#include <mapnik/ctrans.hpp>
#include <mapnik/feature.hpp>
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

View file

@ -61,11 +61,21 @@ sqlite_datasource::sqlite_datasource(parameters const& params)
metadata_(*params.get<std::string>("metadata","")),
geometry_field_(*params.get<std::string>("geometry_field","the_geom")),
key_field_(*params.get<std::string>("key_field","PK_UID")),
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8"))
row_offset_(*params_.get<int>("row_offset",0)),
row_limit_(*params_.get<int>("row_limit",0)),
desc_(*params.get<std::string>("type"), *params.get<std::string>("encoding","utf-8")),
format_(mapnik::wkbGeneric)
{
boost::optional<std::string> file = params.get<std::string>("file");
if (!file) throw datasource_exception("missing <file> paramater");
boost::optional<std::string> wkb = params.get<std::string>("wkb_format");
if (wkb)
{
if (*wkb == "spatialite")
format_ = mapnik::wkbSpatiaLite;
}
multiple_geometries_ = *params_.get<mapnik::boolean>("multiple_geometries",false);
use_spatial_index_ = *params_.get<mapnik::boolean>("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<sqlite_resultset> 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<sqlite_resultset> 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<sqlite_resultset> 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<sqlite_resultset> 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();

View file

@ -28,6 +28,7 @@
#include <mapnik/datasource.hpp>
#include <mapnik/feature.hpp>
#include <mapnik/feature_layer_desc.hpp>
#include <mapnik/wkb.hpp>
// boost
#include <boost/shared_ptr.hpp>
@ -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_;
};

View file

@ -50,9 +50,11 @@ using mapnik::transcoder;
sqlite_featureset::sqlite_featureset(boost::shared_ptr<sqlite_resultset> 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)
{

View file

@ -27,6 +27,7 @@
// mapnik
#include <mapnik/datasource.hpp>
#include <mapnik/unicode.hpp>
#include <mapnik/wkb.hpp>
// boost
#include <boost/scoped_ptr.hpp>
@ -41,12 +42,14 @@ class sqlite_featureset : public mapnik::Featureset
public:
sqlite_featureset(boost::shared_ptr<sqlite_resultset> rs,
std::string const& encoding,
mapnik::wkbFormat format,
bool multiple_geometries);
virtual ~sqlite_featureset();
mapnik::feature_ptr next();
private:
boost::shared_ptr<sqlite_resultset> rs_;
boost::scoped_ptr<mapnik::transcoder> tr_;
mapnik::wkbFormat format_;
bool multiple_geometries_;
};

View file

@ -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;

View file

@ -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;