2013-09-30 15:14:58 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// mapnik
|
|
|
|
#include <mapnik/feature.hpp>
|
2013-10-02 16:14:27 +02:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2013-09-30 15:14:58 +02:00
|
|
|
// stl
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <fstream>
|
2013-10-02 16:14:27 +02:00
|
|
|
// boost
|
|
|
|
#include <boost/variant/static_visitor.hpp>
|
|
|
|
#include <boost/variant/apply_visitor.hpp>
|
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
2013-10-07 12:14:50 +02:00
|
|
|
#include <boost/range/adaptor/sliced.hpp>
|
2013-09-30 15:14:58 +02:00
|
|
|
#include "topojson_featureset.hpp"
|
|
|
|
|
2013-10-02 16:14:27 +02:00
|
|
|
namespace mapnik { namespace topojson {
|
|
|
|
|
2013-10-03 18:12:33 +02:00
|
|
|
struct attribute_value_visitor
|
|
|
|
: boost::static_visitor<mapnik::value>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
attribute_value_visitor(mapnik::transcoder const& tr)
|
|
|
|
: tr_(tr) {}
|
|
|
|
|
|
|
|
mapnik::value operator()(std::string const& val) const
|
|
|
|
{
|
|
|
|
return mapnik::value(tr_.transcode(val.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
mapnik::value operator()(T const& val) const
|
|
|
|
{
|
|
|
|
return mapnik::value(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
mapnik::transcoder const& tr_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void assign_properties(mapnik::feature_impl & feature, T const& geom, mapnik::transcoder const& tr)
|
|
|
|
{
|
|
|
|
if ( geom.props)
|
|
|
|
{
|
|
|
|
for (auto const& p : *geom.props)
|
|
|
|
{
|
|
|
|
feature.put_new(std::get<0>(p), boost::apply_visitor(attribute_value_visitor(tr),std::get<1>(p)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 16:14:27 +02:00
|
|
|
template <typename Context>
|
|
|
|
struct feature_generator : public boost::static_visitor<mapnik::feature_ptr>
|
|
|
|
{
|
2013-10-03 18:12:33 +02:00
|
|
|
feature_generator(Context & ctx, mapnik::transcoder const& tr, topology const& topo, std::size_t feature_id)
|
2013-10-02 16:14:27 +02:00
|
|
|
: ctx_(ctx),
|
2013-10-03 18:12:33 +02:00
|
|
|
tr_(tr),
|
2013-10-02 16:14:27 +02:00
|
|
|
topo_(topo),
|
|
|
|
feature_id_(feature_id) {}
|
|
|
|
|
|
|
|
feature_ptr operator() (point const& pt) const
|
|
|
|
{
|
2013-10-03 18:12:33 +02:00
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_,feature_id_));
|
|
|
|
std::unique_ptr<geometry_type> point_ptr(new geometry_type(geometry_type::types::Point));
|
|
|
|
|
|
|
|
double x = pt.coord.x;
|
|
|
|
double y = pt.coord.y;
|
|
|
|
if (topo_.tr)
|
|
|
|
{
|
|
|
|
x = x * (*topo_.tr).scale_x + (*topo_.tr).translate_x;
|
|
|
|
y = y * (*topo_.tr).scale_y + (*topo_.tr).translate_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
point_ptr->move_to(x,y);
|
|
|
|
feature->paths().push_back(point_ptr.release());
|
|
|
|
assign_properties(*feature, pt, tr_);
|
|
|
|
return feature;
|
2013-10-02 16:14:27 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:40:06 +02:00
|
|
|
feature_ptr operator() (multi_point const& multi_pt) const
|
|
|
|
{
|
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_,feature_id_));
|
|
|
|
|
|
|
|
for (auto const& pt : multi_pt.points)
|
|
|
|
{
|
|
|
|
std::unique_ptr<geometry_type> point_ptr(new geometry_type(geometry_type::types::Point));
|
|
|
|
|
|
|
|
double x = pt.x;
|
|
|
|
double y = pt.y;
|
|
|
|
if (topo_.tr)
|
|
|
|
{
|
|
|
|
x = x * (*topo_.tr).scale_x + (*topo_.tr).translate_x;
|
|
|
|
y = y * (*topo_.tr).scale_y + (*topo_.tr).translate_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
point_ptr->move_to(x,y);
|
|
|
|
feature->paths().push_back(point_ptr.release());
|
|
|
|
}
|
|
|
|
assign_properties(*feature, multi_pt, tr_);
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
|
2013-10-02 16:14:27 +02:00
|
|
|
feature_ptr operator() (linestring const& line) const
|
|
|
|
{
|
2013-10-03 18:12:33 +02:00
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_,feature_id_));
|
|
|
|
std::unique_ptr<geometry_type> line_ptr(new geometry_type(geometry_type::types::LineString));
|
|
|
|
|
|
|
|
double px = 0, py = 0;
|
|
|
|
index_type arc_index = line.ring;
|
|
|
|
bool first = true;
|
|
|
|
for (auto pt : topo_.arcs[arc_index].coordinates)
|
|
|
|
{
|
|
|
|
double x = pt.x;
|
|
|
|
double y = pt.y;
|
|
|
|
if (topo_.tr)
|
|
|
|
{
|
|
|
|
x = (px += x) * (*topo_.tr).scale_x + (*topo_.tr).translate_x;
|
|
|
|
y = (py += y) * (*topo_.tr).scale_y + (*topo_.tr).translate_y;
|
|
|
|
}
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
line_ptr->move_to(x,y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line_ptr->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
feature->paths().push_back(line_ptr.release());
|
|
|
|
assign_properties(*feature, line, tr_);
|
|
|
|
return feature;
|
2013-10-02 16:14:27 +02:00
|
|
|
}
|
|
|
|
|
2013-10-04 16:40:06 +02:00
|
|
|
feature_ptr operator() (multi_linestring const& multi_line) const
|
|
|
|
{
|
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_,feature_id_));
|
|
|
|
|
|
|
|
for (auto const& index : multi_line.rings)
|
|
|
|
{
|
|
|
|
std::unique_ptr<geometry_type> line_ptr(new geometry_type(geometry_type::types::LineString));
|
|
|
|
|
|
|
|
double px = 0, py = 0;
|
|
|
|
bool first = true;
|
2013-10-07 12:22:50 +02:00
|
|
|
bool reverse = index < 0;
|
|
|
|
index_type arc_index = reverse ? std::abs(index) - 1 : index;
|
2013-10-04 16:40:06 +02:00
|
|
|
for (auto pt : topo_.arcs[arc_index].coordinates)
|
|
|
|
{
|
|
|
|
double x = pt.x;
|
|
|
|
double y = pt.y;
|
|
|
|
if (topo_.tr)
|
|
|
|
{
|
|
|
|
x = (px += x) * (*topo_.tr).scale_x + (*topo_.tr).translate_x;
|
|
|
|
y = (py += y) * (*topo_.tr).scale_y + (*topo_.tr).translate_y;
|
|
|
|
}
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
line_ptr->move_to(x,y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line_ptr->line_to(x,y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
feature->paths().push_back(line_ptr.release());
|
|
|
|
}
|
|
|
|
assign_properties(*feature, multi_line, tr_);
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
|
2013-10-02 16:14:27 +02:00
|
|
|
feature_ptr operator() (polygon const& poly) const
|
|
|
|
{
|
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_,feature_id_));
|
|
|
|
std::unique_ptr<geometry_type> poly_ptr(new geometry_type(geometry_type::types::Polygon));
|
2013-10-07 11:52:19 +02:00
|
|
|
std::vector<mapnik::topojson::coordinate> processed_coords;
|
2013-10-07 12:14:50 +02:00
|
|
|
|
2013-10-02 16:14:27 +02:00
|
|
|
for (auto const& ring : poly.rings)
|
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
for (auto const& index : ring)
|
|
|
|
{
|
|
|
|
double px = 0, py = 0;
|
2013-10-07 12:22:50 +02:00
|
|
|
bool reverse = index < 0;
|
|
|
|
index_type arc_index = reverse ? std::abs(index) - 1 : index;
|
2013-10-02 16:14:27 +02:00
|
|
|
auto const& coords = topo_.arcs[arc_index].coordinates;
|
2013-10-07 11:52:19 +02:00
|
|
|
processed_coords.clear();
|
|
|
|
processed_coords.reserve(coords.size());
|
2013-10-02 16:14:27 +02:00
|
|
|
for (auto const& pt : coords )
|
|
|
|
{
|
|
|
|
double x = pt.x;
|
|
|
|
double y = pt.y;
|
|
|
|
|
|
|
|
if (topo_.tr)
|
|
|
|
{
|
2013-10-07 12:14:50 +02:00
|
|
|
transform const& tr = *topo_.tr;
|
|
|
|
x = (px += x) * tr.scale_x + tr.translate_x;
|
|
|
|
y = (py += y) * tr.scale_y + tr.translate_y;
|
2013-10-02 16:14:27 +02:00
|
|
|
}
|
2013-10-07 11:52:19 +02:00
|
|
|
processed_coords.emplace_back(coordinate{x,y});
|
2013-10-02 16:14:27 +02:00
|
|
|
}
|
2013-10-07 12:14:50 +02:00
|
|
|
|
|
|
|
using namespace boost::adaptors;
|
|
|
|
|
2013-10-07 12:22:50 +02:00
|
|
|
if (reverse)
|
2013-10-07 11:52:19 +02:00
|
|
|
{
|
2013-10-07 12:22:50 +02:00
|
|
|
for (auto const& c : processed_coords | reversed | sliced(0,processed_coords.size()-1))
|
2013-10-07 11:52:19 +02:00
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
poly_ptr->move_to(c.x,c.y);
|
|
|
|
}
|
|
|
|
else poly_ptr->line_to(c.x,c.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-10-02 16:14:27 +02:00
|
|
|
{
|
2013-10-07 12:14:50 +02:00
|
|
|
for (auto const& c : processed_coords | sliced(0,processed_coords.size()-1))
|
2013-10-02 16:14:27 +02:00
|
|
|
{
|
2013-10-07 11:52:19 +02:00
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
poly_ptr->move_to(c.x,c.y);
|
|
|
|
}
|
|
|
|
else poly_ptr->line_to(c.x,c.y);
|
2013-10-02 16:14:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
poly_ptr->close_path();
|
|
|
|
}
|
2013-10-03 16:32:07 +02:00
|
|
|
|
2013-10-03 18:12:33 +02:00
|
|
|
feature->paths().push_back(poly_ptr.release());
|
|
|
|
assign_properties(*feature, poly, tr_);
|
2013-10-02 16:14:27 +02:00
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:40:06 +02:00
|
|
|
feature_ptr operator() (multi_polygon const& multi_poly) const
|
|
|
|
{
|
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_,feature_id_));
|
2013-10-07 11:52:19 +02:00
|
|
|
std::vector<mapnik::topojson::coordinate> processed_coords;
|
2013-10-04 16:40:06 +02:00
|
|
|
for (auto const& poly : multi_poly.polygons)
|
|
|
|
{
|
|
|
|
std::unique_ptr<geometry_type> poly_ptr(new geometry_type(geometry_type::types::Polygon));
|
|
|
|
for (auto const& ring : poly)
|
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
for (auto const& index : ring)
|
|
|
|
{
|
|
|
|
double px = 0, py = 0;
|
2013-10-07 12:22:50 +02:00
|
|
|
bool reverse = index < 0;
|
|
|
|
index_type arc_index = reverse ? std::abs(index) - 1 : index;
|
2013-10-04 16:40:06 +02:00
|
|
|
auto const& coords = topo_.arcs[arc_index].coordinates;
|
2013-10-07 11:52:19 +02:00
|
|
|
processed_coords.clear();
|
|
|
|
processed_coords.reserve(coords.size());
|
2013-10-04 16:40:06 +02:00
|
|
|
for (auto const& pt : coords )
|
|
|
|
{
|
|
|
|
double x = pt.x;
|
|
|
|
double y = pt.y;
|
|
|
|
|
|
|
|
if (topo_.tr)
|
|
|
|
{
|
|
|
|
x = (px += x) * (*topo_.tr).scale_x + (*topo_.tr).translate_x;
|
|
|
|
y = (py += y) * (*topo_.tr).scale_y + (*topo_.tr).translate_y;
|
|
|
|
}
|
2013-10-07 11:52:19 +02:00
|
|
|
processed_coords.emplace_back(coordinate{x,y});
|
2013-10-04 16:40:06 +02:00
|
|
|
}
|
|
|
|
|
2013-10-07 12:22:50 +02:00
|
|
|
using namespace boost::adaptors;
|
|
|
|
|
|
|
|
if (reverse)
|
2013-10-04 16:40:06 +02:00
|
|
|
{
|
2013-10-07 12:22:50 +02:00
|
|
|
for (auto const& c : (processed_coords | reversed | sliced(0,processed_coords.size() - 1)))
|
2013-10-04 16:40:06 +02:00
|
|
|
{
|
2013-10-07 11:52:19 +02:00
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
poly_ptr->move_to(c.x,c.y);
|
|
|
|
}
|
|
|
|
else poly_ptr->line_to(c.x,c.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-07 12:22:50 +02:00
|
|
|
for (auto const& c : processed_coords | sliced(0,processed_coords.size() - 1))
|
2013-10-07 11:52:19 +02:00
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
poly_ptr->move_to(c.x,c.y);
|
|
|
|
}
|
|
|
|
else poly_ptr->line_to(c.x,c.y);
|
2013-10-04 16:40:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
poly_ptr->close_path();
|
|
|
|
}
|
|
|
|
feature->paths().push_back(poly_ptr.release());
|
|
|
|
}
|
|
|
|
assign_properties(*feature, multi_poly, tr_);
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
|
2013-10-02 16:14:27 +02:00
|
|
|
template<typename T>
|
|
|
|
feature_ptr operator() (T const& ) const
|
|
|
|
{
|
|
|
|
return feature_ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
Context & ctx_;
|
2013-10-03 18:12:33 +02:00
|
|
|
mapnik::transcoder const& tr_;
|
2013-10-02 16:14:27 +02:00
|
|
|
topology const& topo_;
|
|
|
|
std::size_t feature_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
2013-09-30 15:14:58 +02:00
|
|
|
topojson_featureset::topojson_featureset(mapnik::topojson::topology const& topo,
|
2013-10-03 18:12:33 +02:00
|
|
|
mapnik::transcoder const& tr,
|
|
|
|
std::deque<std::size_t>::const_iterator index_itr,
|
|
|
|
std::deque<std::size_t>::const_iterator index_end)
|
2013-10-02 16:14:27 +02:00
|
|
|
: ctx_(std::make_shared<mapnik::context_type>()),
|
|
|
|
topo_(topo),
|
2013-10-03 18:12:33 +02:00
|
|
|
tr_(tr),
|
2013-09-30 15:14:58 +02:00
|
|
|
index_itr_(index_itr),
|
2013-10-02 16:14:27 +02:00
|
|
|
index_end_(index_end),
|
|
|
|
feature_id_ (0) {}
|
2013-09-30 15:14:58 +02:00
|
|
|
|
|
|
|
topojson_featureset::~topojson_featureset() {}
|
|
|
|
|
|
|
|
mapnik::feature_ptr topojson_featureset::next()
|
|
|
|
{
|
|
|
|
if (index_itr_ != index_end_)
|
|
|
|
{
|
|
|
|
std::size_t index = *index_itr_++;
|
|
|
|
if ( index < topo_.geometries.size())
|
|
|
|
{
|
2013-10-01 20:17:07 +02:00
|
|
|
mapnik::topojson::geometry const& geom = topo_.geometries[index];
|
2013-10-02 16:14:27 +02:00
|
|
|
mapnik::feature_ptr feature = boost::apply_visitor(
|
2013-10-03 18:12:33 +02:00
|
|
|
mapnik::topojson::feature_generator<mapnik::context_ptr>(ctx_, tr_, topo_, feature_id_++),
|
2013-10-02 16:14:27 +02:00
|
|
|
geom);
|
|
|
|
return feature;
|
2013-09-30 15:14:58 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-02 16:14:27 +02:00
|
|
|
|
2013-09-30 15:14:58 +02:00
|
|
|
return mapnik::feature_ptr();
|
|
|
|
}
|