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)
|
|
|
|
*
|
2011-10-22 14:04:05 +00:00
|
|
|
* Copyright (C) 2011 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-10-22 14:04:05 +00:00
|
|
|
// stl
|
|
|
|
#include <iostream>
|
|
|
|
|
2011-05-16 23:41:34 +00:00
|
|
|
// mapnik
|
2008-03-01 11:49:37 +00:00
|
|
|
#include <mapnik/geometry.hpp>
|
2011-05-16 23:41:34 +00:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
|
|
|
|
|
|
|
#include "osm_featureset.hpp"
|
|
|
|
|
2008-03-01 11:49:37 +00:00
|
|
|
using mapnik::Feature;
|
|
|
|
using mapnik::feature_ptr;
|
2010-11-03 13:19:15 +00:00
|
|
|
using mapnik::geometry_type;
|
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)),
|
|
|
|
feature_id_(1),
|
|
|
|
dataset_ (dataset),
|
|
|
|
attribute_names_ (attribute_names)
|
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
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
osm_item* cur_item = dataset_->next_item();
|
|
|
|
if (cur_item != NULL)
|
2010-11-03 13:19:15 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
if (dataset_->current_item_is_node())
|
2010-11-03 13:19:15 +00:00
|
|
|
{
|
2011-05-16 23:41:34 +00:00
|
|
|
feature = feature_factory::create(feature_id_);
|
2011-04-29 20:00:45 +00:00
|
|
|
++feature_id_;
|
2010-11-03 13:19:15 +00:00
|
|
|
double lat = static_cast<osm_node*>(cur_item)->lat;
|
|
|
|
double lon = static_cast<osm_node*>(cur_item)->lon;
|
2011-10-22 14:04:05 +00:00
|
|
|
geometry_type* point = new geometry_type(mapnik::Point);
|
|
|
|
point->move_to(lon, lat);
|
2010-11-03 13:19:15 +00:00
|
|
|
feature->add_geometry(point);
|
2011-05-16 23:41:34 +00:00
|
|
|
success = true;
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
2010-11-03 13:19:15 +00:00
|
|
|
else if (dataset_->current_item_is_way())
|
|
|
|
{
|
|
|
|
bounds b = static_cast<osm_way*>(cur_item)->get_bounds();
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-11-03 13:19:15 +00:00
|
|
|
// Loop until we find a feature which passes the filter
|
2011-10-22 14:04:05 +00:00
|
|
|
while (cur_item != NULL &&
|
|
|
|
! filter_.pass(box2d<double>(b.w, b.s, b.e, b.n)))
|
2010-11-03 13:19:15 +00:00
|
|
|
{
|
|
|
|
cur_item = dataset_->next_item();
|
2011-10-22 14:04:05 +00:00
|
|
|
if (cur_item != NULL)
|
|
|
|
{
|
2010-11-03 13:19:15 +00:00
|
|
|
b = static_cast<osm_way*>(cur_item)->get_bounds();
|
2011-10-22 14:04:05 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
if (cur_item != NULL)
|
2010-11-03 13:19:15 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
if (static_cast<osm_way*>(cur_item)->nodes.size())
|
2010-11-03 13:19:15 +00:00
|
|
|
{
|
2011-05-16 23:41:34 +00:00
|
|
|
feature = feature_factory::create(feature_id_);
|
2011-04-29 20:00:45 +00:00
|
|
|
++feature_id_;
|
2011-10-22 14:04:05 +00:00
|
|
|
geometry_type* geom;
|
|
|
|
if (static_cast<osm_way*>(cur_item)->is_polygon())
|
|
|
|
{
|
2011-05-16 23:41:34 +00:00
|
|
|
geom = new geometry_type(mapnik::Polygon);
|
2011-10-22 14:04:05 +00:00
|
|
|
}
|
2010-11-03 13:19:15 +00:00
|
|
|
else
|
2011-10-22 14:04:05 +00:00
|
|
|
{
|
2011-05-16 23:41:34 +00:00
|
|
|
geom = new geometry_type(mapnik::LineString);
|
2011-10-22 14:04:05 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
geom->set_capacity(static_cast<osm_way*>(cur_item)->nodes.size());
|
|
|
|
geom->move_to(static_cast<osm_way*>(cur_item)->nodes[0]->lon,
|
|
|
|
static_cast<osm_way*>(cur_item)->nodes[0]->lat);
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-22 14:04:05 +00:00
|
|
|
for (unsigned int count = 1;
|
|
|
|
count < static_cast<osm_way*>(cur_item)->nodes.size();
|
|
|
|
count++)
|
2010-11-03 13:19:15 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
geom->line_to(static_cast<osm_way*>(cur_item)->nodes[count]->lon,
|
|
|
|
static_cast<osm_way*>(cur_item)->nodes[count]->lat);
|
2010-11-03 13:19:15 +00:00
|
|
|
}
|
|
|
|
feature->add_geometry(geom);
|
2011-05-16 23:41:34 +00:00
|
|
|
success = true;
|
2010-11-03 13:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
|
|
|
// can feature_ptr be compared to NULL? - no
|
2011-10-22 14:04:05 +00:00
|
|
|
if (success)
|
2010-11-03 13:19:15 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
std::map<std::string,std::string>::iterator i = cur_item->keyvals.begin();
|
2008-03-01 11:49:37 +00:00
|
|
|
|
2010-11-03 13:19:15 +00:00
|
|
|
// add the keyvals to the feature. the feature seems to be a map
|
|
|
|
// of some sort so this should work - see dbf_file::add_attribute()
|
2011-10-22 14:04:05 +00:00
|
|
|
while (i != cur_item->keyvals.end())
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2011-10-22 14:04:05 +00:00
|
|
|
// only add if in the specified set of attribute names
|
|
|
|
if (attribute_names_.find(i->first) != attribute_names_.end())
|
|
|
|
{
|
2010-11-03 13:19:15 +00:00
|
|
|
(*feature)[i->first] = tr_->transcode(i->second.c_str());
|
2011-10-22 14:04:05 +00:00
|
|
|
}
|
|
|
|
|
2010-11-03 13:19:15 +00:00
|
|
|
i++;
|
|
|
|
}
|
2011-10-22 14:04:05 +00:00
|
|
|
|
2010-11-03 13:19:15 +00:00
|
|
|
return feature;
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
2010-11-03 13:19:15 +00:00
|
|
|
return feature_ptr();
|
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>;
|