Update to Worldwind release 20070920
[worldwind-tracker.git] / gov / nasa / worldwind / util / Tile.java
blob2e0821e69a4c2868915a440c9a283d207b54bef9
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.util;
9 import gov.nasa.worldwind.cache.Cacheable;
10 import gov.nasa.worldwind.geom.*;
12 import java.util.Random;
14 /**
15 * @author tag
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
27 private String path;
29 public Tile(Sector sector, Level level, int row, int column)
31 if (sector == null)
33 String msg = Logging.getMessage("nullValue.SectorIsNull");
34 Logging.logger().severe(msg);
35 throw new IllegalArgumentException(msg);
38 if (level == null)
40 String msg = Logging.getMessage("nullValue.LevelIsNull");
41 Logging.logger().severe(msg);
42 throw new IllegalArgumentException(msg);
45 if (row < 0)
47 String msg = Logging.getMessage("generic.RowIndexOutOfRange", row);
48 msg += String.valueOf(row);
50 Logging.logger().severe(msg);
51 throw new IllegalArgumentException(msg);
54 if (column < 0)
56 String msg = Logging.getMessage("generic.ColumnIndexOutOfRange", column);
57 msg += String.valueOf(row);
59 Logging.logger().severe(msg);
60 throw new IllegalArgumentException(msg);
63 this.sector = sector;
64 this.level = level;
65 this.row = row;
66 this.column = column;
67 this.tileKey = new TileKey(this);
68 this.path = null;
71 public Tile(Sector sector, Level level)
73 if (sector == null)
75 String msg = Logging.getMessage("nullValue.SectorIsNull");
76 Logging.logger().severe(msg);
77 throw new IllegalArgumentException(msg);
79 if (level == null)
81 String msg = Logging.getMessage("nullValue.LevelIsNull");
82 Logging.logger().severe(msg);
83 throw new IllegalArgumentException(msg);
86 this.sector = sector;
87 this.level = level;
88 this.row = Tile.computeRow(sector.getDeltaLat(), sector.getMinLatitude());
89 this.column = Tile.computeColumn(sector.getDeltaLon(), sector.getMinLongitude());
90 this.tileKey = new TileKey(this);
91 this.path = null;
94 public Tile(Sector sector)
96 if (sector == null)
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;
106 this.level = null;
107 this.row = random.nextInt();
108 this.column = random.nextInt();
109 this.path = null;
110 this.tileKey = new TileKey(this);
113 public long getSizeInBytes()
115 // Return just an approximate size
116 long size = 0;
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
126 return 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();
138 return this.path;
141 public final Sector getSector()
143 return sector;
146 public Level getLevel()
148 return level;
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()
163 return row;
166 public final int getColumn()
168 return column;
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()
183 return this.tileKey;
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());
196 sb.append("(");
197 sb.append(this.getLevelName());
198 sb.append(")");
199 sb.append(", ").append(this.getRow());
200 sb.append(", ").append(this.getColumn());
202 return sb.toString();
205 public int compareTo(Tile tile)
207 if (tile == null)
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)
216 return 0;
218 if (this.getLevelNumber() < tile.getLevelNumber()) // Lower-res levels compare lower than higher-res
219 return -1;
220 if (this.getLevelNumber() > tile.getLevelNumber())
221 return 1;
223 if (this.row < tile.row)
224 return -1;
225 if (this.row > tile.row)
226 return 1;
228 if (this.column < tile.column)
229 return -1;
231 return 1; // tile.column must be > this.column because equality was tested above
234 @Override
235 public boolean equals(Object o)
237 // Equality based only on the tile key
238 if (this == o)
239 return true;
240 if (o == null || getClass() != o.getClass())
241 return false;
243 final Tile tile = (Tile) o;
245 return !(tileKey != null ? !tileKey.equals(tile.tileKey) : tile.tileKey != null);
248 @Override
249 public int hashCode()
251 return (tileKey != null ? tileKey.hashCode() : 0);
254 @Override
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;
294 else
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;
332 else
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
343 * negative.
345 public static Angle computeRowLatitude(int row, Angle delta)
347 if (delta == null)
349 String message = Logging.getMessage("nullValue.AngleIsNull");
350 Logging.logger().severe(message);
351 throw new IllegalArgumentException(message);
353 if (row < 0)
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
377 * negative.
379 public static Angle computeColumnLongitude(int column, Angle delta)
381 if (delta == null)
383 String message = Logging.getMessage("nullValue.AngleIsNull");
384 Logging.logger().severe(message);
385 throw new IllegalArgumentException(message);
387 if (column < 0)
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()
406 return priority;
409 public void setPriority(double priority)
411 this.priority = priority;