[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[2165] | 2 | package org.openstreetmap.josm.data.coor;
|
---|
| 3 |
|
---|
[8384] | 4 | import org.openstreetmap.josm.tools.Utils;
|
---|
| 5 |
|
---|
[6362] | 6 | public final class QuadTiling {
|
---|
[7509] | 7 |
|
---|
[6360] | 8 | private QuadTiling() {
|
---|
| 9 | // Hide default constructor for utils classes
|
---|
| 10 | }
|
---|
[7509] | 11 |
|
---|
[4319] | 12 | public static final int NR_LEVELS = 24;
|
---|
[8345] | 13 | public static final double WORLD_PARTS = 1 << NR_LEVELS;
|
---|
[2165] | 14 |
|
---|
[4319] | 15 | public static final int TILES_PER_LEVEL_SHIFT = 2; // Has to be 2. Other parts of QuadBuckets code rely on it
|
---|
[8510] | 16 | public static final int TILES_PER_LEVEL = 1 << TILES_PER_LEVEL_SHIFT;
|
---|
[6171] | 17 | public static final int X_PARTS = 360;
|
---|
| 18 | public static final int X_BIAS = -180;
|
---|
[2165] | 19 |
|
---|
[6171] | 20 | public static final int Y_PARTS = 180;
|
---|
| 21 | public static final int Y_BIAS = -90;
|
---|
[2165] | 22 |
|
---|
[6171] | 23 | public static LatLon tile2LatLon(long quad) {
|
---|
[2165] | 24 | // The world is divided up into X_PARTS,Y_PARTS.
|
---|
[10001] | 25 | // The question is how far we move for each bit being set.
|
---|
| 26 | // In the case of the top level, we move half of the world.
|
---|
| 27 | double xUnit = X_PARTS/2;
|
---|
| 28 | double yUnit = Y_PARTS/2;
|
---|
[2165] | 29 | long shift = (NR_LEVELS*2)-2;
|
---|
| 30 |
|
---|
| 31 | double x = 0;
|
---|
| 32 | double y = 0;
|
---|
| 33 | for (int i = 0; i < NR_LEVELS; i++) {
|
---|
| 34 | long bits = (quad >> shift) & 0x3;
|
---|
| 35 | // remember x is the MSB
|
---|
[2422] | 36 | if ((bits & 0x2) != 0) {
|
---|
[10001] | 37 | x += xUnit;
|
---|
[2422] | 38 | }
|
---|
| 39 | if ((bits & 0x1) != 0) {
|
---|
[10001] | 40 | y += yUnit;
|
---|
[2422] | 41 | }
|
---|
[10001] | 42 | xUnit /= 2;
|
---|
| 43 | yUnit /= 2;
|
---|
[2165] | 44 | shift -= 2;
|
---|
| 45 | }
|
---|
| 46 | x += X_BIAS;
|
---|
| 47 | y += Y_BIAS;
|
---|
| 48 | return new LatLon(y, x);
|
---|
| 49 | }
|
---|
[7509] | 50 |
|
---|
[6171] | 51 | static long xy2tile(long x, long y) {
|
---|
[2422] | 52 | long tile = 0;
|
---|
| 53 | int i;
|
---|
[8395] | 54 | for (i = NR_LEVELS-1; i >= 0; i--) {
|
---|
[8345] | 55 | long xbit = (x >> i) & 1;
|
---|
| 56 | long ybit = (y >> i) & 1;
|
---|
[2165] | 57 | tile <<= 2;
|
---|
| 58 | // Note that x is the MSB
|
---|
[8510] | 59 | tile |= (xbit << 1) | ybit;
|
---|
[2422] | 60 | }
|
---|
| 61 | return tile;
|
---|
[2165] | 62 | }
|
---|
[7509] | 63 |
|
---|
[6171] | 64 | static long lon2x(double lon) {
|
---|
[8510] | 65 | long ret = (long) ((lon + 180.0) * WORLD_PARTS / 360.0);
|
---|
[8384] | 66 | if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
|
---|
[2422] | 67 | ret--;
|
---|
| 68 | }
|
---|
| 69 | return ret;
|
---|
[2165] | 70 | }
|
---|
[7509] | 71 |
|
---|
[6171] | 72 | static long lat2y(double lat) {
|
---|
[8510] | 73 | long ret = (long) ((lat + 90.0) * WORLD_PARTS / 180.0);
|
---|
[8384] | 74 | if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
|
---|
[2422] | 75 | ret--;
|
---|
| 76 | }
|
---|
| 77 | return ret;
|
---|
[2165] | 78 | }
|
---|
[7509] | 79 |
|
---|
[6171] | 80 | public static long quadTile(LatLon coor) {
|
---|
| 81 | return xy2tile(lon2x(coor.lon()), lat2y(coor.lat()));
|
---|
[2165] | 82 | }
|
---|
[7509] | 83 |
|
---|
[6171] | 84 | public static int index(int level, long quad) {
|
---|
[2165] | 85 | long mask = 0x00000003;
|
---|
[10001] | 86 | int totalShift = TILES_PER_LEVEL_SHIFT*(NR_LEVELS-level-1);
|
---|
| 87 | return (int) (mask & (quad >> totalShift));
|
---|
[2165] | 88 | }
|
---|
[7509] | 89 |
|
---|
[6171] | 90 | /**
|
---|
| 91 | * Returns quad tiling index for given coordinates and level.
|
---|
| 92 | *
|
---|
| 93 | * @param coor coordinates
|
---|
| 94 | * @param level level
|
---|
| 95 | *
|
---|
| 96 | * @return quad tiling index for given coordinates and level.
|
---|
| 97 | * @since 2263
|
---|
| 98 | */
|
---|
| 99 | public static int index(LatLon coor, int level) {
|
---|
[7509] | 100 | // The nodes that don't return coordinates will all get stuck in a single tile.
|
---|
[6171] | 101 | // Hopefully there are not too many of them
|
---|
[2165] | 102 | if (coor == null)
|
---|
| 103 | return 0;
|
---|
[3145] | 104 |
|
---|
[6171] | 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);
|
---|
[3145] | 121 | int shift = NR_LEVELS-level-1;
|
---|
[8510] | 122 | return (int) ((x >> shift & 1) * 2 + (y >> shift & 1));
|
---|
[2165] | 123 | }
|
---|
| 124 | }
|
---|