2009-02-03 18:06:23 +01:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2009-02-03 18:06:23 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-22 03:33:03 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2009-02-03 18:06:23 +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 "occi_datasource.hpp"
|
|
|
|
#include "occi_featureset.hpp"
|
|
|
|
|
|
|
|
// 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-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/timer.hpp>
|
2009-02-03 18:06:23 +01:00
|
|
|
|
|
|
|
// boost
|
2009-02-24 10:58:31 +01:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2009-02-03 18:06:23 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/tokenizer.hpp>
|
2011-05-17 01:41:34 +02:00
|
|
|
#include <boost/make_shared.hpp>
|
2009-02-03 18:06:23 +01:00
|
|
|
|
|
|
|
// stl
|
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
2009-02-05 12:17:27 +01:00
|
|
|
using boost::lexical_cast;
|
|
|
|
using boost::bad_lexical_cast;
|
|
|
|
|
2009-02-03 18:06:23 +01:00
|
|
|
using mapnik::datasource;
|
|
|
|
using mapnik::parameters;
|
|
|
|
using mapnik::query;
|
|
|
|
using mapnik::featureset_ptr;
|
|
|
|
using mapnik::layer_descriptor;
|
|
|
|
using mapnik::attribute_descriptor;
|
|
|
|
using mapnik::datasource_exception;
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::box2d;
|
2009-02-03 18:06:23 +01:00
|
|
|
using mapnik::coord2d;
|
|
|
|
|
|
|
|
using oracle::occi::Environment;
|
|
|
|
using oracle::occi::Connection;
|
|
|
|
using oracle::occi::Statement;
|
|
|
|
using oracle::occi::ResultSet;
|
|
|
|
using oracle::occi::MetaData;
|
|
|
|
using oracle::occi::SQLException;
|
|
|
|
using oracle::occi::Type;
|
|
|
|
using oracle::occi::StatelessConnectionPool;
|
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
const std::string occi_datasource::METADATA_TABLE = "USER_SDO_GEOM_METADATA";
|
2010-11-22 12:40:08 +01:00
|
|
|
|
2009-02-03 18:06:23 +01:00
|
|
|
DATASOURCE_PLUGIN(occi_datasource)
|
|
|
|
|
2010-10-24 08:34:18 +02:00
|
|
|
occi_datasource::occi_datasource(parameters const& params, bool bind)
|
2012-04-08 02:20:56 +02:00
|
|
|
: datasource (params),
|
|
|
|
type_(datasource::Vector),
|
|
|
|
fields_(*params_.get<std::string>("fields", "*")),
|
|
|
|
geometry_field_(*params_.get<std::string>("geometry_field", "")),
|
|
|
|
srid_initialized_(false),
|
|
|
|
extent_initialized_(false),
|
|
|
|
desc_(*params_.get<std::string>("type"), *params_.get<std::string>("encoding", "utf-8")),
|
|
|
|
use_wkb_(*params_.get<mapnik::boolean>("use_wkb", false)),
|
|
|
|
row_limit_(*params_.get<int>("row_limit", 0)),
|
|
|
|
row_prefetch_(*params_.get<int>("row_prefetch", 100)),
|
|
|
|
pool_(0),
|
|
|
|
conn_(0)
|
2009-02-03 18:06:23 +01:00
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
if (! params_.get<std::string>("user")) throw datasource_exception("OCCI Plugin: no <user> specified");
|
|
|
|
if (! params_.get<std::string>("password")) throw datasource_exception("OCCI Plugin: no <password> specified");
|
|
|
|
if (! params_.get<std::string>("host")) throw datasource_exception("OCCI Plugin: no <host> string specified");
|
2009-02-03 18:06:23 +01:00
|
|
|
|
2010-11-22 12:40:08 +01:00
|
|
|
boost::optional<std::string> table = params_.get<std::string>("table");
|
2011-10-22 03:33:03 +02:00
|
|
|
if (! table)
|
2010-11-22 12:40:08 +01:00
|
|
|
{
|
|
|
|
throw datasource_exception("OCCI Plugin: no <table> parameter specified");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
table_ = *table;
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
use_spatial_index_ = *params_.get<mapnik::boolean>("use_spatial_index",true);
|
|
|
|
use_connection_pool_ = *params_.get<mapnik::boolean>("use_connection_pool",true);
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
boost::optional<std::string> ext = params_.get<std::string>("extent");
|
|
|
|
if (ext) extent_initialized_ = extent_.from_string(*ext);
|
|
|
|
|
2010-11-18 15:24:19 +01:00
|
|
|
boost::optional<int> srid = params_.get<int>("srid");
|
|
|
|
if (srid)
|
|
|
|
{
|
|
|
|
srid_ = *srid;
|
|
|
|
srid_initialized_ = true;
|
|
|
|
}
|
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
if (bind)
|
|
|
|
{
|
|
|
|
this->bind();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
occi_datasource::~occi_datasource()
|
|
|
|
{
|
|
|
|
if (is_bound_)
|
|
|
|
{
|
|
|
|
Environment* env = occi_environment::get_environment();
|
|
|
|
|
|
|
|
if (use_connection_pool_)
|
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
if (pool_ != 0)
|
2011-10-22 03:33:03 +02:00
|
|
|
{
|
|
|
|
env->terminateStatelessConnectionPool(pool_, StatelessConnectionPool::SPD_FORCE);
|
|
|
|
}
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
if (conn_ != 0)
|
2011-10-22 03:33:03 +02:00
|
|
|
{
|
2010-11-16 18:31:13 +01:00
|
|
|
env->terminateConnection(conn_);
|
2011-10-22 03:33:03 +02:00
|
|
|
}
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
|
|
|
}
|
2010-10-24 08:34:18 +02:00
|
|
|
}
|
2009-02-03 18:06:23 +01:00
|
|
|
|
2010-11-05 18:37:48 +01:00
|
|
|
void occi_datasource::bind() const
|
2010-10-24 08:34:18 +02:00
|
|
|
{
|
2010-11-16 18:31:13 +01:00
|
|
|
if (is_bound_) return;
|
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "occi_datasource::bind");
|
|
|
|
#endif
|
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
// connect to environment
|
|
|
|
if (use_connection_pool_)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Environment* env = occi_environment::get_environment();
|
|
|
|
|
|
|
|
pool_ = env->createStatelessConnectionPool(
|
2011-11-14 04:37:50 +01:00
|
|
|
*params_.get<std::string>("user"),
|
|
|
|
*params_.get<std::string>("password"),
|
|
|
|
*params_.get<std::string>("host"),
|
2012-04-10 15:53:58 +02:00
|
|
|
*params_.get<int>("max_size", 5),
|
2011-11-14 04:37:50 +01:00
|
|
|
*params_.get<int>("initial_size", 1),
|
|
|
|
1,
|
|
|
|
StatelessConnectionPool::HOMOGENEOUS);
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
2011-10-22 03:33:03 +02:00
|
|
|
catch (SQLException& ex)
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
throw datasource_exception("OCCI Plugin: " + ex.getMessage());
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Environment* env = occi_environment::get_environment();
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
conn_ = env->createConnection(
|
2011-11-14 04:37:50 +01:00
|
|
|
*params_.get<std::string>("user"),
|
|
|
|
*params_.get<std::string>("password"),
|
|
|
|
*params_.get<std::string>("host"));
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
2011-10-22 03:33:03 +02:00
|
|
|
catch (SQLException& ex)
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
throw datasource_exception("OCCI Plugin: " + ex.getMessage());
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
|
|
|
}
|
2010-11-05 18:37:48 +01:00
|
|
|
|
2011-11-15 14:04:58 +01:00
|
|
|
// extract real table name
|
|
|
|
table_name_ = mapnik::sql_utils::table_from_sql(table_);
|
2010-11-15 10:25:06 +01:00
|
|
|
|
2010-11-18 15:24:19 +01:00
|
|
|
// get SRID and/or GEOMETRY_FIELD from metadata table only if we need to
|
|
|
|
if (! srid_initialized_ || geometry_field_ == "")
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "occi_datasource::get_srid_and_geometry_field");
|
|
|
|
#endif
|
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
std::ostringstream s;
|
2010-11-22 12:40:08 +01:00
|
|
|
s << "SELECT srid, column_name FROM " << METADATA_TABLE << " WHERE";
|
2011-11-15 14:04:58 +01:00
|
|
|
s << " LOWER(table_name) = LOWER('" << table_name_ << "')";
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-16 23:32:07 +01:00
|
|
|
if (geometry_field_ != "")
|
2011-10-22 03:33:03 +02:00
|
|
|
{
|
2010-11-16 23:32:07 +01:00
|
|
|
s << " AND LOWER(column_name) = LOWER('" << geometry_field_ << "')";
|
2011-10-22 03:33:03 +02:00
|
|
|
}
|
2009-02-03 18:06:23 +01:00
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(occi) << "occi_datasource: " << s.str();
|
2010-11-22 12:40:08 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
occi_connection_ptr conn;
|
|
|
|
if (use_connection_pool_) conn.set_pool(pool_);
|
|
|
|
else conn.set_connection(conn_, false);
|
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
ResultSet* rs = conn.execute_query(s.str());
|
2010-11-16 18:31:13 +01:00
|
|
|
if (rs && rs->next ())
|
|
|
|
{
|
2010-11-18 15:24:19 +01:00
|
|
|
if (! srid_initialized_)
|
|
|
|
{
|
|
|
|
srid_ = rs->getInt(1);
|
|
|
|
srid_initialized_ = true;
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-11-16 23:32:07 +01:00
|
|
|
if (geometry_field_ == "")
|
2010-11-18 15:24:19 +01:00
|
|
|
{
|
2010-11-16 23:32:07 +01:00
|
|
|
geometry_field_ = rs->getString(2);
|
2010-11-18 15:24:19 +01:00
|
|
|
}
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
|
|
|
}
|
2011-10-22 03:33:03 +02:00
|
|
|
catch (SQLException& ex)
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
throw datasource_exception("OCCI Plugin: " + ex.getMessage());
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get columns description
|
|
|
|
{
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "occi_datasource::get_column_description");
|
|
|
|
#endif
|
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
std::ostringstream s;
|
2011-11-15 14:04:58 +01:00
|
|
|
s << "SELECT " << fields_ << " FROM (" << table_name_ << ") WHERE rownum < 1";
|
2010-11-16 18:31:13 +01:00
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(occi) << "occi_datasource: " << s.str();
|
2010-11-22 12:40:08 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
occi_connection_ptr conn;
|
|
|
|
if (use_connection_pool_) conn.set_pool(pool_);
|
|
|
|
else conn.set_connection(conn_, false);
|
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
ResultSet* rs = conn.execute_query(s.str());
|
2010-11-16 18:31:13 +01:00
|
|
|
if (rs)
|
|
|
|
{
|
|
|
|
std::vector<MetaData> listOfColumns = rs->getColumnListMetaData();
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < listOfColumns.size(); ++i)
|
|
|
|
{
|
|
|
|
MetaData columnObj = listOfColumns[i];
|
|
|
|
|
|
|
|
std::string fld_name = columnObj.getString(MetaData::ATTR_NAME);
|
|
|
|
int type_oid = columnObj.getInt(MetaData::ATTR_DATA_TYPE);
|
2009-02-24 10:58:31 +01:00
|
|
|
|
2010-11-22 12:40:08 +01:00
|
|
|
/*
|
2011-11-14 04:37:50 +01:00
|
|
|
int type_code = columnObj.getInt(MetaData::ATTR_TYPECODE);
|
|
|
|
if (type_code == OCCI_TYPECODE_OBJECT)
|
|
|
|
{
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Object));
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-22 12:40:08 +01:00
|
|
|
*/
|
2009-02-03 18:06:23 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
switch (type_oid)
|
|
|
|
{
|
|
|
|
case oracle::occi::OCCIBOOL:
|
|
|
|
case oracle::occi::OCCIINT:
|
|
|
|
case oracle::occi::OCCIUNSIGNED_INT:
|
|
|
|
case oracle::occi::OCCIROWID:
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Integer));
|
|
|
|
break;
|
|
|
|
case oracle::occi::OCCIFLOAT:
|
|
|
|
case oracle::occi::OCCIBFLOAT:
|
|
|
|
case oracle::occi::OCCIDOUBLE:
|
|
|
|
case oracle::occi::OCCIBDOUBLE:
|
|
|
|
case oracle::occi::OCCINUMBER:
|
|
|
|
case oracle::occi::OCCI_SQLT_NUM:
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Double));
|
|
|
|
break;
|
|
|
|
case oracle::occi::OCCICHAR:
|
|
|
|
case oracle::occi::OCCISTRING:
|
|
|
|
case oracle::occi::OCCI_SQLT_AFC:
|
|
|
|
case oracle::occi::OCCI_SQLT_AVC:
|
|
|
|
case oracle::occi::OCCI_SQLT_CHR:
|
|
|
|
case oracle::occi::OCCI_SQLT_LVC:
|
|
|
|
case oracle::occi::OCCI_SQLT_RDD:
|
|
|
|
case oracle::occi::OCCI_SQLT_STR:
|
|
|
|
case oracle::occi::OCCI_SQLT_VCS:
|
|
|
|
case oracle::occi::OCCI_SQLT_VNU:
|
|
|
|
case oracle::occi::OCCI_SQLT_VBI:
|
|
|
|
case oracle::occi::OCCI_SQLT_VST:
|
|
|
|
desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::String));
|
|
|
|
break;
|
|
|
|
case oracle::occi::OCCIDATE:
|
|
|
|
case oracle::occi::OCCITIMESTAMP:
|
|
|
|
case oracle::occi::OCCIINTERVALDS:
|
|
|
|
case oracle::occi::OCCIINTERVALYM:
|
|
|
|
case oracle::occi::OCCI_SQLT_DAT:
|
|
|
|
case oracle::occi::OCCI_SQLT_DATE:
|
|
|
|
case oracle::occi::OCCI_SQLT_TIME:
|
|
|
|
case oracle::occi::OCCI_SQLT_TIME_TZ:
|
|
|
|
case oracle::occi::OCCI_SQLT_TIMESTAMP:
|
|
|
|
case oracle::occi::OCCI_SQLT_TIMESTAMP_LTZ:
|
|
|
|
case oracle::occi::OCCI_SQLT_TIMESTAMP_TZ:
|
|
|
|
case oracle::occi::OCCI_SQLT_INTERVAL_YM:
|
|
|
|
case oracle::occi::OCCI_SQLT_INTERVAL_DS:
|
|
|
|
case oracle::occi::OCCIANYDATA:
|
|
|
|
case oracle::occi::OCCIBLOB:
|
|
|
|
case oracle::occi::OCCIBFILE:
|
|
|
|
case oracle::occi::OCCIBYTES:
|
|
|
|
case oracle::occi::OCCICLOB:
|
|
|
|
case oracle::occi::OCCIVECTOR:
|
|
|
|
case oracle::occi::OCCIMETADATA:
|
|
|
|
case oracle::occi::OCCIPOBJECT:
|
|
|
|
case oracle::occi::OCCIREF:
|
|
|
|
case oracle::occi::OCCIREFANY:
|
|
|
|
case oracle::occi::OCCISTREAM:
|
|
|
|
case oracle::occi::OCCICURSOR:
|
|
|
|
case oracle::occi::OCCI_SQLT_FILE:
|
|
|
|
case oracle::occi::OCCI_SQLT_CFILE:
|
|
|
|
case oracle::occi::OCCI_SQLT_REF:
|
|
|
|
case oracle::occi::OCCI_SQLT_CLOB:
|
|
|
|
case oracle::occi::OCCI_SQLT_BLOB:
|
|
|
|
case oracle::occi::OCCI_SQLT_RSET:
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(occi) << "occi_datasource: Unsupported datatype "
|
|
|
|
<< occi_enums::resolve_datatype(type_oid)
|
|
|
|
<< " (type_oid=" << type_oid << ")";
|
2010-11-16 18:31:13 +01:00
|
|
|
break;
|
|
|
|
default:
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(occi) << "occi_datasource: Unknown datatype "
|
|
|
|
<< "(type_oid=" << type_oid << ")";
|
2010-11-16 18:31:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 03:33:03 +02:00
|
|
|
catch (SQLException& ex)
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
|
|
|
throw datasource_exception(ex.getMessage());
|
|
|
|
}
|
2010-10-24 08:34:18 +02:00
|
|
|
}
|
2010-11-16 18:31:13 +01:00
|
|
|
|
|
|
|
is_bound_ = true;
|
2009-02-03 18:06:23 +01:00
|
|
|
}
|
|
|
|
|
2012-07-21 03:34:41 +02:00
|
|
|
const char * occi_datasource::name()
|
2009-02-03 18:06:23 +01:00
|
|
|
{
|
2010-11-14 15:43:49 +01:00
|
|
|
return "occi";
|
2009-02-03 18:06:23 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 07:09:46 +01:00
|
|
|
mapnik::datasource::datasource_t occi_datasource::type() const
|
2009-02-03 18:06:23 +01:00
|
|
|
{
|
2009-02-05 12:17:27 +01:00
|
|
|
return type_;
|
2009-02-03 18:06:23 +01:00
|
|
|
}
|
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> occi_datasource::envelope() const
|
2009-02-03 18:06:23 +01:00
|
|
|
{
|
2009-02-05 12:17:27 +01:00
|
|
|
if (extent_initialized_) return extent_;
|
2011-10-22 03:33:03 +02:00
|
|
|
if (! is_bound_) bind();
|
2009-02-05 12:17:27 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
double lox = 0.0, loy = 0.0, hix = 0.0, hiy = 0.0;
|
2009-02-05 12:17:27 +01:00
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
boost::optional<mapnik::boolean> estimate_extent =
|
|
|
|
params_.get<mapnik::boolean>("estimate_extent",false);
|
2010-11-15 10:25:06 +01:00
|
|
|
|
2010-03-17 23:03:39 +01:00
|
|
|
if (estimate_extent && *estimate_extent)
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "occi_datasource::envelope(estimate_extent)");
|
|
|
|
#endif
|
|
|
|
|
2009-02-23 18:16:09 +01:00
|
|
|
std::ostringstream s;
|
2010-11-16 23:32:07 +01:00
|
|
|
s << "SELECT MIN(c.x), MIN(c.y), MAX(c.x), MAX(c.y) FROM ";
|
|
|
|
s << " (SELECT SDO_AGGR_MBR(" << geometry_field_ << ") shape FROM " << table_ << ") a, ";
|
|
|
|
s << " TABLE(SDO_UTIL.GETVERTICES(a.shape)) c";
|
2009-02-05 12:17:27 +01:00
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(occi) << "occi_datasource: " << s.str();
|
2010-11-16 18:31:13 +01:00
|
|
|
|
2009-02-23 18:16:09 +01:00
|
|
|
try
|
|
|
|
{
|
2010-11-16 18:31:13 +01:00
|
|
|
occi_connection_ptr conn;
|
|
|
|
if (use_connection_pool_) conn.set_pool(pool_);
|
|
|
|
else conn.set_connection(conn_, false);
|
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
ResultSet* rs = conn.execute_query(s.str());
|
|
|
|
if (rs && rs->next())
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
2010-11-15 10:25:06 +01:00
|
|
|
try
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
|
|
|
lox = lexical_cast<double>(rs->getDouble(1));
|
|
|
|
loy = lexical_cast<double>(rs->getDouble(2));
|
|
|
|
hix = lexical_cast<double>(rs->getDouble(3));
|
2010-11-15 10:25:06 +01:00
|
|
|
hiy = lexical_cast<double>(rs->getDouble(4));
|
2011-10-22 03:33:03 +02:00
|
|
|
extent_.init(lox, loy, hix, hiy);
|
2009-02-23 18:16:09 +01:00
|
|
|
extent_initialized_ = true;
|
|
|
|
}
|
2011-10-22 03:33:03 +02:00
|
|
|
catch (bad_lexical_cast& ex)
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(occi) << "OCCI Plugin: " << ex.what();
|
2009-02-23 18:16:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 03:33:03 +02:00
|
|
|
catch (SQLException& ex)
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
throw datasource_exception("OCCI Plugin: " + ex.getMessage());
|
2009-02-23 18:16:09 +01:00
|
|
|
}
|
|
|
|
}
|
2009-02-24 10:58:31 +01:00
|
|
|
else if (use_spatial_index_)
|
2009-02-05 12:17:27 +01:00
|
|
|
{
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "occi_datasource::envelope(use_spatial_index)");
|
|
|
|
#endif
|
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
std::ostringstream s;
|
2010-11-16 23:32:07 +01:00
|
|
|
s << "SELECT dim.sdo_lb, dim.sdo_ub FROM ";
|
2010-11-22 12:40:08 +01:00
|
|
|
s << METADATA_TABLE << " m, TABLE(m.diminfo) dim ";
|
2011-11-15 14:04:58 +01:00
|
|
|
s << " WHERE LOWER(m.table_name) = LOWER('" << table_name_ << "') AND dim.sdo_dimname = 'X'";
|
2010-11-16 18:31:13 +01:00
|
|
|
s << " UNION ";
|
2010-11-16 23:32:07 +01:00
|
|
|
s << "SELECT dim.sdo_lb, dim.sdo_ub FROM ";
|
2010-11-22 12:40:08 +01:00
|
|
|
s << METADATA_TABLE << " m, TABLE(m.diminfo) dim ";
|
2011-11-15 14:04:58 +01:00
|
|
|
s << " WHERE LOWER(m.table_name) = LOWER('" << table_name_ << "') AND dim.sdo_dimname = 'Y'";
|
2010-11-16 18:31:13 +01:00
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(occi) << "occi_datasource: " << s.str();
|
2010-11-16 18:31:13 +01:00
|
|
|
|
|
|
|
try
|
2009-02-05 12:17:27 +01:00
|
|
|
{
|
2010-11-16 18:31:13 +01:00
|
|
|
occi_connection_ptr conn;
|
|
|
|
if (use_connection_pool_) conn.set_pool(pool_);
|
|
|
|
else conn.set_connection(conn_, false);
|
2010-11-15 10:25:06 +01:00
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
ResultSet* rs = conn.execute_query(s.str());
|
2010-11-16 18:31:13 +01:00
|
|
|
if (rs)
|
2009-02-05 12:17:27 +01:00
|
|
|
{
|
2011-10-22 03:33:03 +02:00
|
|
|
if (rs->next())
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
2010-11-15 10:25:06 +01:00
|
|
|
try
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
|
|
|
lox = lexical_cast<double>(rs->getDouble(1));
|
|
|
|
hix = lexical_cast<double>(rs->getDouble(2));
|
|
|
|
}
|
2011-10-22 03:33:03 +02:00
|
|
|
catch (bad_lexical_cast& ex)
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(occi) << "OCCI Plugin: " << ex.what();
|
2009-02-23 18:16:09 +01:00
|
|
|
}
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
if (rs->next())
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
2010-11-15 10:25:06 +01:00
|
|
|
try
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
|
|
|
loy = lexical_cast<double>(rs->getDouble(1));
|
|
|
|
hiy = lexical_cast<double>(rs->getDouble(2));
|
|
|
|
}
|
2011-10-22 03:33:03 +02:00
|
|
|
catch (bad_lexical_cast& ex)
|
2009-02-23 18:16:09 +01:00
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(occi) << "OCCI Plugin: " << ex.what();
|
2009-02-23 18:16:09 +01:00
|
|
|
}
|
|
|
|
}
|
2010-11-16 18:31:13 +01:00
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
extent_.init(lox, loy, hix, hiy);
|
2010-11-16 18:31:13 +01:00
|
|
|
extent_initialized_ = true;
|
2009-02-23 18:16:09 +01:00
|
|
|
}
|
|
|
|
}
|
2011-10-22 03:33:03 +02:00
|
|
|
catch (SQLException& ex)
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
throw datasource_exception("OCCI Plugin: " + ex.getMessage());
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
2009-02-05 12:17:27 +01:00
|
|
|
}
|
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
if (! extent_initialized_)
|
2011-10-22 03:33:03 +02:00
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
throw datasource_exception("OCCI Plugin: unable to determine the extent of a <occi> table");
|
2011-10-22 03:33:03 +02:00
|
|
|
}
|
2010-11-16 18:31:13 +01:00
|
|
|
|
2009-02-05 12:17:27 +01:00
|
|
|
return extent_;
|
2009-02-03 18:06:23 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 07:09:46 +01:00
|
|
|
|
|
|
|
boost::optional<mapnik::datasource::geometry_t> occi_datasource::get_geometry_type() const
|
|
|
|
{
|
|
|
|
// FIXME
|
|
|
|
//if (! is_bound_) bind();
|
|
|
|
return boost::optional<mapnik::datasource::geometry_t>();
|
|
|
|
}
|
|
|
|
|
2009-02-03 18:06:23 +01:00
|
|
|
layer_descriptor occi_datasource::get_descriptor() const
|
|
|
|
{
|
2011-10-22 03:33:03 +02:00
|
|
|
if (! is_bound_) bind();
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2009-02-05 12:17:27 +01:00
|
|
|
return desc_;
|
2009-02-03 18:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
featureset_ptr occi_datasource::features(query const& q) const
|
|
|
|
{
|
2011-10-22 03:33:03 +02:00
|
|
|
if (! is_bound_) bind();
|
2010-11-15 10:25:06 +01:00
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "occi_datasource::features");
|
|
|
|
#endif
|
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
box2d<double> const& box = q.get_bbox();
|
2010-11-16 18:31:13 +01:00
|
|
|
|
|
|
|
std::ostringstream s;
|
2012-04-08 02:20:56 +02:00
|
|
|
s << "SELECT ";
|
|
|
|
if (use_wkb_)
|
|
|
|
{
|
|
|
|
s << "SDO_UTIL.TO_WKBGEOMETRY(" << geometry_field_ << ")";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s << geometry_field_;
|
|
|
|
}
|
2010-11-16 18:31:13 +01: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();
|
2012-01-19 23:36:27 +01:00
|
|
|
mapnik::context_ptr ctx = boost::make_shared<mapnik::context_type>();
|
2012-04-08 02:20:56 +02:00
|
|
|
for (; pos != end; ++pos)
|
2009-02-03 18:06:23 +01:00
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
s << ", " << *pos;
|
2012-01-17 20:22:21 +01:00
|
|
|
ctx->push(*pos);
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
2010-11-15 10:25:06 +01:00
|
|
|
|
2010-11-16 23:32:07 +01:00
|
|
|
s << " FROM ";
|
2009-02-24 10:58:31 +01:00
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
std::string query(table_);
|
2009-02-24 10:58:31 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
if (use_spatial_index_)
|
|
|
|
{
|
|
|
|
std::ostringstream spatial_sql;
|
|
|
|
spatial_sql << std::setprecision(16);
|
2010-11-16 23:32:07 +01:00
|
|
|
spatial_sql << " WHERE SDO_FILTER(" << geometry_field_ << ",";
|
|
|
|
spatial_sql << " MDSYS.SDO_GEOMETRY(" << SDO_GTYPE_2DPOLYGON << "," << srid_ << ",NULL,";
|
|
|
|
spatial_sql << " MDSYS.SDO_ELEM_INFO_ARRAY(1," << SDO_ETYPE_POLYGON << "," << SDO_INTERPRETATION_RECTANGLE << "),";
|
|
|
|
spatial_sql << " MDSYS.SDO_ORDINATE_ARRAY(";
|
2010-11-16 18:31:13 +01:00
|
|
|
spatial_sql << box.minx() << "," << box.miny() << ", ";
|
|
|
|
spatial_sql << box.maxx() << "," << box.maxy() << ")), 'querytype=WINDOW') = 'TRUE'";
|
2009-04-28 16:06:35 +02:00
|
|
|
|
2010-11-16 23:32:07 +01:00
|
|
|
if (boost::algorithm::ifind_first(query, "WHERE"))
|
2009-02-24 10:58:31 +01:00
|
|
|
{
|
2010-11-16 23:32:07 +01:00
|
|
|
boost::algorithm::ireplace_first(query, "WHERE", spatial_sql.str() + " AND ");
|
2009-02-24 10:58:31 +01:00
|
|
|
}
|
2011-11-15 14:04:58 +01:00
|
|
|
else if (boost::algorithm::ifind_first(query, table_name_))
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
2011-11-15 14:04:58 +01:00
|
|
|
boost::algorithm::ireplace_first(query, table_name_, table_name_ + " " + spatial_sql.str());
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
|
|
|
else
|
2009-04-28 16:06:35 +02:00
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(occi) << "occi_datasource: cannot determine where to add the spatial filter declaration";
|
2009-04-28 16:06:35 +02:00
|
|
|
}
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
2010-11-15 10:25:06 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
if (row_limit_ > 0)
|
|
|
|
{
|
2012-04-03 04:20:09 +02:00
|
|
|
std::ostringstream row_limit_string;
|
|
|
|
row_limit_string << "rownum < " << row_limit_;
|
2010-11-16 23:32:07 +01:00
|
|
|
if (boost::algorithm::ifind_first(query, "WHERE"))
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
2012-04-03 04:20:09 +02:00
|
|
|
boost::algorithm::ireplace_first(query, "WHERE", row_limit_string.str() + " AND ");
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
2011-11-15 14:04:58 +01:00
|
|
|
else if (boost::algorithm::ifind_first(query, table_name_))
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
2012-04-03 04:20:09 +02:00
|
|
|
boost::algorithm::ireplace_first(query, table_name_, table_name_ + " " + row_limit_string.str());
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(occi) << "occi_datasource: Cannot determine where to add the row limit declaration";
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
2009-02-03 18:06:23 +01:00
|
|
|
}
|
2010-11-15 10:25:06 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
s << query;
|
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(occi) << "occi_datasource: " << s.str();
|
2010-11-16 18:31:13 +01:00
|
|
|
|
2011-05-17 01:41:34 +02:00
|
|
|
return boost::make_shared<occi_featureset>(pool_,
|
2011-10-22 03:33:03 +02:00
|
|
|
conn_,
|
2012-01-17 20:22:21 +01:00
|
|
|
ctx,
|
2011-10-22 03:33:03 +02:00
|
|
|
s.str(),
|
2012-02-02 02:37:35 +01:00
|
|
|
desc_.get_encoding(),
|
2011-10-22 03:33:03 +02:00
|
|
|
use_connection_pool_,
|
2012-04-08 02:20:56 +02:00
|
|
|
use_wkb_,
|
2012-01-17 20:22:21 +01:00
|
|
|
row_prefetch_);
|
2009-02-03 18:06:23 +01:00
|
|
|
}
|
|
|
|
|
2012-09-28 15:12:10 +02:00
|
|
|
featureset_ptr occi_datasource::features_at_point(coord2d const& pt, double tol) const
|
2009-02-03 18:06:23 +01:00
|
|
|
{
|
2011-10-22 03:33:03 +02:00
|
|
|
if (! is_bound_) bind();
|
2010-10-24 08:34:18 +02:00
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_STATS
|
|
|
|
mapnik::progress_timer __stats__(std::clog, "occi_datasource::features_at_point");
|
|
|
|
#endif
|
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
std::ostringstream s;
|
2012-04-08 02:20:56 +02:00
|
|
|
s << "SELECT ";
|
|
|
|
if (use_wkb_)
|
|
|
|
{
|
|
|
|
s << "SDO_UTIL.TO_WKBGEOMETRY(" << geometry_field_ << ")";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s << geometry_field_;
|
|
|
|
}
|
2010-11-16 18:31:13 +01:00
|
|
|
std::vector<attribute_descriptor>::const_iterator itr = desc_.get_descriptors().begin();
|
|
|
|
std::vector<attribute_descriptor>::const_iterator end = desc_.get_descriptors().end();
|
2012-01-19 23:36:27 +01:00
|
|
|
mapnik::context_ptr ctx = boost::make_shared<mapnik::context_type>();
|
2010-11-16 18:31:13 +01:00
|
|
|
while (itr != end)
|
2009-02-05 12:17:27 +01:00
|
|
|
{
|
2010-11-22 12:40:08 +01:00
|
|
|
s << ", " << itr->get_name();
|
2012-01-17 20:22:21 +01:00
|
|
|
ctx->push(itr->get_name());
|
2010-11-16 18:31:13 +01:00
|
|
|
++itr;
|
|
|
|
}
|
2009-02-24 10:58:31 +01:00
|
|
|
|
2010-11-16 23:32:07 +01:00
|
|
|
s << " FROM ";
|
2009-02-24 10:58:31 +01:00
|
|
|
|
2011-10-22 03:33:03 +02:00
|
|
|
std::string query(table_);
|
2009-02-24 10:58:31 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
if (use_spatial_index_)
|
|
|
|
{
|
2009-02-24 10:58:31 +01:00
|
|
|
std::ostringstream spatial_sql;
|
|
|
|
spatial_sql << std::setprecision(16);
|
2010-11-16 23:32:07 +01:00
|
|
|
spatial_sql << " WHERE SDO_FILTER(" << geometry_field_ << ",";
|
|
|
|
spatial_sql << " MDSYS.SDO_GEOMETRY(" << SDO_GTYPE_2DPOINT << "," << srid_ << ",NULL,";
|
|
|
|
spatial_sql << " MDSYS.SDO_ELEM_INFO_ARRAY(1," << SDO_ETYPE_POINT << "," << SDO_INTERPRETATION_POINT << "),";
|
|
|
|
spatial_sql << " MDSYS.SDO_ORDINATE_ARRAY(";
|
2010-11-15 10:25:06 +01:00
|
|
|
spatial_sql << pt.x << "," << pt.y << ")), 'querytype=WINDOW') = 'TRUE'";
|
2009-02-24 10:58:31 +01:00
|
|
|
|
2010-11-16 23:32:07 +01:00
|
|
|
if (boost::algorithm::ifind_first(query, "WHERE"))
|
2009-02-24 10:58:31 +01:00
|
|
|
{
|
2010-11-16 23:32:07 +01:00
|
|
|
boost::algorithm::ireplace_first(query, "WHERE", spatial_sql.str() + " AND ");
|
2009-02-24 10:58:31 +01:00
|
|
|
}
|
2011-11-15 14:04:58 +01:00
|
|
|
else if (boost::algorithm::ifind_first(query, table_name_))
|
2009-02-24 10:58:31 +01:00
|
|
|
{
|
2011-11-15 14:04:58 +01:00
|
|
|
boost::algorithm::ireplace_first(query, table_name_, table_name_ + " " + spatial_sql.str());
|
2009-02-24 10:58:31 +01:00
|
|
|
}
|
2010-11-16 18:31:13 +01:00
|
|
|
else
|
2009-04-28 16:06:35 +02:00
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(occi) << "occi_datasource: Cannot determine where to add the spatial filter declaration";
|
2009-04-28 16:06:35 +02:00
|
|
|
}
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
2010-11-15 10:25:06 +01:00
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
if (row_limit_ > 0)
|
|
|
|
{
|
2012-04-03 04:20:09 +02:00
|
|
|
std::ostringstream row_limit_string;
|
|
|
|
row_limit_string << "rownum < " << row_limit_;
|
2010-11-16 23:32:07 +01:00
|
|
|
if (boost::algorithm::ifind_first(query, "WHERE"))
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
2012-04-03 04:20:09 +02:00
|
|
|
boost::algorithm::ireplace_first(query, "WHERE", row_limit_string.str() + " AND ");
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
2011-11-15 14:04:58 +01:00
|
|
|
else if (boost::algorithm::ifind_first(query, table_name_))
|
2010-11-16 18:31:13 +01:00
|
|
|
{
|
2012-04-03 04:20:09 +02:00
|
|
|
boost::algorithm::ireplace_first(query, table_name_, table_name_ + " " + row_limit_string.str());
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(occi) << "occi_datasource: Cannot determine where to add the row limit declaration";
|
2010-11-16 18:31:13 +01:00
|
|
|
}
|
2009-02-05 12:17:27 +01:00
|
|
|
}
|
|
|
|
|
2010-11-16 18:31:13 +01:00
|
|
|
s << query;
|
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_DEBUG(occi) << "occi_datasource: " << s.str();
|
2010-11-16 18:31:13 +01:00
|
|
|
|
2011-05-17 01:41:34 +02:00
|
|
|
return boost::make_shared<occi_featureset>(pool_,
|
2011-10-22 03:33:03 +02:00
|
|
|
conn_,
|
2012-01-17 20:22:21 +01:00
|
|
|
ctx,
|
2011-10-22 03:33:03 +02:00
|
|
|
s.str(),
|
|
|
|
desc_.get_encoding(),
|
|
|
|
use_connection_pool_,
|
2012-04-08 02:20:56 +02:00
|
|
|
use_wkb_,
|
2012-01-17 20:22:21 +01:00
|
|
|
row_prefetch_);
|
2009-02-03 18:06:23 +01:00
|
|
|
}
|