Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.fmap.mapcontext.raster / org.gvsig.fmap.mapcontext.raster.impl / src / main / java / org / gvsig / fmap / mapcontext / raster / impl / DefaultRasterLayer.java @ 6288

History | View | Annotate | Download (12.8 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2016 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.mapcontext.raster.impl;
24

    
25
import java.awt.Graphics2D;
26
import java.awt.image.BandCombineOp;
27
import java.awt.image.BufferedImage;
28
import java.util.Observable;
29
import java.util.Observer;
30
import java.util.Set;
31
import java.util.TreeSet;
32

    
33
import org.cresques.cts.ICoordTrans;
34
import org.gvsig.compat.print.PrintAttributes;
35
import org.gvsig.fmap.dal.DataStore;
36
import org.gvsig.fmap.dal.exception.DataException;
37
import org.gvsig.fmap.dal.exception.ReadException;
38
import org.gvsig.fmap.dal.raster.api.RasterQuery;
39
import org.gvsig.fmap.dal.raster.api.RasterStore;
40
import org.gvsig.fmap.dal.raster.api.RasterStoreNotification;
41
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
42
import org.gvsig.fmap.geom.GeometryLocator;
43
import org.gvsig.fmap.geom.GeometryManager;
44
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
45
import org.gvsig.fmap.geom.primitive.Envelope;
46
import org.gvsig.fmap.mapcontext.MapContextLocator;
47
import org.gvsig.fmap.mapcontext.MapContextManager;
48
import org.gvsig.fmap.mapcontext.ViewPort;
49
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException;
50
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
51
import org.gvsig.fmap.mapcontext.layers.FLyrDefault;
52
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer;
53
import org.gvsig.fmap.mapcontext.raster.api.RasterLegendChangedListener;
54
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
55
import org.gvsig.fmap.mapcontext.rendering.legend.IRasterLegend;
56
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
57
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
58
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendContentsChangedListener;
59
import org.gvsig.metadata.exceptions.MetadataException;
60
import org.gvsig.raster.lib.buffer.api.Buffer;
61
import org.gvsig.raster.lib.legend.api.ColorInterpretation;
62
import org.gvsig.raster.lib.legend.api.RasterLegend;
63
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
64
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
65
import org.gvsig.tools.ToolsLocator;
66
import org.gvsig.tools.dynobject.exception.DynMethodException;
67
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
68
import org.gvsig.tools.exception.BaseException;
69
import org.gvsig.tools.task.Cancellable;
70
import org.gvsig.tools.task.SimpleTaskStatus;
71
import org.gvsig.tools.task.TaskStatusManager;
72
import org.slf4j.LoggerFactory;
73

    
74
/**
75
 * Capa b?sica Raster.
76
 *
77
 */
78
public class DefaultRasterLayer extends FLyrDefault implements RasterLayer, RasterLegendChangedListener, Observer {
79

    
80
    final static private org.slf4j.Logger logger
81
    = LoggerFactory.getLogger(DefaultRasterLayer.class);
82

    
83
    private RasterStore store;
84
    private RasterLegend legend;
85
    //TODO remove labeled
86
    //private boolean isLabeled;
87
    //protected ILabelingStrategy strategy;
88
    //private Cancellable cancel;
89
    private RasterQuery baseQuery;
90

    
91
    /**
92
     * Creates a new DefaultRasterLayer
93
     * @param store
94
     * @param legend
95
     * @throws
96
     */
97
    public DefaultRasterLayer() throws LoadLayerException{
98
        super();
99
    }
100

    
101
    /**
102
     * Creates a new DefaultRasterLayer
103
     * @param store
104
     * @param legend
105
     */
106
    public DefaultRasterLayer(RasterStore store, RasterLegend legend){
107
        super();
108
        this.store=store;
109
        this.legend=legend;
110
    }
111

    
112

    
113
    public void setLegend(RasterLegend legend) throws LegendLayerException {
114
        if (this.legend == legend) {
115
            return;
116
        }
117
        if (this.legend != null && this.legend.equals(legend)) {
118
            return;
119
        }
120
        RasterLegend oldLegend = this.legend;
121
        this.legend = legend;
122
        if (oldLegend != null) {
123
            //oldLegend.removeLegendListener( this);
124
            //oldLegend.deleteDrawingObserver(this);
125
        }
126
        if (legend != null) {
127
            //this.legend.addDrawingObserver(this);
128
            //this.legend.addLegendListener( this);
129
        }
130
//        LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, this.legend);
131
//        e.setLayer(this);
132
        updateDrawVersion();
133
//        callLegendChanged(e);
134
    }
135

    
136
    /**
137
     * Devuelve la Leyenda de la capa.
138
     *
139
     * @return Leyenda.
140
     */
141
    public RasterLegend getLegend() {
142

    
143
        return legend;
144
    }
145

    
146
    @Override
147
    public DataStore getDataStore() {
148
        return this.store;
149
    }
150

    
151
    @Override
152
    public void setDataStore(DataStore dataStore) throws LoadLayerException {
153
        if (this.store!=null){
154
            throw new LoadLayerException("Store already created");
155
        }else{
156
            this.store=(RasterStore)dataStore;
157
            initLegend();
158
        }
159
    }
160

    
161
    /**
162
     * Initializes the legend if it is not initalized yet
163
     * @throws LegendLayerException
164
     */
165
    private void initLegend() throws LegendLayerException{
166
        RasterLegend legend=null;
167
        if(legend==null){
168
            //Should be done like this but must be corrected mapContextManager
169
//          MapContextManager mapContextManager
170
//          = MapContextLocator.getMapContextManager();
171
          //Set the legend
172
//          IRasterLegend legend
173
//                  = (IRasterLegend) mapContextManager.getLegend(store);
174

    
175
            //Should be moved to mapContextManager
176
            try {
177
                legend = (RasterLegend) store.invokeDynMethod("getLegend", null);
178
            } catch (DynMethodNotSupportedException e) {
179
                logger.debug("This store {} does not provide a legend.",
180
                    store.getName());
181
            } catch (DynMethodException e) {
182
                logger.error(
183
                        "Can't load the specific legend provided for the store {}.",
184
                        store.getName(), e);
185
            }
186

    
187

    
188
            if (legend == null) {
189
                throw new LegendLayerException(this.getName());
190
            }
191

    
192
            this.setLegend(legend);
193
        }
194
    }
195

    
196
    @Override
197
    public Envelope getFullEnvelope() throws ReadException {
198
        Envelope rAux;
199
        try {
200
            rAux = getRasterStore().getEnvelope();
201
        } catch (BaseException e) {
202
            throw new ReadException(getName(), e);
203
        }
204

    
205
        // Esto es para cuando se crea una capa nueva con el fullExtent de ancho
206
        // y alto 0.
207
        if (rAux == null || rAux.isEmpty() || rAux.getMaximum(0) - rAux.getMinimum(0) == 0
208
                && rAux.getMaximum(1) - rAux.getMinimum(1) == 0) {
209
            try {
210
                GeometryManager geomManager
211
                = GeometryLocator.getGeometryManager();
212
                rAux
213
                        = geomManager.createEnvelope(0, 0, 90, 90, SUBTYPES.GEOM2D);
214
            } catch (CreateEnvelopeException e) {
215
                logger.error("Error creating the envelope", e);
216
                e.printStackTrace();
217
            }
218
        }
219
        // Si existe reproyecci?n, reproyectar el extent
220
        ICoordTrans ct = getCoordTrans();
221
        if (ct != null) {
222
            rAux = rAux.convert(ct);
223
        }
224
        return rAux;
225
    }
226

    
227
    @Override
228
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
229
        Cancellable cancel, double scale) throws ReadException {
230
        if (legend == null) {
231
            return;
232
        }
233

    
234
        if (!this.isWithinScale(scale)) {
235
            return;
236
        }
237
        if (cancel.isCanceled()) {
238
            return;
239
        }
240

    
241
        RasterQuery rasterQuery = createRasterQuery();
242

    
243
        try {
244

    
245
            long tini = System.currentTimeMillis();
246

    
247
            //TODO Task status should be used to cancel
248
            TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
249
            SimpleTaskStatus taskStatus = manager.createDefaultSimpleTaskStatus("Draw "+getDataStore().getName());
250

    
251
            ((RasterLegend) legend).draw(g, getRasterStore().getRasterSet(rasterQuery), viewPort, taskStatus);
252

    
253
            logger.debug("Layer " + this.getName() + " drawn in "
254
                    + (System.currentTimeMillis() - tini) + " milliseconds.");
255

    
256
        } catch (DataException e) {
257
            this.setAvailable(false);
258
            this.setError(e);
259
            throw new ReadException(getName(), e);
260
        } finally {
261

    
262
        }
263

    
264
    }
265

    
266
    @Override
267
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
268
        double scale, PrintAttributes properties) throws ReadException {
269

    
270
        //TODO legend doesn't have print method
271
        throw new UnsupportedOperationException();
272

    
273
    }
274

    
275
    @Override
276
    public Set<RasterStore> getMetadataChildren() throws MetadataException {
277
        Set<RasterStore> ret = new TreeSet<RasterStore>();
278
        ret.add(this.store);
279
        return ret;
280
    }
281

    
282
    @Override
283
    public Object getMetadataID() throws MetadataException {
284
        return "Layer(" + this.getName() + "):"
285
            + this.store.getMetadataID();
286
    }
287

    
288
//    @Override
289
//    public boolean isLabeled() {
290
//        return isLabeled;
291
//    }
292
//
293
//    @Override
294
//    public void setIsLabeled(boolean isLabeled) {
295
//        this.isLabeled = isLabeled;
296
//    }
297
//
298
//    @Override
299
//    public ILabelingStrategy getLabelingStrategy() {
300
//        return strategy;
301
//    }
302
//
303
//    @Override
304
//    public void setLabelingStrategy(ILabelingStrategy strategy) {
305
//        this.strategy = strategy;
306
//        if (strategy == null) {
307
//            return;
308
//        }
309
//        strategy.setLayer(this);
310
//        updateDrawVersion();
311
//    }
312
//
313
//    @Override
314
//    public void drawLabels(BufferedImage image, Graphics2D g, ViewPort viewPort,
315
//        Cancellable cancel, double scale, double dpi) throws ReadException {
316
//        if (strategy != null && isWithinScale(scale)) {
317
//            strategy.draw(image, g, scale, viewPort, cancel, dpi);
318
//        }
319
//    }
320
//
321
//    @Override
322
//    public void printLabels(Graphics2D g, ViewPort viewPort, Cancellable cancel,
323
//        double scale, PrintAttributes properties) throws ReadException {
324
//        if (strategy != null) {
325
//            strategy.print(g, scale, viewPort, cancel, properties);
326
//        }
327
//    }
328

    
329

    
330
    @Override
331
    public void update(Observable observable, Object notification) {
332
        if (observable.equals(this.store)) {
333
            if (notification instanceof RasterStoreNotification) {
334
                RasterStoreNotification event
335
                        = (RasterStoreNotification) notification;
336
                if (event.getType() == RasterStoreNotification.AFTER_REFRESH
337
                        || event.getType() == RasterStoreNotification.SELECTION_CHANGE
338
                        ) {
339
                    this.updateDrawVersion();
340
                } else if (event.getType() == RasterStoreNotification.RESOURCE_CHANGED) {
341
                    this.setAvailable(false);
342
                }
343
            }
344
        }
345
    }
346

    
347
//    @Override
348
//    public boolean symbolChanged(SymbolLegendEvent e) {
349
//        throw new UnsupportedOperationException("Raster layers don't have default symbol");
350
////        this.updateDrawVersion();
351
////        LegendChangedEvent ev = LegendChangedEvent.createLegendChangedEvent(legend, e);
352
////        this.callLegendChanged(ev);
353
////        return true;
354
//    }
355
//
356
//    @Override
357
//    public void legendCleared(LegendClearEvent event) {
358
//        this.updateDrawVersion();
359
//        LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(legend,event);
360
//        this.callLegendChanged(e);
361
//    }
362

    
363
    @Override
364
    public RasterStore getRasterStore() {
365
        return this.store;
366
    }
367

    
368
    @Override
369
    public void setBaseQuery(RasterQuery baseQuery) {
370
        this.baseQuery = baseQuery;
371
    }
372

    
373
    @Override
374
    public RasterQuery getBaseQuery() {
375
        return this.baseQuery;
376
    }
377

    
378
    @Override
379
    public RasterQuery createRasterQuery() {
380
        if( this.baseQuery==null ) {
381
            return this.getRasterStore().createRasterQuery();
382
        }
383
        try {
384
            return (RasterQuery) baseQuery.clone();
385
        } catch (CloneNotSupportedException ex) {
386
            throw new RuntimeException(ex);
387
        }
388
    }
389

    
390
    @Override
391
    protected void doDispose() throws BaseException {
392
        // TODO Do nothing
393

    
394
    }
395

    
396
}