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 @ 6302
History | View | Annotate | Download (12.9 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.Color; |
26 |
import java.awt.Graphics2D; |
27 |
import java.awt.image.BufferedImage; |
28 |
import java.awt.image.ColorModel; |
29 |
import java.awt.image.DirectColorModel; |
30 |
import java.awt.image.IndexColorModel; |
31 |
import java.util.ArrayList; |
32 |
import java.util.List; |
33 |
import java.util.Map.Entry; |
34 |
import java.util.Observable; |
35 |
import java.util.Observer; |
36 |
import java.util.Set; |
37 |
import java.util.TreeSet; |
38 |
|
39 |
import org.cresques.cts.ICoordTrans; |
40 |
import org.gvsig.compat.print.PrintAttributes; |
41 |
import org.gvsig.fmap.dal.DataStore; |
42 |
import org.gvsig.fmap.dal.exception.DataException; |
43 |
import org.gvsig.fmap.dal.exception.ReadException; |
44 |
import org.gvsig.fmap.dal.file.jimi.JimiRasterStoreProvider; |
45 |
import org.gvsig.fmap.dal.file.jimi.MemoryImage; |
46 |
import org.gvsig.fmap.dal.raster.api.RasterQuery; |
47 |
import org.gvsig.fmap.dal.raster.api.RasterSet; |
48 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
49 |
import org.gvsig.fmap.dal.raster.api.RasterStoreNotification; |
50 |
import org.gvsig.fmap.dal.raster.impl.DefaultRasterStore; |
51 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
52 |
import org.gvsig.fmap.geom.GeometryLocator; |
53 |
import org.gvsig.fmap.geom.GeometryManager; |
54 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
55 |
import org.gvsig.fmap.geom.primitive.Envelope; |
56 |
import org.gvsig.fmap.mapcontext.ViewPort; |
57 |
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException; |
58 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
59 |
import org.gvsig.fmap.mapcontext.layers.FLyrDefault; |
60 |
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer; |
61 |
import org.gvsig.fmap.mapcontext.raster.api.RasterLegendChangedListener; |
62 |
import org.gvsig.metadata.exceptions.MetadataException; |
63 |
import org.gvsig.raster.lib.buffer.api.Band; |
64 |
import org.gvsig.raster.lib.buffer.api.BandInfo; |
65 |
import org.gvsig.raster.lib.legend.api.ColorTable; |
66 |
import org.gvsig.raster.lib.legend.api.ColorTableClass; |
67 |
import org.gvsig.raster.lib.legend.api.RasterLegend; |
68 |
import org.gvsig.tools.ToolsLocator; |
69 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
70 |
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
71 |
import org.gvsig.tools.exception.BaseException; |
72 |
import org.gvsig.tools.task.Cancellable; |
73 |
import org.gvsig.tools.task.SimpleTaskStatus; |
74 |
import org.gvsig.tools.task.TaskStatusManager; |
75 |
import org.slf4j.LoggerFactory; |
76 |
|
77 |
/**
|
78 |
* Capa b?sica Raster.
|
79 |
*
|
80 |
*/
|
81 |
public class DefaultRasterLayer extends FLyrDefault implements RasterLayer, RasterLegendChangedListener, Observer { |
82 |
|
83 |
final static private org.slf4j.Logger logger |
84 |
= LoggerFactory.getLogger(DefaultRasterLayer.class); |
85 |
|
86 |
private RasterStore store;
|
87 |
private RasterLegend legend;
|
88 |
//TODO remove labeled
|
89 |
//private boolean isLabeled;
|
90 |
//protected ILabelingStrategy strategy;
|
91 |
//private Cancellable cancel;
|
92 |
private RasterQuery baseQuery;
|
93 |
|
94 |
/**
|
95 |
* Creates a new DefaultRasterLayer
|
96 |
* @throws LoadLayerException
|
97 |
*/
|
98 |
public DefaultRasterLayer() throws LoadLayerException{ |
99 |
super();
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* Creates a new DefaultRasterLayer
|
104 |
* @param store
|
105 |
* @param legend
|
106 |
*/
|
107 |
public DefaultRasterLayer(RasterStore store, RasterLegend legend){
|
108 |
super();
|
109 |
this.store=store;
|
110 |
this.legend=legend;
|
111 |
} |
112 |
|
113 |
|
114 |
public void setLegend(RasterLegend legend) throws LegendLayerException { |
115 |
if (this.legend == legend) { |
116 |
return;
|
117 |
} |
118 |
if (this.legend != null && this.legend.equals(legend)) { |
119 |
return;
|
120 |
} |
121 |
RasterLegend oldLegend = this.legend;
|
122 |
this.legend = legend;
|
123 |
if (oldLegend != null) { |
124 |
//oldLegend.removeLegendListener( this);
|
125 |
//oldLegend.deleteDrawingObserver(this);
|
126 |
} |
127 |
if (legend != null) { |
128 |
//this.legend.addDrawingObserver(this);
|
129 |
//this.legend.addLegendListener( this);
|
130 |
} |
131 |
// LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, this.legend);
|
132 |
// e.setLayer(this);
|
133 |
updateDrawVersion(); |
134 |
// callLegendChanged(e);
|
135 |
} |
136 |
|
137 |
/**
|
138 |
* Devuelve la Leyenda de la capa.
|
139 |
*
|
140 |
* @return Leyenda.
|
141 |
*/
|
142 |
public RasterLegend getLegend() {
|
143 |
|
144 |
return legend;
|
145 |
} |
146 |
|
147 |
@Override
|
148 |
public DataStore getDataStore() {
|
149 |
return this.store; |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public void setDataStore(DataStore dataStore) throws LoadLayerException { |
154 |
if (this.store!=null){ |
155 |
throw new LoadLayerException("Store already created"); |
156 |
}else{
|
157 |
this.store=(RasterStore)dataStore;
|
158 |
initLegend(); |
159 |
} |
160 |
} |
161 |
|
162 |
/**
|
163 |
* Initializes the legend if it is not initalized yet
|
164 |
* @throws LegendLayerException
|
165 |
*/
|
166 |
private void initLegend() throws LegendLayerException{ |
167 |
RasterLegend legend=null;
|
168 |
if(this.legend==null){ |
169 |
//Should be done like this but must be corrected mapContextManager
|
170 |
// MapContextManager mapContextManager
|
171 |
// = MapContextLocator.getMapContextManager();
|
172 |
//Set the legend
|
173 |
// IRasterLegend legend
|
174 |
// = (IRasterLegend) mapContextManager.getLegend(store);
|
175 |
|
176 |
|
177 |
//Should be moved to mapContextManager when RasterLegend implements ILegend
|
178 |
if (legend == null) { |
179 |
try {
|
180 |
legend = (RasterLegend) store.invokeDynMethod(RasterStore.DYNMETHOD_GETLEGEND_NAME, null);
|
181 |
} catch (DynMethodNotSupportedException e) {
|
182 |
logger.debug("This store {} does not provide a legend.",
|
183 |
store.getName()); |
184 |
} catch (DynMethodException e) {
|
185 |
logger.error( |
186 |
"Can't load the specific legend provided for the store {}.",
|
187 |
store.getName(), e); |
188 |
} |
189 |
} |
190 |
|
191 |
|
192 |
if (legend == null) { |
193 |
throw new LegendLayerException(this.getName()); |
194 |
} |
195 |
|
196 |
this.setLegend(legend);
|
197 |
} |
198 |
} |
199 |
|
200 |
@Override
|
201 |
public Envelope getFullEnvelope() throws ReadException { |
202 |
Envelope rAux; |
203 |
try {
|
204 |
rAux = getRasterStore().getEnvelope(); |
205 |
} catch (BaseException e) {
|
206 |
throw new ReadException(getName(), e); |
207 |
} |
208 |
|
209 |
// Esto es para cuando se crea una capa nueva con el fullExtent de ancho
|
210 |
// y alto 0.
|
211 |
if (rAux == null || rAux.isEmpty() || rAux.getMaximum(0) - rAux.getMinimum(0) == 0 |
212 |
&& rAux.getMaximum(1) - rAux.getMinimum(1) == 0) { |
213 |
try {
|
214 |
GeometryManager geomManager |
215 |
= GeometryLocator.getGeometryManager(); |
216 |
rAux |
217 |
= geomManager.createEnvelope(0, 0, 90, 90, SUBTYPES.GEOM2D); |
218 |
} catch (CreateEnvelopeException e) {
|
219 |
logger.error("Error creating the envelope", e);
|
220 |
e.printStackTrace(); |
221 |
} |
222 |
} |
223 |
// Si existe reproyecci?n, reproyectar el extent
|
224 |
ICoordTrans ct = getCoordTrans(); |
225 |
if (ct != null) { |
226 |
rAux = rAux.convert(ct); |
227 |
} |
228 |
return rAux;
|
229 |
} |
230 |
|
231 |
@Override
|
232 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, |
233 |
Cancellable cancel, double scale) throws ReadException { |
234 |
if (legend == null) { |
235 |
return;
|
236 |
} |
237 |
|
238 |
if (!this.isWithinScale(scale)) { |
239 |
return;
|
240 |
} |
241 |
if (cancel.isCanceled()) {
|
242 |
return;
|
243 |
} |
244 |
|
245 |
RasterQuery rasterQuery = createRasterQuery(); |
246 |
|
247 |
try {
|
248 |
|
249 |
long tini = System.currentTimeMillis(); |
250 |
|
251 |
//TODO Task status should be used to cancel
|
252 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
253 |
SimpleTaskStatus taskStatus = manager.createDefaultSimpleTaskStatus("Draw "+getDataStore().getName());
|
254 |
|
255 |
((RasterLegend) legend).draw(g, getRasterStore().getRasterSet(rasterQuery), viewPort, taskStatus); |
256 |
|
257 |
logger.debug("Layer " + this.getName() + " drawn in " |
258 |
+ (System.currentTimeMillis() - tini) + " milliseconds."); |
259 |
|
260 |
} catch (DataException e) {
|
261 |
this.setAvailable(false); |
262 |
this.setError(e);
|
263 |
throw new ReadException(getName(), e); |
264 |
} finally {
|
265 |
|
266 |
} |
267 |
|
268 |
} |
269 |
|
270 |
@Override
|
271 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, |
272 |
double scale, PrintAttributes properties) throws ReadException { |
273 |
|
274 |
//TODO legend doesn't have print method
|
275 |
throw new UnsupportedOperationException(); |
276 |
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
public Set<RasterStore> getMetadataChildren() throws MetadataException { |
281 |
Set<RasterStore> ret = new TreeSet<RasterStore>(); |
282 |
ret.add(this.store);
|
283 |
return ret;
|
284 |
} |
285 |
|
286 |
@Override
|
287 |
public Object getMetadataID() throws MetadataException { |
288 |
return "Layer(" + this.getName() + "):" |
289 |
+ this.store.getMetadataID();
|
290 |
} |
291 |
|
292 |
// @Override
|
293 |
// public boolean isLabeled() {
|
294 |
// return isLabeled;
|
295 |
// }
|
296 |
//
|
297 |
// @Override
|
298 |
// public void setIsLabeled(boolean isLabeled) {
|
299 |
// this.isLabeled = isLabeled;
|
300 |
// }
|
301 |
//
|
302 |
// @Override
|
303 |
// public ILabelingStrategy getLabelingStrategy() {
|
304 |
// return strategy;
|
305 |
// }
|
306 |
//
|
307 |
// @Override
|
308 |
// public void setLabelingStrategy(ILabelingStrategy strategy) {
|
309 |
// this.strategy = strategy;
|
310 |
// if (strategy == null) {
|
311 |
// return;
|
312 |
// }
|
313 |
// strategy.setLayer(this);
|
314 |
// updateDrawVersion();
|
315 |
// }
|
316 |
//
|
317 |
// @Override
|
318 |
// public void drawLabels(BufferedImage image, Graphics2D g, ViewPort viewPort,
|
319 |
// Cancellable cancel, double scale, double dpi) throws ReadException {
|
320 |
// if (strategy != null && isWithinScale(scale)) {
|
321 |
// strategy.draw(image, g, scale, viewPort, cancel, dpi);
|
322 |
// }
|
323 |
// }
|
324 |
//
|
325 |
// @Override
|
326 |
// public void printLabels(Graphics2D g, ViewPort viewPort, Cancellable cancel,
|
327 |
// double scale, PrintAttributes properties) throws ReadException {
|
328 |
// if (strategy != null) {
|
329 |
// strategy.print(g, scale, viewPort, cancel, properties);
|
330 |
// }
|
331 |
// }
|
332 |
|
333 |
|
334 |
@Override
|
335 |
public void update(Observable observable, Object notification) { |
336 |
if (observable.equals(this.store)) { |
337 |
if (notification instanceof RasterStoreNotification) { |
338 |
RasterStoreNotification event |
339 |
= (RasterStoreNotification) notification; |
340 |
if (event.getType() == RasterStoreNotification.AFTER_REFRESH
|
341 |
|| event.getType() == RasterStoreNotification.SELECTION_CHANGE |
342 |
) { |
343 |
this.updateDrawVersion();
|
344 |
} else if (event.getType() == RasterStoreNotification.RESOURCE_CHANGED) { |
345 |
this.setAvailable(false); |
346 |
} |
347 |
} |
348 |
} |
349 |
} |
350 |
|
351 |
// @Override
|
352 |
// public boolean symbolChanged(SymbolLegendEvent e) {
|
353 |
// throw new UnsupportedOperationException("Raster layers don't have default symbol");
|
354 |
//// this.updateDrawVersion();
|
355 |
//// LegendChangedEvent ev = LegendChangedEvent.createLegendChangedEvent(legend, e);
|
356 |
//// this.callLegendChanged(ev);
|
357 |
//// return true;
|
358 |
// }
|
359 |
//
|
360 |
// @Override
|
361 |
// public void legendCleared(LegendClearEvent event) {
|
362 |
// this.updateDrawVersion();
|
363 |
// LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(legend,event);
|
364 |
// this.callLegendChanged(e);
|
365 |
// }
|
366 |
|
367 |
@Override
|
368 |
public RasterStore getRasterStore() {
|
369 |
return this.store; |
370 |
} |
371 |
|
372 |
@Override
|
373 |
public void setBaseQuery(RasterQuery baseQuery) { |
374 |
this.baseQuery = baseQuery;
|
375 |
} |
376 |
|
377 |
@Override
|
378 |
public RasterQuery getBaseQuery() {
|
379 |
return this.baseQuery; |
380 |
} |
381 |
|
382 |
@Override
|
383 |
public RasterQuery createRasterQuery() {
|
384 |
if( this.baseQuery==null ) { |
385 |
return this.getRasterStore().createRasterQuery(); |
386 |
} |
387 |
try {
|
388 |
return (RasterQuery) baseQuery.clone();
|
389 |
} catch (CloneNotSupportedException ex) { |
390 |
throw new RuntimeException(ex); |
391 |
} |
392 |
} |
393 |
|
394 |
@Override
|
395 |
protected void doDispose() throws BaseException { |
396 |
// TODO Do nothing
|
397 |
|
398 |
} |
399 |
|
400 |
} |