sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: Better selection of axis with fatest increasing values.
Date Fri, 01 Mar 2019 14:43:39 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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new bc58b81  Better selection of axis with fatest increasing values.
bc58b81 is described below

commit bc58b8193ca9604736dcc1b24cec1af9e204f275
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Mar 1 15:42:42 2019 +0100

    Better selection of axis with fatest increasing values.
---
 .../java/org/apache/sis/internal/netcdf/Axis.java  | 61 +++++++++++++++++-----
 .../org/apache/sis/internal/netcdf/Resources.java  |  5 ++
 .../sis/internal/netcdf/Resources.properties       |  1 +
 .../sis/internal/netcdf/Resources_fr.properties    |  1 +
 4 files changed, 54 insertions(+), 14 deletions(-)

diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
index c9db227..84879da 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
@@ -260,18 +260,32 @@ public final class Axis extends NamedElement {
                 if (s) break;                       // Need swapping for avoiding collision.
             }
         }
+        /*
+         * Compute increments along x and y axes at the given location. We will compare averages
of those increments in
+         * order to determine which axis increases faster. Note that we do not use formula
like  (last - first) / count
+         * because such formula does not work if the coordinates to not increase or decrease
monotonically.
+         *
+         * ┌────────────────┬────────────────┐
+         * │(0)             │(1)             │(2)
+         * │                │                │
+         * ├────────────────┼────────────────┤
+         * │(3)             │(4)             │(5)
+         * │                │                │
+         * └────────────────┴────────────────┘
+         *  (6)              (7)              (8)
+         */
         if (!s) {
-            int up0 = sourceSizes[0];
-            int up1 = sourceSizes[1];
-            final int mid0 = up0 >>> 1;             // Division by 2 of unsigned
integers.
-            final int mid1 = up1 >>> 1;
-            if (up0 < 0) up0 = Integer.MAX_VALUE;   // For unsigned integers, < 0 means
overflow.
-            if (up1 < 0) up1 = Integer.MAX_VALUE;
-            final double inc0 = (coordinates.coordinateForAxis(    0, mid1) -
-                                 coordinates.coordinateForAxis(up0-1, mid1)) / up0;
-            final double inc1 = (coordinates.coordinateForAxis(mid0,     0) -
-                                 coordinates.coordinateForAxis(mid0, up1-1)) / up1;
-            if (!(Math.abs(inc1) > Math.abs(inc0))) {
+            final int[] x = sampleIndices(sourceSizes[0]);
+            final int[] y = sampleIndices(sourceSizes[1]);
+            double xInc = 0, yInc = 0;
+            for (int c=x.length * y.length; --c >= 0;) {
+                final int i = x[c % y.length];
+                final int j = y[c / y.length];
+                final double vo = coordinates.coordinateForAxis(i, j);
+                xInc += coordinates.coordinateForAxis(i+1, j) - vo;
+                yInc += coordinates.coordinateForAxis(i, j+1) - vo;
+            }
+            if (!(Math.abs(yInc) > Math.abs(xInc))) {
                 return;
             }
         }
@@ -280,6 +294,25 @@ public final class Axis extends NamedElement {
     }
 
     /**
+     * Returns indices of sample values to use in a dimension of the given length.
+     * Current implementation returns indices at beginning, middle and end.
+     *
+     * @param  length  the dimension length.
+     * @return indices to use for subsampling in a dimension of the given length.
+     */
+    private static int[] sampleIndices(final int length) {
+        final int up;
+        if (length >= 0) {
+            if (length <= 1) return ArraysExt.EMPTY_INT;
+            if (length <= 4) return ArraysExt.sequence(0, length - 1);
+            up = length - 2;
+        } else {
+            up = Integer.MAX_VALUE - 1;                 // For unsigned integers, < 0
means overflow.
+        }
+        return new int[] {0, length >>> 1, up};
+    }
+
+    /**
      * Returns the number of dimension of the localization grid used by this axis.
      * This method returns 2 if this axis if backed by a localization grid having 2 or more
dimensions.
      * In the netCDF UCAR library, such axes are handled by a {@link ucar.nc2.dataset.CoordinateAxis2D}.
@@ -682,11 +715,11 @@ main:   switch (getDimension()) {
         final TransferFunction tr = coordinates.decoder.convention().transferFunction(coordinates);
         if (TransferFunctionType.LINEAR.equals(tr.getType())) {
             Vector data = coordinates.read();
-            data = data.subList(0, getSizeProduct(0));              // Trim trailing NaN
values.
-            data = data.transform(tr.getScale(), tr.getOffset());   // Apply scale and offset
attributes, if any.
+            data = data.subList(0, getSizeProduct(0));                  // Trim trailing
NaN values.
+            data = data.transform(tr.getScale(), tr.getOffset());       // Apply scale and
offset attributes, if any.
             return data;
         } else {
-            throw new DataStoreException();     // TODO: non-linear transform.
+            throw new DataStoreException(coordinates.resources().getString(Resources.Keys.CanNotUseAxis_1,
getName()));
         }
     }
 }
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 f2f84e9..bd6577d 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
@@ -92,6 +92,11 @@ public final class Resources extends IndexedResourceBundle {
         public static final short CanNotRender_2 = 14;
 
         /**
+         * Can not use axis “{0}” in a grid geometry.
+         */
+        public static final short CanNotUseAxis_1 = 18;
+
+        /**
          * 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 4b39a7b..1dd77af 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
@@ -25,6 +25,7 @@ CanNotCreateCRS_3                 = Can not create the Coordinate Reference
Syst
 CanNotCreateGridGeometry_3        = Can not create the grid geometry \u201c{1}\u201d in the
\u201c{0}\u201d netCDF file. The reason is: {2}
 CanNotRelateVariableDimension_3   = Can not relate dimension \u201c{2}\u201d of variable
\u201c{1}\u201d to a coordinate system dimension in netCDF file \u201c{0}\u201d.
 CanNotRender_2                    = Can not render an image for \u201c{0}\u201d. The reason
is: {1}
+CanNotUseAxis_1                   = Can not use axis \u201c{0}\u201d in a grid geometry.
 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.
 DuplicatedAxis_2                  = Duplicated axis \u201c{1}\u201d in a grid of 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 ba3888e..7cb59c7 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
@@ -30,6 +30,7 @@ CanNotCreateCRS_3                 = Ne peut pas cr\u00e9er le syst\u00e8me
de r\
 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}
 CanNotRelateVariableDimension_3   = Ne peut pas relier la dimension \u00ab\u202f{2}\u202f\u00bb
de la variable \u00ab\u202f{1}\u202f\u00bb \u00e0 une dimension d\u2019un syst\u00e8me de
coordonn\u00e9es du fichier netCDF \u00ab\u202f{0}\u202f\u00bb.
 CanNotRender_2                    = Ne peut pas produire une image pour \u00ab\u202f{0}\u202f\u00bb.
La raison est\u2008: {1}
+CanNotUseAxis_1                   = Ne peut pas utiliser l\u2019axe \u00ab\u202f{0}\u202f\u00bb
pour une g\u00e9om\u00e9trie de grille.
 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.
 DuplicatedAxis_2                  = Axe \u00ab\u202f{1}\u202f\u00bb dupliqu\u00e9 dans une
grille du fichier netCDF \u00ab\u202f{0}\u202f\u00bb.


Mime
View raw message