001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm.visitor.paint;
003
004import java.util.stream.IntStream;
005import java.util.stream.Stream;
006
007import org.openstreetmap.josm.data.Bounds;
008import org.openstreetmap.josm.data.coor.ILatLon;
009
010/**
011 * A record used for storing tile information for painting.
012 * The origin is upper-left, not lower-left (so more like Google tile coordinates than TMS tile coordinates).
013 * @since 19176
014 */
015public final class TileZXY implements ILatLon {
016    private final int zoom;
017    private final int x;
018    private final int y;
019
020    /**
021     * Create a new {@link TileZXY} object
022     * @param zoom The zoom for which this tile was created
023     * @param x The x coordinate at the specified zoom level
024     * @param y The y coordinate at the specified zoom level
025     */
026    public TileZXY(int zoom, int x, int y) {
027        this.zoom = zoom;
028        this.x = x;
029        this.y = y;
030    }
031
032    /**
033     * Get the zoom level
034     * @return The zoom level for which this tile was created
035     */
036    public int zoom() {
037        return this.zoom;
038    }
039
040    /**
041     * Get the x coordinate
042     * @return The x coordinate for this tile
043     */
044    @SuppressWarnings("PMD.ShortMethodName")
045    public int x() {
046        return this.x;
047    }
048
049    /**
050     * Get the y coordinate
051     * @return The y coordinate for this tile
052     */
053    @SuppressWarnings("PMD.ShortMethodName")
054    public int y() {
055        return this.y;
056    }
057
058    /**
059     * Get the latitude for upper-left corner of this tile
060     * @return The latitude
061     */
062    @Override
063    public double lat() {
064        return yToLat(this.y(), this.zoom());
065    }
066
067    /**
068     * Get the longitude for the upper-left corner of this tile
069     * @return The longitude
070     */
071    @Override
072    public double lon() {
073        return xToLon(this.x(), this.zoom());
074    }
075
076    /**
077     * Convert a bounds to a series of tiles that entirely cover the bounds
078     * @param minLat The minimum latitude
079     * @param minLon The minimum longitude
080     * @param maxLat The maximum latitude
081     * @param maxLon The maximum longitude
082     * @param zoom The zoom level to generate the tiles for
083     * @return The stream of tiles
084     */
085    public static Stream<TileZXY> boundsToTiles(double minLat, double minLon, double maxLat, double maxLon, int zoom) {
086        return boundsToTiles(minLat, minLon, maxLat, maxLon, zoom, 0);
087    }
088
089    /**
090     * Convert a bounds to a series of tiles that entirely cover the bounds
091     * @param minLat The minimum latitude
092     * @param minLon The minimum longitude
093     * @param maxLat The maximum latitude
094     * @param maxLon The maximum longitude
095     * @param zoom The zoom level to generate the tiles for
096     * @param expansion The number of tiles to expand on the x/y axis (1 row north, 1 row south, 1 column left, 1 column right)
097     * @return The stream of tiles
098     */
099    public static Stream<TileZXY> boundsToTiles(double minLat, double minLon, double maxLat, double maxLon, int zoom, int expansion) {
100        final TileZXY upperRight = latLonToTile(maxLat, maxLon, zoom);
101        final TileZXY lowerLeft = latLonToTile(minLat, minLon, zoom);
102        return IntStream.rangeClosed(lowerLeft.x() - expansion, upperRight.x() + expansion)
103                .mapToObj(x -> IntStream.rangeClosed(upperRight.y() - expansion, lowerLeft.y() + expansion)
104                        .mapToObj(y -> new TileZXY(zoom, x, y)))
105                .flatMap(stream -> stream);
106    }
107
108    /**
109     * Convert a tile to the bounds for that tile
110     * @param tile The tile to get the bounds for
111     * @return The bounds
112     */
113    public static Bounds tileToBounds(TileZXY tile) {
114        return new Bounds(yToLat(tile.y() + 1, tile.zoom()), xToLon(tile.x(), tile.zoom()),
115                yToLat(tile.y(), tile.zoom()), xToLon(tile.x() + 1, tile.zoom()));
116    }
117
118    /**
119     * Convert a x tile coordinate to a latitude
120     * @param x The x coordinate
121     * @param zoom The zoom level to use for the calculation
122     * @return The latitude for the x coordinate (upper-left of the tile)
123     */
124    public static double xToLon(int x, int zoom) {
125        return (x / Math.pow(2, zoom)) * 360 - 180;
126    }
127
128    /**
129     * Convert a y tile coordinate to a latitude
130     * @param y The y coordinate
131     * @param zoom The zoom level to use for the calculation
132     * @return The latitude for the y coordinate (upper-left of the tile)
133     */
134    public static double yToLat(int y, int zoom) {
135        double t = Math.PI - (2 * Math.PI * y) / Math.pow(2, zoom);
136        return 180 / Math.PI * Math.atan((Math.exp(t) - Math.exp(-t)) / 2);
137    }
138
139    /**
140     * Convert a lat, lon, and zoom to a tile coordiante
141     * @param lat The latitude
142     * @param lon The longitude
143     * @param zoom The zoom level
144     * @return The specified tile coordinates at the specified zoom
145     */
146    public static TileZXY latLonToTile(double lat, double lon, int zoom) {
147        final double zoom2 = Math.pow(2, zoom);
148        final double latLog = Math.log(Math.tan(Math.toRadians(lat)) + 1 / Math.cos(Math.toRadians(lat)));
149        final int xCoord = (int) Math.floor(zoom2 * (180 + lon) / 360);
150        final int yCoord = (int) Math.floor(zoom2 * (1 - latLog / Math.PI) / 2);
151        return new TileZXY(zoom, xCoord, yCoord);
152    }
153
154    @Override
155    public String toString() {
156        return "TileZXY{" + zoom + "/" + x + "/" + y + "}";
157    }
158
159    @Override
160    public int hashCode() {
161        // We only care about comparing zoom, x, and y
162        return Integer.hashCode(this.zoom) + 31 * (Integer.hashCode(this.x) + 31 * Integer.hashCode(this.y));
163    }
164
165    @Override
166    public boolean equals(Object obj) {
167        if (obj instanceof TileZXY) {
168            TileZXY o = (TileZXY) obj;
169            return this.zoom == o.zoom && this.x == o.x && this.y == o.y;
170        }
171        return false;
172    }
173}