This is an automated email from the ASF dual-hosted git repository. bchapuis pushed a commit to branch osm-test in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git
commit 3f70c035787dcfcdcff5676ea551cf9e9433704e Author: Bertil Chapuis <[email protected]> AuthorDate: Mon Jan 1 19:59:25 2024 +0100 Activate tests for the pbf reader --- .../org/apache/baremaps/utils/GeometryUtils.java | 2 +- .../apache/baremaps/utils/RoundingTransformer.java | 62 +++++++++++ .../apache/baremaps/openstreetmap/OsmTestData.java | 113 +++++++++++---------- 3 files changed, 125 insertions(+), 52 deletions(-) diff --git a/baremaps-core/src/main/java/org/apache/baremaps/utils/GeometryUtils.java b/baremaps-core/src/main/java/org/apache/baremaps/utils/GeometryUtils.java index 5dc9a212..5ab6e803 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/utils/GeometryUtils.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/utils/GeometryUtils.java @@ -32,7 +32,7 @@ import org.locationtech.proj4j.CoordinateTransformFactory; public class GeometryUtils { public static final GeometryFactory GEOMETRY_FACTORY_WGS84 = - new GeometryFactory(new PrecisionModel(1000000000), 4326); + new GeometryFactory(new PrecisionModel(), 4326); private GeometryUtils() {} diff --git a/baremaps-core/src/main/java/org/apache/baremaps/utils/RoundingTransformer.java b/baremaps-core/src/main/java/org/apache/baremaps/utils/RoundingTransformer.java new file mode 100644 index 00000000..40cbd0f8 --- /dev/null +++ b/baremaps-core/src/main/java/org/apache/baremaps/utils/RoundingTransformer.java @@ -0,0 +1,62 @@ +/* + * 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.baremaps.utils; + +import org.locationtech.jts.geom.CoordinateSequence; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.util.GeometryTransformer; + +/** + * A transformer that rounds the coordinates of a geometry to a given precision. + */ +public class RoundingTransformer extends GeometryTransformer { + + private int precision; + + /** + * Constructs a transformer that rounds the coordinates of a geometry to a given precision. + * + * @param precision the precision + */ + public RoundingTransformer(int precision) { + this.precision = precision; + } + + /** + * Rounds the coordinates of a geometry to a given precision. + * + * @param sequence the coordinate sequence + * @param parent the parent geometry + * @return the geometry + */ + @Override + protected CoordinateSequence transformCoordinates(CoordinateSequence sequence, Geometry parent) { + CoordinateSequence rounded = super.transformCoordinates(sequence, parent); + for (int i = 0; i < rounded.size(); i++) { + double roundedX = + Math.round(rounded.getOrdinate(i, CoordinateSequence.X) * Math.pow(10, precision)) + / Math.pow(10, precision); + double roundedY = + Math.round(rounded.getOrdinate(i, CoordinateSequence.Y) * Math.pow(10, precision)) + / Math.pow(10, precision); + rounded.setOrdinate(i, CoordinateSequence.X, roundedX); + rounded.setOrdinate(i, CoordinateSequence.Y, roundedY); + } + return rounded; + } +} diff --git a/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OsmTestData.java b/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OsmTestData.java index bed1dfe7..ac228a08 100644 --- a/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OsmTestData.java +++ b/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OsmTestData.java @@ -27,9 +27,12 @@ import java.util.HashMap; import java.util.Map; import java.util.stream.Stream; import org.apache.baremaps.openstreetmap.model.Element; +import org.apache.baremaps.openstreetmap.model.Entity; +import org.apache.baremaps.openstreetmap.pbf.PbfEntityReader; import org.apache.baremaps.openstreetmap.store.MockDataMap; import org.apache.baremaps.openstreetmap.xml.XmlEntityReader; import org.apache.baremaps.testing.TestFiles; +import org.apache.baremaps.utils.RoundingTransformer; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; @@ -53,68 +56,75 @@ public class OsmTestData { .map(OsmTest::new) .filter(OsmTest::isValid) .sorted() - .map(this::createDynamicTest) + .flatMap(this::createDynamicTest) .toList().stream(); } } @NotNull - private DynamicTest createDynamicTest(OsmTest testFile) { - var displayName = String.format("%s: %s", testFile.getId(), testFile.getDescription()); - return DynamicTest.dynamicTest(displayName, () -> runTest(testFile)); + private Stream<DynamicTest> createDynamicTest(OsmTest osmTest) { + String displayNameFormat = "%s (%s): %s"; + var xmlDisplayName = + String.format(displayNameFormat, osmTest.getId(), "xml", osmTest.getDescription()); + var pbfDisplayName = + String.format(displayNameFormat, osmTest.getId(), "pbf", osmTest.getDescription()); + return Stream.<DynamicTest>builder() + .add(DynamicTest.dynamicTest(xmlDisplayName, () -> runTest(osmTest, new XmlEntityReader() + .coordinateMap(new MockDataMap<>()) + .referenceMap(new MockDataMap<>()) + .geometries(true) + .stream(Files.newInputStream(osmTest.getOsmXml()))))) + .add(DynamicTest.dynamicTest(pbfDisplayName, () -> runTest(osmTest, new PbfEntityReader() + .coordinateMap(new MockDataMap<>()) + .referenceMap(new MockDataMap<>()) + .geometries(true) + .stream(Files.newInputStream(osmTest.getOsmPbf()))))) + .build(); } - public void runTest(OsmTest osmTest) { - try { - var elements = new XmlEntityReader() - .coordinateMap(new MockDataMap<>()) - .referenceMap(new MockDataMap<>()) - .geometries(true) - .stream(Files.newInputStream(osmTest.getOsmXml())) - .filter(e -> e instanceof Element) - .map(e -> (Element) e) - .toList(); - - for (var element : elements) { - // Each element should have a geometry - var id = element.getId(); - var fileGeometry = element.getGeometry(); - assertNotNull(fileGeometry); - - // Prepare the test geometry - var testWkt = osmTest.getWkts().get(id); - Geometry testGeometry = null; - try { - testGeometry = new WKTReader().read(testWkt); - } catch (Exception e) { - // ignore - } - if (testGeometry instanceof LineString lineString - && lineString.isClosed()) { - testGeometry = - testGeometry.getFactory().createPolygon(lineString.getCoordinateSequence()); - } - if (testGeometry instanceof MultiPolygon multiPolygon - && multiPolygon.getNumGeometries() == 1) { - testGeometry = multiPolygon.getGeometryN(0); - } - if (!testGeometry.isValid()) { - var geometryFixer = new GeometryFixer(testGeometry); - testGeometry = geometryFixer.getResult(); - } + public void runTest(OsmTest osmTest, Stream<Entity> entities) { + var elements = entities + .filter(e -> e instanceof Element) + .map(e -> (Element) e) + .toList(); + + for (var element : elements) { + // Each element should have a geometry + var id = element.getId(); + var fileGeometry = element.getGeometry(); + assertNotNull(fileGeometry); + + // Prepare the test geometry + var testWkt = osmTest.getWkts().get(id); + Geometry testGeometry = null; + try { + testGeometry = new WKTReader().read(testWkt); + } catch (Exception e) { + // ignore + } + if (testGeometry instanceof LineString lineString + && lineString.isClosed()) { + testGeometry = + testGeometry.getFactory().createPolygon(lineString.getCoordinateSequence()); + } + if (testGeometry instanceof MultiPolygon multiPolygon + && multiPolygon.getNumGeometries() == 1) { + testGeometry = multiPolygon.getGeometryN(0); + } + if (!testGeometry.isValid()) { + var geometryFixer = new GeometryFixer(testGeometry); + testGeometry = geometryFixer.getResult(); + } - // The test geometry and the file geometry should be equal - var message = String.format("%s: %s\nExpected:\n%s\nActual:\n%s", - osmTest.getId(), osmTest.getDescription(), testGeometry, fileGeometry); + // The test geometry and the file geometry should be equal + var message = String.format("%s: %s\nExpected:\n%s\nActual:\n%s", + osmTest.getId(), osmTest.getDescription(), testGeometry, fileGeometry); - testGeometry.normalize(); - fileGeometry.normalize(); + RoundingTransformer transformer = new RoundingTransformer(2); + fileGeometry = transformer.transform(fileGeometry); - assertTrue(message, testGeometry.equalsTopo(fileGeometry)); + assertTrue(message, testGeometry.equalsTopo(fileGeometry)); - } - } catch (IOException e) { - throw new RuntimeException(e); } } @@ -251,4 +261,5 @@ public class OsmTestData { return Long.compare(getId(), o.getId()); } } + }
