Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / driver / raster / osm / OpenStreetMapDriver.java @ 21606

History | View | Annotate | Download (9.75 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.osm;
57

    
58
import java.awt.Graphics2D;
59
import java.awt.Image;
60
import java.awt.geom.AffineTransform;
61
import java.awt.geom.Point2D;
62
import java.awt.geom.Rectangle2D;
63
import java.awt.image.BufferedImage;
64
import java.io.File;
65
import java.io.IOException;
66
import java.net.URL;
67

    
68
import org.apache.log4j.Logger;
69

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

    
80
public class OpenStreetMapDriver implements GeorreferencedRasterDriver {
81
        
82
        private static Logger logger = Logger.getLogger(OpenStreetMapDriver.class);
83

    
84
        private int layerType = OpenStreetMapUtils.OSM_LAYER_OSMARENDER;
85
        private boolean resizeDown = true;
86
        
87
        private OpenStreetMapDriver() { }
88
        
89
        public OpenStreetMapDriver(int lyr, boolean st) {
90
                resizeDown = st;
91
                layerType = lyr;
92
        }
93
        
94
        public void initialize(IProjection proj) throws IOException {
95
                // TODO Auto-generated method stub
96

    
97
        }
98

    
99
        public IProjection getProjection() {
100
                // TODO Auto-generated method stub
101
                return null;
102
        }
103

    
104
        public void setProjection(IProjection proj) {
105
                // TODO Auto-generated method stub
106

    
107
        }
108

    
109
        public Rectangle2D getFullExtent() {
110
                
111
                double lat_factor = 0.56; 
112
                double lon_factor = 0.98;
113
                return new Rectangle2D.Double(
114
                                - lon_factor * OpenStreetMapUtils.SEMI_EQUATOR_LENGTH,
115
                                - lat_factor * OpenStreetMapUtils.SEMI_EQUATOR_LENGTH,
116
                                2 * lon_factor * OpenStreetMapUtils.SEMI_EQUATOR_LENGTH,
117
                                2 * lat_factor * OpenStreetMapUtils.SEMI_EQUATOR_LENGTH);
118
        }
119

    
120
        public boolean isGeoreferenced() {
121
                // TODO Auto-generated method stub
122
                return true;
123
        }
124

    
125
        public void setAffineTransform(AffineTransform t) {
126
                // TODO Auto-generated method stub
127

    
128
        }
129

    
130
        public AffineTransform getAffineTransform() {
131
                // TODO Auto-generated method stub
132
                return null;
133
        }
134

    
135
        public boolean fileAccepted(File fName) {
136
                // TODO Auto-generated method stub
137
                return false;
138
        }
139

    
140
        public void open(File f) throws IOException {
141
                // TODO Auto-generated method stub
142

    
143
        }
144

    
145
        public void initialize() throws IOException {
146
                // TODO Auto-generated method stub
147

    
148
        }
149

    
150
        public void close() throws IOException {
151
                // TODO Auto-generated method stub
152

    
153
        }
154

    
155
        public String getFilePath() {
156
                // TODO Auto-generated method stub
157
                return null;
158
        }
159

    
160
        public int getNumBands() {
161
                // TODO Auto-generated method stub
162
                return 0;
163
        }
164

    
165
        public int getTransparency() {
166
                // TODO Auto-generated method stub
167
                return 0;
168
        }
169

    
170
        public void setTransparency(int trans) {
171
                // TODO Auto-generated method stub
172

    
173
        }
174

    
175
        public void draw(Graphics2D g, ViewPort viewPort)
176
                        throws FMapDriverException {
177
                
178
                int[] d_w =
179
                        OpenStreetMapUtils.getDepthAndTileSize(
180
                                        viewPort.getAdjustedExtent().getWidth(),
181
                                        viewPort.getImageWidth(),
182
                                        resizeDown);
183
                
184
                int d = d_w[0];
185
                int w = d_w[1];
186

    
187
                // System.out.println("depth = " + d);
188
                // System.out.println("width = " + w);
189
                
190
                int vp_img_w = viewPort.getImageWidth();
191
                int vp_img_h = viewPort.getImageHeight();
192
                
193
                // System.out.println("vp_img_w = " + vp_img_w);
194
                // System.out.println("vp_img_h = " + vp_img_h);
195
                
196
                int n_tiles_hor = 2 + vp_img_w / w;
197
                int n_tiles_ver = 2 + vp_img_h / w;
198
                
199
                // System.out.println("n_tiles_hor = " + n_tiles_hor);
200
                // System.out.println("n_tiles_ver = " + n_tiles_ver);
201

    
202
                Point2D nor_vp_top_left = OpenStreetMapUtils.normalize(
203
                                new Point2D.Double(
204
                                viewPort.getAdjustedExtent().getMinX(),
205
                                viewPort.getAdjustedExtent().getMaxY()));
206
                
207
                // double vp_max_y = viewPort.getAdjustedExtent().getMaxY();
208
                
209
                // System.out.println("vp_min_x = " + vp_min_x);
210
                // System.out.println("vp_max_y = " + vp_max_y);
211

    
212
                // Point2D vp_top_left = new Point2D.Double(vp_min_x, vp_max_y);
213

    
214
                OpenStreetMapTile first_tile =
215
                        OpenStreetMapUtils.getTileFor(nor_vp_top_left, d, layerType);
216
                Point2D fi_tile_top_left = OpenStreetMapUtils.normalize(
217
                                new Point2D.Double(
218
                                                first_tile.getExtent().getMinX(),
219
                                                first_tile.getExtent().getMaxY()));
220

    
221
                // System.out.println("first_tile, w = " + first_tile_w);
222
                
223
                // double first_ve_world = first_tile.getVerWorld();
224
                // System.out.println("first_tile, ve_world = " + first_ve_world);
225
                // double first_ho_world = first_tile.getHorWorld();
226
                // System.out.println("first_tile, ho_world = " + first_ho_world);
227
                
228
                double first_x_offset = fi_tile_top_left.getX() - nor_vp_top_left.getX();
229
                double first_y_offset = nor_vp_top_left.getY() - fi_tile_top_left.getY();
230
                
231
                first_x_offset = OpenStreetMapUtils.normalizeOffset(first_x_offset);
232
                first_y_offset = OpenStreetMapUtils.normalizeOffset(first_y_offset);
233

    
234
                // System.out.println("first_tile, first_x_offset = " + first_x_offset);
235
                // System.out.println("first_tile, first_y_offset = " + first_y_offset);
236

    
237
                int first_x_offset_pix = viewPort.fromMapDistance(first_x_offset);
238
                int first_y_offset_pix = viewPort.fromMapDistance(first_y_offset);
239
                
240
                // System.out.println("first_tile, first_x_offset_pix = " + first_x_offset_pix);
241
                // System.out.println("first_tile, first_y_offset_pix = " + first_y_offset_pix);
242

    
243
                int screen_x_item = 0;
244
                int screen_y_item = 0;
245

    
246
                OpenStreetMapTile top_tile = first_tile;
247
                
248
                for (screen_x_item = 0; screen_x_item < n_tiles_hor; screen_x_item++) {
249
                        
250
                        if (screen_x_item > 0) top_tile = top_tile.getTileToTheRight();
251
                        OpenStreetMapTile item_tile = (OpenStreetMapTile) top_tile.clone();
252

    
253
                        for (screen_y_item = 0; screen_y_item < n_tiles_ver; screen_y_item++) {
254
                                
255
                                if (screen_y_item > 0) item_tile = item_tile.getTileUnder();
256
                                
257
                                URL u = item_tile.getURL();
258
                                
259
                                // System.out.println("TILE URL = " + u.toString());
260

    
261
                                File img_file = null;
262
                                try {
263
                                        img_file = (File) Downloader.downloadObjectCancel(
264
                                                        u, "png", DownloadTask.FILE, "osm",
265
                                                        item_tile.getCacheFilePath(), false);
266
                                        
267
                                } catch (DownloadException e) {
268

    
269
                                        logger.error("While downloading: " + e.getMessage());
270
                                        g.setFont(Utils.TILE_PROBLEM_FONT);
271
                                        if (e.getMessage().indexOf("403") != -1) {
272
                                                g.setColor(Utils.TILE_PROBLEM_403_COLOR);
273
                                                g.drawString(Utils.TILE_PROBLEM_MSG_403,
274
                                                                Utils.TILE_PROBLEM_X_OFFSET + screen_x_item * w + first_x_offset_pix,
275
                                                                Utils.TILE_PROBLEM_Y_OFFSET + screen_y_item * w + first_y_offset_pix);
276
                                        } else {
277
                                                logger.error("While downloading: " + e.getMessage());
278
                                                g.setColor(Utils.TILE_PROBLEM_COLOR);
279
                                                g.drawString(Utils.TILE_PROBLEM_MSG_ERROR,
280
                                                                Utils.TILE_PROBLEM_X_OFFSET + screen_x_item * w + first_x_offset_pix,
281
                                                                Utils.TILE_PROBLEM_Y_OFFSET + screen_y_item * w + first_y_offset_pix);
282
                                        }
283
                                        continue;
284
                                }
285
                                
286
                                logger.debug("Instantiate Image...");
287
                                
288
                                Image img = ResourceReader.getImage(img_file);
289
                                
290
                                // /*
291
                                if ((img == null) || (img.getWidth(null) == -1)) {
292
                                        g.setFont(Utils.TILE_PROBLEM_FONT);
293
                                        g.setColor(Utils.TILE_PROBLEM_COLOR);
294
                                        g.drawString(Utils.TILE_PROBLEM_MSG_NODATA,
295
                                                        Utils.TILE_PROBLEM_X_OFFSET + screen_x_item * w + first_x_offset_pix,
296
                                                        Utils.TILE_PROBLEM_Y_OFFSET + screen_y_item * w + first_y_offset_pix);
297
                                        continue;
298
                                }
299
                                // */
300
                                
301
                                // logger.debug("Instantiate buffered Image...");
302

    
303
                                if (Utils.USING_PDA) {
304
                                        img = img.getScaledInstance(w, w, Image.SCALE_SMOOTH);
305
                                } else {
306
                                        BufferedImage bim = Utils.createBufferedImage(256, 256);
307
                                        bim.getGraphics().drawImage(img, 0, 0, null);
308
                                        img = bim.getScaledInstance(w, w, Image.SCALE_SMOOTH);
309
                                }
310

    
311
                                g.drawImage(
312
                                                img,
313
                                                screen_x_item * w + first_x_offset_pix,
314
                                                screen_y_item * w + first_y_offset_pix,
315
                                                null);
316
                                
317
                                // resize_img = null;
318
                                img = null;
319
                        }
320
                }
321
                
322
                System.gc();
323
        }
324

    
325
        public int getRasterDataType() {
326
                // TODO Auto-generated method stub
327
                return 0;
328
        }
329

    
330
        public Object getData(int x, int y, int band) {
331
                // TODO Auto-generated method stub
332
                return null;
333
        }
334

    
335
        public int[] getPixel(double wcx, double wcy) {
336
                // TODO Auto-generated method stub
337
                return null;
338
        }
339

    
340
        public String getName() {
341
                // TODO Auto-generated method stub
342
                return "OpenStreetMap Raster Driver";
343
        }
344

    
345
}