This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 8a6ea5a5c076cc2719ab0523f9a7884b65216ecc Author: jsorel <[email protected]> AuthorDate: Mon Nov 23 11:52:38 2026 +0100 fix(DGGRS): use one fix convention for Code ordinates : one ordinate per axis --- .../referencing/dggs/DiscreteGlobalGridSystem.java | 4 +- .../sis/referencing/dggs/ZonalReferenceSystem.java | 7 +- .../main/org/apache/sis/referencing/dggs/Zone.java | 37 ++++--- .../shared/AbstractDiscreteGlobalGrid.java | 5 +- .../shared/AbstractDiscreteGlobalGridSystem.java | 20 ++-- .../dggs/internal/shared/AbstractZone.java | 31 +++++- .../shared/DefaultZonalReferenceSystem.java | 9 +- .../apache/sis/referencing/dggs/s2/S2Dggrs.java | 2 +- .../org/apache/sis/referencing/dggs/s2/S2Zone.java | 10 +- .../apache/sis/referencing/dggs/smc/SmcDggrs.java | 2 +- .../apache/sis/referencing/dggs/smc/SmcDggs.java | 3 +- .../apache/sis/referencing/dggs/smc/SmcZone.java | 10 +- .../main/org/apache/sis/referencing/rs/Code.java | 8 +- .../rs/internal/shared/CodeOperations.java | 79 +++++++++++++-- .../dggs/DiscreteGlobalGridCoverageProcessor.java | 4 - .../org/apache/sis/referencing/dggs/ZoneTest.java | 108 +++++++++++++++++++++ .../dggs/internal/shared/AbstractZoneTest.java | 27 ++++++ .../sis/referencing/dggs/smc/SmcDggrsTest.java | 5 +- .../apache/sis/referencing/dggs/smc/SmcTest.java | 5 +- .../sis/referencing/rs/CodeOperationTest.java | 65 +++++++++++++ 20 files changed, 376 insertions(+), 65 deletions(-) diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/DiscreteGlobalGridSystem.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/DiscreteGlobalGridSystem.java index a7cf0e7b06..e52beb7045 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/DiscreteGlobalGridSystem.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/DiscreteGlobalGridSystem.java @@ -17,6 +17,8 @@ package org.apache.sis.referencing.dggs; import java.util.List; +import javax.measure.Quantity; +import javax.measure.quantity.Area; import org.opengis.referencing.crs.CoordinateReferenceSystem; @@ -42,7 +44,7 @@ public interface DiscreteGlobalGridSystem { /** * @return surface of the celestial body. */ - double getCelestialBodySurface(); + Quantity<Area> getCelestialBodySurface(); /** * @return name of the DGGS base polyhedron geometry diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/ZonalReferenceSystem.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/ZonalReferenceSystem.java index 21c5ca33af..aed1f74eb6 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/ZonalReferenceSystem.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/ZonalReferenceSystem.java @@ -38,11 +38,8 @@ public interface ZonalReferenceSystem { /** * @return description of the zonal reference system + * + * @see DiscreteGlobalGridHierarchy#supportLongIdentifiers() */ String getDescription(); - - /** - * @return true if create ZonalIdentifiers have a Long type representation - */ - boolean supportUInt64Form(); } diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/Zone.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/Zone.java index 1568d0b22b..ee9d757a4b 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/Zone.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/Zone.java @@ -16,8 +16,13 @@ */ package org.apache.sis.referencing.dggs; +import java.time.Duration; import java.util.Collection; +import java.util.Optional; import java.util.stream.Stream; +import javax.measure.Quantity; +import javax.measure.quantity.Area; +import javax.measure.quantity.Volume; import org.opengis.geometry.Envelope; import org.opengis.metadata.extent.GeographicExtent; import org.opengis.referencing.gazetteer.Location; @@ -85,19 +90,19 @@ public interface Zone extends Location, Comparable<Zone> { String getShapeType(); /** - * @return area of the zone, can be null if not 2D + * @return area of the zone, or empty if the zone is not two-dimensional. */ - Double getAreaMetersSquare(); + Optional<Quantity<Area>> getArea(); /** - * @return volume if the zone, can be null if not 3D + * @return volume of the zone, or empty if the zone is not three-dimensional. */ - Double volumeMetersCube(); + Optional<Quantity<Volume>> getVolume(); /** - * @return duration of the zone, can be null if it has no temporal dimension + * @return duration of the zone, or empty if the zone has no temporal dimension. */ - Double temporalDurationSeconds(); + Optional<Duration> getDuration(); /** * @return refinement level @@ -212,7 +217,8 @@ public interface Zone extends Location, Comparable<Zone> { /** * If zone have different levels, the lowest level is place first. - * If zone have the same level, they are sorted using the indexed identifier. + * If zone have the same level, they are sorted using the long identifier, + * or the text identifier if the hierarchy of one of the zones has no long identifiers. * * @param o to compare against */ @@ -220,12 +226,19 @@ public interface Zone extends Location, Comparable<Zone> { public default int compareTo(Zone o) { final int level = getLocationType().getRefinementLevel(); final int levelOther = o.getLocationType().getRefinementLevel(); - if (level == levelOther) { + if (level != levelOther) { + return Integer.compare(level, levelOther); + } + if (hasLongIdentifier(this) && hasLongIdentifier(o)) { return Long.compareUnsigned(getLongIdentifier(), o.getLongIdentifier()); - } else if (level < levelOther) { - return -1; //place it before - } else { - return +1; //place it after } + return getTextIdentifier().toString().compareTo(o.getTextIdentifier().toString()); + } + + /** + * Returns whether the hierarchy of the given zone has long identifiers. + */ + private static boolean hasLongIdentifier(final Zone zone) { + return zone.getLocationType().getReferenceSystem().getGridSystem().getHierarchy().supportLongIdentifiers(); } } diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGrid.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGrid.java index e2fb4bd8b0..e755ecb4f2 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGrid.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGrid.java @@ -49,7 +49,8 @@ public abstract class AbstractDiscreteGlobalGrid<T extends AbstractDiscreteGloba @Override public Quantity<?> getPrecision() { - final double area = this.hierarchy.dggrs.getGridSystem().getCelestialBodySurface() / getZoneCount(); + final double area = this.hierarchy.dggrs.getGridSystem().getCelestialBodySurface() + .to(Units.SQUARE_METRE).getValue().doubleValue() / getZoneCount(); return Quantities.create(Math.sqrt(area), Units.METRE); } @@ -68,7 +69,7 @@ public abstract class AbstractDiscreteGlobalGrid<T extends AbstractDiscreteGloba crs = hierarchy.dggrs.getGridSystem().getCoordinateReferenceSystem(); crsDim = crs.getCoordinateSystem().getDimension(); - if (!hierarchy.dggrs.getZonalSystem().supportUInt64Form()) return false; + if (!hierarchy.supportLongIdentifiers()) return false; idToCrs = new AbstractMathTransform() { @Override diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGridSystem.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGridSystem.java index 40c0a1209b..11d5ea9de9 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGridSystem.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractDiscreteGlobalGridSystem.java @@ -16,9 +16,14 @@ */ package org.apache.sis.referencing.dggs.internal.shared; +import javax.measure.Quantity; +import javax.measure.UnitConverter; +import javax.measure.quantity.Area; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.crs.GeographicCRS; import org.opengis.referencing.datum.Ellipsoid; +import org.apache.sis.measure.Quantities; +import org.apache.sis.measure.Units; import org.apache.sis.referencing.datum.DatumOrEnsemble; import org.apache.sis.referencing.dggs.DiscreteGlobalGridSystem; @@ -30,17 +35,20 @@ import org.apache.sis.referencing.dggs.DiscreteGlobalGridSystem; public abstract class AbstractDiscreteGlobalGridSystem implements DiscreteGlobalGridSystem { protected final CoordinateReferenceSystem crs; - protected final double surfaceArea; + + /** + * Surface of the celestial body, in square metres. + */ + protected final Quantity<Area> surfaceArea; public AbstractDiscreteGlobalGridSystem(CoordinateReferenceSystem crs) { this.crs = crs; final GeographicCRS gcrs = (GeographicCRS) crs; final Ellipsoid ellipsoid = DatumOrEnsemble.asDatum(gcrs).getEllipsoid(); - final double semiMajorAxis = ellipsoid.getSemiMajorAxis(); - final double semiMinorAxis = ellipsoid.getSemiMinorAxis(); - final double r = (semiMajorAxis + semiMinorAxis) / 2; - surfaceArea = 4.0 * Math.PI * r * r; + final UnitConverter toMetre = ellipsoid.getAxisUnit().getConverterTo(Units.METRE); + final double r = toMetre.convert((ellipsoid.getSemiMajorAxis() + ellipsoid.getSemiMinorAxis()) / 2); + surfaceArea = Quantities.create(4.0 * Math.PI * r * r, Units.SQUARE_METRE); } @Override @@ -49,7 +57,7 @@ public abstract class AbstractDiscreteGlobalGridSystem implements DiscreteGlobal } @Override - public final double getCelestialBodySurface() { + public final Quantity<Area> getCelestialBodySurface() { return surfaceArea; } diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractZone.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractZone.java index 5448bedb75..b450b25b8d 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractZone.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/AbstractZone.java @@ -16,12 +16,19 @@ */ package org.apache.sis.referencing.dggs.internal.shared; +import java.time.Duration; import java.util.Collection; import java.util.Collections; import java.util.Objects; +import java.util.Optional; +import javax.measure.Quantity; +import javax.measure.quantity.Area; +import javax.measure.quantity.Volume; import org.opengis.metadata.citation.Party; import org.opengis.metadata.extent.TemporalExtent; import org.opengis.util.InternationalString; +import org.apache.sis.measure.Quantities; +import org.apache.sis.measure.Units; import org.apache.sis.referencing.dggs.DiscreteGlobalGridReferenceSystem; import org.apache.sis.referencing.dggs.Zone; @@ -38,14 +45,30 @@ public abstract class AbstractZone<T extends DiscreteGlobalGridReferenceSystem> this.dggrs = dggrs; } + /** + * Returns an empty value, zones are two-dimensional by default. + */ @Override - public Double volumeMetersCube() { - return null; + public Optional<Quantity<Volume>> getVolume() { + return Optional.empty(); } + /** + * Returns an empty value, zones have no temporal dimension by default. + */ @Override - public Double temporalDurationSeconds() { - return null; + public Optional<Duration> getDuration() { + return Optional.empty(); + } + + /** + * Returns the area of a zone covering the given fraction of the celestial body surface. + * + * @param fraction area of the zone on the unit sphere divided by 4π. + */ + protected final Optional<Quantity<Area>> areaFromSurfaceFraction(final double fraction) { + final double surface = dggrs.getGridSystem().getCelestialBodySurface().to(Units.SQUARE_METRE).getValue().doubleValue(); + return Optional.of(Quantities.create(fraction * surface, Units.SQUARE_METRE)); } @Override diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/DefaultZonalReferenceSystem.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/DefaultZonalReferenceSystem.java index 72b774d4a0..afb2a09cea 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/DefaultZonalReferenceSystem.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/internal/shared/DefaultZonalReferenceSystem.java @@ -27,12 +27,10 @@ public class DefaultZonalReferenceSystem implements ZonalReferenceSystem { private final String identifier; private final String description; - private final boolean supportUInt64; - public DefaultZonalReferenceSystem(String identifier, String description, boolean supportUInt64) { + public DefaultZonalReferenceSystem(String identifier, String description) { this.identifier = identifier; this.description = description; - this.supportUInt64 = supportUInt64; } @Override @@ -45,9 +43,4 @@ public class DefaultZonalReferenceSystem implements ZonalReferenceSystem { return description; } - @Override - public boolean supportUInt64Form() { - return supportUInt64; - } - } diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/s2/S2Dggrs.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/s2/S2Dggrs.java index 8962312b82..1354a6be43 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/s2/S2Dggrs.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/s2/S2Dggrs.java @@ -52,7 +52,7 @@ public final class S2Dggrs extends DiscreteGlobalGridReferenceSystem { public static final String IDENTIFIER = "S2"; public static final S2Dggrs INSTANCE = new S2Dggrs(); - private static final ZonalReferenceSystem ZRS = new DefaultZonalReferenceSystem("default", "", true); + private static final ZonalReferenceSystem ZRS = new DefaultZonalReferenceSystem("default", ""); final S2Dggs dggs; diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/s2/S2Zone.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/s2/S2Zone.java index f65ea37ba3..f51eb044da 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/s2/S2Zone.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/s2/S2Zone.java @@ -19,6 +19,9 @@ package org.apache.sis.referencing.dggs.s2; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Optional; +import javax.measure.Quantity; +import javax.measure.quantity.Area; import com.google.common.geometry.S2Cell; import com.google.common.geometry.S2CellId; import com.google.common.geometry.S2LatLng; @@ -71,9 +74,12 @@ final class S2Zone extends AbstractZone<S2Dggrs> { return "square"; } + /** + * Returns the cell area, computed from its area on the unit sphere. + */ @Override - public Double getAreaMetersSquare() { - return new S2Cell(new S2CellId(hash)).exactArea(); + public Optional<Quantity<Area>> getArea() { + return areaFromSurfaceFraction(new S2Cell(new S2CellId(hash)).exactArea() / (4 * Math.PI)); } @Override diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcDggrs.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcDggrs.java index f38c56d143..9485691c7f 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcDggrs.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcDggrs.java @@ -79,7 +79,7 @@ public final class SmcDggrs extends DiscreteGlobalGridReferenceSystem { static final double TOLERANCE = 1e-9; private static final ZonalReferenceSystem ZRS = new DefaultZonalReferenceSystem("default", - "Level, then column and row of the cell south-west corner in units of the level.", true); + "Level, then column and row of the cell south-west corner in units of the level."); /** * Longitude and latitude in degrees of the domain south-west corner, in lattice coordinates. diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcDggs.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcDggs.java index 2c0ac5274e..73a7e73e14 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcDggs.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcDggs.java @@ -25,6 +25,7 @@ import org.opengis.referencing.operation.TransformException; import org.opengis.util.FactoryException; import org.apache.sis.geometry.DirectPosition2D; import org.apache.sis.measure.Longitude; +import org.apache.sis.measure.Units; import org.apache.sis.referencing.CRS; import org.apache.sis.referencing.CommonCRS; import org.apache.sis.referencing.crs.DefaultGeographicCRS; @@ -78,7 +79,7 @@ final class SmcDggs extends AbstractDiscreteGlobalGridSystem { * Returns the radius of the sphere having the surface of the celestial body. */ double radius() { - return Math.sqrt(getCelestialBodySurface() / (4 * Math.PI)); + return Math.sqrt(getCelestialBodySurface().to(Units.SQUARE_METRE).getValue().doubleValue() / (4 * Math.PI)); } /** diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcZone.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcZone.java index 8644abfaa2..66f5bbb741 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcZone.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/dggs/smc/SmcZone.java @@ -19,7 +19,10 @@ package org.apache.sis.referencing.dggs.smc; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; +import java.util.Optional; import java.util.Set; +import javax.measure.Quantity; +import javax.measure.quantity.Area; import org.opengis.geometry.DirectPosition; import org.opengis.geometry.Envelope; import org.opengis.metadata.extent.BoundingPolygon; @@ -146,10 +149,9 @@ final class SmcZone extends AbstractZone<SmcDggrs> { * Returns the cell area on the sphere having the celestial body surface. */ @Override - public Double getAreaMetersSquare() { - final double radius = dggrs.dggs.radius(); - final double width = Math.toRadians(east() - west()); - return radius * radius * width * Math.abs(Math.sin(Math.toRadians(north())) - Math.sin(Math.toRadians(south()))); + public Optional<Quantity<Area>> getArea() { + final double width = Math.toRadians(east() - west()); + return areaFromSurfaceFraction(width * Math.abs(Math.sin(Math.toRadians(north())) - Math.sin(Math.toRadians(south()))) / (4 * Math.PI)); } @Override diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/Code.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/Code.java index e6656c840a..d7142c4936 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/Code.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/Code.java @@ -38,7 +38,13 @@ import org.apache.sis.util.ArraysExt; /** * Holds the ordinates for geometry/area/zone/point within some reference system. * -+ * synonym : ISO-19170 : 8.2.4.3. Cell address + * <ul> + * <li>A coordinate reference system component has one ordinate per axis, as a {@link Number}.</li> + * <li>A referencing by identifiers component has one ordinate, the identifier. + * Identifiers of discrete global grids may be in any type accepted by their hierarchy.</li> + * </ul> + * + * Synonym: ISO-19170 : 8.2.4.3. Cell address * * @author Johann Sorel (Geomatys) */ diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/internal/shared/CodeOperations.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/internal/shared/CodeOperations.java index 382aee6ac1..4d534ce0f6 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/internal/shared/CodeOperations.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/referencing/rs/internal/shared/CodeOperations.java @@ -18,6 +18,8 @@ package org.apache.sis.referencing.rs.internal.shared; import java.util.Arrays; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.opengis.geometry.DirectPosition; import org.opengis.referencing.ReferenceSystem; import org.opengis.referencing.crs.CoordinateReferenceSystem; @@ -27,11 +29,15 @@ import org.opengis.referencing.operation.MathTransform; import org.opengis.referencing.operation.NoninvertibleTransformException; import org.opengis.referencing.operation.TransformException; import org.opengis.util.FactoryException; +import org.apache.sis.geometry.GeneralDirectPosition; +import org.apache.sis.referencing.CRS; import org.apache.sis.referencing.NamedIdentifier; +import org.apache.sis.referencing.dggs.DiscreteGlobalGridReferenceSystem; import org.apache.sis.referencing.gazetteer.ReferencingByIdentifiers; import org.apache.sis.referencing.rs.Code; import org.apache.sis.referencing.rs.CodeOperation; import org.apache.sis.referencing.rs.ReferenceSystems; +import org.apache.sis.util.Utilities; import org.apache.sis.util.iso.Names; // Specific to the geoapi-4.0 branch: @@ -248,6 +254,11 @@ public final class CodeOperations { private final ReferencingByIdentifiers.Coder coder; private final CoordinateReferenceSystem leaningCrs; + /** + * Transforms from the CRS of the decoded positions to the leaning CRS, created when first needed. + */ + private final Map<CoordinateReferenceSystem, MathTransform> toLeaningTransforms = new ConcurrentHashMap<>(); + public RbiToCrs(ReferencingByIdentifiers.Coder coder, CoordinateReferenceSystem leaningCrs) { this.coder = coder; this.leaningCrs = leaningCrs; @@ -272,14 +283,44 @@ public final class CodeOperations { return new CrsToRbi(leaningCrs, coder); } + /** + * Decodes the identifier and returns the location position, as one ordinate per axis of the leaning CRS. + * Identifiers of discrete global grids may be in any type accepted by their hierarchy. + */ @Override public Code transform(Code source, Code target) throws TransformException { - final Location location = coder.decode((CharSequence) source.getOrdinate(0)); - if (target == null) target = new Code(leaningCrs, new Object[1]); - target.setOrdinate(0, location.getPosition()); + final Object identifier = source.getOrdinate(0); + final Location location = (coder instanceof DiscreteGlobalGridReferenceSystem.Coder dggs) + ? dggs.decode(identifier) : coder.decode(identifier.toString()); + final double[] coordinates = toLeaningCrs(location.getPosition()); + if (target == null) target = new Code(leaningCrs); + for (int i = 0; i < coordinates.length; i++) { + target.setOrdinate(i, coordinates[i]); + } return target; } + /** + * Returns the coordinates of the given position in the leaning CRS. + * Positions may be in another CRS, for example UTM zones for the Military Grid Reference System. + */ + private double[] toLeaningCrs(final DirectPosition position) throws TransformException { + final CoordinateReferenceSystem crs = position.getCoordinateReferenceSystem(); + if (crs == null || Utilities.equalsIgnoreMetadata(crs, leaningCrs)) { + return position.getCoordinates(); + } + MathTransform toLeaning = toLeaningTransforms.get(crs); + if (toLeaning == null) { + try { + toLeaning = CRS.findOperation(crs, leaningCrs, null).getMathTransform(); + } catch (FactoryException ex) { + throw new TransformException(ex.getMessage(), ex); + } + toLeaningTransforms.put(crs, toLeaning); + } + return toLeaning.transform(position, null).getCoordinates(); + } + @Override public void transform(Code[] source, int soffset, Code[] target, int toffset, int nb) throws TransformException { for (int i = 0; i < nb; i++) { @@ -331,11 +372,22 @@ public final class CodeOperations { return new CrsToCrs(targetCrs, sourceCrs, transform.inverse()); } + /** + * Transforms the coordinates, given as one ordinate per axis. + */ @Override public Code transform(Code source, Code target) throws TransformException { - final DirectPosition dp = transform.transform((DirectPosition) source.getOrdinate(0), null); - if (target == null) target = new Code(getTargetRS(), new Object[1]); - target.setOrdinate(0, dp); + final int sourceDimension = transform.getSourceDimensions(); + final int targetDimension = transform.getTargetDimensions(); + final double[] coordinates = new double[Math.max(sourceDimension, targetDimension)]; + for (int i = 0; i < sourceDimension; i++) { + coordinates[i] = ((Number) source.getOrdinate(i)).doubleValue(); + } + transform.transform(coordinates, 0, coordinates, 0, 1); + if (target == null) target = new Code(targetCrs); + for (int i = 0; i < targetDimension; i++) { + target.setOrdinate(i, coordinates[i]); + } return target; } @@ -388,11 +440,20 @@ public final class CodeOperations { return new RbiToCrs(coder, leaningCrs); } + /** + * Encodes the coordinates, given as one ordinate per axis of the leaning CRS. + * Discrete global grids give zone identifiers in their native type, other systems give text. + */ @Override public Code transform(Code source, Code target) throws TransformException { - final String code = coder.encode((DirectPosition) source.getOrdinate(0)); - if (target == null) target = new Code(coder.getReferenceSystem(), new Object[1]); - target.setOrdinate(0, code); + final GeneralDirectPosition position = new GeneralDirectPosition(leaningCrs); + for (int i = 0; i < position.getDimension(); i++) { + position.setCoordinate(i, ((Number) source.getOrdinate(i)).doubleValue()); + } + final Object identifier = (coder instanceof DiscreteGlobalGridReferenceSystem.Coder dggs) + ? dggs.encodeIdentifier(position) : coder.encode(position); + if (target == null) target = new Code(coder.getReferenceSystem()); + target.setOrdinate(0, identifier); return target; } diff --git a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessor.java b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessor.java index e87d168991..fb0265cb45 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessor.java +++ b/incubator/src/org.apache.sis.referencing.dggs/main/org/apache/sis/storage/dggs/DiscreteGlobalGridCoverageProcessor.java @@ -626,7 +626,6 @@ public final class DiscreteGlobalGridCoverageProcessor { } final DiscreteGlobalGridReferenceSystem dggrs = dggGrid.getReferenceSystem(); - final DiscreteGlobalGridHierarchy dggh = dggrs.getGridSystem().getHierarchy(); final List<SampleDimension> sampleDimensions = CodedResources.selectBands(coverage.getSampleDimensions(), range); final CodeOperation op = ReferenceSystems.findOperation(dggrs, coverage.getGeometry().getReferenceSystem(), null); @@ -640,9 +639,6 @@ public final class DiscreteGlobalGridCoverageProcessor { while (iterator.next()) { final int[] gridPosition = iterator.getPosition(); final Code code = toCode.toCode(gridPosition); - if (!(code.getOrdinate(0) instanceof CharSequence)) { - code.setOrdinate(0, dggh.toTextIdentifier(code.getOrdinate(0))); - } tcode = op.transform(code, tcode); final double[] values = eva.apply(tcode); if (values != null) { diff --git a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/ZoneTest.java b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/ZoneTest.java new file mode 100644 index 0000000000..a2883a842c --- /dev/null +++ b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/ZoneTest.java @@ -0,0 +1,108 @@ +/* + * 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.referencing.dggs; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; +import java.net.URI; +import java.util.List; +import java.util.Map; +import org.opengis.referencing.gazetteer.LocationType; +import org.apache.sis.referencing.dggs.s2.S2Dggrs; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + + +/** + * Tests the default methods of {@link Zone}. + * + * @author Johann Sorel (Geomatys) + */ +public class ZoneTest { + + /** + * A reference system whose hierarchy has no long identifiers. + */ + private static final class TextOnlyDggrs extends DiscreteGlobalGridReferenceSystem { + TextOnlyDggrs() { + super(Map.of(NAME_KEY, "text only"), new LocationType[0]); + } + + @Override public List<String> getKeywords() {return List.of();} + @Override public URI getUri() {return null;} + @Override public ZonalReferenceSystem getZonalSystem() {return null;} + @Override public SubZoneOrder getSubZoneOrder() {return null;} + @Override public Coder createCoder() {return null;} + + @Override + public DiscreteGlobalGridSystem getGridSystem() { + final DiscreteGlobalGridHierarchy hierarchy = proxy(DiscreteGlobalGridHierarchy.class, (p, method, args) -> + method.getName().equals("supportLongIdentifiers") ? Boolean.FALSE : null); + return proxy(DiscreteGlobalGridSystem.class, (p, method, args) -> + method.getName().equals("getHierarchy") ? hierarchy : null); + } + } + + /** + * Creates an implementation of the given interface which delegates to the given handler. + */ + private static <T> T proxy(final Class<T> type, final InvocationHandler handler) { + return type.cast(Proxy.newProxyInstance(ZoneTest.class.getClassLoader(), new Class<?>[] {type}, handler)); + } + + /** + * Creates a zone at level 0 with the given text identifier, which has no long identifier. + */ + private static Zone textZone(final DiscreteGlobalGridReferenceSystem dggrs, final String identifier) { + return proxy(Zone.class, (zone, method, args) -> { + switch (method.getName()) { + case "getLocationType": return new RefinementLevel(dggrs, 0); + case "getTextIdentifier": return identifier; + case "getLongIdentifier": throw new UnsupportedOperationException(); + default: return method.isDefault() ? InvocationHandler.invokeDefault(zone, method, args) : null; + } + }); + } + + /** + * Tests {@link Zone#compareTo(Zone)} on zones without long identifiers. + * The zones shall be sorted by their text identifiers. + */ + @Test + public void testCompareToWithTextIdentifiers() { + final DiscreteGlobalGridReferenceSystem dggrs = new TextOnlyDggrs(); + final Zone a = textZone(dggrs, "a"); + final Zone b = textZone(dggrs, "b"); + assertTrue(a.compareTo(b) < 0); + assertTrue(b.compareTo(a) > 0); + assertEquals(0, a.compareTo(textZone(dggrs, "a"))); + } + + /** + * Tests {@link Zone#compareTo(Zone)} on zones of different levels: the coarsest level comes first. + */ + @Test + public void testCompareToLevels() throws Exception { + final DiscreteGlobalGridReferenceSystem dggrs = new S2Dggrs(); + final Zone face = dggrs.getGridSystem().getHierarchy().getGrids().get(0).getZones().findFirst().orElseThrow(); + final Zone child = face.getChildren().iterator().next(); + assertTrue(face.compareTo(child) < 0); + assertTrue(child.compareTo(face) > 0); + } +} diff --git a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/internal/shared/AbstractZoneTest.java b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/internal/shared/AbstractZoneTest.java index 961ba844fc..dd6ec09526 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/internal/shared/AbstractZoneTest.java +++ b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/internal/shared/AbstractZoneTest.java @@ -17,7 +17,10 @@ package org.apache.sis.referencing.dggs.internal.shared; import java.util.List; +import javax.measure.Quantity; +import javax.measure.quantity.Area; import org.opengis.referencing.operation.TransformException; +import org.apache.sis.measure.Units; import org.apache.sis.referencing.dggs.DiscreteGlobalGridReferenceSystem; import org.apache.sis.referencing.dggs.Zone; import org.apache.sis.referencing.dggs.s2.S2Dggrs; @@ -50,6 +53,30 @@ public class AbstractZoneTest { return zone.getChildren().iterator().next(); } + /** + * Tests the surface of the celestial body: Earth surface is about 5.1×10¹⁴ m². + */ + @Test + public void testCelestialBodySurface() { + final Quantity<Area> surface = dggrs.getGridSystem().getCelestialBodySurface(); + assertEquals(Units.SQUARE_METRE, surface.getUnit()); + assertEquals(5.1E14, surface.getValue().doubleValue(), 0.005 * 5.1E14); + } + + /** + * Tests the measures of a zone. The six S2 faces have the same area, a sixth of the celestial body surface. + */ + @Test + public void testMeasures() throws Exception { + final double surface = dggrs.getGridSystem().getCelestialBodySurface().getValue().doubleValue(); + final Zone face = faces().get(0); + final Quantity<Area> area = face.getArea().orElseThrow(); + assertEquals(Units.SQUARE_METRE, area.getUnit()); + assertEquals(surface / 6, area.getValue().doubleValue(), surface * 1E-9); + assertTrue(face.getVolume().isEmpty()); + assertTrue(face.getDuration().isEmpty()); + } + /** * Tests {@link AbstractZone#isAncestorOf(Object, int)} and {@link AbstractZone#isDescendantOf(Object, int)} * with a maximum depth equal to or greater than the actual depth. diff --git a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/smc/SmcDggrsTest.java b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/smc/SmcDggrsTest.java index f5351d6bfc..2b9527321f 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/smc/SmcDggrsTest.java +++ b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/smc/SmcDggrsTest.java @@ -83,9 +83,10 @@ public class SmcDggrsTest { double area = 0; for (SmcZone child : zone.getChildren()) { assertEquals(zone, child.getFirstParent()); - area += child.getAreaMetersSquare(); + area += child.getArea().orElseThrow().getValue().doubleValue(); } - assertEquals(zone.getAreaMetersSquare(), area, 1e-12 * zone.getAreaMetersSquare(), zone::toString); + final double expected = zone.getArea().orElseThrow().getValue().doubleValue(); + assertEquals(expected, area, 1e-12 * expected, zone::toString); } } } diff --git a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/smc/SmcTest.java b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/smc/SmcTest.java index bada4012f3..310de88a71 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/smc/SmcTest.java +++ b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/dggs/smc/SmcTest.java @@ -146,9 +146,10 @@ public class SmcTest extends AbstractDggrsTest { assertEquals(zone, child.getFirstParent()); assertTrue(child.south() >= zone.south() && child.north() <= zone.north(), child::toString); assertTrue(child.west() >= zone.west() && child.east() <= zone.east(), child::toString); - area += child.getAreaMetersSquare(); + area += child.getArea().orElseThrow().getValue().doubleValue(); } - assertEquals(zone.getAreaMetersSquare(), area, 1e-12 * zone.getAreaMetersSquare(), zone::toString); + final double expected = zone.getArea().orElseThrow().getValue().doubleValue(); + assertEquals(expected, area, 1e-12 * expected, zone::toString); } } diff --git a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/rs/CodeOperationTest.java b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/rs/CodeOperationTest.java index cbf29ff0fb..642747d565 100644 --- a/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/rs/CodeOperationTest.java +++ b/incubator/src/org.apache.sis.referencing.dggs/test/org/apache/sis/referencing/rs/CodeOperationTest.java @@ -25,8 +25,12 @@ import org.opengis.referencing.operation.NoninvertibleTransformException; import org.opengis.referencing.operation.OperationMethod; import org.opengis.referencing.operation.TransformException; import org.opengis.util.FactoryException; +import org.apache.sis.geometry.DirectPosition2D; import org.apache.sis.referencing.CRS; import org.apache.sis.referencing.CommonCRS; +import org.apache.sis.referencing.dggs.DiscreteGlobalGridReferenceSystem; +import org.apache.sis.referencing.dggs.Zone; +import org.apache.sis.referencing.dggs.s2.S2Dggrs; import org.apache.sis.referencing.operation.DefaultConversion; import org.apache.sis.referencing.operation.transform.DefaultMathTransformFactory; import org.apache.sis.referencing.operation.transform.MathTransforms; @@ -328,4 +332,65 @@ public class CodeOperationTest { final Code code = inverse.transform(new Code(target, 1000.0, 10.0, 20.0), null); assertArrayEquals(new Object[]{10.0, 20.0, 1000.0}, code.getOrdinates()); } + + /** + * Tests an operation between two CRS, with one ordinate per axis. + */ + @Test + public void crsToCrsPerAxis() throws FactoryException, TransformException { + final CodeOperation operation = ReferenceSystems.findOperation( + CommonCRS.WGS84.geographic(), CommonCRS.WGS84.normalizedGeographic(), null); + final Code code = operation.transform(new Code(CommonCRS.WGS84.geographic(), 10.0, 20.0), null); + assertArrayEquals(new Object[]{20.0, 10.0}, code.getOrdinates()); + } + + /** + * Tests the conversion of a MGRS code to the leaning CRS, with one ordinate per axis. + * The expected position is the center of the geohash {@code 87z9y8fhdbff}. + */ + @Test + public void rbiToCrsPerAxis() throws FactoryException, TransformException { + final CodeOperation operation = ReferenceSystems.findOperation(MGRS, CommonCRS.WGS84.normalizedGeographic(), null); + final Code code = operation.transform(new Code(MGRS, "4Q FJ 12 67"), null); + assertEquals(2, code.getDimension()); + assertEquals(-157.914612, (Double) code.getOrdinate(0), 1E-5, "Longitude"); + assertEquals( 21.406264, (Double) code.getOrdinate(1), 1E-5, "Latitude"); + } + + /** + * Tests transforming a code with a referencing by identifiers and a temporal component. + */ + @Test + public void transformCompoundPerAxis() throws FactoryException, TransformException { + final ReferenceSystem source = ReferenceSystems.createCompound(MGRS, TIME_JAVA); + final ReferenceSystem target = ReferenceSystems.createCompound(GEOHASH, TIME_JULIAN); + final Code code = ReferenceSystems.findOperation(source, target, null).transform(new Code(source, "4Q FJ 12 67", 1000.0), null); + assertEquals("87z9y8fhdbff", code.getOrdinate(0)); + assertEquals(2440587.5 + 1000.0 / 86400000, (Double) code.getOrdinate(1), 1E-9, "Julian day of 1 second after 1970"); + } + + /** + * Tests the conversion of a zone identifier in its native type, without conversion to text. + */ + @Test + public void dggrsToCrsWithNativeIdentifier() throws Exception { + final DiscreteGlobalGridReferenceSystem dggrs = new S2Dggrs(); + final Zone zone = dggrs.getGridSystem().getHierarchy().getGrids().get(3).getZones().findFirst().orElseThrow(); + final CodeOperation operation = ReferenceSystems.findOperation(dggrs, CommonCRS.WGS84.normalizedGeographic(), null); + final Code code = operation.transform(new Code(dggrs, zone.getIdentifier()), null); + assertEquals(zone.getPosition().getCoordinate(0), (Double) code.getOrdinate(0), 1E-9); + assertEquals(zone.getPosition().getCoordinate(1), (Double) code.getOrdinate(1), 1E-9); + } + + /** + * Tests that converting a position to a DGGRS gives the zone identifier in its native type. + */ + @Test + public void crsToDggrsWithNativeIdentifier() throws Exception { + final DiscreteGlobalGridReferenceSystem dggrs = new S2Dggrs(); + final CodeOperation operation = ReferenceSystems.findOperation(CommonCRS.WGS84.normalizedGeographic(), dggrs, null); + final Code code = operation.transform(new Code(CommonCRS.WGS84.normalizedGeographic(), 15.0, 30.0), null); + final Object expected = dggrs.createCoder().encodeIdentifier(new DirectPosition2D(CommonCRS.WGS84.normalizedGeographic(), 15, 30)); + assertEquals(expected, code.getOrdinate(0)); + } }
