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