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 / io / OSMProvider.java @ 121

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

    
24
import java.awt.geom.AffineTransform;
25

    
26
import org.cresques.cts.IProjection;
27
import org.gvsig.fmap.crs.CRSFactory;
28
import org.gvsig.fmap.dal.DALLocator;
29
import org.gvsig.fmap.dal.DataStore;
30
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
31
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
32
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
33
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
34
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
35
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
36
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
37
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
38
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
39
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
40
import org.gvsig.fmap.dal.exception.OpenException;
41
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
42
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
43
import org.gvsig.metadata.MetadataLocator;
44
import org.gvsig.raster.cache.tile.provider.TileServer;
45
import org.gvsig.raster.impl.buffer.SpiRasterQuery;
46
import org.gvsig.raster.impl.datastruct.ExtentImpl;
47
import org.gvsig.raster.impl.provider.AbstractRasterProvider;
48
import org.gvsig.raster.impl.provider.RasterProvider;
49
import org.gvsig.raster.impl.store.DefaultRasterStore;
50
import org.gvsig.raster.impl.store.DefaultStoreFactory;
51
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
52
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
53
import org.gvsig.raster.osm.cachestruct.OSMCacheStruct;
54
import org.gvsig.raster.osm.downloader.OSMTileServer;
55
import org.gvsig.tools.ToolsLocator;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58
/**
59
 * Data provider for OSM servers
60
 *
61
 * @author Nacho Brodin (nachobrodin@gmail.com)
62
 */
63
public class OSMProvider extends AbstractRasterProvider {
64
        public static String                     NAME                     = "OSM Raster";
65
        public static String                     DESCRIPTION              = "OSM Raster Server";
66
        public final String                      METADATA_DEFINITION_NAME = NAME;
67
        private Extent                           viewRequest              = null;
68
        private TileServer                       tileServer               = null;
69
        private boolean                          open                     = false;
70
    private DataStoreTransparency            fileTransparency         = null;
71
    private static final Logger              logger                   = LoggerFactory.getLogger(OSMProvider.class);
72
    
73
        public static void register() {
74
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
75
                
76
                if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
77
                        dataman.registerStoreProvider(NAME,
78
                                        OSMProvider.class, OSMDataParametersImpl.class);
79
                }
80
                
81
                dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
82
        }
83
        
84
        public OSMProvider() {
85
        }
86
        
87
        /**
88
         * Opens the dataset.
89
         * @param proj Projection
90
         * @param fName File name
91
         * @throws NotSupportedExtensionException
92
         */
93
        public OSMProvider(String params) throws NotSupportedExtensionException, OpenException {
94
                super(params);
95
                
96
        }
97
        
98
        public OSMProvider (OSMDataParametersImpl params,
99
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException, OpenException {
100
                super(params, storeServices, ToolsLocator.getDynObjectManager()
101
                                .createDynObject(
102
                                                MetadataLocator.getMetadataManager().getDefinition(
103
                                                                DataStore.METADATA_DEFINITION_NAME)));
104
                init(params, storeServices);
105
        }
106

    
107
        /**
108
         * Build file references
109
         * @param proj Projection
110
         * @param param Load parameters
111
         * @throws NotSupportedExtensionException
112
         */
113
        public void init (OSMDataParametersImpl params,
114
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException, OpenException {
115
                setParam(storeServices, params);
116
                setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE});
117
                bandCount = 4;
118
                super.init();
119
                fileSize = (long)(getWidth() * getHeight() * 3);
120
                
121
                Extent ext = getExtent();
122
                ownTransformation = new AffineTransform(
123
                                ext.width() / getWidth(), 0, 
124
                                0, -(ext.height() / getHeight()), 
125
                                getExtent().getMin().getX(), 
126
                                getExtent().getMax().getY());
127
                externalTransformation = (AffineTransform)ownTransformation.clone();
128
                open = true;
129
        }
130
        
131
        public IProjection getProjection() {
132
                if(proj == null) {
133
                        try {
134
                                proj = CRSFactory.getCRS("EPSG:3785");
135
                        } catch(Exception e) {
136
                                logger.info("Projection not loaded", e);
137
                        }
138
                }
139
                return proj;
140
        }
141
        
142
        /**
143
         * Gets the bounding box in world coordinates. 
144
         * @return Extent
145
         */
146
        public Extent getExtent() {
147
                return ((OSMCacheStruct)getTileServer().getStruct()).getWorldExtent();
148
        }
149
        
150
        /**
151
         * Reloads metadata using the selected grid
152
         */
153
        @SuppressWarnings("unused")
154
        private void reloadMetadataFromGrid() {
155
                //wktProjection = null;
156
                //CrsWkt crs = new CrsWkt(wktProjection);
157
                //IProjection proj = CRSFactory.getCRS("EPSG:23030");
158
                
159
                /*ownTransformation = new AffineTransform(
160
                                scaleX, 0, 
161
                                0, -scaleY, 
162
                                pRect.getMinX(), 
163
                                pRect.getMaxY());*/
164
                externalTransformation = (AffineTransform)ownTransformation.clone();
165
                bandCount = 3; 
166
                setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE});
167
        }
168
        
169
        public RasterProvider load() {
170
                return this;
171
        }
172
        
173
        public boolean isOpen() {
174
                return open;
175
        }
176

    
177
        public void close() {
178
        }
179

    
180
        public String translateFileName(String fileName) {
181
                return fileName;
182
        }
183
        
184
        public NoData getNoDataValue() {
185
                NoData nodata = super.getNoDataValue();
186
                if(nodata != null)
187
                        nodata.setNoDataTransparent(false);
188
                return noData;
189
        }
190

    
191
        /**
192
         * Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci�n
193
         * de la vista asignada ya que la petici�n viene en coordenadas del fichero .rmf y la vista (v)
194
         * ha de estar en coordenadas del fichero.
195
         */
196
        public void setView(Extent e) {
197
                viewRequest = new ExtentImpl(e);
198
        }
199

    
200
        public Extent getView() {
201
                return viewRequest;
202
        }
203

    
204
        public double getWidth() {
205
                int n = getTileServer().getStruct().getNumberOfLevels() - 1;
206
                return getExtent().width() / getTileServer().getStruct().getPixelSizeByLevel(n);
207
        }
208

    
209
        public double getHeight() {
210
                int n = getTileServer().getStruct().getNumberOfLevels() - 1;
211
                return getExtent().width() / getTileServer().getStruct().getPixelSizeByLevel(n);
212
        }
213

    
214
        public Object readBlock(int pos, int blockHeight, double scale)
215
                throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
216
                return null;
217
        }
218

    
219
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
220
                return null;
221
        }
222

    
223
        public int getBlockSize(){
224
                return 0;
225
        }
226

    
227
        /**
228
         * Informa de si el driver ha supersampleado en el �ltimo dibujado. Es el driver el que colocar�
229
         * el valor de esta variable cada vez que dibuja.
230
         * @return true si se ha supersampleado y false si no se ha hecho.
231
         */
232
        public boolean isSupersampling() {
233
                return false;
234
        }
235

    
236
        public void setAffineTransform(AffineTransform t){
237
                super.setAffineTransform(t);
238
        }
239

    
240
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
241
                if(band >= getBandCount())
242
                        throw new BandAccessException("Wrong band");
243
                return 0;
244
        }
245

    
246
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
247
                if (band >= getBandCount())
248
                        throw new BandAccessException("Wrong band");
249
                return 0;
250
        }
251

    
252
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
253
                if (band >= getBandCount())
254
                        throw new BandAccessException("Wrong band");
255
                return 0;
256
        }
257

    
258
        public boolean isOverviewsSupported() {
259
                return true;
260
        }
261

    
262
        public boolean isReproyectable() {
263
                return true;
264
        }
265

    
266
        public String getProviderName() {
267
                return NAME;
268
        }
269

    
270
        public String getName() {
271
            return this.getParameters().getOSMLayerName();
272
        }
273
        
274
        public OSMDataParameters getParameters() {
275
                return (OSMDataParameters)this.param;
276
        }
277
        
278
        public DataStoreTransparency getTransparency() {
279
                if(fileTransparency == null)
280
                        fileTransparency = new DataStoreTransparency(getColorInterpretation());
281
                return fileTransparency;
282
        }
283
        
284
        public ColorInterpretation getColorInterpretation() {
285
                if(super.getColorInterpretation() == null) {
286
                        super.setColorInterpretation(DataStoreColorInterpretation.createRGBInterpretation());
287
                }
288
                return super.getColorInterpretation();
289
        }
290
        
291
        public void setStatus(RasterProvider provider) {
292
                if(provider instanceof OSMProvider) {
293
                        //Not implemented yet
294
                }
295
        }
296
        
297
        public boolean isTimeSupported() {
298
                return true;
299
        }
300
        
301
        public boolean isTiled() {
302
                return true;
303
        }
304
        
305
        public TileServer getTileServer() {
306
                if(tileServer == null) {
307
                        DefaultRasterStore store = new DefaultRasterStore();
308
                        store.setProvider(this);
309
                        tileServer = new OSMTileServer(store, 
310
                                        ((OSMDataParameters)param).getTileSuffix(), 
311
                                        ((OSMDataParameters)param).getNumberOfLevels());
312
                }
313
                return tileServer;
314
        }
315

    
316
        @Override
317
        public void loadBuffer(SpiRasterQuery query)
318
                        throws ProcessInterruptedException, RasterDriverException {
319
                
320
        }
321
}