Class AbstractMathTransform
- All Implemented Interfaces:
Parameterized,LenientComparable,org.opengis.referencing.operation.MathTransform
- Direct Known Subclasses:
AbstractLinearTransform,AbstractMathTransform.Inverse,AbstractMathTransform1D,AbstractMathTransform2D,CompoundTransform,ConcatenatedTransform,CoordinateSystemTransform,DatumShiftTransform,EllipsoidToCentricTransform,PassThroughTransform,SpecializableTransform,WraparoundTransform
MathTransform interface.
A MathTransform is an object that actually does the work of applying a
formula to coordinate values.
The math transform does not know or care how the coordinates relate to positions in the real world.
For example if an affine transform scales z values by a factor of 1000,
then it could be converting metres to millimetres, or it could be converting kilometres to metres.
AbstractMathTransform provides a convenient base class from which MathTransform implementations
can be easily derived. It also defines a few additional SIS-specific methods for convenience of performance.
The simplest way to implement this abstract class is to provide an implementation for the following methods only:
transform(…) methods as well.
Immutability and thread safety
All Apache SIS implementations ofMathTransform are immutable and thread-safe.
It is highly recommended that third-party implementations be immutable and thread-safe too.
This means that unless otherwise noted in the javadoc, MathTransform instances can
be shared by many objects and passed between threads without synchronization.
Serialization
MathTransform may or may not be serializable, at implementation choices.
Most Apache SIS implementations are serializable, but the serialized objects are not guaranteed to be compatible
with future SIS versions. Serialization should be used only for short term storage or RMI between applications
running the same SIS version.- Since:
- 0.5
- Version:
- 1.3
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classBase class for implementations of inverse math transforms. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intThe cached hash code value, or 0 if not yet computed.(package private) static final intMaximum buffer size when creating temporary arrays.(package private) static final intMaximum amount ofTransformExceptionto catch while transforming a block of 512 coordinate values in an array. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) intbeforeFormat(List<Object> transforms, int index, boolean inverse) Given a transformation chain, replaces the elements aroundtransforms.get(index)transform by alternative objects to use when formatting WKT.protected intComputes a hash value for this transform.org.opengis.referencing.operation.Matrixderivative(org.opengis.geometry.DirectPosition point) Gets the derivative of this transform at a point.final booleanCompares the specified object with this math transform for strict equality.booleanequals(Object object, ComparisonMode mode) Compares the specified object with this math transform for equality.protected StringFormats the inner part of a Well Known Text version 1 (WKT 1) element.protected ContextualParametersReturns the parameters for a sequence of normalize →this→ denormalize transforms (optional operation).Optional<org.opengis.geometry.Envelope>getDomain(DomainDefinition criteria) Returns the ranges of coordinate values which can be used as inputs.org.opengis.parameter.ParameterDescriptorGroupReturns the parameter descriptors for this math transform, ornullif unknown.org.opengis.parameter.ParameterValueGroupReturns the parameter values for this math transform, ornullif unknown.abstract intReturns the number of dimensions of input points.abstract intReturns the number of dimensions of output points.final inthashCode()Returns a hash value for this transform.org.opengis.referencing.operation.MathTransforminverse()Returns the inverse transform of this object.booleanTests whether this transform does not move any points.(package private) static booleanisInverseEquals(org.opengis.referencing.operation.MathTransform tr1, org.opengis.referencing.operation.MathTransform tr2) Returnstrueiftr1is the inverse oftr2.(package private) static org.opengis.geometry.MismatchedDimensionExceptionmismatchedDimension(String argument, int expected, int dimension) Constructs an error message for theMismatchedDimensionException.abstract org.opengis.referencing.operation.Matrixtransform(double[] srcPts, int srcOff, double[] dstPts, int dstOff, boolean derivate) Transforms a single coordinate tuple in an array, and optionally computes the transform derivative at that location.voidtransform(double[] srcPts, int srcOff, double[] dstPts, int dstOff, int numPts) Transforms a list of coordinate tuples.voidtransform(double[] srcPts, int srcOff, float[] dstPts, int dstOff, int numPts) Transforms a list of coordinate tuples.voidtransform(float[] srcPts, int srcOff, double[] dstPts, int dstOff, int numPts) Transforms a list of coordinate tuples.voidtransform(float[] srcPts, int srcOff, float[] dstPts, int dstOff, int numPts) Transforms a list of coordinate tuples.org.opengis.geometry.DirectPositiontransform(org.opengis.geometry.DirectPosition ptSrc, org.opengis.geometry.DirectPosition ptDst) Transforms the specifiedptSrcand stores the result inptDst.protected org.opengis.referencing.operation.MathTransformtryConcatenate(boolean applyOtherFirst, org.opengis.referencing.operation.MathTransform other, org.opengis.referencing.operation.MathTransformFactory factory) Concatenates or pre-concatenates in an optimized way this math transform with the given one, if possible.private static ObjectUnwraps the given object if it is expected to be an inverse transform.Methods inherited from class org.apache.sis.io.wkt.FormattableObject
print, toString, toString, toWKTMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.opengis.referencing.operation.MathTransform
toWKT
-
Field Details
-
MAXIMUM_BUFFER_SIZE
static final int MAXIMUM_BUFFER_SIZEMaximum buffer size when creating temporary arrays. Must not be too big, otherwise the cost of allocating the buffer may be greater than the benefit of transforming array of coordinates. Remember that double number occupy 8 bytes, so a buffer of size 512 will actually consumes 4 kb of memory.- See Also:
-
MAXIMUM_FAILURES
static final int MAXIMUM_FAILURESMaximum amount ofTransformExceptionto catch while transforming a block of 512 coordinate values in an array. The default implementation oftransformmethods set un-transformable coordinates to NaN before to let the exception propagate. However if more then 32 exceptions occur in a block of 512 coordinates (not coordinate tuples), then we will give up. We put a limit in order to avoid slowing down the application too much if a whole array is not transformable.Note that in case of failure, the first
TransformExceptionis still propagated; we do not "eat" it. We just set the coordinates toNaNbefore to let the propagation happen. If no exception handling should be performed at all, thenMAXIMUM_FAILUREScan be set to 0.Having
MAXIMUM_BUFFER_SIZEsets to 512 andMAXIMUM_FAILURESsets to 32 means that we tolerate about 6.25% of un-transformable points.- See Also:
-
hashCode
private transient int hashCodeThe cached hash code value, or 0 if not yet computed. This field is calculated only when first needed. We do not declare itvolatilebecause it is not a big deal if this field is calculated many times, and the same value should be produced by all computations. The only possible outdated value is 0, which is okay.
-
-
Constructor Details
-
AbstractMathTransform
protected AbstractMathTransform()Constructor for subclasses.
-
-
Method Details
-
getSourceDimensions
public abstract int getSourceDimensions()Returns the number of dimensions of input points.- Specified by:
getSourceDimensionsin interfaceorg.opengis.referencing.operation.MathTransform- Returns:
- the number of dimensions of input points.
- See Also:
-
getTargetDimensions
public abstract int getTargetDimensions()Returns the number of dimensions of output points.- Specified by:
getTargetDimensionsin interfaceorg.opengis.referencing.operation.MathTransform- Returns:
- the number of dimensions of output points.
- See Also:
-
getDomain
public Optional<org.opengis.geometry.Envelope> getDomain(DomainDefinition criteria) throws org.opengis.referencing.operation.TransformException Returns the ranges of coordinate values which can be used as inputs. They are limits where the transform is mathematically and numerically applicable. This is not the domain of validity for which a coordinate reference system has been defined, because this method ignores "real world" considerations such as datum and country boundaries.This method is for allowing callers to crop their data for removing areas that may cause numerical problems. For example, results of Mercator projection tend to infinity when the latitude value approaches a pole. For avoiding data structures with unreasonably large values or
Double.NaN, we commonly crop data to some arbitrary maximal latitude value (typically 80 or 84°) before projection. Those limits are arbitrary, the transform does not become suddenly invalid after a limit. TheDomainDefinitiongives some controls on the criteria for choosing a limit.Many transforms, in particular all affine transforms, have no mathematical limits. Consequently, the default implementation returns an empty value. Again it does not mean that the coordinate operation has no geospatial domain of validity, but the latter is not the purpose of this method. This method is (for example) for preventing a viewer to crash when attempting to render a world-wide image.
Callers do not need to search through transform steps. SIS implementation of
concatenated transformsdo that automatically.- Parameters:
criteria- controls the definition of transform domain.- Returns:
- estimation of a domain where this transform is considered numerically applicable.
- Throws:
org.opengis.referencing.operation.TransformException- if the domain cannot be estimated.- Since:
- 1.3
- See Also:
-
MathTransforms.getDomain(MathTransform)CoordinateOperation.getDomainOfValidity()
-
getParameterDescriptors
public org.opengis.parameter.ParameterDescriptorGroup getParameterDescriptors()Returns the parameter descriptors for this math transform, ornullif unknown.Relationship with ISO 19111: This method is similar toOperationMethod.getParameters(), except that typicalMathTransformimplementations return parameters in standard units (usually metres or decimal degrees).- Specified by:
getParameterDescriptorsin interfaceParameterized- Returns:
- the parameter descriptors for this math transform, or
nullif unspecified. - See Also:
-
getParameterValues
public org.opengis.parameter.ParameterValueGroup getParameterValues()Returns the parameter values for this math transform, ornullif unknown. This is not necessarily the parameters that the user specified at construction time, since implementations may have applied normalizations.Normalized and contextual parameters
Most Apache SIS implementations of map projections perform their calculations on an ellipsoid having a semi-major axis length of 1. In such cases, the group returned by this method contains a"semi_major"parameter with a value of 1. If the real axis length is desired, we need to take in account the context of this math transform, i.e. the scales and offsets applied before and after this transform. This information is provided bygetContextualParameters().- Specified by:
getParameterValuesin interfaceParameterized- Returns:
- the parameter values for this math transform, or
nullif unspecified. Note that those parameters may be normalized (e.g. represent a transformation of an ellipsoid of semi-major axis length of 1). - See Also:
-
getContextualParameters
Returns the parameters for a sequence of normalize →this→ denormalize transforms (optional operation). Subclasses can override this method if they choose to split their computation in linear and non-linear parts. Such split is optional: it can leads to better performance (because SIS can concatenate efficiently consecutive linear transforms), but should not change significantly the result (ignoring differences in rounding errors). If a split has been done, then thisMathTransformrepresents only the non-linear step and Apache SIS needs this method for reconstructing the parameters of the complete transform.- Returns:
- the parameter values for the sequence of normalize →
this→ denormalize transforms, ornullif unspecified. Callers should not modify the returned parameters, since modifications (if allowed) will generally not be reflected back in thisMathTransform. - Since:
- 0.6
-
isIdentity
public boolean isIdentity()Tests whether this transform does not move any points. The default implementation always returnsfalse.- Specified by:
isIdentityin interfaceorg.opengis.referencing.operation.MathTransform
-
mismatchedDimension
static org.opengis.geometry.MismatchedDimensionException mismatchedDimension(String argument, int expected, int dimension) Constructs an error message for theMismatchedDimensionException.- Parameters:
argument- the argument name with the wrong number of dimensions.expected- the expected dimension.dimension- the wrong dimension.
-
transform
public org.opengis.geometry.DirectPosition transform(org.opengis.geometry.DirectPosition ptSrc, org.opengis.geometry.DirectPosition ptDst) throws org.opengis.referencing.operation.TransformException Transforms the specifiedptSrcand stores the result inptDst. The default implementation performs the following steps:- Ensures that the dimension of the given points are consistent with the source and target dimensions of this math transform.
- Delegates to the
transform(double[], int, double[], int, boolean)method.
CoordinateReferenceSystemvalue.- Specified by:
transformin interfaceorg.opengis.referencing.operation.MathTransform- Parameters:
ptSrc- the coordinate tuple to be transformed.ptDst- the coordinate tuple that stores the result of transformingptSrc, ornull.- Returns:
- the coordinate tuple after transforming
ptSrcand storing the result inptDst, or a newly created point ifptDstwas null. - Throws:
org.opengis.geometry.MismatchedDimensionException- ifptSrcorptDstdoesn't have the expected dimension.org.opengis.referencing.operation.TransformException- if the point cannot be transformed.
-
transform
public abstract org.opengis.referencing.operation.Matrix transform(double[] srcPts, int srcOff, double[] dstPts, int dstOff, boolean derivate) throws org.opengis.referencing.operation.TransformException Transforms a single coordinate tuple in an array, and optionally computes the transform derivative at that location. Invoking this method is conceptually equivalent to running the following: However, this method provides two advantages:- It is usually easier to implement for
AbstractMathTransformsubclasses. The defaulttransform(double[], int, double[], int, int)method implementation will invoke this method in a loop, taking care of the iteration strategy depending on the argument value. - When both the transformed point and its derivative are needed, this method may be significantly faster than
invoking the
transformandderivativemethods separately because many internal calculations are the same. Computing those two information in a single step can help to reduce redundant calculation.
Note for implementers
The source and destination may overlap. Consequently, implementers must read all source coordinate values before to start writing the transformed coordinates in the destination array.- Parameters:
srcPts- the array containing the source coordinates (cannot benull).srcOff- the offset to the point to be transformed in the source array.dstPts- the array into which the transformed coordinates is returned. May be the same thansrcPts. May benullif only the derivative matrix is desired.dstOff- the offset to the location of the transformed point that is stored in the destination array.derivate-truefor computing the derivative, orfalseif not needed.- Returns:
- the matrix of the transform derivative at the given source position,
or
nullif thederivateargument isfalse. - Throws:
org.opengis.referencing.operation.TransformException- if the point cannot be transformed or if a problem occurred while calculating the derivative.- See Also:
- It is usually easier to implement for
-
transform
public void transform(double[] srcPts, int srcOff, double[] dstPts, int dstOff, int numPts) throws org.opengis.referencing.operation.TransformException Transforms a list of coordinate tuples. This method is provided for efficiently transforming many points. The supplied array of coordinate values will contain packed coordinate values.Example: if the source dimension is 3, then the coordinates will be packed in this order: (x₀,y₀,z₀, x₁,y₁,z₁ …).The default implementation invokestransform(double[], int, double[], int, boolean)in a loop, using an iteration strategy determined from the arguments for iterating over the points.Implementation note: seeIterationStrategyjavadoc for a method skeleton.- Specified by:
transformin interfaceorg.opengis.referencing.operation.MathTransform- Parameters:
srcPts- the array containing the source point coordinates.srcOff- the offset to the first point to be transformed in the source array.dstPts- the array into which the transformed point coordinates are returned. May be the same thansrcPts.dstOff- the offset to the location of the first transformed point that is stored in the destination array.numPts- the number of point objects to be transformed.- Throws:
org.opengis.referencing.operation.TransformException- if a point cannot be transformed. Some implementations will stop at the first failure, wile some other implementations will fill the untransformable points with Double.NaN values, continue and throw the exception only at end. Implementations that fall in the latter case should set the last completed transform tothis.
-
transform
public void transform(float[] srcPts, int srcOff, float[] dstPts, int dstOff, int numPts) throws org.opengis.referencing.operation.TransformException Transforms a list of coordinate tuples. The default implementation delegates totransform(double[], int, double[], int, int)using a temporary array of doubles.Implementation note: seeIterationStrategyjavadoc for a method skeleton.- Specified by:
transformin interfaceorg.opengis.referencing.operation.MathTransform- Parameters:
srcPts- the array containing the source point coordinates.srcOff- the offset to the first point to be transformed in the source array.dstPts- the array into which the transformed point coordinates are returned. May be the same thansrcPts.dstOff- the offset to the location of the first transformed point that is stored in the destination array.numPts- the number of point objects to be transformed.- Throws:
org.opengis.referencing.operation.TransformException- if a point cannot be transformed. Some implementations will stop at the first failure, wile some other implementations will fill the un-transformable points withFloat.NaNvalues, continue and throw the exception only at end. Implementations that fall in the latter case should set the last completed transform tothis.
-
transform
public void transform(double[] srcPts, int srcOff, float[] dstPts, int dstOff, int numPts) throws org.opengis.referencing.operation.TransformException Transforms a list of coordinate tuples. The default implementation delegates totransform(double[], int, double[], int, int)using a temporary array of doubles.- Specified by:
transformin interfaceorg.opengis.referencing.operation.MathTransform- Parameters:
srcPts- the array containing the source point coordinates.srcOff- the offset to the first point to be transformed in the source array.dstPts- the array into which the transformed point coordinates are returned.dstOff- the offset to the location of the first transformed point that is stored in the destination array.numPts- the number of point objects to be transformed.- Throws:
org.opengis.referencing.operation.TransformException- if a point cannot be transformed. Some implementations will stop at the first failure, wile some other implementations will fill the untransformable points with Float.NaN values, continue and throw the exception only at end. Implementations that fall in the latter case should set the last completed transform tothis.
-
transform
public void transform(float[] srcPts, int srcOff, double[] dstPts, int dstOff, int numPts) throws org.opengis.referencing.operation.TransformException Transforms a list of coordinate tuples. The default implementation delegates totransform(double[], int, double[], int, int)using a temporary array of doubles if necessary.- Specified by:
transformin interfaceorg.opengis.referencing.operation.MathTransform- Parameters:
srcPts- the array containing the source point coordinates.srcOff- the offset to the first point to be transformed in the source array.dstPts- the array into which the transformed point coordinates are returned.dstOff- the offset to the location of the first transformed point that is stored in the destination array.numPts- the number of point objects to be transformed.- Throws:
org.opengis.referencing.operation.TransformException- if a point cannot be transformed. Some implementations will stop at the first failure, wile some other implementations will fill the untransformable points with Double.NaN values, continue and throw the exception only at end. Implementations that fall in the latter case should set the last completed transform tothis.
-
derivative
public org.opengis.referencing.operation.Matrix derivative(org.opengis.geometry.DirectPosition point) throws org.opengis.referencing.operation.TransformException Gets the derivative of this transform at a point. The default implementation performs the following steps:- Ensure that the
pointdimension is equal to this math transform source dimensions. - Copy the coordinates in a temporary array and pass that array to the
transform(double[], int, double[], int, boolean)method, with thederivateboolean argument set totrue. - If the latter method returned a non-null matrix, returns that matrix.
Otherwise throws
TransformException.
- Specified by:
derivativein interfaceorg.opengis.referencing.operation.MathTransform- Parameters:
point- the coordinate tuple where to evaluate the derivative.- Returns:
- the derivative at the specified point (never
null). - Throws:
NullPointerException- if the derivative depends on coordinates andpointisnull.org.opengis.geometry.MismatchedDimensionException- ifpointdoes not have the expected dimension.org.opengis.referencing.operation.TransformException- if the derivative cannot be evaluated at the specified point.
- Ensure that the
-
inverse
public org.opengis.referencing.operation.MathTransform inverse() throws org.opengis.referencing.operation.NoninvertibleTransformExceptionReturns the inverse transform of this object. The default implementation returnsthisif this transform is an identity transform, or throws an exception otherwise. Subclasses should override this method.Implementation note: theAbstractMathTransform.Inverseinner class can be used as a base for inverse transform implementations.- Specified by:
inversein interfaceorg.opengis.referencing.operation.MathTransform- Throws:
org.opengis.referencing.operation.NoninvertibleTransformException
-
isInverseEquals
static boolean isInverseEquals(org.opengis.referencing.operation.MathTransform tr1, org.opengis.referencing.operation.MathTransform tr2) Returnstrueiftr1is the inverse oftr2. If this method is unsure, it conservatively returnsfalse. The transform that may be inverted istr1.- Parameters:
tr1- the transform to inverse.tr2- the transform that may be the inverse oftr1.- Returns:
- whether this transform is the inverse of the given transform. If unsure,
false.
-
tryConcatenate
protected org.opengis.referencing.operation.MathTransform tryConcatenate(boolean applyOtherFirst, org.opengis.referencing.operation.MathTransform other, org.opengis.referencing.operation.MathTransformFactory factory) throws org.opengis.util.FactoryException Concatenates or pre-concatenates in an optimized way this math transform with the given one, if possible. If an optimization is possible, a new math transform is created to perform the combined transformation. TheapplyOtherFirstvalue determines the transformation order as bellow:- If
applyOtherFirstistrue, then transforming a point p by the combined transform is equivalent to first transforming p byotherand then transforming the result bythis. - If
applyOtherFirstisfalse, then transforming a point p by the combined transform is equivalent to first transforming p bythisand then transforming the result byother.
null. In the latter case, the concatenation will be prepared byDefaultMathTransformFactoryusing a generic implementation.The default implementation returns the identity transform if the other transform is the inverse of this transform, or returns
nullotherwise. This method is ought to be overridden by subclasses capable of concatenating some combination of transforms in a special way.LinearTransformimplementations do not need to override this method since matrix multiplications will be handled automatically, and this method does not need to handle theisIdentity()case.- Parameters:
applyOtherFirst-trueif the transformation order isotherfollowed bythis, orfalseif the transformation order isthisfollowed byother.other- the other math transform to (pre-)concatenate with this transform.factory- the factory which is (indirectly) invoking this method, ornullif none.- Returns:
- the math transforms combined in an optimized way, or
nullif no such optimization is available. - Throws:
org.opengis.util.FactoryException- if an error occurred while combining the transforms.- Since:
- 0.8
- See Also:
- If
-
hashCode
public final int hashCode()Returns a hash value for this transform. This method invokescomputeHashCode()when first needed and caches the value for future invocations. Subclasses shall overridecomputeHashCode()instead of this method. -
computeHashCode
protected int computeHashCode()Computes a hash value for this transform. This method is invoked byhashCode()when first needed.- Returns:
- the hash code value. This value may change between different execution of the Apache SIS library.
-
equals
Compares the specified object with this math transform for strict equality. This method is implemented as below (omitting assertions):- Specified by:
equalsin interfaceLenientComparable- Overrides:
equalsin classObject- Parameters:
object- the object to compare with this transform.- Returns:
trueif the given object is a transform of the same class and using the same parameter values.- See Also:
-
equals
Compares the specified object with this math transform for equality. Two math transforms are considered equal if, given identical source positions, their transformed positions would be equal orapproximatelyequal. This method may conservatively returnsfalseif unsure.The default implementation returns
trueif the following conditions are met:objectis an instance of the same class thanthis. We require the same class because there is no interface for the various kinds of transform.- If the hash code value has already been computed for both instances, their values are the same (opportunist performance enhancement).
- The contextual parameters are equal according the given comparison mode.
- Specified by:
equalsin interfaceLenientComparable- Parameters:
object- the object to compare with this transform.mode- the strictness level of the comparison. Default toSTRICT.- Returns:
trueif the given object is considered equals to this math transform.- See Also:
-
beforeFormat
Given a transformation chain, replaces the elements aroundtransforms.get(index)transform by alternative objects to use when formatting WKT. The replacement is performed in-place in the given list.This method shall replace only the previous element and the few next elements that need to be changed as a result of the previous change. This method is not expected to continue the iteration after the changes that are of direct concern to this object.
This method is invoked only by
ConcatenatedTransform.getPseudoSteps()in order to get theParameterValueGroupof a map projection, or to format aPROJCSWKT.- Parameters:
transforms- the full chain of concatenated transforms.index- the index of this transform in thetransformschain.inverse- alwaysfalse, except if we are formatting the inverse transform.- Returns:
- index of this transform in the
transformschain after processing. - See Also:
-
formatTo
Formats the inner part of a Well Known Text version 1 (WKT 1) element. The default implementation formats all parameter values returned bygetParameterValues(). The parameter group name is used as the math transform name.Compatibility note:Param_MTis defined in the WKT 1 specification only. If the formatter convention is set to WKT 2, then this method silently uses the WKT 1 convention without raising an error (unless thisMathTransformcannot be formatted as valid WKT 1 neither).- Specified by:
formatToin classFormattableObject- Parameters:
formatter- the formatter to use.- Returns:
- the WKT element name, which is
"Param_MT"in the default implementation. - See Also:
-
unwrap
Unwraps the given object if it is expected to be an inverse transform. This is used for assertions only.
-