2012-06-26 15:44:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2014-04-24 19:54:18 +00:00
|
|
|
#if defined(GRID_RENDERER)
|
|
|
|
|
2012-06-26 15:44:15 +00:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/grid/grid.hpp>
|
2013-01-04 02:06:07 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
|
|
|
#include <mapnik/feature.hpp>
|
|
|
|
#include <mapnik/value.hpp>
|
|
|
|
|
|
|
|
// boost
|
2012-06-26 15:44:15 +00:00
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
|
2012-07-10 00:32:36 +00:00
|
|
|
template <typename T>
|
|
|
|
const typename hit_grid<T>::value_type hit_grid<T>::base_mask = std::numeric_limits<T>::min();
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
hit_grid<T>::hit_grid(int width, int height, std::string const& key, unsigned int resolution)
|
|
|
|
: width_(width),
|
|
|
|
height_(height),
|
|
|
|
key_(key),
|
|
|
|
data_(width,height),
|
|
|
|
resolution_(resolution),
|
|
|
|
id_name_("__id__"),
|
|
|
|
painted_(false),
|
|
|
|
names_(),
|
|
|
|
f_keys_(),
|
|
|
|
features_(),
|
2013-09-20 13:00:11 +00:00
|
|
|
ctx_(std::make_shared<mapnik::context_type>())
|
2012-07-10 00:32:36 +00:00
|
|
|
{
|
|
|
|
f_keys_[base_mask] = "";
|
|
|
|
data_.set(base_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2012-07-12 09:21:57 +00:00
|
|
|
hit_grid<T>::hit_grid(hit_grid<T> const& rhs)
|
2012-07-10 00:32:36 +00:00
|
|
|
: width_(rhs.width_),
|
|
|
|
height_(rhs.height_),
|
|
|
|
key_(rhs.key_),
|
|
|
|
data_(rhs.data_),
|
|
|
|
resolution_(rhs.resolution_),
|
|
|
|
id_name_("__id__"),
|
|
|
|
painted_(rhs.painted_),
|
|
|
|
names_(rhs.names_),
|
|
|
|
f_keys_(rhs.f_keys_),
|
|
|
|
features_(rhs.features_),
|
|
|
|
ctx_(rhs.ctx_)
|
2014-06-02 16:09:12 +00:00
|
|
|
{
|
|
|
|
f_keys_[base_mask] = "";
|
|
|
|
data_.set(base_mask);
|
|
|
|
}
|
2012-07-10 00:32:36 +00:00
|
|
|
|
2012-11-12 05:35:50 +00:00
|
|
|
template <typename T>
|
|
|
|
void hit_grid<T>::clear()
|
|
|
|
{
|
|
|
|
painted_ = false;
|
|
|
|
f_keys_.clear();
|
|
|
|
features_.clear();
|
|
|
|
names_.clear();
|
|
|
|
f_keys_[base_mask] = "";
|
|
|
|
data_.set(base_mask);
|
2013-09-20 13:00:11 +00:00
|
|
|
ctx_ = std::make_shared<mapnik::context_type>();
|
2012-11-12 05:35:50 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 00:32:36 +00:00
|
|
|
template <typename T>
|
2014-06-02 16:09:12 +00:00
|
|
|
void hit_grid<T>::add_feature(mapnik::feature_impl const& feature)
|
2012-07-10 00:32:36 +00:00
|
|
|
{
|
2012-12-20 03:17:42 +00:00
|
|
|
value_type feature_id = feature.id();
|
2012-07-10 00:32:36 +00:00
|
|
|
// avoid adding duplicate features (e.g. in the case of both a line symbolizer and a polygon symbolizer)
|
|
|
|
typename feature_key_type::const_iterator feature_pos = f_keys_.find(feature_id);
|
|
|
|
if (feature_pos != f_keys_.end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-02 16:09:12 +00:00
|
|
|
if (ctx_->size() == 0)
|
|
|
|
{
|
2012-07-10 01:40:41 +00:00
|
|
|
context_type::map_type::const_iterator itr = feature.context()->begin();
|
|
|
|
context_type::map_type::const_iterator end = feature.context()->end();
|
2012-07-10 00:32:36 +00:00
|
|
|
for ( ;itr!=end; ++itr)
|
|
|
|
{
|
2012-07-10 01:40:41 +00:00
|
|
|
ctx_->add(itr->first,itr->second);
|
2012-07-10 00:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// NOTE: currently lookup keys must be strings,
|
|
|
|
// but this should be revisited
|
|
|
|
lookup_type lookup_value;
|
|
|
|
if (key_ == id_name_)
|
|
|
|
{
|
|
|
|
mapnik::util::to_string(lookup_value,feature_id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (feature.has_key(key_))
|
|
|
|
{
|
|
|
|
lookup_value = feature.get(key_).to_string();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_DEBUG(grid) << "hit_grid: Should not get here: key '" << key_ << "' not found in feature properties";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lookup_value.empty())
|
|
|
|
{
|
|
|
|
// TODO - consider shortcutting f_keys if feature_id == lookup_value
|
|
|
|
// create a mapping between the pixel id and the feature key
|
|
|
|
f_keys_.insert(std::make_pair(feature_id,lookup_value));
|
|
|
|
// if extra fields have been supplied, push them into grid memory
|
|
|
|
if (!names_.empty())
|
|
|
|
{
|
|
|
|
// it is ~ 2x faster to copy feature attributes compared
|
|
|
|
// to building up a in-memory cache of feature_ptrs
|
|
|
|
// https://github.com/mapnik/mapnik/issues/1198
|
|
|
|
mapnik::feature_ptr feature2(mapnik::feature_factory::create(ctx_,feature_id));
|
|
|
|
feature2->set_data(feature.get_data());
|
|
|
|
features_.insert(std::make_pair(lookup_value,feature2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_DEBUG(grid) << "hit_grid: Warning - key '" << key_ << "' was blank for " << feature;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-20 03:24:52 +00:00
|
|
|
template class hit_grid<mapnik::value_integer>;
|
2012-06-26 15:44:15 +00:00
|
|
|
|
|
|
|
}
|
2014-04-24 19:54:18 +00:00
|
|
|
|
2014-04-24 21:50:55 +00:00
|
|
|
#endif
|