Changeset 23478 in osm for applications


Ignore:
Timestamp:
2010-10-05T20:00:42+02:00 (14 years ago)
Author:
mzdila
Message:
  • use built-in methods for distance and heading
  • fixed last node removal of not closed way if it matched removal criteria
File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/simplifyarea/src/sk/zdila/josm/plugin/simplify/SimplifyAreaAction.java

    r23193 r23478  
    164164     *            the way to simplify
    165165     */
    166     private SequenceCommand simplifyWay(final Way w) {
     166    @SuppressWarnings("null")
     167        private SequenceCommand simplifyWay(final Way w) {
    167168        final double angleThreshold = Double.parseDouble(Main.pref.get("simplify-area.angle", "10.0"));
    168169        final double distanceTreshold = Double.parseDouble(Main.pref.get("simplify-area.distance", "0.2"));
     
    202203                final LatLon coord2 = n2.getCoor();
    203204
    204                 if (isRequiredNode(w, n1) || isRequiredNode(w, n2) || computeDistance(coord1, coord2) > distanceTreshold) {
     205                if (isRequiredNode(w, n1) || isRequiredNode(w, n2) || coord1.greatCircleDistance(coord2) > distanceTreshold) {
    205206                    if (!closing) {
    206207                        newNodes.add(n1);
    207208                    }
    208209                } else {
    209                     moveCommandList.add(new MoveCommand(n2, new LatLon((coord1.lat() + coord2.lat()) / 2.0, (coord1.lon() + coord2.lon()) / 2.0)));
     210                    moveCommandList.add(new MoveCommand(n2, coord1.getCenter(coord2)));
    210211                    if (closing) {
    211212                        newNodes.remove(0);
     
    228229
    229230            if (coord1 != null) {
     231//              System.out.print("AREA: " + computeArea(coord1, coord2, coord3) + "; ANGLE: " + computeConvectAngle(coord1, coord2, coord3));
    230232                if (isRequiredNode(w, prevNode) ||
    231                         Math.abs(computeBearing(coord2, coord3) - computeBearing(coord1, coord2)) > angleThreshold ||
     233                                !closed && i == len - 1 || // don't remove last node of the not closed way
     234                                computeConvectAngle(coord1, coord2, coord3) > angleThreshold ||
    232235                        computeArea(coord1, coord2, coord3) > areaTreshold) {
    233236                    newNodes2.add(prevNode);
     237//                      System.out.println(" ... KEEP");
    234238                } else {
     239//                      System.out.println(" ... REMOVE");
    235240                    coord2 = coord1; // at the end of the iteration preserve coord1
    236241                }
     
    267272
    268273
    269     private double computeBearing(final LatLon coord1, final LatLon coord2) {
    270         final double lon1 = Math.toRadians(coord1.getX());
    271         final double lat1 = Math.toRadians(coord1.getY());
    272 
    273         final double lon2 = Math.toRadians(coord2.getX());
    274         final double lat2 = Math.toRadians(coord2.getY());
    275 
    276         final double dLon = lon2 - lon1;
    277         final double y = Math.sin(dLon) * Math.cos(lat2);
    278         final double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
    279         return Math.toDegrees(Math.atan2(y, x));
    280     }
    281 
    282 
    283     private double computeDistance(final LatLon coord1, final LatLon coord2) {
    284         final double lon1 = Math.toRadians(coord1.getX());
    285         final double lon2 = Math.toRadians(coord2.getX());
    286         final double lat1 = Math.toRadians(coord1.getY());
    287         final double lat2 = Math.toRadians(coord2.getY());
    288 
    289         final double R = 6378137d; // m
    290         final double dLon = lon2 - lon1;
    291         final double dLat = lat2 - lat1;
    292         final double a = Math.sin(dLat / 2d) * Math.sin(dLat / 2d) + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon / 2d) * Math.sin(dLon / 2d);
    293         final double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    294         return R * c;
    295     }
     274    public double computeConvectAngle(final LatLon coord1, final LatLon coord2, final LatLon coord3) {
     275        final double angle =  Math.abs(coord2.heading(coord3) - coord1.heading(coord2));
     276        return Math.toDegrees(angle < Math.PI ? angle : 2 * Math.PI - angle);
     277    }
     278
    296279
    297280
    298281    private double computeArea(final LatLon coord1, final LatLon coord2, final LatLon coord3) {
    299         final double a = computeDistance(coord1, coord2);
    300         final double b = computeDistance(coord2, coord3);
    301         final double c = computeDistance(coord3, coord1);
     282        final double a = coord1.greatCircleDistance(coord2);
     283        final double b = coord2.greatCircleDistance(coord3);
     284        final double c = coord3.greatCircleDistance(coord1);
    302285
    303286        final double p = (a + b + c) / 2.0;
Note: See TracChangeset for help on using the changeset viewer.