== topojson ==

support multi geometries
This commit is contained in:
artemp 2013-10-04 15:40:06 +01:00
parent 56a14368bf
commit e3f18717d3
5 changed files with 249 additions and 5 deletions

View file

@ -84,8 +84,11 @@ private:
qi::rule<Iterator, space_type, mapnik::topojson::bounding_box()> bbox;
qi::rule<Iterator, space_type, mapnik::topojson::geometry()> geometry;
qi::rule<Iterator, space_type, mapnik::topojson::point()> point;
qi::rule<Iterator, space_type, mapnik::topojson::multi_point()> multi_point;
qi::rule<Iterator, space_type, mapnik::topojson::linestring()> linestring;
qi::rule<Iterator, space_type, mapnik::topojson::multi_linestring()> multi_linestring;
qi::rule<Iterator, space_type, mapnik::topojson::polygon()> polygon;
qi::rule<Iterator, space_type, mapnik::topojson::multi_polygon()> multi_polygon;
qi::rule<Iterator, space_type, std::vector<index_type>()> ring;
// properties

View file

@ -118,7 +118,14 @@ topojson_grammar<Iterator>::topojson_grammar()
>> lit(']') >> lit('}') >> lit('}')
;
geometry = point | linestring | polygon | omit[object]
geometry =
point |
linestring |
polygon |
multi_point |
multi_linestring |
multi_polygon |
omit[object]
;
point = lit('{')
@ -127,18 +134,44 @@ topojson_grammar<Iterator>::topojson_grammar()
>> lit('}')
;
multi_point = lit('{')
>> lit("\"type\"") >> lit(':') >> lit("\"MultiPoint\"")
>> ((lit(',') >> lit("\"coordinates\"") >> lit(':')
>> lit('[') >> -(coordinate % lit(',')) >> lit(']')) ^ (lit(',') >> properties))
>> lit('}')
;
linestring = lit('{')
>> lit("\"type\"") >> lit(':') >> lit("\"LineString\"")
>> ((lit(',') >> lit("\"arcs\"") >> lit(':') >> lit('[') >> int_ >> lit(']')) ^ (lit(',') >> properties))
>> lit('}')
;
multi_linestring = lit('{')
>> lit("\"type\"") >> lit(':') >> lit("\"MultiLineString\"")
>> ((lit(',') >> lit("\"arcs\"") >> lit(':') >> lit('[')
>> -((lit('[') >> int_ >> lit(']')) % lit(',')) >> lit(']')) ^ (lit(',') >> properties))
>> lit('}')
;
polygon = lit('{')
>> lit("\"type\"") >> lit(':') >> lit("\"Polygon\"")
>> ((lit(',') >> lit("\"arcs\"") >> lit(':') >> lit('[') >> -(ring % lit(',')) >> lit(']')) ^ (lit(',') >> properties))
>> ((lit(',') >> lit("\"arcs\"") >> lit(':')
>> lit('[') >> -(ring % lit(',')) >> lit(']'))
^ (lit(',') >> properties))
>> lit('}')
;
multi_polygon = lit('{')
>> lit("\"type\"") >> lit(':') >> lit("\"MultiPolygon\"")
>> ((lit(',') >> lit("\"arcs\"") >> lit(':')
>> lit('[')
>> -((lit('[') >> -(ring % lit(',')) >> lit(']')) % lit(','))
>> lit(']')) ^ (lit(',') >> properties))
>> lit('}')
;
ring = lit('[') >> -(int_ % lit(',')) >> lit(']')
;
@ -168,8 +201,10 @@ topojson_grammar<Iterator>::topojson_grammar()
coordinate.name("coordinate");
point.name("point");
multi_point.name("multi_point");
linestring.name("linestring");
polygon.name("polygon");
multi_polygon.name("multi_polygon");
on_error<fail>
(

View file

@ -48,6 +48,32 @@ struct bounding_box_visitor : public boost::static_visitor<box2d<double> >
return box2d<double>(x, y, x, y);
}
box2d<double> operator() (mapnik::topojson::multi_point const& multi_pt) const
{
box2d<double> bbox;
bool first = true;
for (auto const& pt : multi_pt.points)
{
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; // TODO : delta encoded ?
}
if (first)
{
first = false;
bbox.init(x,y,x,y);
}
else
{
bbox.expand_to_include(x,y);
}
}
return bbox;
}
box2d<double> operator() (mapnik::topojson::linestring const& line) const
{
box2d<double> bbox;
@ -76,6 +102,37 @@ struct bounding_box_visitor : public boost::static_visitor<box2d<double> >
return bbox;
}
box2d<double> operator() (mapnik::topojson::multi_linestring const& multi_line) const
{
box2d<double> bbox;
bool first = true;
for (auto index : multi_line.rings)
{
double px = 0, py = 0;
index_type arc_index = index < 0 ? std::abs(index) - 1 : index;
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;
@ -112,10 +169,48 @@ struct bounding_box_visitor : public boost::static_visitor<box2d<double> >
return bbox;
}
box2d<double> operator() (mapnik::topojson::multi_polygon const& multi_poly) const
{
box2d<double> bbox;
bool first = true;
for (auto const& poly : multi_poly.polygons)
{
for (auto const& ring : poly)
{
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>();
}

View file

@ -77,7 +77,7 @@ struct polygon
struct multi_polygon
{
std::vector<polygon> polygons;//FIXME
std::vector<std::vector<std::vector<index_type> > > polygons;
boost::optional<properties> props;
};
@ -183,7 +183,7 @@ BOOST_FUSION_ADAPT_STRUCT(
BOOST_FUSION_ADAPT_STRUCT(
mapnik::topojson::multi_polygon,
(std::vector<mapnik::topojson::polygon>, polygons)
(std::vector<std::vector<std::vector<mapnik::topojson::index_type> > >, polygons)
(boost::optional<mapnik::topojson::properties>, props)
)

View file

@ -97,6 +97,29 @@ struct feature_generator : public boost::static_visitor<mapnik::feature_ptr>
return feature;
}
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;
}
feature_ptr operator() (linestring const& line) const
{
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_,feature_id_));
@ -130,6 +153,43 @@ struct feature_generator : public boost::static_visitor<mapnik::feature_ptr>
return feature;
}
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;
bool reversed = index < 0;
index_type arc_index = reversed ? std::abs(index) - 1 : index;
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;
}
feature_ptr operator() (polygon const& poly) const
{
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_,feature_id_));
@ -179,6 +239,57 @@ struct feature_generator : public boost::static_visitor<mapnik::feature_ptr>
return feature;
}
feature_ptr operator() (multi_polygon const& multi_poly) const
{
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx_,feature_id_));
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;
bool reversed = index < 0;
index_type arc_index = reversed ? std::abs(index) - 1 : index;
auto const& coords = topo_.arcs[arc_index].coordinates;
std::deque<mapnik::topojson::coordinate> processed_coords;
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;
}
if (reversed)
processed_coords.emplace_front(coordinate{x,y});
else
processed_coords.emplace_back(coordinate{x,y});
}
for (auto const& c : processed_coords)
{
if (first)
{
first = false;
poly_ptr->move_to(c.x,c.y);
}
else poly_ptr->line_to(c.x,c.y);
}
}
poly_ptr->close_path();
}
feature->paths().push_back(poly_ptr.release());
}
assign_properties(*feature, multi_poly, tr_);
return feature;
}
template<typename T>
feature_ptr operator() (T const& ) const
{