2009-02-09 20:43:57 +01:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2009-02-09 20:43:57 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2009-02-09 20:43:57 +01: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "sqlite_datasource.hpp"
|
|
|
|
#include "sqlite_featureset.hpp"
|
2011-11-04 00:51:37 +01:00
|
|
|
#include "sqlite_resultset.hpp"
|
|
|
|
#include "sqlite_utils.hpp"
|
2009-02-09 20:43:57 +01:00
|
|
|
|
|
|
|
// mapnik
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2012-03-07 19:16:41 +01:00
|
|
|
#include <mapnik/boolean.hpp>
|
2010-11-12 01:12:47 +01:00
|
|
|
#include <mapnik/sql_utils.hpp>
|
2012-01-15 07:35:40 +01:00
|
|
|
#include <mapnik/util/geometry_to_ds_type.hpp>
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/timer.hpp>
|
2012-01-12 05:03:23 +01:00
|
|
|
#include <mapnik/wkb.hpp>
|
2013-01-04 03:02:21 +01:00
|
|
|
#include <mapnik/util/trim.hpp>
|
2013-06-03 04:28:24 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2016-08-02 15:43:02 +02:00
|
|
|
#include <mapnik/geometry/is_empty.hpp>
|
2011-08-13 04:52:46 +02:00
|
|
|
|
2009-02-09 20:43:57 +01:00
|
|
|
// boost
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
using mapnik::attribute_descriptor;
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::box2d;
|
2009-02-09 20:43:57 +01:00
|
|
|
using mapnik::coord2d;
|
2022-01-26 10:43:31 +01:00
|
|
|
using mapnik::datasource;
|
|
|
|
using mapnik::datasource_exception;
|
2009-02-09 20:43:57 +01:00
|
|
|
using mapnik::featureset_ptr;
|
|
|
|
using mapnik::layer_descriptor;
|
2011-10-22 14:50:24 +02:00
|
|
|
using mapnik::parameters;
|
2022-01-26 10:43:31 +01:00
|
|
|
using mapnik::query;
|
2009-02-09 20:43:57 +01:00
|
|
|
|
2022-01-29 12:52:23 +01:00
|
|
|
DATASOURCE_PLUGIN_IMPL(sqlite_datasource_plugin, sqlite_datasource);
|
|
|
|
DATASOURCE_PLUGIN_EXPORT(sqlite_datasource_plugin);
|
2022-02-02 17:20:29 +01:00
|
|
|
DATASOURCE_PLUGIN_EMPTY_AFTER_LOAD(sqlite_datasource_plugin);
|
|
|
|
DATASOURCE_PLUGIN_EMPTY_BEFORE_UNLOAD(sqlite_datasource_plugin);
|
2009-02-09 20:43:57 +01:00
|
|
|
|
2012-12-17 19:03:07 +01:00
|
|
|
sqlite_datasource::sqlite_datasource(parameters const& params)
|
2022-01-26 10:43:31 +01:00
|
|
|
: datasource(params)
|
|
|
|
, extent_()
|
|
|
|
, extent_initialized_(false)
|
|
|
|
, type_(datasource::Vector)
|
|
|
|
, 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", ""))
|
|
|
|
, geometry_field_(*params.get<std::string>("geometry_field", ""))
|
|
|
|
, index_table_(*params.get<std::string>("index_table", ""))
|
|
|
|
, key_field_(*params.get<std::string>("key_field", ""))
|
|
|
|
, row_offset_(*params.get<mapnik::value_integer>("row_offset", 0))
|
|
|
|
, row_limit_(*params.get<mapnik::value_integer>("row_limit", 0))
|
|
|
|
, intersects_token_("!intersects!")
|
|
|
|
, pixel_width_token_("!pixel_width!")
|
|
|
|
, pixel_height_token_("!pixel_height!")
|
|
|
|
, desc_(sqlite_datasource::name(), *params.get<std::string>("encoding", "utf-8"))
|
|
|
|
, format_(mapnik::wkbAuto)
|
|
|
|
, twkb_encoding_(false)
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
2011-10-29 06:24:47 +02:00
|
|
|
/* TODO
|
2011-11-14 04:37:50 +01:00
|
|
|
- throw if no primary key but spatial index is present?
|
|
|
|
- remove auto-indexing
|
2012-01-12 05:03:23 +01:00
|
|
|
- if spatialite - leverage more of the metadata for geometry type detection
|
2011-10-29 06:24:47 +02:00
|
|
|
*/
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
2012-12-17 19:03:07 +01:00
|
|
|
mapnik::progress_timer __stats__(std::clog, "sqlite_datasource::init");
|
2012-04-08 02:20:56 +02:00
|
|
|
#endif
|
|
|
|
|
2024-04-19 20:45:24 +02:00
|
|
|
const auto file = params.get<std::string>("file");
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!file)
|
|
|
|
throw datasource_exception("Sqlite Plugin: missing <file> parameter");
|
2011-10-18 23:32:25 +02:00
|
|
|
|
2024-04-19 20:45:24 +02:00
|
|
|
const auto base = params.get<std::string>("base");
|
2011-10-29 06:24:47 +02:00
|
|
|
if (base)
|
|
|
|
dataset_name_ = *base + "/" + *file;
|
|
|
|
else
|
|
|
|
dataset_name_ = *file;
|
|
|
|
|
2013-06-03 04:28:24 +02:00
|
|
|
if ((dataset_name_.compare(":memory:") != 0) && (!mapnik::util::exists(dataset_name_)))
|
2011-10-29 06:24:47 +02:00
|
|
|
{
|
|
|
|
throw datasource_exception("Sqlite Plugin: " + dataset_name_ + " does not exist");
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
2011-11-04 00:51:37 +01:00
|
|
|
|
2014-07-29 04:46:49 +02:00
|
|
|
use_spatial_index_ = *params.get<mapnik::boolean_type>("use_spatial_index", true);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-11-04 00:51:37 +01:00
|
|
|
// TODO - remove this option once all datasources have an indexing api
|
2014-07-29 04:46:49 +02:00
|
|
|
bool auto_index = *params.get<mapnik::boolean_type>("auto_index", true);
|
2011-11-04 00:51:37 +01:00
|
|
|
|
2024-04-19 20:45:24 +02:00
|
|
|
const auto ext = params.get<std::string>("extent");
|
|
|
|
if (ext.has_value())
|
2022-01-26 10:43:31 +01:00
|
|
|
extent_initialized_ = extent_.from_string(*ext);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2024-04-19 20:45:24 +02:00
|
|
|
const auto wkb = params.get<std::string>("wkb_format");
|
|
|
|
if (wkb.has_value())
|
2011-09-02 23:21:01 +02:00
|
|
|
{
|
|
|
|
if (*wkb == "spatialite")
|
2011-11-04 00:51:37 +01:00
|
|
|
{
|
2011-11-14 04:37:50 +01:00
|
|
|
format_ = mapnik::wkbSpatiaLite;
|
2011-11-04 00:51:37 +01:00
|
|
|
}
|
2011-10-18 22:19:03 +02:00
|
|
|
else if (*wkb == "generic")
|
2011-11-04 00:51:37 +01:00
|
|
|
{
|
2011-10-18 22:19:03 +02:00
|
|
|
format_ = mapnik::wkbGeneric;
|
2011-11-04 00:51:37 +01:00
|
|
|
}
|
2017-04-23 21:48:35 +02:00
|
|
|
else if (*wkb == "twkb")
|
|
|
|
{
|
|
|
|
format_ = mapnik::wkbGeneric;
|
|
|
|
twkb_encoding_ = true;
|
|
|
|
}
|
2011-10-18 22:19:03 +02:00
|
|
|
else
|
2011-11-04 00:51:37 +01:00
|
|
|
{
|
2011-10-18 22:19:03 +02:00
|
|
|
format_ = mapnik::wkbAuto;
|
2011-11-04 00:51:37 +01:00
|
|
|
}
|
2011-09-02 23:21:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate init_statements_
|
|
|
|
// 1. Build attach database statements from the "attachdb" parameter
|
|
|
|
// 2. Add explicit init statements from "initdb" parameter
|
|
|
|
// Note that we do some extra work to make sure that any attached
|
|
|
|
// databases are relative to directory containing dataset_name_. Sqlite
|
|
|
|
// will default to attaching from cwd. Typicaly usage means that the
|
|
|
|
// map loader will produce full paths here.
|
2024-04-19 20:45:24 +02:00
|
|
|
const auto attachdb = params.get<std::string>("attachdb");
|
|
|
|
if (attachdb.has_value())
|
2011-10-18 22:19:03 +02:00
|
|
|
{
|
2011-10-22 14:50:24 +02:00
|
|
|
parse_attachdb(*attachdb);
|
2011-09-02 23:21:01 +02:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2024-04-19 20:45:24 +02:00
|
|
|
const auto initdb = params.get<std::string>("initdb");
|
|
|
|
if (initdb.has_value())
|
2011-10-18 22:19:03 +02:00
|
|
|
{
|
2011-10-22 14:50:24 +02:00
|
|
|
init_statements_.push_back(*initdb);
|
2011-09-02 23:21:01 +02:00
|
|
|
}
|
|
|
|
|
2012-01-12 05:03:23 +01:00
|
|
|
// now actually create the connection and start executing setup sql
|
2013-09-20 15:00:11 +02:00
|
|
|
dataset_ = std::make_shared<sqlite_connection>(dataset_name_);
|
2012-01-12 05:03:23 +01:00
|
|
|
|
2024-04-19 20:45:24 +02:00
|
|
|
const auto table_by_index = params.get<mapnik::value_integer>("table_by_index");
|
2012-01-12 05:03:23 +01:00
|
|
|
|
|
|
|
int passed_parameters = 0;
|
2012-12-17 19:03:07 +01:00
|
|
|
passed_parameters += params.get<std::string>("table") ? 1 : 0;
|
2024-04-19 20:45:24 +02:00
|
|
|
passed_parameters += table_by_index.has_value() ? 1 : 0;
|
2012-01-12 05:03:23 +01:00
|
|
|
|
|
|
|
if (passed_parameters > 1)
|
|
|
|
{
|
|
|
|
throw datasource_exception("SQLite Plugin: you can only select an by name "
|
|
|
|
"('table' parameter), by number ('table_by_index' parameter), "
|
2022-01-26 10:43:31 +01:00
|
|
|
"do not supply 2 or more of them at the same time");
|
2012-01-12 05:03:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (table_by_index)
|
|
|
|
{
|
|
|
|
std::vector<std::string> tables;
|
2022-01-26 10:43:31 +01:00
|
|
|
sqlite_utils::get_tables(dataset_, tables);
|
2013-01-21 23:13:24 +01:00
|
|
|
if (*table_by_index < 0 || *table_by_index >= static_cast<int>(tables.size()))
|
2012-01-12 05:03:23 +01:00
|
|
|
{
|
|
|
|
std::ostringstream s;
|
2022-01-26 10:43:31 +01:00
|
|
|
s << "SQLite Plugin: only " << tables.size() << " table(s) exist, cannot find table by index '"
|
|
|
|
<< *table_by_index << "'";
|
2012-01-12 05:03:23 +01:00
|
|
|
|
|
|
|
throw datasource_exception(s.str());
|
|
|
|
}
|
|
|
|
table_ = tables[*table_by_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (table_.empty())
|
|
|
|
{
|
|
|
|
throw mapnik::datasource_exception("Sqlite Plugin: missing <table> parameter");
|
|
|
|
}
|
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
if (geometry_table_.empty())
|
2011-03-18 00:50:16 +01:00
|
|
|
{
|
2011-11-04 00:51:37 +01:00
|
|
|
geometry_table_ = mapnik::sql_utils::table_from_sql(table_);
|
2011-03-18 00:50:16 +01:00
|
|
|
}
|
2011-04-01 00:32:22 +02:00
|
|
|
|
2011-10-29 06:24:47 +02:00
|
|
|
// if 'table_' is a subquery then we try to deduce names
|
|
|
|
// and types from the first row returned from that query
|
|
|
|
using_subquery_ = false;
|
2011-04-04 05:56:15 +02:00
|
|
|
if (table_ != geometry_table_)
|
|
|
|
{
|
2011-08-17 22:39:07 +02:00
|
|
|
using_subquery_ = true;
|
2011-04-04 05:56:15 +02:00
|
|
|
}
|
2011-11-10 18:42:04 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// attempt to auto-quote table if needed
|
|
|
|
if (sqlite_utils::needs_quoting(table_))
|
|
|
|
{
|
|
|
|
table_ = std::string("[") + table_ + "]";
|
|
|
|
geometry_table_ = table_;
|
|
|
|
}
|
|
|
|
}
|
2011-04-04 05:56:15 +02:00
|
|
|
|
2011-10-29 06:24:47 +02:00
|
|
|
// Execute init_statements_
|
2022-01-26 10:43:31 +01:00
|
|
|
for (std::vector<std::string>::const_iterator iter = init_statements_.begin(); iter != init_statements_.end();
|
|
|
|
++iter)
|
2011-10-29 06:24:47 +02:00
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(sqlite) << "sqlite_datasource: Execute init sql=" << *iter;
|
2012-04-09 12:05:49 +02:00
|
|
|
|
2011-10-29 06:24:47 +02:00
|
|
|
dataset_->execute(*iter);
|
2011-04-04 05:56:15 +02:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-29 06:24:47 +02:00
|
|
|
bool found_types_via_subquery = false;
|
|
|
|
if (using_subquery_)
|
2011-10-18 23:51:01 +02:00
|
|
|
{
|
2011-10-29 06:24:47 +02:00
|
|
|
std::ostringstream s;
|
2018-08-31 09:11:44 +02:00
|
|
|
std::string query = populate_tokens(table_, 0, 0);
|
2011-12-16 19:05:54 +01:00
|
|
|
s << "SELECT " << fields_ << " FROM (" << query << ") LIMIT 1";
|
2022-01-26 10:43:31 +01:00
|
|
|
found_types_via_subquery = sqlite_utils::detect_types_from_subquery(s.str(), geometry_field_, desc_, dataset_);
|
2011-10-18 23:51:01 +02:00
|
|
|
}
|
2011-10-29 06:24:47 +02:00
|
|
|
|
|
|
|
// TODO - consider removing this
|
|
|
|
if (key_field_ == "rowid")
|
2011-10-18 23:51:01 +02:00
|
|
|
{
|
2011-10-29 06:24:47 +02:00
|
|
|
desc_.add_descriptor(attribute_descriptor("rowid", mapnik::Integer));
|
2011-10-18 23:51:01 +02:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
bool found_table =
|
|
|
|
sqlite_utils::table_info(key_field_, found_types_via_subquery, geometry_field_, geometry_table_, desc_, dataset_);
|
2011-10-29 06:24:47 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!found_table)
|
2011-04-04 05:56:15 +02:00
|
|
|
{
|
|
|
|
std::ostringstream s;
|
2011-11-10 04:18:12 +01:00
|
|
|
s << "Sqlite Plugin: could not query table '" << geometry_table_ << "'";
|
|
|
|
if (using_subquery_)
|
|
|
|
{
|
|
|
|
s << " from subquery '" << table_ << "'";
|
|
|
|
}
|
2011-10-18 22:19:03 +02:00
|
|
|
|
2011-11-10 04:18:12 +01:00
|
|
|
// report get available tables
|
|
|
|
std::vector<std::string> tables;
|
2022-01-26 10:43:31 +01:00
|
|
|
sqlite_utils::get_tables(dataset_, tables);
|
2011-11-10 04:18:12 +01:00
|
|
|
if (tables.size() > 0)
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
s << " (available tables for " << dataset_name_ << " are: '" << boost::algorithm::join(tables, ", ")
|
|
|
|
<< "')";
|
2011-11-10 04:18:12 +01:00
|
|
|
}
|
2011-04-04 05:56:15 +02:00
|
|
|
|
2011-10-29 06:24:47 +02:00
|
|
|
throw datasource_exception(s.str());
|
2011-04-04 05:56:15 +02:00
|
|
|
}
|
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
if (geometry_field_.empty())
|
|
|
|
{
|
2011-12-16 16:48:02 +01:00
|
|
|
std::ostringstream s;
|
|
|
|
s << "Sqlite Plugin: unable to detect the column "
|
|
|
|
<< "containing a valid geometry on table '" << geometry_table_ << "'. "
|
|
|
|
<< "Please provide a column name by passing the 'geometry_field' option "
|
|
|
|
<< "or indicate a different spatial table to use by passing the 'geometry_table' option";
|
|
|
|
throw datasource_exception(s.str());
|
2011-04-04 05:56:15 +02:00
|
|
|
}
|
2011-08-15 22:02:57 +02:00
|
|
|
|
2011-10-18 22:53:47 +02:00
|
|
|
if (index_table_.empty())
|
2011-10-18 22:19:03 +02:00
|
|
|
{
|
2011-08-15 22:02:57 +02:00
|
|
|
// Generate implicit index_table name - need to do this after
|
2011-09-02 15:50:40 +02:00
|
|
|
// we have discovered meta-data or else we don't know the column name
|
2022-01-26 10:43:31 +01:00
|
|
|
index_table_ = sqlite_utils::index_for_table(geometry_table_, geometry_field_);
|
2011-08-15 22:02:57 +02:00
|
|
|
}
|
2011-11-04 00:51:37 +01:00
|
|
|
|
|
|
|
std::string index_db = sqlite_utils::index_for_db(dataset_name_);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-29 06:24:47 +02:00
|
|
|
has_spatial_index_ = false;
|
2011-04-01 00:32:22 +02:00
|
|
|
if (use_spatial_index_)
|
|
|
|
{
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
2012-12-17 19:03:07 +01:00
|
|
|
mapnik::progress_timer __stats2__(std::clog, "sqlite_datasource::init(use_spatial_index)");
|
2012-04-08 02:20:56 +02:00
|
|
|
#endif
|
|
|
|
|
2014-10-01 14:17:46 +02:00
|
|
|
bool index_db_attached = false;
|
2013-06-03 04:28:24 +02:00
|
|
|
if (mapnik::util::exists(index_db))
|
2011-08-15 22:02:57 +02:00
|
|
|
{
|
2011-10-29 06:24:47 +02:00
|
|
|
dataset_->execute("attach database '" + index_db + "' as " + index_table_);
|
2014-10-01 14:17:46 +02:00
|
|
|
index_db_attached = true;
|
2011-08-15 22:02:57 +02:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
has_spatial_index_ = sqlite_utils::has_rtree(index_table_, dataset_);
|
2011-11-04 00:51:37 +01:00
|
|
|
|
2011-11-14 01:02:27 +01:00
|
|
|
if (!has_spatial_index_ && auto_index)
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!key_field_.empty())
|
2011-11-04 00:51:37 +01:00
|
|
|
{
|
|
|
|
std::ostringstream query;
|
2022-01-26 10:43:31 +01:00
|
|
|
query << "SELECT " << geometry_field_ << "," << key_field_ << " FROM (" << geometry_table_ << ")";
|
2011-11-21 01:42:28 +01:00
|
|
|
|
2013-07-03 16:54:42 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats2__(std::clog, "sqlite_datasource::init(create_spatial_index)");
|
|
|
|
#endif
|
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
std::shared_ptr<sqlite_resultset> rs = dataset_->execute_query(query.str());
|
2022-01-26 10:43:31 +01:00
|
|
|
if (sqlite_utils::create_spatial_index(index_db, index_table_, rs))
|
2011-11-10 04:18:12 +01:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
// extent_initialized_ = true;
|
2011-11-14 01:02:27 +01:00
|
|
|
has_spatial_index_ = true;
|
2014-10-01 14:17:46 +02:00
|
|
|
if (!index_db_attached && mapnik::util::exists(index_db))
|
2011-11-14 01:02:27 +01:00
|
|
|
{
|
|
|
|
dataset_->execute("attach database '" + index_db + "' as " + index_table_);
|
|
|
|
}
|
2011-11-10 04:18:12 +01:00
|
|
|
}
|
2011-11-04 00:51:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
2012-09-15 01:10:51 +02:00
|
|
|
s << "Sqlite Plugin: could not generate spatial index"
|
|
|
|
<< " for table '" << geometry_table_ << "'"
|
|
|
|
<< " as no primary key can be detected."
|
|
|
|
<< " You should either declare an INTEGER PRIMARY KEY"
|
|
|
|
<< " or set the 'key_field' option to force a"
|
|
|
|
<< " given field to be used as the primary key";
|
2011-11-04 00:51:37 +01:00
|
|
|
throw datasource_exception(s.str());
|
|
|
|
}
|
2011-11-14 01:02:27 +01:00
|
|
|
}
|
2011-04-01 00:32:22 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!extent_initialized_)
|
2011-10-18 22:19:03 +02:00
|
|
|
{
|
2013-07-03 16:54:42 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats2__(std::clog, "sqlite_datasource::init(detect_extent)");
|
|
|
|
#endif
|
2011-11-04 00:51:37 +01:00
|
|
|
// TODO - clean this up - reducing arguments
|
2018-08-31 09:11:44 +02:00
|
|
|
std::string query = populate_tokens(table_, 0, 0);
|
2011-10-29 06:24:47 +02:00
|
|
|
if (!sqlite_utils::detect_extent(dataset_,
|
|
|
|
has_spatial_index_,
|
|
|
|
extent_,
|
|
|
|
index_table_,
|
|
|
|
metadata_,
|
|
|
|
geometry_field_,
|
|
|
|
geometry_table_,
|
|
|
|
key_field_,
|
2011-12-16 19:05:54 +01:00
|
|
|
query))
|
2011-10-18 22:19:03 +02:00
|
|
|
{
|
2011-10-29 06:24:47 +02:00
|
|
|
std::ostringstream s;
|
2022-01-26 10:43:31 +01:00
|
|
|
s << "Sqlite Plugin: extent could not be determined for table '" << geometry_table_
|
|
|
|
<< "' and geometry field '" << geometry_field_ << "'"
|
2011-10-29 06:24:47 +02:00
|
|
|
<< " because an rtree spatial index is missing or empty."
|
|
|
|
<< " - either set the table 'extent' or create an rtree spatial index";
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-29 06:24:47 +02:00
|
|
|
throw datasource_exception(s.str());
|
2011-08-15 22:02:57 +02:00
|
|
|
}
|
2011-08-13 04:52:46 +02:00
|
|
|
}
|
2009-02-09 20:43:57 +01:00
|
|
|
}
|
|
|
|
|
2018-08-31 09:11:44 +02:00
|
|
|
std::string sqlite_datasource::populate_tokens(std::string const& sql, double pixel_width, double pixel_height) const
|
2011-12-16 19:05:54 +01:00
|
|
|
{
|
|
|
|
std::string populated_sql = sql;
|
|
|
|
if (boost::algorithm::ifind_first(populated_sql, intersects_token_))
|
|
|
|
{
|
|
|
|
// replace with dummy comparison that is true
|
|
|
|
boost::algorithm::ireplace_first(populated_sql, intersects_token_, "1=1");
|
|
|
|
}
|
2018-08-31 09:11:44 +02:00
|
|
|
if (boost::algorithm::icontains(sql, pixel_width_token_))
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << pixel_width;
|
|
|
|
boost::algorithm::replace_all(populated_sql, pixel_width_token_, ss.str());
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
if (boost::algorithm::icontains(sql, pixel_height_token_))
|
2018-08-31 09:11:44 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << pixel_height;
|
|
|
|
boost::algorithm::replace_all(populated_sql, pixel_height_token_, ss.str());
|
|
|
|
}
|
2011-12-16 19:05:54 +01:00
|
|
|
return populated_sql;
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
sqlite_datasource::~sqlite_datasource() {}
|
2011-10-29 06:24:47 +02:00
|
|
|
|
2011-10-18 23:32:25 +02:00
|
|
|
void sqlite_datasource::parse_attachdb(std::string const& attachdb) const
|
|
|
|
{
|
|
|
|
boost::char_separator<char> sep(",");
|
2022-01-26 10:43:31 +01:00
|
|
|
boost::tokenizer<boost::char_separator<char>> tok(attachdb, sep);
|
2011-10-18 23:32:25 +02:00
|
|
|
|
|
|
|
// The attachdb line is a comma sparated list of [dbname@]filename
|
2022-01-26 10:43:31 +01:00
|
|
|
for (boost::tokenizer<boost::char_separator<char>>::iterator beg = tok.begin(); beg != tok.end(); ++beg)
|
2011-10-18 23:32:25 +02:00
|
|
|
{
|
|
|
|
std::string const& spec(*beg);
|
|
|
|
size_t atpos = spec.find('@');
|
|
|
|
|
|
|
|
// See if it contains an @ sign
|
|
|
|
if (atpos == spec.npos)
|
|
|
|
{
|
|
|
|
throw datasource_exception("attachdb parameter has syntax dbname@filename[,...]");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Break out the dbname and the filename
|
2013-01-04 03:02:21 +01:00
|
|
|
std::string dbname = mapnik::util::trim_copy(spec.substr(0, atpos));
|
|
|
|
std::string filename = mapnik::util::trim_copy(spec.substr(atpos + 1));
|
2011-10-18 23:32:25 +02:00
|
|
|
// Normalize the filename and make it relative to dataset_name_
|
2013-06-03 04:28:24 +02:00
|
|
|
if (filename.compare(":memory:") != 0 && mapnik::util::is_relative(filename))
|
2011-10-18 23:32:25 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
filename = mapnik::util::make_relative(filename, dataset_name_);
|
2011-10-18 23:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// And add an init_statement_
|
|
|
|
init_statements_.push_back("attach database '" + filename + "' as " + dbname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
const char* sqlite_datasource::name()
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
2011-10-18 22:19:03 +02:00
|
|
|
return "sqlite";
|
2009-02-09 20:43:57 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 07:09:46 +01:00
|
|
|
mapnik::datasource::datasource_t sqlite_datasource::type() const
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
2011-10-18 22:19:03 +02:00
|
|
|
return type_;
|
2009-02-09 20:43:57 +01:00
|
|
|
}
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> sqlite_datasource::envelope() const
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
2011-10-18 22:19:03 +02:00
|
|
|
return extent_;
|
2009-02-09 20:43:57 +01:00
|
|
|
}
|
|
|
|
|
2024-04-19 20:45:24 +02:00
|
|
|
std::optional<mapnik::datasource_geometry_t> sqlite_datasource::get_geometry_type() const
|
2012-01-17 07:09:46 +01:00
|
|
|
{
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "sqlite_datasource::get_geometry_type");
|
|
|
|
#endif
|
|
|
|
|
2024-04-19 20:45:24 +02:00
|
|
|
std::optional<mapnik::datasource_geometry_t> result;
|
2012-01-17 07:09:46 +01:00
|
|
|
if (dataset_)
|
|
|
|
{
|
2015-03-22 22:58:14 +01:00
|
|
|
// get geometry type by querying first features
|
2012-01-17 07:09:46 +01:00
|
|
|
std::ostringstream s;
|
2022-01-26 10:43:31 +01:00
|
|
|
s << "SELECT " << geometry_field_ << " FROM " << geometry_table_;
|
2012-01-17 07:09:46 +01:00
|
|
|
if (row_limit_ > 0 && row_limit_ < 5)
|
|
|
|
{
|
|
|
|
s << " LIMIT " << row_limit_;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s << " LIMIT 5";
|
|
|
|
}
|
2013-09-20 15:00:11 +02:00
|
|
|
std::shared_ptr<sqlite_resultset> rs = dataset_->execute_query(s.str());
|
2012-01-17 07:09:46 +01:00
|
|
|
int multi_type = 0;
|
|
|
|
while (rs->is_valid() && rs->step_next())
|
|
|
|
{
|
|
|
|
int size;
|
2022-01-26 10:43:31 +01:00
|
|
|
const char* data = (const char*)rs->column_blob(0, size);
|
2012-01-17 07:09:46 +01:00
|
|
|
if (data)
|
|
|
|
{
|
2017-04-23 21:48:35 +02:00
|
|
|
mapnik::geometry::geometry<double> geom;
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (twkb_encoding_)
|
|
|
|
geom = mapnik::geometry_utils::from_twkb(data, size);
|
|
|
|
else
|
|
|
|
geom = mapnik::geometry_utils::from_wkb(data, size, format_);
|
2015-03-24 22:16:58 +01:00
|
|
|
if (mapnik::geometry::is_empty(geom))
|
2015-03-22 22:58:14 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-24 12:13:31 +01:00
|
|
|
result = mapnik::util::to_ds_type(geom);
|
2015-03-22 22:58:14 +01:00
|
|
|
if (result)
|
2012-01-17 07:09:46 +01:00
|
|
|
{
|
2015-03-22 22:58:14 +01:00
|
|
|
int type = static_cast<int>(*result);
|
|
|
|
if (multi_type > 0 && multi_type != type)
|
2012-01-17 07:09:46 +01:00
|
|
|
{
|
2024-04-19 20:45:24 +02:00
|
|
|
result = mapnik::datasource_geometry_t::Collection;
|
2015-03-22 22:58:14 +01:00
|
|
|
return result;
|
2012-01-17 07:09:46 +01:00
|
|
|
}
|
2015-03-22 22:58:14 +01:00
|
|
|
multi_type = type;
|
2012-01-17 07:09:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 02:37:35 +01:00
|
|
|
|
2012-01-17 07:09:46 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-02-09 20:43:57 +01:00
|
|
|
layer_descriptor sqlite_datasource::get_descriptor() const
|
|
|
|
{
|
2011-10-18 22:19:03 +02:00
|
|
|
return desc_;
|
2009-02-09 20:43:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr sqlite_datasource::features(query const& q) const
|
|
|
|
{
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "sqlite_datasource::features");
|
|
|
|
#endif
|
|
|
|
|
2011-10-22 14:50:24 +02:00
|
|
|
if (dataset_)
|
|
|
|
{
|
2009-12-16 21:02:06 +01:00
|
|
|
mapnik::box2d<double> const& e = q.get_bbox();
|
2009-02-09 20:43:57 +01:00
|
|
|
|
|
|
|
std::ostringstream s;
|
2018-08-31 09:11:44 +02:00
|
|
|
|
|
|
|
const double px_gw = 1.0 / std::get<0>(q.resolution());
|
|
|
|
const double px_gh = 1.0 / std::get<1>(q.resolution());
|
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-29 06:24:47 +02:00
|
|
|
s << "SELECT " << geometry_field_;
|
|
|
|
if (!key_field_.empty())
|
2012-01-24 08:51:31 +01:00
|
|
|
{
|
2011-10-29 06:24:47 +02:00
|
|
|
s << "," << key_field_;
|
2012-01-24 08:51:31 +01:00
|
|
|
ctx->push(key_field_);
|
|
|
|
}
|
2012-04-04 01:33:06 +02: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();
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
for (; pos != end; ++pos)
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
2012-04-04 01:33:06 +02:00
|
|
|
// TODO - should we restrict duplicate key query?
|
2022-01-26 10:43:31 +01:00
|
|
|
// if (*pos != key_field_)
|
2012-04-04 01:33:06 +02:00
|
|
|
s << ",[" << *pos << "]";
|
|
|
|
ctx->push(*pos);
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
|
|
|
s << " FROM ";
|
|
|
|
|
2011-12-16 19:05:54 +01:00
|
|
|
std::string query(table_);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!key_field_.empty() && has_spatial_index_)
|
2009-02-10 20:09:16 +01:00
|
|
|
{
|
2011-12-16 17:14:08 +01:00
|
|
|
// TODO - debug warn if fails
|
2011-12-16 19:05:54 +01:00
|
|
|
sqlite_utils::apply_spatial_filter(query,
|
|
|
|
e,
|
|
|
|
table_,
|
|
|
|
key_field_,
|
|
|
|
index_table_,
|
|
|
|
geometry_table_,
|
2012-04-08 02:20:56 +02:00
|
|
|
intersects_token_);
|
|
|
|
}
|
2018-08-31 09:11:44 +02:00
|
|
|
|
|
|
|
query = populate_tokens(query, px_gw, px_gh);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
s << query;
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
if (row_limit_ > 0)
|
|
|
|
{
|
2010-11-19 00:36:21 +01:00
|
|
|
s << " LIMIT " << row_limit_;
|
2009-02-23 16:00:25 +01:00
|
|
|
}
|
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
if (row_offset_ > 0)
|
|
|
|
{
|
2010-11-19 00:36:21 +01:00
|
|
|
s << " OFFSET " << row_offset_;
|
2009-02-23 16:00:25 +01:00
|
|
|
}
|
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(sqlite) << "sqlite_datasource: " << s.str();
|
2009-02-10 20:09:16 +01:00
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
std::shared_ptr<sqlite_resultset> rs(dataset_->execute_query(s.str()));
|
2009-02-09 20:43:57 +01:00
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
return std::make_shared<sqlite_featureset>(rs,
|
2022-01-26 10:43:31 +01:00
|
|
|
ctx,
|
|
|
|
desc_.get_encoding(),
|
|
|
|
e,
|
|
|
|
format_,
|
|
|
|
twkb_encoding_,
|
|
|
|
has_spatial_index_,
|
|
|
|
using_subquery_);
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
2009-02-09 20:43:57 +01:00
|
|
|
|
2016-08-15 11:41:30 +02:00
|
|
|
return mapnik::make_invalid_featureset();
|
2009-02-09 20:43:57 +01:00
|
|
|
}
|
|
|
|
|
2012-09-28 15:12:10 +02:00
|
|
|
featureset_ptr sqlite_datasource::features_at_point(coord2d const& pt, double tol) const
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "sqlite_datasource::features_at_point");
|
|
|
|
#endif
|
|
|
|
|
2011-10-22 14:50:24 +02:00
|
|
|
if (dataset_)
|
|
|
|
{
|
2013-05-22 12:07:55 +02:00
|
|
|
mapnik::box2d<double> e(pt.x, pt.y, pt.x, pt.y);
|
|
|
|
e.pad(tol);
|
2011-04-02 06:54:26 +02:00
|
|
|
std::ostringstream s;
|
2013-09-20 15:00:11 +02:00
|
|
|
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
|
2012-01-24 08:51:31 +01:00
|
|
|
|
2011-10-29 06:24:47 +02:00
|
|
|
s << "SELECT " << geometry_field_;
|
|
|
|
if (!key_field_.empty())
|
2012-01-24 08:51:31 +01:00
|
|
|
{
|
2011-10-29 06:24:47 +02:00
|
|
|
s << "," << key_field_;
|
2012-01-24 08:51:31 +01:00
|
|
|
ctx->push(key_field_);
|
|
|
|
}
|
2012-02-02 02:37:35 +01:00
|
|
|
|
2015-10-10 01:07:19 +02:00
|
|
|
auto const& desc = desc_.get_descriptors();
|
2012-01-24 08:51:31 +01:00
|
|
|
|
2015-10-10 01:07:19 +02:00
|
|
|
for (auto const& attr_info : desc)
|
2011-04-02 06:54:26 +02:00
|
|
|
{
|
2015-10-10 01:07:19 +02:00
|
|
|
std::string const& name = attr_info.get_name();
|
|
|
|
if (name != key_field_)
|
2011-10-18 22:19:03 +02:00
|
|
|
{
|
2015-10-10 01:07:19 +02:00
|
|
|
s << ",[" << name << "]";
|
|
|
|
ctx->push(name);
|
2011-10-18 22:19:03 +02:00
|
|
|
}
|
2011-04-02 06:54:26 +02:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
|
|
|
s << " FROM ";
|
|
|
|
|
2011-10-22 14:50:24 +02:00
|
|
|
std::string query(table_);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!key_field_.empty() && has_spatial_index_)
|
2011-04-02 06:54:26 +02:00
|
|
|
{
|
2011-12-16 17:14:08 +01:00
|
|
|
// TODO - debug warn if fails
|
2011-12-16 19:05:54 +01:00
|
|
|
sqlite_utils::apply_spatial_filter(query,
|
|
|
|
e,
|
|
|
|
table_,
|
|
|
|
key_field_,
|
|
|
|
index_table_,
|
|
|
|
geometry_table_,
|
|
|
|
intersects_token_);
|
|
|
|
}
|
2018-08-31 09:11:44 +02:00
|
|
|
|
|
|
|
query = populate_tokens(query, 0, 0);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
s << query;
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
if (row_limit_ > 0)
|
|
|
|
{
|
2011-04-02 06:54:26 +02:00
|
|
|
s << " LIMIT " << row_limit_;
|
|
|
|
}
|
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
if (row_offset_ > 0)
|
|
|
|
{
|
2011-04-02 06:54:26 +02:00
|
|
|
s << " OFFSET " << row_offset_;
|
|
|
|
}
|
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(sqlite) << "sqlite_datasource: " << s.str();
|
2011-04-02 06:54:26 +02:00
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
std::shared_ptr<sqlite_resultset> rs(dataset_->execute_query(s.str()));
|
2011-04-02 06:54:26 +02:00
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
return std::make_shared<sqlite_featureset>(rs,
|
2022-01-26 10:43:31 +01:00
|
|
|
ctx,
|
|
|
|
desc_.get_encoding(),
|
|
|
|
e,
|
|
|
|
format_,
|
|
|
|
twkb_encoding_,
|
|
|
|
has_spatial_index_,
|
|
|
|
using_subquery_);
|
2011-11-14 04:37:50 +01:00
|
|
|
}
|
2012-02-02 02:37:35 +01:00
|
|
|
|
2016-08-15 11:41:30 +02:00
|
|
|
return mapnik::make_invalid_featureset();
|
2009-02-09 20:43:57 +01:00
|
|
|
}
|