title: How do I combine complex polygons?
body: <p>Given two polygons:</p>
  
  <pre><code>POLYGON((1 0, 1 8, 6 4, 1 0))
  POLYGON((4 1, 3 5, 4 9, 9 5, 4 1),(4 5, 5 7, 6 7, 4 4, 4 5))
  </code></pre>
  
  <p>How can I calculate the union (combined polygon)?</p>
  
  <p><a href="https://i.stack.imgur.com/N5P2R.png" rel="noreferrer"><img src="https://i.stack.imgur.com/N5P2R.png" alt="enter image description here"></a></p>
  
  <p><a href="http://blogs.msdn.com/davidlean/archive/2008/10/25/sql-2008-spatial-samples-part-n-2-on-n-geometric-set-theory-methods.aspx" rel="noreferrer">Dave's example</a> uses SQL server to produce the union, but I need to accomplish the same in code. I'm looking for a mathematical formula or code example in any language that exposes the actual math. I am attempting to produce maps that combine countries dynamically into regions. I asked a related question here: <a href="https://stackoverflow.com/questions/2653812/grouping-geographical-shapes">Grouping geographical shapes</a></p>