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