gvsig-osm / org.gvsig.raster.osm / trunk / org.gvsig.raster.osm / org.gvsig.raster.osm.io / src / main / java / org / gvsig / raster / osm / downloader / TileDownloaderForOSM.java @ 85
History | View | Annotate | Download (3.4 KB)
1 | 85 | nbrodin | /* OSM layers for gvSIG.
|
---|---|---|---|
2 | * Geographic Information System of the Valencian Government
|
||
3 | *
|
||
4 | * Copyright (C) 2012 Nacho Brodin
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | package org.gvsig.raster.osm.downloader; |
||
23 | |||
24 | import java.io.BufferedOutputStream; |
||
25 | import java.io.DataInputStream; |
||
26 | import java.io.DataOutputStream; |
||
27 | import java.io.File; |
||
28 | import java.io.FileOutputStream; |
||
29 | import java.net.HttpURLConnection; |
||
30 | import java.net.MalformedURLException; |
||
31 | import java.net.URI; |
||
32 | import java.net.URISyntaxException; |
||
33 | import java.net.URL; |
||
34 | |||
35 | import org.gvsig.compat.net.ICancellable; |
||
36 | import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
||
37 | import org.gvsig.raster.cache.tile.Tile; |
||
38 | import org.gvsig.raster.cache.tile.exception.TileGettingException; |
||
39 | import org.gvsig.raster.impl.provider.tile.BaseTileDownloader; |
||
40 | import org.gvsig.raster.osm.io.OSMDataParameters; |
||
41 | |||
42 | /**
|
||
43 | * Tile getter for Open Street Map
|
||
44 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
45 | */
|
||
46 | public class TileDownloaderForOSM extends BaseTileDownloader { |
||
47 | private String server = null; |
||
48 | private String suffix = null; |
||
49 | private String slash = "/"; |
||
50 | |||
51 | public TileDownloaderForOSM(RasterDataStore store,
|
||
52 | int tileSize) {
|
||
53 | super(store, tileSize, tileSize);
|
||
54 | server = ((OSMDataParameters)store.getParameters()).getURI(); |
||
55 | suffix = ((OSMDataParameters)store.getParameters()).getTileSuffix(); |
||
56 | } |
||
57 | |||
58 | public synchronized Tile downloadTile(Tile tile) throws TileGettingException { |
||
59 | int level = tile.getLevel();
|
||
60 | int y = tile.getRow();
|
||
61 | int x = tile.getCol();
|
||
62 | try {
|
||
63 | URI uri = new URI( |
||
64 | server + |
||
65 | (server.endsWith(slash) ? "" : slash) +
|
||
66 | level + slash + |
||
67 | x + slash + |
||
68 | y + "." + suffix);
|
||
69 | |||
70 | downloadFile(uri.toURL(), tile.getFile(), tile.getCancelled()); |
||
71 | } catch (MalformedURLException e) { |
||
72 | throw new TileGettingException(e); |
||
73 | } catch (URISyntaxException e) { |
||
74 | throw new TileGettingException(e); |
||
75 | } |
||
76 | readTileFromDisk(tile); |
||
77 | return tile;
|
||
78 | |||
79 | } |
||
80 | |||
81 | public synchronized void downloadFile(URL url, File file, ICancellable cancel) throws TileGettingException { |
||
82 | int IO_BUFFER_SIZE = 8 * 1024; |
||
83 | int timeout = 30000; |
||
84 | |||
85 | DataOutputStream dos;
|
||
86 | try {
|
||
87 | DataInputStream is;
|
||
88 | HttpURLConnection connection = null; |
||
89 | |||
90 | connection = (HttpURLConnection)url.openConnection();
|
||
91 | connection.setConnectTimeout(timeout); |
||
92 | |||
93 | is = new DataInputStream(url.openStream()); |
||
94 | |||
95 | dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(file))); |
||
96 | byte[] buffer = new byte[IO_BUFFER_SIZE]; |
||
97 | |||
98 | int read;
|
||
99 | while ((read = is.read(buffer)) != -1) { |
||
100 | dos.write(buffer, 0, read);
|
||
101 | if(cancel != null && cancel.isCanceled()) |
||
102 | return;
|
||
103 | } |
||
104 | |||
105 | dos.close(); |
||
106 | is.close(); |
||
107 | is = null;
|
||
108 | dos = null;
|
||
109 | } catch (Exception e) { |
||
110 | throw new TileGettingException(e); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | } |