Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / driver / raster / google / satellite / GoogleMapsTile.java @ 21606

History | View | Annotate | Download (6.21 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   http://www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 *
43
 *    or
44
 *
45
 *   Instituto de Rob?tica
46
 *   Apartado de correos 2085
47
 *   46071 Valencia
48
 *   (Spain)
49
 *   
50
 *   +34 963 543 577
51
 *   jjordan@robotica.uv.es
52
 *   http://robotica.uv.es
53
 *   
54
 */
55

    
56
package es.prodevelop.gvsig.mobile.fmap.driver.raster.google.satellite;
57

    
58
import java.awt.geom.Rectangle2D;
59
import java.net.MalformedURLException;
60
import java.net.URL;
61
import java.util.Random;
62

    
63
import org.apache.log4j.Logger;
64

    
65
public class GoogleMapsTile implements Cloneable {
66
        
67
        private static Logger logger = Logger.getLogger(GoogleMapsTile.class);
68
        
69
        private Rectangle2D extent;
70
        private int[] ppath;
71
        private Random rnd;
72
        
73
        public int[] getPath() {
74
                return ppath;
75
        }
76

    
77
        public GoogleMapsTile() {
78
                extent = new Rectangle2D.Double(
79
                                - GoogleUtils.SEMI_EQUATOR_LENGTH,
80
                                - GoogleUtils.SEMI_EQUATOR_LENGTH,
81
                                2 * GoogleUtils.SEMI_EQUATOR_LENGTH,
82
                                2 * GoogleUtils.SEMI_EQUATOR_LENGTH);
83
                ppath = new int[1];
84
                ppath[0] = 0;
85
                rnd = new Random(System.currentTimeMillis());
86
        }
87
        
88
        public URL getURL() {
89
                
90
                String u = "";
91
                String amp = "&";
92
                u = "http://kh"
93
                        + "1" // rnd.nextInt(4)
94
                        + ".google.com/kh?n=404"
95
                        + amp + "v=23"
96
                        + amp + "t=";
97
                
98
                for (int i=0; i<ppath.length; i++) u = u + letter(ppath[i]);
99
                
100
                if (GoogleUtils.USING_PROXY) {
101
                        String b64 = Base64.encodeBytes(u.getBytes());
102
                        u = "http://noipsearch.com/index.php?q=" + b64;
103
                }
104
                
105
                
106
                URL resp = null;
107
                try {
108
                        resp = new URL(u);
109
                } catch (MalformedURLException e) {
110
                        logger.error("Bad url: " + u);
111
                }
112
                return resp;
113
        }
114
        
115
        private String letter(int i) {
116
                
117
                if (i == 0) return "t";
118
                if (i == 1) return "s";
119
                if (i == 2) return "r";
120
                if (i == 3) return "q";
121
                return "";
122
        }
123

    
124
        public void goToQ(int q) {
125
                
126
                if ((q < 0) || (q > 3)) return;
127
                
128
                int[] aux = new int[ppath.length + 1];
129
                for (int i=0; i<ppath.length; i++) aux[i] = ppath[i];
130
                aux[ppath.length] = q;
131
                ppath = new int[aux.length];
132
                for (int i=0; i<aux.length; i++) ppath[i] = aux[i];
133
                
134
                double new_x = extent.getMinX();
135
                double new_y = extent.getMinY();
136
                double new_w = 0.5 * extent.getWidth();
137
                if (q == 1) { new_x = extent.getCenterX(); }
138
                if (q == 2) { new_x = extent.getCenterX(); new_y = extent.getCenterY(); }
139
                if (q == 3) { new_y = extent.getCenterY(); }
140
                extent = new Rectangle2D.Double(new_x, new_y, new_w, new_w);
141
                
142
        }
143

    
144
        public GoogleMapsTile(int[] _path) {
145
                
146
                extent = new Rectangle2D.Double(
147
                                - GoogleUtils.SEMI_EQUATOR_LENGTH,
148
                                - GoogleUtils.SEMI_EQUATOR_LENGTH,
149
                                2 * GoogleUtils.SEMI_EQUATOR_LENGTH,
150
                                2 * GoogleUtils.SEMI_EQUATOR_LENGTH);
151
                
152
                ppath = new int[_path.length];
153
                
154
                for (int i=0; i<_path.length; i++) {
155
                        
156
                        int q = _path[i];
157
                        if ((q < 0) || (q > 3)) break;
158
                        
159
                        ppath[i] = q;
160
                        
161
                        if (i == 0) continue;
162
                        
163
                        double new_x = extent.getMinX();
164
                        double new_y = extent.getMinY();
165
                        double new_w = 0.5 * extent.getWidth();
166
                        if (q == 1) { new_x = extent.getCenterX(); }
167
                        if (q == 2) { new_x = extent.getCenterX(); new_y = extent.getCenterY(); }
168
                        if (q == 3) { new_y = extent.getCenterY(); }
169
                        extent = new Rectangle2D.Double(new_x, new_y, new_w, new_w);
170
                }
171
                rnd = new Random(System.currentTimeMillis());
172
                
173
        }
174

    
175
        public Rectangle2D getExtent() {
176
                return extent;
177
        }
178

    
179
        // ------------- movements
180
        
181
        public Object clone() {
182
                return new GoogleMapsTile(ppath);
183
        }
184
        
185
        public GoogleMapsTile getParent() {
186
                
187
                if (ppath.length == 1) return this;
188
                int[] new_path = new int[ppath.length - 1];
189
                for (int i=0; i<(ppath.length - 1); i++) new_path[i] = ppath[i];
190
                return new GoogleMapsTile(new_path);
191
        }
192
        
193

    
194
        public GoogleMapsTile getTileToTheRight() {
195
                
196
                if (ppath.length == 1) return this;
197
                int lastq = ppath[ppath.length - 1];
198
                
199
                if (lastq == 0) {
200
                        GoogleMapsTile parent = getParent();
201
                        parent.goToQ(1);
202
                        return parent;
203
                }
204
                
205
                if (lastq == 3) {
206
                        GoogleMapsTile parent = getParent();
207
                        parent.goToQ(2);
208
                        return parent;
209
                }
210
                
211
                if (lastq == 1) {
212
                        GoogleMapsTile parent = getParent();
213
                        GoogleMapsTile parent_right = parent.getTileToTheRight();
214
                        parent_right.goToQ(0);
215
                        return parent_right;
216
                }
217
                
218
                if (lastq == 2) {
219
                        GoogleMapsTile parent = getParent();
220
                        GoogleMapsTile parent_right = parent.getTileToTheRight();
221
                        parent_right.goToQ(3);
222
                        return parent_right;
223
                }
224
                
225
                logger.error("Bad path item: " + lastq);
226
                return this;
227
        }
228
        
229
        public GoogleMapsTile getTileUnder() {
230
                
231
                if (ppath.length == 1) return this;
232
                int lastq = ppath[ppath.length - 1];
233
                
234
                if (lastq == 2) {
235
                        GoogleMapsTile parent = getParent();
236
                        parent.goToQ(1);
237
                        return parent;
238
                }
239
                
240
                if (lastq == 3) {
241
                        GoogleMapsTile parent = getParent();
242
                        parent.goToQ(0);
243
                        return parent;
244
                }
245
                
246
                if (lastq == 0) {
247
                        GoogleMapsTile parent = getParent();
248
                        GoogleMapsTile parent_under = parent.getTileUnder();
249
                        parent_under.goToQ(3);
250
                        return parent_under;
251
                }
252
                
253
                if (lastq == 1) {
254
                        GoogleMapsTile parent = getParent();
255
                        GoogleMapsTile parent_under = parent.getTileUnder();
256
                        parent_under.goToQ(2);
257
                        return parent_under;
258
                }
259
                
260
                logger.error("Bad path item: " + lastq);
261
                return this;
262
        }
263
        
264

    
265
}