/
RasterDataNode.java
2680 lines (2467 loc) · 111 KB
/
RasterDataNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2012 Brockmann Consult GmbH (info@brockmann-consult.de)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see http://www.gnu.org/licenses/
*/
package org.esa.snap.core.datamodel;
import com.bc.ceres.core.Assert;
import com.bc.ceres.core.ProgressMonitor;
import com.bc.ceres.core.SubProgressMonitor;
import com.bc.ceres.glevel.MultiLevelImage;
import com.bc.ceres.glevel.MultiLevelModel;
import com.bc.ceres.glevel.support.DefaultMultiLevelImage;
import com.bc.ceres.glevel.support.DefaultMultiLevelModel;
import com.bc.ceres.glevel.support.DefaultMultiLevelSource;
import com.bc.ceres.glevel.support.GenericMultiLevelSource;
import com.bc.ceres.jai.operator.InterpretationType;
import com.bc.ceres.jai.operator.ReinterpretDescriptor;
import com.bc.ceres.jai.operator.ScalingType;
import org.esa.snap.core.dataio.ProductReader;
import org.esa.snap.core.dataio.ProductWriter;
import org.esa.snap.core.dataop.barithm.BandArithmetic;
import org.esa.snap.core.image.ImageManager;
import org.esa.snap.core.image.SingleBandedOpImage;
import org.esa.snap.core.transform.MathTransform2D;
import org.esa.snap.core.util.BitRaster;
import org.esa.snap.core.util.Debug;
import org.esa.snap.core.util.ObjectUtils;
import org.esa.snap.core.util.ProductUtils;
import org.esa.snap.core.util.StringUtils;
import org.esa.snap.core.util.SystemUtils;
import org.esa.snap.core.util.jai.SingleBandedSampleModel;
import org.esa.snap.core.util.math.Histogram;
import org.esa.snap.core.util.math.IndexValidator;
import org.esa.snap.core.util.math.MathUtils;
import org.esa.snap.core.util.math.Quantizer;
import org.esa.snap.core.util.math.Range;
import org.esa.snap.runtime.Config;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.NoninvertibleTransformException;
import javax.media.jai.ImageLayout;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.ROI;
import java.awt.Dimension;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.SampleModel;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.prefs.BackingStoreException;
/**
* The <code>RasterDataNode</code> class ist the abstract base class for all objects in the product package that contain
* rasterized data. i.e. <code>Band</code> and <code>TiePointGrid</code>. It unifies the access to raster data in the
* product model. A raster is considered as a rectangular raw data array with a fixed width and height. A raster data
* node can scale its raw raster data samples in order to return geophysically meaningful pixel values.
*
* @author Norman Fomferra
* @see #getRasterData()
* @see #getRasterWidth()
* @see #getRasterHeight()
* @see #isScalingApplied()
* @see #isLog10Scaled()
* @see #getScalingFactor()
* @see #getScalingOffset()
*/
public abstract class RasterDataNode extends DataNode implements Scaling, SceneTransformProvider {
public static final String PROPERTY_NAME_IMAGE_INFO = "imageInfo";
public static final String PROPERTY_NAME_LOG_10_SCALED = "log10Scaled";
public static final String PROPERTY_NAME_SCALING_FACTOR = "scalingFactor";
public static final String PROPERTY_NAME_SCALING_OFFSET = "scalingOffset";
public static final String PROPERTY_NAME_NO_DATA_VALUE = "noDataValue";
public static final String PROPERTY_NAME_NO_DATA_VALUE_USED = "noDataValueUsed";
public static final String PROPERTY_NAME_VALID_PIXEL_EXPRESSION = "validPixelExpression";
public static final String PROPERTY_NAME_GEO_CODING = "geoCoding";
public static final String PROPERTY_NAME_TIME_CODING = "timeCoding";
public static final String PROPERTY_NAME_STX = "stx";
public static final String PROPERTY_NAME_ANCILLARY_VARIABLES = "ancillaryVariables";
public static final String PROPERTY_NAME_ANCILLARY_RELATIONS = "ancillaryRelations";
public static final String PROPERTY_NAME_IMAGE_TO_MODEL_TRANSFORM = "imageToModelTransform";
public static final String PROPERTY_NAME_MODEL_TO_SCENE_TRANSFORM = "modelToSceneTransform";
public static final String PROPERTY_NAME_SCENE_TO_MODEL_TRANSFORM = "sceneToModelTransform";
/**
* Number of bytes used for internal read buffer.
*/
private static final int READ_BUFFER_MAX_SIZE = 8 * 1024 * 1024; // 8 MB
/**
* Text returned by the <code>{@link #getPixelString(int, int)}</code> method if no data is available at the given pixel
* position.
*/
public static final String NO_DATA_TEXT = "NaN"; /*I18N*/
/**
* Text returned by the <code>{@link #getPixelString(int, int)}</code> method if no data is available at the given pixel
* position.
*/
public static final String INVALID_POS_TEXT = "Invalid pos."; /*I18N*/
/**
* Text returned by the <code>{@link #getPixelString(int, int)}</code> method if an I/O error occurred while pixel data was
* reloaded.
*/
public static final String IO_ERROR_TEXT = "I/O error"; /*I18N*/
private double scalingFactor;
private double scalingOffset;
private boolean log10Scaled;
private boolean scalingApplied;
private boolean noDataValueUsed;
private ProductData noData;
private double geophysicalNoDataValue; // invariant, depending on _noData
private String validPixelExpression;
private GeoCoding geoCoding;
private TimeCoding timeCoding;
private AffineTransform imageToModelTransform;
private MathTransform2D modelToSceneTransform;
private MathTransform2D sceneToModelTransform;
private Stx stx;
private ImageInfo imageInfo;
private final ProductNodeGroup<Mask> overlayMasks;
private Pointing pointing;
private MultiLevelImage sourceImage;
private MultiLevelImage geophysicalImage;
private MultiLevelImage validMaskImage;
private ROI validMaskROI;
private ProductNodeGroup<RasterDataNode> ancillaryVariables;
private String[] ancillaryRelations;
private AncillaryBandRemover ancillaryBandRemover;
/**
* Constructs an object of type <code>RasterDataNode</code>.
*
* @param name the name of the new object
* @param dataType the data type used by the raster, must be one of the multiple <code>ProductData.TYPE_<i>X</i></code>
* constants, with the exception of <code>ProductData.TYPE_UINT32</code>
* @param numElems the number of elements in this data node.
*/
protected RasterDataNode(String name, int dataType, long numElems) {
super(name, dataType, numElems);
if (dataType != ProductData.TYPE_INT8
&& dataType != ProductData.TYPE_INT16
&& dataType != ProductData.TYPE_INT32
&& dataType != ProductData.TYPE_UINT8
&& dataType != ProductData.TYPE_UINT16
&& dataType != ProductData.TYPE_UINT32
&& dataType != ProductData.TYPE_FLOAT32
&& dataType != ProductData.TYPE_FLOAT64) {
throw new IllegalArgumentException("dataType is invalid");
}
scalingFactor = 1.0;
scalingOffset = 0.0;
log10Scaled = false;
scalingApplied = false;
noData = null;
noDataValueUsed = false;
geophysicalNoDataValue = 0.0;
validPixelExpression = null;
imageToModelTransform = null;
modelToSceneTransform = MathTransform2D.IDENTITY;
sceneToModelTransform = MathTransform2D.IDENTITY;
overlayMasks = new ProductNodeGroup<>(this, "overlayMasks", false);
}
/**
* @return The native width of the raster in pixels.
*/
public abstract int getRasterWidth();
/**
* @return The native height of the raster in pixels.
*/
public abstract int getRasterHeight();
/**
* @return The native size of the raster in pixels.
*/
public Dimension getRasterSize() {
return new Dimension(getRasterWidth(), getRasterHeight());
}
@Override
public void setModified(boolean modified) {
boolean oldState = isModified();
if (oldState != modified) {
if (!modified && overlayMasks != null) {
overlayMasks.setModified(false);
}
super.setModified(modified);
}
}
/**
* Gets the transformation used to convert this raster's image (pixel) coordinates to model coordinates
* used for rendering the image together with other images and vector data.
* <p>
* If this raster data node's ({@link #isSourceImageSet() source image is set})
* the {@link MultiLevelModel#getImageToModelTransform(int) image-to-model transformation} of the image pyramid's
* lowest level image is returned.
* Otherwise the transformation which has been set using the {@link #setImageToModelTransform(AffineTransform)}
* is returned.
* If the transformation was not set explicitly, the method tries to determine it from geo-codings.
* If this fails, the identity transform is returned.
*
* @return The image-to-model transformation.
* @see #getProduct()
* @see Product#getSceneCRS()
* @see #setImageToModelTransform(AffineTransform)
* @see #getSourceImage()
* @see #getGeoCoding()
* @since SNAP 2.0
*/
public AffineTransform getImageToModelTransform() {
// If a source image is already set, we must return the actual image-to-model transformation in use
if (isSourceImageSet()) {
return getSourceImage().getModel().getImageToModelTransform(0);
}
// If image-to-model transformation is explicitly set, return it
if (imageToModelTransform != null) {
return new AffineTransform(imageToModelTransform);
}
// Try to derive from source product
Product product = getProduct();
if (product != null) {
CoordinateReferenceSystem sceneCRS = product.getSceneCRS();
GeoCoding sceneGeoCoding = product.getSceneGeoCoding();
GeoCoding rasterGeoCoding = getGeoCoding();
CoordinateReferenceSystem appropriateSceneCRS = Product.findModelCRS(rasterGeoCoding);
if (sceneCRS.equals(appropriateSceneCRS)) {
// If both model CRS are equal
return Product.findImageToModelTransform(rasterGeoCoding);
}
if (sceneGeoCoding == null && rasterGeoCoding == null) {
// Fallback: identity transform, works fine for (single-size) products without geo-coding
return new AffineTransform();
}
}
// Fallback: avoid returning null
return new AffineTransform();
}
/**
* Sets the transformation used to convert this raster's image (pixel) coordinates to model coordinates
* used for rendering the image together with other images and vector data.
* <p>
* The method call will fail if this raster data node has already a source image
* which uses a different image-to-model transformation.
* <p>
* <i>WARNING: This method belongs to a preliminary API and may change in an incompatible
* way or may even be removed in a next SNAP release.</i>
*
* @param imageToModelTransform The new image-to-model transformation
* @throws IllegalStateException If a source image is already set which uses a different image-to-model transformation.
* @see #getImageToModelTransform()
* @see #createSourceImage()
* @since SNAP 2.0
*/
public void setImageToModelTransform(AffineTransform imageToModelTransform) {
Assert.notNull(imageToModelTransform, "imageToModelTransform");
AffineTransform imageToModelTransformOld = getImageToModelTransform();
if (!imageToModelTransformOld.equals(imageToModelTransform)) {
if (isSourceImageSet()) {
throw new IllegalStateException("sourceImage already set, imageToModelTransform is now read-only");
}
this.imageToModelTransform = new AffineTransform(imageToModelTransform);
fireProductNodeChanged(PROPERTY_NAME_IMAGE_TO_MODEL_TRANSFORM, imageToModelTransformOld, imageToModelTransform);
}
}
/**
* Gets the transformation that transforms from local {@link RasterDataNode} model coordinates
* to the {@link Product}'s scene coordinates.
* <p>
* If no model-to-scene transformation has been explicitly set but a scene-to-model transformation
* exists, then the latter's {@code inverse()} is returned.
*
* @return The model-to-scene transformation, or {@code null} if no such exists.
* @see Product#getSceneCRS()
* @see #getImageToModelTransform()
* @since SNAP 2.0.3
*/
public MathTransform2D getModelToSceneTransform() {
if (modelToSceneTransform == MathTransform2D.IDENTITY && sceneToModelTransform != MathTransform2D.IDENTITY) {
try {
return sceneToModelTransform.inverse();
} catch (NoninvertibleTransformException e) {
return MathTransform2D.NULL;
}
}
return modelToSceneTransform;
}
/**
* Sets a {@code MathTransform2D} from the local model CRS of this {@code RasterDataNode} to
* a {@code Product}'s scene coordinate reference system. If the sceneToModelTransform is not the inverse
* of this transform or cannot be derived from it, it must be set using {@code setSceneToModelTransform()}.
*
* Only use this method when you know that the model CRS of this {@code RasterDataNode} is different
* than the {@code Product}'s scene CRS or when you want to model a special relationship between
* different {@code RasterDataNode}'s. When no transformation from local model to scene is possible, use
* {@link MathTransform2D#NULL}.
*
* @param modelToSceneTransform The {@code MathTransform2D} to convert local model coordinates to
* product scene coordinates
* @see RasterDataNode#setSceneToModelTransform(MathTransform2D)
* @since SNAP 2.0.3
*/
public void setModelToSceneTransform(MathTransform2D modelToSceneTransform) {
Assert.notNull(modelToSceneTransform, PROPERTY_NAME_MODEL_TO_SCENE_TRANSFORM);
MathTransform2D oldTransform = this.modelToSceneTransform;
this.modelToSceneTransform = modelToSceneTransform;
fireProductNodeChanged(PROPERTY_NAME_MODEL_TO_SCENE_TRANSFORM, oldTransform, this.modelToSceneTransform);
}
/**
* Gets the transformation that transforms from the {@link Product}'s scene coordinates
* to the local {@link RasterDataNode} model coordinates.
* <p>
* If no scene-to-model transformation has been explicitly set but a model-to-scene transformation
* exists, then the latter's {@code inverse()} is returned.
*
* @return The model-to-scene transformation
* @see Product#getSceneCRS()
* @see #getImageToModelTransform()
* @since SNAP 2.0.3
*/
public MathTransform2D getSceneToModelTransform() {
if (sceneToModelTransform == MathTransform2D.IDENTITY && modelToSceneTransform != MathTransform2D.IDENTITY) {
try {
return modelToSceneTransform.inverse();
} catch (NoninvertibleTransformException e) {
return MathTransform2D.NULL;
}
}
return sceneToModelTransform;
}
/**
* Sets a {@code MathTransform2D} from a {@code Product}'s scene coordinate reference system to
* the local model CRS of this {@code RasterDataNode}. If the modelToSceneTransform is not the inverse
* of this transformor cannot be derived from it, it must be set using {@code setModelToSceneTransform()}.
*
* Only use this method when you know that the model CRS of this {@code RasterDataNode} is different
* than the {@code Product}'s scene CRS or when you want to model a special relationship between
* different {@code RasterDataNode}'s. When no transformation from scene to local model is possible, use
* {@link MathTransform2D#NULL}.
*
* @param sceneToModelTransform The {@code MathTransform2D} to convert product scene coordinates
* to local model coordinates
* @see RasterDataNode#setModelToSceneTransform(MathTransform2D)
* @since SNAP 2.0.3
*/
public void setSceneToModelTransform(MathTransform2D sceneToModelTransform) {
Assert.notNull(sceneToModelTransform, PROPERTY_NAME_SCENE_TO_MODEL_TRANSFORM);
MathTransform2D oldTransform = this.sceneToModelTransform;
this.sceneToModelTransform = sceneToModelTransform;
fireProductNodeChanged(PROPERTY_NAME_SCENE_TO_MODEL_TRANSFORM, oldTransform, this.sceneToModelTransform);
}
/**
* Returns the geo-coding of this {@link RasterDataNode}.
*
* @return the geo-coding, or {@code null} if not available.
*/
public GeoCoding getGeoCoding() {
if (geoCoding == null) {
final Product product = getProduct();
if (product != null) {
return product.getSceneGeoCoding();
}
}
return geoCoding;
}
/**
* Sets the geo-coding for this {@link RasterDataNode}.
* Also sets the geo-coding of the parent {@link Product} if it has no geo-coding yet.
* <p>On property change, the method calls {@link #fireProductNodeChanged(String)} with the property
* name {@link #PROPERTY_NAME_GEO_CODING}.
*
* @param geoCoding the new geo-coding
* @see Product#setSceneGeoCoding(GeoCoding)
*/
public void setGeoCoding(final GeoCoding geoCoding) {
if (!ObjectUtils.equalObjects(geoCoding, this.geoCoding)) {
this.geoCoding = geoCoding;
// If our product has no geo-coding yet, it is set to the current one, if any
if (this.geoCoding != null) {
final Product product = getProduct();
if (product != null && product.getSceneGeoCoding() == null) {
product.setSceneGeoCoding(this.geoCoding);
}
}
fireProductNodeChanged(PROPERTY_NAME_GEO_CODING);
}
}
/**
* Gets the time-coding of this {@link RasterDataNode}.
*
* @return the time-coding, or {@code null} if not available.
* @see Product#getSceneTimeCoding()
* @since SNAP 2.0
*/
public TimeCoding getTimeCoding() {
return timeCoding;
}
/**
* Sets the time-coding for this {@link RasterDataNode}.
*
* @param timeCoding the new time-coding
* @see Product#setSceneTimeCoding(TimeCoding)
* @since SNAP 2.0
*/
public void setTimeCoding(final TimeCoding timeCoding) {
if (!ObjectUtils.equalObjects(timeCoding, this.timeCoding)) {
final TimeCoding oldValue = this.timeCoding;
this.timeCoding = timeCoding;
fireProductNodeChanged(PROPERTY_NAME_TIME_CODING, oldValue, timeCoding);
}
}
/**
* Creates a {@link Pointing} applicable for this raster.
*
* @return the pointing object, or null if a pointing is not available
*/
protected Pointing createPointing() {
if (getGeoCoding() == null || getProduct() == null) {
return null;
}
final PointingFactory factory = getProduct().getPointingFactory();
if (factory == null) {
return null;
}
return factory.createPointing(this);
}
/**
* Gets a {@link Pointing} if one is available for this raster.
* The methods calls {@link #createPointing()} if a pointing has not been set so far or if its {@link GeoCoding} changed
* since the last creation of this raster's {@link Pointing} instance.
*
* @return the pointing object, or null if a pointing is not available
*/
public Pointing getPointing() {
if (pointing == null || pointing.getGeoCoding() == getGeoCoding()) {
pointing = createPointing();
}
return pointing;
}
/**
* Tests if this raster data node can be orthorectified.
*
* @return true, if so
*/
public boolean canBeOrthorectified() {
final Pointing pointing = getPointing();
return pointing != null && pointing.canGetViewDir();
}
/**
* Returns <code>true</code> if the pixel data contained in this band is "naturally" a floating point number type.
*
* @return true, if so
*/
@Override
public boolean isFloatingPointType() {
return scalingApplied || super.isFloatingPointType();
}
/**
* Returns the geophysical data type of this <code>RasterDataNode</code>. The value returned is always one of the
* <code>ProductData.TYPE_XXX</code> constants.
*
* @return the geophysical data type
* @see ProductData
* @see #isScalingApplied()
*/
public int getGeophysicalDataType() {
return ImageManager.getProductDataType(
ReinterpretDescriptor.getTargetDataType(ImageManager.getDataBufferType(getDataType()),
getScalingFactor(),
getScalingOffset(),
getScalingType(),
getInterpretationType()));
}
/**
* Gets the scaling factor which is applied to raw {@link ProductData}. The default value is
* <code>1.0</code> (no factor).
*
* @return the scaling factor
* @see #isScalingApplied()
*/
public final double getScalingFactor() {
return scalingFactor;
}
/**
* Sets the scaling factor which is applied to raw {@link ProductData}.
*
* @param scalingFactor the scaling factor
* @see #isScalingApplied()
*/
public final void setScalingFactor(double scalingFactor) {
if (this.scalingFactor != scalingFactor) {
this.scalingFactor = scalingFactor;
setScalingApplied();
resetGeophysicalImage();
fireProductNodeChanged(PROPERTY_NAME_SCALING_FACTOR);
setGeophysicalNoDataValue();
resetValidMask();
setModified(true);
}
}
/**
* Gets the scaling offset which is applied to raw {@link ProductData}. The default value is
* <code>0.0</code> (no offset).
*
* @return the scaling offset
* @see #isScalingApplied()
*/
public final double getScalingOffset() {
return scalingOffset;
}
/**
* Sets the scaling offset which is applied to raw {@link ProductData}.
*
* @param scalingOffset the scaling offset
* @see #isScalingApplied()
*/
public final void setScalingOffset(double scalingOffset) {
if (this.scalingOffset != scalingOffset) {
this.scalingOffset = scalingOffset;
setScalingApplied();
resetGeophysicalImage();
fireProductNodeChanged(PROPERTY_NAME_SCALING_OFFSET);
setGeophysicalNoDataValue();
resetValidMask();
setModified(true);
}
}
/**
* Gets whether or not the {@link ProductData} of this band has a negative binomial distribution and
* thus the common logarithm (base 10) of the values is stored in the raw data. The default value is
* <code>false</code>.
*
* @return whether or not the data is logging-10 scaled
* @see #isScalingApplied()
*/
public final boolean isLog10Scaled() {
return log10Scaled;
}
/**
* Sets whether or not the {@link ProductData} of this band has a negative binomial distribution and
* thus the common logarithm (base 10) of the values is stored in the raw data.
*
* @param log10Scaled whether or not the data is logging-10 scaled
* @see #isScalingApplied()
*/
public final void setLog10Scaled(boolean log10Scaled) {
if (this.log10Scaled != log10Scaled) {
this.log10Scaled = log10Scaled;
setScalingApplied();
resetGeophysicalImage();
setGeophysicalNoDataValue();
resetValidMask();
fireProductNodeChanged(PROPERTY_NAME_LOG_10_SCALED);
setModified(true);
}
}
/**
* Tests whether scaling of raw raster data values is applied before they are returned as geophysically meaningful
* pixel values. <p>The methods which return geophysical pixel values are all {@link #getPixels(int, int, int, int, int[])},
* {@link #setPixels(int, int, int, int, int[])}, {@link #readPixels(int, int, int, int, int[])} and
* {@link #writePixels(int, int, int, int, int[])} methods as well as the <code>getPixel<Type></code> and
* <code>setPixel<Type></code> methods such as {@link #getPixelFloat(int, int)} * and
* {@link #setPixelFloat(int, int, float)}.
*
* @return <code>true</code> if a conversion is applyied to raw data samples before the are retuned.
* @see #getScalingOffset
* @see #getScalingFactor
* @see #isLog10Scaled
*/
public final boolean isScalingApplied() {
return scalingApplied;
}
/**
* Tests if the given name is the name of a property which is relevant for the computation of the valid mask.
*
* @param propertyName the name to test
* @return {@code true}, if so.
* @since BEAM 4.2
*/
public static boolean isValidMaskProperty(final String propertyName) {
return PROPERTY_NAME_NO_DATA_VALUE.equals(propertyName)
|| PROPERTY_NAME_NO_DATA_VALUE_USED.equals(propertyName)
|| PROPERTY_NAME_VALID_PIXEL_EXPRESSION.equals(propertyName)
|| PROPERTY_NAME_DATA.equals(propertyName);
}
/**
* Tests whether or not a no-data value has been specified. The no-data value is not-specified unless either
* {@link #setNoDataValue(double)} or {@link #setGeophysicalNoDataValue(double)} is called.
*
* @return true, if so
* @see #isNoDataValueUsed()
* @see #setNoDataValue(double)
*/
public boolean isNoDataValueSet() {
return noData != null;
}
/**
* Clears the no-data value, so that {@link #isNoDataValueSet()} will return <code>false</code>.
*/
public void clearNoDataValue() {
noData = null;
setGeophysicalNoDataValue();
}
/**
* Tests whether or not the no-data value is used.
* <p>The no-data value is used to determine valid pixels. For more information
* on valid pixels, please refer to the documentation of the {@link #isPixelValid(int, int, javax.media.jai.ROI)}
* method.
*
* @return true, if so
* @see #setNoDataValueUsed(boolean)
* @see #isNoDataValueSet()
*/
public boolean isNoDataValueUsed() {
return noDataValueUsed;
}
/**
* Sets whether or not the no-data value is used.
* If the no-data value is enabled and the no-data value has not been set so far,
* a default no-data value it is set with a value of to zero.
* <p>The no-data value is used to determine valid pixels. For more information
* on valid pixels, please refer to the documentation of the {@link #isPixelValid(int, int, javax.media.jai.ROI)}
* method.
* <p>On property change, the method calls {@link #fireProductNodeChanged(String)} with the property
* name {@link #PROPERTY_NAME_NO_DATA_VALUE_USED}.
*
* @param noDataValueUsed true, if so
* @see #isNoDataValueUsed()
*/
public void setNoDataValueUsed(boolean noDataValueUsed) {
if (this.noDataValueUsed != noDataValueUsed) {
this.noDataValueUsed = noDataValueUsed;
resetValidMask();
setModified(true);
fireProductNodeChanged(PROPERTY_NAME_NO_DATA_VALUE_USED);
fireProductNodeDataChanged();
}
}
/**
* Gets the no-data value as a primitive <code>double</code>.
* <p>Note that the value returned is NOT necessarily the same as the value returned by
* {@link #getGeophysicalNoDataValue()} because no scaling is applied.
* <p>The no-data value is used to determine valid pixels. For more information
* on valid pixels, please refer to the documentation of the {@link #isPixelValid(int, int, javax.media.jai.ROI)}
* method.
* <p>The method returns <code>0.0</code>, if no no-data value has been specified so far.
*
* @return the no-data value. It is returned as a <code>double</code> in order to cover all other numeric types.
* @see #setNoDataValue(double)
* @see #isNoDataValueSet()
*/
public double getNoDataValue() {
return isNoDataValueSet() ? noData.getElemDouble() : 0.0;
}
/**
* Sets the no-data value as a primitive <code>double</code>.
* <p>Note that the given value is related to the "raw", un-scaled raster data.
* In order to set the geophysical, scaled no-data value use the method
* {@link #setGeophysicalNoDataValue(double)}.
* <p>The no-data value is used to determine valid pixels. For more information
* on valid pixels, please refer to the documentation of the {@link #isPixelValid(int, int, javax.media.jai.ROI)}
* method.
* <p>On property change, the method calls {@link #fireProductNodeChanged(String)} with the property
* name {@link #PROPERTY_NAME_NO_DATA_VALUE}.
*
* @param noDataValue the no-data value. It is passed as a <code>double</code> in order to cover all other numeric types.
* @see #getNoDataValue()
* @see #isNoDataValueSet()
*/
public void setNoDataValue(final double noDataValue) {
if (noData == null || getNoDataValue() != noDataValue) {
if (noData == null) {
noData = createCompatibleProductData(1);
}
noData.setElemDouble(noDataValue);
setGeophysicalNoDataValue();
if (isNoDataValueUsed()) {
resetValidMask();
}
setModified(true);
fireProductNodeChanged(PROPERTY_NAME_NO_DATA_VALUE);
if (isNoDataValueUsed()) {
fireProductNodeDataChanged();
}
}
}
/**
* Gets the geophysical no-data value which is simply the scaled "raw" no-data value
* returned by {@link #getNoDataValue()}.
* <p>The no-data value is used to determine valid pixels. For more information
* on valid pixels, please refer to the documentation of the {@link #isPixelValid(int, int, javax.media.jai.ROI)}
* method.
*
* @return the geophysical no-data value
* @see #setGeophysicalNoDataValue(double)
*/
public double getGeophysicalNoDataValue() {
return geophysicalNoDataValue;
}
/**
* Sets the geophysical no-data value which is simply the scaled "raw" no-data value
* returned by {@link #getNoDataValue()}.
* <p>The no-data value is used to determine valid pixels. For more information
* on valid pixels, please refer to the documentation of the {@link #isPixelValid(int, int, javax.media.jai.ROI)}
* method.
* <p>On property change, the method calls {@link #fireProductNodeChanged(String)} with the property
* name {@link #PROPERTY_NAME_NO_DATA_VALUE}.
*
* @param noDataValue the new geophysical no-data value
* @see #setGeophysicalNoDataValue(double)
* @see #isNoDataValueSet()
*/
public void setGeophysicalNoDataValue(double noDataValue) {
setNoDataValue(scaleInverse(noDataValue));
}
/**
* Gets the expression that is used to determine whether a pixel is valid or not.
* For more information
* on valid pixels, please refer to the documentation of the {@link #isPixelValid(int, int, javax.media.jai.ROI)}
* method.
*
* @return the valid mask expression.
*/
public String getValidPixelExpression() {
return validPixelExpression;
}
/**
* Sets the expression that is used to determine whether a pixel is valid or not.
* <p>The valid-pixel expression is used to determine valid pixels. For more information
* on valid pixels, please refer to the documentation of the {@link #isPixelValid(int, int, javax.media.jai.ROI)}
* method.
* <p>On property change, the method calls {@link #fireProductNodeChanged(String)} with the property
* name {@link #PROPERTY_NAME_VALID_PIXEL_EXPRESSION}.
*
* @param validPixelExpression the valid mask expression, can be null
*/
public void setValidPixelExpression(final String validPixelExpression) {
if (!ObjectUtils.equalObjects(this.validPixelExpression, validPixelExpression)) {
this.validPixelExpression = validPixelExpression;
resetValidMask();
setModified(true);
fireProductNodeChanged(PROPERTY_NAME_VALID_PIXEL_EXPRESSION);
fireProductNodeDataChanged();
}
}
/**
* Tests whether or not this raster data node uses a data-mask in order to determine valid pixels. The method returns
* true if either {@link #isValidPixelExpressionSet()} or {@link #isNoDataValueUsed()} returns true.
* <p>The data-mask is used to determine valid pixels. For more information
* on valid pixels, please refer to the documentation of the {@link #isPixelValid(int, int, javax.media.jai.ROI)}
* method.
*
* @return true, if so
*/
public boolean isValidMaskUsed() {
return isValidPixelExpressionSet() || isNoDataValueUsed();
}
/**
* Resets the valid mask of this raster.
* The mask will be lazily regenerated when requested the next time.
*/
public void resetValidMask() {
validMaskROI = null;
validMaskImage = null;
stx = null;
}
/**
* Gets the expression used for the computation of the mask which identifies valid pixel values.
* It recognizes the value of the {@link #getNoDataValue() noDataValue} and the
* {@link #getValidPixelExpression() validPixelExpression} properties, if any.
* The method returns {@code null}, if none of these properties are set.
*
* @return The expression used for the computation of the mask which identifies valid pixel values,
* or {@code null}.
* @see #getValidPixelExpression()
* @see #getNoDataValue()
* @since BEAM 4.2
*/
public String getValidMaskExpression() {
String dataMaskExpression = null;
if (isValidPixelExpressionSet()) {
dataMaskExpression = getValidPixelExpression();
if (isNoDataValueUsed()) {
final String dataMaskExpression2 = createValidMaskExpressionForNoDataValue();
if (!dataMaskExpression2.equals(dataMaskExpression)) {
dataMaskExpression = "(" + dataMaskExpression + ") && " + dataMaskExpression2;
}
}
} else if (isNoDataValueUsed()) {
dataMaskExpression = createValidMaskExpressionForNoDataValue();
}
return dataMaskExpression;
}
private String createValidMaskExpressionForNoDataValue() {
final String ref = BandArithmetic.createExternalName(getName());
final double noDataValue = getGeophysicalNoDataValue();
if (Double.isNaN(noDataValue)) {
return "!nan(" + ref + ")";
} else if (Double.isInfinite(noDataValue)) {
return "!inf(" + ref + ")";
} else {
if (ProductData.isIntType(getDataType())) {
double rawNoDataValue = getNoDataValue();
String rawSymbol = getName() + ".raw";
String extName = BandArithmetic.createExternalName(rawSymbol);
return extName + " != " + rawNoDataValue;
} else {
return "fneq(" + ref + "," + noDataValue + ")";
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void updateExpression(final String oldExternalName, final String newExternalName) {
if (validPixelExpression == null) {
return;
}
final String expression = StringUtils.replaceWord(validPixelExpression, oldExternalName, newExternalName);
if (!validPixelExpression.equals(expression)) {
validPixelExpression = expression;
setModified(true);
}
super.updateExpression(oldExternalName, newExternalName);
}
/**
* Returns true if the raster data of this <code>RasterDataNode</code> is loaded or elsewhere available, otherwise
* false.
*
* @return true, if so.
*/
public boolean hasRasterData() {
return getRasterData() != null;
}
/**
* Gets the raster data for this dataset. If the data hasn't been loaded so far the method returns
* <code>null</code>.
*
* @return the raster data for this band, or <code>null</code> if data has not been loaded
*/
public ProductData getRasterData() {
return getData();
}
/**
* Sets the raster data of this dataset.
* <p> Note that this method does not copy data at all. If the supplied raster data is compatible with this product
* raster, then simply its reference is stored. Modifications in the supplied raster data will also affect this
* dataset's data.
*
* @param rasterData The raster data for this raster data node.
* @see #getRasterData()
*/
public void setRasterData(ProductData rasterData) {
ProductData oldData = getData();
if (oldData != rasterData) {
if (rasterData != null) {
if (rasterData.getType() != getDataType()) {
throw new IllegalArgumentException("rasterData.getType() != getDataType()");
}
if (rasterData.getNumElems() != getRasterWidth() * getRasterHeight()) {
throw new IllegalArgumentException("rasterData.getNumElems() != getRasterWidth() * getRasterHeight()");
}
}
setData(rasterData);
}
}
/**
* @throws java.io.IOException if an I/O error occurs
* @see #loadRasterData(com.bc.ceres.core.ProgressMonitor)
*/
public void loadRasterData() throws IOException {
loadRasterData(ProgressMonitor.NULL);
}
/**
* Loads the raster data for this <code>RasterDataNode</code>. After this method has been called successfully,
* <code>hasRasterData()</code> should always return <code>true</code> and <code>getRasterData()</code> should
* always return a valid <code>ProductData</code> instance with at least <code>getRasterWidth()*getRasterHeight()</code>
* elements (samples).
* <p>The default implementation of this method does nothing.
*
* @param pm a monitor to inform the user about progress
* @throws IOException if an I/O error occurs
* @see #unloadRasterData()
*/
public void loadRasterData(ProgressMonitor pm) throws IOException {
}
/**
* Un-loads the raster data for this <code>RasterDataNode</code>.
* <p>It is up to the implementation whether after this method has been called successfully, the
* <code>hasRasterData()</code> method returns <code>false</code> or <code>true</code>.
* <p>The default implementation of this method does nothing.
*
* @see #loadRasterData()
*/
public void unloadRasterData() {
}
/**
* Releases all of the resources used by this object instance and all of its owned children. Its primary use is to
* allow the garbage collector to perform a vanilla job.
* <p>This method should be called only if it is for sure that this object instance will never be used again. The
* results of referencing an instance of this class after a call to <code>dispose()</code> are undefined.
* <p>Overrides of this method should always call <code>super.dispose();</code> after disposing this instance.
*/
@Override
public void dispose() {
if (imageInfo != null) {
imageInfo.dispose();
imageInfo = null;
}
if (sourceImage != null) {
sourceImage.dispose();
sourceImage = null;