source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/WayTest.java@ 18553

Last change on this file since 18553 was 18553, checked in by taylor.smock, 21 months ago

Fix #20716: Search for missing power line support features (patch by gaben, modified)

Way:

  • Avoid Arrays.stream in hasIncompleteNodes. This decreases CPU cost by 90%+ and makes it free from a memory allocation standpoint.
  • Add method to get segment lengths (meters)

CrossingWays:

  • Avoid Node#getEastNorth calls
  • Add a getSegments call that takes ILatLon

PowerLines:

  • Check for inconsistent support node reference numbering
  • Check for ways with unusually long segments without node supports
  • Check for ways where line types might be misused

ValUtil:

  • Avoid unnecessary calls to Node#getEastNorth
  • Add getSegmentCells for ILatLon

InspectPrimitiveDataText:

  • Add average segment length statistic

Geometry:

  • Add getSegmentSegmentIntersection for ILatLon to avoid new EastNorth objects

Utils:

  • Add getStandardDeviation methods
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertTrue;
7import static org.junit.jupiter.api.Assertions.assertThrows;
8
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.HashSet;
12
13import org.junit.jupiter.api.BeforeAll;
14import org.junit.jupiter.api.Test;
15import org.openstreetmap.josm.JOSMFixture;
16import org.openstreetmap.josm.data.coor.LatLon;
17
18/**
19 * Unit tests of the {@code Way} class.
20 * @since 11270
21 */
22class WayTest {
23
24 /**
25 * Setup test.
26 */
27 @BeforeAll
28 public static void setUpBeforeClass() {
29 JOSMFixture.createUnitTestFixture().init();
30 }
31
32 /**
33 * Test BBox calculation with Way
34 */
35 @Test
36 void testBBox() {
37 DataSet ds = new DataSet();
38 Node n1 = new Node(1);
39 Node n2 = new Node(2);
40 Node n3 = new Node(3);
41 Node n4 = new Node(4);
42 n1.setIncomplete(true);
43 n2.setCoor(new LatLon(10, 10));
44 n3.setCoor(new LatLon(20, 20));
45 n4.setCoor(new LatLon(90, 180));
46 ds.addPrimitive(n1);
47 ds.addPrimitive(n2);
48 ds.addPrimitive(n3);
49 ds.addPrimitive(n4);
50 Way way = new Way(1);
51 assertFalse(way.getBBox().isValid());
52 way.setNodes(Collections.singletonList(n1));
53 assertFalse(way.getBBox().isValid());
54 way.setNodes(Collections.singletonList(n2));
55 assertTrue(way.getBBox().isValid());
56 way.setNodes(Arrays.asList(n1, n2));
57 assertTrue(way.getBBox().isValid());
58 assertEquals(way.getBBox(), new BBox(10, 10));
59 }
60
61 /**
62 * Test remove node
63 */
64 @Test
65 void testRemoveNode() {
66 DataSet ds = new DataSet();
67 Node n1 = new Node(1);
68 Node n2 = new Node(2);
69 Node n3 = new Node(3);
70 Node n4 = new Node(4);
71 n1.setCoor(new LatLon(10, 10));
72 n2.setCoor(new LatLon(11, 11));
73 n3.setCoor(new LatLon(12, 12));
74 n4.setCoor(new LatLon(13, 13));
75 ds.addPrimitive(n1);
76 ds.addPrimitive(n2);
77 ds.addPrimitive(n3);
78 ds.addPrimitive(n4);
79 Way way = new Way(1);
80 ds.addPrimitive(way);
81 // duplicated way node
82 way.setNodes(Arrays.asList(n1, n2, n2, n3, n4, n1));
83 way.setIncomplete(false);
84 way.removeNode(n4);
85 assertEquals(Arrays.asList(n1, n2, n3, n1), way.getNodes());
86 way.removeNode(n3);
87 assertEquals(Arrays.asList(n1, n2), way.getNodes());
88 way.setNodes(Arrays.asList(n1, n2, n3, n4, n1));
89 way.removeNode(n1);
90 assertEquals(Arrays.asList(n2, n3, n4, n2), way.getNodes());
91 }
92
93 /**
94 * Test remove node
95 */
96 @Test
97 void testRemoveNodes() {
98 DataSet ds = new DataSet();
99 Node n1 = new Node(1);
100 Node n2 = new Node(2);
101 Node n3 = new Node(3);
102 Node n4 = new Node(4);
103 n1.setCoor(new LatLon(10, 10));
104 n2.setCoor(new LatLon(11, 11));
105 n3.setCoor(new LatLon(12, 12));
106 n4.setCoor(new LatLon(13, 13));
107 ds.addPrimitive(n1);
108 ds.addPrimitive(n2);
109 ds.addPrimitive(n3);
110 ds.addPrimitive(n4);
111 Way way = new Way(1);
112 ds.addPrimitive(way);
113 // duplicated way node
114 way.setNodes(Arrays.asList(n1, n2, n2, n3, n4, n1));
115 way.setIncomplete(false);
116 way.removeNodes(new HashSet<>(Arrays.asList(n3, n4)));
117 assertEquals(Arrays.asList(n1, n2, n1), way.getNodes());
118 way.setNodes(Arrays.asList(n1, n2, n3, n4, n1));
119 way.removeNodes(new HashSet<>(Collections.singletonList(n1)));
120 assertEquals(Arrays.asList(n2, n3, n4, n2), way.getNodes());
121 }
122
123 /**
124 * Test that {@link Way#cloneFrom} throws IAE for invalid arguments
125 */
126 @Test
127 void testCloneFromIAE() {
128 assertThrows(IllegalArgumentException.class, () -> new Way().cloneFrom(new Node()));
129 }
130
131 /**
132 * Test that {@link Way#load} throws IAE for invalid arguments
133 */
134 @Test
135 void testLoadIAE() {
136 assertThrows(IllegalArgumentException.class, () -> new Way().load(new NodeData()));
137 }
138
139 @Test
140 void getLongestSegmentLength() {
141 DataSet ds = new DataSet();
142 Node n1 = new Node(1);
143 Node n2 = new Node(2);
144 Node n3 = new Node(3);
145 Node n4 = new Node(4);
146 n1.setCoor(new LatLon(0.01, 0.01));
147 n2.setCoor(new LatLon(0.02, 0.02));
148 n3.setCoor(new LatLon(0.03, 0.03));
149 n4.setCoor(new LatLon(0.05, 0.05));
150 ds.addPrimitive(n1);
151 ds.addPrimitive(n2);
152 ds.addPrimitive(n3);
153 ds.addPrimitive(n4);
154 Way way = new Way(1);
155 ds.addPrimitive(way);
156
157 assertEquals(0.0, way.getLongestSegmentLength());
158 way.setNodes(Arrays.asList(n1, n2, n2, n3, n4));
159
160 assertEquals(3148.5902810874577, way.getLongestSegmentLength());
161 }
162}
Note: See TracBrowser for help on using the repository browser.