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 ac6a4fdb0219c1ecceba890a50e81e4caea8161c Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Tue Aug 2 16:28:06 2022 +0200 Allow to fetch statistics in last resort if min/max are not specified in a netCDF file. This is disabled by default, because this operation is potentially costly. --- .../main/java/org/apache/sis/internal/netcdf/Variable.java | 10 +++++++++- .../org/apache/sis/internal/storage/StoreUtilities.java | 13 ++++++++++++- .../java/org/apache/sis/internal/storage/package-info.java | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java index 6bbb896069..4305b22bfa 100644 --- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java +++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java @@ -48,13 +48,15 @@ import org.apache.sis.util.resources.Errors; import ucar.nc2.constants.CDM; // We use only String constants. import ucar.nc2.constants.CF; +import static org.apache.sis.internal.storage.StoreUtilities.ALLOW_LAST_RESORT_STATISTICS; + /** * A netCDF variable created by {@link Decoder}. * * @author Martin Desruisseaux (Geomatys) * @author Johann Sorel (Geomatys) - * @version 1.1 + * @version 1.3 * @since 0.3 * @module */ @@ -851,6 +853,12 @@ public abstract class Variable extends Node { NumberRange<?> range = decoder.convention().validRange(this); if (range == null) { range = getRangeFallback(); + if (ALLOW_LAST_RESORT_STATISTICS && range == null) try { + range = read().range(); + } catch (DataStoreException | IOException e) { + // It should be a fatal error, but maybe the user wants only metadata. + error(Variable.class, "getValidRange", e, Errors.Keys.CanNotRead_1, getName()); + } } return range; } diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreUtilities.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreUtilities.java index cd8fd65d9a..eaeb355bab 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreUtilities.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/StoreUtilities.java @@ -62,11 +62,22 @@ import org.opengis.feature.Feature; * Some methods may also move in public API if we feel confident enough. * * @author Martin Desruisseaux (Geomatys) - * @version 1.2 + * @version 1.3 * @since 1.0 * @module */ public final class StoreUtilities extends Static { + /** + * Whether to allow computation of statistics when no minimum/maximum values can be determined. + * This is a costly operation because it requires loading all data, so any code enabled by this + * flag should be executed in last resort only. + * + * <p>This flag can be set to {@code true} for exploring data that we can not visualize otherwise. + * But it should generally stay to {@code false}, because otherwise browsing resource metadata can + * become as costly (slow and high memory usage) as visualizing the full raster.</p> + */ + public static final boolean ALLOW_LAST_RESORT_STATISTICS = false; + /** * Logger for the {@value Modules#STORAGE} module. This is used when no more specific logger is available, * or if the more specific logger is not appropriate (e.g. because the log message come from base class). diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/package-info.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/package-info.java index d07eeacb65..a110dc7d1f 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/package-info.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/package-info.java @@ -25,7 +25,7 @@ * * @author Martin Desruisseaux (Geomatys) * @author Johann Sorel (Geomatys) - * @version 1.2 + * @version 1.3 * @since 0.4 * @module */