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

Last change on this file since 11710 was 11237, checked in by bastiK, 7 years ago

applied #13933 - Simplify QuadBuckets and improve memory footprint (patch by Gerd Petermann, minor modifications)

  • Property svn:eol-style set to native
File size: 2.4 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 lon2x(double lon) {
52 long ret = (long) ((lon + 180.0) * WORLD_PARTS / 360.0);
53 if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
54 ret--;
55 }
56 return ret;
57 }
58
59 static long lat2y(double lat) {
60 long ret = (long) ((lat + 90.0) * WORLD_PARTS / 180.0);
61 if (Utils.equalsEpsilon(ret, WORLD_PARTS)) {
62 ret--;
63 }
64 return ret;
65 }
66
67 /**
68 * Returns quad tiling index for given coordinates and level.
69 *
70 * @param lat latitude
71 * @param lon longitude
72 * @param level level
73 *
74 * @return quad tiling index for given coordinates and level.
75 * @since 6171
76 */
77 public static byte index(final double lat, final double lon, final int level) {
78 long x = lon2x(lon);
79 long y = lat2y(lat);
80 int shift = NR_LEVELS-level-1;
81 return (byte) ((x >> shift & 1) * 2 + (y >> shift & 1));
82 }
83}
Note: See TracBrowser for help on using the repository browser.