sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/07: Define a mapping between `GeometryType` and the integer codes used in Well-Known Binary (WKB).
Date Thu, 29 Jul 2021 13:07:16 GMT
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 7544551ad9a5041ef39567f5b365af5316ccf394
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Jul 23 12:45:10 2021 +0200

    Define a mapping between `GeometryType` and the integer codes used in Well-Known Binary
(WKB).
---
 .../apache/sis/internal/feature/GeometryType.java  | 77 ++++++++++++++++++++++
 .../sis/internal/feature/GeometryTypeTest.java     | 54 +++++++++++++++
 .../apache/sis/test/suite/FeatureTestSuite.java    |  1 +
 3 files changed, 132 insertions(+)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryType.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryType.java
index 04c028e..f432a9d 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryType.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/internal/feature/GeometryType.java
@@ -16,6 +16,8 @@
  */
 package org.apache.sis.internal.feature;
 
+import java.util.Locale;
+
 
 /**
  * Implementation-neutral description of the type of geometry.
@@ -80,4 +82,79 @@ public enum GeometryType {
      * Set of geometries of any type except other geometry collection.
      */
     GEOMETRY_COLLECTION;
+
+    /**
+     * The type of this geometry as specified in Well-Known Binary (WKB) specification.
+     * This is also the integer value declared in the {@code "GEOMETRY_TYPE"} column of
+     * the {@code "GEOMETRY_COLUMNS} table of a spatial database.
+     *
+     * <p>The WKB specification defines values in the [0 … 15] range for 2D geometries
+     * and adds 1000 for geometries having <var>Z</var> values.
+     * Then 2000 is added again for geometries having <var>M</var> values.</p>
+     *
+     * @return the geometry type specified in WKB specification.
+     *
+     * @see #forBinaryType(int)
+     */
+    public final int binaryType() {
+        return ordinal();
+    }
+
+    /**
+     * Returns the enumeration value for the given name.
+     * This method is case-insensitive.
+     *
+     * @param  name  the geometry type name, or {@code null}.
+     * @return enumeration value for the given name, or {@code null} if the name was null.
+     * @throws IllegalArgumentException if the name is not recognized.
+     */
+    public static GeometryType forName(String name) {
+        if (name != null) {
+            name = name.trim().toUpperCase(Locale.US);
+            int length = name.length();
+            if (length > 0) {
+                // Remove Z, M or ZM suffix.
+                if (/*non-empty*/ name.charAt(length - 1) == 'M') length--;
+                if (length > 0 && name.charAt(length - 1) == 'Z') length--;
+                name = name.substring(0, length);
+                switch (name) {
+                    case "MULTIPOINT":      return MULTI_POINT;
+                    case "MULTILINESTRING": return MULTI_LINESTRING;
+                    case "MULTIPOLYGON":    return MULTI_POLYGON;
+                    case "GEOMCOLLECTION":  return GEOMETRY_COLLECTION;
+                    default: return valueOf(name);
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Returns the enumeration value for the given WKB type, or {@code null} if unknown.
+     * Types for geometries having <var>Z</var> and <var>M</var>
are replaced by 2D types.
+     *
+     * @param  type  WKB geometry type.
+     * @return enumeration value for the given type, or {@code null}.
+     *
+     * @see #binaryType()
+     */
+    public static GeometryType forBinaryType(int type) {
+        if (type >= 1000 && type < 4000) {
+            type %= 1000;
+        }
+        switch (type) {
+            default: return null;
+            case 0:  return GEOMETRY;
+            case 1:  return POINT;
+            case 2:  return LINESTRING;
+            case 3:  return POLYGON;
+            case 4:  return MULTI_POINT;
+            case 5:  return MULTI_LINESTRING;
+            case 6:  return MULTI_POLYGON;
+            case 7:  return GEOMETRY_COLLECTION;
+        //  case 13: return CURVE;
+        //  case 14: return SURFACE;
+        //  case 15: return POLYHEDRALSURFACE;
+        }
+    }
 }
diff --git a/core/sis-feature/src/test/java/org/apache/sis/internal/feature/GeometryTypeTest.java
b/core/sis-feature/src/test/java/org/apache/sis/internal/feature/GeometryTypeTest.java
new file mode 100644
index 0000000..713a613
--- /dev/null
+++ b/core/sis-feature/src/test/java/org/apache/sis/internal/feature/GeometryTypeTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.internal.feature;
+
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests {@link GeometryType}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 1.1
+ * @since   1.1
+ * @module
+ */
+public final strictfp class GeometryTypeTest extends TestCase {
+    /**
+     * Tests {@link GeometryType#forBinaryType(int)} and verifies {@link GeometryType#binaryType()}
values.
+     */
+    @Test
+    public void testBinaryType() {
+        for (final GeometryType type : GeometryType.values()) {
+            assertSame(type.name(), type, GeometryType.forBinaryType(type.binaryType()));
+        }
+    }
+
+    /**
+     * Tests {@link GeometryType#forName(String)}.
+     */
+    @Test
+    public void testForName() {
+        assertSame(GeometryType.MULTI_POLYGON, GeometryType.forName("multi_polygon"));
+        assertSame(GeometryType.MULTI_POLYGON, GeometryType.forName("MULTIPOLYGON"));
+        assertSame(GeometryType.GEOMETRY_COLLECTION, GeometryType.forName("GEOMETRY_COLLECTION"));
+        assertSame(GeometryType.GEOMETRY_COLLECTION, GeometryType.forName("GeomCollection"));
+    }
+}
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 22f6f13..ed26d95 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
@@ -60,6 +60,7 @@ import org.junit.runners.Suite;
     org.apache.sis.filter.TemporalFunctionTest.class,
     org.apache.sis.filter.SpatialUsingJTS_Test.class,
     org.apache.sis.internal.feature.AttributeConventionTest.class,
+    org.apache.sis.internal.feature.GeometryTypeTest.class,
     org.apache.sis.internal.filter.FunctionNamesTest.class,
     org.apache.sis.internal.filter.sqlmm.SQLMMTest.class,
     org.apache.sis.internal.filter.sqlmm.RegistryUsingJTS_Test.class,

Mime
View raw message