- 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:
parent
484ab6cc1c
commit
556095af04
7 changed files with 67 additions and 40 deletions
|
@ -28,13 +28,13 @@
|
||||||
#include <mapnik/geometry.hpp>
|
#include <mapnik/geometry.hpp>
|
||||||
#include <mapnik/ctrans.hpp>
|
#include <mapnik/ctrans.hpp>
|
||||||
#include <mapnik/feature.hpp>
|
#include <mapnik/feature.hpp>
|
||||||
|
|
||||||
namespace mapnik
|
namespace mapnik
|
||||||
{
|
{
|
||||||
enum wkbFormat
|
enum wkbFormat
|
||||||
{
|
{
|
||||||
wkbGeneric=1,
|
wkbGeneric=1,
|
||||||
wkbAutodetect=2,
|
wkbSpatiaLite=2
|
||||||
wkbSQLite=3
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class MAPNIK_DECL geometry_utils
|
class MAPNIK_DECL geometry_utils
|
||||||
|
@ -52,4 +52,5 @@ namespace mapnik
|
||||||
geometry_utils& operator=(const geometry_utils&);
|
geometry_utils& operator=(const geometry_utils&);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //WKB_HPP
|
#endif //WKB_HPP
|
||||||
|
|
|
@ -61,11 +61,21 @@ sqlite_datasource::sqlite_datasource(parameters const& params)
|
||||||
metadata_(*params.get<std::string>("metadata","")),
|
metadata_(*params.get<std::string>("metadata","")),
|
||||||
geometry_field_(*params.get<std::string>("geometry_field","the_geom")),
|
geometry_field_(*params.get<std::string>("geometry_field","the_geom")),
|
||||||
key_field_(*params.get<std::string>("key_field","PK_UID")),
|
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");
|
boost::optional<std::string> file = params.get<std::string>("file");
|
||||||
if (!file) throw datasource_exception("missing <file> paramater");
|
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);
|
multiple_geometries_ = *params_.get<mapnik::boolean>("multiple_geometries",false);
|
||||||
use_spatial_index_ = *params_.get<mapnik::boolean>("use_spatial_index",true);
|
use_spatial_index_ = *params_.get<mapnik::boolean>("use_spatial_index",true);
|
||||||
|
|
||||||
|
@ -124,23 +134,38 @@ sqlite_datasource::sqlite_datasource(parameters const& params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
if (use_spatial_index_)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
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())
|
||||||
|
{
|
||||||
|
use_spatial_index_ = rs->column_integer (0) == 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (use_spatial_index_)
|
if (use_spatial_index_)
|
||||||
{
|
{
|
||||||
std::ostringstream s;
|
std::ostringstream s;
|
||||||
s << "select count (*) from sqlite_master";
|
s << "select count (*) from sqlite_master";
|
||||||
s << " where name = 'idx_" << table_ << "_" << geometry_field_ << "'";
|
s << " where lower(name) = lower('idx_" << table_ << "_" << geometry_field_ << "')";
|
||||||
boost::scoped_ptr<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
boost::scoped_ptr<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
||||||
if (rs->is_valid () && rs->step_next())
|
if (rs->is_valid () && rs->step_next())
|
||||||
{
|
{
|
||||||
if (rs->column_integer (0) == 0)
|
use_spatial_index_ = rs->column_integer (0) == 1;
|
||||||
{
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef MAPNIK_DEBUG
|
#ifdef MAPNIK_DEBUG
|
||||||
|
if (! use_spatial_index_)
|
||||||
clog << "cannot use the spatial index " << endl;
|
clog << "cannot use the spatial index " << endl;
|
||||||
#endif
|
#endif
|
||||||
use_spatial_index_ = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
|
@ -239,13 +264,21 @@ featureset_ptr sqlite_datasource::features(query const& q) const
|
||||||
s << " and ymax>=" << e.miny() << " and ymin<=" << e.maxy() << ")";
|
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
|
//#ifdef MAPNIK_DEBUG
|
||||||
std::cerr << "executing sql: " << s.str() << "\n";
|
std::cerr << s.str() << "\n";
|
||||||
//#endif
|
//#endif
|
||||||
|
|
||||||
boost::shared_ptr<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
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();
|
return featureset_ptr();
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
#include <mapnik/datasource.hpp>
|
#include <mapnik/datasource.hpp>
|
||||||
#include <mapnik/feature.hpp>
|
#include <mapnik/feature.hpp>
|
||||||
#include <mapnik/feature_layer_desc.hpp>
|
#include <mapnik/feature_layer_desc.hpp>
|
||||||
|
#include <mapnik/wkb.hpp>
|
||||||
|
|
||||||
// boost
|
// boost
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
|
@ -53,8 +54,14 @@ class sqlite_datasource : public mapnik::datasource
|
||||||
mutable bool extent_initialized_;
|
mutable bool extent_initialized_;
|
||||||
int type_;
|
int type_;
|
||||||
sqlite_connection* dataset_;
|
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::layer_descriptor desc_;
|
||||||
|
mapnik::wkbFormat format_;
|
||||||
bool multiple_geometries_;
|
bool multiple_geometries_;
|
||||||
bool use_spatial_index_;
|
bool use_spatial_index_;
|
||||||
};
|
};
|
||||||
|
|
|
@ -50,9 +50,11 @@ using mapnik::transcoder;
|
||||||
|
|
||||||
sqlite_featureset::sqlite_featureset(boost::shared_ptr<sqlite_resultset> rs,
|
sqlite_featureset::sqlite_featureset(boost::shared_ptr<sqlite_resultset> rs,
|
||||||
std::string const& encoding,
|
std::string const& encoding,
|
||||||
|
mapnik::wkbFormat format,
|
||||||
bool multiple_geometries)
|
bool multiple_geometries)
|
||||||
: rs_(rs),
|
: rs_(rs),
|
||||||
tr_(new transcoder(encoding)),
|
tr_(new transcoder(encoding)),
|
||||||
|
format_(format),
|
||||||
multiple_geometries_(multiple_geometries)
|
multiple_geometries_(multiple_geometries)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -72,7 +74,7 @@ feature_ptr sqlite_featureset::next()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
feature_ptr feature(new Feature(feature_id));
|
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)
|
for (int i = 2; i < rs_->column_count (); ++i)
|
||||||
{
|
{
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
// mapnik
|
// mapnik
|
||||||
#include <mapnik/datasource.hpp>
|
#include <mapnik/datasource.hpp>
|
||||||
#include <mapnik/unicode.hpp>
|
#include <mapnik/unicode.hpp>
|
||||||
|
#include <mapnik/wkb.hpp>
|
||||||
|
|
||||||
// boost
|
// boost
|
||||||
#include <boost/scoped_ptr.hpp>
|
#include <boost/scoped_ptr.hpp>
|
||||||
|
@ -41,12 +42,14 @@ class sqlite_featureset : public mapnik::Featureset
|
||||||
public:
|
public:
|
||||||
sqlite_featureset(boost::shared_ptr<sqlite_resultset> rs,
|
sqlite_featureset(boost::shared_ptr<sqlite_resultset> rs,
|
||||||
std::string const& encoding,
|
std::string const& encoding,
|
||||||
|
mapnik::wkbFormat format,
|
||||||
bool multiple_geometries);
|
bool multiple_geometries);
|
||||||
virtual ~sqlite_featureset();
|
virtual ~sqlite_featureset();
|
||||||
mapnik::feature_ptr next();
|
mapnik::feature_ptr next();
|
||||||
private:
|
private:
|
||||||
boost::shared_ptr<sqlite_resultset> rs_;
|
boost::shared_ptr<sqlite_resultset> rs_;
|
||||||
boost::scoped_ptr<mapnik::transcoder> tr_;
|
boost::scoped_ptr<mapnik::transcoder> tr_;
|
||||||
|
mapnik::wkbFormat format_;
|
||||||
bool multiple_geometries_;
|
bool multiple_geometries_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -119,13 +119,13 @@ class sqlite_connection
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef int (*sqlite_query_callback) (void*, int, char**, char**);
|
|
||||||
|
|
||||||
sqlite_connection (const std::string& file)
|
sqlite_connection (const std::string& file)
|
||||||
: db_(0)
|
: db_(0)
|
||||||
{
|
{
|
||||||
if (sqlite3_open (file.c_str(), &db_))
|
if (sqlite3_open (file.c_str(), &db_))
|
||||||
throw mapnik::datasource_exception (sqlite3_errmsg (db_));
|
throw mapnik::datasource_exception (sqlite3_errmsg (db_));
|
||||||
|
|
||||||
|
//sqlite3_enable_load_extension(db_, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
~sqlite_connection ()
|
~sqlite_connection ()
|
||||||
|
@ -134,20 +134,6 @@ public:
|
||||||
sqlite3_close (db_);
|
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)
|
sqlite_resultset* execute_query (const std::string& sql)
|
||||||
{
|
{
|
||||||
sqlite3_stmt* stmt = 0;
|
sqlite3_stmt* stmt = 0;
|
||||||
|
|
|
@ -61,14 +61,9 @@ namespace mapnik
|
||||||
pos_(0),
|
pos_(0),
|
||||||
format_(format)
|
format_(format)
|
||||||
{
|
{
|
||||||
if (format_ == wkbAutodetect)
|
|
||||||
{
|
|
||||||
// XXX - todo
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (format_)
|
switch (format_)
|
||||||
{
|
{
|
||||||
case wkbSQLite:
|
case wkbSpatiaLite:
|
||||||
byteOrder_ = (wkbByteOrder) wkb_[1];
|
byteOrder_ = (wkbByteOrder) wkb_[1];
|
||||||
pos_ = 39;
|
pos_ = 39;
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue