Merge pull request #3844 from mapycz/interior-scale-invariant
Interior, polylabel: Scale precision by polygon size
This commit is contained in:
commit
d4826eea6f
3 changed files with 16 additions and 19 deletions
|
@ -145,15 +145,8 @@ struct cell
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
boost::optional<point<T>> polylabel(polygon<T> const& polygon, T precision = 1)
|
point<T> polylabel(polygon<T> const& polygon, box2d<T> const& bbox , T precision = 1)
|
||||||
{
|
{
|
||||||
if (polygon.empty() || polygon.front().empty())
|
|
||||||
{
|
|
||||||
return boost::none;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 point<T> size { bbox.width(), bbox.height() };
|
||||||
|
|
||||||
const T cell_size = std::min(size.x, size.y);
|
const T cell_size = std::min(size.x, size.y);
|
||||||
|
@ -169,14 +162,14 @@ boost::optional<point<T>> polylabel(polygon<T> const& polygon, T precision = 1)
|
||||||
|
|
||||||
if (cell_size == 0)
|
if (cell_size == 0)
|
||||||
{
|
{
|
||||||
return point<T>{ bbox.minx(), bbox.miny() };
|
return { bbox.minx(), bbox.miny() };
|
||||||
}
|
}
|
||||||
|
|
||||||
point<T> centroid;
|
point<T> centroid;
|
||||||
if (!mapnik::geometry::centroid(polygon, centroid))
|
if (!mapnik::geometry::centroid(polygon, centroid))
|
||||||
{
|
{
|
||||||
auto center = bbox.center();
|
auto center = bbox.center();
|
||||||
return point<T>{ center.x, center.y };
|
return { center.x, center.y };
|
||||||
}
|
}
|
||||||
|
|
||||||
fitness_functor<T> fitness_func(centroid, size);
|
fitness_functor<T> fitness_func(centroid, size);
|
||||||
|
@ -224,15 +217,18 @@ boost::optional<point<T>> polylabel(polygon<T> const& polygon, T precision = 1)
|
||||||
template <class T>
|
template <class T>
|
||||||
bool interior(polygon<T> const& polygon, double scale_factor, point<T> & pt)
|
bool interior(polygon<T> const& polygon, double scale_factor, point<T> & pt)
|
||||||
{
|
{
|
||||||
// This precision has been chosen to work well in the map (viewport) coordinates.
|
if (polygon.empty() || polygon.front().empty())
|
||||||
double precision = 10.0 * scale_factor;
|
|
||||||
if (boost::optional<point<T>> opt = detail::polylabel(polygon, precision))
|
|
||||||
{
|
{
|
||||||
pt = *opt;
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
const box2d<T> bbox = envelope(polygon.at(0));
|
||||||
|
|
||||||
|
// Let the precision be 1% of the polygon size to be independent to map scale.
|
||||||
|
double precision = (std::max(bbox.width(), bbox.height()) / 100.0) * scale_factor;
|
||||||
|
|
||||||
|
pt = detail::polylabel(polygon, bbox, precision);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template
|
template
|
||||||
|
|
|
@ -29,8 +29,9 @@ namespace mapnik { namespace geometry {
|
||||||
template <class T>
|
template <class T>
|
||||||
T polylabel_precision(polygon<T> const& polygon, double scale_factor)
|
T polylabel_precision(polygon<T> const& polygon, double scale_factor)
|
||||||
{
|
{
|
||||||
// This precision has been chosen to work well in the map (viewport) coordinates.
|
const box2d<T> bbox = mapnik::geometry::envelope(polygon);
|
||||||
return 10.0 * scale_factor;
|
// Let the precision be 1% of the polygon size to be independent to map scale.
|
||||||
|
return (std::max(bbox.width(), bbox.height()) / 100.0) * scale_factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 62747ea33b88a62fbfc90f109cbf1899435e75da
|
Subproject commit edfc559b9775344eb589474861fff6ca340b0c65
|
Loading…
Reference in a new issue