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 2020/02/14 23:55:51 UTC
[sis] branch geoapi-4.0 updated (8a462de -> e900738)
This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a change to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git.
from 8a462de Port MosaicCalculator from Geotk (was named "RegionCalculator") for use by DatumShiftGridGroup, itself used by NTv2 reader when a NTv2 file contains more than one grid without common parent. The intent is to figure out how to layout the 114 sub-grids found in NTv2 file when the file provided limited information about the parent-child relationships, especially close to the root.
new 327b2df Refactor a bit DatumShiftGridGroup for computing sub-grid coordinates in a more direct way than using AffineTransform. This change allows us to fix a bug, where the translation that we got was not adjusted for the different size of the cell.
new e900738 Renamed MosaicCalculator as TileOrganizer.
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../apache/sis/internal/referencing/j2d/Tile.java | 151 +++++------------
.../{MosaicCalculator.java => TileOrganizer.java} | 15 +-
.../referencing/j2d/TranslatedTransform.java | 2 +-
.../referencing/provider/DatumShiftGridFile.java | 8 +-
.../referencing/provider/DatumShiftGridGroup.java | 183 ++++++++++++++-------
5 files changed, 168 insertions(+), 191 deletions(-)
rename core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/{MosaicCalculator.java => TileOrganizer.java} (97%)
[sis] 02/02: Renamed MosaicCalculator as TileOrganizer.
Posted by de...@apache.org.
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 e900738bdd7e816b198f8ef56f20e730b1c36596
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Sat Feb 15 00:49:05 2020 +0100
Renamed MosaicCalculator as TileOrganizer.
---
.../apache/sis/internal/referencing/j2d/Tile.java | 54 +++++++++++-----------
.../{MosaicCalculator.java => TileOrganizer.java} | 11 ++---
.../referencing/j2d/TranslatedTransform.java | 2 +-
.../referencing/provider/DatumShiftGridGroup.java | 10 ++--
4 files changed, 38 insertions(+), 39 deletions(-)
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java
index 847fb42..b2ec2ae 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java
@@ -92,11 +92,11 @@ import org.apache.sis.io.TableAppender;
* </ul>
*
* The tiles are not required to be arranged on a regular grid, but performances may be better if they are.
- * {@link MosaicCalculator} is responsible for analyzing the layout of a collection of tiles.
+ * {@link TileOrganizer} is responsible for analyzing the layout of a collection of tiles.
*
* <h2>Multi-threading</h2>
* This class is thread-safe. In addition {@code Tile} instances can be considered as immutable after construction.
- * However some properties may be available only after the tiles have been processed by a {@link MosaicCalculator},
+ * However some properties may be available only after the tiles have been processed by a {@link TileOrganizer},
* or only after {@link #fetchSize()} has been invoked.
*
* @author Martin Desruisseaux (Geomatys)
@@ -134,20 +134,20 @@ public class Tile implements Serializable {
* has not yet been computed.
*
* <p>This field should be considered as final. It is not final only because
- * {@link MosaicCalculator} may compute this value automatically.</p>
+ * {@link TileOrganizer} may compute this value automatically.</p>
*
* @see #getSubsampling()
*/
private int xSubsampling, ySubsampling;
/**
- * The "grid to real world" transform, used by {@link MosaicCalculator} in order to compute
+ * The "grid to real world" transform, used by {@link TileOrganizer} in order to compute
* the {@linkplain #getRegion() region} for this tile. This field is set to {@code null} when
- * {@link MosaicCalculator}'s work is in progress, and set to a new value on completion.
+ * {@link TileOrganizer}'s work is in progress, and set to a new value on completion.
*
- * <p><b>Note:</b> {@link MosaicCalculator} really needs a new instance for each tile.
- * No caching allowed before {@link MosaicCalculator} processing.
- * Caching is allowed <em>after</em> {@link MosaicCalculator} processing is completed.</p>
+ * <p><b>Note:</b> {@link TileOrganizer} really needs a new instance for each tile.
+ * No caching allowed before {@link TileOrganizer} processing.
+ * Caching is allowed <em>after</em> {@link TileOrganizer} processing is completed.</p>
*/
private AffineTransform gridToCRS;
@@ -195,16 +195,16 @@ public class Tile implements Serializable {
* Creates a tile for the given region and <cite>"grid to real world"</cite> transform.
* This constructor can be used when the {@linkplain #getLocation() location} of the tile is unknown.
* The location and subsampling will be computed automatically when this tile will be processed by a
- * {@link MosaicCalculator}.
+ * {@link TileOrganizer}.
*
* <p>When using this constructor, the {@link #getLocation()}, {@link #getRegion()} and
* {@link #getSubsampling()} methods will throw an {@link IllegalStateException} until
- * this tile has been processed by a {@link MosaicCalculator}, which will compute those
+ * this tile has been processed by a {@link TileOrganizer}, which will compute those
* values automatically.</p>
*
* @param region the tile region, or {@code null} if unknown.
* The (<var>x</var>,<var>y</var> location of this region is typically (0,0).
- * The final location will be computed when this tile will be given to a {@link MosaicCalculator}.
+ * The final location will be computed when this tile will be given to a {@link TileOrganizer}.
* @param gridToCRS the <cite>"grid to real world"</cite> transform mapping pixel
* {@linkplain PixelOrientation#UPPER_LEFT upper left} corner.
*/
@@ -221,7 +221,7 @@ public class Tile implements Serializable {
/**
* Creates a new tile for the given final transform.
- * This is used for storing {@link MosaicCalculator} results.
+ * This is used for storing {@link TileOrganizer} results.
*/
Tile(final AffineTransform gridToCRS, final Rectangle region) {
this.x = region.x;
@@ -234,7 +234,7 @@ public class Tile implements Serializable {
/**
* Checks if the location, region, and subsampling can be returned. Throws an exception if this
- * tile has been created without location and not yet processed by {@link MosaicCalculator}.
+ * tile has been created without location and not yet processed by {@link TileOrganizer}.
*/
private void ensureDefined() throws IllegalStateException {
if (xSubsampling == 0 || ySubsampling == 0) {
@@ -247,7 +247,7 @@ public class Tile implements Serializable {
*
* @return the tile upper-left corner.
* @throws IllegalStateException if this tile has been {@linkplain #Tile(Rectangle, AffineTransform)
- * created without location} and has not yet been processed by {@link MosaicCalculator}.
+ * created without location} and has not yet been processed by {@link TileOrganizer}.
*
* @see javax.imageio.ImageReadParam#setDestinationOffset(Point)
*/
@@ -263,7 +263,7 @@ public class Tile implements Serializable {
*
* <p>At the difference of {@link #getLocation()} and {@link #getRegion()}, this method never
* throw {@link IllegalStateException} because the tile size does not depend on the processing
- * performed by {@link MosaicCalculator}.</p>
+ * performed by {@link TileOrganizer}.</p>
*
* @return the tile size.
* @throws IOException if an I/O operation was required for fetching the tile size and that operation failed.
@@ -302,7 +302,7 @@ public class Tile implements Serializable {
* @return the region in the mosaic (destination image).
* @throws IOException if an I/O operation was required for fetching the tile size and that operation failed.
* @throws IllegalStateException if this tile has been {@linkplain #Tile(Rectangle, AffineTransform) created
- * without location} and has not yet been processed by {@link MosaicCalculator}, of if this tile does
+ * without location} and has not yet been processed by {@link TileOrganizer}, of if this tile does
* not have enough information for providing a tile size.
*
* @see javax.imageio.ImageReadParam#setSourceRegion(Rectangle)
@@ -325,7 +325,7 @@ public class Tile implements Serializable {
* @return the region in units relative to the tile having the finest resolution.
* @throws IOException if an I/O operation was required for fetching the tile size and that operation failed.
* @throws IllegalStateException if this tile has been {@linkplain #Tile(Rectangle, AffineTransform) created
- * without location} and has not yet been processed by {@link MosaicCalculator}, of if this tile does
+ * without location} and has not yet been processed by {@link TileOrganizer}, of if this tile does
* not have enough information for providing a tile size.
*/
public synchronized Rectangle getAbsoluteRegion() throws IOException {
@@ -340,7 +340,7 @@ public class Tile implements Serializable {
}
/**
- * Invoked by {@link MosaicCalculator} only. No other caller allowed.
+ * Invoked by {@link TileOrganizer} only. No other caller allowed.
* {@link #setSubsampling(Dimension)} must be invoked prior this method.
*
* <p>Note that invoking this method usually invalidate {@link #gridToCRS}.
@@ -368,7 +368,7 @@ public class Tile implements Serializable {
*
* @return the subsampling along <var>x</var> and <var>y</var> axes.
* @throws IllegalStateException if this tile has been {@linkplain #Tile(Rectangle, AffineTransform)
- * created without location} and has not yet been processed by {@link MosaicCalculator}.
+ * created without location} and has not yet been processed by {@link TileOrganizer}.
*
* @see javax.imageio.ImageReadParam#setSourceSubsampling(int, int, int, int)
*/
@@ -379,7 +379,7 @@ public class Tile implements Serializable {
/**
* Sets the subsampling to the given dimension.
- * Invoked by constructors and {@link MosaicCalculator} only.
+ * Invoked by constructors and {@link TileOrganizer} only.
*/
final void setSubsampling(final Dimension subsampling) throws IllegalStateException {
// No assert Thread.holdsLock(this) because invoked from constructors.
@@ -397,14 +397,14 @@ public class Tile implements Serializable {
}
/**
- * If the user-supplied transform is waiting for processing by {@link MosaicCalculator}, returns it.
- * Otherwise returns {@code null}. This method is for internal usage by {@link MosaicCalculator} only.
+ * If the user-supplied transform is waiting for processing by {@link TileOrganizer}, returns it.
+ * Otherwise returns {@code null}. This method is for internal usage by {@link TileOrganizer} only.
*
* <p>This method clears the {@link #gridToCRS} field before to return. This is a way to tell that
* processing is in progress, and also a safety against transform usage while it may become invalid.</p>
*
* @return the transform, or {@code null} if none. This method does not clone the returned value -
- * {@link MosaicCalculator} will reference and modify directly that transform.
+ * {@link TileOrganizer} will reference and modify directly that transform.
*/
final synchronized AffineTransform getPendingGridToCRS() {
if ((xSubsampling | ySubsampling) != 0) {
@@ -437,14 +437,14 @@ public class Tile implements Serializable {
* @return the <cite>"grid to real world"</cite> transform mapping pixel
* {@linkplain PixelOrientation#UPPER_LEFT upper left} corner, or {@code null} if undefined.
* @throws IllegalStateException if this tile has been {@linkplain #Tile(Rectangle, AffineTransform)
- * created without location} and has not yet been processed by {@link MosaicCalculator}.
+ * created without location} and has not yet been processed by {@link TileOrganizer}.
*/
public synchronized AffineTransform2D getGridToCRS() throws IllegalStateException {
ensureDefined();
/*
* The cast should not fail: if the `gridToCRS` is the one specified at construction time,
* then `ensureDefined()` should have thrown an IllegalStateException. Otherwise this tile
- * have been processed by `MosaicCalculator`, which has set an `AffineTransform2D` instance.
+ * have been processed by `TileOrganizer`, which has set an `AffineTransform2D` instance.
* If we get a ClassCastException below, then there is a bug in our pre/post conditions.
*/
return (AffineTransform2D) gridToCRS;
@@ -468,7 +468,7 @@ public class Tile implements Serializable {
}
/**
- * Translates this tile. For internal usage by {@link MosaicCalculator} only.
+ * Translates this tile. For internal usage by {@link TileOrganizer} only.
*
* <p>Reminder: {@link #setGridToCRS(AffineTransform)} should be invoked after this method.</p>
*
@@ -511,7 +511,7 @@ public class Tile implements Serializable {
* which can still be modified (only once) by MocaicCalculator, or by read operations during `getSize()`
* or `getRegion()` execution. This causes confusing behavior when used in an HashMap. We are better to
* rely on system identity. For example `DatumShiftGridGroup` rely on the capability to locate Tiles in
- * HashMap before and after they have been processed by `MosaicCalculator`.
+ * HashMap before and after they have been processed by `TileOrganizer`.
*/
/**
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/MosaicCalculator.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TileOrganizer.java
similarity index 98%
rename from core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/MosaicCalculator.java
rename to core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TileOrganizer.java
index e1f4445..fb4f0a4 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/MosaicCalculator.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TileOrganizer.java
@@ -23,7 +23,6 @@ import java.util.Map;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Collection;
-import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.io.IOException;
@@ -43,7 +42,7 @@ import org.apache.sis.referencing.operation.matrix.AffineTransforms2D;
* But in some cases the destination rectangle is not known directly. Instead we have a set of tiles,
* all of them with an upper-left corner located at (0,0), but different <cite>grid to CRS</cite>
* affine transforms read from <a href="https://en.wikipedia.org/wiki/World_file">World Files</a>.
- * This {@code MosaicCalculator} class infers the destination regions automatically
+ * This {@code TileOrganizer} class infers the destination regions automatically
* from the set of affine transforms.
*
* @author Martin Desruisseaux (Geomatys)
@@ -51,7 +50,7 @@ import org.apache.sis.referencing.operation.matrix.AffineTransforms2D;
* @since 1.1
* @module
*/
-public class MosaicCalculator {
+public class TileOrganizer {
/**
* Small number for floating point comparisons.
*/
@@ -75,7 +74,7 @@ public class MosaicCalculator {
*
* @param location the location, or {@code null} for (0,0).
*/
- public MosaicCalculator(final Point location) {
+ public TileOrganizer(final Point location) {
if (location != null) {
xLocation = location.x;
yLocation = location.y;
@@ -128,7 +127,7 @@ public class MosaicCalculator {
* <p><strong>Invoking this method clear the collection</strong>. On return, this instance is empty.
* This is because current implementation modify its workspace directly for efficiency.</p>
*
- * @return all tiles added to this {@code MosaicCalculator}, grouped by pyramids.
+ * @return all tiles added to this {@code TileOrganizer}, grouped by pyramids.
* @throws IOException if a call to {@link Tile#getSize()} or {@link Tile#getRegion()} failed,
* and {@link #unavailableSize(Tile, IOException)} did not consumed the exception.
*/
@@ -490,7 +489,7 @@ public class MosaicCalculator {
/**
* Invoked when an I/O error occurred in {@link Tile#getSize()} or {@link Tile#getRegion()}.
- * This error is non-fatal since {@code MosaicCalculator} can fallback on calculation based
+ * This error is non-fatal since {@code TileOrganizer} can fallback on calculation based
* on tile location only (without size).
*
* <p>The default implementation returns {@code false}, which instructs the caller to let
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TranslatedTransform.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TranslatedTransform.java
index c755db7..59b2b30 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TranslatedTransform.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/TranslatedTransform.java
@@ -23,7 +23,7 @@ import java.awt.geom.AffineTransform;
/**
* An affine transform which is translated relative to an original transform.
* The translation terms are stored separately without modifying the transform.
- * This class if for internal use by {@link MosaicCalculator} only.
+ * This class if for internal use by {@link TileOrganizer} only.
*
* @author Martin Desruisseaux (Geomatys)
* @version 1.1
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
index e634375..883f45b 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
@@ -29,7 +29,7 @@ import org.opengis.util.FactoryException;
import org.opengis.referencing.operation.NoninvertibleTransformException;
import org.apache.sis.referencing.operation.transform.InterpolatedTransform;
import org.apache.sis.internal.referencing.j2d.AffineTransform2D;
-import org.apache.sis.internal.referencing.j2d.MosaicCalculator;
+import org.apache.sis.internal.referencing.j2d.TileOrganizer;
import org.apache.sis.internal.referencing.j2d.Tile;
import org.apache.sis.internal.referencing.Resources;
import org.apache.sis.internal.util.CollectionsExt;
@@ -58,7 +58,7 @@ final class DatumShiftGridGroup<C extends Quantity<C>, T extends Quantity<T>> ex
/** Subsampling compared to the grid having finest resolution. */
private final double sx, sy;
- /** Creates a new instance from the given {@link MosaicCalculator} result. */
+ /** Creates a new instance from the given {@link TileOrganizer} result. */
Region(final Tile tile) throws IOException {
final Rectangle r = tile.getAbsoluteRegion();
final Dimension s = tile.getSubsampling();
@@ -101,8 +101,8 @@ final class DatumShiftGridGroup<C extends Quantity<C>, T extends Quantity<T>> ex
* The first sub-grid is taken as a template for setting parameter values such as filename (all list
* elements should declare the same filename parameters, so the selected element should not matter).
*
- * @param tiles the tiles computed by {@link MosaicCalculator}.
- * @param grids sub-grids associated to tiles computed by {@link MosaicCalculator}.
+ * @param tiles the tiles computed by {@link TileOrganizer}.
+ * @param grids sub-grids associated to tiles computed by {@link TileOrganizer}.
* @param gridToCRS conversion from grid indices to "real world" coordinates.
* @param nx number of cells along the <var>x</var> axis in the grid.
* @param ny number of cells along the <var>y</var> axis in the grid.
@@ -136,7 +136,7 @@ final class DatumShiftGridGroup<C extends Quantity<C>, T extends Quantity<T>> ex
final Path file, final List<DatumShiftGridFile<C,T>> subgrids)
throws IOException, FactoryException, NoninvertibleTransformException
{
- final MosaicCalculator mosaic = new MosaicCalculator(null);
+ final TileOrganizer mosaic = new TileOrganizer(null);
final Map<Tile,DatumShiftGridFile<C,T>> grids = new LinkedHashMap<>();
for (final DatumShiftGridFile<C,T> grid : subgrids) {
final int[] size = grid.getGridSize();
[sis] 01/02: Refactor a bit DatumShiftGridGroup for computing
sub-grid coordinates in a more direct way than using AffineTransform. This
change allows us to fix a bug,
where the translation that we got was not adjusted for the different size of
the cell.
Posted by de...@apache.org.
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 327b2df53b3e6074e93f85426024d0b82fb6d575
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Sat Feb 15 00:43:28 2020 +0100
Refactor a bit DatumShiftGridGroup for computing sub-grid coordinates in a more direct way than using AffineTransform.
This change allows us to fix a bug, where the translation that we got was not adjusted for the different size of the cell.
---
.../internal/referencing/j2d/MosaicCalculator.java | 4 +-
.../apache/sis/internal/referencing/j2d/Tile.java | 99 ++----------
.../referencing/provider/DatumShiftGridFile.java | 8 +-
.../referencing/provider/DatumShiftGridGroup.java | 179 ++++++++++++++-------
4 files changed, 134 insertions(+), 156 deletions(-)
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/MosaicCalculator.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/MosaicCalculator.java
index b0654cd..e1f4445 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/MosaicCalculator.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/MosaicCalculator.java
@@ -511,8 +511,6 @@ public class MosaicCalculator {
*/
@Override
public String toString() {
- final List<Tile> tiles = new ArrayList<>(this.tiles.values());
- Collections.sort(tiles);
- return Tile.toString(tiles, 400);
+ return Tile.toString(tiles.values(), 400);
}
}
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java
index ec755eb..847fb42 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/Tile.java
@@ -104,7 +104,7 @@ import org.apache.sis.io.TableAppender;
* @since 1.1
* @module
*/
-public class Tile implements Comparable<Tile>, Serializable {
+public class Tile implements Serializable {
/**
* For cross-version compatibility during serialization.
*/
@@ -506,88 +506,13 @@ public class Tile implements Comparable<Tile>, Serializable {
return 0;
}
- /**
- * Compares two tiles for optimal order in sequential reads. Default implementation sorts by
- * increasing {@linkplain #getImageIndex image index}. This ordering allows efficient access
- * for tiles that are stored sequentially in a file.
- *
- * <p>For tiles having the same image index, additional criterion are used like increasing
- * subsampling, increasing <var>y</var> then increasing <var>x</var> coordinates.
- * But the actual set of additional criterion may change in any future version.
- *
- * @param other the tile to compare with.
- * @return -1 if this tile should be read before {@code other},
- * +1 if it should be read after or 0 if equal.
- */
- @Override
- public final int compareTo(final Tile other) {
- int c = getImageIndex() - other.getImageIndex();
- if (c == 0) {
- /*
- * From this point it does not matter much for disk access. But we continue to
- * define criterions for consistency with `equals(Object)` method. We compare
- * subsampling first because it may be undefined while it is needed for (x,y)
- * ordering. Undefined subsampling will be ordered first (this is arbitrary).
- */
- final int sy = this.ySubsampling;
- final int oy = other.ySubsampling;
- c = sy - oy;
- if (c == 0) {
- final int sx = this.xSubsampling;
- final int ox = other.xSubsampling;
- c = sx - ox;
- if (c == 0) {
- c = (y * sy) - (other.y * oy);
- if (c == 0) {
- c = (x * sx) - (other.x * ox);
- }
- }
- }
- }
- return c;
- }
-
- /**
- * Compares this tile with the specified one for equality. Two tiles are considered equal
- * if they are of the same class and have the same {@linkplain #getRegion() region} and
- * same {@linkplain #getSubsampling() subsampling}. Subclasses should override if there
- * is more properties to compare such as image format and index.
- *
- * @param object the object to compare with.
- * @return {@code true} if both objects are equal.
+ /*
+ * Intentionally no implementation for `equals()` and `hashCode()`. Tile is an "almost immutable" class
+ * which can still be modified (only once) by MocaicCalculator, or by read operations during `getSize()`
+ * or `getRegion()` execution. This causes confusing behavior when used in an HashMap. We are better to
+ * rely on system identity. For example `DatumShiftGridGroup` rely on the capability to locate Tiles in
+ * HashMap before and after they have been processed by `MosaicCalculator`.
*/
- @Override
- public boolean equals(final Object object) {
- if (object == this) {
- return true;
- }
- if (object != null && object.getClass() == getClass()) {
- final Tile that = (Tile) object;
- if (this.x == that.x && this.y == that.y &&
- this.xSubsampling == that.xSubsampling &&
- this.ySubsampling == that.ySubsampling)
- {
- /*
- * Compares width and height only if they are defined in both tiles. We do not
- * invoke `getRegion()` because it may be expensive and useless anyway: If both
- * tiles have the same image reader, image index and input, then logically they
- * must have the same size - invoking `getRegion()` would read exactly the same
- * image twice.
- */
- return (width == 0 || that.width == 0 || width == that.width) &&
- (height == 0 || that.height == 0 || height == that.height);
- }
- }
- return false;
- }
-
- /**
- * Returns a hash code value for this tile.
- */
- @Override
- public int hashCode() {
- return x + 37*y;
- }
/**
* Returns a string representation of this tile for debugging purposes.
@@ -632,9 +557,8 @@ public class Tile implements Comparable<Tile>, Serializable {
}
/**
- * Returns a string representation of a collection of tiles. The tiles are formatted in a
- * table in iteration order. Tip: consider sorting the tiles before to invoke this method;
- * tiles are {@link Comparable} for this purpose.
+ * Returns a string representation of a collection of tiles.
+ * The tiles are formatted in a table in iteration order.
*
* <p>This method is not public because it can consume a large amount of memory (the underlying
* {@link StringBuffer} can be quite large). Users are encouraged to use the method expecting a
@@ -658,9 +582,8 @@ public class Tile implements Comparable<Tile>, Serializable {
}
/**
- * Formats a collection of tiles in a table. The tiles are appended in iteration order.
- * Tip: consider sorting the tiles before to invoke this method;
- * tiles are {@link Comparable} for this purpose.
+ * Formats a collection of tiles in a table.
+ * The tiles are appended in iteration order.
*
* @param tiles the tiles to format in a table.
* @param out where to write the table.
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridFile.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridFile.java
index 1826544..c9c3794 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridFile.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridFile.java
@@ -71,11 +71,11 @@ abstract class DatumShiftGridFile<C extends Quantity<C>, T extends Quantity<T>>
static final Cache<Object, DatumShiftGridFile<?,?>> CACHE = new Cache<Object, DatumShiftGridFile<?,?>>(4, 32*1024, true) {
@Override protected int cost(final DatumShiftGridFile<?,?> grid) {
int p = 1;
- for (final Object array : grid.getData()) {
- if (array instanceof DatumShiftGridFile<?,?>) {
- p += cost((DatumShiftGridFile<?,?>) array);
+ for (final Object data : grid.getData()) {
+ if (data instanceof DatumShiftGridFile<?,?>) {
+ p += cost((DatumShiftGridFile<?,?>) data); // When `grid` is a DatumShiftGridGroup.
} else {
- p *= Array.getLength(array);
+ p *= Array.getLength(data); // short[], float[] or double[].
}
}
return p;
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
index d9b5d32..e634375 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
@@ -20,12 +20,14 @@ import java.util.Map;
import java.util.List;
import java.nio.file.Path;
import java.io.IOException;
+import java.awt.Dimension;
import java.awt.Rectangle;
-import java.awt.geom.Point2D;
import java.awt.geom.AffineTransform;
+import java.util.LinkedHashMap;
import javax.measure.Quantity;
import org.opengis.util.FactoryException;
import org.opengis.referencing.operation.NoninvertibleTransformException;
+import org.apache.sis.referencing.operation.transform.InterpolatedTransform;
import org.apache.sis.internal.referencing.j2d.AffineTransform2D;
import org.apache.sis.internal.referencing.j2d.MosaicCalculator;
import org.apache.sis.internal.referencing.j2d.Tile;
@@ -35,10 +37,8 @@ import org.apache.sis.internal.util.CollectionsExt;
/**
* A group of datum shift grids. This is used when a NTv2 file contains more than one grid with no common parent.
- * This class creates a synthetic parent with an affine transform approximating all grids. The affine transform is
- * close to identity transform. Its main purpose is to locate a grid during inverse transforms, before refinements
- * using the real grids. So a "best match" transform (for example estimated using least squares method) would not
- * be useful because the differences would be small compared to grid cell sizes.
+ * This class creates a synthetic parent which always delegate its work to a child (as opposed to more classical
+ * trees where the parent can do some work if no child can).
*
* @author Martin Desruisseaux (Geomatys)
* @version 1.1
@@ -47,66 +47,79 @@ import org.apache.sis.internal.util.CollectionsExt;
*/
final class DatumShiftGridGroup<C extends Quantity<C>, T extends Quantity<T>> extends DatumShiftGridFile<C,T> {
/**
- * For each {@code subgrids[i]}, {@code regions[i]} is the range of indices valid of that grid.
- *
- * @todo We should replace by an R-Tree. For now we assume that the array is small enough.
+ * The bounds of a sub-grid, together with the subsampling level compared to the grid having the finest resolution.
+ * All values in this class are integers, but nevertheless stored as {@code double} for avoiding to cast them every
+ * time {@link #interpolateInCell(double, double, double[])} is executed.
*/
- private final Rectangle[] regions;
+ private static final class Region {
+ /** Grid bounds in units of the grid having finest resolution. */
+ private final double xmin, xmax, ymin, ymax;
+
+ /** Subsampling compared to the grid having finest resolution. */
+ private final double sx, sy;
+
+ /** Creates a new instance from the given {@link MosaicCalculator} result. */
+ Region(final Tile tile) throws IOException {
+ final Rectangle r = tile.getAbsoluteRegion();
+ final Dimension s = tile.getSubsampling();
+ xmin = r.getMinX();
+ xmax = r.getMaxX();
+ ymin = r.getMinY();
+ ymax = r.getMaxY();
+ sx = s.width;
+ sy = s.height;
+ }
+
+ /** Tests whether the given coordinates are included in this region. */
+ final boolean contains(final double x, final double y) {
+ return x >= xmin && x <= xmax && y >= ymin && y <= ymax;
+ }
+
+ /** Converts a coordinate from the parent grid to this grid. */
+ final double x(final double p) {return (p - xmin) / sx;}
+ final double y(final double p) {return (p - ymin) / sy;}
+
+ /** Returns the subsampling (compared to the grid having finest resolution) in the specified dimension. */
+ final double relativeCellSize(final int dim) {
+ switch (dim) {
+ case 0: return sx;
+ case 1: return sy;
+ default: throw new IndexOutOfBoundsException();
+ }
+ }
+ }
/**
- * Converts indices from this grid to indices in sub-grids.
+ * For each {@code subgrids[i]}, {@code regions[i]} is the range of indices valid of that grid.
+ * This array will be used only as a fallback if the {@code MathTransform} has not been able to
+ * find the sub-grid itself. Since it should be rarely used, we do not bother using a R-Tree.
*/
- private final AffineTransform[] toSubGrids;
+ private final Region[] regions;
/**
* Creates a new group for the given list of sub-grids. That list shall contain at least 2 elements.
* The first sub-grid is taken as a template for setting parameter values such as filename (all list
* elements should declare the same filename parameters, so the selected element should not matter).
*
- * @param grids sub-grids with their indices range. The array is declared as {@code Tile[]}
- * because this is the type returned by {@link MosaicCalculator#tiles()}, but
- * each element shall be an instance of {@link Region}.
- * @param gridToCRS conversion from grid indices to "real world" coordinates.
- * @param nx number of cells along the <var>x</var> axis in the grid.
- * @param ny number of cells along the <var>y</var> axis in the grid.
+ * @param tiles the tiles computed by {@link MosaicCalculator}.
+ * @param grids sub-grids associated to tiles computed by {@link MosaicCalculator}.
+ * @param gridToCRS conversion from grid indices to "real world" coordinates.
+ * @param nx number of cells along the <var>x</var> axis in the grid.
+ * @param ny number of cells along the <var>y</var> axis in the grid.
* @throws IOException declared because {@link Tile#getRegion()} declares it, but should not happen.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
- private DatumShiftGridGroup(final Tile[] grids, final AffineTransform2D gridToCRS, final int nx, final int ny)
+ private DatumShiftGridGroup(final Tile[] tiles, final Map<Tile,DatumShiftGridFile<C,T>> grids,
+ final AffineTransform2D gridToCRS, final int nx, final int ny)
throws IOException, NoninvertibleTransformException
{
- super((DatumShiftGridFile<C,T>) ((Region) grids[0]).grid, gridToCRS.inverse(), nx, ny);
- subgrids = new DatumShiftGridFile[grids.length];
- regions = new Rectangle[grids.length];
- toSubGrids = new AffineTransform[grids.length];
- for (int i=0; i<grids.length; i++) {
- final Region r = (Region) grids[i];
- final AffineTransform tr = new AffineTransform(gridToCRS);
- tr.preConcatenate((AffineTransform) r.grid.getCoordinateToGrid());
- subgrids [i] = (DatumShiftGridFile<C,T>) r.grid;
- regions [i] = r.getAbsoluteRegion();
- toSubGrids[i] = tr;
- }
- }
-
- /**
- * A sub-grid wrapped with information about the region where it applies.
- * The region is expressed as indices in a larger grid. That larger grid
- * is what {@link MosaicCalculator} will try to infer.
- */
- @SuppressWarnings("serial")
- private static final class Region extends Tile {
- /** The wrapped sub-grid. */
- final DatumShiftGridFile<?,?> grid;
-
- /**
- * Creates a new wrapper for the given sub-grid.
- *
- * @param size value of {@link DatumShiftGridFile#getGridSize()}.
- */
- Region(final DatumShiftGridFile<?,?> grid, final int[] size) throws NoninvertibleTransformException {
- super(new Rectangle(size[0], size[1]), (AffineTransform) grid.getCoordinateToGrid().inverse());
- this.grid = grid;
+ super(grids.get(tiles[0]), gridToCRS.inverse(), nx, ny);
+ final int n = grids.size();
+ regions = new Region[n];
+ subgrids = new DatumShiftGridFile[n];
+ for (int i=0; i<n; i++) {
+ regions [i] = new Region(tiles[i]);
+ subgrids[i] = grids.get(tiles[i]);
}
}
@@ -124,8 +137,16 @@ final class DatumShiftGridGroup<C extends Quantity<C>, T extends Quantity<T>> ex
throws IOException, FactoryException, NoninvertibleTransformException
{
final MosaicCalculator mosaic = new MosaicCalculator(null);
+ final Map<Tile,DatumShiftGridFile<C,T>> grids = new LinkedHashMap<>();
for (final DatumShiftGridFile<C,T> grid : subgrids) {
- mosaic.add(new Region(grid, grid.getGridSize()));
+ final int[] size = grid.getGridSize();
+ final Tile tile = new Tile(new Rectangle(size[0], size[1]),
+ (AffineTransform) grid.getCoordinateToGrid().inverse());
+ if (mosaic.add(tile)) { // Should never be false, but check anyway.
+ if (grids.put(tile, grid) != null) {
+ throw new AssertionError(tile); // Should never happen (paranoiac check).
+ }
+ }
}
final Map.Entry<Tile,Tile[]> result = CollectionsExt.singletonOrNull(mosaic.tiles().entrySet());
if (result == null) {
@@ -133,7 +154,7 @@ final class DatumShiftGridGroup<C extends Quantity<C>, T extends Quantity<T>> ex
}
final Tile global = result.getKey();
final Rectangle r = global.getRegion();
- return new DatumShiftGridGroup<>(result.getValue(), global.getGridToCRS(), r.width, r.height);
+ return new DatumShiftGridGroup<>(result.getValue(), grids, global.getGridToCRS(), r.width, r.height);
}
/**
@@ -142,9 +163,8 @@ final class DatumShiftGridGroup<C extends Quantity<C>, T extends Quantity<T>> ex
*/
private DatumShiftGridGroup(final DatumShiftGridGroup<C,T> other, final DatumShiftGridFile<C,T>[] data) {
super(other);
- subgrids = data;
- regions = other.regions;
- toSubGrids = other.toSubGrids;
+ subgrids = data;
+ regions = other.regions;
}
/**
@@ -182,6 +202,9 @@ final class DatumShiftGridGroup<C extends Quantity<C>, T extends Quantity<T>> ex
/**
* Returns the translation stored at the given two-dimensional grid indices for the given dimension.
+ * This method is defined for consistency with {@link #interpolateInCell(double, double, double[])}
+ * but should never be invoked. The {@link InterpolatedTransform} class will rather invoke the
+ * {@code interpolateInCell} method for efficiency.
*
* @param dim the dimension of the translation vector component to get.
* @param gridX the grid index on the <var>x</var> axis, from 0 inclusive to {@code gridSize[0]} exclusive.
@@ -191,15 +214,49 @@ final class DatumShiftGridGroup<C extends Quantity<C>, T extends Quantity<T>> ex
@Override
public double getCellValue(final int dim, final int gridX, final int gridY) {
for (int i=0; i<regions.length; i++) {
- final Rectangle r = regions[i];
+ final Region r = regions[i];
if (r.contains(gridX, gridY)) {
- Point2D pt = new Point2D.Double(gridX, gridY);
- pt = toSubGrids[i].transform(pt, pt);
- return subgrids[i].getCellValue(dim,
- Math.toIntExact(Math.round(pt.getX())),
- Math.toIntExact(Math.round(pt.getY())));
+ double shift = subgrids[i].getCellValue(dim,
+ Math.toIntExact(Math.round(r.x(gridX))),
+ Math.toIntExact(Math.round(r.y(gridY))));
+ /*
+ * If the translations have been divided by the cell size, we may need to compensate.
+ * The size of the cells of the grid used below may be bigger than the cells of this
+ * pseudo-grid.
+ */
+ if (isCellValueRatio()) {
+ shift *= r.relativeCellSize(dim);
+ }
+ return shift;
}
}
throw new IndexOutOfBoundsException();
}
+
+ /**
+ * Interpolates the translation to apply for the given two-dimensional grid indices. The result is stored
+ * in the given {@code vector} array. This method is invoked only as a fallback if the transform has not
+ * been able to use directly one of the child transforms. Consequently this implementation does not need
+ * to be very fast.
+ *
+ * @param gridX first grid coordinate of the point for which to get the translation.
+ * @param gridY second grid coordinate of the point for which to get the translation.
+ * @param vector a pre-allocated array where to write the translation vector.
+ */
+ @Override
+ public void interpolateInCell(final double gridX, final double gridY, final double[] vector) {
+ for (int i=0; i<regions.length; i++) {
+ final Region r = regions[i];
+ if (r.contains(gridX, gridY)) {
+ subgrids[i].interpolateInCell(r.x(gridX), r.y(gridY), vector);
+ if (isCellValueRatio()) {
+ for (int dim=0; dim < INTERPOLATED_DIMENSIONS; dim++) {
+ vector[dim] *= r.relativeCellSize(dim);
+ }
+ }
+ return;
+ }
+ }
+ super.interpolateInCell(gridX, gridY, vector);
+ }
}