1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
9 |
|
---|
10 | import java.io.ByteArrayInputStream;
|
---|
11 | import java.io.InputStream;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.nio.file.Files;
|
---|
14 | import java.nio.file.Paths;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.List;
|
---|
18 | import java.util.Optional;
|
---|
19 | import java.util.stream.IntStream;
|
---|
20 |
|
---|
21 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
22 | import org.junit.jupiter.api.Test;
|
---|
23 | import org.openstreetmap.josm.TestUtils;
|
---|
24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
25 | import org.openstreetmap.josm.data.osm.Node;
|
---|
26 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
27 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
28 | import org.openstreetmap.josm.data.osm.Way;
|
---|
29 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Unit tests of {@link GeoJSONReader}.
|
---|
33 | */
|
---|
34 | class GeoJSONReaderTest {
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Setup test.
|
---|
38 | */
|
---|
39 | @RegisterExtension
|
---|
40 | public JOSMTestRules rules = new JOSMTestRules();
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Test reading a GeoJSON file.
|
---|
44 | * @throws Exception in case of error
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | void testReadGeoJson() throws Exception {
|
---|
48 | try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "geo.json"))) {
|
---|
49 | final List<OsmPrimitive> primitives = new ArrayList<>(new GeoJSONReader()
|
---|
50 | .doParseDataSet(in, null)
|
---|
51 | .getPrimitives(it -> true));
|
---|
52 |
|
---|
53 | assertExpectedGeoPrimitives(primitives);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Tests reading a GeoJSON file that is line by line separated, per RFC 7464
|
---|
59 | * @throws Exception in case of an error
|
---|
60 | */
|
---|
61 | @Test
|
---|
62 | void testReadLineByLineGeoJSON() throws Exception {
|
---|
63 | try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "geoLineByLine.json"))) {
|
---|
64 | final List<OsmPrimitive> primitives = new ArrayList<>(new GeoJSONReader()
|
---|
65 | .doParseDataSet(in, null)
|
---|
66 | .getPrimitives(it -> true));
|
---|
67 |
|
---|
68 | assertExpectedGeoPrimitives(primitives);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void assertExpectedGeoPrimitives(Collection<OsmPrimitive> primitives) {
|
---|
73 | assertEquals(20, primitives.size());
|
---|
74 |
|
---|
75 | final Node node1 = new Node(new LatLon(0.5, 102.0));
|
---|
76 | final Optional<OsmPrimitive> foundNode1 = primitives.stream()
|
---|
77 | .filter(it -> areEqualNodes(it, node1))
|
---|
78 | .findFirst();
|
---|
79 | assertTrue(foundNode1.isPresent());
|
---|
80 | assertEquals("valueA", foundNode1.get().get("propA"));
|
---|
81 |
|
---|
82 | final Way way1 = new Way();
|
---|
83 | way1.addNode(new Node(new LatLon(0.5, 102.0)));
|
---|
84 | way1.addNode(new Node(new LatLon(1, 103)));
|
---|
85 | way1.addNode(new Node(new LatLon(0, 104)));
|
---|
86 | way1.addNode(new Node(new LatLon(1, 105)));
|
---|
87 | final Optional<OsmPrimitive> foundWay1 = primitives.stream()
|
---|
88 | .filter(it -> areEqualWays(it, way1))
|
---|
89 | .findFirst();
|
---|
90 | assertTrue(foundWay1.isPresent());
|
---|
91 | assertEquals("valueB", foundWay1.get().get("propB"));
|
---|
92 | assertEquals("0.0", foundWay1.get().get("propB2"));
|
---|
93 | assertEquals(foundNode1.get(), ((Way) foundWay1.get()).firstNode());
|
---|
94 | assertEquals("valueA", ((Way) foundWay1.get()).firstNode().get("propA"));
|
---|
95 |
|
---|
96 | final Way way2 = new Way();
|
---|
97 | way2.addNode(new Node(new LatLon(40, 180)));
|
---|
98 | way2.addNode(new Node(new LatLon(50, 180)));
|
---|
99 | way2.addNode(new Node(new LatLon(50, 170)));
|
---|
100 | way2.addNode(new Node(new LatLon(40, 170)));
|
---|
101 | way2.addNode(new Node(new LatLon(40, 180)));
|
---|
102 | final Optional<OsmPrimitive> foundWay2 = primitives.stream()
|
---|
103 | .filter(it -> areEqualWays(it, way2))
|
---|
104 | .findFirst();
|
---|
105 | assertTrue(foundWay2.isPresent());
|
---|
106 | assertEquals(
|
---|
107 | ((Way) foundWay2.get()).getNode(0),
|
---|
108 | ((Way) foundWay2.get()).getNode(((Way) foundWay2.get()).getNodesCount() - 1)
|
---|
109 | );
|
---|
110 |
|
---|
111 | final Way way3 = new Way();
|
---|
112 | way3.addNode(new Node(new LatLon(40, -170)));
|
---|
113 | way3.addNode(new Node(new LatLon(50, -170)));
|
---|
114 | way3.addNode(new Node(new LatLon(50, -180)));
|
---|
115 | way3.addNode(new Node(new LatLon(40, -180)));
|
---|
116 | way3.addNode(new Node(new LatLon(40, -170)));
|
---|
117 | final Optional<OsmPrimitive> foundWay3 = primitives.stream()
|
---|
118 | .filter(it -> areEqualWays(it, way3))
|
---|
119 | .findFirst();
|
---|
120 | assertTrue(foundWay3.isPresent());
|
---|
121 | assertEquals(
|
---|
122 | ((Way) foundWay3.get()).getNode(0),
|
---|
123 | ((Way) foundWay3.get()).getNode(((Way) foundWay3.get()).getNodesCount() - 1)
|
---|
124 | );
|
---|
125 |
|
---|
126 | final Way way4 = new Way();
|
---|
127 | way4.addNode(new Node(new LatLon(0, 100)));
|
---|
128 | way4.addNode(new Node(new LatLon(0, 101)));
|
---|
129 | way4.addNode(new Node(new LatLon(1, 101)));
|
---|
130 | way4.addNode(new Node(new LatLon(1, 100)));
|
---|
131 | way4.addNode(new Node(new LatLon(0, 100)));
|
---|
132 | final Optional<OsmPrimitive> foundWay4 = primitives.stream()
|
---|
133 | .filter(it -> areEqualWays(it, way4))
|
---|
134 | .findFirst();
|
---|
135 | assertTrue(foundWay4.isPresent());
|
---|
136 | assertEquals(
|
---|
137 | ((Way) foundWay4.get()).getNode(0),
|
---|
138 | ((Way) foundWay4.get()).getNode(((Way) foundWay4.get()).getNodesCount() - 1)
|
---|
139 | );
|
---|
140 | assertEquals("valueD", foundWay4.get().get("propD"));
|
---|
141 | assertFalse(foundWay4.get().hasTag("propD2"));
|
---|
142 | assertEquals("true", foundWay4.get().get("propD3"));
|
---|
143 | assertFalse(foundWay4.get().hasKey("propD4"));
|
---|
144 | assertNull(foundWay4.get().get("propD4"));
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Test reading a GeoJSON file with a named CRS.
|
---|
149 | * @throws Exception in case of error
|
---|
150 | */
|
---|
151 | @Test
|
---|
152 | void testReadGeoJsonNamedCrs() throws Exception {
|
---|
153 | try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "geocrs.json"))) {
|
---|
154 | final List<OsmPrimitive> primitives = new ArrayList<>(new GeoJSONReader()
|
---|
155 | .doParseDataSet(in, null)
|
---|
156 | .getPrimitives(it -> true));
|
---|
157 | assertEquals(24, primitives.size());
|
---|
158 | assertTrue(primitives.stream()
|
---|
159 | .anyMatch(it -> areEqualNodes(it, new Node(new LatLon(52.5840213, 13.1724145)))));
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Test reading a JSON file which is not a proper GeoJSON (type missing).
|
---|
165 | */
|
---|
166 | @Test
|
---|
167 | void testReadGeoJsonWithoutType() {
|
---|
168 | assertThrows(IllegalDataException.class, () ->
|
---|
169 | new GeoJSONReader().doParseDataSet(new ByteArrayInputStream("{}".getBytes(StandardCharsets.UTF_8)), null));
|
---|
170 | }
|
---|
171 |
|
---|
172 | private static boolean areEqualNodes(final OsmPrimitive p1, final OsmPrimitive p2) {
|
---|
173 | return (p1 instanceof Node)
|
---|
174 | && (p2 instanceof Node)
|
---|
175 | && ((Node) p1).getCoor().equalsEpsilon(((Node) p2).getCoor());
|
---|
176 | }
|
---|
177 |
|
---|
178 | private static boolean areEqualWays(final OsmPrimitive p1, final OsmPrimitive p2) {
|
---|
179 | if (
|
---|
180 | (!(p1 instanceof Way))
|
---|
181 | || (!(p2 instanceof Way))
|
---|
182 | || ((Way) p1).getNodes().size() != ((Way) p2).getNodes().size()
|
---|
183 | ) {
|
---|
184 | return false;
|
---|
185 | }
|
---|
186 | return IntStream.range(0, ((Way) p1).getNodes().size())
|
---|
187 | .allMatch(i -> areEqualNodes(((Way) p1).getNode(i), ((Way) p2).getNode(i)));
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/19822">Bug #19822</a>.
|
---|
192 | * @throws Exception in case of error
|
---|
193 | */
|
---|
194 | @Test
|
---|
195 | void testTicket19822() throws Exception {
|
---|
196 | try (InputStream in = TestUtils.getRegressionDataStream(19822, "data.geojson")) {
|
---|
197 | final List<OsmPrimitive> primitives = new ArrayList<>(
|
---|
198 | new GeoJSONReader().doParseDataSet(in, null).getPrimitives(it -> true));
|
---|
199 | assertTrue(primitives.stream().anyMatch(p -> p instanceof Relation && p.isMultipolygon()));
|
---|
200 | assertEquals(3, primitives.stream().filter(Way.class::isInstance).count());
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/19822">Bug #19822</a>.
|
---|
206 | * @throws Exception in case of error
|
---|
207 | */
|
---|
208 | @Test
|
---|
209 | void testTicket19822Nested() throws Exception {
|
---|
210 | try (InputStream in = TestUtils.getRegressionDataStream(19822, "problem3.geojson")) {
|
---|
211 | final List<OsmPrimitive> primitives = new ArrayList<>(
|
---|
212 | new GeoJSONReader().doParseDataSet(in, null).getPrimitives(it -> true));
|
---|
213 | assertTrue(primitives.stream().anyMatch(p -> p instanceof Relation && p.isMultipolygon()));
|
---|
214 | assertEquals(3, primitives.stream().filter(Way.class::isInstance).count());
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | }
|
---|