Issue #2255 Made TransformFactory loop to find true base crs in worldToCRS and worldToGrid.

Change-Id: I0cbb4ce0446ab4080ee0dc0f87c1de2ffc77a5a7

Former-commit-id: 5a4d880498 [formerly e85d66f4e5] [formerly 5a4d880498 [formerly e85d66f4e5] [formerly 3800aec9e6 [formerly d132d2ec2fc296518765d78b04ed942ec246b2e0]]]
Former-commit-id: 3800aec9e6
Former-commit-id: 352e21ffd3 [formerly d89d13d029]
Former-commit-id: ee68615786
This commit is contained in:
Max Schenkelberg 2013-08-15 15:50:40 -05:00
parent b75d942133
commit 6709242e36
2 changed files with 37 additions and 55 deletions

View file

@ -37,15 +37,12 @@ import org.geotools.coverage.grid.GeneralGridEnvelope;
import org.geotools.coverage.grid.GeneralGridGeometry;
import org.geotools.coverage.grid.GridGeometry2D;
import org.geotools.geometry.GeneralEnvelope;
import org.geotools.referencing.CRS;
import org.geotools.referencing.operation.DefaultMathTransformFactory;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.crs.GeneralDerivedCRS;
import org.opengis.referencing.datum.PixelInCell;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import com.raytheon.uf.common.geospatial.TransformFactory;
import com.raytheon.uf.common.serialization.adapters.GridGeometryAdapter;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
@ -709,18 +706,9 @@ public abstract class AbstractDescriptor extends ResourceGroup implements
protected void setupTransforms() throws Exception {
GeneralGridGeometry gridGeometry = getGridGeometry();
MathTransform worldToCRS = getWorldToCRSTransform(gridGeometry);
if (worldToCRS != null) {
MathTransform crsToPixel = gridGeometry.getGridToCRS(
PixelInCell.CELL_CENTER).inverse();
worldToPixel = new DefaultMathTransformFactory()
.createConcatenatedTransform(worldToCRS, crsToPixel);
pixelToWorld = worldToPixel.inverse();
} else {
pixelToWorld = null;
worldToPixel = null;
}
worldToPixel = TransformFactory.worldToGrid(gridGeometry,
PixelInCell.CELL_CENTER);
pixelToWorld = (worldToPixel != null ? worldToPixel.inverse() : null);
}
/*
@ -860,28 +848,4 @@ public abstract class AbstractDescriptor extends ResourceGroup implements
false), envelope);
}
/**
* Get the world to CRS transform used for {@link #worldToPixel(double[])}
* and {@link #pixelToWorld(double[])}
*
* @param gridGeometry
* @return The world to gridGeometry CRS transform or null if there is none
*/
public static MathTransform getWorldToCRSTransform(
GeneralGridGeometry gridGeometry) {
CoordinateReferenceSystem crs = gridGeometry.getEnvelope()
.getCoordinateReferenceSystem();
if (crs instanceof GeneralDerivedCRS) {
GeneralDerivedCRS projCRS = (GeneralDerivedCRS) crs;
CoordinateReferenceSystem worldCRS = projCRS.getBaseCRS();
try {
return CRS.findMathTransform(worldCRS, crs);
} catch (FactoryException e) {
statusHandler.handle(Priority.PROBLEM,
"Error setting up Math Transforms,"
+ " this descriptor may not work properly", e);
}
}
return null;
}
}

View file

@ -20,6 +20,7 @@
package com.raytheon.uf.common.geospatial;
import org.geotools.coverage.grid.GeneralGridGeometry;
import org.geotools.referencing.CRS;
import org.geotools.referencing.operation.DefaultMathTransformFactory;
import org.geotools.referencing.operation.transform.IdentityTransform;
import org.opengis.referencing.FactoryException;
@ -166,6 +167,27 @@ public class TransformFactory {
return factory.createConcatenatedTransform(mt1, mt2);
}
/**
* Constructs a transform from the "world" CRS of the crs passed in. null
* will be returned if no "world" crs exists.
*
* @param crs
* @return
* @throws FactoryException
*/
public static MathTransform worldToCRS(CoordinateReferenceSystem crs)
throws FactoryException {
CoordinateReferenceSystem worldCRS = crs;
while (worldCRS instanceof GeneralDerivedCRS) {
GeneralDerivedCRS derivedCRS = (GeneralDerivedCRS) worldCRS;
worldCRS = derivedCRS.getBaseCRS();
}
if (worldCRS != crs) {
return CRS.findMathTransform(worldCRS, crs);
}
return null;
}
/**
* Constructs a transform from the "world" CRS of the target geometry to the
* grid of the targetGeometry. Will return crsToGrid if no "world" CRS
@ -178,23 +200,19 @@ public class TransformFactory {
*/
public static MathTransform worldToGrid(GeneralGridGeometry targetGeometry,
PixelInCell cellType) throws FactoryException {
CoordinateReferenceSystem crs = targetGeometry.getEnvelope()
.getCoordinateReferenceSystem();
try {
if (crs instanceof GeneralDerivedCRS) {
GeneralDerivedCRS projCRS = (GeneralDerivedCRS) crs;
CoordinateReferenceSystem worldCRS = projCRS.getBaseCRS();
MathTransform worldToCRS = CRSCache.getInstance()
.findMathTransform(worldCRS, crs);
MathTransform crsToPixel = targetGeometry
.getGridToCRS(cellType).inverse();
return factory.createConcatenatedTransform(worldToCRS,
crsToPixel);
} else {
// No associated "world" CRS, go straight crs to grid
return targetGeometry.getGridToCRS(cellType).inverse();
CoordinateReferenceSystem crs = targetGeometry.getEnvelope()
.getCoordinateReferenceSystem();
MathTransform worldToCRS = worldToCRS(crs);
MathTransform worldToGrid = targetGeometry.getGridToCRS(cellType)
.inverse();
if (worldToCRS != null) {
worldToGrid = factory.createConcatenatedTransform(worldToCRS,
worldToGrid);
}
return worldToGrid;
} catch (FactoryException e) {
throw e;
} catch (Exception e) {
throw new FactoryException(e);
}