source: josm/trunk/test/unit/org/openstreetmap/josm/tools/GeometryTest.java@ 14138

Last change on this file since 14138 was 13712, checked in by Don-vip, 6 years ago

see #16256 - improve "building with almost square angle" autofix: try to move only the highlighted node

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.Assert.assertEquals;
5
6import java.io.FileInputStream;
7import java.util.Arrays;
8import java.util.List;
9
10import org.junit.Assert;
11import org.junit.Rule;
12import org.junit.Test;
13import org.openstreetmap.josm.TestUtils;
14import org.openstreetmap.josm.data.coor.EastNorth;
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.Relation;
19import org.openstreetmap.josm.data.osm.Way;
20import org.openstreetmap.josm.data.osm.search.SearchCompiler;
21import org.openstreetmap.josm.io.OsmReader;
22import org.openstreetmap.josm.testutils.JOSMTestRules;
23
24import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25
26/**
27 * Unit tests of {@link Geometry} class.
28 */
29public class GeometryTest {
30 /**
31 * Primitives need preferences and projection.
32 */
33 @Rule
34 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
35 public JOSMTestRules test = new JOSMTestRules().preferences().projection();
36
37 /**
38 * Test of {@link Geometry#getLineLineIntersection} method.
39 */
40 @Test
41 public void testLineLineIntersection() {
42 EastNorth p1 = new EastNorth(-9477809.106349014, 1.5392960539974203E7);
43 EastNorth p2 = new EastNorth(-9477813.789091509, 1.5392954297092048E7);
44 EastNorth p3 = new EastNorth(-9477804.974058038, 1.539295490030348E7);
45 EastNorth p4 = new EastNorth(-9477814.628697459, 1.5392962142181376E7);
46
47 EastNorth intersectionPoint = Geometry.getLineLineIntersection(p1, p2, p3, p4);
48
49 EastNorth d1 = p3.subtract(intersectionPoint);
50 EastNorth d2 = p1.subtract(p2);
51 Double crossProduct = d1.east()*d2.north() - d1.north()*d2.east();
52 Double scalarProduct = d1.east()*d2.east() + d1.north()*d2.north();
53 Double len1 = d1.length();
54 Double len2 = d2.length();
55
56 Double angle1 = Geometry.getCornerAngle(p1, p2, intersectionPoint);
57 Double angle2 = Geometry.getCornerAngle(p3, p4, intersectionPoint);
58 Assert.assertTrue("intersection point not on line, angle: " + angle1,
59 Math.abs(angle1) < 1e-10);
60 Assert.assertTrue("intersection point not on line, angle: " + angle2,
61 Math.abs(angle1) < 1e-10);
62
63 Assert.assertTrue("cross product != 1 : " + Math.abs(crossProduct/len1/len2),
64 Math.abs(Math.abs(crossProduct/len1/len2) - 1) < 1e-10);
65 Assert.assertTrue("scalar product != 0 : " + scalarProduct/len1/len2,
66 Math.abs(scalarProduct/len1/len2) < 1e-10);
67 }
68
69 /**
70 * Test of {@link Geometry#closedWayArea(org.openstreetmap.josm.data.osm.Way)} method.
71 *
72 * @throws Exception if an error occurs
73 */
74 @Test
75 public void testClosedWayArea() throws Exception {
76 try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm")) {
77 DataSet ds = OsmReader.parseDataSet(in, null);
78 Way closedWay = (Way) SubclassFilteredCollection.filter(ds.allPrimitives(),
79 SearchCompiler.compile("landuse=forest")).iterator().next();
80 Assert.assertEquals(5760015.7353515625, Geometry.closedWayArea(closedWay), 1e-3);
81 Assert.assertEquals(5760015.7353515625, Geometry.computeArea(closedWay), 1e-3);
82 }
83 }
84
85 /**
86 * Test of {@link Geometry#multipolygonArea(Relation)}} method.
87 *
88 * @throws Exception if an error occurs
89 */
90 @Test
91 public void testMultipolygonArea() throws Exception {
92 try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "multipolygon.osm")) {
93 DataSet ds = OsmReader.parseDataSet(in, null);
94 final Relation r = ds.getRelations().iterator().next();
95 Assert.assertEquals(4401735.20703125, Geometry.multipolygonArea(r), 1e-3);
96 Assert.assertEquals(4401735.20703125, Geometry.computeArea(r), 1e-3);
97 }
98 }
99
100 /**
101 * Test of {@link Geometry#getAreaAndPerimeter(List)} method.
102 *
103 * @throws Exception if an error occurs
104 */
105 @Test
106 public void testAreaAndPerimeter() throws Exception {
107 try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm")) {
108 DataSet ds = OsmReader.parseDataSet(in, null);
109 Way closedWay = (Way) SubclassFilteredCollection.filter(ds.allPrimitives(),
110 SearchCompiler.compile("landuse=forest")).iterator().next();
111 Geometry.AreaAndPerimeter areaAndPerimeter = Geometry.getAreaAndPerimeter(closedWay.getNodes());
112 Assert.assertEquals(12495000., areaAndPerimeter.getArea(), 1e-3);
113 Assert.assertEquals(15093.201209424187, areaAndPerimeter.getPerimeter(), 1e-3);
114 }
115 }
116
117 /**
118 * Test of {@link Geometry#getNormalizedAngleInDegrees(double)} method.
119 */
120 @Test
121 public void testRightAngle() {
122 Node n1 = new Node(1);
123 Node n2 = new Node(2);
124 Node n3 = new Node(3);
125 n1.setCoor(new LatLon(10.22873540462851, 6.169719398316592));
126 n2.setCoor(new LatLon(10.229332494162811, 6.16978130985785));
127 n3.setCoor(new LatLon(10.22924937004949, 6.17060908367496));
128
129 double angle = Geometry.getNormalizedAngleInDegrees(Geometry.getCornerAngle(n1.getEastNorth(),
130 n2.getEastNorth(), n3.getEastNorth()));
131 assertEquals(90, angle, 1e-8);
132 angle = Geometry.getNormalizedAngleInDegrees(Geometry.getCornerAngle(n1.getEastNorth(),
133 n2.getEastNorth(), n1.getEastNorth()));
134 assertEquals(0, angle, 1e-8);
135
136 n1.setCoor(new LatLon(10.2295011, 6.1693106));
137 n2.setCoor(new LatLon(10.2294958, 6.16930635));
138 n3.setCoor(new LatLon(10.2294895, 6.1693039));
139
140 angle = Geometry.getNormalizedAngleInDegrees(Geometry.getCornerAngle(n1.getEastNorth(),
141 n2.getEastNorth(), n3.getEastNorth()));
142 assertEquals(162.66381817961337, angle, 1e-5);
143
144 angle = Geometry.getNormalizedAngleInDegrees(Geometry.getCornerAngle(n3.getEastNorth(),
145 n2.getEastNorth(), n1.getEastNorth()));
146 assertEquals(162.66381817961337, angle, 1e-5);
147 }
148
149 /**
150 * Test of {@link Geometry#getCentroidEN} method.
151 */
152 @Test
153 public void testCentroidEN() {
154 EastNorth en1 = new EastNorth(100, 200);
155 EastNorth en2 = new EastNorth(150, 400);
156 EastNorth en3 = new EastNorth(200, 200);
157 assertEquals(en1, Geometry.getCentroidEN(Arrays.asList(en1)));
158 assertEquals(new EastNorth(125, 300), Geometry.getCentroidEN(Arrays.asList(en1, en2)));
159 assertEquals(new EastNorth(150, 266d + 2d/3d), Geometry.getCentroidEN(Arrays.asList(en1, en2, en3)));
160 }
161}
Note: See TracBrowser for help on using the repository browser.