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)
|
|
|
|
*
|
2011-10-22 14:50:24 +02:00
|
|
|
* Copyright (C) 2011 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-05-17 01:41:34 +02:00
|
|
|
// mapnik
|
2009-02-09 20:43:57 +01:00
|
|
|
#include <mapnik/global.hpp>
|
|
|
|
#include <mapnik/datasource.hpp>
|
2009-12-16 21:02:06 +01:00
|
|
|
#include <mapnik/box2d.hpp>
|
2009-02-09 20:43:57 +01:00
|
|
|
#include <mapnik/geometry.hpp>
|
|
|
|
#include <mapnik/feature.hpp>
|
|
|
|
#include <mapnik/feature_layer_desc.hpp>
|
|
|
|
#include <mapnik/wkb.hpp>
|
|
|
|
#include <mapnik/unicode.hpp>
|
2011-05-17 01:41:34 +02:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2009-02-09 20:43:57 +01:00
|
|
|
|
2010-05-30 05:23:59 +02:00
|
|
|
// ogr
|
2009-02-09 20:43:57 +01:00
|
|
|
#include "sqlite_featureset.hpp"
|
2011-11-04 00:51:37 +01:00
|
|
|
#include "sqlite_utils.hpp"
|
2009-02-09 20:43:57 +01:00
|
|
|
|
|
|
|
using mapnik::query;
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::box2d;
|
2009-02-09 20:43:57 +01:00
|
|
|
using mapnik::CoordTransform;
|
|
|
|
using mapnik::Feature;
|
|
|
|
using mapnik::feature_ptr;
|
|
|
|
using mapnik::geometry_utils;
|
|
|
|
using mapnik::transcoder;
|
2011-05-17 01:41:34 +02:00
|
|
|
using mapnik::feature_factory;
|
2009-02-09 20:43:57 +01:00
|
|
|
|
|
|
|
sqlite_featureset::sqlite_featureset(boost::shared_ptr<sqlite_resultset> rs,
|
|
|
|
std::string const& encoding,
|
2009-02-23 16:00:25 +01:00
|
|
|
mapnik::wkbFormat format,
|
2011-08-24 02:32:00 +02:00
|
|
|
bool using_subquery)
|
2011-11-14 04:37:50 +01:00
|
|
|
: rs_(rs),
|
|
|
|
tr_(new transcoder(encoding)),
|
|
|
|
format_(format),
|
2012-01-19 22:34:33 +01:00
|
|
|
using_subquery_(using_subquery),
|
|
|
|
ctx_(boost::make_shared<mapnik::context>())
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-22 14:50:24 +02:00
|
|
|
sqlite_featureset::~sqlite_featureset()
|
|
|
|
{
|
|
|
|
}
|
2009-02-09 20:43:57 +01:00
|
|
|
|
|
|
|
feature_ptr sqlite_featureset::next()
|
|
|
|
{
|
|
|
|
if (rs_->is_valid () && rs_->step_next ())
|
|
|
|
{
|
2009-02-10 20:09:16 +01:00
|
|
|
int size;
|
2011-10-22 14:50:24 +02:00
|
|
|
const char* data = (const char*) rs_->column_blob(0, size);
|
2011-10-18 22:19:03 +02:00
|
|
|
if (! data)
|
|
|
|
{
|
2010-10-01 22:16:49 +02:00
|
|
|
return feature_ptr();
|
2011-10-18 22:19:03 +02:00
|
|
|
}
|
|
|
|
|
2012-01-19 22:34:33 +01:00
|
|
|
feature_ptr feature(feature_factory::create(ctx_,rs_->column_integer(1)));
|
2011-12-06 13:53:16 +01:00
|
|
|
geometry_utils::from_wkb(feature->paths(), data, size, format_);
|
|
|
|
|
2011-10-22 14:50:24 +02:00
|
|
|
for (int i = 2; i < rs_->column_count(); ++i)
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
2011-10-22 14:50:24 +02:00
|
|
|
const int type_oid = rs_->column_type(i);
|
2011-08-24 02:32:00 +02:00
|
|
|
const char* fld_name = rs_->column_name(i);
|
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
if (! fld_name)
|
2011-09-02 02:56:42 +02:00
|
|
|
continue;
|
|
|
|
|
2011-10-18 23:32:25 +02:00
|
|
|
std::string fld_name_str(fld_name);
|
|
|
|
|
|
|
|
// subqueries in sqlite lead to field double quoting which we need to strip
|
|
|
|
if (using_subquery_)
|
|
|
|
{
|
|
|
|
sqlite_utils::dequote(fld_name_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type_oid)
|
2011-08-24 02:32:00 +02:00
|
|
|
{
|
2011-10-18 23:32:25 +02:00
|
|
|
case SQLITE_INTEGER:
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
2012-01-19 22:34:33 +01:00
|
|
|
feature->put(fld_name_str, rs_->column_integer(i));
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-10-18 22:19:03 +02:00
|
|
|
|
2011-10-18 23:32:25 +02:00
|
|
|
case SQLITE_FLOAT:
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
2012-01-19 22:34:33 +01:00
|
|
|
feature->put(fld_name_str, rs_->column_double(i));
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-10-18 22:19:03 +02:00
|
|
|
|
2011-10-18 23:32:25 +02:00
|
|
|
case SQLITE_TEXT:
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
|
|
|
int text_size;
|
|
|
|
const char * data = rs_->column_text(i, text_size);
|
|
|
|
UnicodeString ustr = tr_->transcode(data, text_size);
|
2012-01-19 22:34:33 +01:00
|
|
|
feature->put(fld_name_str, ustr);
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-10-18 23:32:25 +02:00
|
|
|
|
|
|
|
case SQLITE_NULL:
|
2011-11-14 04:37:50 +01:00
|
|
|
{
|
2012-01-19 22:34:33 +01:00
|
|
|
feature->put(fld_name_str, mapnik::value_null());
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-10-18 23:32:25 +02:00
|
|
|
|
|
|
|
case SQLITE_BLOB:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-11-14 04:37:50 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-18 23:32:25 +02:00
|
|
|
std::clog << "Sqlite Plugin: field " << fld_name_str
|
|
|
|
<< " unhandled type_oid=" << type_oid << std::endl;
|
2011-11-14 04:37:50 +01:00
|
|
|
#endif
|
2011-10-18 23:32:25 +02:00
|
|
|
break;
|
2011-08-24 02:32:00 +02:00
|
|
|
}
|
2009-02-09 20:43:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
|
|
|
|
return feature_ptr();
|
|
|
|
}
|