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

History | View | Annotate | Download (10.7 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
import java.io.File;
26
import java.net.URI;
27

    
28
import org.cresques.cts.IProjection;
29

    
30
import org.gvsig.fmap.crs.CRSFactory;
31
import org.gvsig.fmap.dal.DALLocator;
32
import org.gvsig.fmap.dal.DataStore;
33
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
34
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
35
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
36
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
37
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
38
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
39
import org.gvsig.fmap.dal.coverage.exception.InvalidSourceException;
40
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
41
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
42
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
43
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
44
import org.gvsig.fmap.dal.exception.OpenException;
45
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
46
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
47
import org.gvsig.metadata.MetadataLocator;
48
import org.gvsig.raster.cache.tile.provider.TileServer;
49
import org.gvsig.raster.impl.buffer.SpiRasterQuery;
50
import org.gvsig.raster.impl.datastruct.ExtentImpl;
51
import org.gvsig.raster.impl.provider.AbstractRasterProvider;
52
import org.gvsig.raster.impl.provider.RasterProvider;
53
import org.gvsig.raster.impl.store.DefaultRasterStore;
54
import org.gvsig.raster.impl.store.DefaultStoreFactory;
55
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
56
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
57
import org.gvsig.raster.osm.cachestruct.OSMCacheStruct;
58
import org.gvsig.raster.osm.downloader.OSMTileServer;
59
import org.gvsig.tools.ToolsLocator;
60

    
61
import org.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63
/**
64
 * Data provider for OSM servers
65
 *
66
 * @author Nacho Brodin (nachobrodin@gmail.com)
67
 */
68
public class OSMProvider extends AbstractRasterProvider {
69
        public static String                     NAME                     = "OSM Raster";
70
        public static String                     DESCRIPTION              = "OSM Raster Server";
71
        public final String                      METADATA_DEFINITION_NAME = NAME;
72
        private Extent                           viewRequest              = null;
73
        private TileServer                       tileServer               = null;
74
        private boolean                          open                     = false;
75
    private DataStoreTransparency            fileTransparency         = null;
76
    private static final Logger              logger                   = LoggerFactory.getLogger(OSMProvider.class);
77

    
78
        public static void register() {
79
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
80

    
81
                if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
82
                        dataman.registerStoreProvider(NAME,
83
                                        OSMProvider.class, OSMDataParametersImpl.class);
84
                }
85

    
86
                dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
87
        }
88

    
89
        public OSMProvider() {
90
        }
91

    
92
        /**
93
         * Opens the dataset.
94
         * @param proj Projection
95
         * @param fName File name
96
         * @throws NotSupportedExtensionException
97
         */
98
        public OSMProvider(String params) throws NotSupportedExtensionException, OpenException {
99
                super(params);
100

    
101
        }
102

    
103
        public OSMProvider (OSMDataParametersImpl params,
104
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException, OpenException {
105
                super(params, storeServices, ToolsLocator.getDynObjectManager()
106
                                .createDynObject(
107
                                                MetadataLocator.getMetadataManager().getDefinition(
108
                                                                DataStore.METADATA_DEFINITION_NAME)));
109
                init(params, storeServices);
110
        }
111

    
112
        /**
113
         * Build file references
114
         * @param proj Projection
115
         * @param param Load parameters
116
         * @throws NotSupportedExtensionException
117
         */
118
        public void init (OSMDataParametersImpl params,
119
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException, OpenException {
120
                setParam(storeServices, params);
121
                setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE});
122
                bandCount = 4;
123
                super.init();
124
                fileSize = (long)(getWidth() * getHeight() * 3);
125

    
126
                Extent ext = getExtent();
127
                ownTransformation = new AffineTransform(
128
                                ext.width() / getWidth(), 0,
129
                                0, -(ext.height() / getHeight()),
130
                                getExtent().getMin().getX(),
131
                                getExtent().getMax().getY());
132
                externalTransformation = (AffineTransform)ownTransformation.clone();
133
                open = true;
134
        }
135

    
136
        public IProjection getProjection() {
137
                if(proj == null) {
138
                        try {
139
                                proj = CRSFactory.getCRS("EPSG:3857");
140
                        } catch(Exception e) {
141
                                logger.info("Projection not loaded", e);
142
                        }
143
                }
144
                return proj;
145
        }
146

    
147
        /**
148
         * Gets the bounding box in world coordinates.
149
         * @return Extent
150
         */
151
        public Extent getExtent() {
152
                return ((OSMCacheStruct)getTileServer().getStruct()).getWorldExtent();
153
        }
154

    
155
        /**
156
         * Reloads metadata using the selected grid
157
         */
158
        @SuppressWarnings("unused")
159
        private void reloadMetadataFromGrid() {
160
                //wktProjection = null;
161
                //CrsWkt crs = new CrsWkt(wktProjection);
162
                //IProjection proj = CRSFactory.getCRS("EPSG:23030");
163

    
164
                /*ownTransformation = new AffineTransform(
165
                                scaleX, 0,
166
                                0, -scaleY,
167
                                pRect.getMinX(),
168
                                pRect.getMaxY());*/
169
                externalTransformation = (AffineTransform)ownTransformation.clone();
170
                bandCount = 3;
171
                setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE});
172
        }
173

    
174
        public RasterProvider load() {
175
                return this;
176
        }
177

    
178
        public boolean isOpen() {
179
                return open;
180
        }
181

    
182
        public void close() {
183
        }
184

    
185
        public URI translateURI(URI uri) {
186
                return uri;
187
        }
188

    
189
        public NoData getNoDataValue() {
190
                NoData nodata = super.getNoDataValue();
191
                if(nodata != null)
192
                        nodata.setNoDataTransparent(false);
193
                return noData;
194
        }
195

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

    
205
        public Extent getView() {
206
                return viewRequest;
207
        }
208

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

    
214
        public double getHeight() {
215
                int n = getTileServer().getStruct().getNumberOfLevels() - 1;
216
                return getExtent().width() / getTileServer().getStruct().getPixelSizeByLevel(n);
217
        }
218

    
219
        public Object readBlock(int pos, int blockHeight, double scale)
220
                throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
221
                return null;
222
        }
223

    
224
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
225
                return null;
226
        }
227

    
228
        public int getBlockSize(){
229
                return 0;
230
        }
231

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

    
241
        public void setAffineTransform(AffineTransform t){
242
                super.setAffineTransform(t);
243
        }
244

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

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

    
257
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
258
                if (band >= getBandCount())
259
                        throw new BandAccessException("Wrong band");
260
                return 0;
261
        }
262

    
263
        public boolean isOverviewsSupported() {
264
                return true;
265
        }
266

    
267
        public boolean isReproyectable() {
268
                return true;
269
        }
270

    
271
        public String getProviderName() {
272
                return NAME;
273
        }
274

    
275
    public String getName() {
276
        return this.getParameters().getOSMLayerName();
277
    }
278

    
279
    @Override
280
    public String getFullName() {
281
        return this.uri.toString();
282
    }
283

    
284
        public OSMDataParameters getParameters() {
285
                return (OSMDataParameters)this.param;
286
        }
287

    
288
        public DataStoreTransparency getTransparency() {
289
                if(fileTransparency == null)
290
                        fileTransparency = new DataStoreTransparency(getColorInterpretation());
291
                return fileTransparency;
292
        }
293

    
294
        public ColorInterpretation getColorInterpretation() {
295
                if(super.getColorInterpretation() == null) {
296
                        super.setColorInterpretation(DataStoreColorInterpretation.createRGBInterpretation());
297
                }
298
                return super.getColorInterpretation();
299
        }
300

    
301
        public void setStatus(RasterProvider provider) {
302
                if(provider instanceof OSMProvider) {
303
                        //Not implemented yet
304
                }
305
        }
306

    
307
        public boolean isTimeSupported() {
308
                return true;
309
        }
310

    
311
        public boolean isTiled() {
312
                return true;
313
        }
314

    
315
        public TileServer getTileServer() {
316
                if(tileServer == null) {
317
                        DefaultRasterStore store = new DefaultRasterStore();
318
                        store.setProvider(this);
319
                        tileServer = new OSMTileServer(store,
320
                                        ((OSMDataParameters)param).getTileSuffix(),
321
                                        ((OSMDataParameters)param).getNumberOfLevels());
322
                }
323
                return tileServer;
324
        }
325

    
326
        @Override
327
        public void loadBuffer(SpiRasterQuery query)
328
                        throws ProcessInterruptedException, RasterDriverException {
329

    
330
        }
331

    
332
    /* (non-Javadoc)
333
     * @see org.gvsig.raster.impl.provider.RasterProvider#addFile(java.io.File)
334
     */
335
    @Override
336
    public void addFile(File file) throws InvalidSourceException {
337
        // Do nothing
338
    }
339

    
340
    /* (non-Javadoc)
341
     * @see org.gvsig.raster.impl.provider.RasterProvider#removeFile(java.io.File)
342
     */
343
    @Override
344
    public void removeFile(File file) {
345
        // Do nothing
346
    }
347

    
348
    /* (non-Javadoc)
349
     * @see org.gvsig.raster.impl.provider.AbstractRasterProvider#getFileSuffix()
350
     */
351
    @Override
352
    public String getFileSuffix() {
353
        return ((OSMDataParameters)getDataParameters()).getTileSuffix();
354
    }
355
}