Statistics
| Revision:

gvsig-raster / org.gvsig.raster.principalcomponents / trunk / org.gvsig.raster.principalcomponents / org.gvsig.raster.principalcomponents.toolbox.algorithm / src / main / java / org / gvsig / raster / principalcomponents / PrincipalComponentsSextanteAlgorithm.java @ 3171

History | View | Annotate | Download (8.02 KB)

1
package org.gvsig.raster.principalcomponents;
2

    
3
import java.util.HashMap;
4
import java.util.List;
5

    
6
import org.gvsig.fmap.dal.coverage.RasterLocator;
7
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
8
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
9
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
10
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
11
import org.gvsig.geoprocess.lib.sextante.dataObjects.FLyrRasterIRasterLayer;
12
import org.gvsig.raster.algorithm.RasterBaseAlgorithmLibrary;
13
import org.gvsig.raster.algorithm.process.DataProcess;
14
import org.gvsig.raster.algorithm.process.IProcessActions;
15
import org.gvsig.raster.algorithm.process.ProcessException;
16
import org.gvsig.raster.fmap.layers.FLyrRaster;
17
import org.gvsig.raster.principalcomponents.algorithm.PCStatsDataStructure;
18
import org.gvsig.raster.principalcomponents.algorithm.PrincipalComponentsAlgorithmLibrary;
19
import org.gvsig.raster.principalcomponents.algorithm.PrincipalComponentsProcess;
20

    
21
import es.unex.sextante.core.AnalysisExtent;
22
import es.unex.sextante.core.Sextante;
23
import es.unex.sextante.dataObjects.IRasterLayer;
24
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
25
import es.unex.sextante.exceptions.RepeatedParameterNameException;
26

    
27
/**
28
 * ...
29
 */
30
public class PrincipalComponentsSextanteAlgorithm extends AbstractSextanteGeoProcess implements IProcessActions {
31
    public static final String RESULT            = "RESULT";
32
    public static final String LAYER             = PrincipalComponentsProcess.RASTER_STORE;
33
    public static final String USE_ROI           = "USE_ROI";
34

    
35
    private boolean            useROI            = false;
36
    private DataProcess        taskStats         = null;
37
    private DataProcess        taskPC            = null;
38

    
39

    
40
    public void defineCharacteristics() {
41
        setName(getTranslation("principalcomponents"));
42
        setGroup(getTranslation("multispectral"));
43

    
44
        try {
45
            m_Parameters.addInputRasterLayer(LAYER, getTranslation("Input_layer"), true);
46
            m_Parameters.addBoolean(USE_ROI, getTranslation("use_roi"), false);
47
        } catch (RepeatedParameterNameException e) {
48
            Sextante.addErrorToLog(e);
49
        }
50
        addOutputRasterLayer(RESULT, getTranslation("principalcomponents"));
51
    }
52

    
53
    public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
54

    
55
            if(existsOutPutFile(PrincipalComponentsSextanteAlgorithm.RESULT, 0)) {
56
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
57
            }
58

    
59
            IRasterLayer input = m_Parameters.getParameterValueAsRasterLayer(LAYER);
60
                useROI = m_Parameters.getParameterValueAsBoolean(USE_ROI);
61

    
62
            FLyrRaster lyrRaster = ((FLyrRaster)input.getBaseDataObject());
63
            IRasterLayer output = null;
64

    
65
            output = getNewRORasterLayer(
66
                            RESULT,
67
                            Sextante.getText("principalcomponents_description"),
68
                            input.getDataType(),
69
                            input.getBandsCount());
70

    
71
            String fileName = ((FLyrRasterIRasterLayer)output).getName();
72

    
73
            try {
74
                    setProgressText(getTranslation("stadistics"));
75
                        taskStats = createStatisticsProcess(lyrRaster.getDataStore());
76
                        taskStats.execute();
77
                        HashMap<String, Object> params = taskStats.getResult();
78
                        PCStatsDataStructure stats = (PCStatsDataStructure)params.get("STATS_RESULT");
79

    
80
                        setProgressText(getTranslation("calc_components"));
81
                        taskPC = createPCAProcess(lyrRaster.getDataStore(), fileName, stats);
82
                        taskPC.execute();
83
                        params = taskPC.getResult();
84

    
85
                        ((FLyrRasterIRasterLayer)output).setBaseDataObject(fileName);
86
                } catch (ProcessInterruptedException e) {
87
                        Sextante.addErrorToLog(e);
88
                } catch (ProcessException e) {
89
                        Sextante.addErrorToLog(e);
90
                }
91

    
92
                if(getTaskMonitor().isCanceled())
93
                        return false;
94

    
95
        return true;
96
    }
97

    
98
    /**
99
     * Creates a process to calculate statistics
100
     * @param inputStore
101
     * @return
102
     * @throws ProcessException
103
     */
104
    private DataProcess createStatisticsProcess(RasterDataStore inputStore) throws ProcessException {
105
            DataProcess taskStats = RasterBaseAlgorithmLibrary.getManager().createRasterTask(PrincipalComponentsAlgorithmLibrary.PC_STATS_PROCESS_LABEL);
106
            taskStats.setActions(this);
107
            List<String> params = taskStats.getRasterTaskInputParameters(PrincipalComponentsAlgorithmLibrary.PC_STATS_PROCESS_LABEL);
108
            for (int i = 0; i < params.size(); i++) {
109
                    String paramName = params.get(i);
110
                    Class<?> paramType = taskStats.getParameterTypeByProcess(PrincipalComponentsAlgorithmLibrary.PC_STATS_PROCESS_LABEL, paramName);
111
                    if(paramType == RasterDataStore.class) {
112
                            taskStats.addParam(paramName, (RasterDataStore)inputStore);
113
                    }
114
                    if(paramName.equals("BANDS")) {
115
                            boolean[] bands = new boolean[inputStore.getBandCount()];
116
                            for (int j = 0; j < bands.length; j++) {
117
                                        bands[j] = true;
118
                                }
119
                            taskStats.addParam(paramName, bands);
120
                    }
121

    
122
                    if(paramName.equals("ROI_EPSG") && useROI) {
123
                            taskStats.addParam(paramName, "EPSG:4326");
124
                    }
125

    
126
                    if(paramName.equals("WINDOW")) {
127
                            AnalysisExtent ext = getAnalysisExtent();
128
                            Extent bbox = RasterLocator.getManager().getDataStructFactory().createExtent(
129
                                            ext.getXMin(), ext.getYMax(), ext.getXMax(), ext.getYMin());
130
                            Extent inputBbox = inputStore.getExtent();
131
                            if(bbox.getULX() != inputBbox.getULX() ||
132
                                    bbox.getULY() != inputBbox.getULY() ||
133
                                    bbox.getLRX() != inputBbox.getLRX() ||
134
                                    bbox.getLRY() != inputBbox.getLRY()) {
135
                                    taskStats.addParam(paramName, bbox);
136
                            }
137
                    }
138
            }
139
            return taskStats;
140
    }
141

    
142
    private DataProcess createPCAProcess(RasterDataStore inputStore, String fileName, PCStatsDataStructure stats) throws ProcessException {
143
            DataProcess taskPC = RasterBaseAlgorithmLibrary.getManager().createRasterTask(PrincipalComponentsAlgorithmLibrary.PC_PROCESS_LABEL);
144
            taskPC.setActions(this);
145
            List<String> params = taskPC.getRasterTaskInputParameters(PrincipalComponentsAlgorithmLibrary.PC_PROCESS_LABEL);
146
            for (int i = 0; i < params.size(); i++) {
147
                    String paramName = params.get(i);
148
                    Class<?> paramType = taskPC.getParameterTypeByProcess(PrincipalComponentsAlgorithmLibrary.PC_PROCESS_LABEL, paramName);
149
                    if(paramType == RasterDataStore.class) {
150
                            taskPC.addParam(paramName, (RasterDataStore)inputStore);
151
                    }
152

    
153
                        if(paramType == Boolean[].class) {
154
                            boolean[] bands = new boolean[inputStore.getBandCount()];
155
                            for (int j = 0; j < bands.length; j++) {
156
                                        bands[j] = true;
157
                                }
158
                            taskPC.addParam(paramName, bands);
159
                        }
160

    
161
                        if(paramType == PCStatsDataStructure.class) {
162
                                taskPC.addParam(paramName, stats);
163
                        }
164

    
165
                        if(paramName.equals("PATH")) {
166
                                taskPC.addParam(paramName, fileName);
167
                        }
168

    
169
                        if(paramName.equals("ROI_EPSG") && useROI) {
170
                                taskPC.addParam(paramName, "EPSG:4326");
171
                    }
172

    
173
                        if(paramName.equals("WINDOW")) {
174
                            AnalysisExtent ext = getAnalysisExtent();
175
                            Extent bbox = RasterLocator.getManager().getDataStructFactory().createExtent(
176
                                            ext.getXMin(), ext.getYMax(), ext.getXMax(), ext.getYMin());
177
                            Extent inputBbox = inputStore.getExtent();
178
                            if(bbox.getULX() != inputBbox.getULX() ||
179
                                    bbox.getULY() != inputBbox.getULY() ||
180
                                    bbox.getLRX() != inputBbox.getLRX() ||
181
                                    bbox.getLRY() != inputBbox.getLRY()) {
182
                                    taskPC.addParam(paramName, bbox);
183
                            }
184
                    }
185
            }
186
            return taskPC;
187
    }
188

    
189
        public void interrupted() {
190

    
191
        }
192

    
193
        public void end(Object param) {
194

    
195
        }
196

    
197
        public void updateProgress(int current, int total) {
198
                boolean cancelled = setProgress(current, total);
199

    
200
                if(!cancelled) {
201
                        if(taskStats != null)
202
                                taskStats.actionCanceled(null);
203
                        if(taskPC != null)
204
                                taskPC.actionCanceled(null);
205
                }
206
        }
207

    
208
    /*
209
     * TODO: Customized panels
210
    @Override
211
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
212
        return PrincipalComponentsParametersPanel.class;
213
    }*/
214
}