001// License: GPL. For details, see Readme.txt file.
002package org.openstreetmap.gui.jmapviewer;
003
004import java.awt.Graphics;
005import java.awt.Graphics2D;
006import java.awt.geom.AffineTransform;
007import java.awt.image.BufferedImage;
008import java.io.IOException;
009import java.io.InputStream;
010import java.util.HashMap;
011import java.util.Map;
012import java.util.concurrent.Callable;
013
014import javax.imageio.ImageIO;
015
016import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
017import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
018
019/**
020 * Holds one map tile. Additionally the code for loading the tile image and
021 * painting it is also included in this class.
022 *
023 * @author Jan Peter Stotz
024 */
025public class Tile {
026
027    /**
028     * Hourglass image that is displayed until a map tile has been loaded, except for overlay sources
029     */
030    public static final BufferedImage LOADING_IMAGE = loadImage("images/hourglass.png");
031
032    /**
033     * Red cross image that is displayed after a loading error, except for overlay sources
034     */
035    public static final BufferedImage ERROR_IMAGE = loadImage("images/error.png");
036
037    protected TileSource source;
038    protected int xtile;
039    protected int ytile;
040    protected int zoom;
041    protected BufferedImage image;
042    protected String key;
043    protected volatile boolean loaded; // field accessed by multiple threads without any monitors, needs to be volatile
044    protected volatile boolean loading;
045    protected volatile boolean error;
046    protected String error_message;
047
048    /** TileLoader-specific tile metadata */
049    protected Map<String, String> metadata;
050
051    /**
052     * Creates a tile with empty image.
053     *
054     * @param source Tile source
055     * @param xtile X coordinate
056     * @param ytile Y coordinate
057     * @param zoom Zoom level
058     */
059    public Tile(TileSource source, int xtile, int ytile, int zoom) {
060        this(source, xtile, ytile, zoom, LOADING_IMAGE);
061    }
062
063    /**
064     * Creates a tile with specified image.
065     *
066     * @param source Tile source
067     * @param xtile X coordinate
068     * @param ytile Y coordinate
069     * @param zoom Zoom level
070     * @param image Image content
071     */
072    public Tile(TileSource source, int xtile, int ytile, int zoom, BufferedImage image) {
073        this.source = source;
074        this.xtile = xtile;
075        this.ytile = ytile;
076        this.zoom = zoom;
077        this.image = image;
078        this.key = getTileKey(source, xtile, ytile, zoom);
079    }
080
081    private static BufferedImage loadImage(String path) {
082        try {
083            return ImageIO.read(JMapViewer.class.getResourceAsStream(path));
084        } catch (IOException | IllegalArgumentException ex) {
085            ex.printStackTrace();
086            return null;
087        }
088    }
089
090    private static class CachedCallable<V> implements Callable<V> {
091        private V result;
092        private Callable<V> callable;
093
094        /**
095         * Wraps callable so it is evaluated only once
096         * @param callable to cache
097         */
098        CachedCallable(Callable<V> callable) {
099            this.callable = callable;
100        }
101
102        @Override
103        public synchronized V call() {
104            try {
105                if (result == null) {
106                    result = callable.call();
107                }
108                return result;
109            } catch (Exception e) {
110                // this should not happen here
111                throw new RuntimeException(e);
112            }
113        }
114    }
115
116    /**
117     * Tries to get tiles of a lower or higher zoom level (one or two level
118     * difference) from cache and use it as a placeholder until the tile has been loaded.
119     * @param cache Tile cache
120     */
121    public void loadPlaceholderFromCache(TileCache cache) {
122        /*
123         *  use LazyTask as creation of BufferedImage is very expensive
124         *  this way we can avoid object creation until we're sure it's needed
125         */
126        final CachedCallable<BufferedImage> tmpImage = new CachedCallable<>(new Callable<BufferedImage>() {
127            @Override
128            public BufferedImage call() throws Exception {
129                return new BufferedImage(source.getTileSize(), source.getTileSize(), BufferedImage.TYPE_INT_RGB);
130            }
131        });
132
133        for (int zoomDiff = 1; zoomDiff < 5; zoomDiff++) {
134            // first we check if there are already the 2^x tiles
135            // of a higher detail level
136            int zoomHigh = zoom + zoomDiff;
137            if (zoomDiff < 3 && zoomHigh <= JMapViewer.MAX_ZOOM) {
138                int factor = 1 << zoomDiff;
139                int xtileHigh = xtile << zoomDiff;
140                int ytileHigh = ytile << zoomDiff;
141                final double scale = 1.0 / factor;
142
143                /*
144                 * use LazyTask for graphics to avoid evaluation of tmpImage, until we have
145                 * something to draw
146                 */
147                CachedCallable<Graphics2D> graphics = new CachedCallable<>(new Callable<Graphics2D>() {
148                    @Override
149                    public Graphics2D call() throws Exception {
150                        Graphics2D g = (Graphics2D) tmpImage.call().getGraphics();
151                        g.setTransform(AffineTransform.getScaleInstance(scale, scale));
152                        return g;
153                    }
154                });
155
156                int paintedTileCount = 0;
157                for (int x = 0; x < factor; x++) {
158                    for (int y = 0; y < factor; y++) {
159                        Tile tile = cache.getTile(source, xtileHigh + x, ytileHigh + y, zoomHigh);
160                        if (tile != null && tile.isLoaded()) {
161                            paintedTileCount++;
162                            tile.paint(graphics.call(), x * source.getTileSize(), y * source.getTileSize());
163                        }
164                    }
165                }
166                if (paintedTileCount == factor * factor) {
167                    image = tmpImage.call();
168                    return;
169                }
170            }
171
172            int zoomLow = zoom - zoomDiff;
173            if (zoomLow >= JMapViewer.MIN_ZOOM) {
174                int xtileLow = xtile >> zoomDiff;
175                int ytileLow = ytile >> zoomDiff;
176                final int factor = 1 << zoomDiff;
177                final double scale = factor;
178                CachedCallable<Graphics2D> graphics = new CachedCallable<>(new Callable<Graphics2D>() {
179                    @Override
180                    public Graphics2D call() throws Exception {
181                        Graphics2D g = (Graphics2D) tmpImage.call().getGraphics();
182                        AffineTransform at = new AffineTransform();
183                        int translateX = (xtile % factor) * source.getTileSize();
184                        int translateY = (ytile % factor) * source.getTileSize();
185                        at.setTransform(scale, 0, 0, scale, -translateX, -translateY);
186                        g.setTransform(at);
187                        return g;
188                    }
189
190                });
191
192                Tile tile = cache.getTile(source, xtileLow, ytileLow, zoomLow);
193                if (tile != null && tile.isLoaded()) {
194                    tile.paint(graphics.call(), 0, 0);
195                    image = tmpImage.call();
196                    return;
197                }
198            }
199        }
200    }
201
202    public TileSource getSource() {
203        return source;
204    }
205
206    /**
207     * Returns the X coordinate.
208     * @return tile number on the x axis of this tile
209     */
210    public int getXtile() {
211        return xtile;
212    }
213
214    /**
215     * Returns the Y coordinate.
216     * @return tile number on the y axis of this tile
217     */
218    public int getYtile() {
219        return ytile;
220    }
221
222    /**
223     * Returns the zoom level.
224     * @return zoom level of this tile
225     */
226    public int getZoom() {
227        return zoom;
228    }
229
230    public BufferedImage getImage() {
231        return image;
232    }
233
234    public void setImage(BufferedImage image) {
235        this.image = image;
236    }
237
238    public void loadImage(InputStream input) throws IOException {
239        image = ImageIO.read(input);
240    }
241
242    /**
243     * @return key that identifies a tile
244     */
245    public String getKey() {
246        return key;
247    }
248
249    public boolean isLoaded() {
250        return loaded;
251    }
252
253    public boolean isLoading() {
254        return loading;
255    }
256
257    public void setLoaded(boolean loaded) {
258        this.loaded = loaded;
259    }
260
261    public String getUrl() throws IOException {
262        return source.getTileUrl(zoom, xtile, ytile);
263    }
264
265    /**
266     * Paints the tile-image on the {@link Graphics} <code>g</code> at the
267     * position <code>x</code>/<code>y</code>.
268     *
269     * @param g the Graphics object
270     * @param x x-coordinate in <code>g</code>
271     * @param y y-coordinate in <code>g</code>
272     */
273    public void paint(Graphics g, int x, int y) {
274        if (image == null)
275            return;
276        g.drawImage(image, x, y, null);
277    }
278
279    /**
280     * Paints the tile-image on the {@link Graphics} <code>g</code> at the
281     * position <code>x</code>/<code>y</code>.
282     *
283     * @param g the Graphics object
284     * @param x x-coordinate in <code>g</code>
285     * @param y y-coordinate in <code>g</code>
286     * @param width width that tile should have
287     * @param height height that tile should have
288     */
289    public void paint(Graphics g, int x, int y, int width, int height) {
290        if (image == null)
291            return;
292        g.drawImage(image, x, y, width, height, null);
293    }
294
295    @Override
296    public String toString() {
297        return "Tile " + key;
298    }
299
300    /**
301     * Note that the hash code does not include the {@link #source}.
302     * Therefore a hash based collection can only contain tiles
303     * of one {@link #source}.
304     */
305    @Override
306    public int hashCode() {
307        final int prime = 31;
308        int result = 1;
309        result = prime * result + xtile;
310        result = prime * result + ytile;
311        result = prime * result + zoom;
312        return result;
313    }
314
315    /**
316     * Compares this object with <code>obj</code> based on
317     * the fields {@link #xtile}, {@link #ytile} and
318     * {@link #zoom}.
319     * The {@link #source} field is ignored.
320     */
321    @Override
322    public boolean equals(Object obj) {
323        if (this == obj)
324            return true;
325        if (obj == null)
326            return false;
327        if (getClass() != obj.getClass())
328            return false;
329        Tile other = (Tile) obj;
330        if (xtile != other.xtile)
331            return false;
332        if (ytile != other.ytile)
333            return false;
334        if (zoom != other.zoom)
335            return false;
336        if (!getTileSource().equals(other.getTileSource())) {
337            return false;
338        }
339        return true;
340    }
341
342    public static String getTileKey(TileSource source, int xtile, int ytile, int zoom) {
343        return zoom + "/" + xtile + "/" + ytile + "@" + source.getName();
344    }
345
346    public String getStatus() {
347        if (this.error)
348            return "error";
349        if (this.loaded)
350            return "loaded";
351        if (this.loading)
352            return "loading";
353        return "new";
354    }
355
356    public boolean hasError() {
357        return error;
358    }
359
360    public String getErrorMessage() {
361        return error_message;
362    }
363
364    public void setError(Exception e) {
365        setError(e.toString());
366    }
367
368    public void setError(String message) {
369        error = true;
370        setImage(ERROR_IMAGE);
371        error_message = message;
372    }
373
374    /**
375     * Puts the given key/value pair to the metadata of the tile.
376     * If value is null, the (possibly existing) key/value pair is removed from
377     * the meta data.
378     *
379     * @param key Key
380     * @param value Value
381     */
382    public void putValue(String key, String value) {
383        if (value == null || value.isEmpty()) {
384            if (metadata != null) {
385                metadata.remove(key);
386            }
387            return;
388        }
389        if (metadata == null) {
390            metadata = new HashMap<>();
391        }
392        metadata.put(key, value);
393    }
394
395    /**
396     * returns the metadata of the Tile
397     *
398     * @param key metadata key that should be returned
399     * @return null if no such metadata exists, or the value of the metadata
400     */
401    public String getValue(String key) {
402        if (metadata == null) return null;
403        return metadata.get(key);
404    }
405
406    /**
407     *
408     * @return metadata of the tile
409     */
410    public Map<String, String> getMetadata() {
411        if (metadata == null) {
412            metadata = new HashMap<>();
413        }
414        return metadata;
415    }
416
417    /**
418     * indicate that loading process for this tile has started
419     */
420    public void initLoading() {
421        error = false;
422        loading = true;
423    }
424
425    /**
426     * indicate that loading process for this tile has ended
427     */
428    public void finishLoading() {
429        loading = false;
430        loaded = true;
431    }
432
433    /**
434     *
435     * @return TileSource from which this tile comes
436     */
437    public TileSource getTileSource() {
438        return source;
439    }
440
441    /**
442     * indicate that loading process for this tile has been canceled
443     */
444    public void loadingCanceled() {
445        loading = false;
446        loaded = false;
447    }
448
449}