svn-gvsig-desktop / tags / v1_0_2_Build_905 / extensions / extRasterTools / src / org / gvsig / rasterTools / saveRaster / operations / RasterizerLayer.java @ 10767
History | View | Annotate | Download (10.2 KB)
1 |
/*
|
---|---|
2 |
* Created on 17-feb-2004
|
3 |
*
|
4 |
* To change the template for this generated file go to
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
6 |
*/
|
7 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 |
*
|
9 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or (at your option) any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
24 |
*
|
25 |
* For more information, contact:
|
26 |
*
|
27 |
* Generalitat Valenciana
|
28 |
* Conselleria d'Infraestructures i Transport
|
29 |
* Av. Blasco Ib??ez, 50
|
30 |
* 46010 VALENCIA
|
31 |
* SPAIN
|
32 |
*
|
33 |
* +34 963862235
|
34 |
* gvsig@gva.es
|
35 |
* www.gvsig.gva.es
|
36 |
*
|
37 |
* or
|
38 |
*
|
39 |
* IVER T.I. S.A
|
40 |
* Salamanca 50
|
41 |
* 46005 Valencia
|
42 |
* Spain
|
43 |
*
|
44 |
* +34 963163400
|
45 |
* dac@iver.es
|
46 |
*/
|
47 |
package org.gvsig.rasterTools.saveRaster.operations; |
48 |
|
49 |
import java.awt.Color; |
50 |
import java.awt.Dimension; |
51 |
import java.awt.Graphics2D; |
52 |
import java.awt.geom.Rectangle2D; |
53 |
import java.awt.image.BufferedImage; |
54 |
|
55 |
import org.cresques.io.Rasterizer; |
56 |
import org.cresques.io.data.RasterBuf; |
57 |
import org.cresques.io.datastruct.Statistic; |
58 |
import org.gvsig.rasterTools.saveRaster.ui.properties.ProgressSaveRasterPanel; |
59 |
|
60 |
import com.iver.cit.gvsig.fmap.DriverException; |
61 |
import com.iver.cit.gvsig.fmap.MapControl; |
62 |
import com.iver.cit.gvsig.fmap.ViewPort; |
63 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
64 |
import com.iver.cit.gvsig.fmap.layers.FLyrRaster; |
65 |
import com.iver.cit.gvsig.fmap.layers.RasterOperations; |
66 |
import com.iver.utiles.swing.threads.Cancellable; |
67 |
|
68 |
/**
|
69 |
* @author Nacho Brodin <brodin_ign@gva.es>
|
70 |
*
|
71 |
* Sirve datos solicitados por los drivers que salvan a raster.
|
72 |
* Hereda de Rasterizer y reescribe el m?todo readData que es el que
|
73 |
* ser? llamado desde el driver cada vez que vacie el buffer y
|
74 |
* necesite m?s datos.
|
75 |
*/
|
76 |
public class RasterizerLayer extends Rasterizer{ |
77 |
|
78 |
private ViewPort viewPort = null; |
79 |
private ViewPort viewPortBlock = null; |
80 |
private FLayers flayers = null; |
81 |
private Color backgroundColor = null; |
82 |
private ProgressSaveRasterPanel progressBar = null; |
83 |
private double incrProgressBar = 0; |
84 |
private boolean firstRead = true; |
85 |
private int nBlocks = 0; |
86 |
private double percentMax = 100.0D; |
87 |
private double incrTemp = 0; |
88 |
|
89 |
/**
|
90 |
* Calculo del viewPort
|
91 |
* @param vp
|
92 |
*/
|
93 |
private void calcViewPort(ViewPort vp){ |
94 |
Rectangle2D ext = null; |
95 |
|
96 |
if(viewPortBlock == null){ |
97 |
ext = new Rectangle2D.Double( vp.getExtent().getMinX(), |
98 |
vp.getExtent().getMaxY() - wcIntervalo, |
99 |
vp.getExtent().getWidth(), |
100 |
wcIntervalo |
101 |
); |
102 |
}else{
|
103 |
ext = new Rectangle2D.Double( vp.getExtent().getMinX(), |
104 |
vp.getExtent().getMinY() - wcIntervalo, |
105 |
vp.getExtent().getWidth(), |
106 |
wcIntervalo |
107 |
); |
108 |
} |
109 |
|
110 |
viewPortBlock = new ViewPort(vp.getProjection());
|
111 |
viewPortBlock.setExtent(ext); |
112 |
viewPortBlock.setImageSize(dimension); |
113 |
viewPortBlock.refreshExtent(); |
114 |
//System.out.println("--->bloque="+contBlocks+" ImageHeight="+viewPortBlock.getImageHeight()+" ImageWidth="+viewPortBlock.getImageWidth());
|
115 |
//System.out.println("--->minX="+viewPortBlock.getExtent().getMinX()+" minY="+viewPortBlock.getExtent().getMinY()+" maxX="+viewPortBlock.getExtent().getMaxX()+" maxY="+viewPortBlock.getExtent().getMaxY()+" width="+viewPortBlock.getExtent().getWidth()+" height="+viewPortBlock.getExtent().getHeight());
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* Constructor
|
120 |
* @param flyrs capas
|
121 |
* @param vp viewport
|
122 |
* @param sizeBlock altura del bloque que se lee de una vez en la imagen de entrada
|
123 |
* @param mapCtrl Mapcontrol
|
124 |
*/
|
125 |
public RasterizerLayer(FLayers flyrs, ViewPort vp, int sB, MapControl mapCtrl, SaveRasterThread thread){ |
126 |
super(sB);
|
127 |
|
128 |
//Recorremos todas las capas comprobando si alguna de ellas implementa RasterOperations y tilea.
|
129 |
//En ese caso se obtiene el ancho de bloque. El ancho de bloque total ser? el menor obtenido.
|
130 |
//Esto lo hacemos para que las capas que tilean WMS, WCS, ... no hagan demasiadas peticiones al servidor
|
131 |
//por tener un ancho de bloque muy peque?o de modo que el ancho del bloque se ajuste al Tile menor
|
132 |
//soportado por los servidores que intervienen en el salvado.
|
133 |
int[] wBlock = null; |
134 |
boolean isTiling = false; |
135 |
int block = Integer.MAX_VALUE; |
136 |
for(int i = 0; i < flyrs.getLayersCount(); i++){ |
137 |
if(flyrs.getLayer(i) instanceof RasterOperations){ |
138 |
if(((RasterOperations)flyrs.getLayer(i)).isTiled()){
|
139 |
wBlock = ((RasterOperations)flyrs.getLayer(i)).getTileSize(); |
140 |
if((wBlock[0] - 1)< block){ |
141 |
block = wBlock[0] - 1; |
142 |
isTiling = true;
|
143 |
} |
144 |
} |
145 |
} |
146 |
} |
147 |
if(isTiling){
|
148 |
setSizeBlock(block); |
149 |
thread.setBlockSize(block); |
150 |
} |
151 |
|
152 |
//System.out.println("===>VP="+vp.getExtent().getMinX()+" "+vp.getExtent().getMinY()+" "+vp.getExtent().getMaxX()+" "+vp.getExtent().getMaxY());
|
153 |
backgroundColor = vp.getBackColor(); |
154 |
viewPort = new ViewPort(vp.getProjection());
|
155 |
viewPort.setImageSize(vp.getImageSize()); |
156 |
viewPort.setExtent(vp.getExtent()); |
157 |
|
158 |
//Calculo del viewPort del primer bloque
|
159 |
|
160 |
wcAlto = viewPort.getExtent().getMaxY() - viewPort.getExtent().getMinY(); |
161 |
wcIntervalo = (sizeBlock * wcAlto) / viewPort.getImageHeight(); |
162 |
dimension = new Dimension(viewPort.getImageWidth(), sizeBlock); |
163 |
|
164 |
nBlocks = (int)(vp.getImageHeight() / sizeBlock);
|
165 |
|
166 |
incrProgressBar = percentMax / ((double)nBlocks);
|
167 |
//System.out.println("===>NBLOCKS="+nBlocks+" INCR="+incrProgressBar);
|
168 |
|
169 |
//Tama?o de ?ltimo bloque en pixeles
|
170 |
lastBlock = vp.getImageHeight()-(nBlocks*sizeBlock); |
171 |
|
172 |
//System.out.println("1--->SizeBlock="+sizeBlock+" ImageHeight="+viewPort.getImageHeight()+" ImageWidth="+viewPort.getImageWidth());
|
173 |
//System.out.println("2--->wcAlto="+wcAlto+" wcIntervalo="+wcIntervalo);
|
174 |
//System.out.println("3--->nBlocks="+nBlocks+" lastBlock="+lastBlock);
|
175 |
//System.out.println("4--->minX="+viewPort.getExtent().getMinX()+" minY="+viewPort.getExtent().getMinY()+" maxX="+viewPort.getExtent().getMaxX()+" maxY="+viewPort.getExtent().getMaxY()+" width="+viewPort.getExtent().getWidth()+" height="+viewPort.getExtent().getHeight());
|
176 |
|
177 |
calcViewPort(viewPort); |
178 |
|
179 |
this.flayers = flyrs;
|
180 |
this.sizeBlock = sizeBlock;
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Compatibilidad con el piloto de raster
|
185 |
* @see readData
|
186 |
*/
|
187 |
public int[] readARGBData(int sX, int sY, int nBand){ |
188 |
return readData( sX, sY, nBand);
|
189 |
} |
190 |
|
191 |
/* (non-Javadoc)
|
192 |
* @see org.cresques.io.IDataWriter#readData(int, int, int)
|
193 |
*/
|
194 |
public int[] readData(int sX, int sY, int nBand){ |
195 |
if(nBand == 0){ //Con nBand==0 se devuelven las 3 bandas |
196 |
image = new BufferedImage(sX, sY, BufferedImage.TYPE_INT_RGB); |
197 |
g = (Graphics2D) image.getGraphics();
|
198 |
BufferedImage img = new BufferedImage( viewPortBlock.getImageWidth(), |
199 |
viewPortBlock.getImageHeight(), |
200 |
BufferedImage.TYPE_INT_ARGB);
|
201 |
g.setColor(backgroundColor); |
202 |
g.fillRect(0, 0, viewPortBlock.getImageWidth(), viewPortBlock.getImageHeight()); |
203 |
try{
|
204 |
//Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
|
205 |
//de realce si la imagen es de 16 bits.
|
206 |
if(firstRead){
|
207 |
for(int i=0;i<flayers.getLayersCount();i++){ |
208 |
if(flayers.getLayer(i) instanceof FLyrRaster){ |
209 |
FLyrRaster raster = (FLyrRaster)flayers.getLayer(i); |
210 |
if( raster.getSource().getDataType() == RasterBuf.TYPE_SHORT ||
|
211 |
raster.getSource().getDataType() == RasterBuf.TYPE_USHORT){ |
212 |
Statistic stats = raster.getSource().getFilterStack().getStats(); |
213 |
stats.history.add(stats.new History(raster.getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue)); |
214 |
} |
215 |
} |
216 |
} |
217 |
firstRead = false;
|
218 |
} |
219 |
|
220 |
Cancellable cancel = new Cancellable() {
|
221 |
public boolean isCanceled() { |
222 |
return false; |
223 |
} |
224 |
|
225 |
public void setCanceled(boolean canceled) { |
226 |
|
227 |
} |
228 |
}; |
229 |
|
230 |
flayers.setDirty(true);
|
231 |
flayers.draw(image, g, viewPortBlock, cancel, flayers.getMapContext().getScaleView()); |
232 |
|
233 |
//Si es el ?ltimo bloque vaciamos el historial de m?ximos y m?nimos
|
234 |
if((contBlocks+1)==nBlocks){ |
235 |
for(int i=0;i<flayers.getLayersCount();i++){ |
236 |
if(flayers.getLayer(i) instanceof FLyrRaster){ |
237 |
FLyrRaster raster = (FLyrRaster)flayers.getLayer(i); |
238 |
if( raster.getSource().getDataType() == RasterBuf.TYPE_SHORT ||
|
239 |
raster.getSource().getDataType() == RasterBuf.TYPE_USHORT){ |
240 |
raster.getSource().getFilterStack().getStats().history.clear(); |
241 |
Statistic stats = raster.getSource().getFilterStack().getStats(); |
242 |
} |
243 |
} |
244 |
} |
245 |
} |
246 |
|
247 |
|
248 |
}catch (DriverException e) {
|
249 |
e.printStackTrace(); |
250 |
} |
251 |
rasterData = image.getRGB(0, 0, sX, sY, rasterData, 0, sX); |
252 |
|
253 |
//Calculamos el viewPort del sgte bloque
|
254 |
|
255 |
if(((contBlocks+1)*sizeBlock)<=viewPort.getImageHeight()){ |
256 |
dimension = new Dimension(sX, sY); |
257 |
} |
258 |
else{ //Calculo de la altura del ?ltimo bloque |
259 |
dimension = new Dimension(sX, (int)(viewPort.getImageHeight()-(contBlocks*sizeBlock))); |
260 |
wcIntervalo = (lastBlock*wcAlto)/viewPort.getImageHeight(); |
261 |
} |
262 |
|
263 |
calcViewPort(viewPortBlock); |
264 |
|
265 |
if(progressBar != null){ |
266 |
this.incrTemp = (incrTemp - ((int)incrTemp)) + progressBar.getJProgressBar().getValue() + incrProgressBar; |
267 |
if(((int)incrTemp) != progressBar.getJProgressBar().getValue()){ |
268 |
progressBar.getJProgressBar().setValue((int)incrTemp);
|
269 |
if(incrTemp <= 100) |
270 |
progressBar.getLabelPercent().setText(((int)incrTemp)+"%"); |
271 |
} |
272 |
} |
273 |
|
274 |
contBlocks++; |
275 |
return rasterData;
|
276 |
} |
277 |
|
278 |
return null; |
279 |
} |
280 |
|
281 |
/**
|
282 |
* Asigna la barra de progreso
|
283 |
* @param bar Barra de progreso
|
284 |
*/
|
285 |
public void setProgressBar(ProgressSaveRasterPanel progress, double percentMax){ |
286 |
this.progressBar = progress;
|
287 |
if(percentMax != 0){ |
288 |
this.percentMax = percentMax;
|
289 |
incrProgressBar = percentMax/((double)nBlocks);
|
290 |
} |
291 |
//progressBar.setValue((int)Math.round(incrProgressBar));
|
292 |
} |
293 |
} |