You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2015/06/29 10:10:23 UTC

svn commit: r1688104 - in /sis/branches/JDK8/core: sis-metadata/src/main/java/org/apache/sis/internal/metadata/ sis-metadata/src/main/java/org/apache/sis/io/wkt/ sis-metadata/src/test/java/org/apache/sis/internal/metadata/ sis-metadata/src/test/java/or...

Author: desruisseaux
Date: Mon Jun 29 08:10:23 2015
New Revision: 1688104

URL: http://svn.apache.org/r1688104
Log:
WKT 2: replace all-lower case axis names by camel case axis names when the name is known.
Axis names are not really free text. They are specified by ISO 19111 and ISO 19162.
SIS does not put restriction on axis names, but we nevertheless try to use a unique name when we recognize it.

Added:
    sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/metadata/AxisNamesTest.java   (with props)
Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/AxisNames.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Transliterator.java
    sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/io/wkt/TransliteratorTest.java
    sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTParserTest.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/AxisNames.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/AxisNames.java?rev=1688104&r1=1688103&r2=1688104&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/AxisNames.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/AxisNames.java [UTF-8] Mon Jun 29 08:10:23 2015
@@ -16,6 +16,11 @@
  */
 package org.apache.sis.internal.metadata;
 
+import java.util.Map;
+import java.util.HashMap;
+import java.lang.reflect.Field;
+import org.apache.sis.util.logging.Logging;
+
 
 /**
  * Constants for axis names specified by ISO 19111 and ISO 19162.
@@ -120,8 +125,70 @@ public final class AxisNames {
     public static final String SOUTHING = "Southing";
 
     /**
+     * A ISO 19162 frequently used name.
+     */
+    public static final String TIME = "Time";
+
+    /**
+     * The map of all of the above values, used for fixing the case.
+     * Shall not be modified after construction.
+     */
+    private static final Map<String,String> VALUES;
+    static {
+        final Map<String,String> values = new HashMap<>(22);    // 18 elements + 25%, rounded to highest integer.
+        final StringBuilder buffer = new StringBuilder(22);     // Length of the longuest string: "Gravity-related height"
+        try {
+            for (final Field f : AxisNames.class.getFields()) {
+                final String name = (String) f.get(null);
+                values.put(toUpperCase(name, buffer).intern(), name);
+                buffer.setLength(0);
+                /*
+                 * The call to 'intern()' is because many upper-case strings match the field name
+                 * (e.g. "LATITUDE", "NORTHING", "DEPTH", etc.), so we use the same String instance.
+                 */
+            }
+        } catch (IllegalAccessException e) {
+            /*
+             * Should never happen. But if it happen anyway, do not kill the application for that.
+             * We will take the values that we have been able to map so far. The other values will
+             * just not have their case fixed.
+             */
+            Logging.unexpectedException(AxisNames.class, "<cinit>", e);
+        }
+        VALUES = values;
+    }
+
+    /**
      * Do not allow instantiation of this class.
      */
     private AxisNames() {
     }
+
+    /**
+     * Returns the given axis name in upper case without punctuation characters.
+     *
+     * @param  name   The axis name to return in upper-case.
+     * @param  buffer A temporary buffer to use. Must be initially empty.
+     * @return The given name converted to upper-case.
+     */
+    private static String toUpperCase(final String name, final StringBuilder buffer) {
+        for (int i=0; i<name.length(); i++) {
+            final char c = name.charAt(i);
+            if (Character.isLetterOrDigit(c)) {
+                buffer.append(Character.toUpperCase(c));
+            }
+        }
+        return buffer.toString();
+    }
+
+    /**
+     * Returns the given name in camel case if it is one of the know names.
+     * This method canonicalizes also the use of {@code '_'}, {@code '-'} and {@code ' '}.
+     *
+     * @param  name The name in any case.
+     * @return The given name in camel case.
+     */
+    public static String toCamelCase(final String name) {
+        return VALUES.getOrDefault(toUpperCase(name, new StringBuilder(name.length())), name);
+    }
 }

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Transliterator.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Transliterator.java?rev=1688104&r1=1688103&r2=1688104&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Transliterator.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Transliterator.java [UTF-8] Mon Jun 29 08:10:23 2015
@@ -223,7 +223,7 @@ public abstract class Transliterator imp
                 break;
             }
         }
-        return name;
+        return AxisNames.toCamelCase(name);
     }
 
     /**
@@ -348,6 +348,9 @@ public abstract class Transliterator imp
             if (condition.equals(csType)) {
                 return replacement;
             }
+        } else {
+            if (isLatLong(AxisNames.LATITUDE,  abbreviation)) return "φ";
+            if (isLatLong(AxisNames.LONGITUDE, abbreviation)) return "λ";
         }
         return abbreviation;
     }

Added: sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/metadata/AxisNamesTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/metadata/AxisNamesTest.java?rev=1688104&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/metadata/AxisNamesTest.java (added)
+++ sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/metadata/AxisNamesTest.java [UTF-8] Mon Jun 29 08:10:23 2015
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.metadata;
+
+import org.junit.Test;
+import java.lang.reflect.Field;
+import org.apache.sis.test.TestCase;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests the {@link AxisNames} class.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.6
+ * @version 0.6
+ * @module
+ */
+public final strictfp class AxisNamesTest extends TestCase {
+    /**
+     * Tests {@link AxisNames#toCamelCase(String)}.
+     */
+    @Test
+    public void testToCamelCase() {
+        assertEquals("Latitude",           AxisNames.toCamelCase("latitude"));
+        assertEquals("Geodetic longitude", AxisNames.toCamelCase("geodetic_longitude"));
+        assertEquals("Ellipsoidal height", AxisNames.toCamelCase("ellipsoidal height"));
+        assertEquals("unknown name",       AxisNames.toCamelCase("unknown name"));
+    }
+
+    /**
+     * Tests the consistency between the constant names and their values.
+     *
+     * @throws IllegalAccessException should never happen.
+     *         If it happen, the {@link AxisNames} static initializer may need to be revisited.
+     */
+    @Test
+    public void testConsistency() throws IllegalAccessException {
+        for (final Field f : AxisNames.class.getFields()) {
+            final String name = f.getName();
+            assertSame(name, f.get(null), AxisNames.toCamelCase(name));
+        }
+    }
+}

Propchange: sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/metadata/AxisNamesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/metadata/AxisNamesTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/io/wkt/TransliteratorTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/io/wkt/TransliteratorTest.java?rev=1688104&r1=1688103&r2=1688104&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/io/wkt/TransliteratorTest.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/io/wkt/TransliteratorTest.java [UTF-8] Mon Jun 29 08:10:23 2015
@@ -49,6 +49,8 @@ public final strictfp class Transliterat
         assertEquals("Geodetic longitude",  t.toLongAxisName("ellipsoidal", AxisDirection.EAST,  "longitude"));
         assertEquals("Spherical latitude",  t.toLongAxisName("spherical",   AxisDirection.NORTH, "lat"));
         assertEquals("Spherical longitude", t.toLongAxisName("spherical",   AxisDirection.EAST,  "long"));
+        assertEquals("Ellipsoidal height",  t.toLongAxisName("ellipsoidal", AxisDirection.UP,    "ellipsoidal_height"));
+        assertEquals("Unknown name",        t.toLongAxisName("ellipsoidal", AxisDirection.UP,    "Unknown name"));
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java?rev=1688104&r1=1688103&r2=1688104&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java [UTF-8] Mon Jun 29 08:10:23 2015
@@ -30,6 +30,7 @@ import org.junit.BeforeClass;
  * @module
  */
 @Suite.SuiteClasses({
+    org.apache.sis.internal.metadata.AxisNamesTest.class,
     org.apache.sis.internal.metadata.WKTKeywordsTest.class,
     org.apache.sis.internal.metadata.NameMeaningTest.class,
     org.apache.sis.internal.metadata.MetadataUtilitiesTest.class,

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java?rev=1688104&r1=1688103&r2=1688104&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java [UTF-8] Mon Jun 29 08:10:23 2015
@@ -150,31 +150,31 @@ public final strictfp class GeodeticObje
         assertEquals("unit", SI.METRE, axis.getUnit());
 
         axis = parse(CoordinateSystemAxis.class, "AXIS[“latitude”,north,ORDER[1],ANGLEUNIT[“degree”,0.0174532925199433]]");
-        assertEquals("name", "latitude", axis.getName().getCode());
+        assertEquals("name", "Latitude", axis.getName().getCode());
         assertEquals("abbreviation", "φ", axis.getAbbreviation());
         assertEquals("direction", AxisDirection.NORTH, axis.getDirection());
         assertEquals("unit", NonSI.DEGREE_ANGLE, axis.getUnit());
 
         axis = parse(CoordinateSystemAxis.class, "AXIS[“longitude”,EAST,order[2],UNIT[“degree”,0.0174532925199433]]");
-        assertEquals("name", "longitude", axis.getName().getCode());
+        assertEquals("name", "Longitude", axis.getName().getCode());
         assertEquals("abbreviation", "λ", axis.getAbbreviation());
         assertEquals("direction", AxisDirection.EAST, axis.getDirection());
         assertEquals("unit", NonSI.DEGREE_ANGLE, axis.getUnit());
 
         axis = parse(CoordinateSystemAxis.class, "AXIS[“ellipsoidal height (h)”,up,ORDER[3],LengthUnit[“kilometre”,1000]]");
-        assertEquals("name", "ellipsoidal height", axis.getName().getCode());
+        assertEquals("name", "Ellipsoidal height", axis.getName().getCode());
         assertEquals("abbreviation", "h", axis.getAbbreviation());
         assertEquals("direction", AxisDirection.UP, axis.getDirection());
         assertEquals("unit", SI.KILOMETRE, axis.getUnit());
 
         axis = parse(CoordinateSystemAxis.class, "AXIS[“time (t)”,future,TimeUnit[“hour”,3600]]");
-        assertEquals("name", "time", axis.getName().getCode());
+        assertEquals("name", "Time", axis.getName().getCode());
         assertEquals("abbreviation", "t", axis.getAbbreviation());
         assertEquals("direction", AxisDirection.FUTURE, axis.getDirection());
         assertEquals("unit", NonSI.HOUR, axis.getUnit());
 
         axis = parse(CoordinateSystemAxis.class, "AXIS[“easting (X)”,south,MERIDIAN[90,UNIT[“degree”,0.0174532925199433]]]");
-        assertEquals("name", "easting", axis.getName().getCode());
+        assertEquals("name", "Easting", axis.getName().getCode());
         assertEquals("abbreviation", "X", axis.getAbbreviation());
         assertEquals("direction", CoordinateSystems.directionAlongMeridian(AxisDirection.SOUTH, 90), axis.getDirection());
         assertEquals("unit", SI.METRE, axis.getUnit());

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTParserTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTParserTest.java?rev=1688104&r1=1688103&r2=1688104&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTParserTest.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTParserTest.java [UTF-8] Mon Jun 29 08:10:23 2015
@@ -16,12 +16,18 @@
  */
 package org.apache.sis.io.wkt;
 
-import org.opengis.referencing.crs.*;
+import org.opengis.referencing.cs.CoordinateSystem;
+import org.opengis.referencing.crs.CRSFactory;
+import org.opengis.util.FactoryException;
 import org.opengis.test.wkt.CRSParserTest;
+import org.apache.sis.internal.metadata.AxisNames;
 import org.apache.sis.test.DependsOn;
+import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
+import static org.junit.Assert.*;
+
 
 /**
  * Tests Well-Known Text parser using the tests defined in GeoAPI. Those tests use the
@@ -41,4 +47,64 @@ public class WKTParserTest extends CRSPa
     public WKTParserTest() {
         super(org.apache.sis.internal.system.DefaultFactories.forClass(CRSFactory.class));
     }
+
+    /**
+     * Verifies the axis names of a geographic CRS. This method is invoked when the parsed object is
+     * expected to have <cite>"Geodetic latitude"</cite> and <cite>"Geodetic longitude"</cite> names.
+     */
+    @SuppressWarnings("fallthrough")
+    private void verifyEllipsoidalCS() {
+        final CoordinateSystem cs = object.getCoordinateSystem();
+        switch (cs.getDimension()) {
+            default: assertEquals("name", AxisNames.ELLIPSOIDAL_HEIGHT, cs.getAxis(2).getName().getCode());
+            case 2:  assertEquals("name", AxisNames.GEODETIC_LONGITUDE, cs.getAxis(1).getName().getCode());
+            case 1:  assertEquals("name", AxisNames.GEODETIC_LATITUDE,  cs.getAxis(0).getName().getCode());
+            case 0:  break;
+        }
+        switch (cs.getDimension()) {
+            default: assertEquals("abbreviation", "h", cs.getAxis(2).getAbbreviation());
+            case 2:  assertEquals("abbreviation", "λ", cs.getAxis(1).getAbbreviation());
+            case 1:  assertEquals("abbreviation", "φ", cs.getAxis(0).getAbbreviation());
+            case 0:  break;
+        }
+    }
+
+    /**
+     * Completes the GeoAPI tests with a check of axis names.
+     *
+     * @throws FactoryException if an error occurred during the WKT parsing.
+     */
+    @Test
+    @Override
+    public void testGeographic() throws FactoryException {
+        super.testGeographic();
+        verifyEllipsoidalCS();
+    }
+
+    /**
+     * Completes the GeoAPI tests with a check of axis names.
+     *
+     * @throws FactoryException if an error occurred during the WKT parsing.
+     */
+    @Test
+    @Override
+    public void testGeographicWithRemark() throws FactoryException {
+        super.testGeographicWithRemark();
+        verifyEllipsoidalCS();
+    }
+
+    /**
+     * Completes the GeoAPI tests with a check of axis names.
+     *
+     * @throws FactoryException if an error occurred during the WKT parsing.
+     */
+    @Test
+    @Override
+    public void testGeocentric() throws FactoryException {
+        super.testGeocentric();
+        final CoordinateSystem cs = object.getCoordinateSystem();
+        assertEquals(AxisNames.GEOCENTRIC_X, cs.getAxis(0).getName().getCode());
+        assertEquals(AxisNames.GEOCENTRIC_Y, cs.getAxis(1).getName().getCode());
+        assertEquals(AxisNames.GEOCENTRIC_Z, cs.getAxis(2).getName().getCode());
+    }
 }