This is an automated email from the ASF dual-hosted git repository.

jsorel 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 57c14cd312 fix(Shapefile): fix combining multiple subset
57c14cd312 is described below

commit 57c14cd312aadb67e0ad0d1fbd9a4720b635f461
Author: jsorel <johann.so...@geomatys.com>
AuthorDate: Fri Nov 24 17:22:36 2023 +0100

    fix(Shapefile): fix combining multiple subset
---
 .../sis/storage/shapefile/ShapefileStore.java      | 37 ++++++++++++++++++----
 1 file changed, 31 insertions(+), 6 deletions(-)

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 64276679b1..c419a714bb 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
@@ -18,6 +18,7 @@ package org.apache.sis.storage.shapefile;
 
 import java.awt.geom.Rectangle2D;
 import java.io.IOException;
+import java.math.BigInteger;
 import java.nio.ByteBuffer;
 import java.nio.channels.SeekableByteChannel;
 import java.nio.channels.WritableByteChannel;
@@ -78,12 +79,14 @@ import org.apache.sis.io.stream.IOUtilities;
 import org.apache.sis.parameter.Parameters;
 import org.apache.sis.referencing.CRS;
 import org.apache.sis.referencing.CommonCRS;
+import org.apache.sis.setup.OptionKey;
 import org.apache.sis.storage.AbstractFeatureSet;
 import org.apache.sis.storage.DataStore;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.FeatureQuery;
 import org.apache.sis.storage.FeatureSet;
 import org.apache.sis.storage.Query;
+import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.UnsupportedQueryException;
 import org.apache.sis.storage.WritableFeatureSet;
 import org.apache.sis.storage.shapefile.cpg.CpgFiles;
@@ -99,6 +102,7 @@ import org.apache.sis.storage.shapefile.shp.ShapeRecord;
 import org.apache.sis.storage.shapefile.shp.ShapeType;
 import org.apache.sis.storage.shapefile.shp.ShapeWriter;
 import org.apache.sis.storage.shapefile.shx.IndexWriter;
+import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.collection.BackingStoreException;
 
 // Specific to the geoapi-3.1 and geoapi-4.0 branches:
@@ -128,11 +132,11 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
 
     private final Path shpPath;
     private final ShpFiles files;
+    private final Charset userDefinedCharSet;
     /**
      * Internal class to inherit AbstractFeatureSet.
      */
     private final AsFeatureSet featureSetView = new AsFeatureSet(null, true, 
null);
-    private Charset charset;
 
     /**
      * Lock to control read and write operations.
@@ -141,6 +145,13 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
 
     public ShapefileStore(Path path) {
         this.shpPath = path;
+        this.userDefinedCharSet = null;
+        this.files = new ShpFiles(shpPath);
+    }
+
+    public ShapefileStore(StorageConnector cnx) throws 
IllegalArgumentException, DataStoreException {
+        this.shpPath = cnx.getStorageAs(Path.class);
+        this.userDefinedCharSet = cnx.getOption(OptionKey.ENCODING);
         this.files = new ShpFiles(shpPath);
     }
 
@@ -214,6 +225,7 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
         private final Rectangle2D.Double filter;
         private final Set<String> dbfProperties;
         private final boolean readShp;
+        private Charset charset;
 
         /**
          * Extracted informations
@@ -517,7 +529,12 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
                         throw new DataStoreException("Failed to transform bbox 
filter", ex);
                     }
                     area = new Rectangle2D.Double(bbox.getMinimum(0), 
bbox.getMinimum(1), bbox.getSpan(0), bbox.getSpan(1));
+                }
 
+                if (area == null) {
+                    //use current subset one
+                    area = filter;
+                } else {
                     //combine this area with the one we already have since 
this is a subset
                     if (filter != null) {
                         area = (Rectangle2D.Double) 
area.createIntersection(filter);
@@ -574,13 +591,17 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
 
             final ShapeHeader shpHeader = new ShapeHeader();
             final DBFHeader dbfHeader = new DBFHeader();
-            Charset charset = StandardCharsets.UTF_8;
+            final Charset charset = userDefinedCharSet == null ? 
StandardCharsets.UTF_8 : userDefinedCharSet;
             CoordinateReferenceSystem crs = 
CommonCRS.WGS84.normalizedGeographic();
 
             for (PropertyType pt : newType.getProperties(true)) {
                 if (pt instanceof AttributeType) {
                     final AttributeType at = (AttributeType) pt;
                     final Class valueClass = at.getValueClass();
+
+                    Integer length = 
AttributeConvention.getMaximalLengthCharacteristic(newType, pt);
+                    if (length == 0) length = 255;
+
                     if (Geometry.class.isAssignableFrom(valueClass)) {
                         if (shpHeader.shapeType != 0) {
                             throw new DataStoreException("Shapefile format can 
only contain one geometry");
@@ -597,13 +618,17 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
                         }
 
                     } else if (String.class.isAssignableFrom(valueClass)) {
-
+                        dbfHeader.fields = ArraysExt.append(dbfHeader.fields,  
new DBFField(idField, (char)DBFField.TYPE_CHAR, 0, length, 0, charset));
+                    } else if (Byte.class.isAssignableFrom(valueClass)) {
+                        dbfHeader.fields = ArraysExt.append(dbfHeader.fields, 
new DBFField(idField, (char)DBFField.TYPE_NUMBER, 0, 4, 0, null));
+                    } else if (Short.class.isAssignableFrom(valueClass)) {
+                        dbfHeader.fields = ArraysExt.append(dbfHeader.fields, 
new DBFField(idField, (char)DBFField.TYPE_NUMBER, 0, 6, 0, null));
                     } else if (Integer.class.isAssignableFrom(valueClass)) {
-
+                        dbfHeader.fields = ArraysExt.append(dbfHeader.fields, 
new DBFField(idField, (char)DBFField.TYPE_NUMBER, 0, 9, 0, null));
                     } else if (Long.class.isAssignableFrom(valueClass)) {
-
+                        dbfHeader.fields = ArraysExt.append(dbfHeader.fields, 
new DBFField(idField, (char)DBFField.TYPE_NUMBER, 0, 19, 0, null));
                     } else if (Float.class.isAssignableFrom(valueClass)) {
-
+                        dbfHeader.fields = ArraysExt.append(dbfHeader.fields, 
new DBFField(idField, (char)DBFField.TYPE_NUMBER, 0, 33, 0, null));
                     } else if (Double.class.isAssignableFrom(valueClass)) {
 
                     } else if (LocalDate.class.isAssignableFrom(valueClass)) {

Reply via email to