sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1786620 - in /sis/branches/JDK8/core: sis-referencing-by-identifiers/src/main/java/org/apache/sis/referencing/gazetteer/ sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/ sis-referencing/src/main/java/org/a...
Date Mon, 13 Mar 2017 00:17:53 GMT
Author: desruisseaux
Date: Mon Mar 13 00:17:53 2017
New Revision: 1786620

URL: http://svn.apache.org/viewvc?rev=1786620&view=rev
Log:
Add tests and bug fixes.

Modified:
    sis/branches/JDK8/core/sis-referencing-by-identifiers/src/main/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystem.java
    sis/branches/JDK8/core/sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/LocationViewer.java
    sis/branches/JDK8/core/sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystemTest.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/IntervalRectangle.java

Modified: sis/branches/JDK8/core/sis-referencing-by-identifiers/src/main/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystem.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing-by-identifiers/src/main/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystem.java?rev=1786620&r1=1786619&r2=1786620&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing-by-identifiers/src/main/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystem.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing-by-identifiers/src/main/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystem.java
[UTF-8] Mon Mar 13 00:17:53 2017
@@ -666,6 +666,7 @@ public class MilitaryGridReferenceSystem
 
             /**
              * Creates a new iterator over MGRS cells in the given area of interest.
+             * The borders of the given envelope are considered <strong>exclusive</strong>.
              */
             @SuppressWarnings({"unchecked", "rawtypes"})        // Generic array creation.
             IteratorAllZones(final Envelope areaOfInterest) throws FactoryException, TransformException
{
@@ -683,15 +684,15 @@ public class MilitaryGridReferenceSystem
                 final Envelope geographicArea = Envelopes.transform(areaOfInterest, datum.normalizedGeographic());
                 final double φmin = geographicArea.getMinimum(1);
                 final double φmax = geographicArea.getMaximum(1);
-                boolean southPole = (φmin <  TransverseMercator.Zoner.SOUTH_BOUNDS);
-                boolean northPole = (φmax >= TransverseMercator.Zoner.NORTH_BOUNDS);
+                boolean southPole = (φmin < TransverseMercator.Zoner.SOUTH_BOUNDS);
+                boolean northPole = (φmax > TransverseMercator.Zoner.NORTH_BOUNDS);
                 boolean southUTM, northUTM;
-                final int zoneStart, zoneEnd;
+                int zoneStart, zoneEnd;
                 if (φmax >= TransverseMercator.Zoner.SOUTH_BOUNDS && φmin <
TransverseMercator.Zoner.NORTH_BOUNDS) {
                     southUTM  = (φmin <  0);
                     northUTM  = (φmax >= 0);
-                    zoneStart = ZONER.zone(0, geographicArea.getMinimum(0));
-                    zoneEnd   = ZONER.zone(0, geographicArea.getMaximum(0)) + 1;
+                    zoneStart = ZONER.zone(0, geographicArea.getMinimum(0));            
       // Inclusive
+                    zoneEnd   = ZONER.zone(0, geographicArea.getMaximum(0)) + 1;        
       // Exclusive
                 } else {
                     southUTM  = false;
                     northUTM  = false;
@@ -742,14 +743,6 @@ public class MilitaryGridReferenceSystem
             }
 
             /**
-             * If this iterator is backed by only one worker iterator, returns that worker
iterator.
-             * Otherwise returns {@code this}. This method should be invoked after construction.
-             */
-            final Spliterator<String> simplify() {
-                return (iterators.length == 1) ? iterators[0] : this;
-            }
-
-            /**
              * Creates an iterator over the first half of the zones covered by the given
iterator.
              * After construction, the given iterator will cover the second half. This constructor
              * is for {@link #trySplit()} method only.
@@ -768,7 +761,15 @@ public class MilitaryGridReferenceSystem
              */
             @Override
             public Spliterator<String> trySplit() {
-                return new IteratorAllZones(this).simplify();
+                return (upper - index >= 2) ? new IteratorAllZones(this).simplify() :
null;
+            }
+
+            /**
+             * If this iterator is backed by only one worker iterator, returns that worker
iterator.
+             * Otherwise returns {@code this}. This method should be invoked after construction.
+             */
+            final Spliterator<String> simplify() {
+                return (upper - index == 1) ? iterators[index] : this;
             }
 
             /**
@@ -936,6 +937,7 @@ public class MilitaryGridReferenceSystem
 
         /**
          * Returns a new iterator for creating MGRS codes in a single UTM or UPS zone.
+         * The borders of the {@code areaOfInterest} rectangle are considered <strong>exclusive</strong>.
          *
          * @param areaOfInterest  the envelope for which to return MGRS codes. This envelope
can be in any CRS.
          * @param geographicArea  the area of interest transformed into a normalized geographic
CRS.
@@ -1004,10 +1006,10 @@ public class MilitaryGridReferenceSystem
              */
             final MathTransform2D op = (MathTransform2D) CRS.findOperation(sourceCRS, targetCRS,
null).getMathTransform();
             final Rectangle2D bounds = Shapes2D.transform(op, areaOfInterest, null);
-            gridX  = (((int) (bounds.getMinX() / step))    ) * step;
-            gridY  = (((int) (bounds.getMinY() / step))    ) * step;
-            xEnd   = (((int) (bounds.getMaxX() / step)) + 1) * step;
-            yEnd   = (((int) (bounds.getMaxY() / step)) + 1) * step;
+            gridX = (((int)          (bounds.getMinX() / step))) * step;                
   // Inclusive
+            gridY = (((int)          (bounds.getMinY() / step))) * step;
+            xEnd  = (((int) Math.ceil(bounds.getMaxX() / step))) * step;                
   // Exclusive
+            yEnd  = (((int) Math.ceil(bounds.getMaxY() / step))) * step;
             /*
              * Determine if we should iterate on rows upward or downward. The intend is to
iterate from equator to pole
              * in UTM zones, or from projection center to projection border in UPS cases.
 Those directions enable some
@@ -1098,6 +1100,10 @@ public class MilitaryGridReferenceSystem
         @Override
         public void forEachRemaining(final Consumer<? super String> action) {
             advance(action, true);
+            if (pending != null) {
+                action.accept(pending);
+                pending = null;
+            }
         }
 
         /**
@@ -1154,14 +1160,19 @@ public class MilitaryGridReferenceSystem
                         if (ref != null) {
                             /*
                              * If there is a change of latitude band, we may have missed
a cell before this one.
-                             * We verify that by encoding the cell for the position just
below current cell and
+                             * We verify that by encoding the cell for the position just
before current cell and
                              * comparing the latitude band of the result with our previous
latitude band.
                              */
-                            final char previous = latitudeBand;
+                            char previous = latitudeBand;
                             latitudeBand = encoder.latitudeBand;
                             if (latitudeBand != previous && previous != 0) {
                                 pending = ref;
-                                normalized.setOrdinate(1, gridY - 1);
+                                int y = gridY - 1;
+                                if (downward) {
+                                    y += step;              // If iterating downward, the
previous cell is upward.
+                                    previous = latitudeBand;
+                                }
+                                normalized.setOrdinate(1, y);
                                 ref = encoder.encode(this, normalized, false, separator,
digits);
                                 if (ref == null || encoder.latitudeBand == previous) {
                                     ref = pending;  // No result or same result than previous
iteration - cancel.
@@ -1962,7 +1973,7 @@ parse:                  switch (part) {
                             final int zc = ZONER.zoneCount();
                             if (zoneError > zc/2) zoneError -= zc;
                             if (ZONER.isSpecialCase(zone, φ)) {
-                                isValid = Math.abs(zoneError) == 1;         // Tolerance
in zone numbers for high latitudes.
+                                isValid = Math.abs(zoneError) <= 2;         // Tolerance
in zone numbers for high latitudes.
                             } else {
                                 final double rλ = Math.IEEEremainder(λ - ZONER.origin,
ZONER.width);    // Distance to closest zone change, in degrees of longitude.
                                 final double cv = (minX - ZONER.easting) / (λ - λ0);  
                 // Approximative conversion factor from degrees to metres.

Modified: sis/branches/JDK8/core/sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/LocationViewer.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/LocationViewer.java?rev=1786620&r1=1786619&r2=1786620&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/LocationViewer.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/LocationViewer.java
[UTF-8] Mon Mar 13 00:17:53 2017
@@ -31,10 +31,10 @@ import javax.swing.JFrame;
 import javax.swing.JPanel;
 import org.opengis.geometry.Envelope;
 import org.opengis.util.FactoryException;
+import org.opengis.referencing.crs.SingleCRS;
 import org.opengis.referencing.gazetteer.Location;
 import org.opengis.referencing.operation.MathTransform2D;
 import org.opengis.referencing.operation.TransformException;
-import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.apache.sis.internal.referencing.j2d.IntervalRectangle;
 import org.apache.sis.geometry.Envelope2D;
 import org.apache.sis.referencing.CommonCRS;
@@ -69,7 +69,12 @@ public final class LocationViewer extend
     /**
      * The coordinate reference system to use for displaying the shapes.
      */
-    private final CoordinateReferenceSystem displayCRS;
+    private final SingleCRS displayCRS;
+
+    /**
+     * The envelope projected to {@link #displayCRS}.
+     */
+    private Shape envelope;
 
     /**
      * The locations to show, together with their label.
@@ -87,7 +92,7 @@ public final class LocationViewer extend
      *
      * @param displayCRS  the coordinate reference system to use for displaying the location
shapes.
      */
-    public LocationViewer(final CoordinateReferenceSystem displayCRS) {
+    public LocationViewer(final SingleCRS displayCRS) {
         this.displayCRS = displayCRS;
         this.locations  = new LinkedHashMap<>();
         setBackground(Color.BLACK);
@@ -103,10 +108,10 @@ public final class LocationViewer extend
      * @throws TransformException if an error occurred while transforming an envelope.
      */
     public static void show(final MilitaryGridReferenceSystem.Coder coder, final Envelope
areaOfInterest,
-            CoordinateReferenceSystem displayCRS) throws FactoryException, TransformException
+            SingleCRS displayCRS) throws FactoryException, TransformException
     {
         if (displayCRS == null) {
-            displayCRS = areaOfInterest.getCoordinateReferenceSystem();
+            displayCRS = CRS.getHorizontalComponent(areaOfInterest.getCoordinateReferenceSystem());
         }
         final LocationViewer viewer = new LocationViewer(displayCRS);
         viewer.addLocations(coder, areaOfInterest);
@@ -132,7 +137,10 @@ public final class LocationViewer extend
         while (it.hasNext()) {
             final String code = it.next();
             addLocation(code, coder.decode(code));
+System.out.print("\"" + code + "\", ");
         }
+        envelope = ((MathTransform2D) CRS.findOperation(areaOfInterest.getCoordinateReferenceSystem(),
displayCRS, null)
+                        .getMathTransform()).createTransformedShape(new IntervalRectangle(areaOfInterest));
     }
 
     /**
@@ -174,8 +182,12 @@ public final class LocationViewer extend
                -getHeight() / bounds.getHeight());
         tr.translate(-bounds.getMinX(), -bounds.getMaxY());
         gr.transform(tr);
-        gr.setColor(Color.YELLOW);
         gr.setStroke(new BasicStroke(0));
+        if (envelope != null) {
+            gr.setColor(Color.RED);
+            gr.draw(envelope);
+        }
+        gr.setColor(Color.YELLOW);
         for (final Shape location : locations.values()) {
             gr.draw(location);
         }

Modified: sis/branches/JDK8/core/sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystemTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystemTest.java?rev=1786620&r1=1786619&r2=1786620&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystemTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing-by-identifiers/src/test/java/org/apache/sis/referencing/gazetteer/MilitaryGridReferenceSystemTest.java
[UTF-8] Mon Mar 13 00:17:53 2017
@@ -200,7 +200,7 @@ public final strictfp class MilitaryGrid
     /**
      * Returns a coder instance to test.
      */
-    private MilitaryGridReferenceSystem.Coder coder() {
+    private static MilitaryGridReferenceSystem.Coder coder() {
         return new MilitaryGridReferenceSystem().createCoder();
     }
 
@@ -644,21 +644,20 @@ public final strictfp class MilitaryGrid
      * <a href="https://www.ff-reichertshausen.de/cms/wp-content/uploads/2012/10/utmmeldegitter.jpg">this
picture</a>
      * (checked on March 2017).
      *
+     * <div class="note"><b>Tip:</b> in case of test failure, see {@link
LocationViewer} as a debugging tool.</div>
+     *
      * @throws TransformException if an error occurred while computing the coordinate.
      */
     @Test
-    @DependsOnMethod({"testEncodeUTM","testEncodeUPS"})
-    public void testIterator() throws TransformException {
-        final MilitaryGridReferenceSystem.Coder coder = coder();
-        coder.setPrecision(100000);
-        final Iterator<String> it = coder.encode(new Envelope2D(CommonCRS.defaultGeographic(),
5, 47, 8, 10));
+    @DependsOnMethod("testEncodeUTM")
+    public void testIteratorNorthUTM() throws TransformException {
         /*
          * Following is the list of MGRS references that we expect to find in the above area
of interest.
          * The references are distributed in 3 zones (31, 32 and 33) and 3 latitude bands
(T, U and V).
          * This test includes the Norway special case: between 56° and 64°N (latitude band
V), zone 32
          * is widened to 9° at the expense of zone 31. The test needs to be insensitive
to iteration order.
          */
-        final List<String> expected = Arrays.asList(
+        testIterator(new Envelope2D(CommonCRS.defaultGeographic(), 5, 47, 8, 10), Arrays.asList(
             "31TFN", "31TGN",    "32TKT", "32TLT", "32TMT", "32TNT", "32TPT", "32TQT",  
 "33TTN", "33TUN",
             "31TFP", "31TGP",    "32TKU", "32TLU", "32TMU", "32TNU", "32TPU", "32TQU",  
 "33TTP", "33TUP",
             "31UFP", "31UGP",    "32UKU", "32ULU", "32UMU", "32UNU", "32UPU", "32UQU",  
 "33UTP", "33UUP",
@@ -672,14 +671,50 @@ public final strictfp class MilitaryGrid
             "31UFB",                      "32ULG", "32UMG", "32UNG", "32UPG",           
 "33UUB",
             "31UFC",                      "32ULH", "32UMH", "32UNH", "32UPH",           
 "33UUC",
             /* Norway case */    "32VKH", "32VLH", "32VMH", "32VNH", "32VPH",           
 "33VUC",
-            /* Norway case */    "32VKJ", "32VLJ", "32VMJ", "32VNJ", "32VPJ",           
 "33VUD");
+            /* Norway case */    "32VKJ", "32VLJ", "32VMJ", "32VNJ", "32VPJ",           
 "33VUD"));
+    }
+
+    /**
+     * Tests iteration over codes in an area in South hemisphere.
+     *
+     * <div class="note"><b>Tip:</b> in case of test failure, see {@link
LocationViewer} as a debugging tool.</div>
+     *
+     * @throws TransformException if an error occurred while computing the coordinate.
+     */
+    @Test
+    @DependsOnMethod("testEncodeUTM")
+    public void testIteratorSouthUTM() throws TransformException {
+        testIterator(new Envelope2D(CommonCRS.defaultGeographic(), 5, -42, 8, 4), Arrays.asList(
+            "31HFT", "31HGT", "32HKC", "32HLC", "32HMC", "32HNC", "32HPC", "32HQC", "33HTT",
"33HUT",
+            "31HFS", "31HGS", "32HKB", "32HLB", "32HMB", "32HNB", "32HPB", "32HQB", "33HTS",
"33HUS",
+            "31HFR", "31HGR", "32HKA", "32HLA", "32HMA", "32HNA", "32HPA", "32HQA", "33HTR",
"33HUR",
+            "31GFR", "31GGR", "32GKA", "32GLA", "32GMA", "32GNA", "32GPA", "32GQA", "33GTR",
"33GUR",
+            "31GFQ", "31GGQ", "32GKV", "32GLV", "32GMV", "32GNV", "32GPV", "32GQV", "33GTQ",
"33GUQ",
+            "31GFP", "31GGP", "32GKU", "32GLU", "32GMU", "32GNU", "32GPU", "32GQU", "33GTP",
"33GUP"));
+    }
 
+    /**
+     * Implementation of {@link #testIteratorUTM()}.
+     */
+    private static void testIterator(final Envelope2D areaOfInterest, final List<String>
expected) throws TransformException {
+        final MilitaryGridReferenceSystem.Coder coder = coder();
+        coder.setPrecision(100000);
+        /*
+         * Test sequential iteration using iterator.
+         */
         final Set<String> remaining = new HashSet<>(expected);
         assertEquals("List of expected codes has duplicated values.", expected.size(), remaining.size());
-        while (it.hasNext()) {
+        for (final Iterator<String> it = coder.encode(areaOfInterest); it.hasNext();)
{
             final String code = it.next();
             assertTrue(code, remaining.remove(code));
         }
         assertTrue(remaining.toString(), remaining.isEmpty());
+        /*
+         * Test parallel iteration using stream.
+         */
+        assertTrue(remaining.addAll(expected));
+        assertEquals("List of expected codes has duplicated values.", expected.size(), remaining.size());
+        coder.encode(areaOfInterest, true).forEach((code) -> assertTrue(code, remaining.remove(code)));
+        assertTrue(remaining.toString(), remaining.isEmpty());
     }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/IntervalRectangle.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/IntervalRectangle.java?rev=1786620&r1=1786619&r2=1786620&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/IntervalRectangle.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/IntervalRectangle.java
[UTF-8] Mon Mar 13 00:17:53 2017
@@ -239,8 +239,9 @@ public class IntervalRectangle extends R
     }
 
     /**
-     * Tests if the interior of this rectangle intersects the interior of a specified set
of
-     * rectangular coordinates.
+     * Tests if the interior of this rectangle intersects the interior of a specified set
of rectangular coordinates.
+     * The edges are considered exclusive; this method returns {@code false} if the two rectangles
just touch to each
+     * other.
      *
      * @param  x       the <var>x</var> minimal ordinate value.
      * @param  y       the <var>y</var> minimal ordinate value.
@@ -259,6 +260,8 @@ public class IntervalRectangle extends R
 
     /**
      * Tests if the interior of this shape intersects the interior of a specified rectangle.
+     * The edges are considered exclusive; this method returns {@code false} if the two rectangles
+     * just touch to each other.
      *
      * @param  rect  the specified rectangle.
      * @return {@code true} if this shape and the specified rectangle intersect each other.



Mime
View raw message