sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1783933 - in /sis/branches/JDK8/core/sis-referencing/src: main/java/org/apache/sis/internal/referencing/ main/java/org/apache/sis/internal/referencing/provider/ main/java/org/apache/sis/referencing/gazetteer/ test/java/org/apache/sis/refer...
Date Tue, 21 Feb 2017 20:16:42 GMT
Author: desruisseaux
Date: Tue Feb 21 20:16:42 2017
New Revision: 1783933

URL: http://svn.apache.org/viewvc?rev=1783933&view=rev
Log:
Add more verification about validity of parsed MGRS reference.

Added:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/ReferenceVerifyException.java
      - copied, changed from r1783077, sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/GazetteerException.java
Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources.properties
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources_fr.properties
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/TransverseMercator.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystem.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystemTest.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/CoordinateDomain.java

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources.java?rev=1783933&r1=1783932&r2=1783933&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources.java
[UTF-8] Tue Feb 21 20:16:42 2017
@@ -227,6 +227,12 @@ public final class Resources extends Ind
         public static final short IncompatibleDatum_2 = 28;
 
         /**
+         * Square identification in the “{0}” reference is inconsistent with the grid
zone designation.
+         * A consistent zone would be “{1}”.
+         */
+        public static final short InconsistentWithGZD_2 = 77;
+
+        /**
          * Inverse operation uses this parameter value with opposite sign.
          */
         public static final short InverseOperationUsesOppositeSign = 29;

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources.properties?rev=1783933&r1=1783932&r2=1783933&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources.properties
[ISO-8859-1] Tue Feb 21 20:16:42 2017
@@ -64,6 +64,7 @@ IllegalUnitFor_2                  = Unit
 IllegalUTMZone_1                  = \u201c{0}\u201d is not a valid Universal Transverse Mercator
(UTM) zone.
 IncompatibleCoordinateSystemTypes = Incompatible coordinate system types.
 IncompatibleDatum_2               = Datum of \u201c{1}\u201d shall be \u201c{0}\u201d.
+InconsistentWithGZD_2             = Square identification in the \u201c{0}\u201d reference
is inconsistent with the grid zone designation. A consistent zone would be \u201c{1}\u201d.
 LatitudesAreOpposite_2            = Latitudes {0} and {1} are opposite.
 MismatchedParameterDescriptor_1   = Mismatched descriptor for \u201c{0}\u201d parameter.
 MismatchedPrimeMeridian_2         = Expected the \u201c{0}\u201d prime meridian but found
\u201c{1}\u201d.

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources_fr.properties?rev=1783933&r1=1783932&r2=1783933&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Resources_fr.properties
[ISO-8859-1] Tue Feb 21 20:16:42 2017
@@ -69,6 +69,7 @@ IllegalUnitFor_2                  = L\u2
 IllegalUTMZone_1                  = \u00ab\u202f{0}\u202f\u00bb n\u2019est pas une zone valide
de la projection Transverse Mercator Universelle (UTM).
 IncompatibleCoordinateSystemTypes = Types de syst\u00e8mes de coordonn\u00e9es incompatibles.
 IncompatibleDatum_2               = Le r\u00e9f\u00e9rentiel de \u00ab\u202f{1}\u202f\u00bb
doit \u00eatre \u00ab\u202f{0}\u202f\u00bb.
+InconsistentWithGZD_2             = L\u2019identification du carr\u00e9 dans la r\u00e9f\u00e9rence
\u00ab\u202f{0}\u202f\u00bb n\u2019est pas coh\u00e9rente avec la d\u00e9signation de la zone
de la grille. Une zone coh\u00e9rente serait \u00ab\u202f{1}\u202f\u00bb.
 LatitudesAreOpposite_2            = Les latitudes {0} et {1} sont oppos\u00e9es.
 MismatchedParameterDescriptor_1   = Le descripteur du param\u00e8tre \u00ab\u202f{0}\u202f\u00bb
ne correspond pas.
 MismatchedPrimeMeridian_2         = Le m\u00e9ridien d\u2019origine \u00ab\u202f{0}\u202f\u00bb
\u00e9tait attendu, mais \u00ab\u202f{1}\u202f\u00bb a \u00e9t\u00e9 trouv\u00e9.

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/TransverseMercator.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/TransverseMercator.java?rev=1783933&r1=1783932&r2=1783933&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/TransverseMercator.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/TransverseMercator.java
[UTF-8] Tue Feb 21 20:16:42 2017
@@ -349,7 +349,7 @@ public final class TransverseMercator ex
          * @return whether the given latitude is in the Norway latitude band.
          */
         public static boolean isNorway(final double φ) {
-            return (φ >= 56) && (φ < 64);
+            return (φ >= NORWAY_BOUNDS) && (φ < 64);
         }
 
         /**
@@ -360,16 +360,30 @@ public final class TransverseMercator ex
          * @return whether the given latitude is in the Svalbard latitude band.
          */
         public static boolean isSvalbard(final double φ) {
-            return (φ >= 72) && (φ < 84);
+            return (φ >= 72) && (φ < NORTH_BOUNDS);
         }
 
         /**
          * Southernmost bound of the first latitude band ({@code 'C'}), inclusive.
+         *
+         * @see #NORTH_BOUNDS
          */
         public static final double SOUTH_BOUNDS = -80;
 
         /**
+         * Northernmost bounds (exclusive) of the latitudes that do not require special case.
+         * Coordinates at latitudes equal or greater than this value may be subject to special
+         * rules regarding their UTM zones.
+         *
+         * @see #isNorway(double)
+         * @see #isSvalbard(double)
+         */
+        public static final double NORWAY_BOUNDS = 56;
+
+        /**
          * Northernmost bound of the last latitude band ({@code 'X'}), exclusive.
+         *
+         * @see #SOUTH_BOUNDS
          */
         public static final double NORTH_BOUNDS = 84;
     }

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystem.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystem.java?rev=1783933&r1=1783932&r2=1783933&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystem.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystem.java
[UTF-8] Tue Feb 21 20:16:42 2017
@@ -175,6 +175,8 @@ public class MilitaryGridReferenceSystem
     public class Coder {
         /**
          * Number of digits to use for formatting the numerical part of a MGRS reference.
+         *
+         * @see #getPrecision()
          */
         private byte digits;
 
@@ -287,6 +289,28 @@ public class MilitaryGridReferenceSystem
         }
 
         /**
+         * Returns the encoder for the given coordinate reference system.
+         *
+         * @throws IllegalArgumentException if the given CRS do not use one of the supported
datums.
+         * @throws FactoryException if the creation of a coordinate operation failed.
+         * @throws TransformException if the creation of an inverse operation failed.
+         */
+        private Encoder encoder(final CoordinateReferenceSystem crs) throws FactoryException,
TransformException {
+            if (crs == null) {
+                throw new GazetteerException(Errors.format(Errors.Keys.UnspecifiedCRS));
+            }
+            // We can not use encoders.computeIfAbsent(crs, ...) because of checked exceptions.
+            Encoder encoder = encoders.get(crs);
+            if (encoder == null) {
+                encoder = new Encoder(avoidDatumChange ? null : datum, crs);
+                if (encoders.put(crs, encoder) != null) {
+                    throw new ConcurrentModificationException();            // Opportunistic
check.
+                }
+            }
+            return encoder;
+        }
+
+        /**
          * Encodes the given position into a MGRS reference.
          * The given position must have a Coordinate Reference System (CRS) associated to
it.
          *
@@ -296,20 +320,8 @@ public class MilitaryGridReferenceSystem
          */
         public String encode(final DirectPosition position) throws TransformException {
             ArgumentChecks.ensureNonNull("position", position);
-            final CoordinateReferenceSystem crs = position.getCoordinateReferenceSystem();
-            if (crs == null) {
-                throw new GazetteerException(Errors.format(Errors.Keys.UnspecifiedCRS));
-            }
-            Encoder encoder = encoders.get(crs);
             try {
-                // We can not use encoders.computeIfAbsent(crs, ...) because of checked exceptions.
-                if (encoder == null) {
-                    encoder = new Encoder(avoidDatumChange ? null : datum, crs);
-                    if (encoders.put(crs, encoder) != null) {
-                        throw new ConcurrentModificationException();            // Opportunistic
check.
-                    }
-                }
-                return encoder.encode(this, position, separator, digits);
+                return encoder(position.getCoordinateReferenceSystem()).encode(this, position,
separator, digits);
             } catch (IllegalArgumentException | FactoryException e) {
                 throw new GazetteerException(e.getLocalizedMessage(), e);
             }
@@ -390,7 +402,14 @@ public class MilitaryGridReferenceSystem
                         break;
                     }
                     case 3: {
-                        row = c - (((zone & 1) == 0) ? 'F' : 'A');
+                        if ((zone & 1) != 0) {
+                            row = c - 'A';
+                        } else {
+                            row = c - 'F';
+                            if (row < 0) {
+                                row += GRID_ROW_COUNT;
+                            }
+                        }
                         break;
                     }
                 }
@@ -405,14 +424,14 @@ public class MilitaryGridReferenceSystem
              */
             final double λ0 = TransverseMercator.Zoner.UTM.centralMeridian(zone);
             final ProjectedCRS crs = datum.universal(Math.signum(φs), λ0);
-            final DirectPosition2D pos = new DirectPosition2D(φs, λ0);
+            final DirectPosition2D position = new DirectPosition2D(φs, λ0);
             final MathTransform projection = crs.getConversionFromBase().getMathTransform();
-            final double northing = Math.floor(projection.transform(pos, pos).getOrdinate(1)
+            final double northing = Math.floor(projection.transform(position, position).getOrdinate(1)
                                     / (GRID_SQUARE_SIZE * GRID_ROW_COUNT))
                                     * (GRID_SQUARE_SIZE * GRID_ROW_COUNT);
-            pos.setCoordinateReferenceSystem(crs);
-            pos.x = col * GRID_SQUARE_SIZE;
-            pos.y = row * GRID_SQUARE_SIZE + northing;
+            position.setCoordinateReferenceSystem(crs);
+            position.x = col * GRID_SQUARE_SIZE;
+            position.y = row * GRID_SQUARE_SIZE + northing;
             if (i < end) {
                 /*
                  * If we have not yet reached the end of string, parse the numerical location.
@@ -443,8 +462,8 @@ public class MilitaryGridReferenceSystem
                                 reference.subSequence(base, s), CharSequences.trimWhitespaces(reference,
s, end)));
                     }
                 }
-                pos.x += x;
-                pos.y += y;
+                position.x += x;
+                position.y += y;
             }
             /*
              * The southernmost bound of the latitude band (φs) has been computed at the
longitude of the central
@@ -466,18 +485,35 @@ public class MilitaryGridReferenceSystem
              */
             if (hasSquareIdentification) {
                 final MathTransform inverse = projection.inverse();
-                DirectPosition check = inverse.transform(pos, null);
-                double delta = truncateLastLatitudeBand(check.getOrdinate(0)) - φs;
+                DirectPosition check = inverse.transform(position, null);
+                double φ = check.getOrdinate(0);
+                double delta = truncateLastLatitudeBand(φ) - φs;
                 if ((φs >= 0) ? (delta < 0) : (delta > LATITUDE_BAND_HEIGHT)) {
-                    pos.y += Math.signum(φs) * (GRID_SQUARE_SIZE * GRID_ROW_COUNT);
-                    check = inverse.transform(pos, check);
-                    delta = truncateLastLatitudeBand(check.getOrdinate(0)) - φs;
+                    position.y += Math.signum(φs) * (GRID_SQUARE_SIZE * GRID_ROW_COUNT);
+                    check = inverse.transform(position, check);
+                    delta = truncateLastLatitudeBand(φ = check.getOrdinate(0)) - φs;
                 }
-                if (!(delta >= 0 && delta <= LATITUDE_BAND_HEIGHT)) {
-                    throw new GazetteerException("Iconsistent MGRS reference.");    // TODO:
localize
+                /*
+                 * Verification. We allow a tolerance on the UTM zone number for latitudes
close to a pole
+                 * because not all users may apply the UTM special rules for Norway and Svalbard.
Anyway,
+                 * using the neighbor zone at those high latitudes is less significant.
+                 */
+                final int actual = TransverseMercator.Zoner.UTM.zone(φ, check.getOrdinate(1));
+                final boolean isHighLat   = Math.abs(φ) >= TransverseMercator.Zoner.NORWAY_BOUNDS;
+                final boolean isZoneValid = Math.abs(actual - zone) <= (isHighLat ? 1
: 0);
+                final boolean isBandValid = delta >= 0 && delta <= LATITUDE_BAND_HEIGHT;
+                if (!isBandValid || !isZoneValid) {
+                    final String gzd;
+                    try {
+                        gzd = encoder(crs).encode(this, position, "", 0);
+                    } catch (IllegalArgumentException | FactoryException e) {
+                        throw new GazetteerException(e.getLocalizedMessage(), e);
+                    }
+                    final CharSequence ref = reference.subSequence(base, end);
+                    throw new ReferenceVerifyException(Resources.format(Resources.Keys.InconsistentWithGZD_2,
ref, gzd));
                 }
             }
-            return pos;
+            return position;
         }
 
         /**
@@ -508,6 +544,9 @@ public class MilitaryGridReferenceSystem
         }
     }
 
+    //  Following methods would be defined as private methods in Coder class
+    //  if we were allowed to define static methods in non-static inner class.
+
     /**
      * Returns the index after the last digit in a sequence of ASCII characters.
      * Leading whitespaces must have been skipped before to invoke this method.
@@ -663,6 +702,8 @@ public class MilitaryGridReferenceSystem
          *                or {@code null} for inferring the datum from the given {@code
crs}.
          * @param  crs    the coordinate reference system of the coordinates for which to
create MGRS references.
          * @throws IllegalArgumentException if the given CRS do not use one of the supported
datums.
+         * @throws FactoryException if the creation of a coordinate operation failed.
+         * @throws TransformException if the creation of an inverse operation failed.
          */
         Encoder(CommonCRS datum, CoordinateReferenceSystem crs) throws FactoryException,
TransformException {
             CoordinateReferenceSystem horizontal = CRS.getHorizontalComponent(crs);
@@ -748,8 +789,8 @@ public class MilitaryGridReferenceSystem
          * @param  digits     number of digits to use for formatting the numerical part of
a MGRS reference.
          * @return the value of {@code buffer.toString()}.
          */
-        String encode(final MilitaryGridReferenceSystem.Coder owner, DirectPosition position,
-                final String separator, final int digits) throws FactoryException, TransformException
+        String encode(final Coder owner, DirectPosition position, final String separator,
final int digits)
+                throws FactoryException, TransformException
         {
             final StringBuilder buffer = owner.buffer;
             if (toNormalized != null) {

Copied: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/ReferenceVerifyException.java
(from r1783077, sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/GazetteerException.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/ReferenceVerifyException.java?p2=sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/ReferenceVerifyException.java&p1=sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/GazetteerException.java&r1=1783077&r2=1783933&rev=1783933&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/GazetteerException.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/ReferenceVerifyException.java
[UTF-8] Tue Feb 21 20:16:42 2017
@@ -16,27 +16,34 @@
  */
 package org.apache.sis.referencing.gazetteer;
 
-import org.opengis.referencing.operation.TransformException;
-
 
 /**
- * Thrown when a coordinate can not be converted to a geographic identifier, or conversely.
+ * Thrown when a geographic identifier (or reference) has been successfully parsed but fails
some verification.
+ * Examples:
+ *
+ * <ul>
+ *   <li>The <cite>100 000-metres square identification</cite> of a {@linkplain
MilitaryGridReferenceSystem MGRS}
+ *       reference is not consistent with the <cite>Grid Zone Designation</cite>.</li>
+ * </ul>
+ *
+ * The purpose of those verifications is to perform some error detection.
+ * For example it may help to detect if a typo occurred when a user entered a coordinate
manually.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.8
  * @version 0.8
  * @module
  */
-public class GazetteerException extends TransformException {
+public class ReferenceVerifyException extends GazetteerException {
     /**
      * Serial number for inter-operability with different versions.
      */
-    private static final long serialVersionUID = 3607149545794483627L;
+    private static final long serialVersionUID = 7501102047565324537L;
 
     /**
      * Constructs a new exception with no detail message.
      */
-    public GazetteerException() {
+    public ReferenceVerifyException() {
     }
 
     /**
@@ -44,7 +51,7 @@ public class GazetteerException extends
      *
      * @param  message  the details message, or {@code null} if none.
      */
-    public GazetteerException(final String message) {
+    public ReferenceVerifyException(final String message) {
         super(message);
     }
 
@@ -54,9 +61,8 @@ public class GazetteerException extends
      *
      * @param  cause  the cause, or {@code null} if none.
      */
-    public GazetteerException(final Throwable cause) {
-        // Reproduce the behavior of standard Throwable(Throwable) constructor.
-        super((cause != null) ? cause.toString() : null, cause);
+    public ReferenceVerifyException(final Throwable cause) {
+        super(cause);
     }
 
     /**
@@ -65,7 +71,7 @@ public class GazetteerException extends
      * @param  message  the details message, or {@code null} if none.
      * @param  cause    the cause, or {@code null} if none.
      */
-    public GazetteerException(final String message, final Throwable cause) {
+    public ReferenceVerifyException(final String message, final Throwable cause) {
         super(message, cause);
     }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystemTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystemTest.java?rev=1783933&r1=1783932&r2=1783933&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystemTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystemTest.java
[UTF-8] Tue Feb 21 20:16:42 2017
@@ -16,7 +16,9 @@
  */
 package org.apache.sis.referencing.gazetteer;
 
+import java.util.Random;
 import org.opengis.referencing.operation.TransformException;
+import org.opengis.referencing.crs.ProjectedCRS;
 import org.opengis.geometry.DirectPosition;
 import org.apache.sis.geometry.DirectPosition2D;
 import org.apache.sis.internal.referencing.provider.TransverseMercator;
@@ -24,6 +26,7 @@ import org.apache.sis.referencing.Common
 import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
+import org.apache.sis.test.TestUtilities;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -261,4 +264,70 @@ public final strictfp class MilitaryGrid
         assertEquals("Easting",   100000, position.getOrdinate(0), STRICT);
         assertEquals("Northing", 4000000, position.getOrdinate(1), STRICT);
     }
+
+    /**
+     * Verifies the exceptions thrown when an invalid reference is given.
+     *
+     * @throws TransformException if an error occurred while computing the coordinate.
+     */
+    @Test
+    @DependsOnMethod("testDecoding")
+    public void testErrorDetection() throws TransformException {
+        final MilitaryGridReferenceSystem.Coder coder = coder();
+        try {
+            coder.decode("32TNL841023923");
+            fail("Shall not accept numeric identifier of odd length.");
+        } catch (GazetteerException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("8410"));
+            assertTrue(message, message.contains("23923"));
+        }
+        try {
+            coder.decode("32TN");
+            fail("Shall not accept half of a grid zone designator.");
+        } catch (GazetteerException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("32TN"));
+        }
+        try {
+            coder.decode("32SNL8410239239");
+            fail("Shall report an invalid latitude band.");
+        } catch (ReferenceVerifyException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("32SNL8410239239"));
+            assertTrue(message, message.contains("32TNL"));
+        }
+    }
+
+    /**
+     * Encode random coordinates, decode them and verifies that the results are approximatively
equals
+     * to the original coordinates.
+     *
+     * @throws TransformException if an error occurred while computing the coordinate.
+     */
+//  @Test
+    @DependsOnMethod({"testEncoding", "testDecoding"})
+    public void verifyConsistency() throws TransformException {
+        final Random random = TestUtilities.createRandomNumberGenerator();
+        final MilitaryGridReferenceSystem.Coder coder = coder();
+        final DirectPosition2D expected = new DirectPosition2D();
+        final DirectPosition2D position = new DirectPosition2D(CommonCRS.WGS84.geographic());
+        for (int i=0; i<100; i++) {
+            position.x = random.nextDouble() * 160 -  80;       // Latitude  (despite the
'x' field name)
+            position.y = random.nextDouble() * 358 - 179;       // Longitude (despite the
'y' field name)
+            final String reference = coder.encode(position);
+            final DirectPosition r = coder.decode(reference);
+            final ProjectedCRS crs = (ProjectedCRS) r.getCoordinateReferenceSystem();
+            assertSame(expected, crs.getConversionFromBase().getMathTransform().transform(position,
expected));
+            final double distance = expected.distance(r.getOrdinate(0), r.getOrdinate(1));
+            if (!(distance < 1.5)) {    // Use '!' for catching NaN.
+                final String lineSeparator = System.lineSeparator();
+                fail("Consistency check failed for φ = " + position.x + " and λ = " + position.y
+ lineSeparator
+                   + "MGRS reference = " + reference + lineSeparator
+                   + "Parsing result = " + r         + lineSeparator
+                   + "Projected φ, λ = " + expected  + lineSeparator
+                   + "Distance (m)   = " + distance  + lineSeparator);
+            }
+        }
+    }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/CoordinateDomain.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/CoordinateDomain.java?rev=1783933&r1=1783932&r2=1783933&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/CoordinateDomain.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/CoordinateDomain.java
[UTF-8] Tue Feb 21 20:16:42 2017
@@ -37,6 +37,12 @@ import org.apache.sis.referencing.datum.
  * @module
  */
 public strictfp class CoordinateDomain {
+    /*
+     * Note: this class is not declared as an enum yet because moving ARTICLE_CIRCLE and
HEIGHT constants
+     *       after the enum declarations causes an "illegal forward reference" compiler error
with JDK 8.
+     */
+
+
     /**
      * Latitude of the Article circle, which is 66°33′45.7″ as of March 30, 2015
      * Note that this value fluctuate by 2° over 40,000-year periods.



Mime
View raw message