001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.coor;
003
004import org.openstreetmap.josm.tools.Utils;
005
006public final class QuadTiling {
007
008    private QuadTiling() {
009        // Hide default constructor for utils classes
010    }
011
012    public static final int NR_LEVELS = 24;
013    public static final double WORLD_PARTS = 1 << NR_LEVELS;
014
015    public static final int TILES_PER_LEVEL_SHIFT = 2; // Has to be 2. Other parts of QuadBuckets code rely on it
016    public static final int TILES_PER_LEVEL = 1 << TILES_PER_LEVEL_SHIFT;
017    public static final int X_PARTS = 360;
018    public static final int X_BIAS = -180;
019
020    public static final int Y_PARTS = 180;
021    public static final int Y_BIAS = -90;
022
023    public static LatLon tile2LatLon(long quad) {
024        // The world is divided up into X_PARTS,Y_PARTS.
025        // The question is how far we move for each bit being set.
026        // In the case of the top level, we move half of the world.
027        double xUnit = X_PARTS/2d;
028        double yUnit = Y_PARTS/2d;
029        long shift = (NR_LEVELS*2L)-2L;
030
031        double x = 0;
032        double y = 0;
033        for (int i = 0; i < NR_LEVELS; i++) {
034            long bits = (quad >> shift) & 0x3;
035            // remember x is the MSB
036            if ((bits & 0x2) != 0) {
037                x += xUnit;
038            }
039            if ((bits & 0x1) != 0) {
040                y += yUnit;
041            }
042            xUnit /= 2;
043            yUnit /= 2;
044            shift -= 2;
045        }
046        x += X_BIAS;
047        y += Y_BIAS;
048        return new LatLon(y, x);
049    }
050
051    static long xy2tile(long x, long y) {
052        long tile = 0;
053        int i;
054        for (i = NR_LEVELS-1; i >= 0; i--) {
055            long xbit = (x >> i) & 1;
056            long ybit = (y >> i) & 1;
057            tile <<= 2;
058            // Note that x is the MSB
059            tile |= (xbit << 1) | ybit;
060        }
061        return tile;
062    }
063
064    static long lon2x(double lon) {
065        long ret = (long) ((lon + 180.0) * WORLD_PARTS / 360.0);
066        if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
067            ret--;
068        }
069        return ret;
070    }
071
072    static long lat2y(double lat) {
073        long ret = (long) ((lat + 90.0) * WORLD_PARTS / 180.0);
074        if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
075            ret--;
076        }
077        return ret;
078    }
079
080    public static long quadTile(LatLon coor) {
081        return xy2tile(lon2x(coor.lon()), lat2y(coor.lat()));
082    }
083
084    public static int index(int level, long quad) {
085        long mask = 0x00000003;
086        int totalShift = TILES_PER_LEVEL_SHIFT*(NR_LEVELS-level-1);
087        return (int) (mask & (quad >> totalShift));
088    }
089
090    /**
091     * Returns quad tiling index for given coordinates and level.
092     *
093     * @param coor coordinates
094     * @param level level
095     *
096     * @return quad tiling index for given coordinates and level.
097     * @since 2263
098     */
099    public static int index(LatLon coor, int level) {
100        // The nodes that don't return coordinates will all get stuck in a single tile.
101        // Hopefully there are not too many of them
102        if (coor == null)
103            return 0;
104
105        return index(coor.lat(), coor.lon(), level);
106    }
107
108    /**
109     * Returns quad tiling index for given coordinates and level.
110     *
111     * @param lat latitude
112     * @param lon longitude
113     * @param level level
114     *
115     * @return quad tiling index for given coordinates and level.
116     * @since 6171
117     */
118    public static int index(final double lat, final double lon, final int level) {
119        long x = lon2x(lon);
120        long y = lat2y(lat);
121        int shift = NR_LEVELS-level-1;
122        return (int) ((x >> shift & 1) * 2 + (y >> shift & 1));
123    }
124}