sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1423316 - in /sis/branches/JDK7: sis-referencing/src/main/java/org/apache/sis/geometry/ sis-utility/src/main/java/org/apache/sis/math/ sis-utility/src/test/java/org/apache/sis/math/
Date Tue, 18 Dec 2012 08:22:44 GMT
Author: desruisseaux
Date: Tue Dec 18 08:22:43 2012
New Revision: 1423316

URL: http://svn.apache.org/viewvc?rev=1423316&view=rev
Log:
Formats the points and envelopes as 'float' numbers when the ordinate seems to only have simple
precision.
This avoid the printing decimal digits having no real signification (e.g. "0.3333333432674408"
instead of "0.33333334").
Note that this approach is not strictly correct since parsing such values in double precision
will not produce exactly
the same result. We should put a warning in the 'toString()' method with a link to a method
providing control on this
behavior (to be comitted later in the Envelopes utility class).

Modified:
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractDirectPosition.java
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/Envelope2D.java
    sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractDirectPosition.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractDirectPosition.java?rev=1423316&r1=1423315&r2=1423316&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractDirectPosition.java
(original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractDirectPosition.java
Tue Dec 18 08:22:43 2012
@@ -176,15 +176,39 @@ public abstract class AbstractDirectPosi
      */
     @Override
     public String toString() {
-        final StringBuilder buffer = new StringBuilder(32).append("POINT(");
-        final int dimension = getDimension();
-        for (int i=0; i<dimension; i++) {
-            if (i != 0) {
-                buffer.append(' ');
+        return toString(this, false);
+    }
+
+    /**
+     * Implementation of the public {@link #toString()} and {@link Envelope2D#toString()}
methods
+     * for formatting a {@code POINT} element from a direct position in <cite>Well
Known Text</cite>
+     * (WKT) format.
+     *
+     * @param  position The position to format.
+     * @param  isSimplePrecision {@code true} if every ordinate values can be casted to {@code
float}.
+     * @return The point as a {@code POINT} in WKT format.
+     */
+    static String toString(final DirectPosition position, final boolean isSimplePrecision)
{
+        final StringBuilder buffer = new StringBuilder(32).append("POINT");
+        final int dimension = position.getDimension();
+        if (dimension == 0) {
+            buffer.append("()");
+        } else {
+            char separator = '(';
+            for (int i=0; i<dimension; i++) {
+                buffer.append(separator);
+                final double ordinate = position.getOrdinate(i);
+                if (isSimplePrecision) {
+                    buffer.append((float) ordinate);
+                } else {
+                    buffer.append(ordinate);
+                }
+                trimFractionalPart(buffer);
+                separator = ' ';
             }
-            trimFractionalPart(buffer.append(getOrdinate(i)));
+            buffer.append(')');
         }
-        return buffer.append(')').toString();
+        return buffer.toString();
     }
 
     /**

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java?rev=1423316&r1=1423315&r2=1423316&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
(original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/AbstractEnvelope.java
Tue Dec 18 08:22:43 2012
@@ -914,7 +914,7 @@ public abstract class AbstractEnvelope i
      */
     @Override
     public String toString() {
-        return toString(this);
+        return toString(this, false);
     }
 
     /**
@@ -922,28 +922,37 @@ public abstract class AbstractEnvelope i
      * for formatting a {@code BOX} element from an envelope in <cite>Well Known Text</cite>
(WKT) format.
      *
      * @param  envelope The envelope to format.
+     * @param  isSimplePrecision {@code true} if every lower and upper corner values can
be casted to {@code float}.
      * @return The envelope as a {@code BOX2D} or {@code BOX3D} (most typical dimensions)
in WKT format.
      *
      * @see GeneralEnvelope#GeneralEnvelope(String)
      * @see org.apache.sis.measure.CoordinateFormat
      * @see org.apache.sis.io.wkt
      */
-    static String toString(final Envelope envelope) {
-        final int            dimension   = envelope.getDimension();
-        final DirectPosition lowerCorner = envelope.getLowerCorner();
-        final DirectPosition upperCorner = envelope.getUpperCorner();
-        final StringBuilder  buffer = new StringBuilder(64).append("BOX").append(dimension).append("D(");
-        for (int i=0; i<dimension; i++) {
-            if (i != 0) {
-                buffer.append(' ');
-            }
-            trimFractionalPart(buffer.append(lowerCorner.getOrdinate(i)));
-        }
-        buffer.append(',');
-        for (int i=0; i<dimension; i++) {
-            trimFractionalPart(buffer.append(' ').append(upperCorner.getOrdinate(i)));
+    static String toString(final Envelope envelope, final boolean isSimplePrecision) {
+        final int dimension = envelope.getDimension();
+        final StringBuilder buffer = new StringBuilder(64).append("BOX").append(dimension).append('D');
+        if (dimension == 0) {
+            buffer.append("()");
+        } else {
+            final DirectPosition lowerCorner = envelope.getLowerCorner();
+            final DirectPosition upperCorner = envelope.getUpperCorner();
+            boolean isUpper = false;
+            do { // Executed exactly twice.
+                for (int i=0; i<dimension; i++) {
+                    buffer.append(i == 0 && !isUpper ? '(' : ' ');
+                    final double ordinate = (isUpper ? upperCorner : lowerCorner).getOrdinate(i);
+                    if (isSimplePrecision) {
+                        buffer.append((float) ordinate);
+                    } else {
+                        buffer.append(ordinate);
+                    }
+                    trimFractionalPart(buffer);
+                }
+                buffer.append(isUpper ? ')' : ',');
+            } while ((isUpper = !isUpper) == true);
         }
-        return buffer.append(')').toString();
+        return buffer.toString();
     }
 
     /**

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java?rev=1423316&r1=1423315&r2=1423316&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
(original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/ArrayEnvelope.java
Tue Dec 18 08:22:43 2012
@@ -38,6 +38,7 @@ import org.apache.sis.referencing.CRS;
 import static org.apache.sis.util.Arrays.resize;
 import static org.apache.sis.util.ArgumentChecks.*;
 import static org.apache.sis.math.MathFunctions.isNegative;
+import static org.apache.sis.math.MathFunctions.isSimplePrecision;
 import static org.apache.sis.internal.referencing.Utilities.isPoleToPole;
 
 // Related to JDK7
@@ -474,4 +475,12 @@ scanNumber: while ((i += Character.charC
         }
         return false;
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return toString(this, isSimplePrecision(ordinates));
+    }
 }

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java?rev=1423316&r1=1423315&r2=1423316&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java
(original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition1D.java
Tue Dec 18 08:22:43 2012
@@ -27,6 +27,8 @@ import org.opengis.geometry.DirectPositi
 import org.opengis.geometry.MismatchedDimensionException;
 import org.apache.sis.util.resources.Errors;
 
+import static org.apache.sis.math.MathFunctions.isSimplePrecision;
+
 
 /**
  * Holds the coordinates for a one-dimensional position within some coordinate reference
system.
@@ -205,6 +207,22 @@ public class DirectPosition1D extends Ab
     }
 
     /**
+     * Formats this position in the <cite>Well Known Text</cite> (WKT) format.
+     * The output is like below:
+     *
+     * {@preformat wkt
+     *   POINT(ordinate)
+     * }
+     *
+     * The string returned by this method can be {@linkplain #DirectPosition1D(CharSequence)
parsed}
+     * by the {@code DirectPosition1D} constructor.
+     */
+    @Override
+    public String toString() {
+        return toString(this, isSimplePrecision(ordinate));
+    }
+
+    /**
      * Returns a copy of this position.
      */
     @Override

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java?rev=1423316&r1=1423315&r2=1423316&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java
(original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/DirectPosition2D.java
Tue Dec 18 08:22:43 2012
@@ -25,7 +25,7 @@ import org.apache.sis.util.resources.Err
 
 import static java.lang.Double.doubleToLongBits;
 import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
-import static org.apache.sis.util.StringBuilders.trimFractionalPart;
+import static org.apache.sis.math.MathFunctions.isSimplePrecision;
 
 // Following imports are needed because we can't extend AbstractDirectPosition.
 // We want to write this class as if it was an AbstractDirectPosition subclass.
@@ -296,10 +296,7 @@ public class DirectPosition2D extends Po
      */
     @Override
     public String toString() {
-        final StringBuilder buffer = new StringBuilder(32);
-        trimFractionalPart(buffer.append("POINT(").append(x));
-        trimFractionalPart(buffer.append(' ').append(y));
-        return buffer.append(')').toString();
+        return AbstractDirectPosition.toString(this, isSimplePrecision(x, y));
     }
 
     /**

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/Envelope2D.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/Envelope2D.java?rev=1423316&r1=1423315&r2=1423316&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/Envelope2D.java
(original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/Envelope2D.java
Tue Dec 18 08:22:43 2012
@@ -982,6 +982,6 @@ public class Envelope2D extends Rectangl
      */
     @Override
     public String toString() {
-        return AbstractEnvelope.toString(this);
+        return AbstractEnvelope.toString(this, false);
     }
 }

Modified: sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java?rev=1423316&r1=1423315&r2=1423316&view=diff
==============================================================================
--- sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java
(original)
+++ sis/branches/JDK7/sis-referencing/src/main/java/org/apache/sis/geometry/GeneralDirectPosition.java
Tue Dec 18 08:22:43 2012
@@ -28,6 +28,7 @@ import org.opengis.geometry.DirectPositi
 import org.opengis.geometry.MismatchedDimensionException;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.apache.sis.util.resources.Errors;
+import org.apache.sis.math.MathFunctions;
 
 // JDK7 related
 import java.util.Objects;
@@ -263,6 +264,14 @@ public class GeneralDirectPosition exten
     }
 
     /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return toString(this, MathFunctions.isSimplePrecision(ordinates));
+    }
+
+    /**
      * Returns a deep copy of this position.
      */
     @Override

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java?rev=1423316&r1=1423315&r2=1423316&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/math/MathFunctions.java Tue
Dec 18 08:22:43 2012
@@ -197,6 +197,23 @@ public final class MathFunctions extends
     }
 
     /**
+     * Returns {@code true} if every values in the given {@code double} array could be casted
+     * to the {@code float} type without precision lost. This method treats all {@code NaN}
values
+     * as equal.
+     *
+     * @param  values The value to test for their precision.
+     * @return {@code true} if every values can be casted to the {@code float} type without
precision lost.
+     */
+    public static boolean isSimplePrecision(final double... values) {
+        for (final double value : values) {
+            if (Double.doubleToLongBits(value) != Double.doubleToLongBits((float) value))
{
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
      * Returns the number of fraction digits needed for formatting in base 10 numbers of
the given
      * accuracy. If the {@code strict} argument is {@code true}, then for any given {@code
accuracy}
      * this method returns a value <var>n</var> such as the difference between
adjacent numbers

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java?rev=1423316&r1=1423315&r2=1423316&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
(original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/math/MathFunctionsTest.java
Tue Dec 18 08:22:43 2012
@@ -84,6 +84,15 @@ public final strictfp class MathFunction
     }
 
     /**
+     * Tests {@link MathFunctions#isSimplePrecision(double[])}.
+     */
+    @Test
+    public void testIsSimplePrecision() {
+        assertTrue (isSimplePrecision(2, 0.5, 0.25, Double.NaN, Double.POSITIVE_INFINITY));
+        assertFalse(isSimplePrecision(2, 0.5, 1.0 / 3));
+    }
+
+    /**
      * Tests {@link MathFunctions#fractionDigitsForDelta(double, boolean)}.
      */
     @Test



Mime
View raw message