[geos-commits] r4129 - in trunk: src/operation/overlay/snap tests/unit/operation/overlay/snap

svn_geos at osgeo.org svn_geos at osgeo.org
Mon Dec 14 09:17:25 PST 2015


Author: strk
Date: 2015-12-14 09:17:25 -0800 (Mon, 14 Dec 2015)
New Revision: 4129

Modified:
   trunk/src/operation/overlay/snap/LineStringSnapper.cpp
   trunk/tests/unit/operation/overlay/snap/LineStringSnapperTest.cpp
Log:
Fix snapping of last segment in a closed linestring

See #758

Modified: trunk/src/operation/overlay/snap/LineStringSnapper.cpp
===================================================================
--- trunk/src/operation/overlay/snap/LineStringSnapper.cpp	2015-12-14 17:17:18 UTC (rev 4128)
+++ trunk/src/operation/overlay/snap/LineStringSnapper.cpp	2015-12-14 17:17:25 UTC (rev 4129)
@@ -153,13 +153,20 @@
     if (vertpos == srcCoords.begin() && isClosed)
     {
       vertpos = srcCoords.end(); --vertpos;
+#if GEOS_DEBUG
+cerr << " Snapped vertex was first in a closed line, also snapping last" << endl;
+#endif
       *vertpos = snapPt;
     }
 
+#if GEOS_DEBUG
+cerr << " After snapping of vertex " << snapPt << ", srcCoors are: " << srcCoords << endl;
+#endif
+
 	}
 
 #if GEOS_DEBUG
-cerr << " After vertex snapping, srcCoors are: " << srcCoords << endl;
+cerr << " After vertices snapping, srcCoors are: " << srcCoords << endl;
 #endif
 
 }
@@ -333,9 +340,11 @@
       LineSegment prevSeg(*segpos, seg.p0);
       if ( prevSeg.distance(newSnapPt) < seg.distance(newSnapPt) ) {
 #if GEOS_DEBUG
-        cerr << " Prev segment closer, inserting " << newSnapPt << " into " << prevSeg << endl;
+        cerr << " Prev segment closer, inserting " << newSnapPt << " into "
+             << prevSeg << endl;
 #endif
         // insert into prev segment
+        ++segpos;
         srcCoords.insert(segpos, newSnapPt);
       } else {
 #if GEOS_DEBUG

Modified: trunk/tests/unit/operation/overlay/snap/LineStringSnapperTest.cpp
===================================================================
--- trunk/tests/unit/operation/overlay/snap/LineStringSnapperTest.cpp	2015-12-14 17:17:18 UTC (rev 4128)
+++ trunk/tests/unit/operation/overlay/snap/LineStringSnapperTest.cpp	2015-12-14 17:17:25 UTC (rev 4129)
@@ -329,4 +329,49 @@
     ensure_equals(ret->operator[](2), src_c);
   }
 
+  // Snap of last segment in closed linestring
+  // See https://trac.osgeo.org/geos/ticket/758
+  template<>
+  template<>
+  void object::test<9>()
+  {
+    using geos::geom::Coordinate;
+    using geos::operation::overlay::snap::LineStringSnapper;
+
+    typedef std::auto_ptr<Coordinate::Vect> CoordsVectAptr;
+
+
+    // Source: (1 1, 5 9, 9 1, 1 1)
+    Coordinate src_a(1, 1);
+    Coordinate src_b(5, 9);
+    Coordinate src_c(9, 1);
+    Coordinate::Vect srcCoords;
+    srcCoords.push_back(src_a);
+    srcCoords.push_back(src_b);
+    srcCoords.push_back(src_c);
+    srcCoords.push_back(src_a);
+
+    // Snap: (0 0, 10 0, 1 0.5)
+    Coordinate snp_a(0, 0);
+    Coordinate snp_b(10, 0);
+    Coordinate snp_c(1, 0.5);
+    Coordinate::ConstVect snpCoords;
+    snpCoords.push_back( &snp_a );
+    snpCoords.push_back( &snp_b );
+    snpCoords.push_back( &snp_c );
+
+    // Snap with tolerance of 2
+    // (both first and second point could be snapped)
+    LineStringSnapper snapper(srcCoords, 2);
+
+    // Expect: (0 0, 5 9, 10 0, 1 0.5, 0 0)
+    CoordsVectAptr ret(snapper.snapTo(snpCoords));
+    ensure_equals(ret->size(), 5u);
+    ensure_equals(ret->operator[](0), snp_a); //  0 0
+    ensure_equals(ret->operator[](1), src_b); //  5 9
+    ensure_equals(ret->operator[](2), snp_b); // 10 0
+    ensure_equals(ret->operator[](3), snp_c); //  1 0.5
+    ensure_equals(ret->operator[](4), snp_a); //  0 0
+  }
+
 } // namespace tut



More information about the geos-commits mailing list