1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import org.junit.Assert;
|
---|
8 | import org.junit.Rule;
|
---|
9 | import org.junit.Test;
|
---|
10 | import org.openstreetmap.josm.TestUtils;
|
---|
11 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
12 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.io.OsmReader;
|
---|
17 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
18 |
|
---|
19 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Unit tests of {@link Geometry} class.
|
---|
23 | */
|
---|
24 | public class GeometryTest {
|
---|
25 | /**
|
---|
26 | * Primitives need preferences and projection.
|
---|
27 | */
|
---|
28 | @Rule
|
---|
29 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
30 | public JOSMTestRules test = new JOSMTestRules().preferences().projection();
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Test of {@link Geometry#getLineLineIntersection} method.
|
---|
34 | */
|
---|
35 | @Test
|
---|
36 | public void testLineLineIntersection() {
|
---|
37 | EastNorth p1 = new EastNorth(-9477809.106349014, 1.5392960539974203E7);
|
---|
38 | EastNorth p2 = new EastNorth(-9477813.789091509, 1.5392954297092048E7);
|
---|
39 | EastNorth p3 = new EastNorth(-9477804.974058038, 1.539295490030348E7);
|
---|
40 | EastNorth p4 = new EastNorth(-9477814.628697459, 1.5392962142181376E7);
|
---|
41 |
|
---|
42 | EastNorth intersectionPoint = Geometry.getLineLineIntersection(p1, p2, p3, p4);
|
---|
43 |
|
---|
44 | EastNorth d1 = p3.subtract(intersectionPoint);
|
---|
45 | EastNorth d2 = p1.subtract(p2);
|
---|
46 | Double crossProduct = d1.east()*d2.north() - d1.north()*d2.east();
|
---|
47 | Double scalarProduct = d1.east()*d2.east() + d1.north()*d2.north();
|
---|
48 | Double len1 = d1.length();
|
---|
49 | Double len2 = d2.length();
|
---|
50 |
|
---|
51 | Double angle1 = Geometry.getCornerAngle(p1, p2, intersectionPoint);
|
---|
52 | Double angle2 = Geometry.getCornerAngle(p3, p4, intersectionPoint);
|
---|
53 | Assert.assertTrue("intersection point not on line, angle: " + angle1,
|
---|
54 | Math.abs(angle1) < 1e-10);
|
---|
55 | Assert.assertTrue("intersection point not on line, angle: " + angle2,
|
---|
56 | Math.abs(angle1) < 1e-10);
|
---|
57 |
|
---|
58 | Assert.assertTrue("cross product != 1 : " + Math.abs(crossProduct/len1/len2),
|
---|
59 | Math.abs(Math.abs(crossProduct/len1/len2) - 1) < 1e-10);
|
---|
60 | Assert.assertTrue("scalar product != 0 : " + scalarProduct/len1/len2,
|
---|
61 | Math.abs(scalarProduct/len1/len2) < 1e-10);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Test of {@link Geometry#closedWayArea(org.openstreetmap.josm.data.osm.Way)} method.
|
---|
66 | *
|
---|
67 | * @throws Exception if an error occurs
|
---|
68 | */
|
---|
69 | @Test
|
---|
70 | public void testClosedWayArea() throws Exception {
|
---|
71 | try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm")) {
|
---|
72 | DataSet ds = OsmReader.parseDataSet(in, null);
|
---|
73 | Way closedWay = (Way) SubclassFilteredCollection.filter(ds.allPrimitives(),
|
---|
74 | SearchCompiler.compile("landuse=forest")).iterator().next();
|
---|
75 | Assert.assertEquals(5760015.7353515625, Geometry.closedWayArea(closedWay), 1e-3);
|
---|
76 | Assert.assertEquals(5760015.7353515625, Geometry.computeArea(closedWay), 1e-3);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Test of {@link Geometry#multipolygonArea(Relation)}} method.
|
---|
82 | *
|
---|
83 | * @throws Exception if an error occurs
|
---|
84 | */
|
---|
85 | @Test
|
---|
86 | public void testMultipolygonArea() throws Exception {
|
---|
87 | try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "multipolygon.osm")) {
|
---|
88 | DataSet ds = OsmReader.parseDataSet(in, null);
|
---|
89 | final Relation r = ds.getRelations().iterator().next();
|
---|
90 | Assert.assertEquals(4401735.20703125, Geometry.multipolygonArea(r), 1e-3);
|
---|
91 | Assert.assertEquals(4401735.20703125, Geometry.computeArea(r), 1e-3);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Test of {@link Geometry#getAreaAndPerimeter(List)} method.
|
---|
97 | *
|
---|
98 | * @throws Exception if an error occurs
|
---|
99 | */
|
---|
100 | @Test
|
---|
101 | public void testAreaAndPerimeter() throws Exception {
|
---|
102 | try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm")) {
|
---|
103 | DataSet ds = OsmReader.parseDataSet(in, null);
|
---|
104 | Way closedWay = (Way) SubclassFilteredCollection.filter(ds.allPrimitives(),
|
---|
105 | SearchCompiler.compile("landuse=forest")).iterator().next();
|
---|
106 | Geometry.AreaAndPerimeter areaAndPerimeter = Geometry.getAreaAndPerimeter(closedWay.getNodes());
|
---|
107 | Assert.assertEquals(12495000., areaAndPerimeter.getArea(), 1e-3);
|
---|
108 | Assert.assertEquals(15093.201209424187, areaAndPerimeter.getPerimeter(), 1e-3);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|