2008-03-01 11:49:37 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2008-03-01 11:49:37 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2014-11-20 14:25:50 +00:00
|
|
|
* Copyright (C) 2014 Artem Pavlenko
|
2008-03-01 11:49:37 +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
|
2014-06-12 14:14:05 +00:00
|
|
|
#include <mapnik/make_unique.hpp>
|
2015-03-24 12:32:05 +00:00
|
|
|
#include <mapnik/geometry.hpp>
|
2013-01-04 17:23:06 +00:00
|
|
|
#include <mapnik/feature.hpp>
|
2011-05-16 23:41:34 +00:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2012-08-05 13:34:22 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
2013-01-04 17:23:06 +00:00
|
|
|
#include <mapnik/unicode.hpp>
|
|
|
|
|
|
|
|
// boost
|
2011-05-16 23:41:34 +00:00
|
|
|
|
|
|
|
#include "osm_featureset.hpp"
|
|
|
|
|
2008-03-01 11:49:37 +00:00
|
|
|
using mapnik::feature_ptr;
|
2011-05-16 23:41:34 +00:00
|
|
|
using mapnik::feature_factory;
|
2008-03-01 11:49:37 +00:00
|
|
|
|
|
|
|
template <typename filterT>
|
2011-11-14 03:37:50 +00:00
|
|
|
osm_featureset<filterT>::osm_featureset(const filterT& filter,
|
2011-10-22 14:04:05 +00:00
|
|
|
osm_dataset* dataset,
|
2011-11-14 03:37:50 +00:00
|
|
|
const std::set<std::string>&
|
2010-11-03 13:19:15 +00:00
|
|
|
attribute_names,
|
|
|
|
std::string const& encoding)
|
2011-11-14 03:37:50 +00:00
|
|
|
: filter_(filter),
|
|
|
|
query_ext_(),
|
|
|
|
tr_(new transcoder(encoding)),
|
|
|
|
dataset_ (dataset),
|
2012-01-19 21:34:33 +00:00
|
|
|
attribute_names_ (attribute_names),
|
2013-09-20 13:00:11 +00:00
|
|
|
ctx_(std::make_shared<mapnik::context_type>())
|
2008-03-01 11:49:37 +00:00
|
|
|
{
|
2010-11-03 13:19:15 +00:00
|
|
|
dataset_->rewind();
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename filterT>
|
|
|
|
feature_ptr osm_featureset<filterT>::next()
|
|
|
|
{
|
2010-11-03 13:19:15 +00:00
|
|
|
feature_ptr feature;
|
2011-10-22 14:04:05 +00:00
|
|
|
|
|
|
|
osm_item* cur_item = dataset_->next_item();
|
2012-03-02 00:01:13 +00:00
|
|
|
if (!cur_item) return feature_ptr();
|
|
|
|
if (dataset_->current_item_is_node())
|
2010-11-03 13:19:15 +00:00
|
|
|
{
|
2012-10-17 00:46:24 +00:00
|
|
|
feature = feature_factory::create(ctx_, cur_item->id);
|
2012-03-02 00:01:13 +00:00
|
|
|
double lat = static_cast<osm_node*>(cur_item)->lat;
|
|
|
|
double lon = static_cast<osm_node*>(cur_item)->lon;
|
2015-04-10 12:16:26 +00:00
|
|
|
feature->set_geometry(mapnik::geometry::point<double>(lon,lat));
|
2012-03-02 00:01:13 +00:00
|
|
|
}
|
|
|
|
else if (dataset_->current_item_is_way())
|
|
|
|
{
|
|
|
|
// Loop until we find a feature which passes the filter
|
|
|
|
while (cur_item)
|
2010-11-03 13:19:15 +00:00
|
|
|
{
|
|
|
|
bounds b = static_cast<osm_way*>(cur_item)->get_bounds();
|
2012-08-05 13:34:22 +00:00
|
|
|
if (filter_.pass(box2d<double>(b.w, b.s, b.e, b.n))
|
|
|
|
&&
|
|
|
|
static_cast<osm_way*>(cur_item)->nodes.size()) break;
|
2012-03-02 00:01:13 +00:00
|
|
|
cur_item = dataset_->next_item();
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2012-03-02 00:01:13 +00:00
|
|
|
if (!cur_item) return feature_ptr();
|
2012-10-17 00:46:24 +00:00
|
|
|
feature = feature_factory::create(ctx_, cur_item->id);
|
2012-08-05 13:35:43 +00:00
|
|
|
if (static_cast<osm_way*>(cur_item)->is_polygon())
|
|
|
|
{
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::linear_ring<double> ring;
|
2015-03-22 22:46:27 +00:00
|
|
|
for (unsigned int count = 0;
|
|
|
|
count < static_cast<osm_way*>(cur_item)->nodes.size();
|
|
|
|
count++)
|
|
|
|
{
|
|
|
|
ring.add_coord(static_cast<osm_way*>(cur_item)->nodes[count]->lon,
|
|
|
|
static_cast<osm_way*>(cur_item)->nodes[count]->lat);
|
|
|
|
}
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::polygon<double> geom;
|
2015-03-22 22:46:27 +00:00
|
|
|
geom.set_exterior_ring(std::move(ring));
|
|
|
|
feature->set_geometry(std::move(geom));
|
2012-08-05 13:35:43 +00:00
|
|
|
}
|
2015-03-22 22:46:27 +00:00
|
|
|
else
|
2012-08-05 13:35:43 +00:00
|
|
|
{
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::line_string<double> geom;
|
2015-03-22 22:46:27 +00:00
|
|
|
for (unsigned int count = 0;
|
|
|
|
count < static_cast<osm_way*>(cur_item)->nodes.size();
|
|
|
|
count++)
|
|
|
|
{
|
|
|
|
geom.add_coord(static_cast<osm_way*>(cur_item)->nodes[count]->lon,
|
|
|
|
static_cast<osm_way*>(cur_item)->nodes[count]->lat);
|
|
|
|
}
|
|
|
|
feature->set_geometry(std::move(geom));
|
2012-08-05 13:35:43 +00:00
|
|
|
}
|
2012-08-16 22:52:32 +00:00
|
|
|
}
|
|
|
|
else
|
2012-08-05 13:34:22 +00:00
|
|
|
{
|
2012-08-16 22:52:32 +00:00
|
|
|
MAPNIK_LOG_ERROR(osm_featureset) << "Current item is neither node nor way.\n";
|
2012-03-02 00:01:13 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2012-03-02 00:01:13 +00:00
|
|
|
std::set<std::string>::const_iterator itr = attribute_names_.begin();
|
|
|
|
std::set<std::string>::const_iterator end = attribute_names_.end();
|
|
|
|
std::map<std::string,std::string>::iterator end_keyvals = cur_item->keyvals.end();
|
|
|
|
for (; itr != end; itr++)
|
|
|
|
{
|
|
|
|
std::map<std::string,std::string>::iterator i = cur_item->keyvals.find(*itr);
|
2012-08-05 13:34:22 +00:00
|
|
|
if (i != end_keyvals)
|
|
|
|
{
|
|
|
|
feature->put_new(i->first, tr_->transcode(i->second.c_str()));
|
2010-11-03 13:19:15 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
2012-03-02 00:01:13 +00:00
|
|
|
return feature;
|
2008-03-01 11:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename filterT>
|
|
|
|
osm_featureset<filterT>::~osm_featureset() {}
|
|
|
|
|
|
|
|
template class osm_featureset<mapnik::filter_in_box>;
|
|
|
|
template class osm_featureset<mapnik::filter_at_point>;
|