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 lon2x(double lon) {
052        long ret = (long) ((lon + 180.0) * WORLD_PARTS / 360.0);
053        if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
054            ret--;
055        }
056        return ret;
057    }
058
059    static long lat2y(double lat) {
060        long ret = (long) ((lat + 90.0) * WORLD_PARTS / 180.0);
061        if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
062            ret--;
063        }
064        return ret;
065    }
066
067    /**
068     * Returns quad tiling index for given coordinates and level.
069     *
070     * @param lat latitude
071     * @param lon longitude
072     * @param level level
073     *
074     * @return quad tiling index for given coordinates and level.
075     * @since 6171
076     */
077    public static int index(final double lat, final double lon, final int level) {
078        long x = lon2x(lon);
079        long y = lat2y(lat);
080        int shift = NR_LEVELS-level-1;
081        return (int) ((x >> shift & 1) * 2 + (y >> shift & 1));
082    }
083}