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

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

see #11516 - Compute closed way area using Sinusoidal projection

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.FileInputStream;
5import java.util.List;
6
7import org.junit.Assert;
8import org.junit.BeforeClass;
9import org.junit.Test;
10import org.openstreetmap.josm.JOSMFixture;
11import org.openstreetmap.josm.TestUtils;
12import org.openstreetmap.josm.actions.search.SearchCompiler;
13import org.openstreetmap.josm.data.coor.EastNorth;
14import org.openstreetmap.josm.data.osm.DataSet;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.io.OsmReader;
17
18/**
19 * Unit tests of {@link Geometry} class.
20 */
21public class GeometryTest {
22
23 /**
24 * Setup test.
25 */
26 @BeforeClass
27 public static void setUp() {
28 JOSMFixture.createUnitTestFixture().init();
29 }
30
31 /**
32 * Test of {@link Geometry#getLineLineIntersection} method.
33 */
34 @Test
35 public void testLineLineIntersection() {
36 EastNorth p1 = new EastNorth(-9477809.106349014, 1.5392960539974203E7);
37 EastNorth p2 = new EastNorth(-9477813.789091509, 1.5392954297092048E7);
38 EastNorth p3 = new EastNorth(-9477804.974058038, 1.539295490030348E7);
39 EastNorth p4 = new EastNorth(-9477814.628697459, 1.5392962142181376E7);
40
41 EastNorth intersectionPoint = Geometry.getLineLineIntersection(p1, p2, p3, p4);
42
43 EastNorth d1 = p3.subtract(intersectionPoint);
44 EastNorth d2 = p1.subtract(p2);
45 Double crossProduct = d1.east()*d2.north() - d1.north()*d2.east();
46 Double scalarProduct = d1.east()*d2.east() + d1.north()*d2.north();
47 Double len1 = d1.length();
48 Double len2 = d2.length();
49
50 Double angle1 = Geometry.getCornerAngle(p1, p2, intersectionPoint);
51 Double angle2 = Geometry.getCornerAngle(p3, p4, intersectionPoint);
52 Assert.assertTrue("intersection point not on line, angle: " + angle1,
53 Math.abs(angle1) < 1e-10);
54 Assert.assertTrue("intersection point not on line, angle: " + angle2,
55 Math.abs(angle1) < 1e-10);
56
57 Assert.assertTrue("cross product != 1 : " + Math.abs(crossProduct/len1/len2),
58 Math.abs(Math.abs(crossProduct/len1/len2) - 1) < 1e-10);
59 Assert.assertTrue("scalar product != 0 : " + scalarProduct/len1/len2,
60 Math.abs(scalarProduct/len1/len2) < 1e-10);
61 }
62
63 /**
64 * Test of {@link Geometry#closedWayArea(org.openstreetmap.josm.data.osm.Way)} method.
65 *
66 * @throws Exception if an error occurs
67 */
68 @Test
69 public void testClosedWayArea() throws Exception {
70 try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm")) {
71 DataSet ds = OsmReader.parseDataSet(in, null);
72 Way closedWay = (Way) Utils.filter(ds.allPrimitives(), SearchCompiler.compile("landuse=forest")).iterator().next();
73 Assert.assertEquals(5760015.7353515625, Geometry.closedWayArea(closedWay), 1e-3);
74 }
75 }
76
77 /**
78 * Test of {@link Geometry#getAreaAndPerimeter(List)} method.
79 *
80 * @throws Exception if an error occurs
81 */
82 @Test
83 public void testAreaAndPerimeter() throws Exception {
84 try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm")) {
85 DataSet ds = OsmReader.parseDataSet(in, null);
86 Way closedWay = (Way) Utils.filter(ds.allPrimitives(), SearchCompiler.compile("landuse=forest")).iterator().next();
87 Geometry.AreaAndPerimeter areaAndPerimeter = Geometry.getAreaAndPerimeter(closedWay.getNodes());
88 Assert.assertEquals(12495000., areaAndPerimeter.getArea(), 1e-3);
89 Assert.assertEquals(15093.201209424187, areaAndPerimeter.getPerimeter(), 1e-3);
90 }
91 }
92}
Note: See TracBrowser for help on using the repository browser.