Changeset 29979 in osm for applications/editors/josm


Ignore:
Timestamp:
2013-09-27T14:36:40+02:00 (11 years ago)
Author:
oliverw
Message:

[josm_elevationprofile] Bugfix: Cursor showed always zero elevation when no SRTM values are present
[josm_elevationprofile] Bugfix: Cancel grid rendering, if no SRTM values are present

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

Legend:

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

    r29921 r29979  
    11/**
    2  * This program is free software: you can redistribute it and/or modify it under 
    3  * the terms of the GNU General Public License as published by the 
    4  * Free Software Foundation, either version 3 of the License, or 
    5  * (at your option) any later version. 
     2 * This program is free software: you can redistribute it and/or modify it under
     3 * the terms of the GNU General Public License as published by the
     4 * Free Software Foundation, either version 3 of the License, or
     5 * (at your option) any later version.
    66 *
    7  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
    8  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
    9  * See the GNU General Public License for more details. 
     7 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
     8 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     9 * See the GNU General Public License for more details.
    1010 *
    11  * You should have received a copy of the GNU General Public License along with this program. 
     11 * You should have received a copy of the GNU General Public License along with this program.
    1212 * If not, see <http://www.gnu.org/licenses/>.
    1313 */
     
    2828 * Provides methods to access way point attributes and some utility methods regarding elevation stuff (
    2929 * e. g. special text formats, unit conversion, geoid calc).
    30  * @author Oliver Wieland <oliver.wieland@online.de> 
     30 * @author Oliver Wieland <oliver.wieland@online.de>
    3131 */
    3232public class ElevationHelper {
    33         public static double METER_TO_FEET = 3.280948;
    34        
    35         /* Countries which use the imperial system instead of the metric system. */
    36         private static String IMPERIAL_SYSTEM_COUNTRIES[] = {
    37                 "en_US",        /* USA */
    38                 "en_CA",        /* Canada */
    39                 "en_AU",        /* Australia */
    40                 "en_NZ",        /* New Zealand */
    41 //              "de_DE",        /* for testing only */
    42                 "en_ZA" /* South Africa */
    43         };
    44        
    45         /** The 'no elevation' data magic. */
    46         public static double NO_ELEVATION = Double.NaN;
    47        
    48         /**
    49          * The name of the elevation height of a way point.
    50          */
    51         public static final String HEIGHT_ATTRIBUTE = "ele";
    52        
    53         private static UnitMode unitMode = UnitMode.NotSelected;
    54                
    55         private static GeoidCorrectionKind geoidKind = GeoidCorrectionKind.None;
    56        
    57         /** The HGT reader instance. */
    58         private static HgtReader hgt = new HgtReader();
    59        
    60         /**
    61          * Gets the current mode of GEOID correction.
    62          * @return
    63          */
    64         public static GeoidCorrectionKind getGeoidKind() {
    65                 return geoidKind;
    66         }
    67 
    68         public static void setGeoidKind(GeoidCorrectionKind geoidKind) {
    69                 ElevationHelper.geoidKind = geoidKind;
    70         }
    71        
    72         /**
    73          * Gets the current unit mode (metric or imperial).
    74          * @return
    75          */
    76         public static UnitMode getUnitMode() {
    77                 //TODO: Use this until /JOSM/src/org/openstreetmap/josm/gui/NavigatableComponent.java
    78                 // has a an appropriate method
    79                
    80                 // unit mode already determined?
    81                 if (unitMode != UnitMode.NotSelected) {
    82                         return unitMode;
    83                 }
    84                
    85                 // Set default
    86                 unitMode = UnitMode.Metric;
    87                
    88                 // Check if user could prefer imperial system
    89                 Locale l = Locale.getDefault();         
    90                 for (int i = 0; i < IMPERIAL_SYSTEM_COUNTRIES.length; i++) {
    91                         String ctry = l.toString();
    92                         if (IMPERIAL_SYSTEM_COUNTRIES[i].equals(ctry)) {
    93                                 unitMode = UnitMode.Imperial;
    94                         }
    95                 }
    96                
    97                 return unitMode;
    98         }
    99        
    100         /**
    101          * Gets the unit string for elevation ("m" or "ft").
    102          * @return
    103          */
    104         public static String getUnit() {               
    105                 switch (getUnitMode()) {
    106                 case Metric:
    107                         return "m";
    108                 case Imperial:
    109                         return "ft";
    110                 default:
    111                         throw new RuntimeException("Invalid or unsupported unit mode: " + unitMode);
    112                 }
    113         }
    114        
    115         /**
    116          * Checks if given value is a valid elevation value.
    117          *
    118          * @param ele the ele
    119          * @return true, if is valid elevation
    120          */
    121         public static boolean isValidElevation(double ele) {
    122             return !Double.isNaN(ele);
    123         }
    124        
    125         /**
    126          * Gets the elevation (Z coordinate) of a GPX way point in meter or feet (for
    127          * US, UK, ZA, AU, NZ and CA).
    128          *
    129          * @param wpt
    130          *            The way point instance.
    131          * @return The x coordinate or <code>NO_ELEVATION</code>, if the given way point is null or contains
    132          *         not height attribute.
    133          */
    134         public static double getElevation(WayPoint wpt) {
    135             if (wpt == null) return NO_ELEVATION;
    136            
    137             // try to get elevation from HGT file
    138             double eleInt = getInternalElevation(wpt.getCoor());           
    139             if (isValidElevation(eleInt)) {
    140                 return convert(eleInt);
     33    public static double METER_TO_FEET = 3.280948;
     34
     35    /* Countries which use the imperial system instead of the metric system. */
     36    private static String IMPERIAL_SYSTEM_COUNTRIES[] = {
     37        "en_US",        /* USA */
     38        "en_CA",        /* Canada */
     39        "en_AU",        /* Australia */
     40        "en_NZ",        /* New Zealand */
     41        //              "de_DE",        /* for testing only */
     42        "en_ZA" /* South Africa */
     43    };
     44
     45    /** The 'no elevation' data magic. */
     46    public static double NO_ELEVATION = Double.NaN;
     47
     48    /**
     49     * The name of the elevation height of a way point.
     50     */
     51    public static final String HEIGHT_ATTRIBUTE = "ele";
     52
     53    private static UnitMode unitMode = UnitMode.NotSelected;
     54
     55    private static GeoidCorrectionKind geoidKind = GeoidCorrectionKind.None;
     56
     57    /** The HGT reader instance. */
     58    private static HgtReader hgt = new HgtReader();
     59
     60    /**
     61     * Gets the current mode of GEOID correction.
     62     * @return
     63     */
     64    public static GeoidCorrectionKind getGeoidKind() {
     65        return geoidKind;
     66    }
     67
     68    public static void setGeoidKind(GeoidCorrectionKind geoidKind) {
     69        ElevationHelper.geoidKind = geoidKind;
     70    }
     71
     72    /**
     73     * Gets the current unit mode (metric or imperial).
     74     * @return
     75     */
     76    public static UnitMode getUnitMode() {
     77        //TODO: Use this until /JOSM/src/org/openstreetmap/josm/gui/NavigatableComponent.java
     78        // has a an appropriate method
     79
     80        // unit mode already determined?
     81        if (unitMode != UnitMode.NotSelected) {
     82            return unitMode;
     83        }
     84
     85        // Set default
     86        unitMode = UnitMode.Metric;
     87
     88        // Check if user could prefer imperial system
     89        Locale l = Locale.getDefault();
     90        for (int i = 0; i < IMPERIAL_SYSTEM_COUNTRIES.length; i++) {
     91            String ctry = l.toString();
     92            if (IMPERIAL_SYSTEM_COUNTRIES[i].equals(ctry)) {
     93                unitMode = UnitMode.Imperial;
    14194            }
    142            
    143             // no HGT, check for elevation data in GPX
    144             if (!wpt.attr.containsKey(HEIGHT_ATTRIBUTE)) {
    145                 // GPX has no elevation data :-(
    146                 return NO_ELEVATION;
     95        }
     96
     97        return unitMode;
     98    }
     99
     100    /**
     101     * Gets the unit string for elevation ("m" or "ft").
     102     * @return
     103     */
     104    public static String getUnit() {
     105        switch (getUnitMode()) {
     106        case Metric:
     107            return "m";
     108        case Imperial:
     109            return "ft";
     110        default:
     111            throw new RuntimeException("Invalid or unsupported unit mode: " + unitMode);
     112        }
     113    }
     114
     115    /**
     116     * Checks if given value is a valid elevation value.
     117     *
     118     * @param ele the ele
     119     * @return true, if is valid elevation
     120     */
     121    public static boolean isValidElevation(double ele) {
     122        return !Double.isNaN(ele);
     123    }
     124
     125    /**
     126     * Gets the elevation (Z coordinate) of a GPX way point in meter or feet (for
     127     * US, UK, ZA, AU, NZ and CA).
     128     *
     129     * @param wpt
     130     *            The way point instance.
     131     * @return The x coordinate or <code>NO_ELEVATION</code>, if the given way point is null or contains
     132     *         not height attribute.
     133     */
     134    public static double getElevation(WayPoint wpt) {
     135        if (wpt == null) return NO_ELEVATION;
     136
     137        // try to get elevation from HGT file
     138        double eleInt = getSrtmElevation(wpt.getCoor());
     139        if (isValidElevation(eleInt)) {
     140            return convert(eleInt);
     141        }
     142
     143        // no HGT, check for elevation data in GPX
     144        if (!wpt.attr.containsKey(HEIGHT_ATTRIBUTE)) {
     145            // GPX has no elevation data :-(
     146            return NO_ELEVATION;
     147        }
     148
     149        // Parse elevation from GPX data
     150        String height = wpt.getString(ElevationHelper.HEIGHT_ATTRIBUTE);
     151        try {
     152            double z = Double.parseDouble(height);
     153
     154            return convert(z);
     155        } catch (NumberFormatException e) {
     156            System.err.println(String.format(
     157                    "Cannot parse double from '%s': %s", height, e
     158                    .getMessage()));
     159            return NO_ELEVATION;
     160        }
     161    }
     162
     163
     164    private static double getElevation(LatLon ll) {
     165        double ele = getSrtmElevation(ll);
     166        //System.out.println("Get elevation " + ll + " => " + ele);
     167        return convert(ele);
     168    }
     169
     170    /**
     171     * Converts the value to feet, if required.
     172     *
     173     * @param ele the elevation to convert
     174     * @return the double
     175     */
     176    private static double convert(double ele) {
     177        if (isValidElevation(ele)) {
     178            if (getUnitMode() == UnitMode.Imperial) {
     179                // translate to feet
     180                return meter2Feet(ele);
     181            } else {
     182                // keep 'as is'
     183                return ele;
    147184            }
    148 
    149             // Parse elevation from GPX data
    150             String height = wpt.getString(ElevationHelper.HEIGHT_ATTRIBUTE);
    151             try {
    152                 double z = Double.parseDouble(height);
    153 
    154                 return convert(z);
    155             } catch (NumberFormatException e) {
    156                 System.err.println(String.format(
    157                         "Cannot parse double from '%s': %s", height, e
    158                         .getMessage()));
    159                 return NO_ELEVATION;
     185        }
     186        return NO_ELEVATION;
     187    }
     188
     189    /**
     190     * Computes the slope <b>in percent</b> between two way points. E. g. an elevation gain of 12m
     191     * within a distance of 100m is equal to a slope of 12%.
     192     *
     193     * @param w1 the first way point
     194     * @param w2 the second way point
     195     * @return the slope in percent
     196     */
     197    public static double computeSlope(LatLon w1, LatLon w2) {
     198        // same coordinates? -> return 0, if yes
     199        if (w1.equals(w2)) return 0;
     200
     201        // get distance in meters and divide it by 100 in advance
     202        double distInMeter = convert(w1.greatCircleDistance(w2) / 100.0);
     203
     204        // get elevation (difference) - is converted automatically to feet
     205        int ele1 = (int) ElevationHelper.getElevation(w1);
     206        int ele2 = (int) ElevationHelper.getElevation(w2);
     207        int dH = ele2 - ele1;
     208
     209        // Slope in percent is define as elevation gain/loss in meters related to a distance of 100m
     210        return dH / distInMeter;
     211    }
     212
     213    /**
     214     * Converts meter into feet
     215     *
     216     * @param meter the meter
     217     * @return the double
     218     */
     219    public static double meter2Feet(double meter) {
     220        return meter * METER_TO_FEET;
     221    }
     222
     223    /**
     224     * Gets the elevation string for a given elevation, e. g "300m" or "800ft".
     225     * @param elevation
     226     * @return
     227     */
     228    public static String getElevationText(int elevation) {
     229        return String.format("%d %s", elevation, getUnit());
     230    }
     231
     232    /**
     233     * Gets the elevation string for a given elevation, e. g "300m" or "800ft".
     234     * @param elevation
     235     * @return
     236     */
     237    public static String getElevationText(double elevation) {
     238        return String.format("%d %s", (int)Math.round(elevation), getUnit());
     239    }
     240
     241    /**
     242     * Gets the elevation string for a given way point, e. g "300m" or "800ft".
     243     *
     244     * @param wpt the way point
     245     * @return the elevation text
     246     */
     247    public static String getElevationText(WayPoint wpt) {
     248        if (wpt == null) return "-";
     249
     250        int elevation = (int)Math.round(ElevationHelper.getElevation(wpt));
     251        return String.format("%d %s", elevation, getUnit());
     252    }
     253
     254    /**
     255     * Get the time string for a given way point.
     256     * @param wpt
     257     * @return
     258     */
     259    public static String getTimeText(WayPoint wpt) {
     260        if (wpt == null) return null;
     261
     262        int hour = ElevationHelper.getHourOfWayPoint(wpt);
     263        int min = ElevationHelper.getMinuteOfWayPoint(wpt);
     264        return String.format("%02d:%02d", hour, min);
     265    }
     266
     267    /**
     268     * Gets the SRTM elevation (Z coordinate) of the given coordinate.
     269     *
     270     * @param ll
     271     *            The coordinate.
     272     * @return The z coordinate or {@link Double#NaN}, if elevation value could not be obtained
     273     *         not height attribute.
     274     */
     275    public static double getSrtmElevation(LatLon ll) {
     276        if (ll != null) {
     277            // Try to read data from SRTM file
     278            // TODO: Option to switch this off
     279            double eleHgt = hgt.getElevationFromHgt(ll);
     280
     281            //System.out.println("Get elevation from HGT " + ll + " => " + eleHgt);
     282            if (isValidElevation(eleHgt)) {
     283                return eleHgt;
    160284            }
    161285        }
    162        
    163        
    164         public static double getElevation(LatLon ll) {
    165                 double ele = getInternalElevation(ll);
    166                 //System.out.println("Get elevation " + ll + " => " + ele);
    167                 return convert(ele);
    168         }
    169 
    170         /**
    171          * Converts the value to feet, if required.
    172          *
    173          * @param ele the elevation to convert
    174          * @return the double
    175          */
    176         private static double convert(double ele) {
    177             if (isValidElevation(ele)) {
    178                 if (getUnitMode() == UnitMode.Imperial) {
    179                         // translate to feet
    180                         return meter2Feet(ele);
    181                 } else {
    182                     // keep 'as is'
    183                     return ele;
    184                 }
    185             }
    186             return NO_ELEVATION;           
    187         }
    188        
    189         /**
    190          * Computes the slope <b>in percent</b> between two way points. E. g. an elevation gain of 12m
    191          * within a distance of 100m is equal to a slope of 12%.
    192          *
    193          * @param w1 the first way point
    194          * @param w2 the second way point
    195          * @return the slope in percent
    196          */
    197         public static double computeSlope(LatLon w1, LatLon w2) {
    198                 // same coordinates? -> return 0, if yes
    199                 if (w1.equals(w2)) return 0;
    200            
    201                 // get distance in meters and divide it by 100 in advance
    202                 double distInMeter = convert(w1.greatCircleDistance(w2) / 100.0);
    203                
    204                 // get elevation (difference) - is converted automatically to feet
    205                 int ele1 = (int) ElevationHelper.getElevation(w1);
    206                 int ele2 = (int) ElevationHelper.getElevation(w2);
    207                 int dH = ele2 - ele1;
    208                
    209                 // Slope in percent is define as elevation gain/loss in meters related to a distance of 100m
    210                 return dH / distInMeter;
    211         }
    212        
    213         /**
    214          * Converts meter into feet
    215          *
    216          * @param meter the meter
    217          * @return the double
    218          */
    219         public static double meter2Feet(double meter) {
    220             return meter * METER_TO_FEET;
    221         }
    222        
    223         /**
    224          * Gets the elevation string for a given elevation, e. g "300m" or "800ft".
    225          * @param elevation
    226          * @return
    227          */
    228         public static String getElevationText(int elevation) {
    229                 return String.format("%d %s", elevation, getUnit());
    230         }
    231        
    232         /**
    233          * Gets the elevation string for a given elevation, e. g "300m" or "800ft".
    234          * @param elevation
    235          * @return
    236          */
    237         public static String getElevationText(double elevation) {
    238                 return String.format("%d %s", (int)Math.round(elevation), getUnit());
    239         }
    240        
    241         /**
    242          * Gets the elevation string for a given way point, e. g "300m" or "800ft".
    243          *
    244          * @param wpt the way point
    245          * @return the elevation text
    246          */
    247         public static String getElevationText(WayPoint wpt) {
    248                 return getElevationText(wpt.getCoor());
    249         }
    250        
    251         /**
    252          * Gets the elevation string for a given coordinate, e. g "300m" or "800ft".
    253          *
    254          * @param coor the coordinate
    255          * @return the elevation text
    256          */
    257         public static String getElevationText(LatLon coor) {
    258                 if (coor == null) return null;
    259                
    260                 int elevation = (int)Math.round(ElevationHelper.getElevation(coor));
    261                 return String.format("%d %s", (int)elevation, getUnit());
    262         }
    263        
    264         /**
    265          * Get the time string for a given way point.
    266          * @param wpt
    267          * @return
    268          */
    269         public static String getTimeText(WayPoint wpt) {
    270                 if (wpt == null) return null;
    271                
    272                 int hour = ElevationHelper.getHourOfWayPoint(wpt);
    273                 int min = ElevationHelper.getMinuteOfWayPoint(wpt);             
    274                 return String.format("%02d:%02d", hour, min);
    275         }
    276 
    277         /**
    278          * Gets the elevation (Z coordinate) of a GPX way point.
    279          *
    280          * @param ll
    281          *            The way point instance.
    282          * @return The x coordinate or 0, if the given way point is null or contains
    283          *         not height attribute.
    284          */
    285         private static double getInternalElevation(LatLon ll) {
    286                 if (ll != null) {
    287                         // Try to read data from SRTM file
    288                         // TODO: Option to switch this off
    289                         double eleHgt = hgt.getElevationFromHgt(ll);
    290                        
    291                         //System.out.println("Get elevation from HGT " + ll + " => " + eleHgt);
    292                         if (isValidElevation(eleHgt)) {                     
    293                             return eleHgt;
    294                         }
    295                 }
    296                 return NO_ELEVATION;
    297         }
    298 
     286        return NO_ELEVATION;
     287    }
     288
     289    /*
     290     * Gets the geoid height for the given way point. See also {@link
     291     * GeoidData}.
     292     */
     293    public static byte getGeoidCorrection(WayPoint wpt) {
    299294        /*
    300          * Gets the geoid height for the given way point. See also {@link
    301          * GeoidData}.
    302          */
    303         public static byte getGeoidCorrection(WayPoint wpt) {
    304                 /*
    305295                int lat = (int)Math.round(wpt.getCoor().lat());
    306296                int lon = (int)Math.round(wpt.getCoor().lon());
    307297                byte geoid = GeoidData.getGeoid(lat, lon);
    308                
     298
    309299                System.out.println(
    310300                                String.format("Geoid(%d, %d) = %d", lat, lon, geoid));
    311                  */
    312                 return 0;
    313         }
    314 
    315         /**
    316          * Reduces a given list of way points to the specified target size.
    317          *
    318          * @param origList
    319          *            The original list containing the way points.
    320          * @param targetSize
    321          *            The desired target size of the list. The resulting list may
    322          *            contain fewer items, so targetSize should be considered as
    323          *            maximum.
    324          * @return A list containing the reduced list.
    325301         */
    326         public static List<WayPoint> downsampleWayPoints(List<WayPoint> origList,
    327                         int targetSize) {
    328                 if (origList == null)
    329                         return null;
    330                 if (targetSize <= 0)
    331                         throw new IllegalArgumentException(
    332                                         "targetSize must be greater than zero");
    333 
    334                 int origSize = origList.size();
    335                 if (origSize <= targetSize) {
    336                         return origList;
    337                 }
    338 
    339                 int delta = (int) Math.max(Math.ceil(origSize / targetSize), 2);
    340 
    341                 List<WayPoint> res = new ArrayList<WayPoint>(targetSize);
    342                 for (int i = 0; i < origSize; i += delta) {
    343                         res.add(origList.get(i));
    344                 }
    345 
    346                 return res;
    347         }
    348        
    349         /**
    350          * Gets the hour value of a way point in 24h format.
    351          * @param wpt
    352          * @return
    353          */
    354         public static int getHourOfWayPoint(WayPoint wpt) {
    355                 if (wpt == null) return -1;
    356                
    357                 Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
    358                 calendar.setTime(wpt.getTime());   // assigns calendar to given date
    359                 return calendar.get(Calendar.HOUR_OF_DAY);
    360         }
    361        
    362         /**
    363          * Gets the minute value of a way point in 24h format.
    364          * @param wpt
    365          * @return
    366          */
    367         public static int getMinuteOfWayPoint(WayPoint wpt) {
    368                 if (wpt == null) return -1;
    369                
    370                 Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
    371                 calendar.setTime(wpt.getTime());   // assigns calendar to given date
    372                 return calendar.get(Calendar.MINUTE);
    373         }
     302        return 0;
     303    }
     304
     305    /**
     306     * Reduces a given list of way points to the specified target size.
     307     *
     308     * @param origList
     309     *            The original list containing the way points.
     310     * @param targetSize
     311     *            The desired target size of the list. The resulting list may
     312     *            contain fewer items, so targetSize should be considered as
     313     *            maximum.
     314     * @return A list containing the reduced list.
     315     */
     316    public static List<WayPoint> downsampleWayPoints(List<WayPoint> origList,
     317            int targetSize) {
     318        if (origList == null)
     319            return null;
     320        if (targetSize <= 0)
     321            throw new IllegalArgumentException(
     322                    "targetSize must be greater than zero");
     323
     324        int origSize = origList.size();
     325        if (origSize <= targetSize) {
     326            return origList;
     327        }
     328
     329        int delta = (int) Math.max(Math.ceil(origSize / targetSize), 2);
     330
     331        List<WayPoint> res = new ArrayList<WayPoint>(targetSize);
     332        for (int i = 0; i < origSize; i += delta) {
     333            res.add(origList.get(i));
     334        }
     335
     336        return res;
     337    }
     338
     339    /**
     340     * Gets the hour value of a way point in 24h format.
     341     * @param wpt
     342     * @return
     343     */
     344    public static int getHourOfWayPoint(WayPoint wpt) {
     345        if (wpt == null) return -1;
     346
     347        Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
     348        calendar.setTime(wpt.getTime());   // assigns calendar to given date
     349        return calendar.get(Calendar.HOUR_OF_DAY);
     350    }
     351
     352    /**
     353     * Gets the minute value of a way point in 24h format.
     354     * @param wpt
     355     * @return
     356     */
     357    public static int getMinuteOfWayPoint(WayPoint wpt) {
     358        if (wpt == null) return -1;
     359
     360        Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
     361        calendar.setTime(wpt.getTime());   // assigns calendar to given date
     362        return calendar.get(Calendar.MINUTE);
     363    }
    374364}
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/grid/EleVertex.java

    r29964 r29979  
    11/**
    2  * This program is free software: you can redistribute it and/or modify it under 
    3  * the terms of the GNU General Public License as published by the 
    4  * Free Software Foundation, either version 3 of the License, or 
    5  * (at your option) any later version. 
     2 * This program is free software: you can redistribute it and/or modify it under
     3 * the terms of the GNU General Public License as published by the
     4 * Free Software Foundation, either version 3 of the License, or
     5 * (at your option) any later version.
    66 *
    7  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
    8  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
    9  * See the GNU General Public License for more details. 
     7 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
     8 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     9 * See the GNU General Public License for more details.
    1010 *
    11  * You should have received a copy of the GNU General Public License along with this program. 
     11 * You should have received a copy of the GNU General Public License along with this program.
    1212 * If not, see <http://www.gnu.org/licenses/>.
    1313 */
     
    2424    private static final int NPOINTS = 3;
    2525    private static final double MIN_DIST = 90;
    26    
     26
    2727    private double avrgEle = Double.NaN;
    2828    private double area = Double.NaN;
    29     private EleCoordinate[] points = new EleCoordinate[NPOINTS];
    30    
     29    private final EleCoordinate[] points = new EleCoordinate[NPOINTS];
     30
    3131    public EleVertex(EleCoordinate p1, EleCoordinate p2, EleCoordinate p3) {
    3232        points[0] = p1;
    3333        points[1] = p2;
    3434        points[2] = p3;
    35        
     35
    3636        // compute elevation
    3737        double z = 0D;
     
    4545            }
    4646        }
    47        
    48         if (eleValid) { 
     47
     48        if (eleValid) {
    4949            avrgEle = z / NPOINTS;
    5050        } else {
    5151            avrgEle = ElevationHelper.NO_ELEVATION;
    5252        }
    53        
     53
    5454        // compute the (approx.!) area of the vertex using heron's formula
    5555        double a = p1.greatCircleDistance(p2);
    5656        double b = p2.greatCircleDistance(p3);
    5757        double c = p1.greatCircleDistance(p3);
    58        
     58
    5959        double s = (a + b + c) / 2D;
    6060        double sq = s * (s - a) * (s - b) * (s - c);
    6161        area = Math.sqrt(sq);
    6262    }
    63    
     63
    6464    public List<EleVertex> divide() {
    6565        TriangleEdge[] edges = new TriangleEdge[NPOINTS];
    66        
     66
    6767        int k = 0;
    6868        for (int i = 0; i < points.length; i++) {
    6969            EleCoordinate c1 = points[i];
    70            
     70
    7171            for (int j = i + 1; j < points.length; j++) {
    7272                EleCoordinate c2 = points[j];
    73                
     73
    7474                edges[k++] = new TriangleEdge(i, j, c1.greatCircleDistance(c2));
    7575            }
     
    8181            System.out.println("#" + i + ": " +triangleEdge);
    8282        }*/
    83        
     83
    8484        // sort by distance
    8585        Arrays.sort(edges);
    8686        // pick the longest edge
    8787        TriangleEdge longest = edges[0];
    88        
    89        
     88
     89
    9090        //System.out.println("Longest " + longest);
    9191        EleCoordinate pI = points[longest.getI()];
     
    9898        System.out.println(pK);
    9999        System.out.println(newP);
    100         */
     100         */
    101101        List<EleVertex> res = new ArrayList<EleVertex>();
    102102        res.add(new EleVertex(pI, pK, newP));
    103103        res.add(new EleVertex(pJ, pK, newP));
    104        
     104
    105105        return res;
    106106    }
    107    
     107
    108108    /**
    109109     * Checks if vertex requires further processing or is finished. Currently this
    110      * method returns <code>true</code>, if the average deviation is < 5m 
     110     * method returns <code>true</code>, if the average deviation is < 5m
    111111     *
    112112     * @return true, if is finished
     
    115115        double z = 0D;
    116116        double avrgEle = getEle();
    117        
     117
    118118        for (EleCoordinate point : points) {
    119119            z += (avrgEle - point.getEle()) * (avrgEle - point.getEle());
    120120        }
    121        
     121
    122122        // TODO: Check for proper limit
    123123        return /*z < 75 || */getArea() < (30 * 30); // = 3 * 25
    124     }   
    125    
     124    }
     125
    126126    /**
    127127     * Gets the approximate area of this vertex in square meters.
     
    130130     */
    131131    public double getArea() {
    132         return area;
     132        return area;
    133133    }
    134134
     
    143143        double x = (c1.getX() + c2.getX()) / 2.0;
    144144        double y = (c1.getY() + c2.getY()) / 2.0;
    145        
     145
    146146        double z = (c1.getEle() + c2.getEle()) / 2.0;
    147147        if (c1.greatCircleDistance(c2) > MIN_DIST) {
    148             double hgtZ = ElevationHelper.getElevation(new LatLon(y, x));
    149            
     148            double hgtZ = ElevationHelper.getSrtmElevation(new LatLon(y, x));
     149
    150150            if (ElevationHelper.isValidElevation(hgtZ)) {
    151151                z = hgtZ;
    152             }       
    153         }
    154        
     152            }
     153        }
     154
    155155        return new EleCoordinate(y, x, z);
    156156    }
    157    
     157
    158158    /**
    159159     * Gets the coordinate for the given index.
    160160     *
    161      * @param index the index between 0 and NPOINTS: 
     161     * @param index the index between 0 and NPOINTS:
    162162     * @return the elevation coordinate instance
    163163     * @throws IllegalArgumentException, if index is invalid
     
    165165    public EleCoordinate get(int index) {
    166166        if (index < 0 || index >= NPOINTS) throw new IllegalArgumentException("Invalid index: " + index);
    167        
     167
    168168        return points[index];
    169169    }
    170    
     170
    171171    /**
    172172     * Gets the average elevation of this vertex.
     
    175175     */
    176176    public double getEle() {
    177        
     177
    178178        return avrgEle;
    179179    }
    180    
     180
    181181    @Override
    182182    public String toString() {
     
    189189
    190190    class TriangleEdge implements Comparable<TriangleEdge> {
    191         private int i;
    192         private int j;
    193         private double dist;
    194        
     191        private final int i;
     192        private final int j;
     193        private final double dist;
     194
    195195        public TriangleEdge(int i, int j, double dist) {
    196196            super();
     
    207207            return j;
    208208        }
    209        
     209
    210210        public int getK() {
    211211            if (i == 0) {
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/grid/ElevationGridTile.java

    r29977 r29979  
    11package org.openstreetmap.josm.plugins.elevation.grid;
     2
     3import static org.openstreetmap.josm.tools.I18n.tr;
    24
    35import java.awt.Graphics;
     
    2224
    2325public class ElevationGridTile extends Tile {
    24     private BlockingDeque<EleVertex> toDo = new LinkedBlockingDeque<EleVertex>();
    25     private BlockingDeque<EleVertex> vertices = new LinkedBlockingDeque<EleVertex>();
     26    private final BlockingDeque<EleVertex> toDo = new LinkedBlockingDeque<EleVertex>();
     27    private final BlockingDeque<EleVertex> vertices = new LinkedBlockingDeque<EleVertex>();
    2628
    2729    private Bounds box;
    28    
     30
    2931    public ElevationGridTile(TileSource source, int xtile, int ytile, int zoom) {
    3032        super(source, xtile, ytile, zoom);
     
    3739            BufferedImage image) {
    3840        super(source, xtile, ytile, zoom, image);
    39        
    40        
     41
     42
    4143    }
    4244
     
    4547        // TODO Auto-generated method stub
    4648        super.loadPlaceholderFromCache(cache);
    47        
     49
    4850        //System.out.println("loadPlaceholderFromCache");
    4951    }
     
    6163    public void paint(Graphics g, int x, int y) {
    6264        super.paint(g, x, y);
    63        
     65
    6466        //g.drawString(String.format("EGT %d/%d ", getXtile(), getYtile()), x, y);
    6567        g.drawString(getStatus(), x, y);
    6668    }
    67    
     69
    6870    /**
    6971     * Paints the vertices of this tile.
     
    9092    @Override
    9193    public void loadImage(InputStream input) throws IOException {
    92         if (isLoaded()) return; 
    93        
    94         // TODO: Save 
    95        
     94        if (isLoaded()) return;
     95
     96        // TODO: Save
     97
    9698        // We abuse the loadImage method to render the vertices...
    97         // 
     99        //
    98100        while (toDo.size() > 0) {
    99101            EleVertex vertex = toDo.poll();
    100            
    101             if (vertex.isFinished()) {         
     102
     103            if (vertex.isFinished()) {
    102104                vertices.add(vertex);
    103105            } else {
     
    110112        setLoaded(true);
    111113    }
    112    
     114
    113115    public BlockingDeque<EleVertex> getVertices() {
    114         return vertices;
     116        return vertices;
    115117    }
    116118
     
    136138        LatLon min = box.getMin();
    137139        LatLon max = box.getMax();
    138        
     140
    139141        // compute missing coordinates
    140         LatLon h1 = new LatLon(min.lat(), max.lon()); 
     142        LatLon h1 = new LatLon(min.lat(), max.lon());
    141143        LatLon h2 = new LatLon(max.lat(), min.lon());
    142        
     144
     145        double eleMin = ElevationHelper.getSrtmElevation(min);
     146        double eleMax = ElevationHelper.getSrtmElevation(max);
     147
     148        // SRTM files present?
     149        if (!ElevationHelper.isValidElevation(eleMax) || !ElevationHelper.isValidElevation(eleMin)) {
     150            setError(tr("No SRTM data"));
     151            return;
     152        }
     153
    143154        // compute elevation coords
    144         EleCoordinate p0 = new EleCoordinate(min, ElevationHelper.getElevation(min));   
    145         EleCoordinate p1 = new EleCoordinate(h1, ElevationHelper.getElevation(h1));
    146         EleCoordinate p2 = new EleCoordinate(max, ElevationHelper.getElevation(max));
    147         EleCoordinate p3 = new EleCoordinate(h2, ElevationHelper.getElevation(h2));
    148                
     155        EleCoordinate p0 = new EleCoordinate(min, eleMin);
     156        EleCoordinate p1 = new EleCoordinate(h1, ElevationHelper.getSrtmElevation(h1));
     157        EleCoordinate p2 = new EleCoordinate(max, eleMax);
     158        EleCoordinate p3 = new EleCoordinate(h2, ElevationHelper.getSrtmElevation(h2));
     159
    149160        // compute initial vertices
    150161        EleVertex v1 = new EleVertex(p0, p1, p2);
     
    160171                + ", ytile=" + ytile + "]";
    161172    }
    162    
    163    
     173
     174
    164175}
Note: See TracChangeset for help on using the changeset viewer.