Ticket #10398: main_boost_geometry_polygon_offset_problem.cpp

File main_boost_geometry_polygon_offset_problem.cpp, 3.1 KB (added by Olivier Tournaire <olivier.tournaire@…>, 8 years ago)

Offsetting with buffer strategies for several polygons and distances

Line 
1#include <iostream>
2
3#include <boost/geometry/geometry.hpp>
4
5#include <boost/geometry/geometries/point_xy.hpp>
6#include <boost/geometry/geometries/polygon.hpp>
7#include <boost/geometry/geometries/multi_polygon.hpp>
8
9#include <boost/geometry/algorithms/buffer.hpp>
10
11#include <boost/geometry/strategies/cartesian/buffer_join_miter.hpp>
12#include <boost/geometry/strategies/cartesian/buffer_end_flat.hpp>
13#include <boost/geometry/strategies/cartesian/buffer_point_square.hpp>
14#include <boost/geometry/strategies/cartesian/buffer_side_straight.hpp>
15
16#include <boost/geometry/io/wkt/read.hpp>
17#include <boost/geometry/io/wkt/write.hpp>
18
19using namespace std;
20
21int main(int argc, char** argv)
22{
23 typedef double coordinate_type;
24 typedef boost::geometry::model::d2::point_xy<coordinate_type> point_xy_type;
25 typedef boost::geometry::model::polygon<point_xy_type> polygon_type;
26 boost::geometry::strategy::buffer::end_flat end_strategy;
27 boost::geometry::strategy::buffer::point_square point_strategy;
28 boost::geometry::strategy::buffer::side_straight side_strategy;
29
30 vector<string> polygon_geometries;
31 polygon_geometries.push_back("POLYGON((897866.5 6272518.7,897882.5 6272519.2,897882.6 6272519,897883.3 6272508.7,897883.5 6272505.5,897855 6272503.5,897852.4 6272505.6,897850.1 6272517.6,897860.8 6272518.5,897866.5 6272518.7))");
32 polygon_geometries.push_back("POLYGON((898882.3 6271337.3,898895.7 6271339.9,898898 6271328.3,898881.6 6271325.1,898879.3 6271336.7,898882.3 6271337.3))");
33 polygon_geometries.push_back("POLYGON((897558.7 6272055,897552.5 6272054.2,897552.5 6272053.7,897546.1 6272052.7,897545.6 6272057.7,897560.7 6272059.6,897560.9 6272055.3,897558.7 6272055))");
34 polygon_geometries.push_back("POLYGON((898563.3 6272366.9,898554.7 6272379.2,898559.7 6272382.3,898561.6 6272379.4,898568.7 6272369.1,898563.8 6272366.2,898563.3 6272366.9))");
35
36 for(unsigned int i=0;i<polygon_geometries.size();++i)
37 {
38 for(unsigned int j=0;j<100;++j)
39 {
40 double offset_distance = j/10.;
41
42 boost::geometry::strategy::buffer::distance_symmetric<coordinate_type> distance_strategy(offset_distance);
43 boost::geometry::strategy::buffer::join_miter join_strategy(offset_distance);
44
45 polygon_type input;
46 boost::geometry::model::multi_polygon<polygon_type> output;
47
48 boost::geometry::read_wkt(polygon_geometries[i], input);
49 // Just to give it a try, but does not change anything
50 //boost::geometry::correct(input);
51
52 /*
53 for(unsigned int k=0;k<input.outer().size();++k)
54 input.outer()[k] = point_xy_type(input.outer()[k].x() - input.outer()[0].x() , input.outer()[k].y() - input.outer()[0].y());
55 */
56
57 boost::geometry::buffer(input, output, distance_strategy, side_strategy, join_strategy, end_strategy, point_strategy);
58
59 if(output.size() == 0)
60 cout << "Unable to offset (" << offset_distance << ") input polygon with index " << i << endl;
61 }
62 }
63
64 return 0;
65}