Class LocalizationGrid
- Object
-
- LocalizationGrid
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
NADConGridShift
public class LocalizationGrid extends Object implements Serializable
A factory forMathTransform2Dbacked by a grid of localization. A grid of localization is a two-dimensional array of coordinate points. The grid size iswidth×height. Input coordinates are (i,j) index in the grid, where i must be in the range[0..width-1]and j in the range[0..height-1]inclusive. Output coordinates are the values stored in the grid of localization at the specified index.The
LocalizationGridclass is usefull when the "grid to coordinate system" transform for a coverage is not some kind of global mathematical relationship like an affine transform. Instead, the "real world" coordinates are explicitly specified for each pixels. If the real world coordinates are know only for some pixels at a fixed interval, then a transformation can be constructed by the concatenation of an affine transform with a grid of localization.After a
LocalizationGridobject has been fully constructed (i.e. real world coordinates have been specified for all grid cells), a transformation from grid coordinates to "real world" coordinates can be obtained with thegetMathTransform()method. If this transformation is close enough to an affine transform, then an instance ofAffineTransformis returned. Otherwise, a transform backed by the localization grid is returned.The example below goes through the steps of constructing a coordinate reference system for a grid coverage from its grid of localization. This example assumes that the "real world" coordinates are longitudes and latitudes on the WGS84 ellipsoid.
// // Constructs a localization grid of size 10×10. // LocalizationGrid grid = new LocalizationGrid(10,10); for (int j=0; j<10; j++) { for (int i=0; i<10; i++) { double x = ...; // Set longitude here double y = ...; // Set latitude here grid.setLocalizationPoint(i,j,x,y); } } // // Constructs the grid coordinate reference system. degree is the polynomial // degree (e.g. 2) for a math transform that approximately map the grid of localization. // For a more accurate (but not always better) math transform backed by the whole grid, // invokes getMathTransform() instead, or use the special value of 0 for the degree // argument. // MathTransform2D realToGrid = grid.getPolynomialTransform(degree).inverse(); CoordinateReferenceSystem realCRS = DefaultGeographicCRS.WGS84; CoordinateReferenceSystem gridCRS = new DefaultDerivedCRS("The grid CRS", new DefaultOperationMethod(realToGrid), realCRS, // The target ("real world") CRS realToGrid, // How the grid CRS relates to the "real world" CRS DefaultCartesianCS.GRID); // // Constructs the grid coverage using the grid coordinate system (not the "real world" // one). It is usefull to display the coverage in its native CRS before we resample it. // Note that if the grid of localization does not define the geographic location for // all pixels, then we need to specify some affine transform in place of the call to // IdentityTransform. For example if the grid of localization defines the location of // 1 pixel, then skip 3, then defines the location of 1 pixel, etc., then the affine // transform should be AffineTransform.getScaleInstance(0.25, 0.25). // WritableRaster raster = RasterFactory.createBandedRaster(DataBuffer.TYPE_FLOAT, width, height, 1, null); for (int y=0; ysome_value); } } GridCoverageFactory factory = FactoryFinder.getGridCoverageFactory(null); GridCoverage coverage = factory.create("My grayscale coverage", raster, gridCRS, IdentityTransform.create(2), null, null, null, null, null); coverage.show(); // // Projects the coverage from its current 'gridCS' to the 'realCS'. If the grid of // localization was built from the orbit of some satellite, then the projected // coverage will tpypically have a curved aspect. // coverage = (Coverage2D) Operations.DEFAULT.resample(coverage, realCRS); coverage.show(); - Since:
- 2.4
- Author:
- Remi Eve, Martin Desruisseaux (IRD), Alessio Fabiani
- See Also:
DerivedCRS, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description LocalizationGrid(int width, int height)Constructs an initially empty localization grid.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AffineTransformgetAffineTransform()Returns an affine transform for the whole grid.Point2DgetLocalizationPoint(Point source)Returns the "real world" coordinates for the specified grid coordinates.MathTransform2DgetMathTransform()Returns a math transform from grid to "real world" coordinates.MathTransform2DgetPolynomialTransform(int degree)Returns a math transform from grid to "real world" coordinates using a polynomial fitting of the specified degree.DimensiongetSize()Returns the grid size.booleanisMonotonic(boolean strict)Returnstrueif all coordinates in this grid are increasing or decreasing.booleanisNaN()Returnstrueif this localization grid contains at least oneNaNvalue.voidremoveSingularities()Makes sure that the grid doesn't contains identical consecutive ordinates.voidsetLocalizationPoint(int sourceX, int sourceY, double targetX, double targetY)Set a point in this localization grid.voidsetLocalizationPoint(Point source, Point2D target)Set a point in this localization grid.voidtransform(AffineTransform transform, Rectangle region)Apply a transformation to every "real world" coordinate points in a sub-region of this grid.
-
-
-
Method Detail
-
getSize
public Dimension getSize()
Returns the grid size. Grid coordinates are always in the rangexinput = [0..width-1]andyinput = [0..height-1]inclusive.
-
getLocalizationPoint
public Point2D getLocalizationPoint(Point source)
Returns the "real world" coordinates for the specified grid coordinates. Grid coordinates must be integers inside this grid's range. For general transformations involving non-integer grid coordinates and/or coordinates outside this grid's range, usegetMathTransform()instead.- Parameters:
source- The point in grid coordinates.- Returns:
- target The corresponding point in "real world" coordinates.
- Throws:
IndexOutOfBoundsException- If the source point is not in this grid's range.
-
setLocalizationPoint
public void setLocalizationPoint(Point source, Point2D target)
Set a point in this localization grid.- Parameters:
source- The point in grid coordinates.target- The corresponding point in "real world" coordinates.- Throws:
IndexOutOfBoundsException- If the source point is not in this grid's range.
-
setLocalizationPoint
public void setLocalizationPoint(int sourceX, int sourceY, double targetX, double targetY)Set a point in this localization grid.- Parameters:
sourceX- x coordinates in grid coordinates, in the range[0..width-1]inclusive.sourceY- y coordinates in grid coordinates. in the range[0..height-1]inclusive.targetX- x coordinates in "real world" coordinates.targetY- y coordinates in "real world" coordinates.- Throws:
IndexOutOfBoundsException- If the source coordinates is not in this grid's range.
-
transform
public void transform(AffineTransform transform, Rectangle region)
Apply a transformation to every "real world" coordinate points in a sub-region of this grid.- Parameters:
transform- The transform to apply.region- The bounding rectangle (in grid coordinate) for region where to apply the transform, ornullto transform the whole grid.
-
isNaN
public boolean isNaN()
Returnstrueif this localization grid contains at least oneNaNvalue.
-
isMonotonic
public boolean isMonotonic(boolean strict)
Returnstrueif all coordinates in this grid are increasing or decreasing. More specifically, returnstrueif the following conditions are meets:- Coordinates in a row must be increasing or decreasing. If
strictistrue, then coordinates must be strictly increasing or decreasing (i.e. equals value are not accepted).NaNvalues are always ignored. - Coordinates in all rows must be increasing, or coordinates in all rows must be decreasing.
- Idem for columns (Coordinates in a columns must be increasing or decreasing, etc.).
- Parameters:
strict-trueto require strictly increasing or decreasing order, orfalseto accept values that are equals.- Returns:
trueif coordinates are increasing or decreasing in the same direction for all rows and columns.
- Coordinates in a row must be increasing or decreasing. If
-
removeSingularities
public void removeSingularities()
Makes sure that the grid doesn't contains identical consecutive ordinates. If many consecutives ordinates are found to be identical in a row or in a column, then the first one is left inchanged and the other ones are linearly interpolated.
-
getAffineTransform
public AffineTransform getAffineTransform()
Returns an affine transform for the whole grid. This transform is only an approximation for this localization grid. It is fitted (like "curve fitting") to grid data using the "least squares" method.- Returns:
- A global affine transform as an approximation for the whole localization grid.
-
getPolynomialTransform
public MathTransform2D getPolynomialTransform(int degree)
Returns a math transform from grid to "real world" coordinates using a polynomial fitting of the specified degree. By convention, adegreeof 0 will returns the math transform backed by the whole grid. Greater values will use a fitted polynomial (affine transform for degree 1, quadratic transform for degree 2, cubic transform for degree 3, etc.).- Parameters:
degree- The polynomial degree for the fitting, or 0 for a transform backed by the whole grid.
-
getMathTransform
public final MathTransform2D getMathTransform()
Returns a math transform from grid to "real world" coordinates. The math transform is backed by the full grid of localization. In terms of JAI's image warp operations, this math transform is backed by aWarpGridwhile the previous methods return math transforms backed byWarpPolynomial.
-
-