sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1783090 - in /sis/branches/JDK8/core/sis-referencing/src: main/java/org/apache/sis/referencing/gazetteer/ test/java/org/apache/sis/referencing/gazetteer/
Date Wed, 15 Feb 2017 10:11:43 GMT
Author: desruisseaux
Date: Wed Feb 15 10:11:42 2017
New Revision: 1783090

URL: http://svn.apache.org/viewvc?rev=1783090&view=rev
Log:
Add a 'setSeparator(String)' method.

Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/MGRSEncoder.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

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/MGRSEncoder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/MGRSEncoder.java?rev=1783090&r1=1783089&r2=1783090&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/MGRSEncoder.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/gazetteer/MGRSEncoder.java
[UTF-8] Wed Feb 15 10:11:42 2017
@@ -297,13 +297,14 @@ final class MGRSEncoder {
      * Encodes the given position into a MGRS label. It is caller responsibility to ensure
that the
      * position CRS is the same than the CRS specified at this {@code MGRSEncoder} creation
time.
      *
-     * @param  owner     the {@code Coder} which own this {@code MGRSEncoder}.
-     * @param  position  the direct position to format as a MGRS label.
-     * @param  digits    number of digits to use for formatting the numerical part of a MGRS
label.
+     * @param  owner      the {@code Coder} which own this {@code MGRSEncoder}.
+     * @param  position   the direct position to format as a MGRS label.
+     * @param  separator  the separator to insert between each component of the MGRS identifier.
+     * @param  digits     number of digits to use for formatting the numerical part of a
MGRS label.
      * @return the value of {@code buffer.toString()}.
      */
-    String encode(final MilitaryGridReferenceSystem.Coder owner, DirectPosition position,
final int digits)
-            throws FactoryException, TransformException
+    String encode(final MilitaryGridReferenceSystem.Coder owner, DirectPosition position,
+            final String separator, final int digits) throws FactoryException, TransformException
     {
         final StringBuilder buffer = owner.buffer;
         if (toNormalized != null) {
@@ -329,7 +330,7 @@ final class MGRSEncoder {
                 owner.normalized = position = toActualZone.transform(geographic, owner.normalized);
             }
             buffer.setLength(0);
-            buffer.append(zone).append(band);
+            buffer.append(zone).append(separator).append(band);
             if (digits >= 0) {
                 /*
                  * Specification said that 100,000-meters columns are lettered from A through
Z (omitting I and O)
@@ -366,15 +367,15 @@ final class MGRSEncoder {
                 }
                 row = 'A' + (row % 20);
                 if (row >= EXCLUDE_I && ++row >= EXCLUDE_O) row++;
-                buffer.append((char) col).append((char) row);
+                buffer.append(separator).append((char) col).append((char) row);
                 /*
                  * Numerical location at the given precision.
                  * The specification requires us to truncate the number, not to round it.
                  */
                 if (digits > 0) {
                     final double precision = MathFunctions.pow10(METRE_PRECISION_DIGITS -
digits);
-                    append(buffer, (int) ((x - cx * GRID_SQUARE_SIZE) / precision), digits);
-                    append(buffer, (int) ((y - cy * GRID_SQUARE_SIZE) / precision), digits);
+                    append(buffer.append(separator), (int) ((x - cx * GRID_SQUARE_SIZE) /
precision), digits);
+                    append(buffer.append(separator), (int) ((y - cy * GRID_SQUARE_SIZE) /
precision), digits);
                 }
             }
         } else {

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=1783090&r1=1783089&r2=1783090&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] Wed Feb 15 10:11:42 2017
@@ -106,6 +106,11 @@ public class MilitaryGridReferenceSystem
         private byte digits;
 
         /**
+         * The separator to insert between each component of the MGRS identifier, or an empty
string if none.
+         */
+        private String separator;
+
+        /**
          * Cached information needed for building a MGRS label from a direct position in
the given CRS.
          */
         private final Map<CoordinateReferenceSystem,MGRSEncoder> encoders;
@@ -125,9 +130,10 @@ public class MilitaryGridReferenceSystem
          * Creates a new coder initialized to the default precision.
          */
         protected Coder() {
-            digits   = 5;                          // 1 meter precision.
-            buffer   = new StringBuilder(12);      // Length of "4QFJ12345678" sample value.
-            encoders = new IdentityHashMap<>();
+            digits    = 5;                          // 1 meter precision.
+            separator = "";
+            buffer    = new StringBuilder(16);      // Length of "4 Q FJ 1234 5678"
sample value.
+            encoders  = new IdentityHashMap<>();
         }
 
         /**
@@ -174,6 +180,30 @@ public class MilitaryGridReferenceSystem
         }
 
         /**
+         * Returns the separator to insert between each component of the MGRS identifier.
+         * Components are zone number, latitude band, 100,000-metres square identifier and
numerical values.
+         * By default the separator is an empty string, which produce labels like "4QFJ12345678".
+         *
+         * @return the separator to insert between each component of the MGRS identifier,
or an empty string if none.
+         */
+        public String getSeparator() {
+            return separator;
+        }
+
+        /**
+         * Sets the separator to insert between each component of the MGRS identifier.
+         * Components are zone number, latitude band, 100,000-metres square identifier and
numerical values.
+         * By default the separator is an empty string, which produce labels like "4QFJ12345678".
+         * If the separator is set to a space, then the labels will be formatted like "4 Q FJ 1234 5678".
+         *
+         * @param  separator  the separator to insert between each component of the MGRS
identifier.
+         */
+        public void setSeparator(final String separator) {
+            ArgumentChecks.ensureNonNull("separator", separator);
+            this.separator = separator;
+        }
+
+        /**
          * Encodes the given position into a MGRS label.
          * The given position must have a CRS associated to it.
          *
@@ -192,7 +222,7 @@ public class MilitaryGridReferenceSystem
                         throw new ConcurrentModificationException();            // Opportunistic
check.
                     }
                 }
-                return encoder.encode(this, position, digits);
+                return encoder.encode(this, position, separator, digits);
             } catch (IllegalArgumentException  | FactoryException e) {
                 throw new GazetteerException(e.getLocalizedMessage(), e);
             }

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=1783090&r1=1783089&r2=1783090&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] Wed Feb 15 10:11:42 2017
@@ -126,4 +126,29 @@ public final strictfp class MilitaryGrid
         assertEquals("precision", 1000000, coder.getPrecision(), STRICT);
         assertEquals("48P", coder.encode(position));
     }
+
+    /**
+     * Tests encoding of the same coordinate with various separators, mixed with various
precisions.
+     *
+     * @throws TransformException if an error occurred while computing the MGRS label.
+     */
+    @Test
+    @DependsOnMethod("testPrecision")
+    public void testSeparator() throws TransformException {
+        final MilitaryGridReferenceSystem.Coder coder = coder();
+        final DirectPosition2D position = new DirectPosition2D(CommonCRS.WGS84.UTM(13, 103));
+        position.x =  377299;
+        position.y = 1483035;
+        assertEquals("separator", "", coder.getSeparator());
+        assertEquals("48PUV7729983035", coder.encode(position));
+
+        coder.setSeparator(" ");
+        assertEquals("separator", " ", coder.getSeparator());
+        assertEquals("48 P UV 77299 83035", coder.encode(position));
+
+        coder.setSeparator("/");
+        coder.setPrecision(100000);
+        assertEquals("separator", "/", coder.getSeparator());
+        assertEquals("48/P/UV", coder.encode(position));
+    }
 }



Mime
View raw message