Changeset 35165 in osm for applications/editors


Ignore:
Timestamp:
2019-10-01T00:12:18+02:00 (5 years ago)
Author:
donvip
Message:

proper handling of system of measurement + code cleanup

Location:
applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/ElevationHelper.java

    r34746 r35165  
    66import java.util.GregorianCalendar;
    77import java.util.List;
    8 import java.util.Locale;
    98
    109import org.openstreetmap.josm.data.Bounds;
     10import org.openstreetmap.josm.data.SystemOfMeasurement;
    1111import org.openstreetmap.josm.data.coor.LatLon;
    1212import org.openstreetmap.josm.data.gpx.WayPoint;
    1313import org.openstreetmap.josm.plugins.elevation.gpx.GeoidCorrectionKind;
     14import org.openstreetmap.josm.tools.Logging;
    1415
    1516/**
     
    2425    }
    2526
    26     public static double METER_TO_FEET = 3.280948;
    27 
    28     /* Countries which use the imperial system instead of the metric system. */
    29     private static String[] IMPERIAL_SYSTEM_COUNTRIES = {
    30             "en_US",     /* USA */
    31             "en_CA",    /* Canada */
    32             "en_AU",    /* Australia */
    33             "en_NZ",    /* New Zealand */
    34             //        "de_DE",    /* for testing only */
    35             "en_ZA"    /* South Africa */
    36     };
    37 
    3827    /** The 'no elevation' data magic. */
    39     public static double NO_ELEVATION = Double.NaN;
     28    public static final double NO_ELEVATION = Double.NaN;
    4029
    4130    /**
     
    4433    public static final String HEIGHT_ATTRIBUTE = "ele";
    4534
    46     private static UnitMode unitMode = UnitMode.NotSelected;
    47 
    4835    private static GeoidCorrectionKind geoidKind = GeoidCorrectionKind.None;
    4936
    5037    /** The HGT reader instance. */
    51     private static HgtReader hgt = new HgtReader();
     38    private static final HgtReader hgt = new HgtReader();
    5239
    5340    /**
     
    6047    public static void setGeoidKind(GeoidCorrectionKind geoidKind) {
    6148        ElevationHelper.geoidKind = geoidKind;
    62     }
    63 
    64     /**
    65      * Gets the current unit mode (metric or imperial).
    66      */
    67     public static UnitMode getUnitMode() {
    68         //TODO: Use this until /JOSM/src/org/openstreetmap/josm/gui/NavigatableComponent.java
    69         // has a an appropriate method
    70 
    71         // unit mode already determined?
    72         if (unitMode != UnitMode.NotSelected) {
    73             return unitMode;
    74         }
    75 
    76         // Set default
    77         unitMode = UnitMode.Metric;
    78 
    79         // Check if user could prefer imperial system
    80         Locale l = Locale.getDefault();
    81         for (int i = 0; i < IMPERIAL_SYSTEM_COUNTRIES.length; i++) {
    82             String ctry = l.toString();
    83             if (IMPERIAL_SYSTEM_COUNTRIES[i].equals(ctry)) {
    84                 unitMode = UnitMode.Imperial;
    85             }
    86         }
    87 
    88         return unitMode;
    89     }
    90 
    91     /**
    92      * Gets the unit string for elevation ("m" or "ft").
    93      */
    94     public static String getUnit() {
    95         switch (getUnitMode()) {
    96         case Metric:
    97             return "m";
    98         case Imperial:
    99             return "ft";
    100         default:
    101             throw new RuntimeException("Invalid or unsupported unit mode: " + unitMode);
    102         }
    10349    }
    10450
     
    12874        double eleInt = getSrtmElevation(wpt.getCoor());
    12975        if (isValidElevation(eleInt)) {
    130             return convert(eleInt);
     76            return eleInt;
    13177        }
    13278
     
    14086        String height = wpt.getString(ElevationHelper.HEIGHT_ATTRIBUTE);
    14187        try {
    142             double z = Double.parseDouble(height);
    143 
    144             return convert(z);
     88            return Double.parseDouble(height);
    14589        } catch (NumberFormatException e) {
    146             System.err.println(String.format(
    147                     "Cannot parse double from '%s': %s", height, e
    148                     .getMessage()));
     90            Logging.error(String.format("Cannot parse double from '%s': %s", height, e.getMessage()));
    14991            return NO_ELEVATION;
    15092        }
     
    15294
    15395    private static double getElevation(LatLon ll) {
    154         double ele = getSrtmElevation(ll);
    155         //System.out.println("Get elevation " + ll + " => " + ele);
    156         return convert(ele);
    157     }
    158 
    159     /**
    160      * Converts the value to feet, if required.
    161      *
    162      * @param ele the elevation to convert
    163      * @return the double
    164      */
    165     private static double convert(double ele) {
    166         if (isValidElevation(ele)) {
    167             if (getUnitMode() == UnitMode.Imperial) {
    168                 // translate to feet
    169                 return meter2Feet(ele);
    170             } else {
    171                 // keep 'as is'
    172                 return ele;
    173             }
    174         }
    175         return NO_ELEVATION;
     96        return getSrtmElevation(ll);
    17697    }
    17798
     
    189110
    190111        // get distance in meters and divide it by 100 in advance
    191         double distInMeter = convert(w1.greatCircleDistance(w2) / 100.0);
    192 
    193         // get elevation (difference) - is converted automatically to feet
     112        double distInMeter = w1.greatCircleDistance(w2) / 100.0;
     113
     114        // get elevation (difference)
    194115        int ele1 = (int) ElevationHelper.getElevation(w1);
    195116        int ele2 = (int) ElevationHelper.getElevation(w2);
     
    201122
    202123    /**
    203      * Converts meter into feet
    204      *
    205      * @param meter the meter
    206      * @return the double
    207      */
    208     public static double meter2Feet(double meter) {
    209         return meter * METER_TO_FEET;
    210     }
    211 
    212     /**
    213124     * Gets the elevation string for a given elevation, e. g "300m" or "800ft".
    214125     */
    215126    public static String getElevationText(int elevation) {
    216         return String.format("%d %s", elevation, getUnit());
     127        return SystemOfMeasurement.getSystemOfMeasurement().getDistText(elevation);
    217128    }
    218129
     
    221132     */
    222133    public static String getElevationText(double elevation) {
    223         return String.format("%d %s", (int) Math.round(elevation), getUnit());
     134        return SystemOfMeasurement.getSystemOfMeasurement().getDistText((int) Math.round(elevation));
    224135    }
    225136
     
    233144        if (wpt == null) return "-";
    234145
    235         int elevation = (int) Math.round(ElevationHelper.getElevation(wpt));
    236         return String.format("%d %s", elevation, getUnit());
     146        return getElevationText(ElevationHelper.getElevation(wpt));
    237147    }
    238148
     
    262172            double eleHgt = hgt.getElevationFromHgt(ll);
    263173
    264             //System.out.println("Get elevation from HGT " + ll + " => " + eleHgt);
    265174            if (isValidElevation(eleHgt)) {
    266175                return eleHgt;
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/HgtReader.java

    r34507 r35165  
    1414import org.openstreetmap.josm.data.coor.LatLon;
    1515import org.openstreetmap.josm.tools.CheckParameterUtil;
     16import org.openstreetmap.josm.tools.Logging;
    1617
    1718/**
     
    6061            return readElevation(coor);
    6162        } catch (FileNotFoundException e) {
    62             System.err.println("Get elevation from HGT " + coor + " failed: => " + e.getMessage());
     63            Logging.error("Get elevation from HGT " + coor + " failed: => " + e.getMessage());
    6364            // no problem... file not there
    6465            return ElevationHelper.NO_ELEVATION;
    6566        } catch (Exception ioe) {
    6667            // oops...
    67             ioe.printStackTrace(System.err);
     68            Logging.error(ioe);
    6869            // fallback
    6970            return ElevationHelper.NO_ELEVATION;
     
    8687
    8788            bb.flip();
    88             //sb = bb.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
    8989            sb = bb.order(ByteOrder.BIG_ENDIAN).asShortBuffer();
    9090        } finally {
     
    122122        int cell = (HGT_ROW_LENGTH * (row - 1)) + col;
    123123
    124         //System.out.println("Read SRTM elevation data from row/col/cell " + row + "," + col + ", " + cell + ", " + sb.limit());
    125 
    126124        // valid position in buffer?
    127125        if (cell < sb.limit()) {
    128126            short ele = sb.get(cell);
    129             //System.out.println("==> Read SRTM elevation data from row/col/cell " + row + "," + col + ", " + cell + " = " + ele);
    130127            // check for data voids
    131128            if (ele == HGT_VOID) {
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/grid/EleVertex.java

    r34133 r35165  
    5959            for (int j = i + 1; j < points.length; j++) {
    6060                EleCoordinate c2 = points[j];
    61 
    6261                edges[k++] = new TriangleEdge(i, j, c1.greatCircleDistance(c2));
    6362            }
    6463        }
    65 
    66         /*
    67     for (int i = 0; i < edges.length; i++) {
    68         TriangleEdge triangleEdge = edges[i];
    69         System.out.println("#" + i + ": " +triangleEdge);
    70     }*/
    7164
    7265        // sort by distance
     
    7568        TriangleEdge longest = edges[0];
    7669
    77 
    78         //System.out.println("Longest " + longest);
    7970        EleCoordinate pI = points[longest.getI()];
    8071        EleCoordinate pJ = points[longest.getJ()];
    8172        EleCoordinate pK = points[longest.getK()];
    8273        EleCoordinate newP = getMid(pI, pJ);
    83         /*
    84     System.out.println(pI);
    85     System.out.println(pJ);
    86     System.out.println(pK);
    87     System.out.println(newP);
    88          */
     74
    8975        List<EleVertex> res = new ArrayList<>();
    9076        res.add(new EleVertex(pI, pK, newP));
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/grid/ElevationGridLayer.java

    r33815 r35165  
    6464            tileSet = new TileSet(box.getMin(), box.getMax(), ELE_ZOOM_LEVEL); // we use a vector format with constant zoom level
    6565            lastBounds = box;
    66             System.out.println("paint " + tileSet);
    6766        }
    6867
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/grid/ElevationGridTile.java

    r34095 r35165  
    1515
    1616import org.openstreetmap.gui.jmapviewer.Tile;
    17 import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
    1817import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
    1918import org.openstreetmap.josm.data.Bounds;
     
    4140            BufferedImage image) {
    4241        super(source, xtile, ytile, zoom, image);
    43 
    44 
    45     }
    46 
    47     @Override
    48     public void loadPlaceholderFromCache(TileCache cache) {
    49         // TODO Auto-generated method stub
    50         super.loadPlaceholderFromCache(cache);
    51 
    52         //System.out.println("loadPlaceholderFromCache");
    53     }
    54 
    55     @Override
    56     public String getUrl() throws IOException {
    57         // TODO Auto-generated method stub
    58         return super.getUrl();
    5942    }
    6043
     
    6750        super.paint(g, x, y);
    6851
    69         //g.drawString(String.format("EGT %d/%d ", getXtile(), getYtile()), x, y);
    7052        g.drawString(getStatus(), x, y);
    7153    }
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/gui/DefaultElevationProfileRenderer.java

    r34507 r35165  
    2626import org.openstreetmap.josm.plugins.elevation.gpx.ElevationWayPointKind;
    2727import org.openstreetmap.josm.tools.CheckParameterUtil;
     28import org.openstreetmap.josm.tools.Logging;
    2829
    2930/**
     
    6566
    6667        if (wpt == null || profile == null) {
    67             System.err.println(String.format(
    68                     "Cannot determine color: prof=%s, wpt=%s", profile, wpt));
     68            Logging.error(String.format("Cannot determine color: prof=%s, wpt=%s", profile, wpt));
    6969            return null;
    7070        }
     
    113113
    114114        if (wpt == null) {
    115             System.err.println(String.format(
    116                     "Cannot paint: mv=%s, prof=%s, wpt=%s", mv, profile, wpt));
     115            Logging.error(String.format("Cannot paint: mv=%s, prof=%s, wpt=%s", mv, profile, wpt));
    117116            return;
    118117        }
     
    142141
    143142        if (wpt1 == null || wpt2 == null) {
    144             System.err.println(String.format(
    145                     "Cannot paint line: mv=%s, prof=%s, kind = %s", mv, profile, kind));
     143            Logging.error(String.format("Cannot paint line: mv=%s, prof=%s, kind = %s", mv, profile, kind));
    146144            return;
    147145        }
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/gui/ElevationProfileDialog.java

    r33815 r35165  
    2626
    2727import org.openstreetmap.josm.data.SystemOfMeasurement;
     28import org.openstreetmap.josm.data.SystemOfMeasurement.SoMChangeListener;
    2829import org.openstreetmap.josm.data.gpx.GpxData;
    2930import org.openstreetmap.josm.gui.MainApplication;
     
    4950 * connection between layer and elevation profile.
    5051 */
    51 public class ElevationProfileDialog extends ToggleDialog implements LayerChangeListener, ActiveLayerChangeListener, ComponentListener {
     52public class ElevationProfileDialog extends ToggleDialog
     53implements LayerChangeListener, ActiveLayerChangeListener, ComponentListener, SoMChangeListener {
    5254
    5355    private static final String EMPTY_DATA_STRING = "-";
     
    198200    @Override
    199201    public void showNotify() {
     202        SystemOfMeasurement.addSoMChangeListener(this);
    200203        MainApplication.getLayerManager().addLayerChangeListener(this);
    201204        MainApplication.getLayerManager().addActiveLayerChangeListener(this);
     
    212215        MainApplication.getLayerManager().removeActiveLayerChangeListener(this);
    213216        MainApplication.getLayerManager().removeLayerChangeListener(this);
     217        SystemOfMeasurement.removeSoMChangeListener(this);
    214218    }
    215219
     
    469473        }
    470474    }
     475
     476    @Override
     477    public void systemOfMeasurementChanged(String oldSoM, String newSoM) {
     478        updateView();
     479    }
    471480}
Note: See TracChangeset for help on using the changeset viewer.