Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tasseledcap / trunk / org.gvsig.raster.tasseledcap / org.gvsig.raster.tasseledcap.algorithm / src / main / java / org / gvsig / raster / tasseledcap / algorithm / TasseledCapProcess.java @ 2381

History | View | Annotate | Download (7.95 KB)

1
package org.gvsig.raster.tasseledcap.algorithm;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.util.List;
5

    
6
import org.gvsig.fmap.dal.coverage.RasterLocator;
7
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
8
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
9
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
10
import org.gvsig.fmap.dal.coverage.exception.CloneException;
11
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
12
import org.gvsig.fmap.dal.coverage.exception.ROIException;
13
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
14
import org.gvsig.i18n.Messages;
15
import org.gvsig.raster.algorithm.process.DataProcess;
16
import org.gvsig.raster.algorithm.process.ProcessException;
17
import org.gvsig.raster.roi.ROI;
18

    
19
/**
20
 * Tasseled Cab Process 
21
 * @author Nacho Brodin (nachobrodin@gmail.com)
22
 */
23
public class TasseledCapProcess extends DataProcess {
24
        public static int             LANDSAT_MS         = 0;
25
        public static int             LANDSAT_TM         = 1;
26
        public static int             LANDSAT_ETM        = 2;
27
        
28
        public static String          RASTER_STORE       = "RASTER_STORE";
29
        public static String          PATH               = "PATH";
30
        public static String          FILENAME           = "FILENAME";
31
        public static String          LANDSAT_TYPE       = "LANDSAT_TYPE";
32
        
33
        public static final String    BUFFERS            = "BUFFERS";
34
        public static final String    GRAPHIC_DATA       = "GRAPHIC_DATA";
35
        public static final String    BANDS              = "BANDS";
36
        
37
        private RasterDataStore       store              = null;
38
        private String                filename           = null;
39
        
40
        private List<ROI>             rois               = null;
41
        private Extent                extentResult       = null;
42
        private boolean[]             bands              = null;
43
        private NoData                nodata             = null;
44
        private double                matrixParams[][]   = null;
45
        private int                   type               = LANDSAT_MS;
46
        
47
        // MATRICES DE COEFICIENTES PARA CADA TRANSFORMACI?N
48
        double LandSatMS[][] = {
49
                        /*   Brillo de suelo       */         {0.433, 0.632, 0.586, 0.264   },
50
                        /*         Indice de Verdor      */        {-0.290, -0.562, 0.600, 0.491 },
51
                        /*         Indice de Senescencia */   {-0.829,  0.522, -0.039, 0.194},
52
                        /*         Cuarta                */        {0.223,  0.012,  -0.543, 0.810}                
53
        };
54

    
55
        double LandSatTM[][] = { 
56
                        /*   Brillo    */         {0.33183,  0.33121,  0.55177,  0.42514,  0.48087, 0.25252                  },
57
                        /*         Verdor    */        {-0.24717,  -0.16263,  -0.40639,  0.85468,  0.05493,  -0.11749        },
58
                        /*         Tercera   */        {0.13929,  0.22490,  0.40359,  0.25178,  -0.70133, -0.45732          },        
59
        };
60

    
61
        double LandSatETM[][] = {
62
                        /*   Brightness */         {0.3561,  0.3972,  0.3904,  0.6966,  0.2286,  0.1596     },
63
                        /*         Greenness         */        {-0.3344,  -0.3544,  -0.4556,  0.6966,  -0.0242,  -0.2630},
64
                        /*         Vetness         */        {0.2626,  0.2141,  0.0926,  0.0656,  -0.7629, -0.5388    },        
65
                        /*         Fourth         */        {0.0805,  -0.0498,  0.1950, -0.1327, 0.5752,  -0.7775    },
66
                        /*         Fifth                 */        {-0.7252,  -0.0202,  0.6683,  0.0631,  -0.1494,  -0.0274 },
67
                        /*         Sixth                 */        {0.4000,  -0.8172,  0.3832,  0.0602,  -0.1095,  -0.0985         }
68
        };
69

    
70
        
71
        public static void registerParameters() {
72
                registerInputParameter(RASTER_STORE, RasterDataStore.class, TasseledCapAlgorithmLibrary.TASSELEDCAP_PROCESS_LABEL);
73
                registerInputParameter(PATH, String.class, TasseledCapAlgorithmLibrary.TASSELEDCAP_PROCESS_LABEL);
74
                registerInputParameter(BANDS, Boolean[].class, TasseledCapAlgorithmLibrary.TASSELEDCAP_PROCESS_LABEL);
75
                registerInputParameter(LANDSAT_TYPE, Integer.class, TasseledCapAlgorithmLibrary.TASSELEDCAP_PROCESS_LABEL);
76
                
77
                registerOutputParameter(FILENAME, String.class, TasseledCapAlgorithmLibrary.TASSELEDCAP_PROCESS_LABEL);
78
                registerOutputParameter(GRAPHIC_DATA, Double[].class, TasseledCapAlgorithmLibrary.TASSELEDCAP_PROCESS_LABEL);
79
        }
80
        
81
        public void init() {
82
                store = getParam(RASTER_STORE) != null ? (RasterDataStore)getParam(RASTER_STORE) : null;
83
                filename = getStringParam(PATH);
84
                bands = (boolean[]) getParam(BANDS);
85
                type = getIntParam(LANDSAT_TYPE);
86
        }
87

    
88
        private boolean loadCoefMatrix(int type) {
89
                switch (type) {
90
                case 0:
91
                        matrixParams = LandSatMS;
92
                        if(getNumberOfOutputBands(bands) != 4) {
93
                                return false;
94
                        }
95
                        break;        
96
                case 1:
97
                        matrixParams = LandSatTM;        
98
                        if(getNumberOfOutputBands(bands) != 6) {
99
                                return false;
100
                        }
101
                        break;
102
                case 2:
103
                        matrixParams = LandSatETM;        
104
                        if(getNumberOfOutputBands(bands) != 6) {
105
                                return false;
106
                        }
107
                        break;
108
                }        
109
                return true;
110
        }
111

    
112
        public void process() throws ProcessInterruptedException, ProcessException {
113
                insertLineLog(Messages.getText("processing_tc"));
114
                if (store == null) {
115
                        throw new TasseledCapException(Messages.getText("need_a_input"));
116
                }
117

    
118
                if(!loadCoefMatrix(type)) {
119
                        throw new TasseledCapException(Messages.getText("wrong_number_of_bands"));
120
                }
121

    
122
                if(getROIEPSG() != null) {
123
                        try {
124
                                rois = store.getRois(getROIEPSG());
125
                        } catch (ROIException e2) {
126
                                logger.error(Messages.getText("error_getting_rois"), e2);
127
                        }
128
                }
129

    
130
                try {
131
                        store = ((RasterDataStore)store).cloneDataStore();
132
                } catch (CloneException e) {
133
                        new TasseledCapException("Error cloning the input DataStore", e);
134
                }
135

    
136
                extentResult = getExtentResult(getOutputWindow(), rois, store);
137
                Rectangle2D sourcePxBBox = getSourcePxBox(extentResult, store);
138

    
139
                double cellSize = store.getCellSize();
140

    
141
                nodata = RasterLocator.getManager().getDataStructFactory().createDefaultNoData(1, Buffer.TYPE_DOUBLE);
142
                double nodataValue = nodata.getValue().doubleValue();
143

    
144
                Buffer outputBuffer = createOutputBuffer((int)sourcePxBBox.getWidth(), (int)sourcePxBBox.getHeight(), matrixParams.length, Buffer.TYPE_FLOAT);
145
                Buffer sourceBuffer = createSourceBuffer(store, sourcePxBBox, bands);
146

    
147

    
148
                insertLineLog(Messages.getText("writting_in_buffer"));
149
                float resultValue = 0;
150

    
151
                for (int i = 0; i < sourceBuffer.getHeight(); i++) {
152
                        for (int j = 0; j < sourceBuffer.getWidth(); j++) {
153
                                if (isInsideOfROI(j, i, rois, extentResult)) {
154
                                        for (int coef = 0; coef < matrixParams.length; coef++) {
155
                                                for(int band = 0; band < matrixParams[0].length; band++) {        
156
                                                        resultValue += getValue(sourceBuffer, i, j, band, coef);
157
                                                }
158
                                                outputBuffer.setElem(i, j, coef, resultValue);
159
                                                resultValue = 0;
160
                                        }
161
                                } else {
162
                                        for (int iBand = 0; iBand < outputBuffer.getBandCount(); iBand++) {
163
                                                outputBuffer.setElem(i, j, iBand, (float)nodataValue);
164
                                        }
165
                                }
166
                        }
167
                        updatePercent((int)(i * 100 / sourcePxBBox.getHeight()), 100);
168
                }
169

    
170
                super.exportRaster(filename, 
171
                                outputBuffer, 
172
                                cellSize, 
173
                                extentResult.getULX(), 
174
                                extentResult.getULY(),
175
                                nodata);
176

    
177
                addOutputValue(FILENAME, filename);
178
                addOutputValue(GRAPHIC_DATA, filename);
179
        }
180

    
181
        /**
182
         * Gets the number of the selected bands
183
         * @param bandsPCs
184
         * @return
185
         */
186
        private int getNumberOfOutputBands(boolean[] b) {
187
                int bandCount = 0;
188
        for (int i = 0; i < b.length; i++) {
189
                        if(b[i])
190
                                bandCount++;
191
                }
192
        return bandCount;
193
        }
194
        
195
        private float getValue(Buffer sourceBuffer, int i, int j, int band, int coef) {
196
                if (sourceBuffer.getDataType() == Buffer.TYPE_BYTE) {
197
                        return (float)((sourceBuffer.getElemByte(i, j, band) & 0xff) * matrixParams[coef][band]);
198
                }
199
                if (sourceBuffer.getDataType() == Buffer.TYPE_SHORT) {
200
                        return (float)(sourceBuffer.getElemShort(i, j, band) * matrixParams[coef][band]);
201
                }
202
                if (sourceBuffer.getDataType() == Buffer.TYPE_INT) {
203
                        return (float)(sourceBuffer.getElemInt(i, j, band) * matrixParams[coef][band]);
204
                }
205
                if (sourceBuffer.getDataType() == Buffer.TYPE_FLOAT) {
206
                        return (float)(sourceBuffer.getElemFloat(i, j, band) * matrixParams[coef][band]);
207
                }
208
                if (sourceBuffer.getDataType() == Buffer.TYPE_DOUBLE) {
209
                        return (float)(sourceBuffer.getElemDouble(i, j, band) * matrixParams[coef][band]);
210
                }
211
                return nodata.getValue().floatValue();
212
        }
213
        
214
        public String getTitle() {
215
                return Messages.getText("tasseled_cap");
216
        }
217
}