root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / cutting / CuttingProcess.java @ 11393
History | View | Annotate | Download (5.7 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 | import java.io.IOException; |
||
22 | |||
23 | import org.gvsig.gui.beans.incrementabletask.IncrementableEvent; |
||
24 | import org.gvsig.gui.beans.incrementabletask.IncrementableListener; |
||
25 | import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
||
26 | 11384 | bsanchez | import org.gvsig.raster.buffer.BufferFactory; |
27 | 11352 | bsanchez | import org.gvsig.raster.dataset.GeoRasterWriter; |
28 | import org.gvsig.raster.dataset.IBuffer; |
||
29 | import org.gvsig.raster.dataset.NotSupportedExtensionException; |
||
30 | import org.gvsig.raster.dataset.RasterDriverException; |
||
31 | 11384 | bsanchez | import org.gvsig.raster.dataset.RasterMultiDataset; |
32 | 11352 | bsanchez | import org.gvsig.raster.dataset.WriterParams; |
33 | import org.gvsig.raster.shared.Extent; |
||
34 | 11384 | bsanchez | import org.gvsig.rastertools.cutting.ui.listener.CuttingPanelListener; |
35 | 11352 | bsanchez | |
36 | /**
|
||
37 | 11393 | bsanchez | * <code>CuttingProcess</code> es un proceso que usa un <code>Thread</code>
|
38 | * para calcular el recorte de una capa.
|
||
39 | 11352 | bsanchez | *
|
40 | * @version 24/04/2007
|
||
41 | * @author Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
||
42 | */
|
||
43 | public class CuttingProcess implements Runnable, IncrementableListener { |
||
44 | 11384 | bsanchez | private String fileName = ""; |
45 | private IncrementableTask incrementableTask = null; |
||
46 | private volatile Thread blinker = null; |
||
47 | private WriterBufferServer writerBufferServer = null; |
||
48 | private RasterMultiDataset rasterMultiDataset = null; |
||
49 | private Extent extent = null; |
||
50 | private boolean oneLayerPerBand = false; |
||
51 | private int[] drawableBands = null; |
||
52 | 11392 | bsanchez | private CuttingPanelListener cuttingPanelListener = null; |
53 | private int[] dValues = null; |
||
54 | 11384 | bsanchez | |
55 | 11352 | bsanchez | /**
|
56 | 11393 | bsanchez | * Variables de la resoluci?n de salida
|
57 | */
|
||
58 | private int resolutionWidth = 0, resolutionHeight = 0; |
||
59 | |||
60 | /**
|
||
61 | * Crea un <code>CuttingProcess</code> para generar un recorte
|
||
62 | * @param cuttingPanelListener
|
||
63 | * @param dValues
|
||
64 | * @param fileName
|
||
65 | * @param writerBufferServer
|
||
66 | * @param rasterMultiDataset
|
||
67 | * @param extent
|
||
68 | * @param drawableBands
|
||
69 | * @param oneLayerPerBand
|
||
70 | */
|
||
71 | 11392 | bsanchez | public CuttingProcess(CuttingPanelListener cuttingPanelListener, int[] dValues, String fileName, WriterBufferServer writerBufferServer, RasterMultiDataset rasterMultiDataset, Extent extent, int[] drawableBands, boolean oneLayerPerBand) { |
72 | 11352 | bsanchez | this.fileName = fileName;
|
73 | this.writerBufferServer = writerBufferServer;
|
||
74 | 11384 | bsanchez | this.rasterMultiDataset = rasterMultiDataset;
|
75 | 11352 | bsanchez | this.extent = extent;
|
76 | 11384 | bsanchez | this.oneLayerPerBand = oneLayerPerBand;
|
77 | this.drawableBands = drawableBands;
|
||
78 | this.cuttingPanelListener = cuttingPanelListener;
|
||
79 | 11392 | bsanchez | this.dValues = dValues;
|
80 | 11352 | bsanchez | } |
81 | 11384 | bsanchez | |
82 | 11352 | bsanchez | /**
|
83 | 11393 | bsanchez | * Arranca el proceso de recorte de un layer
|
84 | 11352 | bsanchez | */
|
85 | public void start() { |
||
86 | blinker = new Thread(this); |
||
87 | blinker.start(); |
||
88 | } |
||
89 | |||
90 | /**
|
||
91 | 11393 | bsanchez | * Establecer la resolucion de salida al crear el recorte
|
92 | * @param width
|
||
93 | * @param height
|
||
94 | 11352 | bsanchez | */
|
95 | 11393 | bsanchez | public void setResolution(int width, int height) { |
96 | resolutionWidth = width; |
||
97 | resolutionHeight = height; |
||
98 | 11352 | bsanchez | } |
99 | |||
100 | 11393 | bsanchez | /**
|
101 | * Escribir el recorte en disco usando las bandas especificadas
|
||
102 | * @param file Ruta completa del fichero
|
||
103 | * @param bands Array de bandas que se quieren guardar en dicho fichero
|
||
104 | * @throws NotSupportedExtensionException
|
||
105 | * @throws RasterDriverException
|
||
106 | * @throws IOException
|
||
107 | */
|
||
108 | 11384 | bsanchez | private void WriteFile(String file, int[] bands) |
109 | throws NotSupportedExtensionException, RasterDriverException, IOException { |
||
110 | |||
111 | BufferFactory bufferFactory = new BufferFactory(rasterMultiDataset);
|
||
112 | bufferFactory.addDrawableBands(bands); |
||
113 | 11393 | bsanchez | bufferFactory.setAreaOfInterest(dValues[0], dValues[3], dValues[2] - dValues[0], dValues[1] - dValues[3], resolutionWidth, resolutionHeight); |
114 | 11392 | bsanchez | |
115 | 11384 | bsanchez | IBuffer buffer = bufferFactory.getRasterBuf(); |
116 | |||
117 | writerBufferServer.setBuffer(buffer); |
||
118 | |||
119 | WriterParams params = GeoRasterWriter.getWriter(file).getParams(); |
||
120 | GeoRasterWriter grw = GeoRasterWriter.getWriter(writerBufferServer, file, |
||
121 | buffer.getBandCount(), extent, buffer.getWidth(), buffer.getHeight(), |
||
122 | buffer.getDataType(), params); |
||
123 | grw.dataWrite(); |
||
124 | grw.writeClose(); |
||
125 | } |
||
126 | |||
127 | 11352 | bsanchez | /**
|
128 | *
|
||
129 | */
|
||
130 | public void run() { |
||
131 | try {
|
||
132 | 11384 | bsanchez | if (oneLayerPerBand) {
|
133 | int[] drawableBandsAux = new int[1]; |
||
134 | for (int i = 0; i < drawableBands.length; i++) { |
||
135 | drawableBandsAux[0] = drawableBands[i];
|
||
136 | WriteFile(fileName + "_B" + drawableBandsAux[0] + ".tif", drawableBandsAux); |
||
137 | cuttingPanelListener.cutFinalize(fileName + "_B" + drawableBandsAux[0] + ".tif"); |
||
138 | } |
||
139 | } else {
|
||
140 | WriteFile(fileName + ".tif", drawableBands);
|
||
141 | cuttingPanelListener.cutFinalize(fileName + ".tif");
|
||
142 | } |
||
143 | 11352 | bsanchez | } catch (NotSupportedExtensionException e) {
|
144 | e.printStackTrace(); |
||
145 | } catch (RasterDriverException e) {
|
||
146 | e.printStackTrace(); |
||
147 | } catch (IOException e) { |
||
148 | e.printStackTrace(); |
||
149 | } |
||
150 | |||
151 | if (incrementableTask != null) |
||
152 | incrementableTask.processFinalize(); |
||
153 | } |
||
154 | |||
155 | /**
|
||
156 | * Asigna la tarea
|
||
157 | * @param value
|
||
158 | */
|
||
159 | public void setIncrementableTask(IncrementableTask value) { |
||
160 | incrementableTask = value; |
||
161 | incrementableTask.addIncrementableListener(this);
|
||
162 | } |
||
163 | |||
164 | public void actionCanceled(IncrementableEvent e) { |
||
165 | } |
||
166 | |||
167 | public void actionResumed(IncrementableEvent e) { |
||
168 | } |
||
169 | |||
170 | public void actionSuspended(IncrementableEvent e) { |
||
171 | } |
||
172 | } |