Ignore:
Timestamp:
2016-03-07T23:29:30+01:00 (8 years ago)
Author:
simon04
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r9951 r9952  
    2727import org.openstreetmap.josm.data.osm.Node;
    2828import org.openstreetmap.josm.data.osm.NodePositionComparator;
     29import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2930import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    3031import org.openstreetmap.josm.data.osm.Relation;
    3132import org.openstreetmap.josm.data.osm.RelationMember;
    3233import org.openstreetmap.josm.data.osm.Way;
     34import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
     35import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
    3336import org.openstreetmap.josm.data.projection.Projection;
    3437import org.openstreetmap.josm.data.projection.Projections;
     
    638641    public static double closedWayArea(Way way) {
    639642        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        }
    640676    }
    641677
Note: See TracChangeset for help on using the changeset viewer.