1 | // License: GPL. Copyright 2009 by Dave Hansen, others
|
---|
2 | package org.openstreetmap.josm.data.coor;
|
---|
3 |
|
---|
4 | public 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 | //return Math.round((lon + 180.0) * QuadBuckets.WORLD_PARTS / 360.0)-1;
|
---|
66 | long ret = (long)((lon + 180.0) * WORLD_PARTS / 360.0);
|
---|
67 | if (ret == WORLD_PARTS) {
|
---|
68 | ret--;
|
---|
69 | }
|
---|
70 | return ret;
|
---|
71 | }
|
---|
72 |
|
---|
73 | static long lat2y(double lat) {
|
---|
74 | //return Math.round((lat + 90.0) * QuadBuckets.WORLD_PARTS / 180.0)-1;
|
---|
75 | long ret = (long)((lat + 90.0) * WORLD_PARTS / 180.0);
|
---|
76 | if (ret == WORLD_PARTS) {
|
---|
77 | ret--;
|
---|
78 | }
|
---|
79 | return ret;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static long quadTile(LatLon coor) {
|
---|
83 | return xy2tile(lon2x(coor.lon()), lat2y(coor.lat()));
|
---|
84 | }
|
---|
85 |
|
---|
86 | public static int index(int level, long quad) {
|
---|
87 | long mask = 0x00000003;
|
---|
88 | int total_shift = TILES_PER_LEVEL_SHIFT*(NR_LEVELS-level-1);
|
---|
89 | return (int)(mask & (quad >> total_shift));
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Returns quad tiling index for given coordinates and level.
|
---|
94 | *
|
---|
95 | * @param coor coordinates
|
---|
96 | * @param level level
|
---|
97 | *
|
---|
98 | * @return quad tiling index for given coordinates and level.
|
---|
99 | * @since 2263
|
---|
100 | */
|
---|
101 | public static int index(LatLon coor, int level) {
|
---|
102 | // The nodes that don't return coordinates will all get stuck in a single tile.
|
---|
103 | // Hopefully there are not too many of them
|
---|
104 | if (coor == null)
|
---|
105 | return 0;
|
---|
106 |
|
---|
107 | return index(coor.lat(), coor.lon(), level);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Returns quad tiling index for given coordinates and level.
|
---|
112 | *
|
---|
113 | * @param lat latitude
|
---|
114 | * @param lon longitude
|
---|
115 | * @param level level
|
---|
116 | *
|
---|
117 | * @return quad tiling index for given coordinates and level.
|
---|
118 | * @since 6171
|
---|
119 | */
|
---|
120 | public static int index(final double lat, final double lon, final int level) {
|
---|
121 | long x = lon2x(lon);
|
---|
122 | long y = lat2y(lat);
|
---|
123 | int shift = NR_LEVELS-level-1;
|
---|
124 | return (int)((x >> shift & 1) * 2 + (y >> shift & 1));
|
---|
125 | }
|
---|
126 | }
|
---|