2006-12-06 21:26:59 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Artem Pavlenko
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
//$Id$
|
2011-05-09 15:52:06 +02:00
|
|
|
// mapnik
|
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>
|
2011-05-09 15:52:06 +02:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
|
|
|
// stl
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <algorithm>
|
2006-12-06 21:26:59 +01:00
|
|
|
|
|
|
|
namespace mapnik {
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
struct accumulate_extent
|
|
|
|
{
|
|
|
|
accumulate_extent(box2d<double> & ext)
|
|
|
|
: ext_(ext),first_(true) {}
|
2006-12-06 21:26:59 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void operator() (feature_ptr feat)
|
|
|
|
{
|
|
|
|
for (unsigned i=0;i<feat->num_geometries();++i)
|
2006-12-06 21:26:59 +01:00
|
|
|
{
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type & geom = feat->get_geometry(i);
|
2010-06-02 13:03:30 +02:00
|
|
|
if ( first_ )
|
|
|
|
{
|
|
|
|
first_ = false;
|
|
|
|
ext_ = geom.envelope();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ext_.expand_to_include(geom.envelope());
|
|
|
|
}
|
2006-12-06 21:26:59 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2006-12-06 21:26:59 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
box2d<double> & ext_;
|
|
|
|
bool first_;
|
|
|
|
};
|
2006-12-06 21:26:59 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
memory_datasource::memory_datasource()
|
2011-04-28 19:36:36 +02:00
|
|
|
: datasource(parameters()),
|
|
|
|
desc_("in-memory datasource","utf-8") {}
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
memory_datasource::~memory_datasource() {}
|
2006-12-06 21:26:59 +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));
|
2010-06-02 13:03:30 +02:00
|
|
|
features_.push_back(feature);
|
|
|
|
}
|
2006-12-06 21:26:59 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
int memory_datasource::type() const
|
|
|
|
{
|
|
|
|
return datasource::Vector;
|
|
|
|
}
|
2006-12-06 21:26:59 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
featureset_ptr memory_datasource::features(const query& q) const
|
|
|
|
{
|
|
|
|
return featureset_ptr(new memory_featureset(q.get_bbox(),*this));
|
|
|
|
}
|
2006-12-06 21:26:59 +01:00
|
|
|
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
featureset_ptr memory_datasource::features_at_point(coord2d const& pt) const
|
|
|
|
{
|
|
|
|
box2d<double> box = box2d<double>(pt.x, pt.y, pt.x, pt.y);
|
2010-03-19 17:16:15 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "box=" << box << ", pt x=" << pt.x << ", y=" << pt.y << "\n";
|
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
return featureset_ptr(new memory_featureset(box,*this));
|
|
|
|
}
|
2006-12-06 21:26:59 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
box2d<double> memory_datasource::envelope() const
|
|
|
|
{
|
|
|
|
box2d<double> ext;
|
|
|
|
accumulate_extent func(ext);
|
|
|
|
std::for_each(features_.begin(),features_.end(),func);
|
|
|
|
return ext;
|
|
|
|
}
|
2006-12-06 21:26:59 +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
|
|
|
}
|
2006-12-06 21:26:59 +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
|
|
|
|
|
|
|
// point_datasource
|
|
|
|
|
|
|
|
void point_datasource::add_point(double x, double y, const char* key, const char* value)
|
|
|
|
{
|
2011-04-29 21:25:00 +02:00
|
|
|
feature_ptr feature(feature_factory::create(feature_id_));
|
|
|
|
++feature_id_;
|
2010-11-03 14:19:15 +01:00
|
|
|
geometry_type * pt = new geometry_type(Point);
|
|
|
|
pt->move_to(x,y);
|
|
|
|
feature->add_geometry(pt);
|
|
|
|
transcoder tr("utf-8");
|
|
|
|
(*feature)[key] = tr.transcode(value);
|
|
|
|
this->push(feature);
|
2011-05-09 15:52:06 +02:00
|
|
|
}
|
|
|
|
|
2006-12-06 21:26:59 +01:00
|
|
|
}
|