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

asf-gitbox-commits 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 32b6256863 fix(Geometry): fix rectangle ShapeAdaptor missing crs
32b6256863 is described below

commit 32b6256863b09ec3bf40169cb9bd1b3575947afb
Author: jsorel <[email protected]>
AuthorDate: Wed Nov 4 11:38:04 2026 +0100

    fix(Geometry): fix rectangle ShapeAdaptor missing crs
---
 .../sis/geometries/adapter/ShapeAdapter.java       | 23 +++++++++++++---
 .../sis/geometries/adapter/ShapeAdapterTest.java   | 32 ++++++++++++++++++++++
 2 files changed, 51 insertions(+), 4 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/adapter/ShapeAdapter.java
 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/adapter/ShapeAdapter.java
index 0b15937a6e..b5fd0785d0 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/adapter/ShapeAdapter.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/adapter/ShapeAdapter.java
@@ -24,7 +24,9 @@ import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 import java.util.Objects;
 import org.opengis.geometry.Envelope;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.apache.sis.geometries.DataPoints;
+import org.apache.sis.geometries.Geometries;
 import org.apache.sis.geometries.Geometry;
 import org.apache.sis.geometries.GeometryFactory;
 import org.apache.sis.geometries.DataPointsType;
@@ -34,7 +36,7 @@ import org.apache.sis.geometry.wrapper.j2d.EmptyShape;
 import org.apache.sis.maths.Array;
 import org.apache.sis.maths.DataType;
 import org.apache.sis.maths.NDArrays;
-import org.apache.sis.maths.SampleSystem;
+import org.apache.sis.referencing.CRS;
 import org.apache.sis.referencing.internal.shared.AbstractShape;
 import org.apache.sis.referencing.internal.shared.IntervalRectangle;
 
@@ -107,7 +109,7 @@ public final class ShapeAdapter extends AbstractShape {
      */
     @Override
     public boolean contains(final double x, final double y) {
-        return 
geometry.contains(GeometryFactory.DEFAULT.createPoint(SampleSystem.ofSize(2), 
x, y));
+        return geometry.contains(GeometryFactory.DEFAULT.createPoint(crs2D(), 
x, y));
     }
 
     /**
@@ -142,12 +144,25 @@ public final class ShapeAdapter extends AbstractShape {
         return geometry.intersects(createRect(x, y, x + width, y + height));
     }
 
+    /**
+     * Returns the two-dimensional CRS of the shape coordinates, which is the 
geometry CRS
+     * or its horizontal component. If no such CRS can be determined, an 
undefined CRS is used.
+     */
+    private CoordinateReferenceSystem crs2D() {
+        CoordinateReferenceSystem crs = 
geometry.getCoordinateReferenceSystem();
+        if (crs != null && crs.getCoordinateSystem().getDimension() != 2) {
+            crs = CRS.getHorizontalComponent(crs);
+        }
+        return (crs != null) ? crs : Geometries.getUndefinedCRS(2);
+    }
+
     /**
      * Creates a SIS polygon which is a rectangle with the given coordinates.
      * This is a temporary shape used for union and intersection tests.
+     * The rectangle is in the same CRS as the geometry, as required by the 
geometry operations.
      */
-    private static Geometry createRect(final double xmin, final double ymin, 
final double xmax, final double ymax) {
-        final Array positions = NDArrays.of(2, new double[]{
+    private Geometry createRect(final double xmin, final double ymin, final 
double xmax, final double ymax) {
+        final Array positions = NDArrays.of(crs2D(), new double[]{
             xmin, ymin,
             xmin, ymax,
             xmax, ymax,
diff --git 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/adapter/ShapeAdapterTest.java
 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/adapter/ShapeAdapterTest.java
index 90d28c0ce2..62a9649e9e 100644
--- 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/adapter/ShapeAdapterTest.java
+++ 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/adapter/ShapeAdapterTest.java
@@ -18,6 +18,7 @@ package org.apache.sis.geometries.adapter;
 
 import java.awt.Shape;
 import java.awt.geom.PathIterator;
+import java.awt.geom.Rectangle2D;
 import org.apache.sis.geometries.Geometry;
 import org.apache.sis.geometries.GeometryFactory;
 import org.apache.sis.geometries.curve.LineString;
@@ -214,4 +215,35 @@ public final class ShapeAdapterTest {
         assertSegmentEquals(PathIterator.SEG_LINETO, 2, 0);
         assertTrue(iterator.isDone());
     }
+    /**
+     * Creates the square [0 … 10] × [0 … 10] in the given coordinate 
reference system.
+     */
+    private static Polygon square(final 
org.opengis.referencing.crs.CoordinateReferenceSystem crs) {
+        final LinearRing ring = GeometryFactory.DEFAULT.createLinearRing(
+                GeometryFactory.DEFAULT.createDataPoints(crs, 0.0, 0.0,  10.0, 
0.0,  10.0, 10.0,  0.0, 10.0,  0.0, 0.0));
+        return GeometryFactory.DEFAULT.createPolygon(ring, null);
+    }
+
+    /**
+     * Tests {@link ShapeAdapter#intersects(Rectangle2D)} and {@link 
ShapeAdapter#contains(Rectangle2D)}
+     * with their variants. The temporary rectangles and points shall be in 
the geometry CRS.
+     */
+    @Test
+    public void testIntersectsAndContains() {
+        for (final var crs : new 
org.opengis.referencing.crs.CoordinateReferenceSystem[] {
+                
org.apache.sis.referencing.CommonCRS.WGS84.normalizedGeographic(),
+                CARTESIAN_2D.getCoordinateReferenceSystem()})
+        {
+            final Shape shape = new ShapeAdapter(square(crs));
+            assertTrue (shape.intersects(new Rectangle2D.Double(5, 5, 10, 
10)));
+            assertTrue (shape.intersects(5, 5, 10, 10));
+            assertFalse(shape.intersects(new Rectangle2D.Double(20, 20, 5, 
5)));
+            assertFalse(shape.intersects(20, 20, 5, 5));
+            assertTrue (shape.contains(new Rectangle2D.Double(2, 2, 3, 3)));
+            assertTrue (shape.contains(2, 2, 3, 3));
+            assertFalse(shape.contains(new Rectangle2D.Double(5, 5, 10, 10)));
+            assertTrue (shape.contains(5, 5));
+            assertFalse(shape.contains(15, 5));
+        }
+    }
 }

Reply via email to