问题
I'm trying to merge some polygons using boost::geometry::union_ but the output vector is empty for some entries.
here is an example :
#include <iostream>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/foreach.hpp>
int main()
{
typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;
polygon green, blue;
boost::geometry::read_wkt(
"POLYGON((440820.110024126 4047009.80267429, 440806.545727707 4046942.39533656, 440797.171880196 4046895.8425726, 440804.202135392 4046888.73092639, 440813.573458131 4046935.27114371, 440827.140279322 4047002.69102807))", green);
boost::geometry::read_wkt(
"POLYGON((440855.857887967 4046932.1248641, 440813.740724389 4046942.21109839, 440806.376538684 4046935.44583646, 440848.493702262 4046925.35960217))", blue);
if(boost::geometry::area(green) < 0) boost::geometry::reverse(green);
if(boost::geometry::area(blue) < 0 ) boost::geometry::reverse(blue);
std::vector<polygon> output;
boost::geometry::union_(green, blue, output);
std::cout << " output size is : " << output.size() << std::endl;
int i = 0;
std::cout << "green || blue:" << std::endl;
BOOST_FOREACH(polygon const& p, output)
{
std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
}
system("pause");
return 0;
}
when I drew the two polygons I obtained :
but the result of the code above was :
output size is : 0
green || blue:
Press any key to continue . . .
is this a bug in this function or there is a mistake in my code ?
回答1:
Boost Geomwetry algorithms, for efficiency, assume that input is 'normalized' or 'canonical'.
So you have to make sure that your input polygons are:
correct(green);
correct(blue);
boost::geometry::union_(green, blue, output);
int i = 0;
for(polygon const& p: output)
{
std::cout << i++ << ": " << boost::geometry::area(p) << "\n";
std::cout << "Shape: " << dsv(p) << "\n";
}
prints:
0: 1289.08
Shape: (((440813, 4.04693e+06), (440804, 4.04689e+06), (440797, 4.0469e+06), (440807, 4.04694e+06), (440820, 4.04701e+06), (440827, 4.047e+06), (440815, 4.04694e+06), (440856, 4.04693e+06), (440848, 4.04693e+06), (440813, 4.04693e+06)))
See it Live on Coliru
Full Example
#include <iostream>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/foreach.hpp>
int main()
{
typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;
using boost::geometry::correct;
using boost::geometry::dsv;
polygon green, blue;
boost::geometry::read_wkt(
"POLYGON((440820.110024126 4047009.80267429, 440806.545727707 4046942.39533656, 440797.171880196 4046895.8425726, 440804.202135392 4046888.73092639, 440813.573458131 4046935.27114371, 440827.140279322 4047002.69102807))",
green);
boost::geometry::read_wkt(
"POLYGON((440855.857887967 4046932.1248641, 440813.740724389 4046942.21109839, 440806.376538684 4046935.44583646, 440848.493702262 4046925.35960217))",
blue);
correct(green);
correct(blue);
std::vector<polygon> output;
boost::geometry::union_(green, blue, output);
int i = 0;
BOOST_FOREACH(polygon const& p, output)
{
std::cout << i++ << ": " << boost::geometry::area(p) << "\n";
std::cout << "Shape: " << dsv(p) << "\n";
}
}
来源:https://stackoverflow.com/questions/22258784/boostgeometryunion-no-result