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

bchapuis pushed a commit to branch refactoring
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git

commit 33cd290536468eecb2022feb45a3e1ae3371ec95
Author: Bertil Chapuis <[email protected]>
AuthorDate: Fri Dec 23 00:40:24 2022 +0100

    Add a consumer that collects geometries in a map
---
 .../baremaps/collection/type/GeometryDataType.java | 42 ++++++++++++++++++
 .../function/CoordinateMapBuilder.java             |  2 +-
 .../openstreetmap/function/GeometryMapBuilder.java | 51 ++++++++++++++++++++++
 .../function/ReferenceMapBuilder.java              |  2 +-
 .../openstreetmap/function/WayGeometryBuilder.java |  5 +--
 .../baremaps/collection/type/DataTypeProvider.java |  6 +++
 6 files changed, 103 insertions(+), 5 deletions(-)

diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/collection/type/GeometryDataType.java
 
b/baremaps-core/src/main/java/org/apache/baremaps/collection/type/GeometryDataType.java
new file mode 100644
index 00000000..4227eb37
--- /dev/null
+++ 
b/baremaps-core/src/main/java/org/apache/baremaps/collection/type/GeometryDataType.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed 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.baremaps.collection.type;
+
+
+
+import java.nio.ByteBuffer;
+import org.apache.baremaps.openstreetmap.utils.GeometryUtils;
+import org.locationtech.jts.geom.Geometry;
+
+public class GeometryDataType implements DataType<Geometry> {
+
+  @Override
+  public int size(Geometry value) {
+    return 4 + GeometryUtils.serialize(value).length;
+  }
+
+  @Override
+  public void write(ByteBuffer buffer, int position, Geometry value) {
+    byte[] bytes = GeometryUtils.serialize(value);
+    buffer.putInt(position, bytes.length);
+    buffer.put(position + 4, bytes);
+  }
+
+  @Override
+  public Geometry read(ByteBuffer buffer, int position) {
+    int length = buffer.getInt(position);
+    byte[] bytes = new byte[length];
+    buffer.get(position + 4, bytes);
+    return GeometryUtils.deserialize(bytes);
+  }
+}
diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CoordinateMapBuilder.java
 
b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CoordinateMapBuilder.java
index f4f10749..38ba64fb 100644
--- 
a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CoordinateMapBuilder.java
+++ 
b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CoordinateMapBuilder.java
@@ -21,7 +21,7 @@ import org.apache.baremaps.openstreetmap.model.DataBlock;
 import org.apache.baremaps.stream.StreamException;
 import org.locationtech.jts.geom.Coordinate;
 
-/** A consumer that caches openstreetmap coordinates in a map. */
+/** A consumer that stores openstreetmap coordinates in a map. */
 public class CoordinateMapBuilder implements Consumer<Block> {
 
   private final LongDataMap<Coordinate> coordinateMap;
diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/GeometryMapBuilder.java
 
b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/GeometryMapBuilder.java
new file mode 100644
index 00000000..2828b496
--- /dev/null
+++ 
b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/GeometryMapBuilder.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed 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.baremaps.openstreetmap.function;
+
+
+
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+import org.apache.baremaps.collection.LongDataMap;
+import org.apache.baremaps.openstreetmap.model.Element;
+import org.apache.baremaps.openstreetmap.model.Entity;
+import org.locationtech.jts.geom.Geometry;
+
+/**
+ * A consumer that stores the geometry of an element in a map.
+ */
+public class GeometryMapBuilder implements Consumer<Entity> {
+
+  private final LongDataMap<Geometry> geometryMap;
+
+  private final Predicate<Entity> filter;
+
+  /**
+   * Constructs a {@code GeometryMapBuilder} with the provided map.
+   *
+   * @param geometryMap the geometry map
+   * @param filter the entity filter
+   */
+  public GeometryMapBuilder(LongDataMap<Geometry> geometryMap, 
Predicate<Entity> filter) {
+    this.geometryMap = geometryMap;
+    this.filter = filter;
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public void accept(Entity entity) {
+    if (filter.test(entity) && entity instanceof Element element) {
+      geometryMap.put(element.getId(), element.getGeometry());
+    }
+  }
+}
diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ReferenceMapBuilder.java
 
b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ReferenceMapBuilder.java
index 14791908..8be71b0b 100644
--- 
a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ReferenceMapBuilder.java
+++ 
b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/ReferenceMapBuilder.java
@@ -21,7 +21,7 @@ import org.apache.baremaps.openstreetmap.model.Block;
 import org.apache.baremaps.openstreetmap.model.DataBlock;
 import org.apache.baremaps.stream.StreamException;
 
-/** A consumer that caches openstreetmap references in a map. */
+/** A consumer that stores openstreetmap references in a map. */
 public class ReferenceMapBuilder implements Consumer<Block> {
 
   private final LongDataMap<List<Long>> referenceMap;
diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/WayGeometryBuilder.java
 
b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/WayGeometryBuilder.java
index 7ffad58d..3d6f21bc 100644
--- 
a/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/WayGeometryBuilder.java
+++ 
b/baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/WayGeometryBuilder.java
@@ -52,9 +52,8 @@ public class WayGeometryBuilder implements Consumer<Way> {
       if (!line.isEmpty()) {
         // Ways can be open or closed depending on the geometry or the tags:
         // https://wiki.openstreetmap.org/wiki/Way
-        if (!line.isClosed()
-          || way.getTags().containsKey("highway")
-          || way.getTags().containsKey("barrier")) {
+        if (!line.isClosed() || way.getTags().containsKey("highway")
+            || way.getTags().containsKey("barrier")) {
           way.setGeometry(line);
         } else {
           Polygon polygon = 
GEOMETRY_FACTORY_WGS84.createPolygon(line.getCoordinates());
diff --git 
a/baremaps-core/src/test/java/org/apache/baremaps/collection/type/DataTypeProvider.java
 
b/baremaps-core/src/test/java/org/apache/baremaps/collection/type/DataTypeProvider.java
index 1d3b8eae..f6d53d92 100644
--- 
a/baremaps-core/src/test/java/org/apache/baremaps/collection/type/DataTypeProvider.java
+++ 
b/baremaps-core/src/test/java/org/apache/baremaps/collection/type/DataTypeProvider.java
@@ -18,6 +18,7 @@ import java.util.List;
 import java.util.stream.Stream;
 import org.junit.jupiter.params.provider.Arguments;
 import org.locationtech.jts.geom.Coordinate;
+import org.locationtech.jts.geom.GeometryFactory;
 
 public class DataTypeProvider {
 
@@ -29,6 +30,11 @@ public class DataTypeProvider {
         Arguments.of(new DoubleListDataType(), List.of((double) 1, (double) 2, 
(double) 3)),
         Arguments.of(new FloatDataType(), (float) 1),
         Arguments.of(new FloatListDataType(), List.of((float) 1, (float) 2, 
(float) 3)),
+        Arguments.of(new GeometryDataType(),
+            new GeometryFactory().createPoint(new Coordinate(1, 1))),
+        Arguments.of(new GeometryDataType(),
+            new GeometryFactory()
+                .createLineString(new Coordinate[] {new Coordinate(1, 1), new 
Coordinate(2, 2)})),
         Arguments.of(new IntegerDataType(), 1),
         Arguments.of(new IntegerListDataType(), List.of(1, 2, 3)),
         Arguments.of(new LongDataType(), (long) 1),

Reply via email to