add polylabel placement method

This commit is contained in:
Jiri Drbalek 2017-09-29 11:31:03 +00:00
parent e243e16fd9
commit ea7ba2c099
14 changed files with 256 additions and 1 deletions

3
.gitmodules vendored
View file

@ -18,3 +18,6 @@
path = deps/mapbox/protozero path = deps/mapbox/protozero
url = https://github.com/mapbox/protozero.git url = https://github.com/mapbox/protozero.git
branch = master branch = master
[submodule "deps/mapbox/polylabel"]
path = deps/mapbox/polylabel
url = https://github.com/mapbox/polylabel.git

View file

@ -1756,6 +1756,7 @@ if not preconfigured:
env.Prepend(CPPPATH = '#deps/mapbox/variant/include') env.Prepend(CPPPATH = '#deps/mapbox/variant/include')
env.Prepend(CPPPATH = '#deps/mapbox/geometry/include') env.Prepend(CPPPATH = '#deps/mapbox/geometry/include')
env.Prepend(CPPPATH = '#deps/mapbox/protozero/include') env.Prepend(CPPPATH = '#deps/mapbox/protozero/include')
env.Prepend(CPPPATH = '#deps/mapbox/polylabel/include')
# prepend deps dir for auxillary headers # prepend deps dir for auxillary headers
env.Prepend(CPPPATH = '#deps') env.Prepend(CPPPATH = '#deps')

1
deps/mapbox/polylabel vendored Submodule

@ -0,0 +1 @@
Subproject commit 51f09d04c21e7b7faf94e2300ca1fe6e1f12fa7f

View file

@ -0,0 +1,62 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2017 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_GEOMETRY_POLYGON_VERTEX_PROCESSOR_HPP
#define MAPNIK_GEOMETRY_POLYGON_VERTEX_PROCESSOR_HPP
// geometry
#include <mapnik/geometry/polygon.hpp>
namespace mapnik { namespace geometry {
template <typename T>
struct polygon_vertex_processor
{
template <typename Path>
void add_path(Path & path)
{
point<T> p;
unsigned cmd;
linear_ring<T> ring;
while ((cmd = path.vertex(&p.x, &p.y)) != SEG_END)
{
switch (cmd)
{
case SEG_MOVETO:
case SEG_LINETO:
ring.emplace_back(p);
break;
case SEG_CLOSE:
ring.emplace_back(ring.front());
polygon_.emplace_back(std::move(ring));
ring = linear_ring<T>();
break;
}
}
}
polygon<T> polygon_;
};
} }
#endif // MAPNIK_GEOMETRY_POLYGON_VERTEX_PROCESSOR_HPP

View file

@ -0,0 +1,39 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2017 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_GEOMETRY_POLYLABEL_HPP
#define MAPNIK_GEOMETRY_POLYLABEL_HPP
#include <mapnik/geometry/polygon.hpp>
#include <mapnik/geometry/point.hpp>
namespace mapnik { namespace geometry {
template <class T>
point<T> polylabel(polygon<T> const& polygon, T precision);
template <class T>
T polylabel_precision(polygon<T> const& polygon, double scale_factor);
} }
#endif // MAPNIK_GEOMETRY_POLYLABEL_HPP

View file

@ -28,6 +28,7 @@
#include <mapnik/markers_placements/interior.hpp> #include <mapnik/markers_placements/interior.hpp>
#include <mapnik/markers_placements/vertex_first.hpp> #include <mapnik/markers_placements/vertex_first.hpp>
#include <mapnik/markers_placements/vertex_last.hpp> #include <mapnik/markers_placements/vertex_last.hpp>
#include <mapnik/markers_placements/polylabel.hpp>
#include <mapnik/symbolizer_enumerations.hpp> #include <mapnik/symbolizer_enumerations.hpp>
namespace mapnik namespace mapnik
@ -65,6 +66,9 @@ public:
case MARKER_VERTEX_LAST_PLACEMENT: case MARKER_VERTEX_LAST_PLACEMENT:
construct(&vertex_last_, locator, detector, params); construct(&vertex_last_, locator, detector, params);
break; break;
case MARKER_POLYLABEL_PLACEMENT:
construct(&polylabel_, locator, detector, params);
break;
} }
} }
@ -89,6 +93,9 @@ public:
case MARKER_VERTEX_LAST_PLACEMENT: case MARKER_VERTEX_LAST_PLACEMENT:
destroy(&vertex_last_); destroy(&vertex_last_);
break; break;
case MARKER_POLYLABEL_PLACEMENT:
destroy(&polylabel_);
break;
} }
} }
@ -109,6 +116,8 @@ public:
return vertex_first_.get_point(x, y, angle, ignore_placement); return vertex_first_.get_point(x, y, angle, ignore_placement);
case MARKER_VERTEX_LAST_PLACEMENT: case MARKER_VERTEX_LAST_PLACEMENT:
return vertex_last_.get_point(x, y, angle, ignore_placement); return vertex_last_.get_point(x, y, angle, ignore_placement);
case MARKER_POLYLABEL_PLACEMENT:
return polylabel_.get_point(x, y, angle, ignore_placement);
} }
} }
@ -122,6 +131,7 @@ private:
markers_interior_placement<Locator, Detector> interior_; markers_interior_placement<Locator, Detector> interior_;
markers_vertex_first_placement<Locator, Detector> vertex_first_; markers_vertex_first_placement<Locator, Detector> vertex_first_;
markers_vertex_last_placement<Locator, Detector> vertex_last_; markers_vertex_last_placement<Locator, Detector> vertex_last_;
markers_polylabel_placement<Locator, Detector> polylabel_;
}; };
template <typename T> template <typename T>

View file

@ -46,6 +46,7 @@ struct markers_placement_params
bool allow_overlap; bool allow_overlap;
bool avoid_edges; bool avoid_edges;
direction_enum direction; direction_enum direction;
double scale_factor;
}; };
class markers_basic_placement : util::noncopyable class markers_basic_placement : util::noncopyable

View file

@ -0,0 +1,77 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2017 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_MARKERS_PLACEMENTS_POLYLABEL_HPP
#define MAPNIK_MARKERS_PLACEMENTS_POLYLABEL_HPP
#include <mapnik/markers_placements/point.hpp>
#include <mapnik/geom_util.hpp>
#include <mapnik/geometry/geometry_types.hpp>
#include <mapnik/geometry/polygon_vertex_processor.hpp>
#include <mapnik/geometry/point.hpp>
#include <mapnik/geometry/polylabel.hpp>
namespace mapnik {
template <typename Locator, typename Detector>
class markers_polylabel_placement : public markers_point_placement<Locator, Detector>
{
public:
using point_placement = markers_point_placement<Locator, Detector>;
using point_placement::point_placement;
bool get_point(double &x, double &y, double &angle, bool ignore_placement)
{
if (this->done_)
{
return false;
}
if (this->locator_.type() != geometry::geometry_types::Polygon)
{
return point_placement::get_point(x, y, angle, ignore_placement);
}
geometry::polygon_vertex_processor<double> vertex_processor;
vertex_processor.add_path(this->locator_);
double precision = geometry::polylabel_precision(vertex_processor.polygon_,
this->params_.scale_factor);
geometry::point<double> placement = geometry::polylabel(vertex_processor.polygon_, precision);
x = placement.x;
y = placement.y;
angle = 0;
if (!this->push_to_detector(x, y, angle, ignore_placement))
{
return false;
}
this->done_ = true;
return true;
}
};
}
#endif // MAPNIK_MARKERS_PLACEMENTS_POLYLABEL_HPP

View file

@ -106,6 +106,7 @@ enum marker_placement_enum : std::uint8_t
MARKER_VERTEX_FIRST_PLACEMENT, MARKER_VERTEX_FIRST_PLACEMENT,
MARKER_VERTEX_LAST_PLACEMENT, MARKER_VERTEX_LAST_PLACEMENT,
MARKER_ANGLED_POINT_PLACEMENT, MARKER_ANGLED_POINT_PLACEMENT,
MARKER_POLYLABEL_PLACEMENT,
marker_placement_enum_MAX marker_placement_enum_MAX
}; };
@ -139,6 +140,7 @@ enum label_placement_enum : std::uint8_t
LINE_PLACEMENT, LINE_PLACEMENT,
VERTEX_PLACEMENT, VERTEX_PLACEMENT,
INTERIOR_PLACEMENT, INTERIOR_PLACEMENT,
POLYLABEL_PLACEMENT,
label_placement_enum_MAX label_placement_enum_MAX
}; };

View file

@ -173,6 +173,7 @@ source = Split(
geometry/closest_point.cpp geometry/closest_point.cpp
geometry/reprojection.cpp geometry/reprojection.cpp
geometry/envelope.cpp geometry/envelope.cpp
geometry/polylabel.cpp
expression_node.cpp expression_node.cpp
expression_string.cpp expression_string.cpp
expression.cpp expression.cpp

View file

@ -0,0 +1,49 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2017 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
*
*****************************************************************************/
#include <mapnik/geometry/polylabel.hpp>
#include <mapnik/geometry/envelope.hpp>
#include <mapbox/polylabel.hpp>
namespace mapnik { namespace geometry {
template <class T>
T polylabel_precision(polygon<T> const& polygon, double scale_factor)
{
// This precision has been chosen to work well in the map (viewport) coordinates.
return 10.0 * scale_factor;
}
template <class T>
point<T> polylabel(polygon<T> const& polygon, T precision)
{
return mapbox::polylabel(polygon, precision);
}
template
point<double> polylabel(polygon<double> const& polygon, double precision);
template
double polylabel_precision(polygon<double> const& polygon, double scale_factor);
} }

View file

@ -250,7 +250,7 @@ void apply_markers_multi(feature_impl const& feature, attributes const& vars,
converter.apply(va, proc); converter.apply(va, proc);
} }
} }
else if ((placement == MARKER_POINT_PLACEMENT || placement == MARKER_INTERIOR_PLACEMENT) && else if ((placement == MARKER_POINT_PLACEMENT || placement == MARKER_INTERIOR_PLACEMENT || placement == MARKER_POLYLABEL_PLACEMENT) &&
multi_policy == MARKER_LARGEST_MULTI) multi_policy == MARKER_LARGEST_MULTI)
{ {
// Only apply to path with largest envelope area // Only apply to path with largest envelope area

View file

@ -68,6 +68,7 @@ static const char * marker_placement_strings[] = {
"vertex-first", "vertex-first",
"vertex-last", "vertex-last",
"angled-point", "angled-point",
"polylabel",
"" ""
}; };
@ -117,6 +118,7 @@ static const char * label_placement_strings[] = {
"line", "line",
"vertex", "vertex",
"interior", "interior",
"polylabel",
"" ""
}; };

View file

@ -31,6 +31,7 @@
#include <mapnik/geometry.hpp> #include <mapnik/geometry.hpp>
#include <mapnik/geometry/geometry_type.hpp> #include <mapnik/geometry/geometry_type.hpp>
#include <mapnik/geometry/centroid.hpp> #include <mapnik/geometry/centroid.hpp>
#include <mapnik/geometry/polylabel.hpp>
#include <mapnik/vertex_processor.hpp> #include <mapnik/vertex_processor.hpp>
#include <mapnik/geom_util.hpp> #include <mapnik/geom_util.hpp>
#include <mapnik/parse_path.hpp> #include <mapnik/parse_path.hpp>
@ -307,6 +308,12 @@ void base_symbolizer_helper::initialize_points() const
points_.emplace_back(label_x, label_y); points_.emplace_back(label_x, label_y);
} }
} }
else if (how_placed == POLYLABEL_PLACEMENT)
{
double precision = geometry::polylabel_precision(tranformed_poly, scale_factor_);
geometry::point<double> pt = geometry::polylabel(tranformed_poly, precision);
points_.emplace_back(pt.x, pt.y);
}
continue; continue;
} }
else else