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

Last change on this file since 13494 was 12375, checked in by michael2402, 7 years ago

Document the data.coor package

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.coor;
3
4import org.openstreetmap.josm.tools.Utils;
5
6/**
7 * This class helps in tiling the world into multiple quad tiles.
8 */
9public final class QuadTiling {
10
11 private QuadTiling() {
12 // Hide default constructor for utils classes
13 }
14
15 /**
16 * The maximum number of levels to split the quads
17 */
18 public static final int NR_LEVELS = 24;
19 /**
20 * The number of parts the world is split into in each direction
21 */
22 public static final double WORLD_PARTS = 1 << NR_LEVELS;
23
24 /**
25 * The log(2) of how many tiles there are per level
26 */
27 public static final int TILES_PER_LEVEL_SHIFT = 2; // Has to be 2. Other parts of QuadBuckets code rely on it
28 /**
29 * How many tiles there are per level
30 */
31 public static final int TILES_PER_LEVEL = 1 << TILES_PER_LEVEL_SHIFT;
32 /**
33 * The size of the world in X direction
34 */
35 public static final int X_PARTS = 360;
36 /**
37 * The offset of the world in x direction
38 */
39 public static final int X_BIAS = -180;
40
41 /**
42 * The size of the world in y direction
43 */
44 public static final int Y_PARTS = 180;
45 /**
46 * The offset of the world in y direction
47 */
48 public static final int Y_BIAS = -90;
49
50 /**
51 * Converts a tile index to a lat/lon position
52 * @param quad The tile to convert
53 * @return The lat/lon position of that tile
54 */
55 public static LatLon tile2LatLon(long quad) {
56 // The world is divided up into X_PARTS,Y_PARTS.
57 // The question is how far we move for each bit being set.
58 // In the case of the top level, we move half of the world.
59 double xUnit = X_PARTS/2d;
60 double yUnit = Y_PARTS/2d;
61 long shift = (NR_LEVELS*2L)-2L;
62
63 double x = 0;
64 double y = 0;
65 for (int i = 0; i < NR_LEVELS; i++) {
66 long bits = (quad >> shift) & 0x3;
67 // remember x is the MSB
68 if ((bits & 0x2) != 0) {
69 x += xUnit;
70 }
71 if ((bits & 0x1) != 0) {
72 y += yUnit;
73 }
74 xUnit /= 2;
75 yUnit /= 2;
76 shift -= 2;
77 }
78 x += X_BIAS;
79 y += Y_BIAS;
80 return new LatLon(y, x);
81 }
82
83 static long lon2x(double lon) {
84 long ret = (long) ((lon + 180.0) * WORLD_PARTS / 360.0);
85 if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
86 ret--;
87 }
88 return ret;
89 }
90
91 static long lat2y(double lat) {
92 long ret = (long) ((lat + 90.0) * WORLD_PARTS / 180.0);
93 if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
94 ret--;
95 }
96 return ret;
97 }
98
99 /**
100 * Returns quad tiling index for given coordinates and level.
101 *
102 * @param lat latitude
103 * @param lon longitude
104 * @param level level
105 *
106 * @return quad tiling index for given coordinates and level.
107 * @since 6171
108 */
109 public static byte index(final double lat, final double lon, final int level) {
110 long x = lon2x(lon);
111 long y = lat2y(lat);
112 int shift = NR_LEVELS-level-1;
113 return (byte) ((x >> shift & 1) * 2 + (y >> shift & 1));
114 }
115}
Note: See TracBrowser for help on using the repository browser.