Changeset 9952 in josm
- Timestamp:
- 2016-03-07T23:29:30+01:00 (9 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java
r9940 r9952 1386 1386 @Override 1387 1387 protected Long getNumber(OsmPrimitive osm) { 1388 if (!(osm instanceof Way && ((Way) osm).isClosed())) 1389 return null; 1390 Way way = (Way) osm; 1391 return (long) Geometry.closedWayArea(way); 1388 final Double area = Geometry.computeArea(osm); 1389 return area == null ? null : area.longValue(); 1392 1390 } 1393 1391 -
trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java
r9854 r9952 652 652 } 653 653 if (extent != null) { 654 if (!usePartialFill(pd.getAreaAndPerimeter( ), extent, extentThreshold)) {654 if (!usePartialFill(pd.getAreaAndPerimeter(null), extent, extentThreshold)) { 655 655 extent = null; 656 656 } else if (!pd.isClosed()) { -
trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/relations/Multipolygon.java
r9077 r9952 26 26 import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent; 27 27 import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon.PolyData.Intersection; 28 import org.openstreetmap.josm.data.projection.Projection; 28 29 import org.openstreetmap.josm.tools.Geometry; 29 30 import org.openstreetmap.josm.tools.Geometry.AreaAndPerimeter; … … 375 376 } 376 377 377 public AreaAndPerimeter getAreaAndPerimeter() { 378 AreaAndPerimeter ap = Geometry.getAreaAndPerimeter(nodes); 378 /** 379 * Calculate area and perimeter length in the given projection. 380 * 381 * @param projection the projection to use for the calculation, {@code null} defaults to {@link Main#getProjection()} 382 * @return area and perimeter 383 */ 384 public AreaAndPerimeter getAreaAndPerimeter(Projection projection) { 385 AreaAndPerimeter ap = Geometry.getAreaAndPerimeter(nodes, projection); 379 386 double area = ap.getArea(); 380 387 double perimeter = ap.getPerimeter(); 381 388 for (PolyData inner : inners) { 382 AreaAndPerimeter apInner = inner.getAreaAndPerimeter( );389 AreaAndPerimeter apInner = inner.getAreaAndPerimeter(projection); 383 390 area -= apInner.getArea(); 384 391 perimeter += apInner.getPerimeter(); -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java
r9929 r9952 531 531 532 532 /** 533 * Returns the area of a closed way in square meters or {@code null}.534 * @param env the environment 535 * @return the area of a closed way in square meters or {@code null}536 * @see Geometry#c losedWayArea(Way)533 * Returns the area of a closed way or multipolygon in square meters or {@code null}. 534 * @param env the environment 535 * @return the area of a closed way or multipolygon in square meters or {@code null} 536 * @see Geometry#computeArea(OsmPrimitive) 537 537 */ 538 538 public static Float areasize(final Environment env) { 539 if (env.osm instanceof Way && ((Way) env.osm).isClosed()) { 540 return (float) Geometry.closedWayArea((Way) env.osm); 541 } else { 542 return null; 543 } 539 final Double area = Geometry.computeArea(env.osm); 540 return area == null ? null : area.floatValue(); 544 541 } 545 542 -
trunk/src/org/openstreetmap/josm/tools/Geometry.java
r9951 r9952 27 27 import org.openstreetmap.josm.data.osm.Node; 28 28 import org.openstreetmap.josm.data.osm.NodePositionComparator; 29 import org.openstreetmap.josm.data.osm.OsmPrimitive; 29 30 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 30 31 import org.openstreetmap.josm.data.osm.Relation; 31 32 import org.openstreetmap.josm.data.osm.RelationMember; 32 33 import org.openstreetmap.josm.data.osm.Way; 34 import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon; 35 import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache; 33 36 import org.openstreetmap.josm.data.projection.Projection; 34 37 import org.openstreetmap.josm.data.projection.Projections; … … 638 641 public static double closedWayArea(Way way) { 639 642 return getAreaAndPerimeter(way.getNodes(), Projections.getProjectionByCode("EPSG:54008")).getArea(); 643 } 644 645 /** 646 * Returns area of a multipolygon in square meters. 647 * 648 * @param multipolygon the multipolygon to measure 649 * @return area of the multipolygon. 650 */ 651 public static double multipolygonArea(Relation multipolygon) { 652 double area = 0.0; 653 final Multipolygon mp = Main.map == null || Main.map.mapView == null 654 ? new Multipolygon(multipolygon) 655 : MultipolygonCache.getInstance().get(Main.map.mapView, multipolygon); 656 for (Multipolygon.PolyData pd : mp.getCombinedPolygons()) { 657 area += pd.getAreaAndPerimeter(Projections.getProjectionByCode("EPSG:54008")).getArea(); 658 } 659 return area; 660 } 661 662 /** 663 * Computes the area of a closed way and multipolygon in square meters, or {@code null} for other primitives 664 * 665 * @param osm the primitive to measure 666 * @return area of the primitive, or {@code null} 667 */ 668 public static Double computeArea(OsmPrimitive osm) { 669 if (osm instanceof Way && ((Way) osm).isClosed()) { 670 return closedWayArea((Way) osm); 671 } else if (osm instanceof Relation && ((Relation) osm).isMultipolygon() && !((Relation) osm).hasIncompleteMembers()) { 672 return multipolygonArea((Relation) osm); 673 } else { 674 return null; 675 } 640 676 } 641 677 -
trunk/test/unit/org/openstreetmap/josm/tools/GeometryTest.java
r9951 r9952 13 13 import org.openstreetmap.josm.data.coor.EastNorth; 14 14 import org.openstreetmap.josm.data.osm.DataSet; 15 import org.openstreetmap.josm.data.osm.Relation; 15 16 import org.openstreetmap.josm.data.osm.Way; 16 17 import org.openstreetmap.josm.io.OsmReader; … … 72 73 Way closedWay = (Way) Utils.filter(ds.allPrimitives(), SearchCompiler.compile("landuse=forest")).iterator().next(); 73 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); 74 91 } 75 92 }
Note:
See TracChangeset
for help on using the changeset viewer.