Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / branches / org.gvsig.raster.tools_dataaccess_refactoring / org.gvsig.raster.tools.toolbox.algorithm / src / main / java / org / gvsig / raster / tools / LayerDatatypeSextanteAlgorithm.java @ 2385

History | View | Annotate | Download (7.46 KB)

1
package org.gvsig.raster.tools;
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

    
18
import es.unex.sextante.core.AnalysisExtent;
19
import es.unex.sextante.core.Sextante;
20
import es.unex.sextante.dataObjects.IRasterLayer;
21
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
22
import es.unex.sextante.exceptions.NullParameterAdditionalInfoException;
23
import es.unex.sextante.exceptions.NullParameterValueException;
24
import es.unex.sextante.exceptions.RepeatedParameterNameException;
25
import es.unex.sextante.exceptions.WrongParameterIDException;
26
import es.unex.sextante.exceptions.WrongParameterTypeException;
27

    
28
/**
29
 * Process to change a layer of data type
30
 * @author Nacho Brodin (nachobrodin@gmail.com)
31
 */
32
public class LayerDatatypeSextanteAlgorithm extends AbstractSextanteGeoProcess implements IProcessActions {
33
    public static final String RESULT            = "RESULT";
34
    public static final String LAYER             = "RasterStore1";
35
        public static final String DATATYPE          = "Datatype";
36
        public static final String ADJUST_DEC2INT    = "AdjustDec2Int";
37
        public static final String ADJUST_BIG2SMALL  = "AdjustBig2Small";
38
        
39
        public static String[]    DEC2INT_OPTIONS    = new String[]{"Trunk", "Round", "Ceil", "Floor"};
40
        public static String[]    BIG2SMALL_OPTIONS  = new String[]{"Trunk", "Maxvalue", "NoData"};
41
        public static String[]    DATATYPES          = new String[]{"Byte", "UShort", "Short", "Integer", "Float", "Double"};
42
        
43
    private DataProcess        task            = null;
44
    
45

    
46
    public void defineCharacteristics() {
47
        setName(getTranslation("layer_datatype"));
48
        setGroup(getTranslation("raster_layer"));
49
       
50
        try {
51
            m_Parameters.addInputRasterLayer(LAYER, getTranslation("Input_layer"), true);
52
            m_Parameters.addSelection(DATATYPE, getTranslation("dst_datatype"), DATATYPES);
53
            m_Parameters.addSelection(ADJUST_DEC2INT, getTranslation("dec_2_int"), DEC2INT_OPTIONS);
54
            m_Parameters.addSelection(ADJUST_BIG2SMALL, getTranslation("big_2_small"), BIG2SMALL_OPTIONS);
55
        } catch (RepeatedParameterNameException e) {
56
            Sextante.addErrorToLog(e);
57
        }
58
        addOutputRasterLayer(RESULT, getTranslation("principalcomponents"));
59
    }
60

    
61
    public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
62

    
63
            if(existsOutPutFile(LayerDatatypeSextanteAlgorithm.RESULT, 0)) {
64
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
65
            }
66
            
67
            IRasterLayer input = m_Parameters.getParameterValueAsRasterLayer(LAYER);
68
            
69
            FLyrRaster lyrRaster = ((FLyrRaster)input.getBaseDataObject());
70
            IRasterLayer output = null;
71

    
72
            output = getNewRORasterLayer(
73
                            RESULT, 
74
                            Sextante.getText("layerdatatype_description"), 
75
                            input.getDataType(), 
76
                            input.getBandsCount());
77

    
78
            String fileName = ((FLyrRasterIRasterLayer)output).getFileName(); //getOutPutFile(RESULT);
79

    
80
            try {
81
                    setProgressText(getTranslation("convert_datatype"));
82
                        task = createLayerDatatypeProcess(lyrRaster.getDataStore(), fileName);
83
                        task.execute();
84
                        HashMap<String, Object> params = task.getResult();
85
                        fileName = (String)params.get("FileName");
86
                        
87
                        ((FLyrRasterIRasterLayer)output).setBaseDataObject(fileName);
88
                } catch (ProcessInterruptedException e) {
89
                        Sextante.addErrorToLog(e);
90
                } catch (ProcessException e) {
91
                        Sextante.addErrorToLog(e);
92
                } 
93

    
94
                if(getTaskMonitor().isCanceled())
95
                        return false;
96

    
97
        return true;
98
    }
99
    
100
    /**
101
     * Creates a process to calculate statistics
102
     * @param inputStore
103
     * @return
104
     * @throws ProcessException
105
     * @throws NullParameterAdditionalInfoException 
106
     * @throws NullParameterValueException 
107
     * @throws WrongParameterIDException 
108
     * @throws WrongParameterTypeException 
109
     */
110
    private DataProcess createLayerDatatypeProcess(RasterDataStore inputStore, String fileName) throws ProcessException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException, NullParameterAdditionalInfoException {
111
            DataProcess taskStats = RasterBaseAlgorithmLibrary.getManager().createRasterTask("LayerDatatypeProcess");
112
            taskStats.setActions(this);
113
            List<String> params = taskStats.getRasterTaskInputParameters("LayerDatatypeProcess");
114
            for (int i = 0; i < params.size(); i++) {
115
                    String paramName = params.get(i);
116
                    Class<?> paramType = taskStats.getParameterTypeByProcess("LayerDatatypeProcess", paramName);
117
                    if(paramType == RasterDataStore.class) {
118
                            taskStats.addParam(paramName, (RasterDataStore)inputStore);
119
                    }
120
                    
121
                    if(paramName.equals("Path")) {
122
                            taskStats.addParam(paramName, fileName);
123
                    }
124
                    
125
                    if(paramName.equals(DATATYPE)) {
126
                            String value = m_Parameters.getParameterValueAsString(DATATYPE);
127
                            int position = 0;
128
                            for (int j = 0; j < DATATYPES.length; j++) {
129
                                        if(DATATYPES[j].equals(value))
130
                                                position = j;
131
                                }
132
                            taskStats.addParam(paramName, position);
133
                    }
134
                    
135
                    if(paramName.equals(ADJUST_DEC2INT)) {
136
                            String value = m_Parameters.getParameterValueAsString(ADJUST_DEC2INT);
137
                            int position = 0;
138
                            for (int j = 0; j < DEC2INT_OPTIONS.length; j++) {
139
                                        if(DEC2INT_OPTIONS[j].equals(value))
140
                                                position = j;
141
                                }
142
                            taskStats.addParam(paramName, position);
143
                    }
144
                    
145
                    if(paramName.equals(ADJUST_BIG2SMALL)) {
146
                            String value = m_Parameters.getParameterValueAsString(ADJUST_BIG2SMALL);
147
                            int position = 0;
148
                            for (int j = 0; j < BIG2SMALL_OPTIONS.length; j++) {
149
                                        if(BIG2SMALL_OPTIONS[j].equals(value))
150
                                                position = j;
151
                                }
152
                            taskStats.addParam(paramName, position);
153
                    }
154

    
155
                    if(paramName.equals("WINDOW")) {
156
                            AnalysisExtent ext = getAnalysisExtent();
157
                            Extent bbox = RasterLocator.getManager().getDataStructFactory().createExtent(
158
                                            ext.getXMin(), ext.getYMax(), ext.getXMax(), ext.getYMin());
159
                            Extent inputBbox = inputStore.getExtent();
160
                            if(bbox.getULX() != inputBbox.getULX() || 
161
                                    bbox.getULY() != inputBbox.getULY() || 
162
                                    bbox.getLRX() != inputBbox.getLRX() || 
163
                                    bbox.getLRY() != inputBbox.getLRY()) {
164
                                    taskStats.addParam(paramName, bbox);
165
                            }
166
                    }
167
            }
168
            return taskStats;
169
    }
170
    
171
        public void interrupted() {
172
                
173
        }
174

    
175
        public void end(Object param) {
176
                
177
        }
178

    
179
        public void updateProgress(int current, int total) {
180
                boolean cancelled = setProgress(current, total);
181
                
182
                if(!cancelled) {
183
                        if(task != null)
184
                                task.actionCanceled(null);
185
                }
186
        }
187
    
188
    /*
189
     * TODO: Customized panels
190
    @Override
191
    public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() {
192
        return PrincipalComponentsParametersPanel.class;
193
    }*/
194
}