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 e0ad3091f1 Add CRS declaration in shapefile geometry user data
e0ad3091f1 is described below

commit e0ad3091f115b9c52b333cee68e45c7c9aca8725
Author: jsorel <[email protected]>
AuthorDate: Thu Jul 10 15:03:28 2025 +0200

    Add CRS declaration in shapefile geometry user data
---
 .../sis/storage/shapefile/ShapefileStore.java       | 21 ++++++++++++++++++++-
 .../sis/storage/shapefile/ShapefileStoreTest.java   |  4 ++++
 2 files changed, 24 insertions(+), 1 deletion(-)

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 5bdd2cb597..5617c12bf9 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
@@ -75,7 +75,7 @@ import org.apache.sis.filter.DefaultFilterFactory;
 import org.apache.sis.filter.Optimization;
 import org.apache.sis.filter.privy.FunctionNames;
 import org.apache.sis.filter.privy.ListingPropertyVisitor;
-import org.apache.sis.geometry.wrapper.Geometries;
+import org.apache.sis.geometry.wrapper.*;
 import org.apache.sis.io.stream.ChannelDataInput;
 import org.apache.sis.io.stream.ChannelDataOutput;
 import org.apache.sis.io.stream.IOUtilities;
@@ -85,6 +85,7 @@ import org.apache.sis.metadata.iso.citation.Citations;
 import org.apache.sis.parameter.Parameters;
 import org.apache.sis.referencing.CRS;
 import org.apache.sis.referencing.CommonCRS;
+import org.apache.sis.referencing.IdentifiedObjects;
 import org.apache.sis.setup.OptionKey;
 import org.apache.sis.storage.AbstractFeatureSet;
 import org.apache.sis.storage.DataStore;
@@ -125,6 +126,7 @@ import org.opengis.filter.LogicalOperator;
 import org.opengis.filter.LogicalOperatorName;
 import org.opengis.filter.SpatialOperatorName;
 import org.opengis.filter.ValueReference;
+import org.opengis.metadata.Identifier;
 
 
 /**
@@ -455,6 +457,15 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
                 throw new DataStoreException("Faild to open shp and dbf 
files.", ex);
             }
 
+            int srid = 0;
+            final Identifier id = IdentifiedObjects.getIdentifier(crs, 
Citations.EPSG);
+            if (id != null) try {
+                srid = Integer.parseInt(id.getCode());
+            } catch (NumberFormatException e) {
+                // Ignore. Note: this is also the exception if id.getCode() is 
null.
+            }
+            final int geomSrid = srid;
+
             final Spliterator spliterator;
             if (readShp && dbfPropertiesIndex.length > 0) {
                 //read both shp and dbf
@@ -471,6 +482,10 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
                             dbfreader.moveToOffset(offset);
                             final Object[] dbfRecord = dbfreader.next();
                             final Feature next = type.newInstance();
+                            if (shpRecord.geometry != null) {
+                                shpRecord.geometry.setUserData(crs);
+                                shpRecord.geometry.setSRID(geomSrid);
+                            }
                             next.setPropertyValue(GEOMETRY_NAME, 
shpRecord.geometry);
                             for (int i = 0; i < dbfPropertiesIndex.length; 
i++) {
                                 
next.setPropertyValue(header.fields[dbfPropertiesIndex[i]].fieldName, 
dbfRecord[i]);
@@ -491,6 +506,10 @@ public final class ShapefileStore extends DataStore 
implements WritableFeatureSe
                             final ShapeRecord shpRecord = shpreader.next();
                             if (shpRecord == null) return false;
                             final Feature next = type.newInstance();
+                            if (shpRecord.geometry != null) {
+                                shpRecord.geometry.setUserData(crs);
+                                shpRecord.geometry.setSRID(geomSrid);
+                            }
                             next.setPropertyValue(GEOMETRY_NAME, 
shpRecord.geometry);
                             action.accept(next);
                             return true;
diff --git 
a/incubator/src/org.apache.sis.storage.shapefile/test/org/apache/sis/storage/shapefile/ShapefileStoreTest.java
 
b/incubator/src/org.apache.sis.storage.shapefile/test/org/apache/sis/storage/shapefile/ShapefileStoreTest.java
index b5f642304f..509c9ea266 100644
--- 
a/incubator/src/org.apache.sis.storage.shapefile/test/org/apache/sis/storage/shapefile/ShapefileStoreTest.java
+++ 
b/incubator/src/org.apache.sis.storage.shapefile/test/org/apache/sis/storage/shapefile/ShapefileStoreTest.java
@@ -50,6 +50,7 @@ import org.opengis.feature.Feature;
 import org.opengis.feature.FeatureType;
 import org.opengis.filter.BinaryComparisonOperator;
 import org.opengis.filter.FilterFactory;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
 
 
 /**
@@ -95,6 +96,7 @@ public class ShapefileStoreTest {
                 assertEquals(20.0, feature1.getPropertyValue("float"));
                 assertEquals(LocalDate.of(2023, 10, 27), 
feature1.getPropertyValue("date"));
                 Point pt1 = (Point) feature1.getPropertyValue("geometry");
+                assertTrue(pt1.getUserData() instanceof 
CoordinateReferenceSystem);
 
                 assertTrue(iterator.hasNext());
                 Feature feature2 = iterator.next();
@@ -104,6 +106,7 @@ public class ShapefileStoreTest {
                 assertEquals(60.0, feature2.getPropertyValue("float"));
                 assertEquals(LocalDate.of(2023, 10, 28), 
feature2.getPropertyValue("date"));
                 Point pt2 = (Point) feature2.getPropertyValue("geometry");
+                assertTrue(pt2.getUserData() instanceof 
CoordinateReferenceSystem);
 
                 assertFalse(iterator.hasNext());
             }
@@ -140,6 +143,7 @@ public class ShapefileStoreTest {
                 assertEquals(60.0, feature.getPropertyValue("float"));
                 assertEquals(LocalDate.of(2023, 10, 28), 
feature.getPropertyValue("date"));
                 Point pt2 = (Point) feature.getPropertyValue("geometry");
+                assertTrue(pt2.getUserData() instanceof 
CoordinateReferenceSystem);
 
                 assertFalse(iterator.hasNext());
             }

Reply via email to