1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 |
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.nio.file.Files;
|
---|
11 | import java.nio.file.Paths;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Optional;
|
---|
15 |
|
---|
16 | import org.junit.Rule;
|
---|
17 | import org.junit.Test;
|
---|
18 | import org.openstreetmap.josm.TestUtils;
|
---|
19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
20 | import org.openstreetmap.josm.data.osm.Node;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Unit tests of {@link GeoJSONReader}.
|
---|
27 | */
|
---|
28 | public class GeoJSONReaderTest {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Setup test.
|
---|
32 | */
|
---|
33 | @Rule
|
---|
34 | public JOSMTestRules rules = new JOSMTestRules();
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Test reading a GeoJSON file.
|
---|
38 | * @throws Exception in case of error
|
---|
39 | */
|
---|
40 | @Test
|
---|
41 | public void test() throws Exception {
|
---|
42 | try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "geo.json"))) {
|
---|
43 | final List<OsmPrimitive> primitives = new ArrayList<>(new GeoJSONReader()
|
---|
44 | .doParseDataSet(in, null)
|
---|
45 | .getPrimitives(it -> true));
|
---|
46 | assertEquals(21, primitives.size());
|
---|
47 |
|
---|
48 | final Node node1 = new Node(new LatLon(0.5, 102.0));
|
---|
49 | final Optional<OsmPrimitive> foundNode1 = primitives.stream()
|
---|
50 | .filter(it -> areEqualNodes(it, node1))
|
---|
51 | .findFirst();
|
---|
52 | assertTrue(foundNode1.isPresent());
|
---|
53 | assertEquals("valueA", foundNode1.get().get("propA"));
|
---|
54 |
|
---|
55 | final Way way1 = new Way();
|
---|
56 | way1.addNode(new Node(new LatLon(0, 102)));
|
---|
57 | way1.addNode(new Node(new LatLon(1, 103)));
|
---|
58 | way1.addNode(new Node(new LatLon(0, 104)));
|
---|
59 | way1.addNode(new Node(new LatLon(1, 105)));
|
---|
60 | final Optional<OsmPrimitive> foundWay1 = primitives.stream()
|
---|
61 | .filter(it -> areEqualWays(it, way1))
|
---|
62 | .findFirst();
|
---|
63 | assertTrue(foundWay1.isPresent());
|
---|
64 | assertEquals("valueB", foundWay1.get().get("propB"));
|
---|
65 | assertEquals("0.0", foundWay1.get().get("propB2"));
|
---|
66 |
|
---|
67 | final Way way2 = new Way();
|
---|
68 | way2.addNode(new Node(new LatLon(40, 180)));
|
---|
69 | way2.addNode(new Node(new LatLon(50, 180)));
|
---|
70 | way2.addNode(new Node(new LatLon(50, 170)));
|
---|
71 | way2.addNode(new Node(new LatLon(40, 170)));
|
---|
72 | way2.addNode(new Node(new LatLon(40, 180)));
|
---|
73 | final Optional<OsmPrimitive> foundWay2 = primitives.stream()
|
---|
74 | .filter(it -> areEqualWays(it, way2))
|
---|
75 | .findFirst();
|
---|
76 | assertTrue(foundWay2.isPresent());
|
---|
77 | assertEquals(
|
---|
78 | ((Way) foundWay2.get()).getNode(0),
|
---|
79 | ((Way) foundWay2.get()).getNode(((Way) foundWay2.get()).getNodesCount() - 1)
|
---|
80 | );
|
---|
81 |
|
---|
82 | final Way way3 = new Way();
|
---|
83 | way3.addNode(new Node(new LatLon(40, -170)));
|
---|
84 | way3.addNode(new Node(new LatLon(50, -170)));
|
---|
85 | way3.addNode(new Node(new LatLon(50, -180)));
|
---|
86 | way3.addNode(new Node(new LatLon(40, -180)));
|
---|
87 | way3.addNode(new Node(new LatLon(40, -170)));
|
---|
88 | final Optional<OsmPrimitive> foundWay3 = primitives.stream()
|
---|
89 | .filter(it -> areEqualWays(it, way3))
|
---|
90 | .findFirst();
|
---|
91 | assertTrue(foundWay3.isPresent());
|
---|
92 | assertEquals(
|
---|
93 | ((Way) foundWay3.get()).getNode(0),
|
---|
94 | ((Way) foundWay3.get()).getNode(((Way) foundWay3.get()).getNodesCount() - 1)
|
---|
95 | );
|
---|
96 |
|
---|
97 | final Way way4 = new Way();
|
---|
98 | way4.addNode(new Node(new LatLon(0, 100)));
|
---|
99 | way4.addNode(new Node(new LatLon(0, 101)));
|
---|
100 | way4.addNode(new Node(new LatLon(1, 101)));
|
---|
101 | way4.addNode(new Node(new LatLon(1, 100)));
|
---|
102 | way4.addNode(new Node(new LatLon(0, 100)));
|
---|
103 | final Optional<OsmPrimitive> foundWay4 = primitives.stream()
|
---|
104 | .filter(it -> areEqualWays(it, way4))
|
---|
105 | .findFirst();
|
---|
106 | assertTrue(foundWay4.isPresent());
|
---|
107 | assertEquals(
|
---|
108 | ((Way) foundWay4.get()).getNode(0),
|
---|
109 | ((Way) foundWay4.get()).getNode(((Way) foundWay4.get()).getNodesCount() - 1)
|
---|
110 | );
|
---|
111 | assertEquals("valueD", foundWay4.get().get("propD"));
|
---|
112 | assertFalse(foundWay4.get().hasTag("propD2"));
|
---|
113 | assertEquals("true", foundWay4.get().get("propD3"));
|
---|
114 | assertFalse(foundWay4.get().hasKey("propD4"));
|
---|
115 | assertNull(foundWay4.get().get("propD4"));
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | private static boolean areEqualNodes(final OsmPrimitive p1, final OsmPrimitive p2) {
|
---|
120 | return (p1 instanceof Node)
|
---|
121 | && (p2 instanceof Node)
|
---|
122 | && ((Node) p1).getCoor().equals(((Node) p2).getCoor());
|
---|
123 | }
|
---|
124 |
|
---|
125 | private static boolean areEqualWays(final OsmPrimitive p1, final OsmPrimitive p2) {
|
---|
126 | if (
|
---|
127 | (!(p1 instanceof Way))
|
---|
128 | || (!(p2 instanceof Way))
|
---|
129 | || ((Way) p1).getNodes().size() != ((Way) p2).getNodes().size()
|
---|
130 | ) {
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 | for (int i = 0; i < ((Way) p1).getNodes().size(); i++) {
|
---|
134 | if (!areEqualNodes(((Way) p1).getNode(i), ((Way) p2).getNode(i))) {
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 | }
|
---|