root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / cutting / WriterBufferServer.java @ 12383
History | View | Annotate | Download (8.03 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 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 org.gvsig.gui.beans.incrementabletask.IIncrementable; |
22 |
import org.gvsig.raster.dataset.IBuffer; |
23 |
import org.gvsig.raster.dataset.IDataWriter; |
24 |
import org.gvsig.raster.hierarchy.ICancellable; |
25 |
|
26 |
import com.iver.andami.PluginServices; |
27 |
/**
|
28 |
* <code>WriterBufferServer</code> sirve los datos desde un IBuffer para el
|
29 |
* driver de escritura. El driver de escritura que va a volcar un
|
30 |
* <code>IBuffer</code> a imagen en disco va solicitando los datos por bloques
|
31 |
* a medida que va salvando los anteriores. Esta clase es la encargada de hacer
|
32 |
* la divisi?n en bloques del buffer a volcar e ir sirviendolos a medida que el
|
33 |
* driver pide m?s datos.
|
34 |
* </P>
|
35 |
* <P>
|
36 |
* Implementa el interfaz <code>IDataWriter</code> que es el que define
|
37 |
* m?todos necesarios para el driver. Adem?s es una tarea incrementable por lo
|
38 |
* que deber? implementar el interfaz <code>IIncrementable</code> para poder
|
39 |
* mostrar el dialogo de incremento de tarea.
|
40 |
* </P>
|
41 |
*
|
42 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
43 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
44 |
* @version 27/04/2007
|
45 |
*/
|
46 |
public class WriterBufferServer implements IDataWriter, IIncrementable, ICancellable { |
47 |
private IBuffer buffer = null; |
48 |
private int row = 0; |
49 |
/**
|
50 |
* Tama?o de bloque. Este se asignar? en la primera petici?n
|
51 |
*/
|
52 |
private int block = 0; |
53 |
/**
|
54 |
* N?mero de bloques del proceso
|
55 |
*/
|
56 |
private int nblocks = 0; |
57 |
private double percent = 0; |
58 |
private double increment = 0; |
59 |
private boolean canceled = false; |
60 |
private int nBand = 0; |
61 |
private boolean initProcess = false; |
62 |
private StringBuffer logs = new StringBuffer(); |
63 |
|
64 |
/**
|
65 |
* Crea un nuevo <code>WriterBufferServer</code>
|
66 |
*/
|
67 |
public WriterBufferServer() {
|
68 |
} |
69 |
|
70 |
/**
|
71 |
* Crea un nuevo <code>WriterBufferServer</code> con el buffer de datos.
|
72 |
* @param buffer
|
73 |
*/
|
74 |
public WriterBufferServer(IBuffer buffer) {
|
75 |
setBuffer(buffer, -1);
|
76 |
} |
77 |
|
78 |
/**
|
79 |
* Asigna el buffer de datos e inicializa variables de
|
80 |
* @param buffer
|
81 |
* @param nband Si es menor que cero sirve datos de todas las bandas. Si es
|
82 |
* mayor que cero sirve datos de la banda indicada por el valor.
|
83 |
*/
|
84 |
public void setBuffer(IBuffer buffer, int nband) { |
85 |
this.buffer = buffer;
|
86 |
nBand = nband; |
87 |
row = 0;
|
88 |
block = 0;
|
89 |
percent = 0;
|
90 |
increment = 0;
|
91 |
} |
92 |
|
93 |
/*
|
94 |
* (non-Javadoc)
|
95 |
* @see org.gvsig.raster.dataset.IDataWriter#readARGBData(int, int, int)
|
96 |
*/
|
97 |
public int[] readARGBData(int sizeX, int sizeY, int nBand) { |
98 |
return null; |
99 |
} |
100 |
|
101 |
/**
|
102 |
* Acciones de inicializaci?n para la lectura de de un bloque
|
103 |
* @param sizeY Tama?o en Y del bloque
|
104 |
*/
|
105 |
private void initRead(int sizeY) { |
106 |
//Si es la primera linea se asigna el tama?o de bloque y el incremento
|
107 |
if (row == 0) { |
108 |
block = sizeY; |
109 |
nblocks = (int)Math.ceil(((double) buffer.getHeight() / (double) sizeY)); |
110 |
increment = 100D / ((double) buffer.getHeight() / (double) sizeY); |
111 |
} |
112 |
|
113 |
//Linea para el log
|
114 |
int b = 0; |
115 |
if (block != 0) |
116 |
b = (int) (row / block);
|
117 |
logs.append(PluginServices.getText(this, "generando_bloque") + "..." + b + "/" + nblocks + "\n"); |
118 |
|
119 |
} |
120 |
|
121 |
/*
|
122 |
* (non-Javadoc)
|
123 |
* @see org.gvsig.raster.dataset.IDataWriter#readByteData(int, int)
|
124 |
*/
|
125 |
public byte[][] readByteData(int sizeX, int sizeY) { |
126 |
initRead(sizeY); |
127 |
percent += increment; |
128 |
int len = buffer.getWidth() * sizeY;
|
129 |
int nbands = (nBand < 0) ? buffer.getBandCount() : 1; |
130 |
byte[][] b = new byte[nbands][len]; |
131 |
if(nBand < 0) { |
132 |
for (int iBand = 0; iBand < nbands; iBand++) |
133 |
for (int j = row; j < (row + sizeY); j++) |
134 |
for (int i = 0; i < buffer.getWidth(); i++) |
135 |
b[iBand][(j % block) * buffer.getWidth() + i] = buffer.getElemByte(j, i, iBand); |
136 |
} else {
|
137 |
for (int j = row; j < (row + sizeY); j++) |
138 |
for (int i = 0; i < buffer.getWidth(); i++) |
139 |
b[0][(j % block) * buffer.getWidth() + i] = buffer.getElemByte(j, i, nBand);
|
140 |
} |
141 |
row += sizeY; |
142 |
return b;
|
143 |
} |
144 |
|
145 |
/*
|
146 |
* (non-Javadoc)
|
147 |
* @see org.gvsig.raster.dataset.IDataWriter#readShortData(int, int)
|
148 |
*/
|
149 |
public short[][] readShortData(int sizeX, int sizeY) { |
150 |
initRead(sizeY); |
151 |
percent += increment; |
152 |
int len = buffer.getWidth() * sizeY;
|
153 |
short[][] b = new short[buffer.getBandCount()][len]; |
154 |
for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) |
155 |
for (int j = row; j < (row + sizeY); j++) |
156 |
for (int i = 0; i < buffer.getWidth(); i++) |
157 |
b[iBand][(j % block) * buffer.getWidth() + i] = buffer.getElemShort(j, i, iBand); |
158 |
row += sizeY; |
159 |
return b;
|
160 |
} |
161 |
|
162 |
/*
|
163 |
* (non-Javadoc)
|
164 |
* @see org.gvsig.raster.dataset.IDataWriter#readIntData(int, int)
|
165 |
*/
|
166 |
public int[][] readIntData(int sizeX, int sizeY) { |
167 |
initRead(sizeY); |
168 |
percent += increment; |
169 |
int len = buffer.getWidth() * sizeY;
|
170 |
int[][] b = new int[buffer.getBandCount()][len]; |
171 |
for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) |
172 |
for (int j = row; j < (row + sizeY); j++) |
173 |
for (int i = 0; i < buffer.getWidth(); i++) |
174 |
b[iBand][(j % block) * buffer.getWidth() + i] = buffer.getElemInt(j, i, iBand); |
175 |
row += sizeY; |
176 |
return b;
|
177 |
} |
178 |
|
179 |
/*
|
180 |
* (non-Javadoc)
|
181 |
* @see org.gvsig.raster.dataset.IDataWriter#readFloatData(int, int)
|
182 |
*/
|
183 |
public float[][] readFloatData(int sizeX, int sizeY) { |
184 |
initRead(sizeY); |
185 |
percent += increment; |
186 |
int len = buffer.getWidth() * sizeY;
|
187 |
float[][] b = new float[buffer.getBandCount()][len]; |
188 |
for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) |
189 |
for (int j = row; j < (row + sizeY); j++) |
190 |
for (int i = 0; i < buffer.getWidth(); i++) |
191 |
b[iBand][(j % block) * buffer.getWidth() + i] = buffer.getElemFloat(j, i, iBand); |
192 |
row += sizeY; |
193 |
return b;
|
194 |
} |
195 |
|
196 |
/*
|
197 |
* (non-Javadoc)
|
198 |
* @see org.gvsig.raster.dataset.IDataWriter#readDoubleData(int, int)
|
199 |
*/
|
200 |
public double[][] readDoubleData(int sizeX, int sizeY) { |
201 |
initRead(sizeY); |
202 |
percent += increment; |
203 |
int len = buffer.getWidth() * sizeY;
|
204 |
double[][] b = new double[buffer.getBandCount()][len]; |
205 |
for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) |
206 |
for (int j = row; j < (row + sizeY); j++) |
207 |
for (int i = 0; i < buffer.getWidth(); i++) |
208 |
b[iBand][(j % block) * buffer.getWidth() + i] = buffer.getElemDouble(j, i, iBand); |
209 |
row += sizeY; |
210 |
return b;
|
211 |
} |
212 |
|
213 |
/*
|
214 |
* (non-Javadoc)
|
215 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
|
216 |
*/
|
217 |
public String getLabel() { |
218 |
return PluginServices.getText(this, "generando_recorte") + "..."; |
219 |
} |
220 |
|
221 |
/*
|
222 |
* (non-Javadoc)
|
223 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
|
224 |
*/
|
225 |
public String getLog() { |
226 |
if(!initProcess) {
|
227 |
initProcess = true;
|
228 |
logs.append(PluginServices.getText(this, "leyendo_raster") + "\n"); |
229 |
} |
230 |
return logs.toString();
|
231 |
} |
232 |
|
233 |
/*
|
234 |
* (non-Javadoc)
|
235 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
236 |
*/
|
237 |
public int getPercent() { |
238 |
return Math.min((int)percent, 100); |
239 |
} |
240 |
|
241 |
/*
|
242 |
* (non-Javadoc)
|
243 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
244 |
*/
|
245 |
public String getTitle() { |
246 |
return PluginServices.getText(this, "incremento_recorte"); |
247 |
} |
248 |
|
249 |
/*
|
250 |
* (non-Javadoc)
|
251 |
* @see org.gvsig.raster.util.ICancellable#isCanceled()
|
252 |
*/
|
253 |
public boolean isCanceled(int process) { |
254 |
return canceled;
|
255 |
} |
256 |
|
257 |
/*
|
258 |
* (non-Javadoc)
|
259 |
* @see org.gvsig.raster.util.ICancellable#setCanceled(boolean)
|
260 |
*/
|
261 |
public void setCanceled(boolean value, int process) { |
262 |
logs.append(PluginServices.getText(this, "cancelando") + "..."); |
263 |
canceled = value; |
264 |
} |
265 |
} |