sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1676637 - in /sis/branches/JDK8/core: sis-metadata/src/main/java/org/apache/sis/io/wkt/ sis-referencing/src/main/java/org/apache/sis/referencing/crs/ sis-referencing/src/test/java/org/apache/sis/referencing/crs/
Date Tue, 28 Apr 2015 21:31:52 GMT
Author: desruisseaux
Date: Tue Apr 28 21:31:52 2015
New Revision: 1676637

URL: http://svn.apache.org/r1676637
Log:
Referencing: first WKT2-compliant formatting of ProjectedCRS.

Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractCRS.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultGeodeticCRS.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultProjectedCRS.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java?rev=1676637&r1=1676636&r2=1676637&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
[UTF-8] Tue Apr 28 21:31:52 2015
@@ -1127,7 +1127,9 @@ public class Formatter implements Locali
             openElement(false, keyword);
             setColor(ElementKind.UNIT);
             final int fromIndex = buffer.appendCodePoint(symbols.getOpeningQuote(0)).length();
-            if (NonSI.DEGREE_ANGLE.equals(unit)) {
+            if (Unit.ONE.equals(unit)) {
+                buffer.append("unity");
+            } else if (NonSI.DEGREE_ANGLE.equals(unit)) {
                 buffer.append("degree");
             } else if (SI.METRE.equals(unit)) {
                 buffer.append(convention.usesCommonUnits() ? "meter" : "metre");

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractCRS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractCRS.java?rev=1676637&r1=1676636&r2=1676637&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractCRS.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractCRS.java
[UTF-8] Tue Apr 28 21:31:52 2015
@@ -393,14 +393,6 @@ public class AbstractCRS extends Abstrac
     }
 
     /**
-     * Returns the unit used for all axis, or {@code null} if not all axis uses the same
unit.
-     * This method is often used for formatting according  Well Known Text (WKT) version
1.
-     */
-    final Unit<?> getUnit() {
-        return ReferencingUtilities.getUnit(coordinateSystem);
-    }
-
-    /**
      * Compares this coordinate reference system with the specified object for equality.
      * If the {@code mode} argument value is {@link ComparisonMode#STRICT STRICT} or
      * {@link ComparisonMode#BY_CONTRACT BY_CONTRACT}, then all available properties are
@@ -467,20 +459,30 @@ public class AbstractCRS extends Abstrac
     @Override
     protected String formatTo(final Formatter formatter) {
         final String  keyword = super.formatTo(formatter);
-        final CoordinateSystem cs = getCoordinateSystem();
-        final boolean isWKT1  = formatter.getConvention().majorVersion() == 1;
-        final Unit<?> unit    = ReferencingUtilities.getUnit(cs);
-        final Unit<?> oldUnit = formatter.addContextualUnit(unit);
         formatter.newLine();
         formatter.append(toFormattable(getDatum()));
         formatter.newLine();
+        formatCS(formatter, getCoordinateSystem(), formatter.getConvention().majorVersion()
== 1);
+        return keyword;
+    }
+
+    /**
+     * Formats the given coordinate system.
+     *
+     * @param formatter The formatter where to append the coordinate system.
+     * @param cs        The coordinate system to append.
+     * @param isWKT1    {@code true} if formatting WKT 1, or {@code false} for WKT 2.
+     */
+    final void formatCS(final Formatter formatter, final CoordinateSystem cs, final boolean
isWKT1) {
+        final Unit<?> unit    = ReferencingUtilities.getUnit(cs);
+        final Unit<?> oldUnit = formatter.addContextualUnit(unit);
         if (isWKT1) { // WKT 1 writes unit before axes, while WKT 2 writes them after axes.
             formatter.append(unit);
             if (unit == null) {
                 formatter.setInvalidWKT(this, null);
             }
         } else {
-            formatter.append(toFormattable(cs)); // The concept of CoordinateSystem was not
explicit in WKT 1.
+            formatter.append(toFormattable(cs)); // WKT2 only, since the concept of CoordinateSystem
was not explicit in WKT 1.
             formatter.indent(+1);
         }
         final int dimension = cs.getDimension();
@@ -496,6 +498,5 @@ public class AbstractCRS extends Abstrac
         formatter.removeContextualUnit(unit);
         formatter.addContextualUnit(oldUnit);
         formatter.newLine(); // For writing the ID[…] element on its own line.
-        return keyword;
     }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultGeodeticCRS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultGeodeticCRS.java?rev=1676637&r1=1676636&r2=1676637&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultGeodeticCRS.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultGeodeticCRS.java
[UTF-8] Tue Apr 28 21:31:52 2015
@@ -17,7 +17,6 @@
 package org.apache.sis.referencing.crs;
 
 import java.util.Map;
-import javax.measure.unit.Unit;
 import javax.measure.unit.NonSI;
 import javax.xml.bind.annotation.XmlType;
 import javax.xml.bind.annotation.XmlElement;
@@ -175,16 +174,10 @@ class DefaultGeodeticCRS extends Abstrac
     @Override
     protected String formatTo(final Formatter formatter) {
         WKTUtilities.appendName(this, formatter, null);
-        final boolean isWKT1  = formatter.getConvention().majorVersion() == 1;
-        final Unit<?> unit    = getUnit();
-        final Unit<?> oldUnit = formatter.addContextualUnit(unit);
-        formatTo(isWKT1, getDatum(), formatter);
+        final boolean isWKT1 = (formatter.getConvention().majorVersion() == 1);
+        formatDatum(formatter, getDatum(), isWKT1);
         CoordinateSystem cs = getCoordinateSystem();
-        if (isWKT1) { // WKT 1 writes unit before axes, while WKT 2 writes them after axes.
-            formatter.append(unit);
-            if (unit == null) {
-                formatter.setInvalidWKT(this, null);
-            }
+        if (isWKT1) {
             /*
              * Replaces the given coordinate system by an instance conform to the conventions
used in WKT 1.
              * Note that we can not delegate this task to subclasses, because XML unmarshalling
of a geodetic
@@ -198,23 +191,8 @@ class DefaultGeodeticCRS extends Abstrac
                     formatter.setInvalidWKT(cs, null);
                 }
             }
-        } else {
-            formatter.append(toFormattable(cs)); // WKT2 only, since the concept of CoordinateSystem
was not explicit in WKT 1.
-            formatter.indent(+1);
-        }
-        final int dimension = cs.getDimension();
-        for (int i=0; i<dimension; i++) {
-            formatter.newLine();
-            formatter.append(toFormattable(cs.getAxis(i)));
         }
-        if (!isWKT1) { // WKT 2 writes unit after axes, while WKT 1 wrote them before axes.
-            formatter.newLine();
-            formatter.append(unit);
-            formatter.indent(-1);
-        }
-        formatter.removeContextualUnit(unit);
-        formatter.addContextualUnit(oldUnit);
-        formatter.newLine(); // For writing the ID[…] element on its own line.
+        formatCS(formatter, cs, isWKT1);
         if (!isWKT1) {
             return "GeodeticCRS";
         }
@@ -232,11 +210,13 @@ class DefaultGeodeticCRS extends Abstrac
      * Formats the given geodetic datum to the given formatter. This is part of the WKT formatting
      * for a geodetic CRS, either standalone or as part of a projected CRS.
      *
-     * @param isWKT1    {@code true} if formatting WKT 1, or {@code false} for WKT 2.
-     * @param datum     The datum to format.
      * @param formatter Where to format the datum.
+     * @param datum     The datum to format.
+     * @param isWKT1    {@code true} if formatting WKT 1, or {@code false} for WKT 2.
+     *
+     * @see #formatCS(Formatter, CoordinateSystem, boolean)
      */
-    static void formatTo(final boolean isWKT1, final GeodeticDatum datum, final Formatter
formatter) {
+    static void formatDatum(final Formatter formatter, final GeodeticDatum datum, final boolean
isWKT1) {
         formatter.newLine();
         formatter.append(toFormattable(datum));
         formatter.newLine();

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultProjectedCRS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultProjectedCRS.java?rev=1676637&r1=1676636&r2=1676637&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultProjectedCRS.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultProjectedCRS.java
[UTF-8] Tue Apr 28 21:31:52 2015
@@ -251,17 +251,11 @@ public class DefaultProjectedCRS extends
     @Override
     protected String formatTo(final Formatter formatter) {
         WKTUtilities.appendName(this, formatter, null);
-        final Convention    convention     = formatter.getConvention();
-        final boolean       isWKT1         = (convention.majorVersion() == 1);
-        final Conversion    conversion     = getConversionFromBase();
-        final GeographicCRS baseCRS        = getBaseCRS();
-        final Ellipsoid     ellipsoid      = getDatum().getEllipsoid();
-        final CartesianCS   cs             = getCoordinateSystem();
-        final Unit<?>       linearUnit     = ReferencingUtilities.getUnit(cs);
-        final Unit<Angle>   angularUnit    = ReferencingUtilities.getAngularUnit(baseCRS.getCoordinateSystem());
-        final Unit<?>       oldLinearUnit  = formatter.addContextualUnit(linearUnit);
-        final Unit<Angle>   oldAngularUnit = formatter.addContextualUnit(angularUnit);
-        final Unit<Length>  axisUnit       = ellipsoid.getAxisUnit();
+        final Convention    convention = formatter.getConvention();
+        final boolean       isWKT1     = (convention.majorVersion() == 1);
+        final GeographicCRS baseCRS    = getBaseCRS();
+        final Unit<Angle>   unit       = ReferencingUtilities.getAngularUnit(baseCRS.getCoordinateSystem());
+        final Unit<Angle>   oldUnit    = formatter.addContextualUnit(unit);
         formatter.newLine();
         if (isWKT1) {
             formatter.append(toFormattable(baseCRS));
@@ -275,52 +269,23 @@ public class DefaultProjectedCRS extends
              * like the opposite of what we would expect, but this is because formatting
the unit
              * here allow us to avoid repeating the unit in many projection parameters.
              */
-            formatter.append(new BaseCRS(baseCRS, isWKT1, convention.isSimplified() ? angularUnit
: null));
+            formatter.append(new BaseCRS(baseCRS, isWKT1, convention.isSimplified() ? unit
: null));
         }
         formatter.newLine();
-        formatter.append(DefaultOperationMethod.castOrCopy(conversion.getMethod()));
-        formatter.newLine();
-        for (final GeneralParameterValue param : conversion.getParameterValues().values())
{
-            final GeneralParameterDescriptor desc = param.getDescriptor();
-            String name;
-            if (IdentifiedObjects.isHeuristicMatchForName(desc, name = Constants.SEMI_MAJOR)
||
-                IdentifiedObjects.isHeuristicMatchForName(desc, name = Constants.SEMI_MINOR))
-            {
-                /*
-                 * Do not format semi-major and semi-minor axis length in most cases,  since
those
-                 * informations are provided in the ellipsoid. An exception to this rule
occurs if
-                 * the lengths are different from the ones declared in the datum.
-                 */
-                if (param instanceof ParameterValue<?>) {
-                    final double value = ((ParameterValue<?>) param).doubleValue(axisUnit);
-                    final double expected = (name == Constants.SEMI_MINOR)   // using '=='
is okay here.
-                            ? ellipsoid.getSemiMinorAxis() : ellipsoid.getSemiMajorAxis();
-                    if (value == expected) {
-                        continue;
-                    }
-                }
-            }
-            WKTUtilities.append(param, formatter);
-        }
-        formatter.append(linearUnit);
-        final int dimension = cs.getDimension();
-        for (int i=0; i<dimension; i++) {
-            formatter.newLine();
-            formatter.append(toFormattable(cs.getAxis(i)));
-        }
-        if (linearUnit == null) {
-            formatter.setInvalidWKT(this, null);
+        final Parameters p = new Parameters(this);
+        if (isWKT1) {
+            p.append(formatter);    // Format outside of any "Conversion" element.
+        } else {
+            formatter.append(p);    // Format inside a "Conversion" element.
         }
-        formatter.removeContextualUnit(linearUnit);
-        formatter.removeContextualUnit(angularUnit);
-        formatter.addContextualUnit(oldAngularUnit);
-        formatter.addContextualUnit(oldLinearUnit);
-        formatter.newLine(); // For writing the ID[…] element on its own line.
+        formatCS(formatter, getCoordinateSystem(), isWKT1);
+        formatter.removeContextualUnit(unit);
+        formatter.addContextualUnit(oldUnit);
         return isWKT1 ? "ProjCS" : "ProjectedCRS";
     }
 
     /**
-     * Temporary object use for formatting the {@code BaseGeodCRS} element inside a {@code
ProjectedCRS} element.
+     * Temporary object for formatting the {@code BaseGeodCRS} element inside a {@code ProjectedCRS}
element.
      */
     private static final class BaseCRS extends FormattableObject {
         /** The base CRS. */
@@ -342,10 +307,64 @@ public class DefaultProjectedCRS extends
         /** Formats this {@code BaseGeodCRS} element. */
         @Override protected String formatTo(final Formatter formatter) {
             WKTUtilities.appendName(baseCRS, formatter, null);
-            DefaultGeodeticCRS.formatTo(isWKT1, baseCRS.getDatum(), formatter);
+            DefaultGeodeticCRS.formatDatum(formatter, baseCRS.getDatum(), isWKT1);
             formatter.append(angularUnit);  // May be null.
             formatter.newLine();
             return "BaseGeodCRS";
         }
     }
+
+    /**
+     * Temporary object for formatting the projection method and parameters inside a {@code
Conversion} element.
+     */
+    private static final class Parameters extends FormattableObject {
+        /** The conversion which specify the operation method and parameters. */
+        private final Conversion conversion;
+
+        /** Semi-major and semi-minor axis lengths. */
+        private final Ellipsoid ellipsoid;
+
+        /** Creates a new temporary {@code Conversion} elements for the parameters of the
given CRS. */
+        Parameters(final DefaultProjectedCRS crs) {
+            conversion = crs.getConversionFromBase();
+            ellipsoid = crs.getDatum().getEllipsoid();
+        }
+
+        /** Formats this {@code Conversion} element. */
+        @Override protected String formatTo(final Formatter formatter) {
+            WKTUtilities.appendName(conversion, formatter, null);
+            formatter.newLine();
+            append(formatter);
+            return "Conversion";
+        }
+
+        /** Formats this {@code Conversion} element without the conversion name. */
+        void append(final Formatter formatter) {
+            final Unit<Length> axisUnit = ellipsoid.getAxisUnit();
+            formatter.append(DefaultOperationMethod.castOrCopy(conversion.getMethod()));
+            formatter.newLine();
+            for (final GeneralParameterValue param : conversion.getParameterValues().values())
{
+                final GeneralParameterDescriptor desc = param.getDescriptor();
+                String name;
+                if (IdentifiedObjects.isHeuristicMatchForName(desc, name = Constants.SEMI_MAJOR)
||
+                    IdentifiedObjects.isHeuristicMatchForName(desc, name = Constants.SEMI_MINOR))
+                {
+                    /*
+                     * Do not format semi-major and semi-minor axis length in most cases,
 since those
+                     * informations are provided in the ellipsoid. An exception to this rule
occurs if
+                     * the lengths are different from the ones declared in the datum.
+                     */
+                    if (param instanceof ParameterValue<?>) {
+                        final double value = ((ParameterValue<?>) param).doubleValue(axisUnit);
+                        final double expected = (name == Constants.SEMI_MINOR)   // using
'==' is okay here.
+                                ? ellipsoid.getSemiMinorAxis() : ellipsoid.getSemiMajorAxis();
+                        if (value == expected) {
+                            continue;
+                        }
+                    }
+                }
+                WKTUtilities.append(param, formatter);
+            }
+        }
+    }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java?rev=1676637&r1=1676636&r2=1676637&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/crs/DefaultProjectedCRSTest.java
[UTF-8] Tue Apr 28 21:31:52 2015
@@ -23,7 +23,10 @@ import org.opengis.util.FactoryException
 import org.opengis.referencing.crs.ProjectedCRS;
 import org.apache.sis.referencing.cs.HardCodedCS;
 import org.apache.sis.referencing.GeodeticObjectBuilder;
+import org.apache.sis.metadata.iso.citation.Citations;
+import org.apache.sis.internal.util.Constants;
 import org.apache.sis.io.wkt.Convention;
+import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
@@ -56,7 +59,9 @@ public final strictfp class DefaultProje
                 .setParameter("Scale factor at natural origin", 0.99987742, Unit.ONE)
                 .setParameter("False easting",                      600000, SI.METRE)
                 .setParameter("False northing",                    2200000, SI.METRE)
+                .setCodeSpace(Citations.EPSG, Constants.EPSG)
                 .addName("NTF (Paris) / Lambert zone II")
+                .addIdentifier("27572")
                 .createProjectedCRS(HardCodedCRS.NTF, HardCodedCS.PROJECTED);
     }
 
@@ -73,18 +78,48 @@ public final strictfp class DefaultProje
                 "  GEOGCS[“NTF (Paris)”,\n" +
                 "    DATUM[“Nouvelle Triangulation Francaise”,\n" +
                 "      SPHEROID[“NTF”, 6378249.2, 293.4660212936269]],\n" +
-                "    PRIMEM[“Paris”, 2.33722917],\n" +
+                "    PRIMEM[“Paris”, 2.33722917],\n" +                      // Note the
conversion from 2.5969213 grades.
                 "    UNIT[“degree”, 0.017453292519943295],\n" +
                 "    AXIS[“Longitude”, EAST],\n" +
                 "    AXIS[“Latitude”, NORTH]],\n" +
                 "  PROJECTION[“Lambert_Conformal_Conic_1SP”, AUTHORITY[“EPSG”, “9801”]],\n"
+
-                "  PARAMETER[“latitude_of_origin”, 46.8],\n" +
+                "  PARAMETER[“latitude_of_origin”, 46.8],\n" +              // Note the
conversion from 52 grades.
                 "  PARAMETER[“scale_factor”, 0.99987742],\n" +
                 "  PARAMETER[“false_easting”, 600000.0],\n" +
                 "  PARAMETER[“false_northing”, 2200000.0],\n" +
                 "  UNIT[“metre”, 1],\n" +
                 "  AXIS[“Easting”, EAST],\n" +
-                "  AXIS[“Northing”, NORTH]]",
+                "  AXIS[“Northing”, NORTH],\n" +
+                "  AUTHORITY[“EPSG”, “27572”]]",
+                crs);
+    }
+
+    /**
+     * Tests WKT 2 formatting.
+     *
+     * @throws FactoryException if the CRS creation failed.
+     */
+    @Test
+    @DependsOnMethod("testWKT1")
+    public void testWKT2() throws FactoryException {
+        final ProjectedCRS crs = create();
+        assertWktEquals(Convention.WKT2,
+                "ProjectedCRS[“NTF (Paris) / Lambert zone II”,\n" +
+                "  BaseGeodCRS[“NTF (Paris)”,\n" +
+                "    Datum[“Nouvelle Triangulation Francaise”,\n" +
+                "      Ellipsoid[“NTF”, 6378249.2, 293.4660212936269, LengthUnit[“metre”,
1]]],\n" +
+                "      PrimeMeridian[“Paris”, 2.5969213, AngleUnit[“grade”, 0.015707963267948967]]],\n"
+
+                "  Conversion[“Lambert zone II”,\n" +
+                "    Method[“Lambert Conic Conformal (1SP)”, Id[“EPSG”, 9801, Citation[“IOGP”]]],\n"
+
+                "    Parameter[“Latitude of natural origin”, 46.8, AngleUnit[“degree”,
0.017453292519943295], Id[“EPSG”, 8801]],\n" +
+                "    Parameter[“Scale factor at natural origin”, 0.99987742, ScaleUnit[“unity”,
1], Id[“EPSG”, 8805]],\n" +
+                "    Parameter[“False easting”, 600000.0, LengthUnit[“metre”, 1],
Id[“EPSG”, 8806]],\n" +
+                "    Parameter[“False northing”, 2200000.0, LengthUnit[“metre”, 1],
Id[“EPSG”, 8807]]],\n" +
+                "  CS[“Cartesian”, 2],\n" +
+                "    Axis[“Easting (E)”, east, Order[1]],\n" +
+                "    Axis[“Northing (N)”, north, Order[2]],\n" +
+                "    LengthUnit[“metre”, 1],\n" +
+                "  Id[“EPSG”, 27572, Citation[“IOGP”], URI[“urn:ogc:def:crs:EPSG::27572”]]]",
                 crs);
     }
 }



Mime
View raw message