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