sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/02: Improve error messages if an image can not be produced.
Date Mon, 07 Jan 2019 20:12:50 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit c0798640e7082fdeb66d7581030c8374a555b2c5
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Mon Jan 7 11:56:57 2019 +0100

    Improve error messages if an image can not be produced.
---
 .../apache/sis/coverage/grid/ImageRenderer.java    | 29 +++++++++++++++++++---
 .../org/apache/sis/internal/netcdf/Resources.java  |  5 ++++
 .../sis/internal/netcdf/Resources.properties       |  1 +
 .../sis/internal/netcdf/Resources_fr.properties    |  1 +
 .../apache/sis/storage/netcdf/GridResource.java    |  2 +-
 .../java/org/apache/sis/storage/netcdf/Image.java  | 16 +++++++++---
 6 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
index 8f95bc0..2d6b4a9 100644
--- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
+++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
@@ -55,12 +55,33 @@ import org.apache.sis.math.Vector;
  * computes automatically the offsets from that position to the position of the first value
included
  * in the {@code sliceExtent} given to the constructor.</p>
  *
- * <p>Current implementation constructs only images made of a single tile.
- * Support for tiled images will be added in a future version.</p>
+ * <div class="note"><b>Usage example:</b>
+ * {@preformat java
+ *     class MyResource extends GridCoverage {
+ *         &#64;Override
+ *         public RenderedImage render(GridExtent sliceExtent) {
+ *             try {
+ *                 ImageRenderer renderer = new ImageRenderer(this, sliceExtent);
+ *                 renderer.setData(data);
+ *                 return renderer.image();
+ *             } catch (IllegalArgumentException | ArithmeticException | RasterFormatException
e) {
+ *                 throw new CannotEvaluateException("Can not create an image.", e);
+ *             }
+ *         }
+ *     }
+ * }
+ * </div>
+ *
+ * <div class="section">Limitations</div>
+ * Current implementation constructs only images made of a single tile.
+ * Support for tiled images will be added in a future version.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @version 1.0
- * @since   1.0
+ *
+ * @see GridCoverage#render(GridExtent)
+ *
+ * @since 1.0
  * @module
  */
 public class ImageRenderer {
@@ -327,6 +348,7 @@ public class ImageRenderer {
      *
      * @return the raster.
      * @throws IllegalStateException if no {@code setData(…)} method has been invoked before
this method call.
+     * @throws RasterFormatException if a call to a {@link WritableRaster} factory method
failed.
      */
     public WritableRaster raster() {
         if (buffer == null) {
@@ -342,6 +364,7 @@ public class ImageRenderer {
      *
      * @return the image.
      * @throws IllegalStateException if no {@code setData(…)} method has been invoked before
this method call.
+     * @throws RasterFormatException if a call to a {@link WritableRaster} factory method
failed.
      */
     public RenderedImage image() {
         WritableRaster raster = raster();
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.java
index 5ce117b..2e81e3a 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.java
@@ -81,6 +81,11 @@ public final class Resources extends IndexedResourceBundle {
         public static final short CanNotCreateGridGeometry_3 = 12;
 
         /**
+         * Can not render an image for “{0}”. The reason is: {1}
+         */
+        public static final short CanNotRender_2 = 14;
+
+        /**
          * Can not use UCAR library for netCDF format. Fallback on Apache SIS implementation.
          */
         public static final short CanNotUseUCAR = 4;
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.properties
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.properties
index c2f0084..a8062b0 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.properties
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources.properties
@@ -23,6 +23,7 @@ AmbiguousAxisDirection_4          = NetCDF file \u201c{0}\u201d provides
an ambi
 CanNotComputeVariablePosition_2   = Can not compute data location for \u201c{1}\u201d variable
in the \u201c{0}\u201d netCDF file.
 CanNotCreateCRS_3                 = Can not create the Coordinate Reference System for grid
geometry \u201c{1}\u201d in the \u201c{0}\u201d netCDF file. The reason is: {2}
 CanNotCreateGridGeometry_3        = Can not create the grid geometry \u201c{1}\u201d in the
\u201c{0}\u201d netCDF file. The reason is: {2}
+CanNotRender_2                    = Can not render an image for \u201c{0}\u201d. The reason
is: {1}
 CanNotUseUCAR                     = Can not use UCAR library for netCDF format. Fallback
on Apache SIS implementation.
 DimensionNotFound_3               = Dimension \u201c{2}\u201d declared by attribute \u201c{1}\u201d
is not found in the \u201c{0}\u201d file.
 DuplicatedReference_2             = Duplicated reference to \u201c{1}\u201d in netCDF file
\u201c{0}\u201d.
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources_fr.properties
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources_fr.properties
index 8a8a578..6d5c18d 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources_fr.properties
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Resources_fr.properties
@@ -28,6 +28,7 @@ AmbiguousAxisDirection_4          = Le fichier netCDF \u00ab\u202f{0}\u202f\u00b
 CanNotComputeVariablePosition_2   = Ne peut pas calculer la position des donn\u00e9es de
la variable \u00ab\u202f{1}\u202f\u00bb dans le fichier netCDF \u00ab\u202f{0}\u202f\u00bb.
 CanNotCreateCRS_3                 = Ne peut pas cr\u00e9er le syst\u00e8me de r\u00e9f\u00e9rence
des coordonn\u00e9es pour la g\u00e9om\u00e9trie de grille \u00ab\u202f{1}\u202f\u00bb dans
le fichier netCDF \u00ab\u202f{0}\u202f\u00bb. La raison est\u2008: {2}
 CanNotCreateGridGeometry_3        = Ne peut pas cr\u00e9er la g\u00e9om\u00e9trie de grille
\u00ab\u202f{1}\u202f\u00bb dans le fichier netCDF \u00ab\u202f{0}\u202f\u00bb. La raison
est\u2008: {2}
+CanNotRender_2                    = Ne peut pas produire une image pour \u00ab\u202f{0}\u202f\u00bb.
La raison est\u2008: {1}
 CanNotUseUCAR                     = Ne peut pas utiliser la biblioth\u00e8que de l\u2019UCAR
pour le format netCDF. L\u2019impl\u00e9mentation de Apache SIS sera utilis\u00e9e \u00e0
la place.
 DimensionNotFound_3               = La dimension \u00ab\u202f{2}\u202f\u00bb d\u00e9clar\u00e9e
par l\u2019attribut \u00ab\u202f{1}\u202f\u00bb n\u2019a pas \u00e9t\u00e9 trouv\u00e9e dans
le fichier \u00ab\u202f{0}\u202f\u00bb.
 DuplicatedReference_2             = R\u00e9f\u00e9rence vers \u00ab\u202f{1}\u202f\u00bb
dupliqu\u00e9e dans le fichier netCDF \u00ab\u202f{0}\u202f\u00bb.
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
index 1d40374..8c388ee 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
@@ -411,7 +411,7 @@ final class GridResource extends AbstractGridResource implements ResourceOnFileS
         if (imageBuffer == null) {
             throw new DataStoreContentException(Errors.format(Errors.Keys.UnsupportedType_1,
dataType.name()));
         }
-        return new Image(domain, UnmodifiableArrayList.wrap(selected), imageBuffer);
+        return new Image(domain, UnmodifiableArrayList.wrap(selected), imageBuffer, first.getName());
     }
 
     /**
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/Image.java b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/Image.java
index 845eaf3..4190b49 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/Image.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/Image.java
@@ -19,12 +19,14 @@ package org.apache.sis.storage.netcdf;
 import java.util.List;
 import java.awt.image.DataBuffer;
 import java.awt.image.RenderedImage;
+import java.awt.image.RasterFormatException;
 import org.opengis.coverage.CannotEvaluateException;
 import org.apache.sis.coverage.SampleDimension;
 import org.apache.sis.coverage.grid.GridCoverage;
 import org.apache.sis.coverage.grid.GridGeometry;
 import org.apache.sis.coverage.grid.GridExtent;
 import org.apache.sis.coverage.grid.ImageRenderer;
+import org.apache.sis.internal.netcdf.Resources;
 
 
 /**
@@ -42,11 +44,17 @@ final class Image extends GridCoverage {
     private final DataBuffer data;
 
     /**
+     * Name to display in error messages. Not to be used for processing.
+     */
+    private final String label;
+
+    /**
      * Creates a new raster from the given resource.
      */
-    Image(final GridGeometry domain, final List<SampleDimension> range, final DataBuffer
data) {
+    Image(final GridGeometry domain, final List<SampleDimension> range, final DataBuffer
data, final String label) {
         super(domain, range);
-        this.data = data;
+        this.data  = data;
+        this.label = label;
     }
 
     /**
@@ -59,8 +67,8 @@ final class Image extends GridCoverage {
             final ImageRenderer renderer = new ImageRenderer(this, target);
             renderer.setData(data);
             return renderer.image();
-        } catch (ArithmeticException | IllegalArgumentException e) {
-            throw new CannotEvaluateException(null, e);
+        } catch (IllegalArgumentException | ArithmeticException | RasterFormatException e)
{
+            throw new CannotEvaluateException(Resources.format(Resources.Keys.CanNotRender_2,
label, e), e);
         }
     }
 }


Mime
View raw message