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)
|
|
|
|
*
|
2016-12-20 17:57:22 +01:00
|
|
|
* Copyright (C) 2016 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>
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2017-01-26 09:51:37 +01:00
|
|
|
#include <mapnik/geometry/box2d.hpp>
|
2009-02-09 20:43:57 +01:00
|
|
|
#include <mapnik/feature.hpp>
|
|
|
|
#include <mapnik/feature_layer_desc.hpp>
|
|
|
|
#include <mapnik/wkb.hpp>
|
|
|
|
#include <mapnik/unicode.hpp>
|
2016-08-02 16:34:59 +02:00
|
|
|
#include <mapnik/value/types.hpp>
|
2011-05-17 01:41:34 +02:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2016-08-02 15:43:02 +02:00
|
|
|
#include <mapnik/geometry/is_empty.hpp>
|
|
|
|
#include <mapnik/geometry/envelope.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::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
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
sqlite_featureset::sqlite_featureset(std::shared_ptr<sqlite_resultset> rs,
|
2012-01-24 08:51:31 +01:00
|
|
|
mapnik::context_ptr const& ctx,
|
2009-02-09 20:43:57 +01:00
|
|
|
std::string const& encoding,
|
2012-07-02 19:13:12 +02:00
|
|
|
mapnik::box2d<double> const& bbox,
|
2009-02-23 16:00:25 +01:00
|
|
|
mapnik::wkbFormat format,
|
2012-07-02 19:13:12 +02:00
|
|
|
bool spatial_index,
|
2011-08-24 02:32:00 +02:00
|
|
|
bool using_subquery)
|
2011-11-14 04:37:50 +01:00
|
|
|
: rs_(rs),
|
2012-07-02 19:13:12 +02:00
|
|
|
ctx_(ctx),
|
2011-11-14 04:37:50 +01:00
|
|
|
tr_(new transcoder(encoding)),
|
2012-07-02 19:13:12 +02:00
|
|
|
bbox_(bbox),
|
2011-11-14 04:37:50 +01:00
|
|
|
format_(format),
|
2012-07-02 19:13:12 +02:00
|
|
|
spatial_index_(spatial_index),
|
|
|
|
using_subquery_(using_subquery)
|
|
|
|
{}
|
2009-02-09 20:43:57 +01:00
|
|
|
|
2012-07-02 19:13:12 +02:00
|
|
|
sqlite_featureset::~sqlite_featureset() {}
|
2009-02-09 20:43:57 +01:00
|
|
|
|
|
|
|
feature_ptr sqlite_featureset::next()
|
|
|
|
{
|
2012-07-02 19:13:12 +02:00
|
|
|
while (rs_->is_valid () && rs_->step_next ())
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
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);
|
2012-07-02 19:13:12 +02:00
|
|
|
if (data == 0)
|
2011-10-18 22:19:03 +02:00
|
|
|
{
|
2010-10-01 22:16:49 +02:00
|
|
|
return feature_ptr();
|
2011-10-18 22:19:03 +02:00
|
|
|
}
|
|
|
|
|
2013-04-18 00:34:21 +02:00
|
|
|
// null feature id is not acceptable
|
|
|
|
if (rs_->column_type(1) == SQLITE_NULL)
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(postgis) << "sqlite_featureset: null value encountered for key_field";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-12-20 04:24:52 +01:00
|
|
|
feature_ptr feature = feature_factory::create(ctx_,rs_->column_integer64(1));
|
2016-03-15 13:01:54 +01:00
|
|
|
mapnik::geometry::geometry<double> geom = 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
|
|
|
{
|
2012-07-21 00:09:01 +02:00
|
|
|
continue;
|
2015-03-22 22:58:14 +01:00
|
|
|
}
|
2012-02-02 02:37:35 +01:00
|
|
|
|
2012-07-02 19:13:12 +02:00
|
|
|
if (!spatial_index_)
|
|
|
|
{
|
|
|
|
// we are not using r-tree index, check if feature intersects bounding box
|
2015-03-24 14:21:28 +01:00
|
|
|
box2d<double> bbox = mapnik::geometry::envelope(geom);
|
2015-03-22 22:58:14 +01:00
|
|
|
if (!bbox_.intersects(bbox))
|
2012-07-02 19:13:12 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-03-22 22:58:14 +01:00
|
|
|
feature->set_geometry(std::move(geom));
|
2012-07-02 19:13:12 +02:00
|
|
|
|
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-12-20 04:24:52 +01:00
|
|
|
feature->put<mapnik::value_integer>(fld_name_str, rs_->column_integer64(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
|
|
|
{
|
2012-09-04 23:35:03 +02:00
|
|
|
int text_col_size;
|
2013-01-04 03:01:36 +01:00
|
|
|
const char * text_data = rs_->column_text(i, text_col_size);
|
2013-08-14 00:52:04 +02:00
|
|
|
feature->put(fld_name_str, tr_->transcode(text_data, text_col_size));
|
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
|
|
|
{
|
2013-04-12 01:05:29 +02:00
|
|
|
// NOTE: we intentionally do not store null here
|
|
|
|
// since it is equivalent to the attribute not existing
|
2011-11-14 04:37:50 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-10-18 23:32:25 +02:00
|
|
|
|
|
|
|
case SQLITE_BLOB:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-04-09 03:00:51 +02:00
|
|
|
MAPNIK_LOG_WARN(sqlite) << "sqlite_featureset: Field=" << fld_name_str << " unhandled type_oid=" << type_oid;
|
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();
|
|
|
|
}
|