root/trunk/processing/sketches/modest_maps/Tiles.pde

Revision 489, 6.8 kB (checked in by tom, 6 months ago)

moving processing libs around

Line 
1
2 void doTilesTest() {
3   println();
4   println("tiles test");
5   println(); 
6
7   println( "1".equals(binary(1)) );
8   println( "10".equals(binary(2)) );
9   println( "11".equals(binary(3)) );
10   println( "100".equals(binary(4)) );
11
12   println( 1 == unbinary("1") );
13   println( 3 == unbinary("11") );
14   println( 5 == unbinary("101") );
15   println( 9 == unbinary("1001") );
16
17 /*
18 >>> fromGoogleRoad(0, 0, 16)
19    (0, 0, 1)
20    >>> fromGoogleRoad(10507, 25322, 1)
21    (10507, 25322, 16)
22    >>> fromGoogleRoad(10482, 25333, 1)
23    (10482, 25333, 16)
24    
25    >>> toGoogleRoad(0, 0, 1)
26    (0, 0, 16)
27    >>> toGoogleRoad(10507, 25322, 16)
28    (10507, 25322, 1)
29    >>> toGoogleRoad(10482, 25333, 16)
30    (10482, 25333, 1)
31    
32    >>> fromGoogleAerial('tq')
33    (0, 0, 1)
34    >>> fromGoogleAerial('tqtsqrqtrtttqsqsr')
35    (10507, 25322, 16)
36    >>> fromGoogleAerial('tqtsqrqtqssssqtrt')
37    (10482, 25333, 16)
38    
39    >>> toGoogleAerial(0, 0, 1)
40    'tq'
41    >>> toGoogleAerial(10507, 25322, 16)
42    'tqtsqrqtrtttqsqsr'
43    >>> toGoogleAerial(10482, 25333, 16)
44    'tqtsqrqtqssssqtrt'
45 */
46 /*   
47    >>> fromYahooRoad(0, 0, 17)
48    (0, 0, 1)
49    >>> fromYahooRoad(10507, 7445, 2)
50    (10507, 25322, 16)
51    >>> fromYahooRoad(10482, 7434, 2)
52    (10482, 25333, 16)
53    
54    >>> toYahooRoad(0, 0, 1)
55    (0, 0, 17)
56    >>> toYahooRoad(10507, 25322, 16)
57    (10507, 7445, 2)
58    >>> toYahooRoad(10482, 25333, 16)
59    (10482, 7434, 2)
60    
61    >>> fromYahooAerial(0, 0, 17)
62    (0, 0, 1)
63    >>> fromYahooAerial(10507, 7445, 2)
64    (10507, 25322, 16)
65    >>> fromYahooAerial(10482, 7434, 2)
66    (10482, 25333, 16)
67    
68    >>> toYahooAerial(0, 0, 1)
69    (0, 0, 17)
70    >>> toYahooAerial(10507, 25322, 16)
71    (10507, 7445, 2)
72    >>> toYahooAerial(10482, 25333, 16)
73    (10482, 7434, 2)
74    
75    */
76
77   Coordinate c = fromMicrosoftRoad("0");
78   println(c.column == 0.0 && c.row == 0.0 && c.zoom == 1.0);
79   Coordinate d = fromMicrosoftRoad("0230102122203031");
80   println(d.column == 25322.0 && d.row == 10507.0 && d.zoom == 16.0);
81   Coordinate e = fromMicrosoftRoad("0230102033330212");
82   println(e.column == 25333.0 && e.row == 10482.0 && e.zoom == 16.0);
83
84   println( "0".equals( toMicrosoftRoad(0, 0, 1) ) );
85   println( "0230102122203031".equals(toMicrosoftRoad(10507, 25322, 16) ) );
86   println( "0230102033330212".equals(toMicrosoftRoad(10482, 25333, 16) ) );
87
88   c = fromMicrosoftAerial("0");
89   println(c.column == 0.0 && c.row == 0.0 && c.zoom == 1.0);
90   d = fromMicrosoftAerial("0230102122203031");
91   println(d.column == 25322.0 && d.row == 10507.0 && d.zoom == 16.0);
92   e = fromMicrosoftAerial("0230102033330212");
93   println(e.column == 25333.0 && e.row == 10482.0 && e.zoom == 16.0);
94
95   println( "0".equals( toMicrosoftAerial(0, 0, 1) ) );
96   println( "0230102122203031".equals(toMicrosoftAerial(10507, 25322, 16) ) );
97   println( "0230102033330212".equals(toMicrosoftAerial(10482, 25333, 16) ) );
98
99 }
100
101 Coordinate fromGoogleRoad(Coordinate coord) {
102   // Return column, row, zoom for Google Road tile x, y, z.
103   return new Coordinate(coord.row, coord.column, 17 - coord.zoom);
104 }
105
106 Coordinate toGoogleRoad(Coordinate coord) {
107   // Return x, y, z for Google Road tile column, row, zoom.
108   return new Coordinate(coord.row, coord.column, 17 - coord.zoom);
109 }
110
111
112 //googleFromCorners = {'t' { '00', 's' { '01', 'q' { '10', 'r' { '11'}
113 //googleToCorners = {'00' { 't', '01' { 's', '10' { 'q', '11' { 'r'}
114
115 Coordinate fromGoogleAerial(String s) {
116   // Return column, row, zoom for Google Aerial tile string.
117   String rowS = "";
118   String colS = "";
119   for (int i = 0; i < s.length(); i++) {
120     switch (s.charAt(i)) {
121     case 't':
122       rowS += '0';
123       colS += '0';
124       break;
125     case 's':
126       rowS += '0';
127       colS += '1';
128       break;
129     case 'q':
130       rowS += '1';
131       colS += '0';
132       break;
133     case 'r':
134       rowS += '1';
135       colS += '1';
136       break;
137     }
138   }
139   int row = unbinary(rowS);
140   int col = unbinary(colS);
141   int zoom = s.length() - 1;
142   row = (int)pow(2, zoom) - row - 1;
143   return new Coordinate( row, col, zoom );
144 }
145
146 String toGoogleAerial(Coordinate coord) {
147   // Return string for Google Road tile column, row, zoom.
148   int x = (int)coord.column;
149   int y = (int)pow(2, coord.zoom) - (int)coord.row - 1;
150   int z = (int)coord.zoom + 1;
151   String yb = binary(y,z);
152   String xb = binary(x,z);
153   String string = "";
154   String googleToCorners = "tsqr";
155   for (int c = 0; c < z; c++) {
156     string += googleToCorners.charAt(unbinary("" + yb.charAt(c) + xb.charAt(c)));
157   }
158   return string;
159 }
160
161 /*
162 def fromYahoo(x, y, z) {
163  """ Return column, row, zoom for Yahoo x, y, z.
164  """
165  zoom = 18 - z
166  row = int(math.pow(2, zoom - 1) - y - 1)
167  col = x
168  return col, row, zoom
169  
170  def toYahoo(col, row, zoom) {
171  """ Return x, y, z for Yahoo tile column, row, zoom.
172  """
173  x = col
174  y = int(math.pow(2, zoom - 1) - row - 1)
175  z = 18 - zoom
176  return x, y, z
177  
178  def fromYahooRoad(x, y, z) {
179  """ Return column, row, zoom for Yahoo Road tile x, y, z.
180  """
181  return fromYahoo(x, y, z)
182  
183  def toYahooRoad(col, row, zoom) {
184  """ Return x, y, z for Yahoo Road tile column, row, zoom.
185  """
186  return toYahoo(col, row, zoom)
187  
188  def fromYahooAerial(x, y, z) {
189  """ Return column, row, zoom for Yahoo Aerial tile x, y, z.
190  """
191  return fromYahoo(x, y, z)
192  
193  def toYahooAerial(col, row, zoom) {
194  """ Return x, y, z for Yahoo Aerial tile column, row, zoom.
195  """
196  return toYahoo(col, row, zoom)
197  
198  */
199
200
201 //microsoftFromCorners = {'0' { '00', '1' { '01', '2' { '10', '3' { '11'}
202 //microsoftToCorners = {'00' { '0', '01' { '1', '10' { '2', '11' { '3'}
203
204
205 Coordinate fromMicrosoft(String s) {
206   // Return column, row, zoom for Microsoft tile string.
207   String rowS = "";
208   String colS = "";
209   for (int i = 0; i < s.length(); i++) {
210     int v = parseInt("" + s.charAt(i));
211     String bv = binary(v,2);
212     rowS += bv.charAt(0);
213     colS += bv.charAt(1);
214   }
215   return new Coordinate(unbinary(colS), unbinary(rowS), s.length());
216 }
217
218
219 String toMicrosoft(int col, int row, int zoom) {
220   // Return string for Microsoft tile column, row, zoom
221   String y = binary(row, zoom);
222   String x = binary(col, zoom);
223   String out = "";
224   for (int i = 0; i < zoom; i++) {
225     out += unbinary("" + y.charAt(i) + x.charAt(i));
226   }
227   return out;
228 }
229
230
231 Coordinate fromMicrosoftRoad(String s) {
232   // Return column, row, zoom for Microsoft Road tile string.
233   return fromMicrosoft(s);
234 }
235
236 String toMicrosoftRoad(int col, int row, int zoom) {
237   // Return x, y, z for Microsoft Road tile column, row, zoom.
238   return toMicrosoft(col, row, zoom);
239 }
240
241
242 Coordinate fromMicrosoftAerial(String s) {
243   // Return column, row, zoom for Microsoft Aerial tile string.
244   return fromMicrosoft(s);
245 }
246
247 String toMicrosoftAerial(int col, int row, int zoom) {
248   // Return x, y, z for Microsoft Aerial tile column, row, zoom.
249   return toMicrosoft(col, row, zoom);
250 }
Note: See TracBrowser for help on using the browser.