Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.io / src / main / java / org / gvsig / raster / wmts / io / downloader / TileDownloaderForWMTS.java @ 2613

History | View | Annotate | Download (3.99 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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.wmts.io.downloader;
23

    
24
import java.io.File;
25
import java.io.IOException;
26
import java.net.ConnectException;
27
import java.net.URL;
28

    
29
import org.gvsig.compat.net.ICancellable;
30
import org.gvsig.fmap.dal.coverage.exception.RemoteServiceException;
31
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
32
import org.gvsig.raster.cache.tile.Tile;
33
import org.gvsig.raster.cache.tile.exception.TileGettingException;
34
import org.gvsig.raster.impl.provider.tile.BaseTileDownloader;
35
import org.gvsig.raster.wmts.io.WMTSDataParameters;
36
import org.gvsig.raster.wmts.ogc.WMTSClient;
37
import org.gvsig.raster.wmts.ogc.WMTSOGCLocator;
38
import org.gvsig.raster.wmts.ogc.WMTSStatus;
39
import org.gvsig.raster.wmts.ogc.exception.ServerErrorException;
40
import org.gvsig.raster.wmts.ogc.exception.WMTSException;
41

    
42
/** 
43
 * Tile getter 
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public class TileDownloaderForWMTS extends BaseTileDownloader {
47
        private WMTSClient             ogcClient  = null;
48
        
49
        public TileDownloaderForWMTS(RasterDataStore store, 
50
                        int tilePxWidth,
51
                        int tilePxHeight) {
52
                super(store, tilePxWidth, tilePxHeight);
53
        }
54
        
55
        /**
56
         * Gets the connector from the URL
57
         * @return
58
         * @throws RemoteServiceException
59
         */
60
        public WMTSClient getOGCClient() throws WMTSException {
61
                if(ogcClient == null) {
62
                        WMTSDataParameters p = (WMTSDataParameters)store.getParameters();
63
                        ogcClient = p.getOGCClient();
64
                        if(ogcClient != null)
65
                                return ogcClient;
66
                        
67
                        URL url = null;
68
                        try {
69
                                url = new URL(p.getURI());
70
                        } catch (Exception e) {
71
                                throw new WMTSException("Malformed URL",e);
72
                        }
73
                        try {
74
                                ogcClient = WMTSOGCLocator.getManager().createWMTSClient(url.toString());
75
                                ogcClient.connect(false, new ICancellable() {
76
                                        public boolean isCanceled() {
77
                                                return false;
78
                                        }
79
                                        
80
                                        public Object getID() {
81
                                                return null;
82
                                        }
83
                                });
84
                        } catch (ConnectException e) {
85
                                throw new WMTSException("Connect exception",e);
86
                        } catch (IOException e) {
87
                                throw new WMTSException("Connect exception",e);
88
                        }
89
                }
90
                return ogcClient;
91
        }
92
        
93
        public synchronized Tile downloadTile(Tile tile) throws TileGettingException {
94
                try {
95
                        WMTSStatus status = (WMTSStatus)tile.getDownloaderParams("WMTSStatus");
96
                        status.setTileRow(tile.getRow());
97
                        status.setTileCol(tile.getCol());
98
                        
99
                        String urlFromTemplate = status.getResourceURL(tile.getRow() + "" + tile.getCol() + "");
100
                        System.out.println(urlFromTemplate);
101
                        File f = null;
102
                        if(urlFromTemplate != null)
103
                                f = getOGCClient().getTile(urlFromTemplate, tile.getCancelled(), tile.getFile());
104
                        else
105
                                f = getOGCClient().getTile(status, tile.getCancelled(), tile.getFile());
106
                        tile.setFile(f);
107
                        //Si borramos el rmf no se puede leer la etiqueta Alpha. En caso de que se modifique jgdal para
108
                        //poder guardar esta etiqueta deberiamos borrar el rmf para ahorrar ficheros
109
                        //File rmf = new File(tile.getFile().getAbsolutePath() + ".rmf");
110
                        //if(rmf.exists())
111
                                //rmf.delete();
112
                } catch (WMTSException e) {
113
                        throw new TileGettingException(e);
114
                } catch (ServerErrorException e) {
115
                        throw new TileGettingException(e);
116
                }
117
                readTileFromDisk(tile);
118
                return tile;
119
        }
120
        
121
}