sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jso...@apache.org
Subject [sis] branch geoapi-4.0 updated: Coverage : add setValues(WritabableRaster) method on GridCoverageBuilder
Date Thu, 19 Mar 2020 14:05:35 GMT
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 ebc9c64  Coverage : add setValues(WritabableRaster) method on GridCoverageBuilder
ebc9c64 is described below

commit ebc9c6485d1e931ca539ac860fe9b410d77e97b2
Author: jsorel <johann.sorel@geomatys.com>
AuthorDate: Thu Mar 19 15:04:32 2020 +0100

    Coverage : add setValues(WritabableRaster) method on GridCoverageBuilder
---
 .../sis/coverage/grid/GridCoverageBuilder.java     | 42 +++++++++++++++
 .../sis/coverage/grid/GridCoverageBuilderTest.java | 62 ++++++++++++++++++++++
 2 files changed, 104 insertions(+)

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 fe89f23..9cf1796 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
@@ -16,14 +16,18 @@
  */
 package org.apache.sis.coverage.grid;
 
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
 import java.awt.image.DataBuffer;
 import java.awt.image.RenderedImage;
+import java.awt.image.WritableRaster;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import org.apache.sis.coverage.SampleDimension;
 import org.apache.sis.internal.coverage.j2d.BufferedGridCoverage;
+import org.apache.sis.internal.coverage.j2d.ColorModelFactory;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.ArraysExt;
 import org.opengis.geometry.Envelope;
@@ -42,6 +46,7 @@ import org.opengis.referencing.operation.TransformException;
 public class GridCoverageBuilder {
 
     private List<SampleDimension> ranges;
+    private WritableRaster raster;
     private RenderedImage image;
     private DataBuffer buffer;
     private int bufferWidth = -1;
@@ -57,6 +62,22 @@ public class GridCoverageBuilder {
     public void setValues(RenderedImage image) {
         ArgumentChecks.ensureNonNull("image", image);
         this.image = image;
+        this.raster = null;
+        this.buffer = null;
+        this.bufferWidth = -1;
+        this.bufferHeight = -1;
+        this.bufferNbSample = -1;
+    }
+
+    /**
+     * Sets coverage data raster.
+     *
+     * @param raster The raster to be wrapped by {@code GridCoverage2D}, not {@code null}.
+     */
+    public void setValues(WritableRaster raster) {
+        ArgumentChecks.ensureNonNull("raster", raster);
+        this.image = null;
+        this.raster = raster;
         this.buffer = null;
         this.bufferWidth = -1;
         this.bufferHeight = -1;
@@ -137,6 +158,8 @@ public class GridCoverageBuilder {
      */
     public void setValues(DataBuffer data) {
         ArgumentChecks.ensureNonNull("data", data);
+        this.image = null;
+        this.raster = null;
         this.buffer = data;
         this.bufferWidth = -1;
         this.bufferHeight = -1;
@@ -144,6 +167,8 @@ public class GridCoverageBuilder {
     }
 
     private void setValues(DataBuffer data, int width, int height) {
+        this.image = null;
+        this.raster = null;
         this.buffer = data;
         this.bufferWidth = width;
         this.bufferHeight = height;
@@ -201,6 +226,23 @@ public class GridCoverageBuilder {
 
         if (image != null) {
             return new GridCoverage2D(grid, ranges, image);
+        } else if (raster != null) {
+
+            final int dataType = raster.getSampleModel().getDataType();
+            final int numBands = raster.getSampleModel().getNumBands();
+
+            List<SampleDimension> ranges = this.ranges;
+            if (ranges == null) {
+                ranges = new ArrayList<>(numBands);
+                for (int i = 0; i < numBands; i++) {
+                    ranges.add(new SampleDimension.Builder().setName(i).build());
+                }
+            }
+
+            final ColorModel colors = ColorModelFactory.createColorModel(ranges.toArray(new
SampleDimension[0]), 0, dataType, ColorModelFactory.GRAYSCALE);
+            final BufferedImage image = new BufferedImage(colors, raster, false, null);
+            return new GridCoverage2D(grid, this.ranges, image);
+
         } else if (buffer != null) {
 
             GridGeometry grid = this.grid;
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
index 489fa58..18eda9f 100644
--- 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
@@ -20,6 +20,7 @@ import java.awt.image.BufferedImage;
 import java.awt.image.DataBuffer;
 import java.awt.image.DataBufferInt;
 import java.awt.image.RenderedImage;
+import java.awt.image.WritableRaster;
 import org.apache.sis.coverage.SampleDimension;
 import org.apache.sis.geometry.GeneralEnvelope;
 import org.apache.sis.referencing.CommonCRS;
@@ -101,6 +102,67 @@ public class GridCoverageBuilderTest extends TestCase {
     }
 
     /**
+     * Tests {@link GridCoverageBuilder#setValues(Raster)}.
+     */
+    @Test
+    public void createFromRasterTest() {
+
+        final WritableRaster raster = new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR).getRaster();
+
+        final GridCoverageBuilder builder = new GridCoverageBuilder();
+        builder.setValues(raster);
+
+        { //sample dimensions and an undefined grid geometry should be created
+            GridCoverage coverage = builder.build();
+            Assert.assertEquals(3, 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();
+            builder.setRanges(r0, r1, r2);
+            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


Mime
View raw message