org.geotools.geometry
Class TransformedDirectPosition

java.lang.Object
  extended byorg.geotools.geometry.GeneralDirectPosition
      extended byorg.geotools.geometry.TransformedDirectPosition
All Implemented Interfaces:
java.lang.Cloneable, org.opengis.util.Cloneable, org.opengis.spatialschema.geometry.DirectPosition, org.opengis.spatialschema.geometry.geometry.Position, java.io.Serializable

public class TransformedDirectPosition
extends GeneralDirectPosition

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 invoking CoordinateOperationFactory.createOperation(sourceCRS, targetCRS) for every points.

Note 1: This class is advantageous on a performance point of view only if the same instance of is used for transforming many points between arbitrary CRS and the CRS of this position.

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 are frozen for a given set of positions, then using CoordinateOperation directly gives much better performances. This is because checks if the CRS changed before every transformations, which may be costly.

Note 3: This class is called Transformed Direct Position because its more commonly used for transforming many points from arbitrary CRS to a common CRS (using the transform(DirectPosition) method) than the other way around.

This class usually don't appears in a public API. It is more typicaly used as a helper private field in some more complex class. For example suppose that 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. could be written as below:

 public class MyClass {
     private static final CoordinateReferenceSystem INTERNAL_CRS = ...
     private static final CoordinateReferenceSystem   PUBLIC_CRS = ...

     private final TransformedDirectPosition myPosition = 
             new TransformedDirectPosition(INTERNAL_CRS, null);

     public DirectPosition getPosition() throws TransformException {
         return myPosition.transform(PUBLIC_CRS);
     }

     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);
     }
 }
 

Since:
2.2
Version:
$Id: TransformedDirectPosition.java 18471 2006-03-06 13:10:10Z desruisseaux $
Author:
Martin Desruisseaux
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.geotools.geometry.GeneralDirectPosition
ordinates
 
Constructor Summary
TransformedDirectPosition()
          Creates a new direct position with the WGS84 CRS.
TransformedDirectPosition(org.opengis.referencing.crs.CoordinateReferenceSystem crs, Hints hints)
          Creates a new direct position with the specified coordinate reference system.
 
Method Summary
 void setCoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem crs)
          Sets the coordinate reference system in which the coordinate is given.
 org.opengis.spatialschema.geometry.DirectPosition transform(org.opengis.referencing.crs.CoordinateReferenceSystem targetCRS)
          Returns a new point with the same coordinates than this one, but transformed in the given CRS.
 void transform(org.opengis.spatialschema.geometry.DirectPosition position)
          Transforms a given position and stores the result in this object.
 
Methods inherited from class org.geotools.geometry.GeneralDirectPosition
clone, getCoordinateReferenceSystem, getCoordinates, getDimension, getOrdinate, getPosition, hashCode, setLocation, setLocation, setLocation, setOrdinate, toPoint2D, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TransformedDirectPosition

public TransformedDirectPosition()
Creates a new direct position with the WGS84 CRS.

Since:
2.3

TransformedDirectPosition

public TransformedDirectPosition(org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                 Hints hints)
Creates a new direct position with the specified coordinate reference system.

Parameters:
crs - The CRS for this direct position.
hints - The set of hints to use for fetching a CoordinateOperationFactory, or if none.
Method Detail

setCoordinateReferenceSystem

public void setCoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem crs)
                                  throws org.opengis.spatialschema.geometry.MismatchedDimensionException
Sets the coordinate reference system in which the coordinate is given. The given CRS will be used as:

Overrides:
setCoordinateReferenceSystem in class GeneralDirectPosition
Parameters:
crs - The new CRS for this direct position.
Throws:
org.opengis.spatialschema.geometry.MismatchedDimensionException - if the specified CRS doesn't have the expected number of dimensions.

transform

public org.opengis.spatialschema.geometry.DirectPosition transform(org.opengis.referencing.crs.CoordinateReferenceSystem targetCRS)
                                                            throws org.opengis.referencing.operation.TransformException
Returns a new point with the same coordinates than this one, but transformed in the given CRS. This method never returns , so the returned point usually doesn't need to be cloned.

Parameters:
targetCRS - the target CRS.
Returns:
The same position than , but transformed in the specified target CRS.
Throws:
org.opengis.referencing.operation.TransformException - if a coordinate transformation was required and failed.

transform

public void transform(org.opengis.spatialschema.geometry.DirectPosition position)
               throws org.opengis.referencing.operation.TransformException
Transforms a given position and stores the result in this object. The source CRS is the CRS of the given position. The target CRS is the CRS of this position.

Parameters:
position - A position using an arbitrary CRS. This object will not be modified.
Throws:
org.opengis.referencing.operation.TransformException - if a coordinate transformation was required and failed.


Copyright © GeoTools. All Rights Reserved.