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

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

fix #16189 - Add "almost square check" for buildings (patch by marxin, modified)

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