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 ad1dc970ce Add a simple test case for ASCII Grid reader. ad1dc970ce is described below commit ad1dc970cede78d649b84956316f17e9cfac2a68 Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Tue Apr 5 16:51:50 2022 +0200 Add a simple test case for ASCII Grid reader. --- ide-project/NetBeans/build.xml | 1 + .../sis/internal/storage/MetadataBuilder.java | 8 +- .../sis/internal/storage/ascii/StoreTest.java | 99 ++++++++++++++++++++++ .../apache/sis/test/suite/StorageTestSuite.java | 1 + .../org/apache/sis/internal/storage/ascii/grid.asc | 34 ++++++++ .../org/apache/sis/internal/storage/ascii/grid.prj | 5 ++ 6 files changed, 146 insertions(+), 2 deletions(-) diff --git a/ide-project/NetBeans/build.xml b/ide-project/NetBeans/build.xml index af72e7e2e9..ba9d889f9a 100644 --- a/ide-project/NetBeans/build.xml +++ b/ide-project/NetBeans/build.xml @@ -302,6 +302,7 @@ </fileset> <fileset dir="${project.root}/storage/sis-storage/src/test/resources"> <include name="**/*.txt"/> + <include name="**/*.asc"/> <include name="**/*.prj"/> <include name="**/*.xml"/> </fileset> diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java index e759ad7642..2e83572902 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java @@ -1892,9 +1892,13 @@ parse: for (int i = 0; i < length;) { */ public final void addExtent(final Envelope envelope) throws TransformException { if (envelope != null) { - addReferenceSystem(envelope.getCoordinateReferenceSystem()); + final CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem(); + addReferenceSystem(crs); if (!(envelope instanceof AbstractEnvelope && ((AbstractEnvelope) envelope).isAllNaN())) { - extent().addElements(envelope); + if (crs != null) { + extent().addElements(envelope); + } + // Future version could add as a geometry in unspecified CRS. } } } diff --git a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ascii/StoreTest.java b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ascii/StoreTest.java new file mode 100644 index 0000000000..4df0a60adc --- /dev/null +++ b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/ascii/StoreTest.java @@ -0,0 +1,99 @@ +/* + * 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.internal.storage.ascii; + +import java.awt.image.Raster; +import java.awt.image.RenderedImage; +import org.opengis.metadata.Metadata; +import org.opengis.metadata.extent.GeographicBoundingBox; +import org.opengis.metadata.identification.Identification; +import org.apache.sis.coverage.grid.GridCoverage; +import org.apache.sis.storage.DataStoreException; +import org.apache.sis.storage.StorageConnector; +import org.apache.sis.test.TestCase; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.apache.sis.test.TestUtilities.getSingleton; + + +/** + * Tests {@link Store}. + * + * @author Martin Desruisseaux (Geomatys) + * @version 1.2 + * @since 1.2 + * @module + */ +public final strictfp class StoreTest extends TestCase { + /** + * Opens an ASCII Grid store on the test data. + */ + private static Store open() throws DataStoreException { + return new Store(null, new StorageConnector(StoreTest.class.getResource("grid.asc"))); + } + + /** + * Tests the metadata of the {@code "grid.asc"} file. + * + * @throws DataStoreException if an error occurred while reading the file. + */ + @Test + public void testMetadata() throws DataStoreException { + try (Store store = open()) { + assertEquals("grid", store.getIdentifier().get().toString()); + final Metadata metadata = store.getMetadata(); + /* + * Format information is hard-coded in "SpatialMetadata" database. Complete string should + * be "ESRI ArcInfo ASCII Grid format" but it depends on the presence of Derby dependency. + */ + final Identification id = getSingleton(metadata.getIdentificationInfo()); + final String format = getSingleton(id.getResourceFormats()).getFormatSpecificationCitation().getTitle().toString(); + assertTrue(format, format.contains("ASCII Grid")); + /* + * This information should have been read from the PRJ file. + */ + assertEquals("WGS 84 / World Mercator", + getSingleton(metadata.getReferenceSystemInfo()).getName().getCode()); + final GeographicBoundingBox bbox = (GeographicBoundingBox) + getSingleton(getSingleton(id.getExtents()).getGeographicElements()); + assertEquals(-84, bbox.getSouthBoundLatitude(), 1); + assertEquals(+85, bbox.getNorthBoundLatitude(), 1); + } + } + + /** + * Tests reading a few values from the {@code "grid.asc"} file. + * + * @throws DataStoreException if an error occurred while reading the file. + */ + @Test + public void testRead() throws DataStoreException { + try (Store store = open()) { + final GridCoverage coverage = store.read(null, null); + final RenderedImage image = coverage.render(null); + assertEquals(10, image.getWidth()); + assertEquals(20, image.getHeight()); + final Raster tile = image.getTile(0,0); + assertEquals( 1.061f, tile.getSampleFloat(0, 0, 0), 0f); + assertEquals(Float.NaN, tile.getSampleFloat(9, 0, 0), 0f); + assertEquals(Float.NaN, tile.getSampleFloat(9, 19, 0), 0f); + assertEquals( -1.075f, tile.getSampleFloat(0, 19, 0), 0f); + assertEquals( 27.039f, tile.getSampleFloat(4, 10, 0), 0f); + } + } +} diff --git a/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java b/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java index 1703a23ed5..222e1dd34b 100644 --- a/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java +++ b/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java @@ -57,6 +57,7 @@ import org.junit.BeforeClass; org.apache.sis.internal.storage.wkt.StoreTest.class, org.apache.sis.internal.storage.csv.StoreProviderTest.class, org.apache.sis.internal.storage.csv.StoreTest.class, + org.apache.sis.internal.storage.ascii.StoreTest.class, org.apache.sis.internal.storage.folder.StoreTest.class, org.apache.sis.internal.storage.JoinFeatureSetTest.class, org.apache.sis.internal.storage.ConcatenatedFeatureSetTest.class, diff --git a/storage/sis-storage/src/test/resources/org/apache/sis/internal/storage/ascii/grid.asc b/storage/sis-storage/src/test/resources/org/apache/sis/internal/storage/ascii/grid.asc new file mode 100644 index 0000000000..54f3704c11 --- /dev/null +++ b/storage/sis-storage/src/test/resources/org/apache/sis/internal/storage/ascii/grid.asc @@ -0,0 +1,34 @@ +# +# The Apache SIS implementation of ASCII Grid reader is a slight extension +# of the format defined by ESRI. It can skip comment lines like those ones, +# and accept a few more keywords such as [X|Y]CELLSIZE (not used here). +# +# Data were originally temperature data, but locations have been modified +# arbitrarily with round numbers for easier debugging. +# +NCOLS 10 +NROWS 20 +XLLCENTER -10000 +YLLCENTER -20000 +CELLSIZE 2000 +NODATA_VALUE -9999 + 1.061 0.273 -0.819 0.358 -9999 7.806 4.296 -9999 -9999 -9999 + 1.766 -9999 -9999 0.401 4.618 9.824 6.146 -9999 -9999 -9999 + 3.476 -9999 -9999 0.001 8.476 10.522 -9999 -9999 -9999 -9999 + 5.393 9.315 -9999 -9999 9.472 12.499 -9999 -9999 -9999 3.825 + 6.644 9.936 -9999 -9999 13.941 -9999 -9999 -9999 -9999 6.235 + 13.691 15.224 -9999 19.109 18.523 20.674 -9999 -9999 -9999 15.641 + 22.031 19.662 -9999 24.164 22.161 -9999 -9999 -9999 -9999 21.837 + 26.475 22.612 -9999 27.756 23.313 -9999 28.996 -9999 27.922 29.193 + 27.468 27.278 28.891 26.914 26.498 -9999 -9999 28.951 29.253 29.330 + 28.744 26.183 23.483 -9999 27.619 25.820 -9999 29.347 29.522 29.773 + 29.436 27.970 24.466 -9999 27.039 23.890 -9999 27.037 28.221 26.856 + 24.670 25.626 22.430 -9999 25.086 20.259 25.069 23.321 24.248 -9999 + 18.940 19.303 18.557 -9999 17.933 18.475 20.097 18.529 18.691 -9999 + 13.850 13.268 12.258 -9999 13.968 10.872 14.141 13.677 12.603 13.964 + 10.503 8.636 8.195 8.200 5.086 4.501 3.874 3.660 5.974 9.339 + 5.454 4.409 5.834 6.084 0.290 -0.027 1.315 2.086 1.963 2.895 + 1.209 0.159 2.569 -0.424 -1.421 -1.375 -0.952 -0.780 0.080 -0.666 + -0.340 -1.228 -0.598 -1.688 -1.810 -1.517 -1.865 -1.419 -1.825 -1.876 + -0.804 -1.852 -1.280 -9999 -1.428 -1.881 -9999 -9999 -9999 -9999 + -1.075 -1.869 -9999 -9999 -1.362 -9999 -9999 -9999 -9999 -9999 diff --git a/storage/sis-storage/src/test/resources/org/apache/sis/internal/storage/ascii/grid.prj b/storage/sis-storage/src/test/resources/org/apache/sis/internal/storage/ascii/grid.prj new file mode 100644 index 0000000000..61f8cc4918 --- /dev/null +++ b/storage/sis-storage/src/test/resources/org/apache/sis/internal/storage/ascii/grid.prj @@ -0,0 +1,5 @@ +PROJCS["WGS 84 / World Mercator", + GEOGCS["WGS 84", + DATUM["World Geodetic System 1984", SPHEROID["WGS 84", 6378137.0, 298.257223563]], + PRIMEM["Greenwich", 0.0], UNIT["degree", 0.017453292519943295]], + PROJECTION["Mercator_1SP"], UNIT["km", 1000.0]]