Statistics
| Revision:

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 @ 1342

History | View | Annotate | Download (4.66 KB)

1
/* 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.File;
25
import java.io.FileOutputStream;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.net.MalformedURLException;
29
import java.net.URI;
30
import java.net.URISyntaxException;
31
import java.net.URL;
32
import org.apache.commons.io.IOUtils;
33
import org.apache.http.HttpResponse;
34
import org.apache.http.client.ClientProtocolException;
35
import org.apache.http.client.ResponseHandler;
36
import org.apache.http.client.config.RequestConfig;
37
import org.apache.http.client.methods.HttpGet;
38
import org.apache.http.impl.client.CloseableHttpClient;
39
import org.apache.http.impl.client.HttpClientBuilder;
40
import org.gvsig.compat.net.ICancellable;
41
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
42
import org.gvsig.raster.cache.tile.Tile;
43
import org.gvsig.raster.cache.tile.exception.TileGettingException;
44
import org.gvsig.raster.impl.provider.tile.BaseTileDownloader;
45
import org.gvsig.raster.osm.io.OSMDataParameters;
46
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48

    
49
/**
50
 * Tile getter for Open Street Map
51
 * @author Nacho Brodin (nachobrodin@gmail.com)
52
 */
53
public class TileDownloaderForOSM extends BaseTileDownloader {
54
        private String server   = null;
55
        private String suffix   = null;
56
        private final String slash    = "/";
57

    
58
        private static final Logger LOGGER = LoggerFactory.getLogger(TileDownloaderForOSM.class);
59

    
60

    
61
        public TileDownloaderForOSM(RasterDataStore store,
62
                        int tileSize) {
63
                super(store, tileSize, tileSize);
64
                server = ((OSMDataParameters)store.getParameters()).getURI().toString();
65
                suffix = ((OSMDataParameters)store.getParameters()).getTileSuffix();
66
        }
67

    
68
    @Override
69
        public synchronized Tile downloadTile(Tile tile) throws TileGettingException {
70
                int level = tile.getLevel();
71
                int y = tile.getRow();
72
                int x = tile.getCol();
73
                try {
74
                        String stringUri = server +
75
            (server.endsWith(slash) ? "" : slash) +
76
            level + slash +
77
            x + slash +
78
            y + "." + suffix;
79
                        //logger.info("OSM URI = "+stringUri);
80
            URI uri = new URI(stringUri);
81

    
82
                        URL url = uri.toURL();
83
            downloadFile(url, tile.getFile(), tile.getCancelled());
84
                } catch (MalformedURLException | URISyntaxException e) {
85
                        throw new TileGettingException(e);
86
                }
87
                readTileFromDisk(tile);
88
                return tile;
89

    
90
        }
91

    
92
    public synchronized void downloadFile(URL url, File file, ICancellable cancel) throws TileGettingException {
93
        int timeout = 30000;
94

    
95
        try {
96

    
97
            HttpGet request = new HttpGet(url.toURI());
98
            request.setHeader("User-Agent", "gvSIG-desktop");
99
            request.setHeader("Referer", "http://www.gvsig.com");
100

    
101
            RequestConfig config = RequestConfig.custom()
102
                .setConnectTimeout(timeout)
103
                .setConnectionRequestTimeout(timeout)
104
                .setSocketTimeout(timeout).build();
105

    
106
            CloseableHttpClient httpClient = HttpClientBuilder.create()
107
                .setDefaultRequestConfig(config)
108
                .build();
109

    
110
            httpClient.execute(request, new ResponseHandler<Object>() {
111
                @Override
112
                public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
113
                    FileOutputStream fs = null;
114
                    try {
115
                        fs = new FileOutputStream(file);
116
                        InputStream content = response.getEntity().getContent();
117
                        IOUtils.copy(content, fs);
118
                    } catch (IOException ex) {
119
                        throw ex;
120
                    } catch (Exception ex) {
121
                        throw new IOException(ex);
122
                    } finally {
123
                        IOUtils.closeQuietly(fs);
124
                    }
125
                    return null;
126
                }
127
            });
128

    
129
        } catch (Exception e) {
130
            throw new TileGettingException(e);
131
        }
132
    }
133

    
134
}