sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ama...@apache.org
Subject [sis] 02/07: fix(Feature): When initializing a spatial operator, allow receiving bbox values
Date Fri, 30 Jul 2021 16:49:38 GMT
This is an automated email from the ASF dual-hosted git repository.

amanin pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit ae0272774736c4db96fd821c54c08cde5879f8e9
Author: Alexis Manin <alexis.manin@geomatys.com>
AuthorDate: Wed Jul 28 15:58:49 2021 +0200

    fix(Feature): When initializing a spatial operator, allow receiving bbox values
    
    Bbox values will be automatically converted to geometry
    
    TODO: a review might be necessary in the future to simplify conversion, because it is
maybe not necessary to run the conversion at this specific place.
---
 .../apache/sis/filter/BinaryGeometryFilter.java    |  6 ++++-
 .../apache/sis/internal/feature/Geometries.java    | 29 ++++++++++++++++++++++
 .../sis/internal/filter/GeometryConverter.java     |  5 +++-
 3 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/filter/BinaryGeometryFilter.java
b/core/sis-feature/src/main/java/org/apache/sis/filter/BinaryGeometryFilter.java
index 477e59e..8fd29c4 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/filter/BinaryGeometryFilter.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/filter/BinaryGeometryFilter.java
@@ -100,17 +100,21 @@ abstract class BinaryGeometryFilter<R,G> extends Node implements
SpatialOperator
          */
         final int index;
         final Literal<? super R, ?> literal;
+        final GeometryWrapper<G> value;
         if (geometry2 instanceof Literal<?,?>) {
             literal = (Literal<? super R, ?>) geometry2;
+            value = expression2.apply(null);
             index   = 1;
         } else if (geometry1 instanceof Literal<?,?>) {
             literal = (Literal<? super R, ?>) geometry1;
+            value = expression1.apply(null);
             index = 0;
         } else {
             literal = null;
+            value = null;
             index   = -1;
         }
-        final GeometryWrapper<G> value = (literal == null) ? null : library.castOrWrap(literal.getValue());
+
         try {
             context = new SpatialOperationContext(null, value, systemUnit, index);
             if (value != null) {
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
index f1c221a..fd8f124 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/Geometries.java
@@ -21,9 +21,12 @@ import java.io.Serializable;
 import java.nio.ByteBuffer;
 import java.util.Optional;
 import java.util.Iterator;
+import org.apache.sis.geometry.ImmutableEnvelope;
 import org.apache.sis.referencing.cs.AxesConvention;
+import org.apache.sis.util.UnconvertibleObjectException;
 import org.opengis.geometry.DirectPosition;
 import org.opengis.geometry.Envelope;
+import org.opengis.metadata.extent.GeographicBoundingBox;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.opengis.referencing.cs.CoordinateSystem;
 import org.opengis.referencing.cs.AxisDirection;
@@ -463,6 +466,32 @@ public abstract class Geometries<G> implements Serializable {
     }
 
     /**
+     * Convert a given object to a geometry. For now, only bounding boxes are supported.
No Wrap-around resolution
+     * is applied. This method is a workaround to attempt conversion of an arbitrary value
to a geometry. When more
+     * context is available, prefer using directly typed methods. For example, if you have
an {@link Envelope}, you
+     * should use {@link Geometries#toGeometry2D(Envelope, WraparoundMethod)} directly.
+     *
+     * @param value An object to map to Geometry API if needed. Currently supported convertible
types are
+     * {@link GeographicBoundingBox} and {@link Envelope}. If param is null or already converted,
it is returned as is.
+     * @param adjustmentStrategy The wrap-around method to use if we've got to convert an
envelope. If null, default to
+     *                           {@link WraparoundMethod#NONE}.
+     * @return The conversion result, possibly null.
+     * @throws UnconvertibleObjectException If no conversion strategy exists for given object.
+     * @see #toGeometry2D(Envelope, WraparoundMethod) envelope conversion method.
+     */
+    public GeometryWrapper<G> toGeometry(Object value, WraparoundMethod adjustmentStrategy)
throws UnconvertibleObjectException {
+        if (value == null) return null;
+        else try {
+            if (value instanceof GeographicBoundingBox) value = new ImmutableEnvelope((GeographicBoundingBox)
value);
+            // TODO: what wrap-around method to use ? Should we assume user must manage it
before-hand ?
+            if (value instanceof Envelope) value = toGeometry2D((Envelope) value, adjustmentStrategy
== null ? WraparoundMethod.NONE : adjustmentStrategy);
+            return castOrWrap(value);
+        } catch (RuntimeException e) {
+            throw new UnconvertibleObjectException("Geometry conversion: unsupported value
type: "+value.getClass().getCanonicalName(), e);
+        }
+    }
+
+    /**
      * Merges a sequence of points or polylines into a single polyline instances.
      * Each previous polyline will be a separated path in the new polyline instances.
      * The implementation returned by this method is an instance of {@link #rootClass}.
diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/filter/GeometryConverter.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/filter/GeometryConverter.java
index 0fed2a0..abf559c 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/filter/GeometryConverter.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/filter/GeometryConverter.java
@@ -19,6 +19,7 @@ package org.apache.sis.internal.filter;
 import java.util.List;
 import java.util.Collection;
 import java.util.Collections;
+import org.apache.sis.geometry.WraparoundMethod;
 import org.opengis.util.ScopedName;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
@@ -122,7 +123,9 @@ final class GeometryConverter<R,G> extends Node implements Optimization.OnExpres
      */
     @Override
     public GeometryWrapper<G> apply(final R input) {
-        return library.castOrWrap(expression.apply(input));
+        // TODO: review if this component should only cast geometries. If conversion is kept,
check how to propagate
+        // wrap-around method from build context.
+        return library.toGeometry(expression.apply(input), WraparoundMethod.NONE);
     }
 
     /**

Mime
View raw message