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

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

see #11516 - Compute multipolygon area, include in MapCSS, JOSM search

  • Property svn:eol-style set to native
File size: 4.4 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.Relation;
16import org.openstreetmap.josm.data.osm.Way;
17import org.openstreetmap.josm.io.OsmReader;
18
19/**
20 * Unit tests of {@link Geometry} class.
21 */
22public class GeometryTest {
23
24 /**
25 * Setup test.
26 */
27 @BeforeClass
28 public static void setUp() {
29 JOSMFixture.createUnitTestFixture().init();
30 }
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) Utils.filter(ds.allPrimitives(), SearchCompiler.compile("landuse=forest")).iterator().next();
74 Assert.assertEquals(5760015.7353515625, Geometry.closedWayArea(closedWay), 1e-3);
75 Assert.assertEquals(5760015.7353515625, Geometry.computeArea(closedWay), 1e-3);
76 }
77 }
78
79 /**
80 * Test of {@link Geometry#multipolygonArea(Relation)}} method.
81 *
82 * @throws Exception if an error occurs
83 */
84 @Test
85 public void testMultipolygonArea() throws Exception {
86 try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "multipolygon.osm")) {
87 DataSet ds = OsmReader.parseDataSet(in, null);
88 final Relation r = ds.getRelations().iterator().next();
89 Assert.assertEquals(4401735.20703125, Geometry.multipolygonArea(r), 1e-3);
90 Assert.assertEquals(4401735.20703125, Geometry.computeArea(r), 1e-3);
91 }
92 }
93
94 /**
95 * Test of {@link Geometry#getAreaAndPerimeter(List)} method.
96 *
97 * @throws Exception if an error occurs
98 */
99 @Test
100 public void testAreaAndPerimeter() throws Exception {
101 try (FileInputStream in = new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm")) {
102 DataSet ds = OsmReader.parseDataSet(in, null);
103 Way closedWay = (Way) Utils.filter(ds.allPrimitives(), SearchCompiler.compile("landuse=forest")).iterator().next();
104 Geometry.AreaAndPerimeter areaAndPerimeter = Geometry.getAreaAndPerimeter(closedWay.getNodes());
105 Assert.assertEquals(12495000., areaAndPerimeter.getArea(), 1e-3);
106 Assert.assertEquals(15093.201209424187, areaAndPerimeter.getPerimeter(), 1e-3);
107 }
108 }
109}
Note: See TracBrowser for help on using the repository browser.