sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1676452 - in /sis/branches/JDK8/core/sis-referencing/src: main/java/org/apache/sis/parameter/ParameterFormat.java test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java
Date Tue, 28 Apr 2015 09:09:20 GMT
Author: desruisseaux
Date: Tue Apr 28 09:09:19 2015
New Revision: 1676452

URL: http://svn.apache.org/r1676452
Log:
Referencing: fix accuracy problems when formatting map projection scale factor in a ParameterValueGroup.

Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java?rev=1676452&r1=1676451&r2=1676452&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
[UTF-8] Tue Apr 28 09:09:19 2015
@@ -27,6 +27,7 @@ import java.util.Locale;
 import java.util.TimeZone;
 import java.io.IOException;
 import java.text.Format;
+import java.text.NumberFormat;
 import java.text.FieldPosition;
 import java.text.ParsePosition;
 import java.text.ParseException;
@@ -707,6 +708,9 @@ public class ParameterFormat extends Tab
                          */
                         final Format format = getFormat(value.getClass());
                         if (format != null) {
+                            if (format instanceof NumberFormat && value instanceof
Number) {
+                                configure((NumberFormat) format, Math.abs(((Number) value).doubleValue()));
+                            }
                             value = format.format(value, buffer, dummyFP);
                         }
                         table.append(value.toString());
@@ -754,6 +758,42 @@ public class ParameterFormat extends Tab
             }
         }
     }
+
+    /**
+     * Configures the number pattern to use for the given value. The main intend of this
method is to ensure that
+     * the map projection scale factor (a value close to 1) is formatted with a sufficient
number of fraction digits.
+     * A common default NumberFormat precision is 3 digits, which is not sufficient. For
example the scale factor of
+     * Transverse Mercator projections is 0.9996 (4 digits), and the scale factor of "NTF
(Paris) / Lambert zone II"
+     * projection is 0.99987742 (8 digits).
+     *
+     * @param format The format to configure.
+     * @param m The absolute value (magnitude) of the value to write.
+     */
+    private static void configure(final NumberFormat format, final double m) {
+        if (format.getMaximumFractionDigits() <= 9) {
+            /*
+             * If the maximum fraction digits is higher than 9, then that value has not been
set by this class.
+             * Maybe the user overrides the createFormat(Class<?>) method in his own
subclass, in which case we
+             * will respect his wish and not set a lower value here.
+             */
+            final int n;
+            if (m < 10) {
+                n = 9;
+            } else if (m < 1000) {  // No real use case for this threshold yet, but added
for more progressive behavior.
+                n = 6;
+            } else {
+                n = 3;
+            }
+            /*
+             * The minimum fraction digits is usually 0. But if we find a higher value (for
example because the
+             * user overrides the createFormat(Class<?>) method), then we will respect
user's wish and not set
+             * a lower value.
+             */
+            if (n >= format.getMinimumFractionDigits()) {
+                format.setMaximumFractionDigits(n);
+            }
+        }
+    }
 
     /**
      * Implementation of public {@code format(…)} methods for {@code NAME_SUMMARY} content
level.

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java?rev=1676452&r1=1676451&r2=1676452&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java
[UTF-8] Tue Apr 28 09:09:19 2015
@@ -52,7 +52,6 @@ public final strictfp class DefaultProje
      * @see HardCodedCRS#NTF
      *
      * @todo Move this kind of code in a helper class.
-     * @todo Accuracy problem in formatting of scale factor in DefaultParameterValueGroup.toString().
      */
     private static DefaultProjectedCRS create() throws FactoryException {
         final MathTransformFactory mtFactory = DefaultFactories.forBuildin(MathTransformFactory.class);



Mime
View raw message