source: josm/trunk/test/unit/org/openstreetmap/josm/actions/OrthogonalizeActionTest.java@ 9925

Last change on this file since 9925 was 9925, checked in by simon04, 8 years ago

see #6401 - Orthogonalize objects based on selected nodes

This is done by assembling a virtual way from the selected nodes. This is sensitive to the selection order.

File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static junit.framework.Assert.assertEquals;
5
6import java.io.FileInputStream;
7import java.util.ArrayList;
8import java.util.List;
9
10import org.junit.BeforeClass;
11import org.junit.Test;
12import org.openstreetmap.josm.JOSMFixture;
13import org.openstreetmap.josm.TestUtils;
14import org.openstreetmap.josm.actions.search.SearchCompiler;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.Way;
19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
20import org.openstreetmap.josm.io.OsmReader;
21import org.openstreetmap.josm.tools.Geometry;
22import org.openstreetmap.josm.tools.Utils;
23
24/**
25 * Unit tests for class {@link OsmDataLayer}.
26 */
27public class OrthogonalizeActionTest {
28
29 /**
30 * Setup test.
31 */
32 @BeforeClass
33 public static void setUp() {
34 JOSMFixture.createUnitTestFixture().init(false);
35 }
36
37 @Test(expected = OrthogonalizeAction.InvalidUserInputException.class)
38 public void testNoSelection() throws Exception {
39 performTest("nothing selected");
40 }
41
42 @Test
43 public void testClosedWay() throws Exception {
44 final DataSet ds = performTest("name=ClosedWay");
45 final Way way = ds.getSelectedWays().iterator().next();
46 assertEquals(new LatLon(8.538808176881814, 55.72978898396922), way.getNode(0).getCoor());
47 assertEquals(new LatLon(8.539618224318104, 55.73039799489563), way.getNode(1).getCoor());
48 assertEquals(new LatLon(8.538993302766201, 55.73124794515577), way.getNode(2).getCoor());
49 assertEquals(new LatLon(8.538183254003354, 55.730638934229376), way.getNode(3).getCoor());
50 verifyRectangleClockwise(way);
51 }
52
53 @Test
54 public void testTwoWaysFormingClosedWay() throws Exception {
55 performTest("name=TwoWaysFormingClosedWay");
56 }
57
58 @Test
59 public void testTwoRingsAtOnce() throws Exception {
60 performTest("name=ClosedWay OR name=TwoWaysFormingClosedWay");
61 }
62
63 @Test
64 public void testClosedWayWithReferenceNodes() throws Exception {
65 final DataSet ds = performTest("name=ClosedWayWithReferenceNodes");
66 final Way way = ds.getSelectedWays().iterator().next();
67 assertEquals(new LatLon(8.534711427, 55.73000670312), way.getNode(0).getCoor());
68 assertEquals(new LatLon(8.53547720918594, 55.73067141759374), way.getNode(1).getCoor());
69 assertEquals(new LatLon(8.534835495633061, 55.73142735279376), way.getNode(2).getCoor());
70 assertEquals(new LatLon(8.53406971216, 55.73076263832), way.getNode(3).getCoor());
71 verifyRectangleClockwise(way);
72 }
73
74 @Test
75 public void testFourNodes() throws Exception {
76 final DataSet ds = performTest(
77 "name=NodeToRectify-01", "name=NodeToRectify-02", "name=NodeToRectify-03", "name=NodeToRectify-04");
78 final List<Node> nodes = new ArrayList<>(ds.getSelectedNodes());
79 assertEquals(new LatLon(8.532735415272217, 55.72986948949525), nodes.get(0).getCoor());
80 assertEquals(new LatLon(8.533520827858515, 55.73043325105434), nodes.get(1).getCoor());
81 assertEquals(new LatLon(8.532914283300173, 55.73129729115582), nodes.get(2).getCoor());
82 assertEquals(new LatLon(8.532055019939826, 55.73068052126457), nodes.get(3).getCoor());
83 }
84
85 DataSet performTest(String... search) throws Exception {
86 try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "orthogonalize.osm")) {
87 final DataSet ds = OsmReader.parseDataSet(in, null);
88 for (String s : search) {
89 ds.addSelected(Utils.filter(ds.allPrimitives(), SearchCompiler.compile(s)));
90 }
91 OrthogonalizeAction.orthogonalize(ds.getSelected()).executeCommand();
92 return ds;
93 }
94 }
95
96 void verifyRectangleClockwise(final Way way) {
97 for (int i = 1; i < way.getNodesCount() - 1; i++) {
98 assertEquals(-Math.PI / 2, Geometry.getCornerAngle(
99 way.getNode(i - 1).getEastNorth(), way.getNode(i).getEastNorth(), way.getNode(i + 1).getEastNorth()), 1e-6);
100 }
101 }
102}
Note: See TracBrowser for help on using the repository browser.