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

Last change on this file since 6296 was 6296, checked in by Don-vip, 11 years ago

Sonar/Findbugs - Avoid commented-out lines of code, javadoc

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. Copyright 2009 by Dave Hansen, others
2package org.openstreetmap.josm.data.coor;
3
4public class QuadTiling
5{
6 public static final int NR_LEVELS = 24;
7 public static final double WORLD_PARTS = (1 << NR_LEVELS);
8
9 public static final int TILES_PER_LEVEL_SHIFT = 2; // Has to be 2. Other parts of QuadBuckets code rely on it
10 public static final int TILES_PER_LEVEL = 1<<TILES_PER_LEVEL_SHIFT;
11 public static final int X_PARTS = 360;
12 public static final int X_BIAS = -180;
13
14 public static final int Y_PARTS = 180;
15 public static final int Y_BIAS = -90;
16
17 public static LatLon tile2LatLon(long quad) {
18 // The world is divided up into X_PARTS,Y_PARTS.
19 // The question is how far we move for each bit
20 // being set. In the case of the top level, we
21 // move half of the world.
22 double x_unit = X_PARTS/2;
23 double y_unit = Y_PARTS/2;
24 long shift = (NR_LEVELS*2)-2;
25
26 double x = 0;
27 double y = 0;
28 for (int i = 0; i < NR_LEVELS; i++) {
29 long bits = (quad >> shift) & 0x3;
30 // remember x is the MSB
31 if ((bits & 0x2) != 0) {
32 x += x_unit;
33 }
34 if ((bits & 0x1) != 0) {
35 y += y_unit;
36 }
37 x_unit /= 2;
38 y_unit /= 2;
39 shift -= 2;
40 }
41 x += X_BIAS;
42 y += Y_BIAS;
43 return new LatLon(y, x);
44 }
45
46 static long xy2tile(long x, long y) {
47 long tile = 0;
48 int i;
49 for (i = NR_LEVELS-1; i >= 0; i--)
50 {
51 long xbit = ((x >> i) & 1);
52 long ybit = ((y >> i) & 1);
53 tile <<= 2;
54 // Note that x is the MSB
55 tile |= (xbit<<1) | ybit;
56 }
57 return tile;
58 }
59
60 static long coorToTile(LatLon coor) {
61 return quadTile(coor);
62 }
63
64 static long lon2x(double lon) {
65 long ret = (long)((lon + 180.0) * WORLD_PARTS / 360.0);
66 if (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 (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 total_shift = TILES_PER_LEVEL_SHIFT*(NR_LEVELS-level-1);
87 return (int)(mask & (quad >> total_shift));
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.