Skip to content

Fix/issue 1410 repair dissolve #1411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 42 additions & 187 deletions extensions/test/algorithms/dissolve.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test

// Copyright (c) 2010-2017 Barend Gehrels, Amsterdam, the Netherlands.
// Copyright (c) 2010-2025 Barend Gehrels, Amsterdam, the Netherlands.

// This file was modified by Oracle on 2022.
// Modifications copyright (c) 2022, Oracle and/or its affiliates.
Expand All @@ -11,8 +11,11 @@
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//#define BOOST_GEOMETRY_DEBUG_ENRICH
//#define BOOST_GEOMETRY_DEBUG_SEGMENT_IDENTIFIER

#if defined(TEST_WITH_GEOJSON)
#define BOOST_GEOMETRY_DEBUG_SEGMENT_IDENTIFIER
#define BOOST_GEOMETRY_DEBUG_IDENTIFIER
#endif

#include <geometry_test_common.hpp>

Expand All @@ -33,18 +36,10 @@
#include <boost/geometry/strategies/strategies.hpp>

#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/geometry/multi/io/wkt/wkt.hpp>

#include <boost/geometry/multi/algorithms/for_each.hpp>

#include <boost/geometry/multi/geometries/multi_linestring.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>


#if defined(TEST_WITH_SVG)
# include <boost/geometry/io/svg/svg_mapper.hpp>
# include <boost/geometry/io/svg/write_svg_multi.hpp>
# include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
#if defined(TEST_WITH_GEOJSON)
#include <boost/geometry/extensions/gis/io/geojson/geojson_writer.hpp>
#include "dissolve_geojson_visitor.hpp"
#endif


Expand Down Expand Up @@ -176,135 +171,6 @@ namespace
std::string const dissolve_ticket10713 = "POLYGON((-0.7189743518829346 4.1308121681213379, 0.0831791982054710 4.1034231185913086, 0.1004156470298767 4.1107301712036133, 0.1044322624802589 4.1026973724365234, 0.0831791982054710 4.1034231185913086, -0.7711903452873230 3.7412264347076416, -0.7189743518829346 4.1308121681213379))";
}


#if defined(TEST_WITH_SVG)
template <typename Mapper>
struct map_visitor
{
map_visitor(Mapper& mapper)
: m_mapper(mapper)
{}

template <typename Turns>
void visit_turns(int phase, Turns const& turns)
{
typedef typename boost::range_value<Turns>::type turn_type;
std::size_t index = 0;
for (turn_type const& turn : turns)
{
switch (phase)
{
case 2 : // after self_turns and enrich
if (turn.discarded)
{
m_mapper.map(turn.point, "fill:rgb(255,128,0);", 2);
}
else
{
m_mapper.map(turn.point, "fill:rgb(255,128,0);"
"stroke:rgb(0,0,0);stroke-width:1", 4);
}
break;
case 3 : // after enrich/traverse
label_turn(index, turn, -2, "fill:rgb(0,0,128);");
break;
}
index++;
}
}

template <typename Clusters, typename Turns>
void visit_clusters(Clusters const& , Turns const& ) {}

template <typename Turns, typename Turn, typename Operation>
void visit_traverse(Turns const& , Turn const& , Operation const& , char const*)
{}

template <typename Turns, typename Turn, typename Operation>
void visit_traverse_reject(Turns const& , Turn const& , Operation const& ,
bg::detail::overlay::traverse_error_type )
{}

template <typename Rings>
void visit_generated_rings(Rings const& rings)
{
typedef typename boost::range_value<Rings>::type ring_type;
for (ring_type const& ring : rings)
{
double const area = bg::area(ring);
std::string const color = area < 0 ? "rgb(255,0,0)" : "rgb(0,0,255)";
std::string const style = "stroke:" + color
+ ";stroke-width:0.1;fill-opacity:0.1;fill:" + color;
m_mapper.map(ring, style);
}
}


private :

template <typename Turn>
bool label_operation(Turn const& turn, std::size_t index, std::ostream& os)
{
os << bg::operation_char(turn.operations[index].operation);
bool result = false;
if (! turn.discarded)
{
os << "->" << turn.operations[index].enriched.get_next_turn_index();
result = true;
}
if (turn.operations[index].enriched.prefer_start)
{
os << "$";
}
if (! turn.operations[index].enriched.startable)
{
os << "@";
}

return result;
}

template <typename Turn>
void label_turn(std::size_t index, Turn const& turn, int y_offset, std::string const& color)
{
std::ostringstream out;
out << index;
if (turn.cluster_id != -1)
{
out << " c=" << turn.cluster_id << " ";
}
bool lab1 = label_operation(turn, 0, out);
out << " / ";
bool lab2 = label_operation(turn, 1, out);
if (turn.discarded)
{
out << "!";
}

std::string font8 = "font-family:Arial;font-size:6px";
std::string font6 = "font-family:Arial;font-size:4px";
std::string style = color + ";" + font8;
if (turn.discarded)
{
style = "fill:rgb(92,92,92);" + font6;
}
else if (turn.cluster_id != -1)
{
style = color + ";" + font8;
}
else if (! lab1 || ! lab2)
{
style = color + ";" + font6;
}

m_mapper.text(turn.point, out.str(), style, 5, y_offset, 6);
}

Mapper& m_mapper;
};

#endif

//! Unittest settings
struct ut_settings
{
Expand Down Expand Up @@ -334,6 +200,17 @@ void test_dissolve(std::string const& caseid, Geometry const& geometry,
std::size_t expected_point_count,
ut_settings const& settings)
{
#if defined(TEST_WITH_GEOJSON)
std::ostringstream filename;
// For QGis, it is usually convenient to always write to the same geojson file.
filename << "/tmp/"
// << caseid << "_"
<< "dissolve.geojson";
std::ofstream geojson_file(filename.str().c_str());

boost::geometry::geojson_writer writer(geojson_file);
#endif

using coordinate_type = typename bg::coordinate_type<Geometry>::type;

static const bool is_line = bg::geometry_id<GeometryOut>::type::value == 2;
Expand All @@ -350,59 +227,21 @@ void test_dissolve(std::string const& caseid, Geometry const& geometry,
Geometry, Geometry
>::type;

using rescale_policy_type = typename bg::rescale_policy_type
<
typename bg::point_type<Geometry>::type
>::type;

rescale_policy_type robust_policy
= bg::get_rescale_policy<rescale_policy_type>(geometry);

// This will optionally also create SVG with turn-debug information
strategy_type strategy;


#if ! defined(TEST_WITH_SVG)
bg::detail::overlay::overlay_null_visitor visitor;
#if defined(TEST_WITH_GEOJSON)
geojson_visitor visitor(writer);
#else
std::ostringstream filename;
filename << "dissolve_" << caseid << "_"
<< string_from_type<coordinate_type>::name()
<< ".svg";

std::ofstream svg(filename.str().c_str());

using mapper_type = bg::svg_mapper
<
typename bg::point_type<Geometry>::type
>;

mapper_type mapper(svg, 500, 500);
mapper.add(geometry);

mapper.map(geometry, "fill-opacity:0.5;fill:rgb(153,204,0);"
"stroke:rgb(153,204,0);stroke-width:2;fill-rule:nonzero;");

map_visitor<mapper_type> visitor(mapper);
#endif
bg::detail::overlay::overlay_null_visitor visitor;
#endif

bg::dispatch::dissolve
<
Geometry,
GeometryOut,
false
>::apply(geometry, robust_policy, std::back_inserter(dissolved1),
>::apply(geometry, std::back_inserter(dissolved1),
strategy, visitor);

#if defined(TEST_WITH_SVG)
for (GeometryOut& dissolved : dissolved1)
{
mapper.map(dissolved, "fill-opacity:0.1;fill:rgb(255,0,0);"
"stroke-opacity:0.4;stroke:rgb(255,0,255);stroke-width:3;"
"fill-rule:nonzero;");
}
#endif

}

if (settings.test_validity)
Expand Down Expand Up @@ -482,8 +321,19 @@ void test_dissolve(std::string const& caseid, Geometry const& geometry,
BOOST_CHECK_MESSAGE(wkt1 == wkt3, caseid << " : output differs: " << wkt1 << " VERSUS " << wkt3);
}
}
}

#if defined(TEST_WITH_GEOJSON)
writer.feature(geometry);
writer.add_property("type", "input");

for (const auto& polygon : dissolved3)
{
writer.feature(polygon);
writer.add_property("type", "dissolved");
}
#endif

}

template <typename Geometry, typename GeometryOut>
void test_one(std::string caseid, std::string const& wkt,
Expand All @@ -505,6 +355,8 @@ void test_one(std::string caseid, std::string const& wkt,
expected_clip_count, expected_hole_count, expected_point_count,
settings);

#ifdef BOOST_GEOMETRY_TEST_REVERSE

// Verify if reversed version is indeed identical (it should, because each
// ring is now corrected within dissolve itself
bg::reverse(geometry);
Expand All @@ -514,6 +366,7 @@ void test_one(std::string caseid, std::string const& wkt,
expected_area,
expected_clip_count, expected_hole_count, expected_point_count,
settings);
#endif

#ifdef BOOST_GEOMETRY_TEST_MULTI_PERMUTATIONS
// Test different combinations of a multi-polygon
Expand Down Expand Up @@ -652,7 +505,9 @@ void test_all(ut_settings const& settings_for_sensitive_cases)

int test_main(int, char* [])
{
#if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
test_all<bg::model::d2::point_xy<float>, true >(ut_settings(0.01));
#endif
test_all<bg::model::d2::point_xy<double>, true >(ut_settings());
// Counter clockwise input does not work correctly in all cases, it is
// partly a problem of the test itself
Expand Down
Loading
Loading