1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Iterator;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.fest.reflect.core.Reflection;
|
---|
13 | import org.fest.reflect.reference.TypeRef;
|
---|
14 | import org.junit.Assert;
|
---|
15 | import org.junit.BeforeClass;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.openstreetmap.josm.JOSMFixture;
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
20 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
21 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
22 | import org.openstreetmap.josm.io.OsmReader;
|
---|
23 |
|
---|
24 | public class QuadBucketsTest {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Setup test.
|
---|
28 | */
|
---|
29 | @BeforeClass
|
---|
30 | public static void init() {
|
---|
31 | JOSMFixture.createUnitTestFixture().init();
|
---|
32 | }
|
---|
33 |
|
---|
34 | private void removeAllTest(DataSet ds) {
|
---|
35 | List<Node> allNodes = new ArrayList<>(ds.getNodes());
|
---|
36 | List<Way> allWays = new ArrayList<>(ds.getWays());
|
---|
37 | List<Relation> allRelations = new ArrayList<>(ds.getRelations());
|
---|
38 |
|
---|
39 | QuadBuckets<Node> nodes = Reflection.field("nodes").ofType(new TypeRef<QuadBuckets<Node>>() {}).in(ds).get();
|
---|
40 | QuadBuckets<Way> ways = Reflection.field("ways").ofType(new TypeRef<QuadBuckets<Way>>() {}).in(ds).get();
|
---|
41 | Collection<Relation> relations = Reflection.field("relations").ofType(new TypeRef<Collection<Relation>>() {}).in(ds).get();
|
---|
42 |
|
---|
43 | int expectedCount = allNodes.size();
|
---|
44 | for (OsmPrimitive o: allNodes) {
|
---|
45 | ds.removePrimitive(o);
|
---|
46 | checkIterator(nodes, --expectedCount);
|
---|
47 | }
|
---|
48 | expectedCount = allWays.size();
|
---|
49 | for (OsmPrimitive o: allWays) {
|
---|
50 | ds.removePrimitive(o);
|
---|
51 | checkIterator(ways, --expectedCount);
|
---|
52 | }
|
---|
53 | for (OsmPrimitive o: allRelations) {
|
---|
54 | ds.removePrimitive(o);
|
---|
55 | }
|
---|
56 | Assert.assertTrue(nodes.isEmpty());
|
---|
57 | Assert.assertTrue(ways.isEmpty());
|
---|
58 | Assert.assertTrue(relations.isEmpty());
|
---|
59 | }
|
---|
60 |
|
---|
61 | private void checkIterator(Collection<? extends OsmPrimitive> col, int expectedCount) {
|
---|
62 | int count = 0;
|
---|
63 | Iterator<? extends OsmPrimitive> it = col.iterator();
|
---|
64 | while (it.hasNext()) {
|
---|
65 | count++;
|
---|
66 | it.next();
|
---|
67 | }
|
---|
68 | Assert.assertEquals(expectedCount, count);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Test
|
---|
72 | public void testRemove() throws Exception {
|
---|
73 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
|
---|
74 | try (InputStream fis = new FileInputStream("data_nodist/restriction.osm")) {
|
---|
75 | DataSet ds = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
76 | removeAllTest(ds);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Test
|
---|
81 | public void testMove() throws Exception {
|
---|
82 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
|
---|
83 | try (InputStream fis = new FileInputStream("data_nodist/restriction.osm")) {
|
---|
84 | DataSet ds = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
85 |
|
---|
86 | for (Node n: ds.getNodes()) {
|
---|
87 | n.setCoor(new LatLon(10, 10));
|
---|
88 | }
|
---|
89 |
|
---|
90 | removeAllTest(ds);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Test handling of objects with invalid bbox
|
---|
96 | * @throws Exception
|
---|
97 | */
|
---|
98 | @Test
|
---|
99 | public void testSpecialBBox() throws Exception {
|
---|
100 | QuadBuckets<Node> qbNodes = new QuadBuckets<>();
|
---|
101 | QuadBuckets<Way> qbWays = new QuadBuckets<>();
|
---|
102 | Way w1 = new Way(1);
|
---|
103 | Way w2 = new Way(2);
|
---|
104 | Way w3 = new Way(3);
|
---|
105 | Node n1 = new Node(1);
|
---|
106 | Node n2 = new Node(2); n2.setCoor(new LatLon(10, 20));
|
---|
107 | Node n3 = new Node(3); n2.setCoor(new LatLon(20, 30));
|
---|
108 | w2.setNodes(Arrays.asList(n1));
|
---|
109 | w3.setNodes(Arrays.asList(n1, n2, n3));
|
---|
110 |
|
---|
111 | qbNodes.add(n1);
|
---|
112 | qbNodes.add(n2);
|
---|
113 | Assert.assertEquals(2, qbNodes.size());
|
---|
114 | Assert.assertTrue(qbNodes.contains(n1));
|
---|
115 | Assert.assertTrue(qbNodes.contains(n2));
|
---|
116 | Assert.assertFalse(qbNodes.contains(n3));
|
---|
117 | qbNodes.remove(n1);
|
---|
118 | Assert.assertEquals(1, qbNodes.size());
|
---|
119 | Assert.assertFalse(qbNodes.contains(n1));
|
---|
120 | Assert.assertTrue(qbNodes.contains(n2));
|
---|
121 | qbNodes.remove(n2);
|
---|
122 | Assert.assertEquals(0, qbNodes.size());
|
---|
123 | Assert.assertFalse(qbNodes.contains(n1));
|
---|
124 | Assert.assertFalse(qbNodes.contains(n2));
|
---|
125 |
|
---|
126 | qbNodes.addAll(Arrays.asList(n1, n2, n3));
|
---|
127 | qbNodes.removeAll(Arrays.asList(n1, n3));
|
---|
128 | Assert.assertEquals(1, qbNodes.size());
|
---|
129 | Assert.assertTrue(qbNodes.contains(n2));
|
---|
130 |
|
---|
131 | qbWays.add(w1);
|
---|
132 | qbWays.add(w2);
|
---|
133 | qbWays.add(w3);
|
---|
134 | Assert.assertEquals(3, qbWays.size());
|
---|
135 | Assert.assertTrue(qbWays.contains(w1));
|
---|
136 | Assert.assertTrue(qbWays.contains(w2));
|
---|
137 | Assert.assertTrue(qbWays.contains(w3));
|
---|
138 | qbWays.remove(w1);
|
---|
139 | Assert.assertEquals(2, qbWays.size());
|
---|
140 | Assert.assertFalse(qbWays.contains(w1));
|
---|
141 | Assert.assertTrue(qbWays.contains(w2));
|
---|
142 | Assert.assertTrue(qbWays.contains(w3));
|
---|
143 | qbWays.remove(w2);
|
---|
144 | Assert.assertEquals(1, qbWays.size());
|
---|
145 | Assert.assertFalse(qbWays.contains(w1));
|
---|
146 | Assert.assertFalse(qbWays.contains(w2));
|
---|
147 | Assert.assertTrue(qbWays.contains(w3));
|
---|
148 | qbWays.remove(w3);
|
---|
149 | Assert.assertEquals(0, qbWays.size());
|
---|
150 | Assert.assertFalse(qbWays.contains(w1));
|
---|
151 | Assert.assertFalse(qbWays.contains(w2));
|
---|
152 | Assert.assertFalse(qbWays.contains(w3));
|
---|
153 |
|
---|
154 | qbWays.clear();
|
---|
155 | Assert.assertEquals(0, qbWays.size());
|
---|
156 | List<Way> allWays = new ArrayList<>(Arrays.asList(w1, w2, w3));
|
---|
157 | qbWays.addAll(allWays);
|
---|
158 | Assert.assertEquals(3, qbWays.size());
|
---|
159 | int count = 0;
|
---|
160 | for (Way w : qbWays) {
|
---|
161 | Assert.assertTrue(allWays.contains(w));
|
---|
162 | count++;
|
---|
163 | }
|
---|
164 | Assert.assertEquals(3, count);
|
---|
165 | // test remove with iterator
|
---|
166 | Iterator<Way> iter = qbWays.iterator();
|
---|
167 | while (iter.hasNext()) {
|
---|
168 | iter.next();
|
---|
169 | iter.remove();
|
---|
170 | count--;
|
---|
171 | Assert.assertEquals(count, qbWays.size());
|
---|
172 | }
|
---|
173 | Assert.assertEquals(0, qbWays.size());
|
---|
174 | }
|
---|
175 | }
|
---|