This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 72c433cb3a5401f535e70d91330c27f0d71e0903 Author: Martin Desruisseaux <[email protected]> AuthorDate: Tue May 24 18:46:08 2022 +0200 Add a `GridExtent.setRange(…)` method. Change the order of some other methods for grouping related methods. --- .../org/apache/sis/coverage/grid/GridExtent.java | 179 +++++++++++++-------- .../apache/sis/coverage/grid/GridExtentTest.java | 58 ++++--- 2 files changed, 145 insertions(+), 92 deletions(-) diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java index 9064f91fdd..cf38ad7140 100644 --- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java +++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridExtent.java @@ -88,7 +88,7 @@ import org.opengis.coverage.PointOutsideCoverageException; * * @author Martin Desruisseaux (IRD, Geomatys) * @author Alexis Manin (Geomatys) - * @version 1.2 + * @version 1.3 * @since 1.0 * @module */ @@ -126,7 +126,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable /** * A pool of shared {@link DimensionNameType} arrays. We use a pool - * because a small amount of arrays is shared by most grid extent. + * because a small amount of arrays is shared by most grid extents. */ private static final WeakValueHashMap<DimensionNameType[],DimensionNameType[]> POOL = new WeakValueHashMap<>(DimensionNameType[].class); @@ -221,9 +221,9 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable } /** - * Creates an initially empty grid envelope with the given number of dimensions. + * Creates an initially empty grid extent with the given number of dimensions. * All grid coordinate values are initialized to zero. This constructor is private - * since {@code GridExtent} coordinate values can not be modified by public API. + * because {@code GridExtent} coordinate values can not be modified by public API. * * @param dimension number of dimensions. * @param axisTypes the axis types, or {@code null} if unspecified. @@ -290,7 +290,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable } /** - * Constructs a new grid envelope set to the specified coordinates. + * Constructs a new grid extent set to the specified coordinates. * The given arrays contain a minimum (inclusive) and maximum value for each dimension of the grid coverage. * The lowest valid grid coordinates are often zero, but this is not mandatory. * As a convenience for this common case, a null {@code low} array means that all low coordinates are zero. @@ -578,7 +578,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable * Creates a copy of the given grid extent. The {@link #coordinates} array is cloned * while the {@link #types} array is shared between the two instances. This constructor * is reserved to methods that modify the coordinates after construction. It must be - * private since we do not allow coordinates modifications by public API. + * private because we do not allow coordinates modifications by public API. * * @see #GridExtent(int, DimensionNameType[]) */ @@ -588,9 +588,9 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable } /** - * Creates a new grid envelope as a copy of the given one. + * Creates a new grid extent as a copy of the given one. * - * @param extent the grid envelope to copy. + * @param extent the grid extent to copy. * @throws IllegalArgumentException if a coordinate value in the low part is * greater than the corresponding coordinate value in the high part. * @@ -609,12 +609,12 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable } /** - * Returns the given grid envelope as a {@code GridExtent} implementation. + * Returns the given grid extent as a {@code GridExtent} implementation. * If the given extent is already a {@code GridExtent} instance or is null, then it is returned as-is. * Otherwise a new extent is created using the {@linkplain #GridExtent(GridEnvelope) copy constructor}. * - * @param extent the grid envelope to cast or copy, or {@code null}. - * @return the grid envelope as a {@code GridExtent}, or {@code null} if the given extent was null. + * @param extent the grid extent to cast or copy, or {@code null}. + * @return the grid extent as a {@code GridExtent}, or {@code null} if the given extent was null. */ public static GridExtent castOrCopy(final GridEnvelope extent) { if (extent == null || extent instanceof GridExtent) { @@ -686,6 +686,8 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable * The sequence contains a minimum value for each dimension of the grid coverage. * * @return the valid minimum grid coordinates, inclusive. + * + * @see #getLow(int) */ @Override public GridCoordinates getLow() { @@ -697,6 +699,8 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable * The sequence contains a maximum value for each dimension of the grid coverage. * * @return the valid maximum grid coordinates, <strong>inclusive</strong>. + * + * @see #getHigh(int) */ @Override public GridCoordinates getHigh() { @@ -713,6 +717,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable * * @see #getLow() * @see #getHigh(int) + * @see #setRange(int, long, long) */ @Override public long getLow(final int index) { @@ -730,6 +735,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable * * @see #getHigh() * @see #getLow(int) + * @see #setRange(int, long, long) */ @Override public long getHigh(final int index) { @@ -750,6 +756,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable * * @see #getLow(int) * @see #getHigh(int) + * @see #resize(long...) */ @Override public long getSize(final int index) { @@ -920,6 +927,37 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable return indexShown; } + /** + * Returns a grid extent identical to this grid extent except for the coordinate values in the specified dimension. + * + * @param index the dimension for which to set the coordinate values. + * @param low the low coordinate value at the given dimension, inclusive. + * @param high the high coordinate value at the given dimension, <strong>inclusive</strong>. + * @return a grid extent with the specified coordinate values, or {@code this} if values are unchanged. + * @throws IllegalArgumentException if the low coordinate value is greater than the high coordinate value. + * + * @see #getLow(int) + * @see #getHigh(int) + * + * @since 1.3 + */ + public GridExtent setRange(final int index, final long low, final long high) { + int ih = getDimension(); + ArgumentChecks.ensureValidIndex(ih, index); + ih += index; + if (coordinates[index] == low && coordinates[ih] == high) { + return this; + } + if (low > high) { + throw new IllegalArgumentException(Resources.format( + Resources.Keys.IllegalGridEnvelope_3, getAxisIdentification(index, index), low, high)); + } + final GridExtent copy = new GridExtent(this); + copy.coordinates[index] = low; + copy.coordinates[ih] = high; + return copy; + } + /** * Transforms this grid extent to a "real world" envelope using the given transform. * The transform shall map <em>cell corner</em> to real world coordinates. @@ -1055,7 +1093,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable } /** - * Returns a new grid envelope with the specified dimension inserted at the given index in this grid envelope. + * Returns a new grid extent with the specified dimension inserted at the given index in this grid extent. * To append a new dimension after all existing dimensions, set {@code offset} to {@link #getDimension()}. * * @param offset where to insert the new dimension, from 0 to {@link #getDimension()} inclusive. @@ -1065,7 +1103,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable * @param isHighIncluded {@code true} if the {@code high} value is inclusive (as in ISO 19123 specification), * or {@code false} if it is exclusive (as in Java2D usage). * This argument does not apply to {@code low} value, which is always inclusive. - * @return a new grid envelope with the specified dimension added. + * @return a new grid extent with the specified dimension added. * @throws IllegalArgumentException if the low coordinate value is greater than the high coordinate value. * * @since 1.1 @@ -1097,6 +1135,30 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable return ex; } + /** + * Returns a grid extent that encompass only some dimensions of this grid extent. + * This method copies the specified dimensions of this grid extent into a new grid extent. + * The given dimensions must be in strictly ascending order without duplicated values. + * The number of dimensions of the sub grid extent will be {@code dimensions.length}. + * + * <p>This method performs a <cite>dimensionality reduction</cite> and can be used as the + * converse of {@link #insertDimension(int, DimensionNameType, long, long, boolean)}. + * This method can not be used for changing dimension order.</p> + * + * @param dimensions the dimensions to select, in strictly increasing order. + * @return the sub-envelope, or {@code this} if the given array contains all dimensions of this grid extent. + * @throws IndexOutOfBoundsException if an index is out of bounds. + * + * @see #getSubspaceDimensions(int) + * @see GridGeometry#reduce(int...) + * + * @since 1.1 + */ + public GridExtent reduceDimension(int... dimensions) { + dimensions = verifyDimensions(dimensions, getDimension()); + return (dimensions != null) ? reorder(dimensions) : this; + } + /** * Verifies the validity of a given {@code dimensions} argument. * @@ -1122,6 +1184,29 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.IndexOutOfBounds_1, d)); } + /** + * Changes axis order or reduces the number of dimensions. + * It is caller responsibility to ensure that the given dimensions are valid. + */ + final GridExtent reorder(final int[] dimensions) { + final int sd = getDimension(); + final int td = dimensions.length; + DimensionNameType[] tt = null; + if (types != null) { + tt = new DimensionNameType[td]; + for (int i=0; i<td; i++) { + tt[i] = types[dimensions[i]]; + } + } + final GridExtent sub = new GridExtent(td, tt); + for (int i=0; i<td; i++) { + final int j = dimensions[i]; + sub.coordinates[i] = coordinates[j]; + sub.coordinates[i+td] = coordinates[j+sd]; + } + return sub; + } + /** * Returns a grid extent expanded by the given amount of cells on both sides along each dimension. * This method adds the given margins to the {@linkplain #getHigh(int) high coordinates} @@ -1208,6 +1293,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable * @return a grid extent having the given sizes, or {@code this} if there is no change. * @throws ArithmeticException if resizing this extent to the given size overflows {@code long} capacity. * + * @see #getSize(int) * @see GridDerivation#subgrid(GridExtent, int...) */ public GridExtent resize(final long... sizes) { @@ -1238,53 +1324,6 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable return Arrays.equals(c, coordinates) ? this : resize; } - /** - * Returns a grid extent that encompass only some dimensions of this grid extent. - * This method copies the specified dimensions of this grid extent into a new grid extent. - * The given dimensions must be in strictly ascending order without duplicated values. - * The number of dimensions of the sub grid envelope will be {@code dimensions.length}. - * - * <p>This method performs a <cite>dimensionality reduction</cite> and can be used as the - * converse of {@link #insertDimension(int, DimensionNameType, long, long, boolean)}. - * This method can not be used for changing dimension order.</p> - * - * @param dimensions the dimensions to select, in strictly increasing order. - * @return the sub-envelope, or {@code this} if the given array contains all dimensions of this grid extent. - * @throws IndexOutOfBoundsException if an index is out of bounds. - * - * @see #getSubspaceDimensions(int) - * @see GridGeometry#reduce(int...) - * - * @since 1.1 - */ - public GridExtent reduceDimension(int... dimensions) { - dimensions = verifyDimensions(dimensions, getDimension()); - return (dimensions != null) ? reorder(dimensions) : this; - } - - /** - * Changes axis order or reduces the number of dimensions. - * It is caller responsibility to ensure that the given dimensions are valid. - */ - final GridExtent reorder(final int[] dimensions) { - final int sd = getDimension(); - final int td = dimensions.length; - DimensionNameType[] tt = null; - if (types != null) { - tt = new DimensionNameType[td]; - for (int i=0; i<td; i++) { - tt[i] = types[dimensions[i]]; - } - } - final GridExtent sub = new GridExtent(td, tt); - for (int i=0; i<td; i++) { - final int j = dimensions[i]; - sub.coordinates[i] = coordinates[j]; - sub.coordinates[i+td] = coordinates[j+sd]; - } - return sub; - } - /** * Creates a new grid extent subsampled by the given amount of cells along each grid dimensions. * This method divides {@linkplain #getLow(int) low coordinates} and {@linkplain #getSize(int) grid sizes} @@ -1535,10 +1574,10 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable } /** - * Returns a hash value for this grid envelope. This value needs not to remain + * Returns a hash value for this grid extent. This value needs not to remain * consistent between different implementations of the same class. * - * @return a hash value for this grid envelope. + * @return a hash value for this grid extent. */ @Override public int hashCode() { @@ -1546,11 +1585,11 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable } /** - * Compares the specified object with this grid envelope for equality. + * Compares the specified object with this grid extent for equality. * This method delegates to {@code equals(object, ComparisonMode.STRICT)}. * - * @param object the object to compare with this grid envelope for equality. - * @return {@code true} if the given object is equal to this grid envelope. + * @param object the object to compare with this grid extent for equality. + * @return {@code true} if the given object is equal to this grid extent. */ @Override public final boolean equals(final Object object) { @@ -1558,13 +1597,13 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable } /** - * Compares the specified object with this grid envelope for equality. + * Compares the specified object with this grid extent for equality. * If the mode is {@link ComparisonMode#IGNORE_METADATA} or more flexible, * then the {@linkplain #getAxisType(int) axis types} are ignored. * - * @param object the object to compare with this grid envelope for equality. + * @param object the object to compare with this grid extent for equality. * @param mode the strictness level of the comparison. - * @return {@code true} if the given object is equal to this grid envelope. + * @return {@code true} if the given object is equal to this grid extent. * * @since 1.1 */ @@ -1588,7 +1627,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable } /** - * Returns a string representation of this grid envelope. The returned string + * Returns a string representation of this grid extent. The returned string * is implementation dependent and is provided for debugging purposes only. */ @Override @@ -1603,7 +1642,7 @@ public class GridExtent implements GridEnvelope, LenientComparable, Serializable } /** - * Writes a string representation of this grid envelope in the given buffer. + * Writes a string representation of this grid extent in the given buffer. * This method is provided for allowing caller to recycle the same buffer. * * @param out where to write the string representation. diff --git a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java index b5fd1c0511..169740150b 100644 --- a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java +++ b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java @@ -45,7 +45,7 @@ import static org.apache.sis.test.ReferencingAssert.*; * * @author Martin Desruisseaux (IRD, Geomatys) * @author Alexis Manin (Geomatys) - * @version 1.2 + * @version 1.3 * @since 1.0 * @module */ @@ -137,12 +137,13 @@ public final strictfp class GridExtentTest extends TestCase { * with {@code offset} set to {@link GridExtent#getDimension()}. */ @Test - public void testAppend() { + public void testAppendDimension() { appendOrInsert(2, 1); } /** - * Tests {@link GridExtent#insertDimension(int, DimensionNameType, long, long, boolean)}. + * Tests {@link GridExtent#insertDimension(int, DimensionNameType, long, long, boolean)} + * with {@code offset} somewhere in the middle of the extent. */ @Test public void testInsertDimension() { @@ -165,6 +166,38 @@ public final strictfp class GridExtentTest extends TestCase { assertEquals(DimensionNameType.TIME, extent.getAxisType(offset).get()); } + /** + * Tests {@link GridExtent#reduceDimension(int[])}. + */ + @Test + public void testReduceDimension() { + final GridExtent extent = create3D(); + GridExtent reduced = extent.reduceDimension(0, 1); + assertEquals("dimension", 2, reduced.getDimension()); + assertExtentEquals(reduced, 0, 100, 499); + assertExtentEquals(reduced, 1, 200, 799); + assertEquals(DimensionNameType.COLUMN, reduced.getAxisType(0).get()); + assertEquals(DimensionNameType.ROW, reduced.getAxisType(1).get()); + + reduced = extent.reduceDimension(2); + assertEquals("dimension", 1, reduced.getDimension()); + assertExtentEquals(reduced, 0, 40, 49); + assertEquals(DimensionNameType.TIME, reduced.getAxisType(0).get()); + } + + /** + * Tests {@link GridExtent#setRange(int, long, long)}. + */ + @Test + public void testSetRange() { + GridExtent extent = create3D(); + assertSame(extent, extent.setRange(1, 200, 799)); + extent = extent.setRange(2, 30, 60); + assertExtentEquals(extent, 0, 100, 499); + assertExtentEquals(extent, 1, 200, 799); + assertExtentEquals(extent, 2, 30, 60); + } + /** * Tests {@link GridExtent#expand(long...)}. */ @@ -228,25 +261,6 @@ public final strictfp class GridExtentTest extends TestCase { assertSame(extent.intersect(domain), extent); } - /** - * Tests {@link GridExtent#reduceDimension(int[])}. - */ - @Test - public void testReduceDimension() { - final GridExtent extent = create3D(); - GridExtent reduced = extent.reduceDimension(0, 1); - assertEquals("dimension", 2, reduced.getDimension()); - assertExtentEquals(reduced, 0, 100, 499); - assertExtentEquals(reduced, 1, 200, 799); - assertEquals(DimensionNameType.COLUMN, reduced.getAxisType(0).get()); - assertEquals(DimensionNameType.ROW, reduced.getAxisType(1).get()); - - reduced = extent.reduceDimension(2); - assertEquals("dimension", 1, reduced.getDimension()); - assertExtentEquals(reduced, 0, 40, 49); - assertEquals(DimensionNameType.TIME, reduced.getAxisType(0).get()); - } - /** * Tests {@link GridExtent#slice(DirectPosition, int[])}. */
