[geos-commits] r3292 - in trunk: include/geos/geom src/geom tests/unit tests/unit/geom tests/unit/io

svn_geos at osgeo.org svn_geos at osgeo.org
Fri Apr 22 09:04:20 EDT 2011


Author: strk
Date: 2011-04-22 06:04:20 -0700 (Fri, 22 Apr 2011)
New Revision: 3292

Added:
   trunk/tests/unit/geom/PrecisionModelTest.cpp
Modified:
   trunk/include/geos/geom/PrecisionModel.h
   trunk/include/geos/geom/PrecisionModel.inl
   trunk/src/geom/PrecisionModel.cpp
   trunk/tests/unit/Makefile.am
   trunk/tests/unit/io/WKTReaderTest.cpp
   trunk/tests/unit/io/WKTWriterTest.cpp
Log:
Sync PrecisionModel to JTS-1.12, add unit testing, fix getMaximumPrecisionDigit to behave as documented (JTS doesn't).

Modified: trunk/include/geos/geom/PrecisionModel.h
===================================================================
--- trunk/include/geos/geom/PrecisionModel.h	2011-04-20 17:13:13 UTC (rev 3291)
+++ trunk/include/geos/geom/PrecisionModel.h	2011-04-22 13:04:20 UTC (rev 3292)
@@ -4,6 +4,7 @@
  * GEOS - Geometry Engine Open Source
  * http://geos.refractions.net
  *
+ * Copyright (C) 2011 Sandro Santilli <strk at keybit.net>
  * Copyright (C) 2006 Refractions Research Inc.
  *
  * This is free software; you can redistribute and/or modify it under
@@ -13,7 +14,7 @@
  *
  **********************************************************************
  *
- * Last port: geom/PrecisionModel.java rev. 1.51 (JTS-1.7)
+ * Last port: geom/PrecisionModel.java r378 (JTS-1.12)
  *
  **********************************************************************/
 
@@ -73,6 +74,10 @@
  *   - jtsPt.x = round( inputPt.x * scale ) / scale
  *   - jtsPt.y = round( inputPt.y * scale ) / scale
  *
+ * For example, to specify 3 decimal places of precision, use a scale factor
+ * of 1000. To specify -3 decimal places of precision (i.e. rounding to
+ * the nearest 1000), use a scale factor of 0.001.
+ *
  * Coordinates are represented internally as Java double-precision values.
  * Since Java uses the IEEE-394 floating point standard, this
  * provides 53 bits of precision. (Thus the maximum precisely representable

Modified: trunk/include/geos/geom/PrecisionModel.inl
===================================================================
--- trunk/include/geos/geom/PrecisionModel.inl	2011-04-20 17:13:13 UTC (rev 3291)
+++ trunk/include/geos/geom/PrecisionModel.inl	2011-04-22 13:04:20 UTC (rev 3292)
@@ -4,6 +4,7 @@
  * GEOS - Geometry Engine Open Source
  * http://geos.refractions.net
  *
+ * Copyright (C) 2011 Sandro Santilli <strk at keybit.net>
  * Copyright (C) 2005-2006 Refractions Research Inc.
  *
  * This is free software; you can redistribute and/or modify it under
@@ -13,7 +14,7 @@
  *
  **********************************************************************
  *
- * Last port: geom/PrecisionModel.java rev. 1.51 (JTS-1.7)
+ * Last port: geom/PrecisionModel.java r378 (JTS-1.12)
  *
  **********************************************************************/
 

Modified: trunk/src/geom/PrecisionModel.cpp
===================================================================
--- trunk/src/geom/PrecisionModel.cpp	2011-04-20 17:13:13 UTC (rev 3291)
+++ trunk/src/geom/PrecisionModel.cpp	2011-04-22 13:04:20 UTC (rev 3292)
@@ -4,6 +4,7 @@
  * GEOS - Geometry Engine Open Source
  * http://geos.refractions.net
  *
+ * Copyright (C) 2011 Sandro Santilli <strk at keybit.net>
  * Copyright (C) 2001-2002 Vivid Solutions Inc.
  *
  * This is free software; you can redistribute and/or modify it under
@@ -13,7 +14,7 @@
  *
  **********************************************************************
  *
- * Last port: geom/PrecisionModel.java rev. 1.51 (JTS-1.7)
+ * Last port: geom/PrecisionModel.java r378 (JTS-1.12)
  *
  **********************************************************************/
 
@@ -80,7 +81,7 @@
 PrecisionModel::PrecisionModel()
 	:
 	modelType(FLOATING),
-	scale(1.0)
+	scale(0.0)
 {
 #if GEOS_DEBUG
 	cerr<<"PrecisionModel["<<this<<"] ctor()"<<endl;
@@ -161,8 +162,13 @@
 	} else if (modelType == FLOATING_SINGLE) {
 		maxSigDigits = 6;
 	} else if (modelType == FIXED) {
-        const int dgts = static_cast<int>(std::ceil(std::log(getScale()) / std::log(double(10.0))));
-        maxSigDigits = 1 + dgts;
+
+		double dgtsd = std::log(getScale()) / std::log(double(10.0));
+		const int dgts = static_cast<int>(
+			dgtsd > 0 ? std::ceil(dgtsd)
+			          : std::floor(dgtsd)
+		);
+		maxSigDigits = dgts;
 	}
 	return maxSigDigits;
 }

Modified: trunk/tests/unit/Makefile.am
===================================================================
--- trunk/tests/unit/Makefile.am	2011-04-20 17:13:13 UTC (rev 3291)
+++ trunk/tests/unit/Makefile.am	2011-04-22 13:04:20 UTC (rev 3292)
@@ -57,6 +57,7 @@
 	geom/MultiPolygonTest.cpp \
 	geom/PointTest.cpp \
 	geom/PolygonTest.cpp \
+  geom/PrecisionModelTest.cpp \
 	geom/prep/PreparedGeometryFactoryTest.cpp \
 	geom/TriangleTest.cpp \
 	geom/util/GeometryExtracterTest.cpp \

Added: trunk/tests/unit/geom/PrecisionModelTest.cpp
===================================================================
--- trunk/tests/unit/geom/PrecisionModelTest.cpp	                        (rev 0)
+++ trunk/tests/unit/geom/PrecisionModelTest.cpp	2011-04-22 13:04:20 UTC (rev 3292)
@@ -0,0 +1,143 @@
+// $Id$
+// 
+// Test Suite for geos::geom::PrecisionModel class.
+
+// tut
+#include <tut.hpp>
+// geos
+#include <geos/geom/PrecisionModel.h>
+#include <geos/geom/Coordinate.h>
+
+namespace tut
+{
+    //
+    // Test Group
+    //
+
+    // Common data used by tests
+    struct test_precisionmodel_data
+    {
+        typedef geos::geom::PrecisionModel PrecisionModel;
+        typedef geos::geom::Coordinate Coordinate;
+        test_precisionmodel_data() {}
+
+        void preciseCoordinateTester(const PrecisionModel& pm,
+                      double x1, double y1,
+                      double x2, double y2)
+        {
+              Coordinate p(x1, y1);
+      
+              pm.makePrecise(p);
+      
+              Coordinate pPrecise(x2, y2);
+              ensure(p.equals2D(pPrecise));
+        }
+
+
+    };
+
+    typedef test_group<test_precisionmodel_data> group;
+    typedef group::object object;
+
+    group test_precisionmodel_group("geos::geom::PrecisionModel");
+
+    //
+    // Test Cases
+    //
+
+    // Test of default constructor
+    template<>
+    template<>
+    void object::test<1>()
+    {
+        PrecisionModel pm;
+        ensure(pm.isFloating());
+        ensure_equals(pm.getMaximumSignificantDigits(), 16);
+        ensure_equals(pm.getScale(), 0);
+    }
+
+    // Test FLOAT_SINGLE model
+    template<>
+    template<>
+    void object::test<2>()
+    {
+        PrecisionModel pm(PrecisionModel::FLOATING_SINGLE);
+        ensure(pm.isFloating());
+        ensure_equals(pm.getType(), PrecisionModel::FLOATING_SINGLE);
+        ensure_equals(pm.getMaximumSignificantDigits(), 6);
+    }
+
+    // Test default FIXED model
+    template<>
+    template<>
+    void object::test<3>()
+    {
+        PrecisionModel pm(PrecisionModel::FIXED);
+        ensure(!pm.isFloating());
+        ensure_equals(pm.getType(), PrecisionModel::FIXED);
+        ensure_equals(pm.getMaximumSignificantDigits(), 0);
+    }
+
+    // Test maximum significant digits (1:0)
+    template<>
+    template<>
+    void object::test<4>()
+    {
+        PrecisionModel pm(1);
+        ensure(!pm.isFloating());
+        ensure_equals(pm.getType(), PrecisionModel::FIXED);
+        ensure_equals(pm.getMaximumSignificantDigits(), 0);
+    }
+
+    // Test maximum significant digits (10:1)
+    template<>
+    template<>
+    void object::test<5>()
+    {
+        PrecisionModel pm(10);
+        ensure_equals(pm.getType(), PrecisionModel::FIXED);
+        ensure_equals(pm.getMaximumSignificantDigits(), 1);
+    }
+
+    // Test maximum significant digits (1000:3)
+    template<>
+    template<>
+    void object::test<6>()
+    {
+        PrecisionModel pm(1000);
+        ensure_equals(pm.getType(), PrecisionModel::FIXED);
+        ensure_equals(pm.getMaximumSignificantDigits(), 3);
+    }
+
+    // Test maximum significant digits (0.1:-1)
+    template<>
+    template<>
+    void object::test<7>()
+    {
+        PrecisionModel pm(0.1);
+        ensure_equals(pm.getType(), PrecisionModel::FIXED);
+        ensure_equals(pm.getMaximumSignificantDigits(), -1);
+    }
+
+    // Test maximum significant digits (0.001:-3)
+    template<>
+    template<>
+    void object::test<8>()
+    {
+        PrecisionModel pm(0.001);
+        ensure_equals(pm.getType(), PrecisionModel::FIXED);
+        ensure_equals(pm.getMaximumSignificantDigits(), -3);
+    }
+
+    // Test makePrecise
+    template<>
+    template<>
+    void object::test<9>()
+    {
+        PrecisionModel pm_10(0.1);
+        preciseCoordinateTester(pm_10, 1200.4, 1240.4, 1200, 1240);
+        preciseCoordinateTester(pm_10, 1209.4, 1240.4, 1210, 1240);
+    }
+
+} // namespace tut
+

Modified: trunk/tests/unit/io/WKTReaderTest.cpp
===================================================================
--- trunk/tests/unit/io/WKTReaderTest.cpp	2011-04-20 17:13:13 UTC (rev 3291)
+++ trunk/tests/unit/io/WKTReaderTest.cpp	2011-04-22 13:04:20 UTC (rev 3292)
@@ -105,7 +105,7 @@
             ensure( coords->getDimension() == 3 );
 
             ensure_equals( wktwriter.write(geom.get()), 
-                           std::string("LINESTRING Z (-117.0 33.0 2.0, -116.0 34.0 4.0)") );
+                           std::string("LINESTRING Z (-117 33 2, -116 34 4)") );
 
             delete coords;
     }
@@ -117,7 +117,7 @@
 	{         
             GeomPtr geom(wktreader.read("LineString (-117 33 2, -116 34 4)"));
             ensure_equals( wktwriter.write(geom.get()), 
-                           std::string("LINESTRING Z (-117.0 33.0 2.0, -116.0 34.0 4.0)") );
+                           std::string("LINESTRING Z (-117 33 2, -116 34 4)") );
     }
 
     // 6 - invalid WKT (see http://trac.osgeo.org/geos/ticket/361)

Modified: trunk/tests/unit/io/WKTWriterTest.cpp
===================================================================
--- trunk/tests/unit/io/WKTWriterTest.cpp	2011-04-20 17:13:13 UTC (rev 3291)
+++ trunk/tests/unit/io/WKTWriterTest.cpp	2011-04-22 13:04:20 UTC (rev 3292)
@@ -1,6 +1,6 @@
 // $Id: WKBReaderTest.cpp 2344 2009-04-09 21:46:30Z mloskot $
 // 
-// Test Suite for geos::io::WKTReader 
+// Test Suite for geos::io::WKTWriter 
 
 // tut
 #include <tut.hpp>
@@ -59,12 +59,12 @@
         wktwriter.setTrim(false);
         result = wktwriter.write( geom );
 
-        ensure( result == "POINT (-117.0000 33.0000)" );
+        ensure_equals( result , "POINT (-117.000 33.000)" );
 
         wktwriter.setTrim(true);
         result = wktwriter.write( geom );
 
-        ensure( result == "POINT (-117 33)" );
+        ensure_equals( result , "POINT (-117 33)" );
 
         delete geom;
     }
@@ -80,12 +80,12 @@
         wktwriter.setTrim(false);
         result = wktwriter.write( geom );
 
-        ensure( result == "POINT (-117.1230 33.1230)" );
+        ensure_equals( result , "POINT (-117.123 33.123)" );
 
         wktwriter.setRoundingPrecision(2);
         result = wktwriter.write( geom );
 
-        ensure( result == "POINT (-117.12 33.12)" );
+        ensure_equals( result , "POINT (-117.12 33.12)" );
 
         delete geom;
     }
@@ -132,6 +132,29 @@
 
         delete geom;
     }
+
+  // 5 - Test fixed precision model geometries
+  template<>
+  template<>
+  void object::test<5>()
+  {         
+
+/*
+ * For example, to specify 3 decimal places of precision, use a scale factor
+ * of 1000. To specify -3 decimal places of precision (i.e. rounding to
+ * the nearest 1000), use a scale factor of 0.001.
+ */
+
+    geos::geom::PrecisionModel pm3(1000, 0, 0);
+    geos::geom::GeometryFactory gf3(&pm3);
+    geos::io::WKTReader wktreader3(&gf3);
+    geos::geom::Geometry *geom = wktreader3.read("POINT(0.123456 1.98765)");
+
+    std::string  result = wktwriter.write( geom );
+    ensure_equals( result, std::string("POINT (0.123 1.988)") );
+
+    delete geom;
+  }
     
 } // namespace tut
 



More information about the geos-commits mailing list