|
9 | 9 | import java.util.HashMap; |
10 | 10 | import java.util.Map; |
11 | 11 | import org.geotools.api.coverage.grid.GridCoverageReader; |
| 12 | +import org.geotools.api.referencing.operation.MathTransform; |
12 | 13 | import org.geotools.coverage.grid.GridCoverage2D; |
13 | 14 | import org.geotools.gce.geotiff.GeoTiffFormat; |
14 | 15 | import org.junit.jupiter.api.Test; |
@@ -71,4 +72,112 @@ public void testCogExport() throws Exception { |
71 | 72 | reader.dispose(); |
72 | 73 | } |
73 | 74 | } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testCogExportWithCenterAndScale() throws Exception { |
| 78 | + final Configuration config = configurationFactory.getConfig(getFile(BASE_DIR + "config.yaml")); |
| 79 | + |
| 80 | + final PJsonObject requestData = |
| 81 | + parseJSONObjectFromFile( |
| 82 | + MapCogExportOutputFormatTest.class, BASE_DIR + "requestData-center.json"); |
| 83 | + |
| 84 | + final OutputFormat format = this.outputFormat.get("cogMapOutputFormat"); |
| 85 | + |
| 86 | + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 87 | + |
| 88 | + format.print( |
| 89 | + new HashMap<>(), |
| 90 | + requestData, |
| 91 | + config, |
| 92 | + getFile(MapCogExportOutputFormatTest.class, BASE_DIR), |
| 93 | + getTaskDirectory(), |
| 94 | + outputStream); |
| 95 | + |
| 96 | + final GeoTiffFormat geoTiffFormat = new GeoTiffFormat(); |
| 97 | + final GridCoverageReader reader = |
| 98 | + geoTiffFormat.getReader(new ByteArrayInputStream(outputStream.toByteArray())); |
| 99 | + |
| 100 | + assertNotNull(reader); |
| 101 | + |
| 102 | + try { |
| 103 | + final GridCoverage2D coverage = (GridCoverage2D) reader.read(null); |
| 104 | + |
| 105 | + final MathTransform gridToCRS = coverage.getGridGeometry().getGridToCRS(); |
| 106 | + |
| 107 | + final double[] gridCenter = { |
| 108 | + coverage.getRenderedImage().getWidth() / 2.0, coverage.getRenderedImage().getHeight() / 2.0 |
| 109 | + }; |
| 110 | + |
| 111 | + final double[] worldCenter = new double[2]; |
| 112 | + |
| 113 | + gridToCRS.transform(gridCenter, 0, worldCenter, 0, 1); |
| 114 | + |
| 115 | + assertEquals(637395.386, worldCenter[0], 0.000001); |
| 116 | + assertEquals(5788326.345, worldCenter[1], 0.000001); |
| 117 | + |
| 118 | + } finally { |
| 119 | + reader.dispose(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testCogExportWithRotation() throws Exception { |
| 125 | + final Configuration config = configurationFactory.getConfig(getFile(BASE_DIR + "config.yaml")); |
| 126 | + |
| 127 | + final PJsonObject requestData = |
| 128 | + parseJSONObjectFromFile( |
| 129 | + MapCogExportOutputFormatTest.class, BASE_DIR + "requestData-center-rotation.json"); |
| 130 | + |
| 131 | + final OutputFormat format = this.outputFormat.get("cogMapOutputFormat"); |
| 132 | + |
| 133 | + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 134 | + |
| 135 | + format.print( |
| 136 | + new HashMap<>(), |
| 137 | + requestData, |
| 138 | + config, |
| 139 | + getFile(MapCogExportOutputFormatTest.class, BASE_DIR), |
| 140 | + getTaskDirectory(), |
| 141 | + outputStream); |
| 142 | + |
| 143 | + final GeoTiffFormat geoTiffFormat = new GeoTiffFormat(); |
| 144 | + final GridCoverageReader reader = |
| 145 | + geoTiffFormat.getReader(new ByteArrayInputStream(outputStream.toByteArray())); |
| 146 | + |
| 147 | + assertNotNull(reader); |
| 148 | + |
| 149 | + try { |
| 150 | + final GridCoverage2D coverage = (GridCoverage2D) reader.read(null); |
| 151 | + |
| 152 | + final MathTransform gridToCRS = coverage.getGridGeometry().getGridToCRS(); |
| 153 | + |
| 154 | + final double[] gridCenter = { |
| 155 | + coverage.getRenderedImage().getWidth() / 2.0, coverage.getRenderedImage().getHeight() / 2.0 |
| 156 | + }; |
| 157 | + |
| 158 | + final double[] worldCenter = new double[2]; |
| 159 | + |
| 160 | + gridToCRS.transform(gridCenter, 0, worldCenter, 0, 1); |
| 161 | + |
| 162 | + assertEquals(637395.386, worldCenter[0], 0.000001); |
| 163 | + assertEquals(5788326.345, worldCenter[1], 0.000001); |
| 164 | + |
| 165 | + final double[] gridRight = {gridCenter[0] + 100.0, gridCenter[1]}; |
| 166 | + |
| 167 | + final double[] worldRight = new double[2]; |
| 168 | + |
| 169 | + gridToCRS.transform(gridRight, 0, worldRight, 0, 1); |
| 170 | + |
| 171 | + final double expectedDistance = 100.0 * 3600.0 * 0.0254 / 72.0; |
| 172 | + |
| 173 | + final double expectedOffset = expectedDistance / Math.sqrt(2.0); |
| 174 | + |
| 175 | + assertEquals(expectedOffset, worldRight[0] - worldCenter[0], 0.000001); |
| 176 | + |
| 177 | + assertEquals(expectedOffset, worldRight[1] - worldCenter[1], 0.000001); |
| 178 | + |
| 179 | + } finally { |
| 180 | + reader.dispose(); |
| 181 | + } |
| 182 | + } |
74 | 183 | } |
0 commit comments