root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / cutting / CuttingProcess.java @ 13328
History | View | Annotate | Download (10.2 KB)
1 | 11352 | bsanchez | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | */
|
||
19 | package org.gvsig.rastertools.cutting; |
||
20 | |||
21 | 11900 | nacho | import java.awt.Component; |
22 | 12793 | nacho | import java.awt.geom.AffineTransform; |
23 | 11900 | nacho | import java.io.File; |
24 | 11352 | bsanchez | import java.io.IOException; |
25 | |||
26 | 11900 | nacho | import javax.swing.JOptionPane; |
27 | |||
28 | 12425 | nacho | import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
29 | 11352 | bsanchez | import org.gvsig.gui.beans.incrementabletask.IncrementableEvent; |
30 | import org.gvsig.gui.beans.incrementabletask.IncrementableListener; |
||
31 | import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
||
32 | 11384 | bsanchez | import org.gvsig.raster.buffer.BufferFactory; |
33 | 11480 | nacho | import org.gvsig.raster.buffer.BufferInterpolation; |
34 | import org.gvsig.raster.buffer.RasterBuffer; |
||
35 | 11352 | bsanchez | import org.gvsig.raster.dataset.GeoRasterWriter; |
36 | import org.gvsig.raster.dataset.IBuffer; |
||
37 | 13328 | nacho | import org.gvsig.raster.dataset.IRasterDataSource; |
38 | 11414 | nacho | import org.gvsig.raster.dataset.InvalidSetViewException; |
39 | 11352 | bsanchez | import org.gvsig.raster.dataset.NotSupportedExtensionException; |
40 | 11931 | nacho | import org.gvsig.raster.dataset.Params; |
41 | 11352 | bsanchez | import org.gvsig.raster.dataset.RasterDriverException; |
42 | 11900 | nacho | |
43 | import com.iver.andami.PluginServices; |
||
44 | 12502 | nacho | import com.iver.andami.messages.NotificationManager; |
45 | 11900 | nacho | import com.iver.andami.ui.mdiManager.IWindow; |
46 | import com.iver.cit.gvsig.exceptions.layers.LoadLayerException; |
||
47 | import com.iver.cit.gvsig.fmap.layers.FLayer; |
||
48 | import com.iver.cit.gvsig.project.documents.view.gui.View; |
||
49 | 11352 | bsanchez | /**
|
50 | 11393 | bsanchez | * <code>CuttingProcess</code> es un proceso que usa un <code>Thread</code>
|
51 | * para calcular el recorte de una capa.
|
||
52 | 12369 | bsanchez | *
|
53 | 11352 | bsanchez | * @version 24/04/2007
|
54 | 12369 | bsanchez | * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
55 | 11352 | bsanchez | */
|
56 | public class CuttingProcess implements Runnable, IncrementableListener { |
||
57 | 11947 | nacho | private String fileName = ""; |
58 | private IncrementableTask incrementableTask = null; |
||
59 | private volatile Thread blinker = null; |
||
60 | private WriterBufferServer writerBufferServer = null; |
||
61 | 13328 | nacho | private IRasterDataSource raster = null; |
62 | 12793 | nacho | private AffineTransform at = null; |
63 | 11947 | nacho | private boolean oneLayerPerBand = false; |
64 | private int[] drawableBands = null; |
||
65 | private int[] dValues = null; |
||
66 | private boolean cancel = false; |
||
67 | private GeoRasterWriter grw = null; |
||
68 | private int interpMethod = BufferInterpolation.INTERPOLATION_Undefined; |
||
69 | private boolean loadToc = false; |
||
70 | private String viewName = ""; |
||
71 | private Params params = null; |
||
72 | 12369 | bsanchez | |
73 | 11352 | bsanchez | /**
|
74 | 11393 | bsanchez | * Variables de la resoluci?n de salida
|
75 | */
|
||
76 | 11815 | bsanchez | private int resolutionWidth = 0, resolutionHeight = 0; |
77 | 11393 | bsanchez | |
78 | /**
|
||
79 | * Crea un <code>CuttingProcess</code> para generar un recorte
|
||
80 | 11900 | nacho | * @param loadToc
|
81 | 11393 | bsanchez | * @param dValues
|
82 | * @param fileName
|
||
83 | * @param writerBufferServer
|
||
84 | * @param rasterMultiDataset
|
||
85 | * @param extent
|
||
86 | * @param drawableBands
|
||
87 | * @param oneLayerPerBand
|
||
88 | */
|
||
89 | 12369 | bsanchez | public CuttingProcess(boolean loadToc, String viewName, int[] dValues, String fileName, |
90 | 13328 | nacho | WriterBufferServer writerBufferServer, IRasterDataSource raster, |
91 | 12793 | nacho | AffineTransform at, int[] drawableBands, boolean oneLayerPerBand, int interpMethod) { |
92 | 11352 | bsanchez | this.fileName = fileName;
|
93 | this.writerBufferServer = writerBufferServer;
|
||
94 | 13328 | nacho | this.raster = raster;
|
95 | 12793 | nacho | this.at = at;
|
96 | 11384 | bsanchez | this.oneLayerPerBand = oneLayerPerBand;
|
97 | this.drawableBands = drawableBands;
|
||
98 | 11900 | nacho | this.loadToc = loadToc;
|
99 | 11392 | bsanchez | this.dValues = dValues;
|
100 | 11900 | nacho | this.viewName = viewName;
|
101 | 11480 | nacho | this.interpMethod = interpMethod;
|
102 | 11352 | bsanchez | } |
103 | 12369 | bsanchez | |
104 | 11352 | bsanchez | /**
|
105 | 11393 | bsanchez | * Arranca el proceso de recorte de un layer
|
106 | 11352 | bsanchez | */
|
107 | public void start() { |
||
108 | 11423 | nacho | cancel = false;
|
109 | 11352 | bsanchez | blinker = new Thread(this); |
110 | blinker.start(); |
||
111 | } |
||
112 | |||
113 | /**
|
||
114 | 11393 | bsanchez | * Establecer la resolucion de salida al crear el recorte
|
115 | * @param width
|
||
116 | * @param height
|
||
117 | 11352 | bsanchez | */
|
118 | 11393 | bsanchez | public void setResolution(int width, int height) { |
119 | resolutionWidth = width; |
||
120 | resolutionHeight = height; |
||
121 | 11352 | bsanchez | } |
122 | 11384 | bsanchez | |
123 | 11352 | bsanchez | /**
|
124 | 11408 | bsanchez | * M?todo donde se ejecutar? el Thread, aqu? se genera el recorte
|
125 | 11352 | bsanchez | */
|
126 | public void run() { |
||
127 | try {
|
||
128 | 13328 | nacho | IRasterDataSource d = raster.copy(); |
129 | 12163 | nacho | BufferFactory bufferFactory = new BufferFactory(d);
|
130 | 12152 | nacho | bufferFactory.setDrawableBands(drawableBands); |
131 | 12369 | bsanchez | |
132 | 11423 | nacho | if(cancel)
|
133 | return;
|
||
134 | 12369 | bsanchez | |
135 | 11480 | nacho | IBuffer buffer = null;
|
136 | 12369 | bsanchez | |
137 | 11480 | nacho | if( interpMethod != BufferInterpolation.INTERPOLATION_Undefined &&
|
138 | interpMethod != BufferInterpolation.INTERPOLATION_NearestNeighbour) { |
||
139 | bufferFactory.setAreaOfInterest(dValues[0], dValues[3], dValues[2] - dValues[0], dValues[1] - dValues[3]); |
||
140 | buffer = bufferFactory.getRasterBuf(); |
||
141 | buffer = ((RasterBuffer)buffer).getAdjustedWindow(resolutionWidth, resolutionHeight, interpMethod); |
||
142 | } else {
|
||
143 | try {
|
||
144 | 11900 | nacho | bufferFactory.setAreaOfInterest(dValues[0], dValues[3], Math.abs(dValues[2] - dValues[0]), Math.abs(dValues[1] - dValues[3]), resolutionWidth, resolutionHeight); |
145 | 11480 | nacho | buffer = bufferFactory.getRasterBuf(); |
146 | } catch (InvalidSetViewException e) {
|
||
147 | 12502 | nacho | NotificationManager.addError("No se ha podido asignar la vista al inicial el proceso de recorte.", e);
|
148 | 11480 | nacho | } |
149 | } |
||
150 | 12470 | nacho | //TODO: FUNCIONALIDAD: Poner los gerWriter con la proyecci?n del fichero fuente
|
151 | 11384 | bsanchez | if (oneLayerPerBand) {
|
152 | for (int i = 0; i < drawableBands.length; i++) { |
||
153 | 11423 | nacho | if(cancel)
|
154 | return;
|
||
155 | 11414 | nacho | String f = fileName + "_B" + drawableBands[i] + ".tif"; |
156 | writerBufferServer.setBuffer(buffer, i); |
||
157 | 11931 | nacho | Params p = null;
|
158 | if(params == null) |
||
159 | p = GeoRasterWriter.getWriter(f).getParams(); |
||
160 | 12369 | bsanchez | else
|
161 | 11931 | nacho | p = params; |
162 | 11423 | nacho | grw = GeoRasterWriter.getWriter(writerBufferServer, f, |
163 | 12793 | nacho | 1, at, buffer.getWidth(), buffer.getHeight(),
|
164 | 12514 | bsanchez | buffer.getDataType(), p, null);
|
165 | 12494 | nacho | grw.setWkt(d.getWktProjection()); |
166 | 11414 | nacho | grw.dataWrite(); |
167 | grw.writeClose(); |
||
168 | 11423 | nacho | if(!cancel)
|
169 | 11900 | nacho | cutFinalize(f); |
170 | 11384 | bsanchez | } |
171 | } else {
|
||
172 | 11414 | nacho | writerBufferServer.setBuffer(buffer, -1);
|
173 | 11947 | nacho | if(params == null) { |
174 | grw = GeoRasterWriter.getWriter(writerBufferServer, fileName + ".tif",
|
||
175 | 12793 | nacho | buffer.getBandCount(), at, buffer.getWidth(), buffer.getHeight(), |
176 | 12470 | nacho | buffer.getDataType(), GeoRasterWriter.getWriter(fileName + ".tif").getParams(), null); |
177 | 12369 | bsanchez | } else
|
178 | 11947 | nacho | grw = GeoRasterWriter.getWriter(writerBufferServer, fileName, |
179 | 12793 | nacho | buffer.getBandCount(), at, buffer.getWidth(), buffer.getHeight(), |
180 | 12470 | nacho | buffer.getDataType(), params, null);
|
181 | 12494 | nacho | grw.setWkt(d.getWktProjection()); |
182 | 11414 | nacho | grw.dataWrite(); |
183 | grw.writeClose(); |
||
184 | 11947 | nacho | if(!cancel) {
|
185 | if(params == null) |
||
186 | cutFinalize(fileName + ".tif");
|
||
187 | else
|
||
188 | cutFinalize(fileName); |
||
189 | } |
||
190 | 11384 | bsanchez | } |
191 | 12163 | nacho | d.close(); |
192 | 11352 | bsanchez | } catch (NotSupportedExtensionException e) {
|
193 | 12502 | nacho | NotificationManager.addError("No se ha podido obtener el writer. Extensi?n no soportada", e);
|
194 | 11352 | bsanchez | } catch (RasterDriverException e) {
|
195 | 12502 | nacho | NotificationManager.addError("No se ha podido obtener el writer.", e);
|
196 | 11352 | bsanchez | } catch (IOException e) { |
197 | 12502 | nacho | NotificationManager.addError("Error en la escritura en GeoRasterWriter.", e);
|
198 | 11352 | bsanchez | } |
199 | 12369 | bsanchez | |
200 | 11352 | bsanchez | if (incrementableTask != null) |
201 | incrementableTask.processFinalize(); |
||
202 | } |
||
203 | |||
204 | /**
|
||
205 | 11408 | bsanchez | * Establece el <code>IncrementableTask</code>
|
206 | 11352 | bsanchez | * @param value
|
207 | */
|
||
208 | public void setIncrementableTask(IncrementableTask value) { |
||
209 | incrementableTask = value; |
||
210 | incrementableTask.addIncrementableListener(this);
|
||
211 | } |
||
212 | |||
213 | 11408 | bsanchez | /*
|
214 | * (non-Javadoc)
|
||
215 | * @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionCanceled(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
|
||
216 | */
|
||
217 | 11352 | bsanchez | public void actionCanceled(IncrementableEvent e) { |
218 | 11423 | nacho | if(writerBufferServer != null) |
219 | writerBufferServer.setCanceled(true, 0); |
||
220 | if(grw != null) |
||
221 | grw.writeCancel(); |
||
222 | cancel = true;
|
||
223 | 11352 | bsanchez | } |
224 | |||
225 | 11408 | bsanchez | /*
|
226 | * (non-Javadoc)
|
||
227 | * @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionResumed(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
|
||
228 | */
|
||
229 | 11352 | bsanchez | public void actionResumed(IncrementableEvent e) { |
230 | } |
||
231 | |||
232 | 11408 | bsanchez | /*
|
233 | * (non-Javadoc)
|
||
234 | * @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionSuspended(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
|
||
235 | */
|
||
236 | 11352 | bsanchez | public void actionSuspended(IncrementableEvent e) { |
237 | } |
||
238 | 12369 | bsanchez | |
239 | 11900 | nacho | /**
|
240 | * Acciones que se realizan al finalizar de crear los recortes de imagen.
|
||
241 | * Este m?todo es llamado por el thread TailRasterProcess al finalizar.
|
||
242 | */
|
||
243 | private void cutFinalize(String fileName) { |
||
244 | if (!loadToc)
|
||
245 | return;
|
||
246 | if(!new File(fileName).exists()) |
||
247 | return;
|
||
248 | // seleccionamos la vista de gvSIG
|
||
249 | com.iver.cit.gvsig.project.documents.view.gui.View theView = null;
|
||
250 | try {
|
||
251 | IWindow[] allViews = PluginServices.getMDIManager().getAllWindows();
|
||
252 | for (int i = 0; i < allViews.length; i++) { |
||
253 | if (allViews[i] instanceof com.iver.cit.gvsig.project.documents.view.gui.View |
||
254 | && PluginServices.getMDIManager().getWindowInfo((View) allViews[i]).getTitle().equals(viewName))
|
||
255 | theView = (com.iver.cit.gvsig.project.documents.view.gui.View) allViews[i]; |
||
256 | } |
||
257 | if (theView == null) |
||
258 | return;
|
||
259 | } catch (ClassCastException ex) { |
||
260 | // logger.error(PluginServices.getText(this,"cant_get_view "), ex);
|
||
261 | return;
|
||
262 | } |
||
263 | |||
264 | // Cargamos las capas
|
||
265 | theView.getMapControl().getMapContext().beginAtomicEvent(); |
||
266 | 12369 | bsanchez | |
267 | 11900 | nacho | //Driver driver = LayerFactory.getDM().getDriver("gvSIG Raster Driver");
|
268 | int endIndex = fileName.lastIndexOf("."); |
||
269 | if (endIndex < 0) |
||
270 | endIndex = fileName.length(); |
||
271 | FLayer lyr = null;
|
||
272 | try {
|
||
273 | lyr = FLyrRasterSE.createLayer(fileName.substring( |
||
274 | fileName.lastIndexOf(File.separator) + 1, endIndex), |
||
275 | new File(fileName), theView.getMapControl().getProjection()); |
||
276 | } catch (LoadLayerException e) {
|
||
277 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
||
278 | PluginServices.getText(this, "error_cargar_capa")); |
||
279 | } |
||
280 | theView.getMapControl().getMapContext().getLayers().addLayer(lyr); |
||
281 | 12369 | bsanchez | |
282 | 12163 | nacho | theView.getMapControl().getMapContext().invalidate(); |
283 | 11900 | nacho | theView.getMapControl().getMapContext().endAtomicEvent(); |
284 | // grid.free();
|
||
285 | 12369 | bsanchez | |
286 | 11900 | nacho | } |
287 | 11931 | nacho | |
288 | /**
|
||
289 | 12369 | bsanchez | * Asigna los par?metros si estos han de ser cambiados. En caso de
|
290 | 11931 | nacho | * que no se asignen se usaran los que tiene el driver por defecto.
|
291 | * @param params Par?metros del driver de escritura.
|
||
292 | */
|
||
293 | public void setParams(Params params) { |
||
294 | this.params = params;
|
||
295 | } |
||
296 | 11352 | bsanchez | } |