Revision 6701 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
DefaultRasterLayer.java | ||
---|---|---|
27 | 27 |
import java.awt.geom.Point2D; |
28 | 28 |
import java.awt.image.BufferedImage; |
29 | 29 |
import java.io.File; |
30 |
import java.io.IOException; |
|
31 |
import java.util.Iterator; |
|
32 | 30 |
import java.util.List; |
33 |
import java.util.Map; |
|
34 |
import java.util.Map.Entry; |
|
35 | 31 |
import java.util.Observable; |
36 | 32 |
import java.util.Observer; |
37 | 33 |
import java.util.Set; |
38 | 34 |
import java.util.TreeSet; |
39 | 35 |
|
40 |
import javax.imageio.ImageIO; |
|
41 |
|
|
42 |
import org.apache.commons.lang3.StringEscapeUtils; |
|
43 | 36 |
import org.cresques.cts.ICoordTrans; |
44 |
import org.cresques.cts.IProjection; |
|
45 | 37 |
import org.slf4j.LoggerFactory; |
46 | 38 |
|
47 | 39 |
import org.gvsig.compat.print.PrintAttributes; |
48 | 40 |
import org.gvsig.fmap.dal.DataStore; |
49 | 41 |
import org.gvsig.fmap.dal.exception.DataException; |
50 | 42 |
import org.gvsig.fmap.dal.exception.ReadException; |
51 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
52 | 43 |
import org.gvsig.fmap.dal.raster.api.BandAttributeDescriptor; |
53 | 44 |
import org.gvsig.fmap.dal.raster.api.BandDescriptor; |
54 | 45 |
import org.gvsig.fmap.dal.raster.api.RasterQuery; |
... | ... | |
65 | 56 |
import org.gvsig.fmap.mapcontext.ViewPort; |
66 | 57 |
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException; |
67 | 58 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
68 |
import org.gvsig.fmap.mapcontext.layers.FLayerStatus; |
|
69 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
|
70 | 59 |
import org.gvsig.fmap.mapcontext.layers.FLyrDefault; |
71 | 60 |
import org.gvsig.fmap.mapcontext.layers.LayerInformationBuilder; |
72 | 61 |
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer; |
73 | 62 |
import org.gvsig.fmap.mapcontext.raster.api.RasterLegendChangedListener; |
74 | 63 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
75 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
|
|
64 |
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
|
|
76 | 65 |
import org.gvsig.metadata.exceptions.MetadataException; |
77 |
import org.gvsig.raster.lib.buffer.api.Band; |
|
78 |
import org.gvsig.raster.lib.buffer.api.BandInfo; |
|
79 |
import org.gvsig.raster.lib.buffer.api.BandTileManager; |
|
80 | 66 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
81 | 67 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
82 |
import org.gvsig.raster.lib.buffer.api.statistics.HistogramBand; |
|
83 | 68 |
import org.gvsig.raster.lib.buffer.api.statistics.Statistics; |
84 | 69 |
import org.gvsig.raster.lib.buffer.api.statistics.StatisticsBand; |
85 |
import org.gvsig.raster.lib.buffer.impl.TiledBandByte; |
|
86 |
import org.gvsig.raster.lib.buffer.impl.TiledBandDouble; |
|
87 |
import org.gvsig.raster.lib.buffer.impl.TiledBandFloat; |
|
88 |
import org.gvsig.raster.lib.buffer.impl.TiledBandInt; |
|
89 |
import org.gvsig.raster.lib.buffer.impl.TiledBandShort; |
|
90 |
import org.gvsig.raster.lib.buffer.impl.exceptions.CreateBandTypeException; |
|
91 |
import org.gvsig.raster.lib.legend.api.ColorInterpretation; |
|
92 | 70 |
import org.gvsig.raster.lib.legend.api.RasterLegend; |
93 | 71 |
import org.gvsig.tools.ToolsLocator; |
94 | 72 |
import org.gvsig.tools.dispose.DisposeUtils; |
... | ... | |
114 | 92 |
*/ |
115 | 93 |
public class DefaultRasterLayer extends FLyrDefault implements RasterLayer, RasterLegendChangedListener, Observer { |
116 | 94 |
|
117 |
final static private org.slf4j.Logger logger |
|
118 |
= LoggerFactory.getLogger(DefaultRasterLayer.class); |
|
95 |
final static private org.slf4j.Logger logger = LoggerFactory.getLogger(DefaultRasterLayer.class); |
|
119 | 96 |
|
120 | 97 |
private RasterStore store; |
121 | 98 |
private RasterLegend legend; |
122 |
//private Cancellable cancel; |
|
99 |
// private Cancellable cancel;
|
|
123 | 100 |
private RasterQuery baseQuery; |
124 | 101 |
private Statistics statistics; |
125 | 102 |
|
... | ... | |
141 | 118 |
|
142 | 119 |
/** |
143 | 120 |
* Creates a new DefaultRasterLayer |
121 |
* |
|
144 | 122 |
* @throws LoadLayerException |
145 | 123 |
*/ |
146 |
public DefaultRasterLayer() throws LoadLayerException{ |
|
124 |
public DefaultRasterLayer() throws LoadLayerException {
|
|
147 | 125 |
super(); |
148 | 126 |
} |
149 | 127 |
|
150 | 128 |
/** |
151 | 129 |
* Creates a new DefaultRasterLayer |
130 |
* |
|
152 | 131 |
* @param store |
153 | 132 |
* @param legend |
154 | 133 |
*/ |
... | ... | |
195 | 174 |
|
196 | 175 |
} |
197 | 176 |
|
198 |
public void setLegend(RasterLegend legend) throws LegendLayerException {
|
|
177 |
private void setLegend(RasterLegend legend) {
|
|
199 | 178 |
if (this.legend == legend) { |
200 | 179 |
return; |
201 | 180 |
} |
... | ... | |
205 | 184 |
RasterLegend oldLegend = this.legend; |
206 | 185 |
this.legend = legend; |
207 | 186 |
if (oldLegend != null) { |
208 |
//oldLegend.removeLegendListener( this); |
|
209 |
//oldLegend.deleteDrawingObserver(this); |
|
187 |
// oldLegend.removeLegendListener( this);
|
|
188 |
// oldLegend.deleteDrawingObserver(this);
|
|
210 | 189 |
} |
211 | 190 |
if (legend != null) { |
212 |
//this.legend.addDrawingObserver(this); |
|
213 |
//this.legend.addLegendListener( this); |
|
191 |
// this.legend.addDrawingObserver(this);
|
|
192 |
// this.legend.addLegendListener( this);
|
|
214 | 193 |
} |
215 |
// LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, this.legend); |
|
216 |
// e.setLayer(this); |
|
194 |
|
|
195 |
LegendChangedEvent e = LegendChangedEvent.createLegendChangedEvent(oldLegend, this.legend); |
|
196 |
// FIXME: cuando se arregle LegendChangeEvent para que admita capas |
|
197 |
// raster (y no solo las ClassifiableVectorial) descomentarizar la siguiente l?nea |
|
198 |
// e.setLayer(this); |
|
217 | 199 |
updateDrawVersion(); |
218 |
// callLegendChanged(e);
|
|
200 |
callLegendChanged(e); |
|
219 | 201 |
} |
220 | 202 |
|
221 |
|
|
222 | 203 |
/** |
223 | 204 |
* Devuelve la Leyenda de la capa. |
224 | 205 |
* |
... | ... | |
226 | 207 |
* @throws LegendLayerException |
227 | 208 |
*/ |
228 | 209 |
public RasterLegend getLegend() throws LegendLayerException { |
229 |
if(legend==null){
|
|
210 |
if (legend == null) {
|
|
230 | 211 |
initLegend(); |
231 | 212 |
} |
232 | 213 |
return legend; |
... | ... | |
252 | 233 |
|
253 | 234 |
/** |
254 | 235 |
* Initializes the legend if it is not initalized yet |
236 |
* |
|
255 | 237 |
* @throws LegendLayerException |
256 | 238 |
*/ |
257 | 239 |
private void initLegend() throws LegendLayerException { |
258 | 240 |
RasterLegend legend = null; |
259 | 241 |
if (this.legend == null) { |
260 | 242 |
// Should be done like this but must be corrected mapContextManager |
261 |
// MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
|
243 |
// MapContextManager mapContextManager = |
|
244 |
// MapContextLocator.getMapContextManager(); |
|
262 | 245 |
// Set the legend |
263 |
// IRasterLegend legend = (IRasterLegend) mapContextManager.getLegend(store); |
|
246 |
// IRasterLegend legend = (IRasterLegend) |
|
247 |
// mapContextManager.getLegend(store); |
|
264 | 248 |
|
265 | 249 |
// Should be moved to mapContextManager when RasterLegend implements |
266 | 250 |
// ILegend |
... | ... | |
294 | 278 |
// Esto es para cuando se crea una capa nueva con el fullExtent de ancho |
295 | 279 |
// y alto 0. |
296 | 280 |
if (rAux == null || rAux.isEmpty() || rAux.getMaximum(0) - rAux.getMinimum(0) == 0 |
297 |
&& rAux.getMaximum(1) - rAux.getMinimum(1) == 0) {
|
|
281 |
&& rAux.getMaximum(1) - rAux.getMinimum(1) == 0) { |
|
298 | 282 |
try { |
299 | 283 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
300 | 284 |
rAux = geomManager.createEnvelope(0, 0, 90, 90, SUBTYPES.GEOM2D); |
... | ... | |
312 | 296 |
} |
313 | 297 |
|
314 | 298 |
@Override |
315 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, |
|
316 |
Cancellable cancel, double scale) throws ReadException {
|
|
299 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale)
|
|
300 |
throws ReadException { |
|
317 | 301 |
if (legend == null) { |
318 | 302 |
try { |
319 | 303 |
initLegend(); |
... | ... | |
332 | 316 |
} |
333 | 317 |
|
334 | 318 |
RasterQuery rasterQuery = createRasterQuery(); |
335 |
Envelope reprojectedViewPortEnvelope = (Envelope) viewPort.getAdjustedEnvelope(); //.getEnvelope(); |
|
319 |
Envelope reprojectedViewPortEnvelope = (Envelope) viewPort.getAdjustedEnvelope(); // .getEnvelope();
|
|
336 | 320 |
ICoordTrans ct = getCoordTrans(); |
337 | 321 |
if (ct != null) { |
338 | 322 |
try { |
... | ... | |
345 | 329 |
} |
346 | 330 |
} |
347 | 331 |
|
348 |
double pixelSize = reprojectedViewPortEnvelope.getLength(0) / (double)viewPort.getImageWidth(); |
|
332 |
double pixelSize = reprojectedViewPortEnvelope.getLength(0) / (double) viewPort.getImageWidth();
|
|
349 | 333 |
|
350 | 334 |
rasterQuery.setPixelSize(pixelSize); |
351 | 335 |
rasterQuery.setClip(reprojectedViewPortEnvelope); |
352 | 336 |
|
337 |
// ColorInterpretation colorInterpretation = |
|
338 |
// legend.getColorInterpretation(); |
|
339 |
// String[] renderBands = |
|
340 |
// colorInterpretation.getValues();//.buildRenderBands(); |
|
341 |
// for (int i = 0; i < renderBands.length; i++) { |
|
342 |
// int band = colorInterpretation.getBand(renderBands[i]); |
|
343 |
// if(band>=0){ |
|
344 |
// rasterQuery.addBand(this.store.createBandQuery(band)); |
|
345 |
// } |
|
346 |
// } |
|
353 | 347 |
|
354 |
// ColorInterpretation colorInterpretation = legend.getColorInterpretation(); |
|
355 |
// String[] renderBands = colorInterpretation.getValues();//.buildRenderBands(); |
|
356 |
// for (int i = 0; i < renderBands.length; i++) { |
|
357 |
// int band = colorInterpretation.getBand(renderBands[i]); |
|
358 |
// if(band>=0){ |
|
359 |
// rasterQuery.addBand(this.store.createBandQuery(band)); |
|
360 |
// } |
|
361 |
// } |
|
362 |
|
|
363 | 348 |
RasterSet rasterSet = null; |
364 | 349 |
try { |
365 | 350 |
|
366 | 351 |
long tini = System.currentTimeMillis(); |
367 | 352 |
|
368 |
//TODO Task status should be used to cancel |
|
353 |
// TODO Task status should be used to cancel
|
|
369 | 354 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
370 |
SimpleTaskStatus taskStatus = manager.createDefaultSimpleTaskStatus("Draw "+getDataStore().getName());
|
|
355 |
SimpleTaskStatus taskStatus = manager.createDefaultSimpleTaskStatus("Draw " + getDataStore().getName());
|
|
371 | 356 |
|
372 | 357 |
rasterSet = getRasterStore().getRasterSet(rasterQuery); |
373 |
// BufferedImage img = rasterSet.getBufferedImage(); |
|
374 |
// if (img != null) { |
|
375 |
// try { |
|
376 |
// ImageIO.write(rasterSet.getBufferedImage(), "png32", new File("/tmp/"+rasterSet.getClass().getName()+".png")); |
|
377 |
// } catch (IOException e) { |
|
378 |
// // TODO Auto-generated catch block |
|
379 |
// e.printStackTrace(); |
|
380 |
// } |
|
381 |
// } |
|
358 |
// BufferedImage img = rasterSet.getBufferedImage(); |
|
359 |
// if (img != null) { |
|
360 |
// try { |
|
361 |
// ImageIO.write(rasterSet.getBufferedImage(), "png32", new |
|
362 |
// File("/tmp/"+rasterSet.getClass().getName()+".png")); |
|
363 |
// } catch (IOException e) { |
|
364 |
// // TODO Auto-generated catch block |
|
365 |
// e.printStackTrace(); |
|
366 |
// } |
|
367 |
// } |
|
382 | 368 |
if (!rasterSet.isEmpty()) { |
383 | 369 |
((RasterLegend) legend).draw(g, rasterSet, viewPort, taskStatus); |
384 | 370 |
|
... | ... | |
391 | 377 |
this.setError(e); |
392 | 378 |
throw new ReadException(getName(), e); |
393 | 379 |
} finally { |
394 |
if(rasterSet!=null){
|
|
380 |
if (rasterSet != null) {
|
|
395 | 381 |
DisposeUtils.dispose(rasterSet); |
396 | 382 |
rasterSet = null; |
397 | 383 |
} |
... | ... | |
400 | 386 |
} |
401 | 387 |
|
402 | 388 |
@Override |
403 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, |
|
404 |
double scale, PrintAttributes properties) throws ReadException {
|
|
389 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintAttributes properties)
|
|
390 |
throws ReadException { |
|
405 | 391 |
|
406 |
//TODO legend doesn't have print method |
|
392 |
// TODO legend doesn't have print method
|
|
407 | 393 |
throw new UnsupportedOperationException(); |
408 | 394 |
|
409 | 395 |
} |
... | ... | |
417 | 403 |
|
418 | 404 |
@Override |
419 | 405 |
public Object getMetadataID() throws MetadataException { |
420 |
return "Layer(" + this.getName() + "):" |
|
421 |
+ this.store.getMetadataID(); |
|
406 |
return "Layer(" + this.getName() + "):" + this.store.getMetadataID(); |
|
422 | 407 |
} |
423 | 408 |
|
424 | 409 |
@Override |
425 | 410 |
public void update(Observable observable, Object notification) { |
426 | 411 |
if (observable.equals(this.store)) { |
427 | 412 |
if (notification instanceof RasterStoreNotification) { |
428 |
RasterStoreNotification event |
|
429 |
= (RasterStoreNotification) notification; |
|
413 |
RasterStoreNotification event = (RasterStoreNotification) notification; |
|
430 | 414 |
if (event.getType() == RasterStoreNotification.AFTER_REFRESH |
431 |
|| event.getType() == RasterStoreNotification.SELECTION_CHANGE |
|
432 |
) { |
|
415 |
|| event.getType() == RasterStoreNotification.SELECTION_CHANGE) { |
|
433 | 416 |
this.updateDrawVersion(); |
434 | 417 |
} else if (event.getType() == RasterStoreNotification.RESOURCE_CHANGED) { |
435 | 418 |
this.setAvailable(false); |
... | ... | |
455 | 438 |
|
456 | 439 |
@Override |
457 | 440 |
public RasterQuery createRasterQuery() { |
458 |
if( this.baseQuery==null ) {
|
|
441 |
if (this.baseQuery == null) {
|
|
459 | 442 |
return this.getRasterStore().createRasterQuery(); |
460 | 443 |
} |
461 | 444 |
try { |
... | ... | |
503 | 486 |
|
504 | 487 |
@Override |
505 | 488 |
public void setLegend(ILegend legend) { |
506 |
if(legend instanceof RasterLegend){
|
|
507 |
this.legend = (RasterLegend)legend;
|
|
489 |
if (legend instanceof RasterLegend) {
|
|
490 |
setLegend((RasterLegend) legend);
|
|
508 | 491 |
} else { |
509 | 492 |
throw new IllegalArgumentException("The legend isn't a RasterLegend."); |
510 | 493 |
} |
... | ... | |
525 | 508 |
builder.title().labelkey("Data_source"); |
526 | 509 |
builder.property().labelkey("Source_type").value(store.getProviderName()); |
527 | 510 |
builder.property().labelkey("origen").value(store.getFullName()); |
528 |
if( store.getParameters() instanceof FilesystemStoreParameters ) {
|
|
529 |
File f = ((FilesystemStoreParameters)(store.getParameters())).getFile(); |
|
530 |
if( f!=null ) {
|
|
511 |
if (store.getParameters() instanceof FilesystemStoreParameters) {
|
|
512 |
File f = ((FilesystemStoreParameters) (store.getParameters())).getFile();
|
|
513 |
if (f != null) {
|
|
531 | 514 |
builder.property().labelkey("size").value("%d bytes", f.length()); |
532 | 515 |
} |
533 | 516 |
} |
534 | 517 |
|
535 |
builder.property().labelkey("_bands").value("%d",bands); |
|
536 |
builder.property().labelkey("transparency").value("%d",this.getTransparency()); |
|
518 |
builder.property().labelkey("_bands").value("%d", bands);
|
|
519 |
builder.property().labelkey("transparency").value("%d", this.getTransparency());
|
|
537 | 520 |
|
538 | 521 |
builder.title().labelkey("_Coordenadas_geograficas"); |
539 | 522 |
builder.envelope().value(envelope); |
... | ... | |
543 | 526 |
builder.text().asWKT(this.getProjection()); |
544 | 527 |
|
545 | 528 |
for (int i = 0; i < bands; i++) { |
546 |
builder.title().labelkey(i18nManager.getTranslation("_band")+" "+i);
|
|
529 |
builder.title().labelkey(i18nManager.getTranslation("_band") + " " + i);
|
|
547 | 530 |
|
548 | 531 |
BandDescriptor bandDescriptor = rasterStore.getBandDescriptor(i); |
549 | 532 |
|
550 | 533 |
builder.property().labelkey("_name").value(bandDescriptor.getName()); |
551 | 534 |
builder.property().labelkey("_description").value(bandDescriptor.getDescription()); |
552 |
builder.property().labelkey("_dataType").value(BufferLocator.getBufferManager().getTypeName(bandDescriptor.getDataType())); |
|
535 |
builder.property().labelkey("_dataType") |
|
536 |
.value(BufferLocator.getBufferManager().getTypeName(bandDescriptor.getDataType())); |
|
553 | 537 |
|
554 |
|
|
555 | 538 |
if (bandDescriptor.getNoData() != null) { |
556 | 539 |
builder.property().labelkey("_noData").value(bandDescriptor.getNoData().toString()); |
557 | 540 |
} else { |
... | ... | |
601 | 584 |
} |
602 | 585 |
} |
603 | 586 |
|
604 |
|
|
605 | 587 |
@Override |
606 | 588 |
public String getStatisticsString() { |
607 | 589 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
... | ... | |
618 | 600 |
int dataType = statisticsBand.getDataType(); |
619 | 601 |
builder.property().labelkey("_dataType").value(dataTypeString(dataType)); |
620 | 602 |
|
621 |
Long dataCount = statisticsBand.getBandLenght();
|
|
603 |
Long dataCount = statisticsBand.getBandLength();
|
|
622 | 604 |
builder.property().labelkey("_dataCount").value(dataCount.toString()); |
623 | 605 |
|
624 | 606 |
Long noDataValuesCount = statisticsBand.getNoDataValuesCount(); |
... | ... | |
660 | 642 |
return builder.toString(); |
661 | 643 |
} |
662 | 644 |
|
663 |
private Statistics getStatistics(SimpleTaskStatus status) {
|
|
664 |
if(statistics==null) {
|
|
645 |
public Statistics getStatistics(SimpleTaskStatus status) {
|
|
646 |
if (statistics == null) {
|
|
665 | 647 |
try { |
666 | 648 |
statistics = getRasterStore().getRasterSet().getStatistics(status); |
667 | 649 |
} catch (DataException e) { |
... | ... | |
671 | 653 |
return statistics; |
672 | 654 |
} |
673 | 655 |
|
674 |
public void recalculateStatistics(){ |
|
656 |
public void recalculateStatistics() {
|
|
675 | 657 |
this.statistics = null; |
676 | 658 |
} |
677 | 659 |
|
Also available in: Unified diff