Merge branch 'master' of github.com:mapnik/mapnik

This commit is contained in:
Dane Springmeyer 2013-10-01 09:26:43 -07:00
commit 433e711a0e
3 changed files with 144 additions and 9 deletions

View file

@ -0,0 +1,128 @@
/*****************************************************************************
*
* 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
*
*****************************************************************************/
#ifndef MAPNIK_TOPOJSON_UTILS_HPP
#define MAPNIK_TOPOJSON_UTILS_HPP
// mapnik
#include <mapnik/box2d.hpp>
#include <mapnik/json/topology.hpp>
// boost
#include <boost/variant/static_visitor.hpp>
namespace mapnik { namespace topojson {
struct bounding_box_visitor : public boost::static_visitor<box2d<double> >
{
bounding_box_visitor(topology const& topo)
: topo_(topo) {}
box2d<double> operator() (mapnik::topojson::point const& pt) const
{
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;
}
return box2d<double>(x, y, x, y);
}
box2d<double> operator() (mapnik::topojson::linestring const& line) const
{
box2d<double> bbox;
bool first = true;
double px = 0, py = 0;
index_type arc_index = line.ring;
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;
bbox.init(x, y, x, y);
}
else
{
bbox.expand_to_include(x, y);
}
}
return bbox;
}
box2d<double> operator() (mapnik::topojson::polygon const& poly) const
{
box2d<double> bbox;
bool first = true;
for (auto const& ring : poly.rings)
{
for (auto index : ring)
{
double px = 0, py = 0;
index_type arc_index = index < 0 ? std::abs(index) - 1 : index;
for (auto const& 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;
bbox.init( x, y, x, y);
}
else
{
bbox.expand_to_include(x, y);
}
}
}
}
return bbox;
}
template<typename T>
box2d<double> operator() (T const& ) const
{
std::cerr << "FIXME" << std::endl;
return box2d<double>();
}
private:
topology topo_;
};
}}
#endif //MAPNIK_TOPOJSON_UTILS_HPP

View file

@ -98,10 +98,10 @@ struct arc
struct transform
{
double scale_x = 1.0;
double scale_y = 1.0;
double translate_x = 0.0;
double translate_y = 0.0;
double scale_x;
double scale_y;
double translate_x;
double translate_y;
};
struct bounding_box
@ -116,7 +116,7 @@ struct topology
{
std::vector<geometry> geometries;
std::vector<arc> arcs;
transform tr;
boost::optional<transform> tr;
boost::optional<bounding_box> bbox;
};
@ -189,7 +189,7 @@ BOOST_FUSION_ADAPT_STRUCT(
mapnik::topojson::topology,
(std::vector<mapnik::topojson::geometry>, geometries)
(std::vector<mapnik::topojson::arc>, arcs)
(mapnik::topojson::transform, tr)
(boost::optional<mapnik::topojson::transform>, tr)
(boost::optional<mapnik::topojson::bounding_box>, bbox)
)

View file

@ -41,14 +41,13 @@
#include <mapnik/unicode.hpp>
#include <mapnik/utils.hpp>
#include <mapnik/feature.hpp>
//#include <mapnik/feature_kv_iterator.hpp>
#include <mapnik/value_types.hpp>
#include <mapnik/box2d.hpp>
#include <mapnik/debug.hpp>
#include <mapnik/proj_transform.hpp>
#include <mapnik/projection.hpp>
//#include <mapnik/util/geometry_to_ds_type.hpp>
#include <mapnik/json/topojson_grammar.hpp>
#include <mapnik/json/topojson_utils.hpp>
using mapnik::datasource;
using mapnik::parameters;
@ -100,7 +99,15 @@ topojson_datasource::topojson_datasource(parameters const& params)
throw mapnik::datasource_exception("topojson_datasource: Failed parse TopoJSON file '" + file_ + "'");
}
// tree_.insert(box_type(point_type(box.minx(),box.miny()),point_type(box.maxx(),box.maxy())), count++);
std::size_t count = 0;
for (auto const& geom : topo_.geometries)
{
mapnik::box2d<double> bbox = boost::apply_visitor(mapnik::topojson::bounding_box_visitor(topo_), geom);
if (bbox.valid())
{
tree_.insert(box_type(point_type(bbox.minx(),bbox.miny()),point_type(bbox.maxx(),bbox.maxy())), count++);
}
}
}
topojson_datasource::~topojson_datasource() { }