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 3d84f4f38a8783a7da90caa67db25e4ff5211f8c
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Thu May 4 12:47:21 2023 +0200

    More conservative handling of nested operations in 
`GroupAsPolylineOperation`.
---
 .../sis/feature/GroupAsPolylineOperation.java      | 24 ++++----
 .../java/org/apache/sis/feature/LinkOperation.java |  2 +-
 .../sis/internal/coverage/j2d/ObservableImage.java |  2 +-
 .../apache/sis/feature/FeatureOperationsTest.java  |  2 +-
 .../sis/feature/GroupAsPolylineOperationTest.java  | 71 ++++++++++++++++++++++
 .../apache/sis/test/suite/FeatureTestSuite.java    |  1 +
 6 files changed, 88 insertions(+), 14 deletions(-)

diff --git 
a/core/sis-feature/src/main/java/org/apache/sis/feature/GroupAsPolylineOperation.java
 
b/core/sis-feature/src/main/java/org/apache/sis/feature/GroupAsPolylineOperation.java
index ee206a37d1..59f515554d 100644
--- 
a/core/sis-feature/src/main/java/org/apache/sis/feature/GroupAsPolylineOperation.java
+++ 
b/core/sis-feature/src/main/java/org/apache/sis/feature/GroupAsPolylineOperation.java
@@ -89,22 +89,24 @@ final class GroupAsPolylineOperation extends 
AbstractOperation {
      * @param  components      attribute, association or operation providing 
the geometries to group as a polyline.
      */
     static Operation create(final Map<String,?> identification, final 
GeometryLibrary library, PropertyType components) {
-        FeatureAssociationRole association = 
Features.toAssociation(components).orElse(null);
-        if (association != null && association.getMaximumOccurs() == 1) {
-            components = association;
+        if (components instanceof LinkOperation) {
+            components = ((LinkOperation) components).result;
+        }
+        final boolean isFeatureAssociation;
+        if (components instanceof AttributeType<?>) {
+            if (((AttributeType<?>) components).getMaximumOccurs() <= 1) {
+                return new LinkOperation(identification, components);
+            }
+            isFeatureAssociation = false;
         } else {
-            association = null;
-            AttributeType<?> attribute = 
Features.toAttribute(components).orElse(null);
-            if (attribute == null) {
+            isFeatureAssociation = (components instanceof 
FeatureAssociationRole)
+                    && ((FeatureAssociationRole) 
components).getMaximumOccurs() == 1;
+            if (!isFeatureAssociation) {
                 throw new 
IllegalArgumentException(Resources.format(Resources.Keys.IllegalPropertyType_2,
                                                    components.getName(), 
components.getClass()));
             }
-            if (attribute.getMaximumOccurs() <= 1) {
-                return new LinkOperation(identification, components);
-            }
-            components = attribute;
         }
-        return new GroupAsPolylineOperation(identification, 
Geometries.implementation(library), components, association != null);
+        return new GroupAsPolylineOperation(identification, 
Geometries.implementation(library), components, isFeatureAssociation);
     }
 
     /**
diff --git 
a/core/sis-feature/src/main/java/org/apache/sis/feature/LinkOperation.java 
b/core/sis-feature/src/main/java/org/apache/sis/feature/LinkOperation.java
index e974d358da..d195ebe596 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/feature/LinkOperation.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/feature/LinkOperation.java
@@ -55,7 +55,7 @@ final class LinkOperation extends AbstractOperation {
      * The type of the result.
      */
     @SuppressWarnings("serial")         // Most SIS implementations are 
serializable.
-    private final PropertyType result;
+    final PropertyType result;
 
     /**
      * The name of the referenced attribute or feature association.
diff --git 
a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ObservableImage.java
 
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ObservableImage.java
index 07279f0ef8..9872e12b3b 100644
--- 
a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ObservableImage.java
+++ 
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ObservableImage.java
@@ -278,7 +278,7 @@ public class ObservableImage extends BufferedImage {
         fireTileUpdate(count, true);
         try {
             // Do not use super.setData(…) because it does not handle 
correctly the float and double types.
-            getRaster().setRect(0, 0, data);
+            getRaster().setRect(data);
         } finally {
             synchronized (this) {
                 // Similar to `releaseWritableTile(…)` but without throwing 
exception.
diff --git 
a/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureOperationsTest.java
 
b/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureOperationsTest.java
index 3235dfad10..876556afc9 100644
--- 
a/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureOperationsTest.java
+++ 
b/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureOperationsTest.java
@@ -42,7 +42,7 @@ import org.opengis.feature.PropertyType;
 /**
  * Tests a feature combining various {@link FeatureOperations} applied of 
either sparse or dense features.
  * This is an integration test. For tests specific to a particular operation, 
see for example
- * {@link LinkOperation} or {@link EnvelopeOperation}.
+ * {@link LinkOperationTest} or {@link EnvelopeOperationTest}.
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
diff --git 
a/core/sis-feature/src/test/java/org/apache/sis/feature/GroupAsPolylineOperationTest.java
 
b/core/sis-feature/src/test/java/org/apache/sis/feature/GroupAsPolylineOperationTest.java
new file mode 100644
index 0000000000..e3285521d3
--- /dev/null
+++ 
b/core/sis-feature/src/test/java/org/apache/sis/feature/GroupAsPolylineOperationTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.feature;
+
+import java.util.Map;
+import java.util.Arrays;
+import com.esri.core.geometry.Point;
+import com.esri.core.geometry.Polyline;
+import org.apache.sis.feature.builder.FeatureTypeBuilder;
+import org.apache.sis.setup.GeometryLibrary;
+
+// Test dependencies
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+// Branch-dependent imports
+import org.opengis.feature.Attribute;
+import org.opengis.feature.Feature;
+import org.opengis.feature.Operation;
+import org.opengis.feature.Property;
+
+
+/**
+ * Tests {@link GroupAsPolylineOperation}.
+ *
+ * @author  Johann Sorel (Geomatys)
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 1.4
+ * @since   0.4
+ */
+public final class GroupAsPolylineOperationTest extends TestCase {
+    /**
+     * Tests a feature with a sequence of points.
+     */
+    @Test
+    public void testPoints() {
+        final FeatureTypeBuilder builder = new 
FeatureTypeBuilder().setName("test");
+        
builder.addAttribute(Point.class).setMaximumOccurs(10).setName("points");
+        final Feature feature = builder.build().newInstance();
+        feature.setPropertyValue("points", Arrays.asList(
+            new Point(-6, 4),
+            new Point(12, 7),
+            new Point( 8, 6)));
+
+        final Operation group = 
FeatureOperations.groupAsPolyline(Map.of("name", "polyline"),
+                                GeometryLibrary.ESRI, 
feature.getType().getProperty("points"));
+
+        final Property result = group.apply(feature, null);
+        final Object   value  = ((Attribute<?>) result).getValue();
+        final Polyline poly   = (Polyline) value;
+        assertEquals(-6, poly.getPoint(0).getX(), STRICT);
+        assertEquals( 7, poly.getPoint(1).getY(), STRICT);
+        assertEquals( 8, poly.getPoint(2).getX(), STRICT);
+    }
+}
diff --git 
a/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
 
b/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
index d0984716c4..a8ca5f5ba1 100644
--- 
a/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
+++ 
b/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
@@ -46,6 +46,7 @@ import org.junit.runners.Suite;
     org.apache.sis.feature.LinkOperationTest.class,
     org.apache.sis.feature.StringJoinOperationTest.class,
     org.apache.sis.feature.EnvelopeOperationTest.class,
+    org.apache.sis.feature.GroupAsPolylineOperationTest.class,
     org.apache.sis.feature.FeatureOperationsTest.class,
     org.apache.sis.feature.FeatureFormatTest.class,
     org.apache.sis.feature.FeaturesTest.class,

Reply via email to