[geos-commits] r2457 - in trunk: source/geom
source/headers/geos/geom tests/unit/geom
svn_geos at osgeo.org
svn_geos at osgeo.org
Sat May 2 06:59:19 EDT 2009
Author: strk
Date: 2009-05-02 06:59:19 -0400 (Sat, 02 May 2009)
New Revision: 2457
Modified:
trunk/source/geom/CoordinateArraySequence.cpp
trunk/source/headers/geos/geom/CoordinateArraySequence.h
trunk/source/headers/geos/geom/CoordinateSequence.h
trunk/tests/unit/geom/CoordinateArraySequenceTest.cpp
Log:
Add an insert-like virtual method to CoordinateSequence. This comes from CoordinateList of JTS, historically bound to CoordinateSequence in GEOS. Add test for that interface.
Modified: trunk/source/geom/CoordinateArraySequence.cpp
===================================================================
--- trunk/source/geom/CoordinateArraySequence.cpp 2009-05-02 09:44:32 UTC (rev 2456)
+++ trunk/source/geom/CoordinateArraySequence.cpp 2009-05-02 10:59:19 UTC (rev 2457)
@@ -98,6 +98,29 @@
vect->push_back(c);
}
+/*public*/
+void
+CoordinateArraySequence::add(size_t i, const Coordinate& coord,
+ bool allowRepeated)
+{
+ // don't add duplicate coordinates
+ if (! allowRepeated) {
+ size_t sz = size();
+ if (sz > 0) {
+ if (i > 0) {
+ const Coordinate& prev = getAt(i - 1);
+ if (prev.equals2D(coord)) return;
+ }
+ if (i < sz) {
+ const Coordinate& next = getAt(i);
+ if (next.equals2D(coord)) return;
+ }
+ }
+ }
+
+ vect->insert(vect->begin()+i, coord);
+}
+
size_t
CoordinateArraySequence::getSize() const
{
Modified: trunk/source/headers/geos/geom/CoordinateArraySequence.h
===================================================================
--- trunk/source/headers/geos/geom/CoordinateArraySequence.h 2009-05-02 09:44:32 UTC (rev 2456)
+++ trunk/source/headers/geos/geom/CoordinateArraySequence.h 2009-05-02 10:59:19 UTC (rev 2457)
@@ -69,6 +69,19 @@
virtual void add(const Coordinate& c, bool allowRepeated);
+ /** \brief
+ * Inserts the specified coordinate at the specified position in
+ * this list.
+ *
+ * @param i the position at which to insert
+ * @param coord the coordinate to insert
+ * @param allowRepeated if set to false, repeated coordinates are
+ * collapsed
+ *
+ * NOTE: this is a CoordinateList interface in JTS
+ */
+ virtual void add(size_t i, const Coordinate& coord, bool allowRepeated);
+
void setAt(const Coordinate& c, size_t pos);
void deleteAt(size_t pos);
Modified: trunk/source/headers/geos/geom/CoordinateSequence.h
===================================================================
--- trunk/source/headers/geos/geom/CoordinateSequence.h 2009-05-02 09:44:32 UTC (rev 2456)
+++ trunk/source/headers/geos/geom/CoordinateSequence.h 2009-05-02 10:59:19 UTC (rev 2457)
@@ -180,6 +180,19 @@
*/
virtual void add(const Coordinate& c, bool allowRepeated);
+ /** \brief
+ * Inserts the specified coordinate at the specified position in
+ * this list.
+ *
+ * @param i the position at which to insert
+ * @param coord the coordinate to insert
+ * @param allowRepeated if set to false, repeated coordinates are
+ * collapsed
+ *
+ * NOTE: this is a CoordinateList interface in JTS
+ */
+ virtual void add(size_t i, const Coordinate& coord, bool allowRepeated)=0;
+
/// Returns <code>true</code> it list contains no coordinates.
virtual bool isEmpty() const=0;
Modified: trunk/tests/unit/geom/CoordinateArraySequenceTest.cpp
===================================================================
--- trunk/tests/unit/geom/CoordinateArraySequenceTest.cpp 2009-05-02 09:44:32 UTC (rev 2456)
+++ trunk/tests/unit/geom/CoordinateArraySequenceTest.cpp 2009-05-02 10:59:19 UTC (rev 2457)
@@ -10,6 +10,7 @@
// std
#include <string>
#include <vector>
+#include <iostream>
namespace tut
{
@@ -557,4 +558,52 @@
}
+ // Test of add() in the middle
+ template<>
+ template<>
+ void object::test<16>()
+ {
+ using geos::geom::Coordinate;
+
+ // Create empty sequence to fill with coordinates
+ const size_t size = 0;
+ geos::geom::CoordinateArraySequence sequence;
+
+ sequence.add(Coordinate(0,0));
+ sequence.add(Coordinate(1,1));
+ sequence.add(Coordinate(2,2));
+
+ ensure_equals( sequence.size(), 3 );
+
+ sequence.add(0, Coordinate(4,4), false); // don't alow repeated
+ ensure_equals( sequence.size(), 4 );
+ ensure_equals( sequence.getAt(0).x, 4 );
+
+ // do not allow repeated
+ sequence.add(0, Coordinate(4,4), false);
+ ensure_equals( sequence.size(), 4 );
+
+ // allow repeated
+ sequence.add(0, Coordinate(4,4), true);
+ ensure_equals( sequence.size(), 5 );
+
+ // Now looks like this: 4,4,0,1,2
+ // we'll add at position 4 a 2 (equals to the one after)
+ sequence.add(4, Coordinate(2,2), false);
+ ensure_equals( sequence.size(), 5 );
+
+ // we'll add at position 4 a 1 (equals to the one before)
+ sequence.add(4, Coordinate(1,1), false);
+ ensure_equals( sequence.size(), 5 );
+
+ // we'll add at position 4 a 1 (equals to the one before)
+ // but allowing duplicates
+ sequence.add(4, Coordinate(1,1), true);
+ ensure_equals( sequence.size(), 6 );
+ ensure_equals( sequence.getAt(3).x, 1 );
+ ensure_equals( sequence.getAt(4).x, 1 );
+ ensure_equals( sequence.getAt(5).x, 2 );
+
+ }
+
} // namespace tut
More information about the geos-commits
mailing list