Merge branch 'polylabel-as-interior' of https://github.com/mapycz/mapnik into mapycz-polylabel-as-interior

This commit is contained in:
Artem Pavlenko 2018-01-08 10:21:09 +00:00
commit c24568f033
22 changed files with 556 additions and 93 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

@ -537,80 +537,6 @@ bool hit_test(PathType & path, double x, double y, double tol)
return inside; return inside;
} }
template <typename PathType>
bool interior_position(PathType & path, double & x, double & y)
{
// start with the centroid
if (!label::centroid(path, x,y))
return false;
// otherwise we find a horizontal line across the polygon and then return the
// center of the widest intersection between the polygon and the line.
std::vector<double> intersections; // only need to store the X as we know the y
geometry::point<double> p0, p1, move_to;
unsigned command = SEG_END;
path.rewind(0);
while (SEG_END != (command = path.vertex(&p0.x, &p0.y)))
{
switch (command)
{
case SEG_MOVETO:
move_to = p0;
break;
case SEG_CLOSE:
p0 = move_to;
case SEG_LINETO:
// if the segments overlap
if (p0.y == p1.y)
{
if (p0.y == y)
{
double xi = (p0.x + p1.x) / 2.0;
intersections.push_back(xi);
}
}
// if the path segment crosses the bisector
else if ((p0.y <= y && p1.y >= y) ||
(p0.y >= y && p1.y <= y))
{
// then calculate the intersection
double xi = p0.x;
if (p0.x != p1.x)
{
double m = (p1.y - p0.y) / (p1.x - p0.x);
double c = p0.y - m * p0.x;
xi = (y - c) / m;
}
intersections.push_back(xi);
}
break;
}
p1 = p0;
}
// no intersections we just return the default
if (intersections.empty())
return true;
std::sort(intersections.begin(), intersections.end());
double max_width = 0;
for (unsigned ii = 1; ii < intersections.size(); ii += 2)
{
double xlow = intersections[ii-1];
double xhigh = intersections[ii];
double width = xhigh - xlow;
if (width > max_width)
{
x = (xlow + xhigh) / 2.0;
max_width = width;
}
}
return true;
}
}} }}
#endif // MAPNIK_GEOM_UTIL_HPP #endif // MAPNIK_GEOM_UTIL_HPP

View file

@ -0,0 +1,36 @@
/*****************************************************************************
*
* 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_INTERIOR_HPP
#define MAPNIK_GEOMETRY_INTERIOR_HPP
#include <mapnik/geometry/polygon.hpp>
#include <mapnik/geometry/point.hpp>
namespace mapnik { namespace geometry {
template <class T>
point<T> interior(polygon<T> const& polygon, double scale_factor);
} }
#endif // MAPNIK_GEOMETRY_INTERIOR_HPP

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

@ -26,6 +26,8 @@
#include <mapnik/markers_placements/point.hpp> #include <mapnik/markers_placements/point.hpp>
#include <mapnik/geom_util.hpp> #include <mapnik/geom_util.hpp>
#include <mapnik/geometry/geometry_types.hpp> #include <mapnik/geometry/geometry_types.hpp>
#include <mapnik/geometry/interior.hpp>
#include <mapnik/geometry/polygon_vertex_processor.hpp>
namespace mapnik { namespace mapnik {
@ -58,11 +60,13 @@ public:
} }
else else
{ {
if (!label::interior_position(this->locator_, x, y)) geometry::polygon_vertex_processor<double> vertex_processor;
{ vertex_processor.add_path(this->locator_);
this->done_ = true; geometry::point<double> placement = geometry::interior(vertex_processor.polygon_,
return false; this->params_.scale_factor);
}
x = placement.x;
y = placement.y;
} }
angle = 0; angle = 0;

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

@ -30,6 +30,7 @@
#include <mapnik/marker_cache.hpp> #include <mapnik/marker_cache.hpp>
#include <mapnik/label_collision_detector.hpp> #include <mapnik/label_collision_detector.hpp>
#include <mapnik/geometry/centroid.hpp> #include <mapnik/geometry/centroid.hpp>
#include <mapnik/geometry/interior.hpp>
#include <mapnik/geometry/geometry_type.hpp> #include <mapnik/geometry/geometry_type.hpp>
#include <mapnik/geometry/geometry_types.hpp> #include <mapnik/geometry/geometry_types.hpp>
#include <mapnik/vertex_adapters.hpp> #include <mapnik/vertex_adapters.hpp>
@ -79,9 +80,7 @@ void render_point_symbolizer(point_symbolizer const &sym,
else if (type == mapnik::geometry::geometry_types::Polygon) else if (type == mapnik::geometry::geometry_types::Polygon)
{ {
auto const& poly = mapnik::util::get<geometry::polygon<double> >(geometry); auto const& poly = mapnik::util::get<geometry::polygon<double> >(geometry);
geometry::polygon_vertex_adapter<double> va(poly); pt = geometry::interior(poly, common.scale_factor_);
if (!label::interior_position(va ,pt.x, pt.y))
return;
} }
else else
{ {

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,8 @@ source = Split(
geometry/closest_point.cpp geometry/closest_point.cpp
geometry/reprojection.cpp geometry/reprojection.cpp
geometry/envelope.cpp geometry/envelope.cpp
geometry/interior.cpp
geometry/polylabel.cpp
expression_node.cpp expression_node.cpp
expression_string.cpp expression_string.cpp
expression.cpp expression.cpp

226
src/geometry/interior.cpp Normal file
View file

@ -0,0 +1,226 @@
/*****************************************************************************
*
* 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/interior.hpp>
#include <mapnik/geometry/envelope.hpp>
#include <mapnik/geometry/box2d.hpp>
#include <mapbox/geometry/point_arithmetic.hpp>
#include <mapnik/geometry/centroid.hpp>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
namespace mapnik { namespace geometry {
// Interior algorithm is realized as a modification of Polylabel algorithm
// from https://github.com/mapbox/polylabel.
// The modification aims to improve visual output by prefering
// placements closer to centroid.
namespace detail {
// get squared distance from a point to a segment
template <class T>
T segment_dist_sq(const point<T>& p,
const point<T>& a,
const point<T>& b)
{
auto x = a.x;
auto y = a.y;
auto dx = b.x - x;
auto dy = b.y - y;
if (dx != 0 || dy != 0) {
auto t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = b.x;
y = b.y;
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
dx = p.x - x;
dy = p.y - y;
return dx * dx + dy * dy;
}
// signed distance from point to polygon outline (negative if point is outside)
template <class T>
auto point_to_polygon_dist(const point<T>& point, const polygon<T>& polygon)
{
bool inside = false;
auto min_dist_sq = std::numeric_limits<double>::infinity();
for (const auto& ring : polygon)
{
for (std::size_t i = 0, len = ring.size(), j = len - 1; i < len; j = i++)
{
const auto& a = ring[i];
const auto& b = ring[j];
if ((a.y > point.y) != (b.y > point.y) &&
(point.x < (b.x - a.x) * (point.y - a.y) / (b.y - a.y) + a.x)) inside = !inside;
min_dist_sq = std::min(min_dist_sq, segment_dist_sq(point, a, b));
}
}
return (inside ? 1 : -1) * std::sqrt(min_dist_sq);
}
template <class T>
struct fitness_functor
{
fitness_functor(point<T> const& centroid, point<T> const& polygon_size)
: centroid(centroid),
max_size(std::max(polygon_size.x, polygon_size.y))
{}
T operator()(const point<T>& cell_center, T distance_polygon) const
{
if (distance_polygon <= 0)
{
return distance_polygon;
}
point<T> d = cell_center - centroid;
double distance_centroid = std::sqrt(d.x * d.x + d.y * d.y);
return distance_polygon * (1 - distance_centroid / max_size);
}
point<T> centroid;
T max_size;
};
template <class T>
struct cell
{
template <class FitnessFunc>
cell(const point<T>& c_, T h_,
const polygon<T>& polygon,
const FitnessFunc& ff)
: c(c_),
h(h_),
d(point_to_polygon_dist(c, polygon)),
fitness(ff(c, d)),
max_fitness(ff(c, d + h * std::sqrt(2)))
{}
point<T> c; // cell center
T h; // half the cell size
T d; // distance from cell center to polygon
T fitness; // fitness of the cell center
T max_fitness; // a "potential" of the cell calculated from max distance to polygon within the cell
};
template <class T>
point<T> polylabel(const polygon<T>& polygon, T precision = 1)
{
// find the bounding box of the outer ring
const box2d<T> bbox = envelope(polygon.at(0));
const point<T> size { bbox.width(), bbox.height() };
const T cell_size = std::min(size.x, size.y);
T h = cell_size / 2;
// a priority queue of cells in order of their "potential" (max distance to polygon)
auto compare_func = [] (const cell<T>& a, const cell<T>& b)
{
return a.max_fitness < b.max_fitness;
};
using Queue = std::priority_queue<cell<T>, std::vector<cell<T>>, decltype(compare_func)>;
Queue queue(compare_func);
if (cell_size == 0)
{
return { bbox.minx(), bbox.miny() };
}
point<T> centroid;
if (!mapnik::geometry::centroid(polygon, centroid))
{
auto center = bbox.center();
return { center.x, center.y };
}
fitness_functor<T> fitness_func(centroid, size);
// cover polygon with initial cells
for (T x = bbox.minx(); x < bbox.maxx(); x += cell_size)
{
for (T y = bbox.miny(); y < bbox.maxy(); y += cell_size)
{
queue.push(cell<T>({x + h, y + h}, h, polygon, fitness_func));
}
}
// take centroid as the first best guess
auto best_cell = cell<T>(centroid, 0, polygon, fitness_func);
while (!queue.empty())
{
// pick the most promising cell from the queue
auto current_cell = queue.top();
queue.pop();
// update the best cell if we found a better one
if (current_cell.fitness > best_cell.fitness)
{
best_cell = current_cell;
}
// do not drill down further if there's no chance of a better solution
if (current_cell.max_fitness - best_cell.fitness <= precision) continue;
// split the cell into four cells
h = current_cell.h / 2;
queue.push(cell<T>({current_cell.c.x - h, current_cell.c.y - h}, h, polygon, fitness_func));
queue.push(cell<T>({current_cell.c.x + h, current_cell.c.y - h}, h, polygon, fitness_func));
queue.push(cell<T>({current_cell.c.x - h, current_cell.c.y + h}, h, polygon, fitness_func));
queue.push(cell<T>({current_cell.c.x + h, current_cell.c.y + h}, h, polygon, fitness_func));
}
return best_cell.c;
}
} // namespace detail
template <class T>
point<T> interior(polygon<T> const& polygon, double scale_factor)
{
// This precision has been chosen to work well in the map (viewport) coordinates.
double precision = 10.0 * scale_factor;
return detail::polylabel(polygon, precision);
}
template
point<double> interior(polygon<double> const& polygon, double scale_factor);
} }

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

@ -228,7 +228,8 @@ markers_dispatch_params::markers_dispatch_params(box2d<double> const& size,
get<value_double, keys::max_error>(sym, feature, vars), get<value_double, keys::max_error>(sym, feature, vars),
get<value_bool, keys::allow_overlap>(sym, feature, vars), get<value_bool, keys::allow_overlap>(sym, feature, vars),
get<value_bool, keys::avoid_edges>(sym, feature, vars), get<value_bool, keys::avoid_edges>(sym, feature, vars),
get<direction_enum, keys::direction>(sym, feature, vars)} get<direction_enum, keys::direction>(sym, feature, vars),
scale}
, placement_method(get<marker_placement_enum, keys::markers_placement_type>(sym, feature, vars)) , placement_method(get<marker_placement_enum, keys::markers_placement_type>(sym, feature, vars))
, ignore_placement(get<value_bool, keys::ignore_placement>(sym, feature, vars)) , ignore_placement(get<value_bool, keys::ignore_placement>(sym, feature, vars))
, snap_to_pixels(snap) , snap_to_pixels(snap)

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,8 @@
#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/interior.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>
@ -40,6 +42,10 @@
#include <mapnik/text/placement_finder_impl.hpp> #include <mapnik/text/placement_finder_impl.hpp>
#include <mapnik/text/placements/base.hpp> #include <mapnik/text/placements/base.hpp>
#include <mapnik/text/placements/dummy.hpp> #include <mapnik/text/placements/dummy.hpp>
#include <mapnik/geometry/transform.hpp>
#include <mapnik/geometry/strategy.hpp>
#include <mapnik/proj_strategy.hpp>
#include <mapnik/view_strategy.hpp>
namespace mapnik { namespace mapnik {
namespace geometry { namespace geometry {
@ -286,11 +292,27 @@ void base_symbolizer_helper::initialize_points() const
success = true; success = true;
} }
} }
else if (how_placed == INTERIOR_PLACEMENT && type == geometry::geometry_types::Polygon) else if (type == geometry::geometry_types::Polygon)
{ {
auto const& poly = mapnik::util::get<geometry::polygon<double> >(geom); auto const& poly = util::get<geometry::polygon<double>>(geom);
geometry::polygon_vertex_adapter<double> va(poly); proj_transform backwart_transform(prj_trans_.dest(), prj_trans_.source());
success = label::interior_position(va, label_x, label_y); view_strategy vs(t_);
proj_strategy ps(backwart_transform);
using transform_group_type = geometry::strategy_group<proj_strategy, view_strategy>;
transform_group_type transform_group(ps, vs);
geometry::polygon<double> tranformed_poly(geometry::transform<double>(poly, transform_group));
if (how_placed == INTERIOR_PLACEMENT)
{
geometry::point<double> pt = geometry::interior(tranformed_poly, scale_factor_);
points_.emplace_back(pt.x, pt.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;
} }
else else
{ {

@ -1 +1 @@
Subproject commit 22db436a656a78fb1e142a6471609b3cb482e510 Subproject commit 62747ea33b88a62fbfc90f109cbf1899435e75da

View file

@ -23,7 +23,7 @@ SECTION("empty geometry") {
mapnik::markers_placement_params params { mapnik::markers_placement_params params {
mapnik::box2d<double>(0, 0, 10, 10), mapnik::box2d<double>(0, 0, 10, 10),
agg::trans_affine(), agg::trans_affine(),
0, 0, false, false, DIRECTION_AUTO }; 0, 0, false, false, DIRECTION_AUTO, 1.0 };
placement_type placement(va, detector, params); placement_type placement(va, detector, params);
@ -45,7 +45,7 @@ SECTION("point") {
mapnik::markers_placement_params params { mapnik::markers_placement_params params {
mapnik::box2d<double>(0, 0, 10, 10), mapnik::box2d<double>(0, 0, 10, 10),
agg::trans_affine(), agg::trans_affine(),
0, 0, false, false, DIRECTION_AUTO }; 0, 0, false, false, DIRECTION_AUTO, 1.0 };
placement_type placement(va, detector, params); placement_type placement(va, detector, params);
@ -75,7 +75,7 @@ SECTION("line string") {
mapnik::markers_placement_params params { mapnik::markers_placement_params params {
mapnik::box2d<double>(0, 0, 10, 10), mapnik::box2d<double>(0, 0, 10, 10),
agg::trans_affine(), agg::trans_affine(),
0, 0, false, false, DIRECTION_AUTO }; 0, 0, false, false, DIRECTION_AUTO, 1.0 };
placement_type placement(va, detector, params); placement_type placement(va, detector, params);
@ -109,7 +109,7 @@ SECTION("polygon") {
mapnik::markers_placement_params params { mapnik::markers_placement_params params {
mapnik::box2d<double>(0, 0, 10, 10), mapnik::box2d<double>(0, 0, 10, 10),
agg::trans_affine(), agg::trans_affine(),
0, 0, false, false, DIRECTION_AUTO }; 0, 0, false, false, DIRECTION_AUTO, 1.0 };
placement_type placement(va, detector, params); placement_type placement(va, detector, params);