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 db6cf1093bad234acd250da52f5cca85a9079a80
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Tue May 13 19:21:45 2025 +0200

    More reliable determination of the path to the metadata file used for 
opening a Landsat store.
    Opportunistic minor editions.
---
 .../main/org/apache/sis/storage/landsat/Band.java  |  4 ++--
 .../apache/sis/storage/landsat/LandsatStore.java   | 26 +++++++++++++---------
 .../sis/storage/shapefile/ShapefileStore.java      | 12 +++++-----
 3 files changed, 24 insertions(+), 18 deletions(-)

diff --git 
a/endorsed/src/org.apache.sis.storage.earthobservation/main/org/apache/sis/storage/landsat/Band.java
 
b/endorsed/src/org.apache.sis.storage.earthobservation/main/org/apache/sis/storage/landsat/Band.java
index 40fe5d7143..96ffd4e4e4 100644
--- 
a/endorsed/src/org.apache.sis.storage.earthobservation/main/org/apache/sis/storage/landsat/Band.java
+++ 
b/endorsed/src/org.apache.sis.storage.earthobservation/main/org/apache/sis/storage/landsat/Band.java
@@ -226,9 +226,9 @@ final class Band extends GridResourceWrapper implements 
CoverageModifier {
     public Optional<FileSet> getFileSet() throws DataStoreException {
         return Optional.of(new FileSet(getDataPath()));
     }
-    
+
     /**
-     * Resolve Path to image file. 
+     * Resolves path to image file.
      */
     private Path getDataPath() {
         if (parent.directory != null) {
diff --git 
a/endorsed/src/org.apache.sis.storage.earthobservation/main/org/apache/sis/storage/landsat/LandsatStore.java
 
b/endorsed/src/org.apache.sis.storage.earthobservation/main/org/apache/sis/storage/landsat/LandsatStore.java
index f6adab64fe..f77b7089a4 100644
--- 
a/endorsed/src/org.apache.sis.storage.earthobservation/main/org/apache/sis/storage/landsat/LandsatStore.java
+++ 
b/endorsed/src/org.apache.sis.storage.earthobservation/main/org/apache/sis/storage/landsat/LandsatStore.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.storage.landsat;
 
+import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.List;
 import java.util.Optional;
@@ -28,10 +29,7 @@ import java.net.URI;
 import java.nio.charset.Charset;
 import java.nio.file.Path;
 import java.nio.file.Files;
-import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
-import java.util.HashSet;
-import java.util.Set;
 import org.opengis.util.NameSpace;
 import org.opengis.util.GenericName;
 import org.opengis.util.NameFactory;
@@ -93,6 +91,11 @@ public class LandsatStore extends DataStore implements 
Aggregate {
      */
     private Reader source;
 
+    /**
+     * The file from which {@link #source} has been read, or {@code null} if 
unknown.
+     */
+    private final Path sourceFile;
+
     /**
      * The root directory where this file is located, or {@code null} if 
unknown.
      */
@@ -138,11 +141,13 @@ public class LandsatStore extends DataStore implements 
Aggregate {
         location  = connector.getStorageAs(URI.class);
         source    = connector.getStorageAs(Reader.class);
         connector.closeAllExcept(source);
+        Path file = null;
         if (path != null) {
             if (source != null) {
+                file = path;
                 path = path.getParent();        // If the source has been 
opened, then the path is a file.
             } else try {
-                final Path file = LandsatStoreProvider.getMetadataFile(path);
+                file = LandsatStoreProvider.getMetadataFile(path);
                 if (file != null) {
                     final Charset encoding = 
connector.getOption(OptionKey.ENCODING);
                     if (encoding != null) {
@@ -155,7 +160,8 @@ public class LandsatStore extends DataStore implements 
Aggregate {
                 throw new DataStoreException(e);
             }
         }
-        directory = path;
+        sourceFile = file;
+        directory  = path;
         if (source == null) {
             throw new UnsupportedStorageException(super.getLocale(), 
LandsatStoreProvider.NAME,
                     connector.getStorage(), 
connector.getOption(OptionKey.OPEN_OPTIONS));
@@ -302,16 +308,16 @@ public class LandsatStore extends DataStore implements 
Aggregate {
     }
 
     /**
-     * Returns the list of band files and the *_MTL.txt file.
+     * Returns the list of band files and the {@code *_MTL.txt} file.
      */
     @Override
     public Optional<FileSet> getFileSet() throws DataStoreException {
-        components(); //force loading bands
-        final Set<Path> paths = new HashSet<>();
-        paths.add(Paths.get(location));
-        for (BandGroup b : components) {
+        final var paths = new LinkedHashSet<Path>();    // Order matter, as 
`sourceFile` should be first.
+        paths.add(sourceFile);                          // May be null (all 
nulls are removed at the end).
+        for (Aggregate b : components()) {
             b.getFileSet().map(FileSet::getPaths).ifPresent(paths::addAll);
         }
+        paths.remove(null);
         return Optional.of(new FileSet(paths));
     }
 
diff --git 
a/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/ShapefileStore.java
 
b/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/ShapefileStore.java
index 326b827a8f..5bdd2cb597 100644
--- 
a/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/ShapefileStore.java
+++ 
b/incubator/src/org.apache.sis.storage.shapefile/main/org/apache/sis/storage/shapefile/ShapefileStore.java
@@ -929,7 +929,7 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
          */
         public Path getShx(boolean create) {
             if (create && shxFile == null) {
-                return shpFile.resolveSibling(baseName + "." + (baseUpper ? 
"SHX" : "shx"));
+                return shpFile.resolveSibling(baseName + '.' + (baseUpper ? 
"SHX" : "shx"));
             }
             return shxFile;
         }
@@ -940,7 +940,7 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
          */
         public Path getDbf(boolean create) {
             if (create && dbfFile == null) {
-                return shpFile.resolveSibling(baseName + "." + (baseUpper ? 
"DBF" : "dbf"));
+                return shpFile.resolveSibling(baseName + '.' + (baseUpper ? 
"DBF" : "dbf"));
             }
             return dbfFile;
         }
@@ -951,7 +951,7 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
          */
         public Path getPrj(boolean create) {
             if (create && prjFile == null) {
-                return shpFile.resolveSibling(baseName + "." + (baseUpper ? 
"PRJ" : "prj"));
+                return shpFile.resolveSibling(baseName + '.' + (baseUpper ? 
"PRJ" : "prj"));
             }
             return prjFile;
         }
@@ -962,7 +962,7 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
          */
         public Path getCpg(boolean create) {
             if (create && cpgFile == null) {
-                return shpFile.resolveSibling(baseName + "." + (baseUpper ? 
"CPG" : "cpg"));
+                return shpFile.resolveSibling(baseName + '.' + (baseUpper ? 
"CPG" : "cpg"));
             }
             return cpgFile;
         }
@@ -1007,9 +1007,9 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
         }
 
         private Path findSibling(String extension) {
-            Path candidate = shpFile.resolveSibling(baseName + "." + 
extension);
+            Path candidate = shpFile.resolveSibling(baseName + '.' + 
extension);
             if (java.nio.file.Files.isRegularFile(candidate)) return candidate;
-            candidate = shpFile.resolveSibling(baseName + "." + 
extension.toUpperCase());
+            candidate = shpFile.resolveSibling(baseName + '.' + 
extension.toUpperCase());
             if (java.nio.file.Files.isRegularFile(candidate)) return candidate;
             return null;
         }

Reply via email to