Merge "Issue #1279 add some comments to the grid wrap chaecking code." into development

Former-commit-id: d232355852 [formerly 936aaae5a4] [formerly 0152b058e5] [formerly d232355852 [formerly 936aaae5a4] [formerly 0152b058e5] [formerly 6f441fc85e [formerly 0152b058e5 [formerly faf4edee48f169841eb5c1ab3b4ad4dc28f1f338]]]]
Former-commit-id: 6f441fc85e
Former-commit-id: 7179e2fc3e [formerly cbb6deec7a] [formerly 40616ad4b8ef2ffe36fcb4caa18d67dc7b739576 [formerly 12f20ca32e]]
Former-commit-id: 61ae694d509ed331b7e187f6c7e3a3b9fd313fb4 [formerly 8fd45f3537]
Former-commit-id: 500e71e0f1
This commit is contained in:
Richard Peter 2012-10-25 12:44:10 -05:00 committed by Gerrit Code Review
commit d13491ec6a

View file

@ -130,18 +130,45 @@ public abstract class AbstractDataWrapper implements DataSource,
.getGridToCRS(PixelInCell.CELL_CENTER);
MathTransform crs2LatLon = CRS.findMathTransform(sourceCRS,
DefaultGeographicCRS.WGS84);
// Although theoretically any two points would yield the same
// result, nx is chosen as the x coordinate because it overcomes
// some of the normalization performed in geotools math transforms.
// For most math transforms, geotools will normalize the LatLon
// values into into the range centralMeridian +/- 180. In these
// cases the 360 degree shift performed later is completely
// worthless since geotools will normalize the points into the
// same range regardless of how it is shifted. For a worldwide grid
// the nx coordinate is guaranteed to be just slightly over the
// normalized range, so that when the transform normalizes it will
// use a point that is -360 degrees away from the original point.
// This means that even though the -360 degree shift we apply is
// worthless, the normalization process actually applies an
// equivelant shift that yields the correct result. The end result
// is that whether the transform normalizes or not there is always a
// shift of -360 degrees for all worldwide grids.
// Start with two points in grid space, one on each corner side of
// the y direction.
DirectPosition2D corner1 = new DirectPosition2D(nx, 0);
DirectPosition2D corner2 = new DirectPosition2D(nx, ny - 1);
// transform the points to crs space.
grid2crs.transform(corner1, corner1);
grid2crs.transform(corner2, corner2);
// and then to latLon space
crs2LatLon.transform(corner1, corner1);
crs2LatLon.transform(corner2, corner2);
// shift the points by 360 degrees because the goal is to know how
// many grid cells exist in one 360 roll of longitude.
corner1.x = corner1.x - 360;
corner2.x = corner2.x - 360;
// transform back to crs.
crs2LatLon.inverse().transform(corner1, corner1);
crs2LatLon.inverse().transform(corner2, corner2);
// and back to grid space
grid2crs.inverse().transform(corner1, corner1);
grid2crs.inverse().transform(corner2, corner2);
// the difference between the starting x value and the current x
// value is the number
int sourceWrapX = (int) (nx - corner1.x);
// In order to wrap then the transformed point x value should be
// on the other side of the grid and the y value should not have