2009-02-09 19:43:57 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-04-01 01:22:19 +00:00
|
|
|
* Copyright (C) 2010 Artem Pavlenko
|
2009-02-09 19:43:57 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
#include "sqlite_datasource.hpp"
|
|
|
|
#include "sqlite_featureset.hpp"
|
|
|
|
|
|
|
|
// mapnik
|
|
|
|
#include <mapnik/ptree_helpers.hpp>
|
2010-11-12 00:12:47 +00:00
|
|
|
#include <mapnik/sql_utils.hpp>
|
2009-02-09 19:43:57 +00:00
|
|
|
|
|
|
|
// boost
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/tokenizer.hpp>
|
2009-03-11 23:39:26 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2009-02-09 19:43:57 +00:00
|
|
|
|
|
|
|
using boost::lexical_cast;
|
|
|
|
using boost::bad_lexical_cast;
|
|
|
|
|
|
|
|
using mapnik::datasource;
|
|
|
|
using mapnik::parameters;
|
|
|
|
|
|
|
|
DATASOURCE_PLUGIN(sqlite_datasource)
|
|
|
|
|
2009-12-16 20:02:06 +00:00
|
|
|
using mapnik::box2d;
|
2009-02-09 19:43:57 +00:00
|
|
|
using mapnik::coord2d;
|
|
|
|
using mapnik::query;
|
|
|
|
using mapnik::featureset_ptr;
|
|
|
|
using mapnik::layer_descriptor;
|
|
|
|
using mapnik::attribute_descriptor;
|
|
|
|
using mapnik::datasource_exception;
|
|
|
|
|
|
|
|
|
2010-10-24 06:34:18 +00:00
|
|
|
sqlite_datasource::sqlite_datasource(parameters const& params, bool bind)
|
2009-02-09 19:43:57 +00:00
|
|
|
: datasource(params),
|
|
|
|
extent_(),
|
|
|
|
extent_initialized_(false),
|
|
|
|
type_(datasource::Vector),
|
2011-03-17 23:50:16 +00:00
|
|
|
table_(*params_.get<std::string>("table","")),
|
|
|
|
fields_(*params_.get<std::string>("fields","*")),
|
|
|
|
metadata_(*params_.get<std::string>("metadata","")),
|
|
|
|
geometry_table_(*params_.get<std::string>("geometry_table","")),
|
2011-04-04 03:56:15 +00:00
|
|
|
geometry_field_(*params_.get<std::string>("geometry_field","")),
|
2011-03-31 22:32:22 +00:00
|
|
|
// http://www.sqlite.org/lang_createtable.html#rowid
|
|
|
|
key_field_(*params_.get<std::string>("key_field","rowid")),
|
2009-02-23 15:00:25 +00:00
|
|
|
row_offset_(*params_.get<int>("row_offset",0)),
|
|
|
|
row_limit_(*params_.get<int>("row_limit",0)),
|
2011-03-17 23:50:16 +00:00
|
|
|
desc_(*params_.get<std::string>("type"), *params_.get<std::string>("encoding","utf-8")),
|
2010-05-30 03:23:59 +00:00
|
|
|
format_(mapnik::wkbGeneric)
|
2009-02-09 19:43:57 +00:00
|
|
|
{
|
2011-04-04 03:56:15 +00:00
|
|
|
// TODO
|
|
|
|
// - change param from 'file' to 'dbname'
|
|
|
|
// - be able to calculate extent manually - like ogr?
|
|
|
|
// - require wkb_format?
|
|
|
|
// - split plugin into sqlite vs spatialite?
|
|
|
|
// - conversion/sqlite initialization tool
|
|
|
|
// - sync code with rasterlite
|
|
|
|
|
2011-03-17 23:50:16 +00:00
|
|
|
boost::optional<std::string> file = params_.get<std::string>("file");
|
2010-11-18 23:36:21 +00:00
|
|
|
if (!file) throw datasource_exception("Sqlite Plugin: missing <file> parameter");
|
2009-02-09 19:43:57 +00:00
|
|
|
|
2011-03-31 22:32:22 +00:00
|
|
|
if (table_.empty()) throw mapnik::datasource_exception("Sqlite Plugin: missing <table> parameter");
|
2011-04-04 03:56:15 +00:00
|
|
|
|
2011-03-17 23:50:16 +00:00
|
|
|
boost::optional<std::string> wkb = params_.get<std::string>("wkb_format");
|
2009-02-23 15:00:25 +00:00
|
|
|
if (wkb)
|
|
|
|
{
|
|
|
|
if (*wkb == "spatialite")
|
|
|
|
format_ = mapnik::wkbSpatiaLite;
|
|
|
|
}
|
|
|
|
|
2009-02-09 19:43:57 +00:00
|
|
|
multiple_geometries_ = *params_.get<mapnik::boolean>("multiple_geometries",false);
|
2009-02-10 19:09:16 +00:00
|
|
|
use_spatial_index_ = *params_.get<mapnik::boolean>("use_spatial_index",true);
|
2009-02-09 19:43:57 +00:00
|
|
|
|
2010-11-14 14:51:04 +00:00
|
|
|
boost::optional<std::string> ext = params_.get<std::string>("extent");
|
|
|
|
if (ext) extent_initialized_ = extent_.from_string(*ext);
|
|
|
|
|
2011-03-17 23:50:16 +00:00
|
|
|
boost::optional<std::string> base = params_.get<std::string>("base");
|
2009-03-30 20:19:57 +00:00
|
|
|
if (base)
|
|
|
|
dataset_name_ = *base + "/" + *file;
|
|
|
|
else
|
|
|
|
dataset_name_ = *file;
|
|
|
|
|
2010-10-24 06:34:18 +00:00
|
|
|
if (bind)
|
|
|
|
{
|
|
|
|
this->bind();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sqlite_datasource::bind() const
|
|
|
|
{
|
|
|
|
if (is_bound_) return;
|
|
|
|
|
2010-11-18 23:36:21 +00:00
|
|
|
if (!boost::filesystem::exists(dataset_name_))
|
|
|
|
throw datasource_exception("Sqlite Plugin: " + dataset_name_ + " does not exist");
|
2009-03-30 20:19:57 +00:00
|
|
|
|
|
|
|
dataset_ = new sqlite_connection (dataset_name_);
|
2009-02-09 19:43:57 +00:00
|
|
|
|
2011-03-17 23:50:16 +00:00
|
|
|
if(geometry_table_.empty())
|
|
|
|
{
|
|
|
|
geometry_table_ = mapnik::table_from_sql(table_);
|
|
|
|
}
|
2009-02-23 22:13:21 +00:00
|
|
|
|
2011-03-31 22:32:22 +00:00
|
|
|
|
2011-04-04 03:56:15 +00:00
|
|
|
// should we deduce column names and types using PRAGMA?
|
|
|
|
bool use_pragma_table_info = true;
|
|
|
|
|
|
|
|
if (table_ != geometry_table_)
|
|
|
|
{
|
|
|
|
// if 'table_' is a subquery then we try to deduce names
|
|
|
|
// and types from the first row returned from that query
|
|
|
|
use_pragma_table_info = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!use_pragma_table_info)
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "SELECT " << fields_ << " FROM (" << table_ << ") LIMIT 1";
|
|
|
|
|
|
|
|
boost::scoped_ptr<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
|
|
|
if (rs->is_valid () && rs->step_next())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < rs->column_count (); ++i)
|
|
|
|
{
|
|
|
|
const int type_oid = rs->column_type (i);
|
|
|
|
const char* fld_name = rs->column_name (i);
|
|
|
|
switch (type_oid)
|
|
|
|
{
|
|
|
|
case SQLITE_INTEGER:
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Integer));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SQLITE_FLOAT:
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Double));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SQLITE_TEXT:
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::String));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SQLITE_NULL:
|
|
|
|
// sqlite reports based on value, not actual column type unless
|
|
|
|
// PRAGMA table_info is used so here we assume the column is a string
|
|
|
|
// which is a lesser evil than altogether dropping the column
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::String));
|
|
|
|
|
|
|
|
case SQLITE_BLOB:
|
|
|
|
if (geometry_field_.empty() &&
|
|
|
|
(boost::algorithm::icontains(fld_name,"geom") ||
|
|
|
|
boost::algorithm::icontains(fld_name,"point") ||
|
|
|
|
boost::algorithm::icontains(fld_name,"linestring") ||
|
|
|
|
boost::algorithm::icontains(fld_name,"polygon"))
|
|
|
|
)
|
|
|
|
geometry_field_ = std::string(fld_name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "Sqlite Plugin: unknown type_oid=" << type_oid << std::endl;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if we do not have at least a row and
|
|
|
|
// we cannot determine the right columns types and names
|
|
|
|
// as all column_type are SQLITE_NULL
|
|
|
|
// so we fallback to using PRAGMA table_info
|
|
|
|
use_pragma_table_info = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO - ensure that the supplied key_field is a valid "integer primary key"
|
|
|
|
desc_.add_descriptor(attribute_descriptor("rowid",mapnik::Integer));
|
|
|
|
|
|
|
|
if (use_pragma_table_info)
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "PRAGMA table_info(" << geometry_table_ << ")";
|
|
|
|
boost::scoped_ptr<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
|
|
|
bool found_table = false;
|
|
|
|
while (rs->is_valid () && rs->step_next())
|
|
|
|
{
|
|
|
|
found_table = true;
|
|
|
|
const char* fld_name = rs->column_text(1);
|
|
|
|
std::string fld_type(rs->column_text(2));
|
|
|
|
boost::algorithm::to_lower(fld_type);
|
|
|
|
|
|
|
|
// see 2.1 "Column Affinity" at http://www.sqlite.org/datatype3.html
|
|
|
|
if (geometry_field_.empty() &&
|
|
|
|
(
|
|
|
|
(boost::algorithm::icontains(fld_name,"geom") ||
|
|
|
|
boost::algorithm::icontains(fld_name,"point") ||
|
|
|
|
boost::algorithm::icontains(fld_name,"linestring") ||
|
|
|
|
boost::algorithm::icontains(fld_name,"polygon"))
|
|
|
|
||
|
|
|
|
(boost::algorithm::contains(fld_type,"geom") ||
|
|
|
|
boost::algorithm::contains(fld_type,"point") ||
|
|
|
|
boost::algorithm::contains(fld_type,"linestring") ||
|
|
|
|
boost::algorithm::contains(fld_type,"polygon"))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
geometry_field_ = std::string(fld_name);
|
|
|
|
else if (boost::algorithm::contains(fld_type,"int"))
|
|
|
|
{
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Integer));
|
|
|
|
}
|
|
|
|
else if (boost::algorithm::contains(fld_type,"text") ||
|
|
|
|
boost::algorithm::contains(fld_type,"char") ||
|
|
|
|
boost::algorithm::contains(fld_type,"clob"))
|
|
|
|
{
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::String));
|
|
|
|
}
|
|
|
|
else if (boost::algorithm::contains(fld_type,"real") ||
|
|
|
|
boost::algorithm::contains(fld_type,"float") ||
|
|
|
|
boost::algorithm::contains(fld_type,"double"))
|
|
|
|
{
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Double));
|
|
|
|
}
|
|
|
|
else if (boost::algorithm::contains(fld_type,"blob") && !geometry_field_.empty())
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::String));
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// "Column Affinity" says default to "Numeric" but for now we pass..
|
|
|
|
//desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Double));
|
|
|
|
std::clog << "Sqlite Plugin: column '" << std::string(fld_name) << "' unhandled due to unknown type: " << fld_type << std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (!found_table)
|
|
|
|
{
|
|
|
|
throw datasource_exception("Sqlite Plugin: could not query table '" + geometry_table_ + "' using pragma table_info(" + geometry_table_ + ");");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (geometry_field_.empty()) {
|
|
|
|
throw datasource_exception("Sqlite Plugin: cannot detect geometry_field, please supply the name of the geometry_field to use.");
|
|
|
|
}
|
|
|
|
|
2011-03-31 22:32:22 +00:00
|
|
|
if (use_spatial_index_)
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "SELECT COUNT (*) FROM sqlite_master";
|
|
|
|
s << " WHERE LOWER(name) = LOWER('idx_" << geometry_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!use_spatial_index_)
|
|
|
|
std::clog << "Sqlite Plugin: spatial index not found for table '"
|
|
|
|
<< geometry_table_ << "'" << std::endl;
|
|
|
|
}
|
|
|
|
|
2011-03-17 23:50:16 +00:00
|
|
|
if (metadata_ != "" && !extent_initialized_)
|
2009-02-09 19:43:57 +00:00
|
|
|
{
|
|
|
|
std::ostringstream s;
|
2010-11-18 23:36:21 +00:00
|
|
|
s << "SELECT xmin, ymin, xmax, ymax FROM " << metadata_;
|
2011-03-17 23:50:16 +00:00
|
|
|
s << " WHERE LOWER(f_table_name) = LOWER('" << geometry_table_ << "')";
|
2009-02-10 19:09:16 +00:00
|
|
|
boost::scoped_ptr<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
2009-02-09 19:43:57 +00:00
|
|
|
if (rs->is_valid () && rs->step_next())
|
|
|
|
{
|
2009-02-10 19:09:16 +00:00
|
|
|
double xmin = rs->column_double (0);
|
|
|
|
double ymin = rs->column_double (1);
|
|
|
|
double xmax = rs->column_double (2);
|
|
|
|
double ymax = rs->column_double (3);
|
2009-02-23 15:00:25 +00:00
|
|
|
|
2009-02-10 19:09:16 +00:00
|
|
|
extent_.init (xmin,ymin,xmax,ymax);
|
|
|
|
extent_initialized_ = true;
|
2009-02-09 19:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-31 22:32:22 +00:00
|
|
|
|
|
|
|
if (!extent_initialized_ && use_spatial_index_)
|
2009-02-10 19:09:16 +00:00
|
|
|
{
|
2009-02-23 16:04:57 +00:00
|
|
|
std::ostringstream s;
|
2011-04-01 04:35:56 +00:00
|
|
|
s << "SELECT MIN(xmin), MIN(ymin), MAX(xmax), MAX(ymax) FROM "
|
2011-03-31 22:32:22 +00:00
|
|
|
<< "idx_" << geometry_table_ << "_" << geometry_field_;
|
2009-02-23 16:04:57 +00:00
|
|
|
boost::scoped_ptr<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
|
|
|
if (rs->is_valid () && rs->step_next())
|
2009-02-10 19:09:16 +00:00
|
|
|
{
|
2011-03-31 22:32:22 +00:00
|
|
|
double xmin = rs->column_double (0);
|
|
|
|
double ymin = rs->column_double (1);
|
|
|
|
double xmax = rs->column_double (2);
|
|
|
|
double ymax = rs->column_double (3);
|
2009-02-23 15:00:25 +00:00
|
|
|
|
2011-03-31 22:32:22 +00:00
|
|
|
extent_.init (xmin,ymin,xmax,ymax);
|
|
|
|
extent_initialized_ = true;
|
|
|
|
}
|
2009-02-10 19:09:16 +00:00
|
|
|
}
|
2009-02-23 22:13:21 +00:00
|
|
|
|
2011-04-04 03:56:15 +00:00
|
|
|
if (!extent_initialized_) {
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "Sqlite Plugin: extent could not be determined for table|geometry '"
|
|
|
|
<< geometry_table_ << "|" << geometry_field_ << "'"
|
|
|
|
<< " because an rtree spatial index is missing."
|
|
|
|
<< " - either set the table 'extent' or create an rtree spatial index";
|
|
|
|
throw datasource_exception(s.str());
|
2009-02-09 19:43:57 +00:00
|
|
|
}
|
2010-10-24 06:34:18 +00:00
|
|
|
|
|
|
|
is_bound_ = true;
|
2009-02-09 19:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sqlite_datasource::~sqlite_datasource()
|
|
|
|
{
|
2010-10-24 06:34:18 +00:00
|
|
|
if (is_bound_)
|
|
|
|
{
|
|
|
|
delete dataset_;
|
|
|
|
}
|
2009-02-09 19:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string sqlite_datasource::name()
|
|
|
|
{
|
2010-11-14 14:51:04 +00:00
|
|
|
return "sqlite";
|
2009-02-09 19:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sqlite_datasource::type() const
|
|
|
|
{
|
|
|
|
return type_;
|
|
|
|
}
|
|
|
|
|
2009-12-16 20:02:06 +00:00
|
|
|
box2d<double> sqlite_datasource::envelope() const
|
2009-02-09 19:43:57 +00:00
|
|
|
{
|
2010-10-24 06:34:18 +00:00
|
|
|
if (!is_bound_) bind();
|
2009-02-09 19:43:57 +00:00
|
|
|
return extent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
layer_descriptor sqlite_datasource::get_descriptor() const
|
|
|
|
{
|
2010-10-24 06:34:18 +00:00
|
|
|
if (!is_bound_) bind();
|
2009-02-09 19:43:57 +00:00
|
|
|
return desc_;
|
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr sqlite_datasource::features(query const& q) const
|
|
|
|
{
|
2010-10-24 06:34:18 +00:00
|
|
|
if (!is_bound_) bind();
|
2009-02-09 19:43:57 +00:00
|
|
|
if (dataset_)
|
|
|
|
{
|
2009-12-16 20:02:06 +00:00
|
|
|
mapnik::box2d<double> const& e = q.get_bbox();
|
2009-02-09 19:43:57 +00:00
|
|
|
|
|
|
|
std::ostringstream s;
|
2009-02-23 22:13:21 +00:00
|
|
|
|
2010-11-18 23:36:21 +00:00
|
|
|
s << "SELECT " << geometry_field_ << "," << key_field_;
|
2009-02-09 19:43:57 +00:00
|
|
|
std::set<std::string> const& props = q.property_names();
|
|
|
|
std::set<std::string>::const_iterator pos = props.begin();
|
|
|
|
std::set<std::string>::const_iterator end = props.end();
|
|
|
|
while (pos != end)
|
|
|
|
{
|
2011-03-31 22:32:22 +00:00
|
|
|
s << ",\"" << *pos << "\"";
|
|
|
|
++pos;
|
|
|
|
}
|
2009-02-23 22:13:21 +00:00
|
|
|
|
2010-11-18 23:36:21 +00:00
|
|
|
s << " FROM ";
|
2009-02-23 22:13:21 +00:00
|
|
|
|
|
|
|
std::string query (table_);
|
|
|
|
|
2009-02-10 19:09:16 +00:00
|
|
|
if (use_spatial_index_)
|
|
|
|
{
|
2009-02-23 22:13:21 +00:00
|
|
|
std::ostringstream spatial_sql;
|
|
|
|
spatial_sql << std::setprecision(16);
|
2011-03-31 22:32:22 +00:00
|
|
|
spatial_sql << " WHERE " << key_field_ << " IN (SELECT pkid FROM idx_" << geometry_table_ << "_" << geometry_field_;
|
|
|
|
spatial_sql << " WHERE xmax>=" << e.minx() << " AND xmin<=" << e.maxx() ;
|
|
|
|
spatial_sql << " AND ymax>=" << e.miny() << " AND ymin<=" << e.maxy() << ")";
|
2010-11-18 23:36:21 +00:00
|
|
|
if (boost::algorithm::ifind_first(query, "WHERE"))
|
2009-02-23 22:13:21 +00:00
|
|
|
{
|
2010-11-18 23:36:21 +00:00
|
|
|
boost::algorithm::ireplace_first(query, "WHERE", spatial_sql.str() + " AND ");
|
2009-02-23 22:13:21 +00:00
|
|
|
}
|
2011-03-17 23:50:16 +00:00
|
|
|
else if (boost::algorithm::ifind_first(query, geometry_table_))
|
2009-02-23 22:13:21 +00:00
|
|
|
{
|
2011-03-17 23:50:16 +00:00
|
|
|
boost::algorithm::ireplace_first(query, table_, table_ + " " + spatial_sql.str());
|
2009-02-23 22:13:21 +00:00
|
|
|
}
|
2009-02-10 19:09:16 +00:00
|
|
|
}
|
2009-02-23 22:13:21 +00:00
|
|
|
|
|
|
|
s << query ;
|
|
|
|
|
2009-02-23 15:00:25 +00:00
|
|
|
if (row_limit_ > 0) {
|
2010-11-18 23:36:21 +00:00
|
|
|
s << " LIMIT " << row_limit_;
|
2009-02-23 15:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (row_offset_ > 0) {
|
2010-11-18 23:36:21 +00:00
|
|
|
s << " OFFSET " << row_offset_;
|
2009-02-23 15:00:25 +00:00
|
|
|
}
|
|
|
|
|
2009-02-23 22:13:21 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-18 23:36:21 +00:00
|
|
|
std::clog << "Sqlite Plugin: " << s.str() << std::endl;
|
2009-02-23 22:13:21 +00:00
|
|
|
#endif
|
2009-02-10 19:09:16 +00:00
|
|
|
|
|
|
|
boost::shared_ptr<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
2009-02-09 19:43:57 +00:00
|
|
|
|
2009-02-23 15:00:25 +00:00
|
|
|
return featureset_ptr (new sqlite_featureset(rs, desc_.get_encoding(), format_, multiple_geometries_));
|
2009-02-09 19:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return featureset_ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr sqlite_datasource::features_at_point(coord2d const& pt) const
|
|
|
|
{
|
2010-10-24 06:34:18 +00:00
|
|
|
if (!is_bound_) bind();
|
2009-02-09 19:43:57 +00:00
|
|
|
|
2011-04-02 04:54:26 +00:00
|
|
|
if (dataset_)
|
|
|
|
{
|
|
|
|
// TODO - need tolerance
|
|
|
|
mapnik::box2d<double> const e(pt.x,pt.y,pt.x,pt.y);
|
|
|
|
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "SELECT " << geometry_field_ << "," << key_field_;
|
|
|
|
std::vector<attribute_descriptor>::const_iterator itr = desc_.get_descriptors().begin();
|
|
|
|
std::vector<attribute_descriptor>::const_iterator end = desc_.get_descriptors().end();
|
|
|
|
while (itr != end)
|
|
|
|
{
|
|
|
|
std::string fld_name = itr->get_name();
|
|
|
|
if (fld_name != key_field_)
|
|
|
|
s << ",\"" << itr->get_name() << "\"";
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
s << " FROM ";
|
|
|
|
|
|
|
|
std::string query (table_);
|
|
|
|
|
|
|
|
if (use_spatial_index_)
|
|
|
|
{
|
|
|
|
std::ostringstream spatial_sql;
|
|
|
|
spatial_sql << std::setprecision(16);
|
|
|
|
spatial_sql << " WHERE " << key_field_ << " IN (SELECT pkid FROM idx_" << geometry_table_ << "_" << geometry_field_;
|
|
|
|
spatial_sql << " WHERE xmax>=" << e.minx() << " AND xmin<=" << e.maxx() ;
|
|
|
|
spatial_sql << " AND ymax>=" << e.miny() << " AND ymin<=" << e.maxy() << ")";
|
|
|
|
if (boost::algorithm::ifind_first(query, "WHERE"))
|
|
|
|
{
|
|
|
|
boost::algorithm::ireplace_first(query, "WHERE", spatial_sql.str() + " AND ");
|
|
|
|
}
|
|
|
|
else if (boost::algorithm::ifind_first(query, geometry_table_))
|
|
|
|
{
|
|
|
|
boost::algorithm::ireplace_first(query, table_, table_ + " " + spatial_sql.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s << query ;
|
|
|
|
|
|
|
|
if (row_limit_ > 0) {
|
|
|
|
s << " LIMIT " << row_limit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row_offset_ > 0) {
|
|
|
|
s << " OFFSET " << row_offset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "Sqlite Plugin: " << s.str() << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
boost::shared_ptr<sqlite_resultset> rs (dataset_->execute_query (s.str()));
|
|
|
|
|
|
|
|
return featureset_ptr (new sqlite_featureset(rs, desc_.get_encoding(), format_, multiple_geometries_));
|
|
|
|
}
|
|
|
|
|
2009-02-09 19:43:57 +00:00
|
|
|
return featureset_ptr();
|
|
|
|
}
|
|
|
|
|