Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wms / branches / org.gvsig.raster.wms_dataaccess_refactoring / org.gvsig.raster.wms.io / src / main / java / org / gvsig / raster / wms / io / downloader / TileDownloaderForWMS.java @ 2378

History | View | Annotate | Download (5.83 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.wms.io.downloader;
23

    
24
import java.awt.geom.Rectangle2D;
25
import java.io.File;
26
import java.io.IOException;
27
import java.util.List;
28
import java.util.Vector;
29

    
30
import org.cresques.cts.IProjection;
31
import org.gvsig.fmap.dal.coverage.RasterLocator;
32
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
33
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
34
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
35
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
36
import org.gvsig.fmap.dal.coverage.exception.QueryException;
37
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
38
import org.gvsig.fmap.dal.coverage.exception.RemoteServiceException;
39
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
40
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
41
import org.gvsig.fmap.dal.exception.InitializeException;
42
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
43
import org.gvsig.raster.cache.tile.Tile;
44
import org.gvsig.raster.cache.tile.exception.TileGettingException;
45
import org.gvsig.raster.impl.DefaultRasterManager;
46
import org.gvsig.raster.impl.provider.RasterProvider;
47
import org.gvsig.raster.impl.provider.tile.BaseTileDownloader;
48
import org.gvsig.raster.util.DefaultProviderServices;
49
import org.gvsig.raster.wms.io.RemoteWMSStyle;
50
import org.gvsig.raster.wms.io.WMSConnector;
51
import org.gvsig.raster.wms.io.WMSDataParameters;
52
import org.gvsig.raster.wms.io.WMSProvider;
53
import org.gvsig.remoteclient.utils.Utilities;
54
import org.gvsig.remoteclient.wms.WMSStatus;
55
import org.gvsig.tools.locator.LocatorException;
56

    
57
/** 
58
 * Tile getter 
59
 * @author Nacho Brodin (nachobrodin@gmail.com)
60
 */
61
public class TileDownloaderForWMS extends BaseTileDownloader {
62
        private WMSConnector             connector  = null;
63
        private Extent                   lyrExtent  = null;
64

    
65
        
66
        
67
        public TileDownloaderForWMS(RasterDataStore store,
68
                        int tilePxWidth,
69
                        int tilePxHeight) throws RemoteServiceException {
70
                super(store, tilePxWidth, tilePxHeight);
71
                this.connector = ((WMSProvider)store.getProvider()).getConnector();
72
                lyrExtent = store.getExtent();
73
        }
74
        
75
        public synchronized Tile downloadTile(Tile tile) throws TileGettingException {
76
                try {
77
                        Rectangle2D r = new Rectangle2D.Double(tile.getExtent().getMinX(), 
78
                                        tile.getExtent().getMinY() - tile.getExtent().getHeight(), 
79
                                        tile.getExtent().getWidth(), 
80
                                        tile.getExtent().getHeight());
81
                        //Extent tileExtent = RasterLocator.getManager().getDataStructFactory().createExtent(r);
82
                        WMSDataParameters p = (WMSDataParameters)store.getParameters();
83
                        WMSStatus wmsStatus = new WMSStatus();
84
                        wmsStatus.setLayerNames(Utilities.createVector(p.getLayerQuery(), ","));
85
                        wmsStatus.setSrs(p.getSRSCode());
86
                        wmsStatus.setFormat(p.getFormat());
87
                        List<RemoteWMSStyle> listStyles = p.getStyles();
88
                        Vector<?> v = listStyles != null ? new Vector<RemoteWMSStyle>(listStyles) : null;
89
                        wmsStatus.setStyles(v);
90
                        wmsStatus.setDimensions(p.getDimensions());
91
                        wmsStatus.setTransparency(p.isWmsTransparent());
92
                        wmsStatus.setOnlineResource((String) p.getOnlineResource().get("GetMap"));
93
                        wmsStatus.setExtent(r);
94
                        wmsStatus.setHeight(tile.getHeightPx());
95
                        wmsStatus.setWidth(tile.getWidthPx());
96
                        IProjection proj = (IProjection)p.getSRS();
97
                        if(proj != null)
98
                                wmsStatus.setProjected(proj.isProjected());
99
                        connector.getMap(wmsStatus, tile.getCancelled(), tile.getFile());
100

    
101
                        //Si borramos el rmf no se puede leer la etiqueta Alpha. En caso de que se modifique jgdal para
102
                        //poder guardar esta etiqueta deberiamos borrar el rmf para ahorrar ficheros
103
                        //File rmf = new File(tile.getFile().getAbsolutePath() + ".rmf");
104
                        //if(rmf.exists())
105
                                //rmf.delete();
106
                } catch (RemoteServiceException e) {
107
                        throw new TileGettingException(e);
108
                } 
109
                readTileFromDisk(tile);
110
                return tile;
111
        }
112
        
113
        @SuppressWarnings("unused")
114
        private void fuseFiles(Extent dstExtent, int dstWidth, int dstHeight, File src, File dst, double pixelSize) 
115
                throws RasterDriverException, ProcessInterruptedException, ProviderNotRegisteredException, InitializeException, LocatorException, NotSupportedExtensionException, IOException, QueryException {
116
                RasterProvider provider = DefaultProviderServices.loadProvider(src);
117
                
118
                RasterQuery q = RasterLocator.getManager().createQuery();
119
                Buffer bufInput = store.query(q);
120
                
121
                Buffer bufDst = DefaultRasterManager.getInstance().createMemoryBuffer(provider.getDataType()[0], dstWidth, dstHeight, provider.getBandCount(), true);
122
                String extension = RasterLocator.getManager().getFileUtils().getExtensionFromFileName(src.getAbsolutePath());
123
                RasterLocator.getManager().getRasterUtils().copyToBuffer(
124
                                bufDst, 
125
                                dstExtent, 
126
                                bufInput, 
127
                                lyrExtent, 
128
                                pixelSize, 
129
                                provider.getColorInterpretation().hasAlphaBand());
130
                RasterLocator.getManager().getProviderServices().exportBufferToFile(
131
                                bufDst, 
132
                                provider.getCellSize(), 
133
                                extension, 
134
                                provider.getColorInterpretation().hasAlphaBand(), 
135
                                dst, 
136
                                dstExtent, 
137
                                provider.getColorInterpretation());
138
        }
139
        
140
}