Class TransformedPosition
- Object
-
- AbstractPosition
-
- GeneralPosition
-
- TransformedPosition
-
- All Implemented Interfaces:
Serializable
,Cloneable
,Position
,Cloneable
public class TransformedPosition extends GeneralPosition
A direct position capable to transform a point between an arbitrary CRS and its own CRS. This class caches the last transform used in order to improve the performances when the source and target CRS don't change often. Using this class is faster than invokingCoordinateOperationFactory.createOperation(sourceCRS, targetCRS)
for every points.-
Note 1: This class is advantageous on a performance point of view only if the same instance of
TransformedPosition
is used for transforming many points between arbitrary CRS and this position CRS. -
Note 2: This convenience class is useful when the source and target CRS are not likely to change often. If you are sure that the source and target CRS will not change at all for a given set of positions, then using
CoordinateOperation
directly gives better performances. This is becauseTransformedPosition
checks if the CRS changed before every transformations, which may be costly. -
Note 3: This class is called Transformed Direct Position because it is more commonly used for transforming many points from arbitrary CRS to a common CRS (using the
transform(Position)
method) than the other way around.
MyClass
needs to perform its internal working in some particular CRS, but we want robust API that adjusts itself to whatever CRS the client happen to use.MyClass
could be written as below:public class MyClass { private static final CoordinateReferenceSystem PUBLIC_CRS = ... private static final CoordinateReferenceSystem INTERNAL_CRS = ... private final TransformedPosition myPosition = new TransformedPosition(PUBLIC_CRS, INTERNAL_CRS, null); public void setPosition(DirectPosition position) throws TransformException { // The position CRS is usually PUBLIC_CRS, but code below will work even if it is not. myPosition.transform(position); } public DirectPosition getPosition() throws TransformException { return myPosition.inverseTransform(PUBLIC_CRS); } }
- Since:
- 2.2
- Author:
- Martin Desruisseaux (IRD)
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class GeneralPosition
ordinates
-
-
Constructor Summary
Constructors Constructor Description TransformedPosition()
Creates a new direct position initialized with the WGS84 CRS.TransformedPosition(CoordinateReferenceSystem sourceCRS, CoordinateReferenceSystem targetCRS, Hints hints)
Creates a new position which will contains the result of coordinate transformations fromsourceCRS
totargetCRS
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Position
inverseTransform()
Returns a new point with the same coordinates than this one, but transformed in thesourceCRS
given at construction time.Position
inverseTransform(CoordinateReferenceSystem crs)
Returns a new point with the same coordinates than this one, but transformed in the given CRS.void
setCoordinateReferenceSystem(CoordinateReferenceSystem crs)
Sets the coordinate reference system in which the coordinate is given.void
transform(Position position)
Transforms a given position and stores the result in this object.-
Methods inherited from class GeneralPosition
clone, getCoordinate, getCoordinateReferenceSystem, getDimension, getOrdinate, hashCode, setLocation, setLocation, setLocation, setOrdinate, toPoint2D
-
Methods inherited from class AbstractPosition
checkCoordinateReferenceSystemDimension, equals, getDirectPosition, setPosition, toString
-
-
-
-
Constructor Detail
-
TransformedPosition
public TransformedPosition()
Creates a new direct position initialized with the WGS84 CRS.- Since:
- 2.3
-
TransformedPosition
public TransformedPosition(CoordinateReferenceSystem sourceCRS, CoordinateReferenceSystem targetCRS, Hints hints) throws FactoryRegistryException
Creates a new position which will contains the result of coordinate transformations fromsourceCRS
totargetCRS
. The CRS associated with this position will be initially set totargetCRS
.- Parameters:
sourceCRS
- The default CRS to be used by thetransform
(position)position
has a null associated CRS. ThissourceCRS
argument may benull
, in which case it is assumed the same thantargetCRS
.targetCRS
- The CRS associated with this position. Used for every coordinate transformations until the next call tosetCoordinateReferenceSystem
orsetLocation
. This argument can not be null.hints
- The set of hints to use for fetching aCoordinateOperationFactory
, ornull
if none.- Throws:
IllegalArgumentException
- iftargetCRS
wasnull
.FactoryRegistryException
- if no coordinate operation factory can be found for the specified hints.- Since:
- 2.3
-
-
Method Detail
-
setCoordinateReferenceSystem
public void setCoordinateReferenceSystem(CoordinateReferenceSystem crs) throws MismatchedDimensionException
Sets the coordinate reference system in which the coordinate is given. The given CRS will be used as:- the target CRS for every call to
transform(Position)
- the source CRS for every call to
inverseTransform(CoordinateReferenceSystem)
- Overrides:
setCoordinateReferenceSystem
in classGeneralPosition
- Parameters:
crs
- The new CRS for this direct position.- Throws:
MismatchedDimensionException
- if the specified CRS doesn't have the expected number of dimensions.
- the target CRS for every call to
-
transform
public void transform(Position position) throws TransformException
Transforms a given position and stores the result in this object.-
The source CRS is the CRS associated with the given position, or the
sourceCRS
argument given at construction time if and only if the CRS associated withposition
is null. -
The target CRS is the CRS associated with this position. This is always the
targetCRS
argument given at construction time or by the last call tosetCoordinateReferenceSystem
.
- Parameters:
position
- A position using an arbitrary CRS. This object will not be modified.- Throws:
TransformException
- if a coordinate transformation was required and failed.
-
-
inverseTransform
public Position inverseTransform(CoordinateReferenceSystem crs) throws TransformException
Returns a new point with the same coordinates than this one, but transformed in the given CRS. This method never returnsthis
, so the returned point usually doesn't need to be cloned.- Parameters:
crs
- The CRS for the position to be returned.- Returns:
- The same position than
this
, but transformed in the specified CRS. - Throws:
TransformException
- if a coordinate transformation was required and failed.- Since:
- 2.3
-
inverseTransform
public Position inverseTransform() throws TransformException
Returns a new point with the same coordinates than this one, but transformed in thesourceCRS
given at construction time. This method never returnsthis
, so the returned point usually doesn't need to be cloned.- Returns:
- The same position than
this
, but transformed in the source CRS. - Throws:
TransformException
- if a coordinate transformation was required and failed.- Since:
- 2.3
-
-