2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
7 package gov
.nasa
.worldwind
.util
;
9 import gov
.nasa
.worldwind
.cache
.Cacheable
;
10 import gov
.nasa
.worldwind
.geom
.*;
12 import java
.util
.Random
;
16 * @version $Id: Tile.java 2786 2007-09-10 17:41:00Z tgaskins $
18 public class Tile
implements Comparable
<Tile
>, Cacheable
20 private final Sector sector
;
21 private final Level level
;
22 private final int row
;
23 private final int column
;
24 private final TileKey tileKey
;
25 private double priority
= Double
.MAX_VALUE
; // Default is minimum priority
26 // The following is late bound because it's only selectively needed and costly to create
29 public Tile(Sector sector
, Level level
, int row
, int column
)
33 String msg
= Logging
.getMessage("nullValue.SectorIsNull");
34 Logging
.logger().severe(msg
);
35 throw new IllegalArgumentException(msg
);
40 String msg
= Logging
.getMessage("nullValue.LevelIsNull");
41 Logging
.logger().severe(msg
);
42 throw new IllegalArgumentException(msg
);
47 String msg
= Logging
.getMessage("generic.RowIndexOutOfRange", row
);
48 msg
+= String
.valueOf(row
);
50 Logging
.logger().severe(msg
);
51 throw new IllegalArgumentException(msg
);
56 String msg
= Logging
.getMessage("generic.ColumnIndexOutOfRange", column
);
57 msg
+= String
.valueOf(row
);
59 Logging
.logger().severe(msg
);
60 throw new IllegalArgumentException(msg
);
67 this.tileKey
= new TileKey(this);
71 public Tile(Sector sector
, Level level
)
75 String msg
= Logging
.getMessage("nullValue.SectorIsNull");
76 Logging
.logger().severe(msg
);
77 throw new IllegalArgumentException(msg
);
81 String msg
= Logging
.getMessage("nullValue.LevelIsNull");
82 Logging
.logger().severe(msg
);
83 throw new IllegalArgumentException(msg
);
88 this.row
= Tile
.computeRow(sector
.getDeltaLat(), sector
.getMinLatitude());
89 this.column
= Tile
.computeColumn(sector
.getDeltaLon(), sector
.getMinLongitude());
90 this.tileKey
= new TileKey(this);
94 public Tile(Sector sector
)
98 String msg
= Logging
.getMessage("nullValue.SectorIsNull");
99 Logging
.logger().severe(msg
);
100 throw new IllegalArgumentException(msg
);
103 Random random
= new Random();
105 this.sector
= sector
;
107 this.row
= random
.nextInt();
108 this.column
= random
.nextInt();
110 this.tileKey
= new TileKey(this);
113 public long getSizeInBytes()
115 // Return just an approximate size
118 if (this.sector
!= null)
119 size
+= this.sector
.getSizeInBytes();
121 if (this.path
!= null)
122 size
+= this.getPath().length();
124 size
+= 32; // to account for the references and the TileKey size
129 public String
getPath()
131 if (this.path
== null)
133 this.path
= this.level
.getPath() + "/" + this.row
+ "/" + this.row
+ "_" + this.column
;
134 if (!this.level
.isEmpty())
135 path
+= this.level
.getFormatSuffix();
141 public final Sector
getSector()
146 public Level
getLevel()
151 public final int getLevelNumber()
153 return this.level
!= null ?
this.level
.getLevelNumber() : 0;
156 public final String
getLevelName()
158 return this.level
!= null ?
this.level
.getLevelName() : "";
161 public final int getRow()
166 public final int getColumn()
171 public final String
getCacheName()
173 return this.level
!= null ?
this.level
.getCacheName() : null;
176 public final String
getFormatSuffix()
178 return this.level
!= null ?
this.level
.getFormatSuffix() : null;
181 public final TileKey
getTileKey()
186 public java
.net
.URL
getResourceURL() throws java
.net
.MalformedURLException
188 return this.level
!= null ?
this.level
.getTileResourceURL(this) : null;
191 public String
getLabel()
193 StringBuilder sb
= new StringBuilder();
195 sb
.append(this.getLevelNumber());
197 sb
.append(this.getLevelName());
199 sb
.append(", ").append(this.getRow());
200 sb
.append(", ").append(this.getColumn());
202 return sb
.toString();
205 public int compareTo(Tile tile
)
209 String msg
= Logging
.getMessage("nullValue.TileIsNull");
210 Logging
.logger().severe(msg
);
211 throw new IllegalArgumentException(msg
);
214 // No need to compare Sectors or path because they are redundant with row and column
215 if (tile
.getLevelNumber() == this.getLevelNumber() && tile
.row
== this.row
&& tile
.column
== this.column
)
218 if (this.getLevelNumber() < tile
.getLevelNumber()) // Lower-res levels compare lower than higher-res
220 if (this.getLevelNumber() > tile
.getLevelNumber())
223 if (this.row
< tile
.row
)
225 if (this.row
> tile
.row
)
228 if (this.column
< tile
.column
)
231 return 1; // tile.column must be > this.column because equality was tested above
235 public boolean equals(Object o
)
237 // Equality based only on the tile key
240 if (o
== null || getClass() != o
.getClass())
243 final Tile tile
= (Tile
) o
;
245 return !(tileKey
!= null ?
!tileKey
.equals(tile
.tileKey
) : tile
.tileKey
!= null);
249 public int hashCode()
251 return (tileKey
!= null ? tileKey
.hashCode() : 0);
255 public String
toString()
257 return this.getPath();
261 * Computes the row index of a latitude in the global tile grid corresponding to a specified grid interval.
263 * @param delta the grid interval
264 * @param latitude the latitude for which to compute the row index
265 * @return the row index of the row containing the specified latitude
266 * @throws IllegalArgumentException if <code>delta</code> is null or non-positive, or <code>latitude</code> is null,
267 * greater than positive 90 degrees, or less than negative 90 degrees
269 public static int computeRow(Angle delta
, Angle latitude
)
271 if (delta
== null || latitude
== null)
273 String message
= Logging
.getMessage("nullValue.AngleIsNull");
274 Logging
.logger().severe(message
);
275 throw new IllegalArgumentException(message
);
278 if (delta
.degrees
<= 0d
)
280 String message
= Logging
.getMessage("generic.DeltaAngleOutOfRange", delta
);
281 Logging
.logger().severe(message
);
282 throw new IllegalArgumentException(message
);
285 if (latitude
.degrees
< -90d
|| latitude
.degrees
> 90d
)
287 String message
= Logging
.getMessage("generic.AngleOutOfRange", latitude
);
288 Logging
.logger().severe(message
);
289 throw new IllegalArgumentException(message
);
292 if (latitude
.degrees
== 90d
)
293 return (int) (180d
/ delta
.degrees
) - 1;
295 return (int) ((latitude
.degrees
+ 90d
) / delta
.degrees
);
299 * Computes the column index of a longitude in the global tile grid corresponding to a specified grid interval.
301 * @param delta the grid interval
302 * @param longitude the longitude for which to compute the column index
303 * @return the column index of the column containing the specified latitude
304 * @throws IllegalArgumentException if <code>delta</code> is null or non-positive, or <code>longitude</code> is
305 * null, greater than positive 180 degrees, or less than negative 180 degrees
307 public static int computeColumn(Angle delta
, Angle longitude
)
309 if (delta
== null || longitude
== null)
311 String message
= Logging
.getMessage("nullValue.AngleIsNull");
312 Logging
.logger().severe(message
);
313 throw new IllegalArgumentException(message
);
316 if (delta
.degrees
<= 0d
)
318 String message
= Logging
.getMessage("generic.DeltaAngleOutOfRange", delta
);
319 Logging
.logger().severe(message
);
320 throw new IllegalArgumentException(message
);
323 if (longitude
.degrees
< -180d
|| longitude
.degrees
> 180d
)
325 String message
= Logging
.getMessage("generic.AngleOutOfRange", longitude
);
326 Logging
.logger().severe(message
);
327 throw new IllegalArgumentException(message
);
330 if (longitude
.degrees
== 180d
)
331 return (int) (360d
/ delta
.degrees
) - 1;
333 return (int) ((longitude
.degrees
+ 180d
) / delta
.degrees
);
337 * Determines the minimum latitude of a row in the global tile grid corresponding to a specified grid interval.
339 * @param row the row index of the row in question
340 * @param delta the grid interval
341 * @return the minimum latitude of the tile corresponding to the specified row
342 * @throws IllegalArgumentException if the grid interval (<code>delta</code>) is null or zero or the row index is
345 public static Angle
computeRowLatitude(int row
, Angle delta
)
349 String message
= Logging
.getMessage("nullValue.AngleIsNull");
350 Logging
.logger().severe(message
);
351 throw new IllegalArgumentException(message
);
355 String msg
= Logging
.getMessage("generic.RowIndexOutOfRange", row
);
356 Logging
.logger().severe(msg
);
357 throw new IllegalArgumentException(msg
);
360 if (delta
.degrees
<= 0d
)
362 String message
= Logging
.getMessage("generic.DeltaAngleOutOfRange", delta
);
363 Logging
.logger().severe(message
);
364 throw new IllegalArgumentException(message
);
367 return Angle
.fromDegrees(-90d
+ delta
.degrees
* row
);
371 * Determines the minimum longitude of a column in the global tile grid corresponding to a specified grid interval.
373 * @param column the row index of the row in question
374 * @param delta the grid interval
375 * @return the minimum longitude of the tile corresponding to the specified column
376 * @throws IllegalArgumentException if the grid interval (<code>delta</code>) is null or zero or the column index is
379 public static Angle
computeColumnLongitude(int column
, Angle delta
)
383 String message
= Logging
.getMessage("nullValue.AngleIsNull");
384 Logging
.logger().severe(message
);
385 throw new IllegalArgumentException(message
);
389 String msg
= Logging
.getMessage("generic.ColumnIndexOutOfRange", column
);
390 Logging
.logger().severe(msg
);
391 throw new IllegalArgumentException(msg
);
394 if (delta
.degrees
<= 0d
)
396 String message
= Logging
.getMessage("generic.DeltaAngleOutOfRange", delta
);
397 Logging
.logger().severe(message
);
398 throw new IllegalArgumentException(message
);
401 return Angle
.fromDegrees(-180 + delta
.degrees
* column
);
404 public double getPriority()
409 public void setPriority(double priority
)
411 this.priority
= priority
;