2006-12-06 21:26:59 +01:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2006-12-06 21:26:59 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2015-06-16 12:49:16 +02:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
2006-12-06 21:26:59 +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-09 15:52:06 +02:00
|
|
|
// mapnik
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2012-07-25 04:08:26 +02:00
|
|
|
#include <mapnik/query.hpp>
|
|
|
|
#include <mapnik/box2d.hpp>
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <mapnik/memory_datasource.hpp>
|
2006-12-06 21:26:59 +01:00
|
|
|
#include <mapnik/memory_featureset.hpp>
|
2014-05-30 11:16:49 +02:00
|
|
|
#include <mapnik/boolean.hpp>
|
2015-03-24 22:46:26 +01:00
|
|
|
#include <mapnik/geometry_envelope.hpp>
|
2012-03-10 01:20:50 +01:00
|
|
|
|
2011-05-09 15:52:06 +02:00
|
|
|
// stl
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <algorithm>
|
2006-12-06 21:26:59 +01:00
|
|
|
|
2014-05-30 11:16:49 +02:00
|
|
|
using mapnik::datasource;
|
|
|
|
using mapnik::parameters;
|
|
|
|
|
|
|
|
DATASOURCE_PLUGIN(mapnik::memory_datasource)
|
|
|
|
|
2006-12-06 21:26:59 +01:00
|
|
|
namespace mapnik {
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
struct accumulate_extent
|
|
|
|
{
|
|
|
|
accumulate_extent(box2d<double> & ext)
|
|
|
|
: ext_(ext),first_(true) {}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2015-02-06 16:45:51 +01:00
|
|
|
void operator() (feature_ptr const& feat)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2015-03-24 22:46:26 +01:00
|
|
|
auto const& geom = feat->get_geometry();
|
|
|
|
auto bbox = geometry::envelope(geom);
|
|
|
|
if ( first_ )
|
2006-12-06 21:26:59 +01:00
|
|
|
{
|
2015-03-24 22:46:26 +01:00
|
|
|
first_ = false;
|
|
|
|
ext_ = bbox;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ext_.expand_to_include(bbox);
|
2006-12-06 21:26:59 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
box2d<double> & ext_;
|
|
|
|
bool first_;
|
|
|
|
};
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2014-05-30 11:16:49 +02:00
|
|
|
const char * memory_datasource::name()
|
|
|
|
{
|
|
|
|
return "memory";
|
|
|
|
}
|
|
|
|
|
|
|
|
memory_datasource::memory_datasource(parameters const& params)
|
|
|
|
: datasource(params),
|
2014-06-26 11:51:24 +02:00
|
|
|
desc_(memory_datasource::name(),
|
|
|
|
*params.get<std::string>("encoding","utf-8")),
|
2014-05-30 11:16:49 +02:00
|
|
|
type_(datasource::Vector),
|
2016-01-14 15:36:12 +01:00
|
|
|
bbox_check_(*params.get<boolean_type>("bbox_check", true)),
|
|
|
|
type_set_(false) {}
|
2011-04-28 19:36:36 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
memory_datasource::~memory_datasource() {}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void memory_datasource::push(feature_ptr feature)
|
|
|
|
{
|
2011-04-28 19:36:36 +02:00
|
|
|
// TODO - collect attribute descriptors?
|
|
|
|
//desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Integer));
|
2016-01-14 15:36:12 +01:00
|
|
|
if (feature->get_raster())
|
|
|
|
{
|
|
|
|
// if a feature has a raster_ptr set it must be of raster type.
|
|
|
|
if (!type_set_)
|
|
|
|
{
|
|
|
|
type_ = datasource::Raster;
|
|
|
|
type_set_ = true;
|
|
|
|
}
|
|
|
|
else if (type_ == datasource::Vector)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Can not add a raster feature to a memory datasource that contains vectors");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!type_set_)
|
|
|
|
{
|
|
|
|
type_set_ = true;
|
|
|
|
}
|
|
|
|
else if (type_ == datasource::Raster)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Can not add a vector feature to a memory datasource that contains rasters");
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
features_.push_back(feature);
|
2015-07-17 20:16:21 +02:00
|
|
|
dirty_extent_ = true;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2012-01-17 07:09:46 +01:00
|
|
|
datasource::datasource_t memory_datasource::type() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-10-27 03:18:35 +02:00
|
|
|
return type_;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
featureset_ptr memory_datasource::features(const query& q) const
|
|
|
|
{
|
2013-09-20 15:00:11 +02:00
|
|
|
return std::make_shared<memory_featureset>(q.get_bbox(),*this,bbox_check_);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2006-12-06 21:26:59 +01:00
|
|
|
|
|
|
|
|
2012-09-28 15:12:10 +02:00
|
|
|
featureset_ptr memory_datasource::features_at_point(coord2d const& pt, double tol) const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
box2d<double> box = box2d<double>(pt.x, pt.y, pt.x, pt.y);
|
2013-05-22 12:07:55 +02:00
|
|
|
box.pad(tol);
|
2012-04-09 21:41:56 +02:00
|
|
|
MAPNIK_LOG_DEBUG(memory_datasource) << "memory_datasource: Box=" << box << ", Point x=" << pt.x << ",y=" << pt.y;
|
2013-09-20 15:00:11 +02:00
|
|
|
return std::make_shared<memory_featureset>(box,*this);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2013-02-19 20:36:13 +01:00
|
|
|
void memory_datasource::set_envelope(box2d<double> const& box)
|
|
|
|
{
|
|
|
|
extent_ = box;
|
2016-01-21 22:20:50 +01:00
|
|
|
dirty_extent_ = false;
|
2013-02-19 20:36:13 +01:00
|
|
|
}
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
box2d<double> memory_datasource::envelope() const
|
|
|
|
{
|
2015-07-17 20:16:21 +02:00
|
|
|
if (!extent_.valid() || dirty_extent_)
|
2013-02-19 20:36:13 +01:00
|
|
|
{
|
|
|
|
accumulate_extent func(extent_);
|
|
|
|
std::for_each(features_.begin(),features_.end(),func);
|
2015-07-17 20:16:21 +02:00
|
|
|
dirty_extent_ = false;
|
2013-02-19 20:36:13 +01:00
|
|
|
}
|
|
|
|
return extent_;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-01-15 07:35:40 +01:00
|
|
|
|
2015-03-24 12:13:31 +01:00
|
|
|
boost::optional<datasource_geometry_t> memory_datasource::get_geometry_type() const
|
2012-01-15 07:35:40 +01:00
|
|
|
{
|
2012-01-17 07:09:46 +01:00
|
|
|
// TODO - detect this?
|
2015-03-24 12:13:31 +01:00
|
|
|
return datasource_geometry_t::Collection;
|
2012-01-15 07:35:40 +01:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
layer_descriptor memory_datasource::get_descriptor() const
|
|
|
|
{
|
2011-04-28 19:36:36 +02:00
|
|
|
return desc_;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
size_t memory_datasource::size() const
|
|
|
|
{
|
|
|
|
return features_.size();
|
|
|
|
}
|
2010-11-03 14:19:15 +01:00
|
|
|
|
2011-10-13 01:30:18 +02:00
|
|
|
void memory_datasource::clear()
|
|
|
|
{
|
|
|
|
features_.clear();
|
|
|
|
}
|
|
|
|
|
2006-12-06 21:26:59 +01:00
|
|
|
}
|