title: Boost::Geometry: how to join intersecting polygons in a multi_polygon?
body: <p>So I want to join all interrelated poligons in a multi_polygon. How to do such thing?</p>
  
  <p>We have such image (of one green multi_polygon) which we want to optimize (we can see yellow doted lines - result of simplification that apparently was performed on each poligon of multi_polygon not on multi_polygon in general):</p>
  
  <p><img src="https://i.stack.imgur.com/UxaLH.jpg" alt="enter image description here"></p>
  
  <p>And here is compilable code to generate such image:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;boost/assign.hpp&gt;
  
  #include &lt;boost/algorithm/string.hpp&gt;
  #include &lt;boost/geometry/geometry.hpp&gt;
  #include &lt;boost/geometry/geometries/geometries.hpp&gt;
  #include &lt;boost/geometry/multi/geometries/multi_polygon.hpp&gt;
  
  #include &lt;boost/geometry/extensions/io/svg/svg_mapper.hpp&gt;
  
  template &lt;typename Geometry1, typename Geometry2&gt;
  void create_svg(std::string const&amp; filename, Geometry1 const&amp; a, Geometry2 const&amp; b)
  {
      typedef typename boost::geometry::point_type&lt;Geometry1&gt;::type point_type;
      std::ofstream svg(filename.c_str());
  
      boost::geometry::svg_mapper&lt;point_type&gt; mapper(svg, 400, 400);
      mapper.add(a);
      mapper.add(b);
  
      mapper.map(a, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2");
      mapper.map(b, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
  }
  
  
  boost::geometry::model::polygon&lt;boost::geometry::model::d2::point_xy&lt;double&gt; &gt; make_point(int x, int y)
  {
      boost::geometry::model::polygon&lt;boost::geometry::model::d2::point_xy&lt;double&gt; &gt; return_item;
      boost::geometry::model::d2::point_xy&lt;double&gt; p1(x, y);
      boost::geometry::model::d2::point_xy&lt;double&gt; p2(x-1, y);
      boost::geometry::model::d2::point_xy&lt;double&gt; p3(x-1, y-1);
      boost::geometry::model::d2::point_xy&lt;double&gt; p4(x, y-1);
  
      boost::geometry::append(  return_item, p1);
      boost::geometry::append(  return_item, p2);
      boost::geometry::append(  return_item, p3);
      boost::geometry::append(  return_item, p4);
  
      return return_item;
  
  }
  
  int main()
  {
      // create a container for joined points structure
      boost::geometry::model::multi_polygon&lt; boost::geometry::model::polygon&lt;boost::geometry::model::d2::point_xy&lt;double&gt; &gt; &gt; output, simpl;
  
      // join points one by one (because one day we would have many=))
      output.push_back(make_point(1,1));
      boost::geometry::correct(output);
  
      output.push_back(make_point(2,1));
      boost::geometry::correct(output);
  
      output.push_back(make_point(3,1));
      boost::geometry::correct(output);
  
      output.push_back(make_point(4,1));
      boost::geometry::correct(output);
  
      output.push_back(make_point(5,1));
      boost::geometry::correct(output);
  
      output.push_back(make_point(2,2));
      boost::geometry::correct(output);
  
      output.push_back(make_point(3,2));
      boost::geometry::correct(output);
  
      output.push_back(make_point(5,2));
      boost::geometry::correct(output);
  
      output.push_back(make_point(5,5));
      boost::geometry::correct(output);
  
  
      // simplify joined structure
      boost::geometry::simplify(output, simpl, 0.5);
  
      // create an svg image
      create_svg("make_envelope.svg",  output, simpl );
  }
  </code></pre>
  
  <p>requires at least boost 1.47.0 and 3 files from  <a href="http://svn.boost.org/svn/boost/trunk/boost/geometry/extensions/io/svg/" rel="nofollow noreferrer">boost/geometry/extensions/io/svg/</a></p>
  
  <p>what I need is simple: how to group interrelated poligons? In this case we shall get 2 poligons in our multy_poligon like shown here - red and green:</p>
  
  <p><img src="https://i.stack.imgur.com/CFLeI.jpg" alt="enter image description here"></p>
  
  <h1>Update:</h1>
  
  <p>So I found this info on <a href="http://barendgehrels.blogspot.com/2011/02/dissolving-pentagram.html" rel="nofollow noreferrer">dissolve</a> and created sample code that uses rings for cels creation:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;boost/assign.hpp&gt;
  
  //Boost
  #include &lt;boost/algorithm/string.hpp&gt;
  #include &lt;boost/geometry/geometry.hpp&gt;
  #include &lt;boost/geometry/geometries/geometries.hpp&gt;
  #include &lt;boost/geometry/multi/geometries/multi_polygon.hpp&gt;
  #include &lt;boost/geometry/geometries/adapted/boost_tuple.hpp&gt;
  
  BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  
  #include &lt;boost/foreach.hpp&gt;
  
  //Boost Geometry extensions (from trunk) 
  #include &lt;boost/geometry/extensions/io/svg/svg_mapper.hpp&gt;
  
  template &lt;typename Geometry1, typename Geometry2&gt;
  void create_svg(std::string const&amp; filename, Geometry1 const&amp; a, Geometry2 const&amp; b)
  {
      typedef typename boost::geometry::point_type&lt;Geometry1&gt;::type point_type;
      std::ofstream svg(filename.c_str());
  
      boost::geometry::svg_mapper&lt;point_type&gt; mapper(svg, 400, 400);
      mapper.add(a);
      mapper.add(b);
  
      mapper.map(a, "fill-rule:nonzero;fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2;");
      mapper.map(b, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
  }
  
  void make_point(int x, int y, boost::geometry::model::ring&lt;boost::geometry::model::d2::point_xy&lt;double&gt; &gt; &amp; ring)
  {
      using namespace boost::assign;
  
      ring += 
          boost::geometry::model::d2::point_xy&lt;double&gt;(x-1, y-1),
          boost::geometry::model::d2::point_xy&lt;double&gt;(x, y-1),
          boost::geometry::model::d2::point_xy&lt;double&gt;(x, y),
          boost::geometry::model::d2::point_xy&lt;double&gt;(x-1, y),
          boost::geometry::model::d2::point_xy&lt;double&gt;(x-1, y-1);
  
  }
  
  int main()
  {
      using namespace boost::assign;
      boost::geometry::model::ring&lt;boost::geometry::model::d2::point_xy&lt;double&gt; &gt; ring0, ring1,ring;
  
      boost::geometry::model::multi_polygon&lt; boost::geometry::model::polygon&lt;boost::geometry::model::d2::point_xy&lt;double&gt; &gt; &gt; outputw;
  
      make_point(1, 1, ring) ;
      make_point(2, 1, ring) ;
      make_point(3, 1, ring) ;
      make_point(4, 1, ring) ;
      make_point(5, 1, ring) ;
      make_point(2, 2, ring) ;
      make_point(3, 2, ring) ;
      make_point(5, 2, ring) ;
  
      boost::geometry::model::ring&lt;boost::geometry::model::d2::point_xy&lt;double&gt; &gt; output;
      boost::geometry::simplify(ring, output, 1);
  
      // create an svg image
      create_svg("make_envelope.svg",  ring, output );
  }
  </code></pre>
  
  <p>It returns such image of ring:</p>
  
  <p><img src="https://i.stack.imgur.com/OKM85.jpg" alt="enter image description here"></p>
  
  <p>If we could use dissolve to turn it into poligon that would really solve some of my problems. But looks like currently we can not due to this compiler errors problem described <a href="https://stackoverflow.com/questions/8055873/is-it-me-or-part-of-boost-track-svn-boost-geometry-extension-dissolve-is-uncom">here</a> </p>