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_ARGB);
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    /**
231     * @return tile indexes of the top left corner as TileXY object
232     */
233    public TileXY getTileXY() {
234        return new TileXY(xtile, ytile);
235    }
236
237    public BufferedImage getImage() {
238        return image;
239    }
240
241    public void setImage(BufferedImage image) {
242        this.image = image;
243    }
244
245    public void loadImage(InputStream input) throws IOException {
246        image = ImageIO.read(input);
247    }
248
249    /**
250     * @return key that identifies a tile
251     */
252    public String getKey() {
253        return key;
254    }
255
256    public boolean isLoaded() {
257        return loaded;
258    }
259
260    public boolean isLoading() {
261        return loading;
262    }
263
264    public void setLoaded(boolean loaded) {
265        this.loaded = loaded;
266    }
267
268    public String getUrl() throws IOException {
269        return source.getTileUrl(zoom, xtile, ytile);
270    }
271
272    /**
273     * Paints the tile-image on the {@link Graphics} <code>g</code> at the
274     * position <code>x</code>/<code>y</code>.
275     *
276     * @param g the Graphics object
277     * @param x x-coordinate in <code>g</code>
278     * @param y y-coordinate in <code>g</code>
279     */
280    public void paint(Graphics g, int x, int y) {
281        if (image == null)
282            return;
283        g.drawImage(image, x, y, null);
284    }
285
286    /**
287     * Paints the tile-image on the {@link Graphics} <code>g</code> at the
288     * position <code>x</code>/<code>y</code>.
289     *
290     * @param g the Graphics object
291     * @param x x-coordinate in <code>g</code>
292     * @param y y-coordinate in <code>g</code>
293     * @param width width that tile should have
294     * @param height height that tile should have
295     */
296    public void paint(Graphics g, int x, int y, int width, int height) {
297        if (image == null)
298            return;
299        g.drawImage(image, x, y, width, height, null);
300    }
301
302    @Override
303    public String toString() {
304        return "Tile " + key;
305    }
306
307    /**
308     * Note that the hash code does not include the {@link #source}.
309     * Therefore a hash based collection can only contain tiles
310     * of one {@link #source}.
311     */
312    @Override
313    public int hashCode() {
314        final int prime = 31;
315        int result = 1;
316        result = prime * result + xtile;
317        result = prime * result + ytile;
318        result = prime * result + zoom;
319        return result;
320    }
321
322    /**
323     * Compares this object with <code>obj</code> based on
324     * the fields {@link #xtile}, {@link #ytile} and
325     * {@link #zoom}.
326     * The {@link #source} field is ignored.
327     */
328    @Override
329    public boolean equals(Object obj) {
330        if (this == obj)
331            return true;
332        if (obj == null)
333            return false;
334        if (getClass() != obj.getClass())
335            return false;
336        Tile other = (Tile) obj;
337        if (xtile != other.xtile)
338            return false;
339        if (ytile != other.ytile)
340            return false;
341        if (zoom != other.zoom)
342            return false;
343        if (!getTileSource().equals(other.getTileSource())) {
344            return false;
345        }
346        return true;
347    }
348
349    public static String getTileKey(TileSource source, int xtile, int ytile, int zoom) {
350        return zoom + "/" + xtile + "/" + ytile + "@" + source.getName();
351    }
352
353    public String getStatus() {
354        if (this.error)
355            return "error";
356        if (this.loaded)
357            return "loaded";
358        if (this.loading)
359            return "loading";
360        return "new";
361    }
362
363    public boolean hasError() {
364        return error;
365    }
366
367    public String getErrorMessage() {
368        return error_message;
369    }
370
371    public void setError(Exception e) {
372        setError(e.toString());
373    }
374
375    public void setError(String message) {
376        error = true;
377        setImage(ERROR_IMAGE);
378        error_message = message;
379    }
380
381    /**
382     * Puts the given key/value pair to the metadata of the tile.
383     * If value is null, the (possibly existing) key/value pair is removed from
384     * the meta data.
385     *
386     * @param key Key
387     * @param value Value
388     */
389    public void putValue(String key, String value) {
390        if (value == null || value.isEmpty()) {
391            if (metadata != null) {
392                metadata.remove(key);
393            }
394            return;
395        }
396        if (metadata == null) {
397            metadata = new HashMap<>();
398        }
399        metadata.put(key, value);
400    }
401
402    /**
403     * returns the metadata of the Tile
404     *
405     * @param key metadata key that should be returned
406     * @return null if no such metadata exists, or the value of the metadata
407     */
408    public String getValue(String key) {
409        if (metadata == null) return null;
410        return metadata.get(key);
411    }
412
413    /**
414     *
415     * @return metadata of the tile
416     */
417    public Map<String, String> getMetadata() {
418        if (metadata == null) {
419            metadata = new HashMap<>();
420        }
421        return metadata;
422    }
423
424    /**
425     * indicate that loading process for this tile has started
426     */
427    public void initLoading() {
428        error = false;
429        loading = true;
430    }
431
432    /**
433     * indicate that loading process for this tile has ended
434     */
435    public void finishLoading() {
436        loading = false;
437        loaded = true;
438    }
439
440    /**
441     *
442     * @return TileSource from which this tile comes
443     */
444    public TileSource getTileSource() {
445        return source;
446    }
447
448    /**
449     * indicate that loading process for this tile has been canceled
450     */
451    public void loadingCanceled() {
452        loading = false;
453        loaded = false;
454    }
455
456}