Statistics
| Revision:

gvsig-raster / org.gvsig.raster.multifile / trunk / org.gvsig.raster.multifile / org.gvsig.raster.multifile.app.multifileclient / src / main / java / org / gvsig / raster / multifile / app / MainDialogActions.java @ 2178

History | View | Annotate | Download (7.73 KB)

1
package org.gvsig.raster.multifile.app;
2

    
3
import java.io.File;
4
import java.io.IOException;
5
import java.util.ArrayList;
6
import java.util.List;
7

    
8
import org.gvsig.andami.PluginServices;
9
import org.gvsig.andami.ui.mdiManager.IWindow;
10
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
11
import org.gvsig.fmap.dal.coverage.RasterLocator;
12
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
13
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
14
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
15
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
16
import org.gvsig.fmap.mapcontext.MapContextLocator;
17
import org.gvsig.fmap.mapcontext.MapContextManager;
18
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
19
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
20
import org.gvsig.i18n.Messages;
21
import org.gvsig.raster.algorithm.RasterBaseAlgorithmLibrary;
22
import org.gvsig.raster.algorithm.process.DataProcess;
23
import org.gvsig.raster.algorithm.process.IProcessActions;
24
import org.gvsig.raster.algorithm.process.ProcessException;
25
import org.gvsig.raster.fmap.layers.FLyrRaster;
26
import org.gvsig.raster.impl.provider.RasterProvider;
27
import org.gvsig.raster.multifile.app.panel.BandSelectorPanel;
28
import org.gvsig.raster.multifile.io.MultiFileDataParameters;
29
import org.gvsig.raster.multifile.io.MultiFileFormat;
30
import org.gvsig.raster.swing.RasterSwingLibrary;
31

    
32
public class MainDialogActions implements IProcessActions {
33
        private String                          file                 = null;
34
        private String                          folder               = null;
35
        private BandSelectorPanel               panel                = null;
36
        private AbstractViewPanel               selectedView         = null;
37
        
38
        public MainDialogActions(BandSelectorPanel panel, String file, String folder) {
39
                this.file = file;
40
                this.folder = folder;
41
                this.panel = panel;
42
        }
43
        
44
        public void buildOneLayer() {
45
                RasterDataStore mainRasterStore = panel.getResult();
46
                saveMetadata();
47
                
48
                DataProcess clippingProcess = null;
49
                try {
50
                        clippingProcess = RasterBaseAlgorithmLibrary.getManager().createRasterTask("ClippingProcess");
51
                } catch (ProcessException e) {
52
                        RasterSwingLibrary.messageBoxError(Messages.getText("error_processing_layer"), panel, e);
53
                }
54
                clippingProcess.setActions(this);
55
                String tit = PluginServices.getMDIManager().getWindowInfo(getView()).getTitle();
56
                clippingProcess.addParam("viewname", tit);
57
                clippingProcess.addParam("pixelcoordinates", new int[] { 0, (int) mainRasterStore.getHeight(), (int) mainRasterStore.getWidth(), 0 });
58
                clippingProcess.addParam("resolution", new int[]{(int) mainRasterStore.getWidth(),
59
                                                                                                                          (int) mainRasterStore.getHeight()});
60
                clippingProcess.addParam("suffix", ".tif");
61
                
62
                clippingProcess.addParam("filename", folder + File.separator + file);
63
                clippingProcess.addParam("layer", mainRasterStore);
64
                int[] drawableBands = new int[mainRasterStore.getBandCount()];
65
                for (int i = 0; i < drawableBands.length; i++) {
66
                        drawableBands[i] = i;
67
                }
68
                clippingProcess.addParam("drawablebands", drawableBands);
69
                clippingProcess.addParam("onelayerperband", new Boolean(false));
70
                clippingProcess.addParam("interpolationmethod", new Integer(Buffer.INTERPOLATION_NearestNeighbour));
71
                clippingProcess.addParam("affinetransform", mainRasterStore.getAffineTransform());
72

    
73

    
74
                /*if (params != null)
75
                        RasterToolsUtil.loadWriterParamsFromPropertiesPanel(panelProperty, params);
76
                clippingProcess.addParam("driverparams", params);*/
77
                clippingProcess.start();
78
        }
79
        
80
        public void loadLayer() {
81
                RasterDataStore mainRasterStore = panel.getResult();
82
                if(mainRasterStore == null) {
83
                        return;
84
                }
85
                
86
                saveMff(mainRasterStore);
87
                saveMetadata();
88
                try {
89
                        loadLayerInView(mainRasterStore);
90
                } catch (LoadLayerException e) {
91
                        RasterSwingLibrary.messageBoxError(Messages.getText("error_loading_layer"), panel, e);
92
                }
93
        }
94
        
95
        private void saveMff(RasterDataStore mainRasterStore) {
96
                MultiFileDataParameters params = (MultiFileDataParameters)mainRasterStore.getParameters();
97
                ArrayList<File> uriList = new ArrayList<File>();
98
                List<RasterProvider> providers = params.getProviders();
99
                for (int i = 0; i < providers.size(); i++) {
100
                        uriList.add(new File(providers.get(i).getURI()));
101
                }
102
                try {
103
                        MultiFileFormat.saveMultiFileFormat(file, folder, uriList);
104
                } catch (IOException e) {
105
                        RasterSwingLibrary.messageBoxError(Messages.getText("error_salvando_mff"), panel, e);
106
                }
107
        }
108
        
109
        private void saveMetadata() {
110
                RasterDataStore mainRasterStore = panel.getResult();
111
                int rBand = panel.getColorInterpretationByColorBandBand(RasterDataStore.RED_BAND);
112
                int gBand = panel.getColorInterpretationByColorBandBand(RasterDataStore.GREEN_BAND);
113
                int bBand = panel.getColorInterpretationByColorBandBand(RasterDataStore.BLUE_BAND);
114
                int aBand = panel.getColorInterpretationByColorBandBand(RasterDataStore.ALPHA_BAND);
115

    
116
                if (!isCorrectAssignedBand(rBand, gBand, bBand, aBand)) {
117
                        RasterSwingLibrary.messageBoxError(Messages.getText("combinacion_no_asignable"), panel);
118
                        return;
119
                }
120

    
121
                try {
122
                        ColorInterpretation ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(
123
                                        new String[mainRasterStore.getBandCount()]);
124
                        // Combinaci?n GRAY
125
                        if ((rBand == gBand) && (rBand == bBand) && (rBand >= 0)) {
126
                                for (int iBand = 0; iBand < panel.getARGBTable().getRowCount(); iBand++) {
127
                                        ci.setColorInterpValue(iBand, ColorInterpretation.UNDEF_BAND);
128
                                }
129
                                ci.setColorInterpValue(rBand, ColorInterpretation.GRAY_BAND);
130
                                ci.setColorInterpValue(aBand, ColorInterpretation.ALPHA_BAND);
131
                        } else {
132
                                // Combinaci?n RGB
133
                                ci.setColorInterpValue(aBand, ColorInterpretation.ALPHA_BAND);
134
                                ci.setColorInterpValue(rBand, ColorInterpretation.RED_BAND);
135
                                ci.setColorInterpValue(gBand, ColorInterpretation.GREEN_BAND);
136
                                ci.setColorInterpValue(bBand, ColorInterpretation.BLUE_BAND);
137
                        }
138
                        RasterLocator.getManager().getProviderServices().saveObjectToRmfFile(folder + File.separator + file, ci);
139
                } catch (RmfSerializerException exc) {
140
                        RasterSwingLibrary.messageBoxError(Messages.getText("error_salvando_rmf"), panel, exc);
141
                } catch (NotInitializeException exc) {
142
                        RasterSwingLibrary.messageBoxError(Messages.getText("table_not_initialize"), panel, exc);
143
                }
144
        }
145
        
146
        private void loadLayerInView(RasterDataStore mainRasterStore) throws LoadLayerException {
147
                MapContextManager mcm = MapContextLocator.getMapContextManager();
148
                FLyrRaster lyr = (FLyrRaster) mcm.createLayer(file, mainRasterStore);
149
                lyr.reload();
150

    
151
                getView().getMapControl().getMapContext().beginAtomicEvent();
152
                getView().getMapControl().getMapContext().getLayers().addLayer(lyr);
153
                getView().getMapControl().getMapContext().invalidate();
154
                getView().getMapControl().getMapContext().endAtomicEvent();
155
        }
156
        
157
        private AbstractViewPanel getView() {
158
                if(selectedView == null) {
159
                        IWindow[] wList = PluginServices.getMDIManager().getAllWindows();
160
                        for (int i = 0; i < wList.length; i++) {
161
                                if(wList[i] instanceof AbstractViewPanel)
162
                                        selectedView = (AbstractViewPanel)wList[i];
163
                        }
164
                }
165
                return selectedView;
166
        }
167
        
168
        private boolean isCorrectAssignedBand(int r, int g, int b, int a) {
169
                // Si es gris es correcta la asignacion
170
                if ((r == g) && (r == b) && (r >= 0)) {
171
                        // Si el alpha esta asignado a la misma banda es incorrecto
172
                        if (r == a)
173
                                return false;
174
                        // En caso contrario es correcto
175
                        return true;
176
                }
177

    
178
                // Si dos bandas coinciden, se dice que no es correcta la asignacion
179
                int list[] = { r, g, b, a };
180
                for (int i = 0; i <= 3; i++)
181
                        for (int j = 0; j <= 3; j++)
182
                                if ((i != j) && (list[i] == list[j]) && (list[i] > -1))
183
                                        return false;
184

    
185
                return true;
186
        }
187

    
188
        public void interrupted() {
189
                // TODO Auto-generated method stub
190
                
191
        }
192

    
193
        public void end(Object param) {
194
                
195
        }
196

    
197
        public void updateProgress(int current, int total) {
198
                // TODO Auto-generated method stub
199
                
200
        }
201
}