Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
migrated ntv2 transformation from deegree 3 for 4314 -> 4258
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbllmnn committed Mar 1, 2013
1 parent 6ade055 commit 4f71b40
Show file tree
Hide file tree
Showing 8 changed files with 1,362 additions and 33 deletions.
Binary file added lib/jgridshift-1.0-deegreeversion.jar
Binary file not shown.
55 changes: 35 additions & 20 deletions src/org/deegree/crs/transformations/TransformationFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.deegree.crs.transformations.coordinate.MatrixTransform;
import org.deegree.crs.transformations.coordinate.ProjectionTransform;
import org.deegree.crs.transformations.helmert.Helmert;
import org.deegree.crs.transformations.ntv2.NTv2Transformation;
import org.deegree.crs.transformations.polynomial.PolynomialTransformation;
import org.deegree.crs.utilities.Matrix;
import org.deegree.framework.log.ILogger;
Expand Down Expand Up @@ -247,10 +248,9 @@ public Transformation createFromCoordinateSystems( final CoordinateSystem source
result = createMatrixTransform( sourceCRS, targetCRS, matrix );
} else {
LOG.logDebug( "Concatenating the result, with the conversion matrices." );
result = concatenate(
createMatrixTransform( sourceCRS, sourceCRS, toStandardizedValues( sourceCRS, false ) ),
result, createMatrixTransform( targetCRS, targetCRS, toStandardizedValues( targetCRS,
true ) ) );
result = concatenate( createMatrixTransform( sourceCRS, sourceCRS, toStandardizedValues( sourceCRS, false ) ),
result,
createMatrixTransform( targetCRS, targetCRS, toStandardizedValues( targetCRS, true ) ) );
}
if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
StringBuilder output = new StringBuilder( "The resulting transformation chain: \n" );
Expand Down Expand Up @@ -290,8 +290,7 @@ private Matrix toStandardizedValues( CoordinateSystem sourceCRS, boolean invert
if ( !( Unit.RADIAN.equals( targetUnit ) || Unit.METRE.equals( targetUnit ) ) ) {
if ( !( targetUnit.canConvert( Unit.RADIAN ) || targetUnit.canConvert( Unit.METRE ) ) ) {
throw new TransformationException(
Messages.getMessage(
"CRS_TRANSFORMATION_NO_APLLICABLE_UNIT",
Messages.getMessage( "CRS_TRANSFORMATION_NO_APLLICABLE_UNIT",
targetUnit ) );
}
// lazy instantiation
Expand Down Expand Up @@ -408,7 +407,8 @@ private Transformation createTransformation( CompoundCRS sourceCRS, CompoundCRS
*/
if ( helmertTransformation != null ) {
// create a 2d->3d mapping.
final CRSTransformation axisAligned = createMatrixTransform( sourceGeographic, sourceGeocentric,
final CRSTransformation axisAligned = createMatrixTransform( sourceGeographic,
sourceGeocentric,
swapAxis( sourceGeographic,
GeographicCRS.WGS84 ) );
if ( LOG.isDebug() ) {
Expand Down Expand Up @@ -444,7 +444,8 @@ private Transformation createTransformation( CompoundCRS sourceCRS, CompoundCRS
*/
if ( helmertTransformation != null ) {
// create a 2d->3d mapping.
final CRSTransformation axisAligned = createMatrixTransform( targetGeocentric, targetGeographic,
final CRSTransformation axisAligned = createMatrixTransform( targetGeocentric,
targetGeographic,
swapAxis( GeographicCRS.WGS84,
targetGeographic ) );
final CRSTransformation geoCentricTransform = new GeocentricTransform( targetCRS, targetGeocentric );
Expand All @@ -462,9 +463,9 @@ private Transformation createTransformation( CompoundCRS sourceCRS, CompoundCRS

/**
* Creates a transformation between two geographic coordinate systems. This method is automatically invoked by
* {@link #createFromCoordinateSystems createFromCoordinateSystems(...)}. The default implementation can adjust
* axis order and orientation (e.g. transforming from <code>(NORTH,WEST)</code> to <code>(EAST,NORTH)</code>),
* performs units conversion and apply Bursa Wolf transformation if needed.
* {@link #createFromCoordinateSystems createFromCoordinateSystems(...)}. The default implementation can adjust axis
* order and orientation (e.g. transforming from <code>(NORTH,WEST)</code> to <code>(EAST,NORTH)</code>), performs
* units conversion and apply Bursa Wolf transformation if needed.
*
* @param sourceCRS
* Input coordinate system.
Expand All @@ -478,6 +479,9 @@ private Transformation createTransformation( final GeographicCRS sourceCRS, fina
throws TransformationException {
final GeodeticDatum sourceDatum = sourceCRS.getGeodeticDatum();
final GeodeticDatum targetDatum = targetCRS.getGeodeticDatum();
if(sourceCRS.getIdentifier().equals("EPSG:4314") && targetCRS.getIdentifier().equals("EPSG:4258")){
return new NTv2Transformation(sourceCRS, targetCRS, new Identifiable("urn:ogc:def:coordinateOperation:epsg::15948"), TransformationFactory.class.getResource("ntv2/beta2007.gsb" ) );
}
if ( sourceDatum.equals( targetDatum ) ) {
LOG.logDebug( "The datums of geographic (source): " + sourceCRS.getIdentifier()
+ " equals geographic (target): " + targetCRS.getIdentifier() + " returning null" );
Expand Down Expand Up @@ -596,8 +600,7 @@ private Transformation createTransformation( final GeographicCRS sourceCRS, fina
if ( helmertTransformation == null ) {
sourceGeocentric = targetCRS;
}
final CRSTransformation axisAlign = createMatrixTransform(
sourceCRS,
final CRSTransformation axisAlign = createMatrixTransform( sourceCRS,
sourceGeocentric,
swapAndRotateGeoAxis( sourceCRS, GeographicCRS.WGS84 ) );
if ( LOG.isDebug() ) {
Expand All @@ -616,9 +619,9 @@ private Transformation createTransformation( final GeographicCRS sourceCRS, fina

/**
* Creates a transformation between two projected coordinate systems. This method is automatically invoked by
* {@link #createFromCoordinateSystems createFromCoordinateSystems(...)}. The default implementation can adjust
* axis order and orientation. It also performs units conversion if it is the only extra change needed. Otherwise,
* it performs three steps:
* {@link #createFromCoordinateSystems createFromCoordinateSystems(...)}. The default implementation can adjust axis
* order and orientation. It also performs units conversion if it is the only extra change needed. Otherwise, it
* performs three steps:
*
* <ol>
* <li>Unproject <code>sourceCRS</code>.</li>
Expand Down Expand Up @@ -684,8 +687,8 @@ private Transformation createTransformation( final ProjectedCRS sourceCRS, final
* Input coordinate system.
* @param targetCRS
* Output coordinate system.
* @return A coordinate transformation from <code>sourceCRS</code> to <code>targetCRS</code> or
* <code>null</code> if {@link ProjectedCRS#getGeographicCRS()}.equals targetCRS.
* @return A coordinate transformation from <code>sourceCRS</code> to <code>targetCRS</code> or <code>null</code> if
* {@link ProjectedCRS#getGeographicCRS()}.equals targetCRS.
* @throws TransformationException
* if no transformation path has been found.
*/
Expand Down Expand Up @@ -891,8 +894,8 @@ private Transformation concatenateTransformations( Transformation first, Transfo
}

/*
* If one or both math transform are instance of {@link ConcatenatedTransform}, then concatenate <code>tr1</code>
* or <code>tr2</code> with one of step transforms.
* If one or both math transform are instance of {@link ConcatenatedTransform}, then concatenate
* <code>tr1</code> or <code>tr2</code> with one of step transforms.
*/
if ( first instanceof ConcatenatedTransform ) {
final ConcatenatedTransform ctr = (ConcatenatedTransform) first;
Expand Down Expand Up @@ -1066,4 +1069,16 @@ private Matrix swapAndRotateGeoAxis( final GeographicCRS sourceCRS, final Geogra
}
return matrix;
}

/**
* Create a new geographic crs with the same axis as the wgs.
*
* @param sourceCRS
* @return a new crs with same axis as wgs84.
*/
public final static GeographicCRS createWGSAlligned( GeographicCRS sourceCRS ) {
return new GeographicCRS( sourceCRS.getGeodeticDatum(), GeographicCRS.WGS84.getAxis(),
new Identifiable( "wgsalligned" ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import java.util.List;

import javax.vecmath.GMatrix;
import javax.vecmath.Point3d;

import org.deegree.crs.Identifiable;
Expand Down Expand Up @@ -151,4 +152,175 @@ public String getImplementationName() {
return "Concatenated-Transform";
}

/**
* Concatenates two existing transforms.
*
* @param first
* The first transform to apply to points.
* @param second
* The second transform to apply to points.
* @param keepIdentity
* true if identity {@link Transformation}s should be kept in the resulting {@link ConcatenatedTransform}
* . Default value is <code>false</code>.
* @return The concatenated transform.
*
*/
private static Transformation concatenateTransformations( Transformation first, Transformation second,
final boolean keepIdentity ) {
if ( first == null ) {
return second;
}
if ( second == null ) {
return first;
}
// if one of the two is an identity transformation, just return the other.
if ( !keepIdentity && first.isIdentity() ) {
return second;
}
if ( !keepIdentity && second.isIdentity() ) {
return first;
}

/*
* If one transform is the inverse of the other, just return an identitiy transform.
*/
if ( first.areInverse( second ) ) {
return null;
}

/*
* If both transforms use matrix, then we can create a single transform using the concatened matrix.
*/
if ( first instanceof MatrixTransform && second instanceof MatrixTransform ) {
GMatrix m1 = ( (MatrixTransform) first ).getMatrix();
GMatrix m2 = ( (MatrixTransform) second ).getMatrix();
if ( m1 == null ) {
if ( m2 == null ) {
// both matrices are null, just return the identiy matrix.
return new MatrixTransform( first.getSourceCRS(), first.getTargetCRS(),
new GMatrix( second.getTargetDimension() + 1,
first.getSourceDimension() + 1 ) );
}
return second;
} else if ( m2 == null ) {
return first;
}
if ( m2.getNumCol() != m1.getNumCol() || m2.getNumRow() != m1.getNumRow() ) {
return new ConcatenatedTransform( first, second );
}
m2.mul( m1 );

// m1.mul( m2 );
MatrixTransform result = new MatrixTransform( first.getSourceCRS(), second.getTargetCRS(), m2 );
// if ( result.isIdentity() ) {
// return null;
// }
return result;
}

/*
* If one or both math transform are instance of {@link ConcatenatedTransform}, then concatenate
* <code>tr1</code> or <code>tr2</code> with one of step transforms.
*/
if ( first instanceof ConcatenatedTransform ) {
final ConcatenatedTransform ctr = (ConcatenatedTransform) first;
first = ctr.getFirstTransform();
second = concatenateTransformations( ctr.getSecondTransform(), second, keepIdentity );
} else if ( second instanceof ConcatenatedTransform ) {
final ConcatenatedTransform ctr = (ConcatenatedTransform) second;
first = concatenateTransformations( first, ctr.getFirstTransform(), keepIdentity );
second = ctr.getSecondTransform();
}
// because of the concatenation one of the transformations may be null.
if ( first == null ) {
return second;
}
if ( second == null ) {
return first;
}

return new ConcatenatedTransform( first, second );
}

/**
* Concatenate two transformations.
*
* @param step1
* The first step, or <code>null</code> for the identity transform.
* @param step2
* The second step, or <code>null</code> for the identity transform.
* @param keepIdentity
* true if identity {@link Transformation}s should be kept in the resulting {@link ConcatenatedTransform}
* . Default value is <code>false</code>.
* @return A concatenated transform, or <code>null</code> if all arguments was nul.
*/
public static Transformation concatenate( final Transformation step1, final Transformation step2,
final boolean keepIdentity ) {
if ( step1 == null ) {
return step2;
}
if ( step2 == null ) {
return step1;
}
return concatenateTransformations( step1, step2, keepIdentity );
}

/**
* Concatenate two transformations.
*
* @param step1
* The first step, or <code>null</code> for the identity transform.
* @param step2
* The second step, or <code>null</code> for the identity transform.
* @return A concatenated transform, or <code>null</code> if all arguments was nul.
*/
public static Transformation concatenate( final Transformation step1, final Transformation step2 ) {
return concatenate( step1, step2, false );
}

/**
* Concatenate three transformations into one.
*
* @param step1
* The first step, or <code>null</code> for the identity transform.
* @param step2
* The second step, or <code>null</code> for the identity transform.
* @param step3
* The third step, or <code>null</code> for the identity transform.
* @param keepIdentity
* true if identity {@link Transformation}s should be kept in the resulting {@link ConcatenatedTransform}
* . Default value is <code>false</code>.
* @return A concatenated transform, or <code>null</code> if all arguments were <code>null</code>.
*/
public static Transformation concatenate( final Transformation step1, final Transformation step2,
final Transformation step3, final boolean keepIdentity ) {
if ( step1 == null ) {
return concatenate( step2, step3, keepIdentity );
}
if ( step2 == null ) {
return concatenate( step1, step3, keepIdentity );
}
if ( step3 == null ) {
return concatenate( step1, step2, keepIdentity );
}
return concatenateTransformations( step1, concatenateTransformations( step2, step3, keepIdentity ),
keepIdentity );
}

/**
* Concatenate three transformations into one.
*
* @param step1
* The first step, or <code>null</code> for the identity transform.
* @param step2
* The second step, or <code>null</code> for the identity transform.
* @param step3
* The third step, or <code>null</code> for the identity transform.
* @return A concatenated transform, or <code>null</code> if all arguments were <code>null</code>.
*/
public static Transformation concatenate( final Transformation step1, final Transformation step2,
final Transformation step3 ) {
return concatenate( step1, step2, step3, false );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

import org.deegree.crs.Identifiable;
import org.deegree.crs.coordinatesystems.CoordinateSystem;
import org.deegree.crs.exceptions.TransformationException;
import org.deegree.crs.utilities.Matrix;

/**
*
Expand Down Expand Up @@ -347,8 +349,8 @@ public boolean equals( final Object object ) {
*
* <p>
* For example, a square matrix of size 4&times;4. Is a three-dimensional transformation for incoming and outgoing
* coordinates. The transformed points <code>(x',y',z')</code> are computed as below (note that this computation
* is similar to {@link PerspectiveTransform}):
* coordinates. The transformed points <code>(x',y',z')</code> are computed as below (note that this computation is
* similar to {@link PerspectiveTransform}):
*
* <blockquote> <code>
* <pre>
Expand Down Expand Up @@ -478,4 +480,34 @@ public String getImplementationName() {
return transformationName;
}

/**
* Creates a Matrix transform from a matrix.
*
* @param sourceCRS
* used as the CRS for transforming ordinates on the matrix
* @param targetCRS
* used as the result CRS after transforming ordinates on the matrix
* @param matrix
* The matrix used to define the affine transform.
* @return A {@link MatrixTransform} mapping incoming ordinates given in the sourceCRS to the targetCRS.
* @throws TransformationException
* if the matrix is not affine.
*
*/
public static MatrixTransform createMatrixTransform( CoordinateSystem sourceCRS, CoordinateSystem targetCRS,
final Matrix matrix )
throws TransformationException {
if ( matrix == null ) {
return null;
}
if ( matrix.isAffine() ) {// Affine transform are square.
if ( matrix.getNumRow() == 3 && matrix.getNumCol() == 3 && !matrix.isIdentity() ) {
Matrix3d tmp = matrix.toAffineTransform();
return new MatrixTransform( sourceCRS, targetCRS, tmp );
}
return new MatrixTransform( sourceCRS, targetCRS, matrix );
}
throw new TransformationException( "Given matrix is not affine, cannot continue" );
}

}
Loading

0 comments on commit 4f71b40

Please sign in to comment.