source: josm/trunk/src/org/openstreetmap/josm/data/coor/QuadTiling.java@ 10747

Last change on this file since 10747 was 10181, checked in by Don-vip, 8 years ago

sonar - squid:S2184 - Math operands should be cast before assignment

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.coor;
3
4import org.openstreetmap.josm.tools.Utils;
5
6public 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 being set.
26 // In the case of the top level, we move half of the world.
27 double xUnit = X_PARTS/2d;
28 double yUnit = Y_PARTS/2d;
29 long shift = (NR_LEVELS*2L)-2L;
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
36 if ((bits & 0x2) != 0) {
37 x += xUnit;
38 }
39 if ((bits & 0x1) != 0) {
40 y += yUnit;
41 }
42 xUnit /= 2;
43 yUnit /= 2;
44 shift -= 2;
45 }
46 x += X_BIAS;
47 y += Y_BIAS;
48 return new LatLon(y, x);
49 }
50
51 static long xy2tile(long x, long y) {
52 long tile = 0;
53 int i;
54 for (i = NR_LEVELS-1; i >= 0; i--) {
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 lon2x(double lon) {
65 long ret = (long) ((lon + 180.0) * WORLD_PARTS / 360.0);
66 if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
67 ret--;
68 }
69 return ret;
70 }
71
72 static long lat2y(double lat) {
73 long ret = (long) ((lat + 90.0) * WORLD_PARTS / 180.0);
74 if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
75 ret--;
76 }
77 return ret;
78 }
79
80 public static long quadTile(LatLon coor) {
81 return xy2tile(lon2x(coor.lon()), lat2y(coor.lat()));
82 }
83
84 public static int index(int level, long quad) {
85 long mask = 0x00000003;
86 int totalShift = TILES_PER_LEVEL_SHIFT*(NR_LEVELS-level-1);
87 return (int) (mask & (quad >> totalShift));
88 }
89
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) {
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}
Note: See TracBrowser for help on using the repository browser.