Statistics
| Revision:

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

History | View | Annotate | Download (11.2 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.Color;
59
import java.awt.Graphics2D;
60
import java.awt.Image;
61
import java.awt.geom.AffineTransform;
62
import java.awt.geom.Point2D;
63
import java.awt.geom.Rectangle2D;
64
import java.awt.image.BufferedImage;
65
import java.io.File;
66
import java.io.IOException;
67
import java.net.MalformedURLException;
68
import java.net.URL;
69

    
70
import org.apache.log4j.Logger;
71
import org.gvsig.compatible.StringUtil;
72

    
73
import es.prodevelop.gvsig.mobile.fmap.driver.FMapDriverException;
74
import es.prodevelop.gvsig.mobile.fmap.driver.raster.GeorreferencedRasterDriver;
75
import es.prodevelop.gvsig.mobile.fmap.proj.IProjection;
76
import es.prodevelop.gvsig.mobile.fmap.util.ResourceReader;
77
import es.prodevelop.gvsig.mobile.fmap.util.Utils;
78
import es.prodevelop.gvsig.mobile.fmap.util.download.DownloadException;
79
import es.prodevelop.gvsig.mobile.fmap.util.download.DownloadTask;
80
import es.prodevelop.gvsig.mobile.fmap.util.download.Downloader;
81
import es.prodevelop.gvsig.mobile.fmap.viewport.ViewPort;
82

    
83
public class GoogleMapsDriver implements GeorreferencedRasterDriver {
84
        
85
        private static Logger logger = Logger.getLogger(GoogleMapsDriver.class);
86

    
87
        public void initialize(IProjection proj) throws IOException {
88
        }
89

    
90
        public IProjection getProjection() {
91
                return null;
92
        }
93

    
94
        public void setProjection(IProjection proj) {
95
        }
96

    
97
        public Rectangle2D getFullExtent() {
98
                return new Rectangle2D.Double(
99
                                - GoogleUtils.SEMI_EQUATOR_LENGTH,
100
                                - GoogleUtils.SEMI_EQUATOR_LENGTH,
101
                                2 * GoogleUtils.SEMI_EQUATOR_LENGTH,
102
                                2 * GoogleUtils.SEMI_EQUATOR_LENGTH);
103
        }
104

    
105
        public boolean isGeoreferenced() {
106
                return true;
107
        }
108

    
109
        public void setAffineTransform(AffineTransform t) {
110
                // TODO Auto-generated method stub
111

    
112
        }
113

    
114
        public AffineTransform getAffineTransform() {
115
                // TODO Auto-generated method stub
116
                return null;
117
        }
118

    
119
        public boolean fileAccepted(File fName) {
120
                // TODO Auto-generated method stub
121
                return false;
122
        }
123

    
124
        public void open(File f) throws IOException {
125
                // TODO Auto-generated method stub
126

    
127
        }
128

    
129
        public void initialize() throws IOException {
130
                // TODO Auto-generated method stub
131

    
132
        }
133

    
134
        public void close() throws IOException {
135
                // TODO Auto-generated method stub
136

    
137
        }
138

    
139
        public String getFilePath() {
140
                // TODO Auto-generated method stub
141
                return null;
142
        }
143

    
144
        public int getNumBands() {
145
                // TODO Auto-generated method stub
146
                return 3;
147
        }
148

    
149
        public int getTransparency() {
150
                // TODO Auto-generated method stub
151
                return 0;
152
        }
153

    
154
        public void setTransparency(int trans) {
155
                // TODO Auto-generated method stub
156

    
157
        }
158

    
159
        public void draw(Graphics2D g, ViewPort viewPort)
160
                        throws FMapDriverException {
161

    
162
                int[] d_w =
163
                        GoogleUtils.getDepthAndTileSize(
164
                                        viewPort.getAdjustedExtent().getWidth(),
165
                                        viewPort.getImageWidth());
166

    
167
                int d = d_w[0];
168
                int w = d_w[1];
169
                String owner = getRightsOwner(viewPort, d);
170

    
171
                // System.out.println("depth = " + d);
172
                // System.out.println("width = " + w);
173
                
174
                int vp_img_w = viewPort.getImageWidth();
175
                int vp_img_h = viewPort.getImageHeight();
176
                
177
                // System.out.println("vp_img_w = " + vp_img_w);
178
                // System.out.println("vp_img_h = " + vp_img_h);
179
                
180
                int n_tiles_hor = 2 + vp_img_w / w;
181
                int n_tiles_ver = 2 + vp_img_h / w;
182
                
183
                // System.out.println("n_tiles_hor = " + n_tiles_hor);
184
                // System.out.println("n_tiles_ver = " + n_tiles_ver);
185

    
186
                Point2D nor_vp_top_left = GoogleUtils.normalize(
187
                                new Point2D.Double(
188
                                viewPort.getAdjustedExtent().getMinX(),
189
                                viewPort.getAdjustedExtent().getMaxY()));
190
                
191
                // double vp_max_y = viewPort.getAdjustedExtent().getMaxY();
192
                
193
                // System.out.println("vp_min_x = " + vp_min_x);
194
                // System.out.println("vp_max_y = " + vp_max_y);
195

    
196
                // Point2D vp_top_left = new Point2D.Double(vp_min_x, vp_max_y);
197

    
198
                GoogleMapsTile first_tile = GoogleUtils.getTileFor(nor_vp_top_left, d);
199
                
200
                Point2D fi_tile_top_left = GoogleUtils.normalize(
201
                                new Point2D.Double(
202
                                                first_tile.getExtent().getMinX(),
203
                                                first_tile.getExtent().getMaxY()));
204

    
205
                // System.out.println("first_tile, w = " + first_tile_w);
206
                
207
                // double first_ve_world = first_tile.getVerWorld();
208
                // System.out.println("first_tile, ve_world = " + first_ve_world);
209
                // double first_ho_world = first_tile.getHorWorld();
210
                // System.out.println("first_tile, ho_world = " + first_ho_world);
211
                
212
                double first_x_offset = fi_tile_top_left.getX() - nor_vp_top_left.getX();
213
                double first_y_offset = nor_vp_top_left.getY() - fi_tile_top_left.getY();
214
                
215
                first_x_offset = GoogleUtils.normalizeOffset(first_x_offset);
216
                first_y_offset = GoogleUtils.normalizeOffset(first_y_offset);
217

    
218
                // System.out.println("first_tile, first_x_offset = " + first_x_offset);
219
                // System.out.println("first_tile, first_y_offset = " + first_y_offset);
220

    
221
                int first_x_offset_pix = viewPort.fromMapDistance(first_x_offset);
222
                int first_y_offset_pix = viewPort.fromMapDistance(first_y_offset);
223
                
224
                // System.out.println("first_tile, first_x_offset_pix = " + first_x_offset_pix);
225
                // System.out.println("first_tile, first_y_offset_pix = " + first_y_offset_pix);
226

    
227
                int screen_x_item = 0;
228
                int screen_y_item = 0;
229

    
230
                GoogleMapsTile top_tile = first_tile;
231
                
232
                for (screen_x_item = 0; screen_x_item < n_tiles_hor; screen_x_item++) {
233
                        
234
                        if (screen_x_item > 0) top_tile = top_tile.getTileToTheRight();
235
                        GoogleMapsTile item_tile = (GoogleMapsTile) top_tile.clone();
236

    
237
                        for (screen_y_item = 0; screen_y_item < n_tiles_ver; screen_y_item++) {
238
                                
239
                                if (screen_y_item > 0) item_tile = item_tile.getTileUnder();
240
                                
241
                                URL u = item_tile.getURL();
242
                                
243
                                // System.out.println("TILE URL = " + u.toString());
244

    
245
                                File img_file = null;
246
                                try {
247

    
248
                                        // img_file = Downloader.get(u, "jpg");
249
                                        // img_file = Downloader.getByteArray(u, "jpg");
250
                                        // /*
251
                                        img_file = (File) Downloader.downloadObjectCancel(
252
                                                        u,
253
                                                        "jpg",
254
                                                        DownloadTask.FILE,
255
                                                        "goo",
256
                                                        null, true);
257
                                                        // */
258

    
259
                                } catch (DownloadException e) {
260
                                        
261
                                        g.setFont(Utils.TILE_PROBLEM_FONT);
262
                                        if (e.getMessage().indexOf("403") != -1) {
263
                                                g.setColor(Utils.TILE_PROBLEM_403_COLOR);
264
                                                g.drawString(Utils.TILE_PROBLEM_MSG_403,
265
                                                                Utils.TILE_PROBLEM_X_OFFSET + screen_x_item * w + first_x_offset_pix,
266
                                                                Utils.TILE_PROBLEM_Y_OFFSET + screen_y_item * w + first_y_offset_pix);
267
                                        } else {
268
                                                logger.error("While downloading: " + e.getMessage());
269
                                                g.setColor(Utils.TILE_PROBLEM_COLOR);
270
                                                g.drawString(Utils.TILE_PROBLEM_MSG_ERROR,
271
                                                                Utils.TILE_PROBLEM_X_OFFSET + screen_x_item * w + first_x_offset_pix,
272
                                                                Utils.TILE_PROBLEM_Y_OFFSET + screen_y_item * w + first_y_offset_pix);
273
                                        }
274
                                        continue;
275
                                }
276
                                
277
                                Image img = ResourceReader.getImage(img_file);
278
                                
279
                                // /*
280
                                if ((img == null) || (img.getWidth(null) == -1)) {
281
                                        g.setFont(Utils.TILE_PROBLEM_FONT);
282
                                        g.setColor(Utils.TILE_PROBLEM_COLOR);
283
                                        g.drawString(Utils.TILE_PROBLEM_MSG_NODATA,
284
                                                        Utils.TILE_PROBLEM_X_OFFSET + screen_x_item * w + first_x_offset_pix,
285
                                                        Utils.TILE_PROBLEM_Y_OFFSET + screen_y_item * w + first_y_offset_pix);
286
                                        continue;
287
                                }
288
                                // */
289
                                
290
                                if (Utils.USING_PDA) {
291
                                        img = img.getScaledInstance(w, w, Image.SCALE_SMOOTH);
292
                                } else {
293
                                        BufferedImage bim = Utils.createBufferedImage(256, 256);
294
                                        bim.getGraphics().drawImage(img, 0, 0, null);
295
                                        img = bim.getScaledInstance(w, w, Image.SCALE_SMOOTH);
296
                                }
297
                                
298
                                
299
                                /*
300
                                BufferedImage bim = Utils.createBufferedImage(256, 256);
301
                                bim.getGraphics().drawImage(img, 0, 0, null);
302
                                
303
                                Image resize_img = bim.getScaledInstance(w, w, Image.SCALE_FAST);
304
                                */
305

    
306
                                // System.out.println("Paste tile: x = " + (screen_x_item * w + first_x_offset_pix));                                
307
                                // System.out.println("Paste tile: y = " + (screen_y_item * w + first_y_offset_pix));                                
308
                                
309
                                g.drawImage(
310
                                                img,
311
                                                screen_x_item * w + first_x_offset_pix,
312
                                                screen_y_item * w + first_y_offset_pix,
313
                                                null);
314
                                
315
                                // System.out.println("===========================================");                                
316
                                
317
                        }
318

    
319
                }
320
                
321
                if (owner != null) {
322
                        g.setFont(GoogleUtils.OWNER_FONT);
323
                        g.setColor(Color.YELLOW);
324
                        int y = viewPort.getImageHeight() - 5;
325
                        g.drawString("Provided by: " + owner, 5, y);
326
                }
327
        }
328

    
329
        private String getRightsOwner(ViewPort viewPort, int d) {
330
                
331
                Rectangle2D.Double w = new Rectangle2D.Double(-180, 90, 360, 180);
332
                Rectangle2D.Double intersect = new Rectangle2D.Double(0, 0, 1, 1);
333
                Rectangle2D.intersect(w, viewPort.getAdjustedExtent(), intersect);
334
                
335
                double cx = intersect.getCenterX();
336
                double cy = intersect.getCenterY();
337
                
338
                String u = "http://maps.google.com/maps?spn="
339
                        + Math.abs(intersect.getHeight())
340
                        + ","
341
                        + Math.abs(intersect.getWidth()) 
342
                        + "&t=k&z="
343
                        + d
344
                        + "&vp="
345
                        + cx
346
                        + ","
347
                        + cy
348
                        + "&ev=zi";
349
                
350
                try {
351
                        URL url = new URL(u);
352
                        byte[] owner_byte = Downloader.getByteArray(url, "txt");
353
                        String owner_str = new String(owner_byte);
354
                        String[] params =  StringUtil.splitString(owner_str, ",");
355
                        if (params.length >= 8) {
356
                                String quote = params[7];
357
                                String clean = "";
358
                                for (int i=0; i<quote.length(); i++)
359
                                        if (quote.substring(i, i+1).compareTo("\"") != 0)
360
                                                clean = clean + quote.substring(i, i+1);
361
                                return clean;
362
                        }
363
                } catch (MalformedURLException e) {
364
                        logger.error("Bad URL: " + u);
365
                } catch (DownloadException e) {
366
                        logger.error("While getting owner: " + e.getMessage());
367
                }
368
                return null;
369
        }
370

    
371
        private boolean forbidden(byte[] img_file) {
372
                
373
                String sample = new String(img_file, 0, 80);
374
                if (sample.indexOf("403") != 1) return true;
375
                return false;
376
        }
377

    
378
        public int getRasterDataType() {
379
                // TODO Auto-generated method stub
380
                return 0;
381
        }
382

    
383
        public Object getData(int x, int y, int band) {
384
                // TODO Auto-generated method stub
385
                return null;
386
        }
387

    
388
        public int[] getPixel(double wcx, double wcy) {
389
                // TODO Auto-generated method stub
390
                return null;
391
        }
392

    
393
        public String getName() {
394
                // TODO Auto-generated method stub
395
                return "Google Maps Raster Driver";
396
        }
397

    
398
}