-
Notifications
You must be signed in to change notification settings - Fork 693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SEDONA-411] Add RS_Rotation #1061
Changes from 14 commits
0a8b1dd
da0505f
d9507d2
893d5fc
6d6c947
731f75c
2f2cc22
11f5b21
c043bd3
ad558f7
70ec512
7592fdb
6ee3e79
a89252d
c1f53aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import org.opengis.referencing.crs.CoordinateReferenceSystem; | ||
import org.opengis.referencing.operation.TransformException; | ||
|
||
import java.awt.image.RenderedImage; | ||
import java.util.Arrays; | ||
import java.util.Set; | ||
|
||
|
@@ -127,6 +128,70 @@ public static String getGeoReference(GridCoverage2D raster, String format) { | |
} | ||
} | ||
|
||
|
||
public static double[] getGeoTransform(GridCoverage2D raster) throws FactoryException { | ||
// size of a pixel along the transformed i axis | ||
double magnitudeI; | ||
|
||
// size of a pixel along the transformed j axis | ||
double magnitudeJ; | ||
|
||
// angle by which the raster is rotated (Radians positive clockwise) | ||
double thetaI; | ||
|
||
// angle from transformed i axis to transformed j axis (Radians positive counter-clockwise) | ||
double thetaIJ; | ||
|
||
RenderedImage renderedImage = raster.getRenderedImage(); | ||
|
||
// x ordinate of the upper-left corner of the upper-left pixel | ||
double offsetX = renderedImage.getTileGridXOffset(); | ||
|
||
// y ordinate of the upper-left corner of the upper-left pixel | ||
double offsetY = renderedImage.getTileGridYOffset(); | ||
|
||
double[] metadata = metadata(raster); | ||
double scaleX = metadata[4]; | ||
double scaleY = metadata[5]; | ||
double skewX = metadata[6]; | ||
double skewY = metadata[7]; | ||
|
||
// pixel size in i direction - west-east | ||
magnitudeI = Math.sqrt(scaleX * scaleX + skewY * skewY); | ||
|
||
// pixel size in j direction - north-south | ||
magnitudeJ = Math.sqrt(scaleY * scaleY + skewX * skewX); | ||
|
||
// Rotation | ||
thetaI = Math.acos(scaleX / magnitudeI); | ||
double thetaTest = Math.acos(skewY / magnitudeI); | ||
if (thetaTest < Math.PI / 2) { | ||
thetaI = -thetaI; | ||
} | ||
|
||
// Angular separation | ||
thetaIJ = Math.acos((((scaleX * skewX) + (skewY * scaleY)) / (magnitudeI * magnitudeJ))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can |
||
thetaTest = Math.acos(((-skewY * skewX) + (scaleX * scaleY)) / (magnitudeI * magnitudeJ)); | ||
if (thetaTest > Math.PI / 2) { | ||
thetaIJ = -thetaIJ; | ||
} | ||
|
||
double[] result = new double[6]; | ||
result[0] = magnitudeI; | ||
result[1] = magnitudeJ; | ||
result[2] = thetaI; | ||
result[3] = thetaIJ; | ||
result[4] = offsetX; | ||
result[5] = offsetY; | ||
|
||
return result; | ||
} | ||
|
||
public static double getRotation(GridCoverage2D raster) throws FactoryException { | ||
double rotation = getGeoTransform(raster)[2]; | ||
return rotation; | ||
} | ||
|
||
public static Geometry getGridCoord(GridCoverage2D raster, double x, double y) throws TransformException { | ||
int[] coords = RasterUtils.getGridCoordinatesFromWorld(raster, x, y); | ||
coords = Arrays.stream(coords).map(number -> number + 1).toArray(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,7 @@ | |
|
||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.junit.Assert.*; | ||
|
||
public class RasterAccessorsTest extends RasterTestBase | ||
{ | ||
|
@@ -60,6 +59,22 @@ public void testSrid() throws FactoryException { | |
assertEquals(4326, RasterAccessors.srid(multiBandRaster)); | ||
} | ||
|
||
@Test | ||
public void testRotation() throws IOException, FactoryException { | ||
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(2, 10, 15, 1, 2, 1, -2, 10, 10, 0); | ||
double actual = RasterAccessors.getRotation(emptyRaster); | ||
double expected = -1.4711276743037347; | ||
assertEquals(expected, actual, 1e-9); | ||
} | ||
|
||
@Test | ||
public void testGeoTransform() throws FactoryException { | ||
GridCoverage2D emptyRaster = RasterConstructors.makeEmptyRaster(1, 10, 15, 1, 2, 1, -1, 10, 10, 0); | ||
double[] actual = RasterAccessors.getGeoTransform(emptyRaster); | ||
double[] expected = new double[] {10.04987562112089, 10.04987562112089, -1.4711276743037347, -1.5707963267948966, 0.0, 0.0}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are getting 0 offsets for rasters with non-zero offsets, this is caused by the problem mentioned above. |
||
assertArrayEquals(expected, actual, 1e-9); | ||
} | ||
|
||
@Test | ||
public void testUpperLeftX() throws FactoryException { | ||
GridCoverage2D gridCoverage2D = RasterConstructors.makeEmptyRaster(1, 3, 4, 1,2, 5); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not correct.
offsetX
andoffsetY
should be directly retrieved from the upperLeftX and upperLeftY components of the raster metadata.