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 @ 5988
History | View | Annotate | Download (7.83 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 |
/**
|
33 |
* @author gvSIG team
|
34 |
*
|
35 |
*/
|
36 |
public class MainDialogActions implements IProcessActions { |
37 |
private String file = null; |
38 |
private String folder = null; |
39 |
private BandSelectorPanel panel = null; |
40 |
private AbstractViewPanel selectedView = null; |
41 |
|
42 |
/**
|
43 |
* @param panel
|
44 |
* @param file
|
45 |
* @param folder
|
46 |
*/
|
47 |
public MainDialogActions(BandSelectorPanel panel, String file, String folder) { |
48 |
this.file = file;
|
49 |
this.folder = folder;
|
50 |
this.panel = panel;
|
51 |
} |
52 |
|
53 |
/**
|
54 |
*
|
55 |
*/
|
56 |
public void buildOneLayer() { |
57 |
RasterDataStore mainRasterStore = panel.getResult(); |
58 |
saveMetadata(); |
59 |
|
60 |
DataProcess clippingProcess = null;
|
61 |
try {
|
62 |
clippingProcess = RasterBaseAlgorithmLibrary.getManager().createRasterTask("ClippingProcess");
|
63 |
} catch (ProcessException e) {
|
64 |
RasterSwingLibrary.messageBoxError(Messages.getText("error_processing_layer"), panel, e);
|
65 |
} |
66 |
clippingProcess.setActions(this);
|
67 |
String tit = PluginServices.getMDIManager().getWindowInfo(getView()).getTitle();
|
68 |
clippingProcess.addParam("viewname", tit);
|
69 |
clippingProcess.addParam("pixelcoordinates", new int[] { 0, (int) mainRasterStore.getHeight(), (int) mainRasterStore.getWidth(), 0 }); |
70 |
clippingProcess.addParam("resolution", new int[]{(int) mainRasterStore.getWidth(), |
71 |
(int) mainRasterStore.getHeight()});
|
72 |
clippingProcess.addParam("suffix", ".tif"); |
73 |
|
74 |
clippingProcess.addParam("filename", folder + File.separator + file); |
75 |
clippingProcess.addParam("layer", mainRasterStore);
|
76 |
int[] drawableBands = new int[mainRasterStore.getBandCount()]; |
77 |
for (int i = 0; i < drawableBands.length; i++) { |
78 |
drawableBands[i] = i; |
79 |
} |
80 |
clippingProcess.addParam("drawablebands", drawableBands);
|
81 |
clippingProcess.addParam("onelayerperband", new Boolean(false)); |
82 |
clippingProcess.addParam("interpolationmethod", new Integer(Buffer.INTERPOLATION_NearestNeighbour)); |
83 |
clippingProcess.addParam("affinetransform", mainRasterStore.getAffineTransform());
|
84 |
|
85 |
|
86 |
/*if (params != null)
|
87 |
RasterToolsUtil.loadWriterParamsFromPropertiesPanel(panelProperty, params);
|
88 |
clippingProcess.addParam("driverparams", params);*/
|
89 |
clippingProcess.start(); |
90 |
} |
91 |
|
92 |
/**
|
93 |
*
|
94 |
*/
|
95 |
public void loadLayer() { |
96 |
RasterDataStore mainRasterStore = panel.getResult(); |
97 |
if(mainRasterStore == null) { |
98 |
return;
|
99 |
} |
100 |
|
101 |
saveMff(mainRasterStore); |
102 |
saveMetadata(); |
103 |
try {
|
104 |
loadLayerInView(mainRasterStore); |
105 |
} catch (LoadLayerException e) {
|
106 |
RasterSwingLibrary.messageBoxError(Messages.getText("error_loading_layer"), panel, e);
|
107 |
} |
108 |
} |
109 |
|
110 |
private void saveMff(RasterDataStore mainRasterStore) { |
111 |
MultiFileDataParameters params = (MultiFileDataParameters)mainRasterStore.getParameters(); |
112 |
ArrayList<File> uriList = new ArrayList<File>(); |
113 |
List<RasterProvider> providers = params.getProviders();
|
114 |
for (int i = 0; i < providers.size(); i++) { |
115 |
uriList.add(new File(providers.get(i).getURI())); |
116 |
} |
117 |
try {
|
118 |
MultiFileFormat.saveMultiFileFormat(file, folder, uriList); |
119 |
} catch (IOException e) { |
120 |
RasterSwingLibrary.messageBoxError(Messages.getText("error_salvando_mff"), panel, e);
|
121 |
} |
122 |
} |
123 |
|
124 |
private void saveMetadata() { |
125 |
RasterDataStore mainRasterStore = panel.getResult(); |
126 |
int rBand = panel.getColorInterpretationByColorBandBand(RasterDataStore.RED_BAND);
|
127 |
int gBand = panel.getColorInterpretationByColorBandBand(RasterDataStore.GREEN_BAND);
|
128 |
int bBand = panel.getColorInterpretationByColorBandBand(RasterDataStore.BLUE_BAND);
|
129 |
int aBand = panel.getColorInterpretationByColorBandBand(RasterDataStore.ALPHA_BAND);
|
130 |
|
131 |
if (!isCorrectAssignedBand(rBand, gBand, bBand, aBand)) {
|
132 |
RasterSwingLibrary.messageBoxError(Messages.getText("combinacion_no_asignable"), panel);
|
133 |
return;
|
134 |
} |
135 |
|
136 |
try {
|
137 |
ColorInterpretation ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation( |
138 |
new String[mainRasterStore.getBandCount()]); |
139 |
// Combinaci?n GRAY
|
140 |
if ((rBand == gBand) && (rBand == bBand) && (rBand >= 0)) { |
141 |
for (int iBand = 0; iBand < panel.getARGBTable().getRowCount(); iBand++) { |
142 |
ci.setColorInterpValue(iBand, ColorInterpretation.UNDEF_BAND); |
143 |
} |
144 |
ci.setColorInterpValue(rBand, ColorInterpretation.GRAY_BAND); |
145 |
ci.setColorInterpValue(aBand, ColorInterpretation.ALPHA_BAND); |
146 |
} else {
|
147 |
// Combinaci?n RGB
|
148 |
ci.setColorInterpValue(aBand, ColorInterpretation.ALPHA_BAND); |
149 |
ci.setColorInterpValue(rBand, ColorInterpretation.RED_BAND); |
150 |
ci.setColorInterpValue(gBand, ColorInterpretation.GREEN_BAND); |
151 |
ci.setColorInterpValue(bBand, ColorInterpretation.BLUE_BAND); |
152 |
} |
153 |
RasterLocator.getManager().getProviderServices().saveObjectToRmfFile(folder + File.separator + file, ci);
|
154 |
} catch (RmfSerializerException exc) {
|
155 |
RasterSwingLibrary.messageBoxError(Messages.getText("error_salvando_rmf"), panel, exc);
|
156 |
} catch (NotInitializeException exc) {
|
157 |
RasterSwingLibrary.messageBoxError(Messages.getText("table_not_initialize"), panel, exc);
|
158 |
} |
159 |
} |
160 |
|
161 |
private void loadLayerInView(RasterDataStore mainRasterStore) throws LoadLayerException { |
162 |
MapContextManager mcm = MapContextLocator.getMapContextManager(); |
163 |
FLyrRaster lyr = (FLyrRaster) mcm.createLayer(file, mainRasterStore); |
164 |
lyr.reload(); |
165 |
|
166 |
getView().getMapControl().getMapContext().beginAtomicEvent(); |
167 |
getView().getMapControl().getMapContext().getLayers().addLayer(lyr); |
168 |
getView().getMapControl().getMapContext().invalidate(); |
169 |
getView().getMapControl().getMapContext().endAtomicEvent(); |
170 |
} |
171 |
|
172 |
private AbstractViewPanel getView() {
|
173 |
if(selectedView == null) { |
174 |
IWindow[] wList = PluginServices.getMDIManager().getAllWindows();
|
175 |
for (int i = 0; i < wList.length; i++) { |
176 |
if(wList[i] instanceof AbstractViewPanel) |
177 |
selectedView = (AbstractViewPanel)wList[i]; |
178 |
} |
179 |
} |
180 |
return selectedView;
|
181 |
} |
182 |
|
183 |
private boolean isCorrectAssignedBand(int r, int g, int b, int a) { |
184 |
// Si es gris es correcta la asignacion
|
185 |
if ((r == g) && (r == b) && (r >= 0)) { |
186 |
// Si el alpha esta asignado a la misma banda es incorrecto
|
187 |
if (r == a)
|
188 |
return false; |
189 |
// En caso contrario es correcto
|
190 |
return true; |
191 |
} |
192 |
|
193 |
// Si dos bandas coinciden, se dice que no es correcta la asignacion
|
194 |
int list[] = { r, g, b, a }; |
195 |
for (int i = 0; i <= 3; i++) |
196 |
for (int j = 0; j <= 3; j++) |
197 |
if ((i != j) && (list[i] == list[j]) && (list[i] > -1)) |
198 |
return false; |
199 |
|
200 |
return true; |
201 |
} |
202 |
|
203 |
public void interrupted() { |
204 |
// TODO Auto-generated method stub
|
205 |
|
206 |
} |
207 |
|
208 |
public void end(Object param) { |
209 |
|
210 |
} |
211 |
|
212 |
public void updateProgress(int current, int total) { |
213 |
// TODO Auto-generated method stub
|
214 |
|
215 |
} |
216 |
} |