Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / main / java / org / gvsig / raster / cache / tile / impl / TileImpl.java @ 595

History | View | Annotate | Download (8.1 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.cache.tile.impl;
23

    
24
import java.awt.geom.AffineTransform;
25
import java.awt.geom.NoninvertibleTransformException;
26
import java.awt.geom.Point2D;
27
import java.awt.geom.Rectangle2D;
28
import java.io.File;
29
import java.util.HashMap;
30

    
31
import org.gvsig.raster.cache.tile.AtomicTask;
32
import org.gvsig.raster.cache.tile.Tile;
33
import org.gvsig.raster.cache.tile.exception.TileGettingException;
34
import org.gvsig.raster.cache.tile.impl.pool.TilePipe;
35
import org.gvsig.raster.cache.tile.provider.Downloader;
36

    
37
/**
38
 * Main implementation for the tile cache
39
 *
40
 * @author Nacho Brodin (nachobrodin@gmail.com)
41
 */
42
public class TileImpl implements Tile {
43
        private boolean     corrupt          = false;
44
        private int         level            = 0;
45
        private int         col              = 0;
46
        private int         row              = 0;
47
        private int         widthPx          = 0;
48
        private int         heightPx         = 0;
49
        private Point2D     ul               = null;
50
        private Point2D     lr               = null;
51
        
52
        private File        file             = null;
53
        private Downloader  downloader       = null;
54
        private HashMap<String, Object>    
55
                        downloaderParams = new HashMap<String, Object>();
56
        private TilePipe    sharedPipe       = null;
57
        private int         priority         = 0;
58
        private Object[]    data             = null;
59
        
60
        public TileImpl(int level, int col, int row) {
61
                this.level = level;
62
                this.col = col;
63
                this.row = row;
64
        }
65
        
66
        public TileImpl(int wPx, int hPx, int row, int col, Point2D ul, Point2D lr) {
67
                this.row = row;
68
                this.col = col;
69
                this.ul = ul;
70
                this.lr = lr;
71
                this.widthPx = wPx;
72
                this.heightPx = hPx;
73
        }
74
        
75
        /**
76
         * Convierte un punto desde del mundo a coordenadas pixel.
77
         * @param pt Punto a transformar
78
         * @return punto transformado en coordenadas pixel
79
         */
80
        public Point2D worldToRaster(Point2D pt) {
81
                Point2D p = new Point2D.Double();
82
                double psX = (lr.getX() - ul.getX()) / widthPx;
83
                double psY = (lr.getY() - ul.getY()) / heightPx;
84
                AffineTransform t = new AffineTransform(psX, 0, 0, psY, ul.getX() - (psX / 2), ul.getY() + (psY / 2));
85
                try {
86
                        t.inverseTransform(pt, p);
87
                } catch (NoninvertibleTransformException e) {
88
                        return pt;
89
                }
90
                return p;
91
        }
92
        
93
        public Tile cloneTile() {
94
                TileImpl newTile = new TileImpl(widthPx, heightPx, row, col, (Point2D)ul.clone(), (Point2D)lr.clone());
95
                newTile.file = file;
96
                return newTile;
97
        }
98
        
99
        /*
100
         * (non-Javadoc)
101
         * @see org.gvsig.raster.tilecache.Tile#getLevel()
102
         */
103
        public int getLevel() {
104
                return level;
105
        }
106

    
107
        /*
108
         * (non-Javadoc)
109
         * @see org.gvsig.raster.tilecache.Tile#getX()
110
         */
111
        public int getCol() {
112
                return col;
113
        }
114

    
115
        /*
116
         * (non-Javadoc)
117
         * @see org.gvsig.raster.tilecache.Tile#getY()
118
         */
119
        public int getRow() {
120
                return row;
121
        }
122
        
123
        /*
124
         * (non-Javadoc)
125
         * @see org.gvsig.raster.tilecache.Tile#setX(int)
126
         */
127
        public void setCol(int col) {
128
                this.col = col;
129
        }
130
        
131
        /*
132
         * (non-Javadoc)
133
         * @see org.gvsig.raster.tilecache.Tile#setY(int)
134
         */
135
        public void setRow(int row) {
136
                this.row = row;
137
        }
138
        
139
        /*
140
         * (non-Javadoc)
141
         * @see org.gvsig.raster.tilecache.Tile#setLevel(int)
142
         */
143
        public void setLevel(int level) {
144
                this.level = level;
145
        }
146

    
147
        /*
148
         * (non-Javadoc)
149
         * @see org.gvsig.raster.cache.tile.Tile#getWidthPx()
150
         */
151
        public int getWidthPx() {
152
                return widthPx;
153
        }
154

    
155
        /*
156
         * (non-Javadoc)
157
         * @see org.gvsig.raster.cache.tile.Tile#setWidthPx(int)
158
         */
159
        public void setWidthPx(int widthPx) {
160
                this.widthPx = widthPx;
161
        }
162

    
163
        /*
164
         * (non-Javadoc)
165
         * @see org.gvsig.raster.cache.tile.Tile#getHeightPx()
166
         */
167
        public int getHeightPx() {
168
                return heightPx;
169
        }
170

    
171
        /*
172
         * (non-Javadoc)
173
         * @see org.gvsig.raster.cache.tile.Tile#setHeightPx(int)
174
         */
175
        public void setHeightPx(int heightPx) {
176
                this.heightPx = heightPx;
177
        }
178
        
179
        /*
180
         * (non-Javadoc)
181
         * @see org.gvsig.raster.cache.tile.Tile#getExtent()
182
         */
183
        public Rectangle2D getExtent() {
184
                return new Rectangle2D.Double(ul.getX(), ul.getY(), 
185
                                Math.abs(ul.getX() - lr.getX()), Math.abs(ul.getY() - lr.getY()));
186
        }
187

    
188
        /*
189
         * (non-Javadoc)
190
         * @see org.gvsig.raster.cache.tile.Tile#getUl()
191
         */
192
        public Point2D getUl() {
193
                return ul;
194
        }
195

    
196
        /*
197
         * (non-Javadoc)
198
         * @see org.gvsig.raster.cache.tile.Tile#setUl(java.awt.geom.Point2D)
199
         */
200
        public void setUl(Point2D ul) {
201
                this.ul = ul;
202
        }
203

    
204
        /*
205
         * (non-Javadoc)
206
         * @see org.gvsig.raster.cache.tile.Tile#getLr()
207
         */
208
        public Point2D getLr() {
209
                return lr;
210
        }
211

    
212
        /*
213
         * (non-Javadoc)
214
         * @see org.gvsig.raster.cache.tile.Tile#setLr(java.awt.geom.Point2D)
215
         */
216
        public void setLr(Point2D lr) {
217
                this.lr = lr;
218
        }
219

    
220
        /*
221
         * (non-Javadoc)
222
         * @see org.gvsig.raster.cache.tile.Tile#getFile()
223
         */
224
        public File getFile() {
225
                return file;
226
        }
227

    
228
        /*
229
         * (non-Javadoc)
230
         * @see org.gvsig.raster.cache.tile.Tile#setFile(java.io.File)
231
         */
232
        public void setFile(File file) {
233
                this.file = file;
234
        }
235

    
236
        /**
237
         * Gets the downloader for this tile
238
         * @return
239
         */
240
        public Downloader getDownloader() {
241
                return downloader;
242
        }
243

    
244
        /**
245
         * Sets the downloader for this tile
246
         * @param downloader
247
         */
248
        public void setDownloader(Downloader downloader) {
249
                this.downloader = downloader;
250
        }
251

    
252
        /*
253
         * (non-Javadoc)
254
         * @see org.gvsig.raster.cache.tile.Tile#getDownloaderParams()
255
         */
256
        public Object getDownloaderParams(String key) {
257
                return downloaderParams.get(key.toUpperCase());
258
        }
259

    
260
        /*
261
         * (non-Javadoc)
262
         * @see org.gvsig.raster.cache.tile.Tile#setDownloaderParams(java.lang.Object[])
263
         */
264
        public void setDownloaderParams(String key, Object value) {
265
                this.downloaderParams.put(key.toUpperCase(), value);
266
        }
267
        
268
        /*
269
         * (non-Javadoc)
270
         * @see org.gvsig.raster.cache.tile.Tile#setCorrupt()
271
         */
272
        public void setCorrupt() {
273
                corrupt = true;
274
        }
275
        
276
        /**
277
         * Actions for download a tile
278
         */
279
        public void run() {
280
                try {
281
                        downloader.downloadTile(this);
282
                        if(!corrupt && sharedPipe != null)
283
                                sharedPipe.setTile(this);
284
                        corrupt = false;
285
                } catch (TileGettingException e) {
286
                }
287
                
288
                //System.out.println("Downloading " + this.getRow() + " " + this.getCol());
289
        }
290

    
291
        /**
292
         * Gets the pipe of tiles
293
         * @return
294
         */
295
        public TilePipe getSharedPipe() {
296
                return sharedPipe;
297
        }
298

    
299
        /**
300
         * Sets the pipe of tiles
301
         * @param sharedPipe
302
         */
303
        public void setSharedPipe(TilePipe sharedPipe) {
304
                this.sharedPipe = sharedPipe;
305
        }
306
        
307
        /*
308
         * (non-Javadoc)
309
         * @see org.gvsig.raster.cache.tile.Tile#getId()
310
         */
311
        public String getId() {
312
                return getLevel() + "_" + getRow() + "_" + getCol();
313
        }
314

    
315
        /*
316
         * (non-Javadoc)
317
         * @see org.gvsig.raster.cache.tile.AtomicTask#getPriority()
318
         */
319
        public int getPriority() {
320
                return priority;
321
        }
322
        
323
        /*
324
         * (non-Javadoc)
325
         * @see org.gvsig.raster.cache.tile.AtomicTask#setPriority(int)
326
         */
327
        public void setPriority(int priority) {
328
                this.priority = priority;
329
        }
330
        
331
        /*
332
         * (non-Javadoc)
333
         * @see org.gvsig.raster.cache.tile.Tile#setData(java.lang.Object)
334
         */
335
        public void setData(Object[] data) {
336
                this.data = data;
337
        }
338
        
339
        /*
340
         * (non-Javadoc)
341
         * @see org.gvsig.raster.cache.tile.Tile#getData()
342
         */
343
        public Object[] getData() {
344
                return data;
345
        }
346

    
347
        /*
348
         * (non-Javadoc)
349
         * @see java.lang.Comparable#compareTo(java.lang.Object)
350
         */
351
        public int compareTo(AtomicTask o) {
352
                if(getPriority() > o.getPriority())
353
                        return 1;
354
                if(getPriority() < o.getPriority())
355
                        return -1;
356
                return 0;
357
        }
358
        
359
        /*
360
         * (non-Javadoc)
361
         * @see org.gvsig.raster.cache.tile.Tile#dataIsLoaded()
362
         */
363
        public boolean dataIsLoaded() {
364
                return (getData() != null && getData().length > 0);
365
        }
366

    
367
}