Changeset 6203 in josm for trunk/src/org/openstreetmap/josm/data/Bounds.java
- Timestamp:
- 2013-08-28T03:03:40+02:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Bounds.java
r6162 r6203 9 9 10 10 import org.openstreetmap.josm.data.coor.LatLon; 11 import org.openstreetmap.josm.data.osm.BBox; 11 12 import org.openstreetmap.josm.tools.CheckParameterUtil; 12 13 … … 27 28 } 28 29 30 /** 31 * Returns min latitude of bounds. Efficient shortcut for {@code getMin().lat()}. 32 * 33 * @return min latitude of bounds. 34 * @since 6203 35 */ 36 public double getMinLat() { 37 return minLat; 38 } 39 40 /** 41 * Returns min longitude of bounds. Efficient shortcut for {@code getMin().lon()}. 42 * 43 * @return min longitude of bounds. 44 * @since 6203 45 */ 46 public double getMinLon() { 47 return minLon; 48 } 49 29 50 public LatLon getMax() { 30 51 return new LatLon(maxLat, maxLon); 52 } 53 54 /** 55 * Returns max latitude of bounds. Efficient shortcut for {@code getMax().lat()}. 56 * 57 * @return max latitude of bounds. 58 * @since 6203 59 */ 60 public double getMaxLat() { 61 return maxLat; 62 } 63 64 /** 65 * Returns max longitude of bounds. Efficient shortcut for {@code getMax().lon()}. 66 * 67 * @return max longitude of bounds. 68 * @since 6203 69 */ 70 public double getMaxLon() { 71 return maxLon; 31 72 } 32 73 … … 37 78 38 79 /** 39 * Construct bounds out of two points 80 * Construct bounds out of two points. Coords will be rounded. 40 81 */ 41 82 public Bounds(LatLon min, LatLon max) { … … 51 92 } 52 93 94 /** 95 * Single point Bounds defined by lat/lon {@code b}. 96 * Coordinates will be rounded to osm precision if {@code roundToOsmPrecision} is true. 97 * 98 * @param b lat/lon of given point. 99 * @param roundToOsmPrecision defines if lat/lon will be rounded. 100 */ 53 101 public Bounds(LatLon b, boolean roundToOsmPrecision) { 102 this(b.lat(), b.lon(), roundToOsmPrecision); 103 } 104 105 /** 106 * Single point Bounds defined by point [lat,lon]. 107 * Coordinates will be rounded to osm precision if {@code roundToOsmPrecision} is true. 108 * 109 * @param lat latitude of given point. 110 * @param lon longitude of given point. 111 * @param roundToOsmPrecision defines if lat/lon will be rounded. 112 * @since 6203 113 */ 114 public Bounds(double lat, double lon, boolean roundToOsmPrecision) { 54 115 // Do not call this(b, b) to avoid GPX performance issue (see #7028) until roundToOsmPrecision() is improved 55 116 if (roundToOsmPrecision) { 56 this.minLat = LatLon.roundToOsmPrecision( b.lat());57 this.minLon = LatLon.roundToOsmPrecision( b.lon());58 } else { 59 this.minLat = b.lat();60 this.minLon = b.lon();117 this.minLat = LatLon.roundToOsmPrecision(lat); 118 this.minLon = LatLon.roundToOsmPrecision(lon); 119 } else { 120 this.minLat = lat; 121 this.minLon = lon; 61 122 } 62 123 this.maxLat = this.minLat; … … 153 214 } 154 215 155 public Bounds(Bounds other) { 156 this(other.getMin(), other.getMax()); 216 /** 217 * Creates new {@code Bounds} from an existing one. 218 * @param other The bounds to copy 219 */ 220 public Bounds(final Bounds other) { 221 this(other.minLat, other.minLon, other.maxLat, other.maxLon); 157 222 } 158 223 … … 186 251 } 187 252 253 /** 254 * Creates BBox with same coordinates. 255 * 256 * @return BBox with same coordinates. 257 * @since 6203 258 */ 259 public BBox toBBox() { 260 return new BBox(minLon, minLat, maxLon, maxLat); 261 } 262 188 263 @Override public String toString() { 189 264 return "Bounds["+minLat+","+minLon+","+maxLat+","+maxLon+"]"; … … 203 278 public LatLon getCenter() 204 279 { 280 if (crosses180thMeridian()) { 281 double lat = (minLat + maxLat) / 2; 282 double lon = (minLon + maxLon - 360.0) / 2; 283 if (lon < -180.0){ 284 lon += 360.0; 285 } 286 return new LatLon(lat, lon); 287 } else { 288 return new LatLon((minLat + maxLat) / 2, (minLon + maxLon) / 2); 289 } 290 } 291 292 /** 293 * Extend the bounds if necessary to include the given point. 294 * @param ll The point to include into these bounds 295 */ 296 public void extend(LatLon ll) { 297 extend(ll.lat(), ll.lon()); 298 } 299 300 /** 301 * Extend the bounds if necessary to include the given point [lat,lon]. 302 * Good to use if you know coordinates to avoid creation of LatLon object. 303 * @param lat Latitude of point to include into these bounds 304 * @param lon Longitude of point to include into these bounds 305 * @since 6203 306 */ 307 public void extend(final double lat, final double lon) { 308 if (lat < minLat) { 309 minLat = LatLon.roundToOsmPrecision(lat); 310 } 311 if (lat > maxLat) { 312 maxLat = LatLon.roundToOsmPrecision(lat); 313 } 205 314 if (crosses180thMeridian()) { 206 LatLon result = new LatLon(minLat, minLon-360.0).getCenter(getMax()); 207 if (result.lon() < -180.0) { 208 result = new LatLon(result.lat(), result.lon() + 360.0); 209 } 210 return result; 211 } else { 212 return getMin().getCenter(getMax()); 213 } 214 } 215 216 /** 217 * Extend the bounds if necessary to include the given point. 218 */ 219 public void extend(LatLon ll) { 220 if (ll.lat() < minLat) { 221 minLat = LatLon.roundToOsmPrecision(ll.lat()); 222 } 223 if (ll.lat() > maxLat) { 224 maxLat = LatLon.roundToOsmPrecision(ll.lat()); 225 } 226 if (crosses180thMeridian()) { 227 if (ll.lon() > maxLon && ll.lon() < minLon) { 228 if (Math.abs(ll.lon() - minLon) <= Math.abs(ll.lon() - maxLon)) { 229 minLon = LatLon.roundToOsmPrecision(ll.lon()); 315 if (lon > maxLon && lon < minLon) { 316 if (Math.abs(lon - minLon) <= Math.abs(lon - maxLon)) { 317 minLon = LatLon.roundToOsmPrecision(lon); 230 318 } else { 231 maxLon = LatLon.roundToOsmPrecision(l l.lon());319 maxLon = LatLon.roundToOsmPrecision(lon); 232 320 } 233 321 } 234 322 } else { 235 if (l l.lon()< minLon) {236 minLon = LatLon.roundToOsmPrecision(l l.lon());323 if (lon < minLon) { 324 minLon = LatLon.roundToOsmPrecision(lon); 237 325 } 238 if (l l.lon()> maxLon) {239 maxLon = LatLon.roundToOsmPrecision(l l.lon());326 if (lon > maxLon) { 327 maxLon = LatLon.roundToOsmPrecision(lon); 240 328 } 241 329 } … … 243 331 244 332 public void extend(Bounds b) { 245 extend(b.getMin()); 246 extend(b.getMax()); 247 } 248 249 /** 250 * Is the given point within this bounds? 333 extend(b.minLat, b.minLon); 334 extend(b.maxLat, b.maxLon); 335 } 336 337 /** 338 * Determines if the given point {@code ll} is within these bounds. 339 * @param ll The lat/lon to check 340 * @return {@code true} if {@code ll} is within these bounds, {@code false} otherwise 251 341 */ 252 342 public boolean contains(LatLon ll) { … … 324 414 */ 325 415 public boolean isCollapsed() { 326 return getMin().equals(getMax());416 return (minLat == maxLat) && (minLon == maxLon); 327 417 } 328 418
Note:
See TracChangeset
for help on using the changeset viewer.