From 968089d322e7b41808ca5d2d43467626c676c2bf Mon Sep 17 00:00:00 2001
From: Peter Sadrozinski <psadro@albert.arrs.arrisi.com>
Date: Wed, 10 Jun 2015 07:11:09 -0400
Subject: [PATCH] fixes for Martin's tiles that don't clip correctly -
 ogrdecode can create degenerate polys.  clean them before clipping - when
 tesselating with extra points, add constrain pointes and segments   before
 non contrained points.

---
 src/BuildTiles/Main/tgconstruct_poly.cxx   | 3 +++
 src/Lib/terragear/tg_polygon_tesselate.cxx | 6 ++++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/BuildTiles/Main/tgconstruct_poly.cxx b/src/BuildTiles/Main/tgconstruct_poly.cxx
index d39389d3..90d52634 100644
--- a/src/BuildTiles/Main/tgconstruct_poly.cxx
+++ b/src/BuildTiles/Main/tgconstruct_poly.cxx
@@ -112,6 +112,9 @@ int TGConstruct::LoadLandclassPolys( void ) {
                         }
 
                         /* make sure we loaded a valid poly */
+                        poly = tgPolygon::RemoveDups(poly);
+                        poly = tgPolygon::RemoveCycles(poly);
+                        poly = tgPolygon::RemoveDups(poly);
                         poly = tgPolygon::RemoveBadContours(poly);
                         if ( poly.Contours() ) {
                             polys_in.add_poly( area, poly );
diff --git a/src/Lib/terragear/tg_polygon_tesselate.cxx b/src/Lib/terragear/tg_polygon_tesselate.cxx
index 0f262d8f..181f00ac 100644
--- a/src/Lib/terragear/tg_polygon_tesselate.cxx
+++ b/src/Lib/terragear/tg_polygon_tesselate.cxx
@@ -114,8 +114,6 @@ void tgPolygon::Tesselate( const std::vector<SGGeod>& extra )
             points.push_back( Point(extra[n].getLongitudeDeg(), extra[n].getLatitudeDeg() ) );
         }
 
-        cdt.insert(points.begin(), points.end());
-
         // then insert each polygon as a constraint into the triangulation
         for ( unsigned int c = 0; c < contours.size(); c++ ) {
             tgContour contour = contours[c];
@@ -129,6 +127,10 @@ void tgPolygon::Tesselate( const std::vector<SGGeod>& extra )
             tg_insert_polygon(cdt, poly);
         }
 
+        if ( !points.empty() ) {
+            cdt.insert(points.begin(), points.end());
+        }
+        
         /* make conforming - still has an issue, and can't be compiled with exact_construction kernel */
         // CGAL::make_conforming_Delaunay_2( cdt );