Changeset 29979 in osm for applications/editors/josm/plugins/ElevationProfile/src
- Timestamp:
- 2013-09-27T14:36:40+02:00 (11 years ago)
- 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 1 1 /** 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. 6 6 * 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. 10 10 * 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. 12 12 * If not, see <http://www.gnu.org/licenses/>. 13 13 */ … … 28 28 * Provides methods to access way point attributes and some utility methods regarding elevation stuff ( 29 29 * 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> 31 31 */ 32 32 public 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; 141 94 } 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; 147 184 } 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; 160 284 } 161 285 } 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) { 299 294 /* 300 * Gets the geoid height for the given way point. See also {@link301 * GeoidData}.302 */303 public static byte getGeoidCorrection(WayPoint wpt) {304 /*305 295 int lat = (int)Math.round(wpt.getCoor().lat()); 306 296 int lon = (int)Math.round(wpt.getCoor().lon()); 307 297 byte geoid = GeoidData.getGeoid(lat, lon); 308 298 309 299 System.out.println( 310 300 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 origList319 * The original list containing the way points.320 * @param targetSize321 * The desired target size of the list. The resulting list may322 * contain fewer items, so targetSize should be considered as323 * maximum.324 * @return A list containing the reduced list.325 301 */ 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 } 374 364 } -
applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/grid/EleVertex.java
r29964 r29979 1 1 /** 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. 6 6 * 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. 10 10 * 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. 12 12 * If not, see <http://www.gnu.org/licenses/>. 13 13 */ … … 24 24 private static final int NPOINTS = 3; 25 25 private static final double MIN_DIST = 90; 26 26 27 27 private double avrgEle = Double.NaN; 28 28 private double area = Double.NaN; 29 private EleCoordinate[] points = new EleCoordinate[NPOINTS];30 29 private final EleCoordinate[] points = new EleCoordinate[NPOINTS]; 30 31 31 public EleVertex(EleCoordinate p1, EleCoordinate p2, EleCoordinate p3) { 32 32 points[0] = p1; 33 33 points[1] = p2; 34 34 points[2] = p3; 35 35 36 36 // compute elevation 37 37 double z = 0D; … … 45 45 } 46 46 } 47 48 if (eleValid) { 47 48 if (eleValid) { 49 49 avrgEle = z / NPOINTS; 50 50 } else { 51 51 avrgEle = ElevationHelper.NO_ELEVATION; 52 52 } 53 53 54 54 // compute the (approx.!) area of the vertex using heron's formula 55 55 double a = p1.greatCircleDistance(p2); 56 56 double b = p2.greatCircleDistance(p3); 57 57 double c = p1.greatCircleDistance(p3); 58 58 59 59 double s = (a + b + c) / 2D; 60 60 double sq = s * (s - a) * (s - b) * (s - c); 61 61 area = Math.sqrt(sq); 62 62 } 63 63 64 64 public List<EleVertex> divide() { 65 65 TriangleEdge[] edges = new TriangleEdge[NPOINTS]; 66 66 67 67 int k = 0; 68 68 for (int i = 0; i < points.length; i++) { 69 69 EleCoordinate c1 = points[i]; 70 70 71 71 for (int j = i + 1; j < points.length; j++) { 72 72 EleCoordinate c2 = points[j]; 73 73 74 74 edges[k++] = new TriangleEdge(i, j, c1.greatCircleDistance(c2)); 75 75 } … … 81 81 System.out.println("#" + i + ": " +triangleEdge); 82 82 }*/ 83 83 84 84 // sort by distance 85 85 Arrays.sort(edges); 86 86 // pick the longest edge 87 87 TriangleEdge longest = edges[0]; 88 89 88 89 90 90 //System.out.println("Longest " + longest); 91 91 EleCoordinate pI = points[longest.getI()]; … … 98 98 System.out.println(pK); 99 99 System.out.println(newP); 100 */100 */ 101 101 List<EleVertex> res = new ArrayList<EleVertex>(); 102 102 res.add(new EleVertex(pI, pK, newP)); 103 103 res.add(new EleVertex(pJ, pK, newP)); 104 104 105 105 return res; 106 106 } 107 107 108 108 /** 109 109 * 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 111 111 * 112 112 * @return true, if is finished … … 115 115 double z = 0D; 116 116 double avrgEle = getEle(); 117 117 118 118 for (EleCoordinate point : points) { 119 119 z += (avrgEle - point.getEle()) * (avrgEle - point.getEle()); 120 120 } 121 121 122 122 // TODO: Check for proper limit 123 123 return /*z < 75 || */getArea() < (30 * 30); // = 3 * 25 124 } 125 124 } 125 126 126 /** 127 127 * Gets the approximate area of this vertex in square meters. … … 130 130 */ 131 131 public double getArea() { 132 132 return area; 133 133 } 134 134 … … 143 143 double x = (c1.getX() + c2.getX()) / 2.0; 144 144 double y = (c1.getY() + c2.getY()) / 2.0; 145 145 146 146 double z = (c1.getEle() + c2.getEle()) / 2.0; 147 147 if (c1.greatCircleDistance(c2) > MIN_DIST) { 148 double hgtZ = ElevationHelper.get Elevation(new LatLon(y, x));149 148 double hgtZ = ElevationHelper.getSrtmElevation(new LatLon(y, x)); 149 150 150 if (ElevationHelper.isValidElevation(hgtZ)) { 151 151 z = hgtZ; 152 } 153 154 152 } 153 } 154 155 155 return new EleCoordinate(y, x, z); 156 156 } 157 157 158 158 /** 159 159 * Gets the coordinate for the given index. 160 160 * 161 * @param index the index between 0 and NPOINTS: 161 * @param index the index between 0 and NPOINTS: 162 162 * @return the elevation coordinate instance 163 163 * @throws IllegalArgumentException, if index is invalid … … 165 165 public EleCoordinate get(int index) { 166 166 if (index < 0 || index >= NPOINTS) throw new IllegalArgumentException("Invalid index: " + index); 167 167 168 168 return points[index]; 169 169 } 170 170 171 171 /** 172 172 * Gets the average elevation of this vertex. … … 175 175 */ 176 176 public double getEle() { 177 177 178 178 return avrgEle; 179 179 } 180 180 181 181 @Override 182 182 public String toString() { … … 189 189 190 190 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 195 195 public TriangleEdge(int i, int j, double dist) { 196 196 super(); … … 207 207 return j; 208 208 } 209 209 210 210 public int getK() { 211 211 if (i == 0) { -
applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/grid/ElevationGridTile.java
r29977 r29979 1 1 package org.openstreetmap.josm.plugins.elevation.grid; 2 3 import static org.openstreetmap.josm.tools.I18n.tr; 2 4 3 5 import java.awt.Graphics; … … 22 24 23 25 public 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>(); 26 28 27 29 private Bounds box; 28 30 29 31 public ElevationGridTile(TileSource source, int xtile, int ytile, int zoom) { 30 32 super(source, xtile, ytile, zoom); … … 37 39 BufferedImage image) { 38 40 super(source, xtile, ytile, zoom, image); 39 40 41 42 41 43 } 42 44 … … 45 47 // TODO Auto-generated method stub 46 48 super.loadPlaceholderFromCache(cache); 47 49 48 50 //System.out.println("loadPlaceholderFromCache"); 49 51 } … … 61 63 public void paint(Graphics g, int x, int y) { 62 64 super.paint(g, x, y); 63 65 64 66 //g.drawString(String.format("EGT %d/%d ", getXtile(), getYtile()), x, y); 65 67 g.drawString(getStatus(), x, y); 66 68 } 67 69 68 70 /** 69 71 * Paints the vertices of this tile. … … 90 92 @Override 91 93 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 96 98 // We abuse the loadImage method to render the vertices... 97 // 99 // 98 100 while (toDo.size() > 0) { 99 101 EleVertex vertex = toDo.poll(); 100 101 if (vertex.isFinished()) { 102 103 if (vertex.isFinished()) { 102 104 vertices.add(vertex); 103 105 } else { … … 110 112 setLoaded(true); 111 113 } 112 114 113 115 public BlockingDeque<EleVertex> getVertices() { 114 116 return vertices; 115 117 } 116 118 … … 136 138 LatLon min = box.getMin(); 137 139 LatLon max = box.getMax(); 138 140 139 141 // compute missing coordinates 140 LatLon h1 = new LatLon(min.lat(), max.lon()); 142 LatLon h1 = new LatLon(min.lat(), max.lon()); 141 143 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 143 154 // compute elevation coords 144 EleCoordinate p0 = new EleCoordinate(min, ElevationHelper.getElevation(min));145 EleCoordinate p1 = new EleCoordinate(h1, ElevationHelper.get Elevation(h1));146 EleCoordinate p2 = new EleCoordinate(max, ElevationHelper.getElevation(max));147 EleCoordinate p3 = new EleCoordinate(h2, ElevationHelper.get Elevation(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 149 160 // compute initial vertices 150 161 EleVertex v1 = new EleVertex(p0, p1, p2); … … 160 171 + ", ytile=" + ytile + "]"; 161 172 } 162 163 173 174 164 175 }
Note:
See TracChangeset
for help on using the changeset viewer.