This is an automated email from the ASF dual-hosted git repository.
jsorel pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
new 9d4fa26 Coverage : add GridCoverageBuilder tests
9d4fa26 is described below
commit 9d4fa26b363b0e2d04ac3c0e15da0c9a5a92b010
Author: jsorel <[email protected]>
AuthorDate: Wed Mar 18 10:15:39 2020 +0100
Coverage : add GridCoverageBuilder tests
---
.../sis/coverage/grid/GridCoverageBuilder.java | 131 +++++++++---------
.../coverage/j2d/BufferedGridCoverage.java | 15 ++-
.../sis/coverage/grid/GridCoverageBuilderTest.java | 150 +++++++++++++++++++++
.../apache/sis/test/suite/FeatureTestSuite.java | 1 +
4 files changed, 228 insertions(+), 69 deletions(-)
diff --git
a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageBuilder.java
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageBuilder.java
index d1d234b..fe89f23 100644
---
a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageBuilder.java
+++
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/GridCoverageBuilder.java
@@ -17,7 +17,6 @@
package org.apache.sis.coverage.grid;
import java.awt.image.DataBuffer;
-import java.awt.image.DataBufferInt;
import java.awt.image.RenderedImage;
import java.util.ArrayList;
import java.util.Arrays;
@@ -64,71 +63,71 @@ public class GridCoverageBuilder {
this.bufferNbSample = -1;
}
- /**
- * Creates a coverage from the given matrix.
- * This method copies the values from the given matrix to a new DataBuffer.
- * <p>
- * The coverage height will be the length of the {@code matrix} argument.
- * The coverage width will be the length of the first row, all rows are
expected
- * to have the same length.
- *
- * @param matrix The matrix data in a {@code [row][column]} layout.
- * @throws ArithmeticException if the buffer size exceeds the {@code int}
capacity.
- */
- public void setValues(int[][] matrix) throws ArithmeticException {
- final int height = matrix.length;
- final int width = matrix[0].length;
- final int[] datas = new int[StrictMath.toIntExact( ((long) height) *
width)];
- for (int i = 0, offset=0; i < matrix.length; i++,offset+=width) {
- System.arraycopy(matrix[i], 0, datas, offset, width);
- }
- final DataBuffer buffer = new DataBufferInt(datas, datas.length);
- setValues(buffer, width, height);
- }
-
- /**
- * Creates a coverage from the given matrix.
- * This method copies the values from the given matrix to a new DataBuffer.
- * <p>
- * The coverage height will be the length of the {@code matrix} argument.
- * The coverage width will be the length of the first row, all rows are
expected
- * to have the same length.
- *
- * @param matrix The matrix data in a {@code [row][column]} layout.
- * @throws ArithmeticException if the buffer size exceeds the {@code int}
capacity.
- */
- public void setValues(float[][] matrix) throws ArithmeticException {
- final int height = matrix.length;
- final int width = matrix[0].length;
- final int[] datas = new int[StrictMath.toIntExact( ((long) height) *
width)];
- for (int i = 0, offset=0; i < matrix.length; i++,offset+=width) {
- System.arraycopy(matrix[i], 0, datas, offset, width);
- }
- final DataBuffer buffer = new DataBufferInt(datas, datas.length);
- setValues(buffer, width, height);
- }
-
- /**
- * Creates a coverage from the given matrix.
- * This method copies the values from the given matrix to a new DataBuffer.
- * <p>
- * The coverage height will be the length of the {@code matrix} argument.
- * The coverage width will be the length of the first row, all rows are
expected
- * to have the same length.
- *
- * @param matrix The matrix data in a {@code [row][column]} layout.
- * @throws ArithmeticException if the buffer size exceeds the {@code int}
capacity.
- */
- public void setValues(double[][] matrix) throws ArithmeticException {
- final int height = matrix.length;
- final int width = matrix[0].length;
- final int[] datas = new int[StrictMath.toIntExact( ((long) height) *
width)];
- for (int i = 0, offset=0; i < matrix.length; i++,offset+=width) {
- System.arraycopy(matrix[i], 0, datas, offset, width);
- }
- final DataBuffer buffer = new DataBufferInt(datas, datas.length);
- setValues(buffer, width, height);
- }
+// /**
+// * Creates a coverage from the given matrix.
+// * This method copies the values from the given matrix to a new
DataBuffer.
+// * <p>
+// * The coverage height will be the length of the {@code matrix} argument.
+// * The coverage width will be the length of the first row, all rows are
expected
+// * to have the same length.
+// *
+// * @param matrix The matrix data in a {@code [row][column]} layout.
+// * @throws ArithmeticException if the buffer size exceeds the {@code
int} capacity.
+// */
+// public void setValues(int[][] matrix) throws ArithmeticException {
+// final int height = matrix.length;
+// final int width = matrix[0].length;
+// final int[] datas = new int[Math.multiplyExact(height,width)];
+// for (int i = 0, offset=0; i < matrix.length; i++,offset+=width) {
+// System.arraycopy(matrix[i], 0, datas, offset, width);
+// }
+// final DataBuffer buffer = new DataBufferInt(datas, datas.length);
+// setValues(buffer, width, height);
+// }
+//
+// /**
+// * Creates a coverage from the given matrix.
+// * This method copies the values from the given matrix to a new
DataBuffer.
+// * <p>
+// * The coverage height will be the length of the {@code matrix} argument.
+// * The coverage width will be the length of the first row, all rows are
expected
+// * to have the same length.
+// *
+// * @param matrix The matrix data in a {@code [row][column]} layout.
+// * @throws ArithmeticException if the buffer size exceeds the {@code
int} capacity.
+// */
+// public void setValues(float[][] matrix) throws ArithmeticException {
+// final int height = matrix.length;
+// final int width = matrix[0].length;
+// final float[] datas = new float[Math.multiplyExact(height,width)];
+// for (int i = 0, offset=0; i < matrix.length; i++,offset+=width) {
+// System.arraycopy(matrix[i], 0, datas, offset, width);
+// }
+// final DataBuffer buffer = new DataBufferFloat(datas, datas.length);
+// setValues(buffer, width, height);
+// }
+//
+// /**
+// * Creates a coverage from the given matrix.
+// * This method copies the values from the given matrix to a new
DataBuffer.
+// * <p>
+// * The coverage height will be the length of the {@code matrix} argument.
+// * The coverage width will be the length of the first row, all rows are
expected
+// * to have the same length.
+// *
+// * @param matrix The matrix data in a {@code [row][column]} layout.
+// * @throws ArithmeticException if the buffer size exceeds the {@code
int} capacity.
+// */
+// public void setValues(double[][] matrix) throws ArithmeticException {
+// final int height = matrix.length;
+// final int width = matrix[0].length;
+// final double[] datas = new double[Math.multiplyExact(height,width)];
+// for (int i = 0, offset=0; i < matrix.length; i++,offset+=width) {
+// System.arraycopy(matrix[i], 0, datas, offset, width);
+// }
+// final DataBuffer buffer = new DataBufferDouble(datas, datas.length);
+// setValues(buffer, width, height);
+// }
/**
* Creates a coverage from the given buffer.
diff --git
a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BufferedGridCoverage.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BufferedGridCoverage.java
index f11cf6d..061c079 100644
---
a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BufferedGridCoverage.java
+++
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/BufferedGridCoverage.java
@@ -16,7 +16,6 @@
*/
package org.apache.sis.internal.coverage.j2d;
-import java.util.Collection;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferDouble;
@@ -26,15 +25,15 @@ import java.awt.image.DataBufferShort;
import java.awt.image.DataBufferUShort;
import java.awt.image.RasterFormatException;
import java.awt.image.RenderedImage;
+import java.util.Collection;
import org.apache.sis.coverage.SampleDimension;
import org.apache.sis.coverage.grid.GridCoverage;
import org.apache.sis.coverage.grid.GridExtent;
import org.apache.sis.coverage.grid.GridGeometry;
+import org.apache.sis.coverage.grid.IllegalGridGeometryException;
import org.apache.sis.coverage.grid.ImageRenderer;
import org.apache.sis.util.ArgumentChecks;
import org.apache.sis.util.resources.Errors;
-
-// Branch-specific imports
import org.opengis.coverage.CannotEvaluateException;
@@ -67,6 +66,16 @@ public class BufferedGridCoverage extends GridCoverage {
super(grid, bands);
this.data = data;
ArgumentChecks.ensureNonNull("data", data);
+
+ //verify buffer size
+ GridExtent extent = grid.getExtent();
+ long expectedSize = extent.getSize(0) * bands.size();
+ for (int i = 1; i <extent.getDimension(); i++) {
+ expectedSize *= extent.getSize(i);
+ }
+ if (data.getSize() < expectedSize) {
+ throw new IllegalGridGeometryException("Expecting a buffer size of
at least " + expectedSize + " to contain all samples from given grid geometry,
but buffer is only " +data.getSize());
+ }
}
/**
diff --git
a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverageBuilderTest.java
b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverageBuilderTest.java
new file mode 100644
index 0000000..489fa58
--- /dev/null
+++
b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/GridCoverageBuilderTest.java
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.coverage.grid;
+
+import java.awt.image.BufferedImage;
+import java.awt.image.DataBuffer;
+import java.awt.image.DataBufferInt;
+import java.awt.image.RenderedImage;
+import org.apache.sis.coverage.SampleDimension;
+import org.apache.sis.geometry.GeneralEnvelope;
+import org.apache.sis.referencing.CommonCRS;
+import org.apache.sis.test.TestCase;
+import org.apache.sis.util.NullArgumentException;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ * Tests the {@link GridCoverageBuilder} helper class.
+ *
+ * @author Johann Sorel (Geomatys)
+ * @version 1.1
+ * @since 1.1
+ */
+public class GridCoverageBuilderTest extends TestCase {
+
+ /**
+ * Tests {@link GridCoverageBuilder#setValues(Image)}.
+ */
+ @Test
+ public void createFromImageTest() {
+
+ final RenderedImage image = new BufferedImage(10, 10,
BufferedImage.TYPE_INT_ARGB);
+
+ final GridCoverageBuilder builder = new GridCoverageBuilder();
+ builder.setValues(image);
+
+ { //sample dimensions and an undefined grid geometry should be created
+ GridCoverage coverage = builder.build();
+ Assert.assertEquals(4, coverage.getSampleDimensions().size());
+
Assert.assertFalse(coverage.getGridGeometry().isDefined(GridGeometry.CRS));
+
Assert.assertFalse(coverage.getGridGeometry().isDefined(GridGeometry.ENVELOPE));
+
Assert.assertFalse(coverage.getGridGeometry().isDefined(GridGeometry.GRID_TO_CRS));
+ }
+
+ { //should cause an exceptin number of sample dimensions do not match
+ builder.setRanges(new
SampleDimension.Builder().setName(0).build());
+ try {
+ GridCoverage coverage = builder.build();
+ Assert.fail("Wrong number of sample dimensions, build should
fail");
+ } catch (IllegalArgumentException ex) {
+ //ok
+ }
+ }
+
+ { //number of sample matches, should build correctly
+ final SampleDimension r0 = new
SampleDimension.Builder().setName(0).build();
+ final SampleDimension r1 = new
SampleDimension.Builder().setName(1).build();
+ final SampleDimension r2 = new
SampleDimension.Builder().setName(2).build();
+ final SampleDimension r3 = new
SampleDimension.Builder().setName(3).build();
+ builder.setRanges(r0, r1, r2, r3);
+ GridCoverage coverage = builder.build();
+ }
+
+ { //should cause an exceptin extent size do not match
+ final GeneralEnvelope env = new
GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
+ env.setRange(0, 0, 10);
+ env.setRange(1, 0, 10);
+ final GridGeometry grid = new GridGeometry(new GridExtent(8, 6),
env);
+ builder.setDomain(grid);
+ try {
+ GridCoverage coverage = builder.build();
+ Assert.fail("Wrong extent size, build should fail");
+ } catch (IllegalArgumentException ex) {
+ //ok
+ }
+ }
+
+ { //extent size matches, should build correctly
+ final GeneralEnvelope env = new
GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
+ env.setRange(0, 0, 10);
+ env.setRange(1, 0, 10);
+ final GridGeometry grid = new GridGeometry(new GridExtent(10, 10),
env);
+ builder.setDomain(grid);
+ GridCoverage coverage = builder.build();
+ }
+ }
+
+ /**
+ * Tests {@link GridCoverageBuilder#setValues(DataBuffer)}.
+ */
+ @Test
+ public void createFromBufferTest() {
+
+ final DataBuffer buffer = new DataBufferInt(new int[]{1,2,3,4,5,6},6);
+
+ final GridCoverageBuilder builder = new GridCoverageBuilder();
+ builder.setValues(buffer);
+ final SampleDimension r0 = new
SampleDimension.Builder().setName(0).build();
+ builder.setRanges(r0);
+
+ { // size is undefined, build should fail
+ try {
+ GridCoverage coverage = builder.build();
+ Assert.fail("Extent is undefined, build should fail");
+ } catch (NullArgumentException ex) {
+ //ok
+ }
+ }
+
+ { //should cause an exceptin extent size do not match
+ final GeneralEnvelope env = new
GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
+ env.setRange(0, 0, 10);
+ env.setRange(1, 0, 10);
+ final GridGeometry grid = new GridGeometry(new GridExtent(8, 6),
env);
+ builder.setDomain(grid);
+ try {
+ GridCoverage coverage = builder.build();
+ Assert.fail("Wrong extent size, build should fail");
+ } catch (IllegalGridGeometryException ex) {
+ //ok
+ }
+ }
+
+ { //extent size matches, should build correctly
+ final GeneralEnvelope env = new
GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
+ env.setRange(0, 0, 10);
+ env.setRange(1, 0, 10);
+ final GridGeometry grid = new GridGeometry(new GridExtent(3, 2),
env);
+ builder.setDomain(grid);
+ GridCoverage coverage = builder.build();
+ }
+
+ }
+
+}
diff --git
a/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
b/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
index 7fe6dd4..355cc3e 100644
---
a/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
+++
b/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
@@ -92,6 +92,7 @@ import org.junit.runners.Suite;
org.apache.sis.coverage.grid.FractionalGridCoordinatesTest.class,
org.apache.sis.coverage.grid.ReshapedImageTest.class,
org.apache.sis.coverage.grid.GridCoverage2DTest.class,
+ org.apache.sis.coverage.grid.GridCoverageBuilderTest.class,
org.apache.sis.internal.coverage.j2d.BandedSampleConverterTest.class,
org.apache.sis.internal.coverage.j2d.BufferedGridCoverageTest.class,