svn-gvsig-desktop / branches / v10 / extensions / extRasterTools / src / org / gvsig / rasterTools / saveRaster / operations / RasterizerLayer.java @ 9056
History | View | Annotate | Download (10.1 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.filter.RasterBuf; |
56 |
import org.cresques.io.Rasterizer; |
57 |
import org.cresques.io.datastruct.Statistic; |
58 |
import org.cresques.io.datastruct.Statistic.History; |
59 |
import org.gvsig.rasterTools.saveRaster.ui.properties.ProgressSaveRasterPanel; |
60 |
|
61 |
import com.iver.cit.gvsig.fmap.DriverException; |
62 |
import com.iver.cit.gvsig.fmap.MapControl; |
63 |
import com.iver.cit.gvsig.fmap.ViewPort; |
64 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
65 |
import com.iver.cit.gvsig.fmap.layers.FLyrRaster; |
66 |
import com.iver.cit.gvsig.fmap.layers.RasterOperations; |
67 |
import com.iver.utiles.swing.threads.Cancellable; |
68 |
|
69 |
/**
|
70 |
* @author Nacho Brodin <brodin_ign@gva.es>
|
71 |
*
|
72 |
* Sirve datos solicitados por los drivers que salvan a raster.
|
73 |
* Hereda de Rasterizer y reescribe el m?todo readData que es el que
|
74 |
* ser? llamado desde el driver cada vez que vacie el buffer y
|
75 |
* necesite m?s datos.
|
76 |
*/
|
77 |
public class RasterizerLayer extends Rasterizer{ |
78 |
|
79 |
private ViewPort viewPort = null; |
80 |
private ViewPort viewPortBlock = null; |
81 |
private FLayers flayers = null; |
82 |
private Color backgroundColor = null; |
83 |
private ProgressSaveRasterPanel progressBar = null; |
84 |
private double incrProgressBar = 0; |
85 |
private boolean firstRead = true; |
86 |
private int nBlocks = 0; |
87 |
private double percentMax = 100.0D; |
88 |
private double incrTemp = 0; |
89 |
|
90 |
/**
|
91 |
* Calculo del viewPort
|
92 |
* @param vp
|
93 |
*/
|
94 |
private void calcViewPort(ViewPort vp){ |
95 |
Rectangle2D ext = null; |
96 |
|
97 |
if(viewPortBlock == null){ |
98 |
ext = new Rectangle2D.Double( vp.getExtent().getMinX(), |
99 |
vp.getExtent().getMaxY() - wcIntervalo, |
100 |
vp.getExtent().getWidth(), |
101 |
wcIntervalo |
102 |
); |
103 |
}else{
|
104 |
ext = new Rectangle2D.Double( vp.getExtent().getMinX(), |
105 |
vp.getExtent().getMinY() - wcIntervalo, |
106 |
vp.getExtent().getWidth(), |
107 |
wcIntervalo |
108 |
); |
109 |
} |
110 |
|
111 |
viewPortBlock = new ViewPort(vp.getProjection());
|
112 |
viewPortBlock.setExtent(ext); |
113 |
viewPortBlock.setImageSize(dimension); |
114 |
viewPortBlock.refreshExtent(); |
115 |
//System.out.println("--->bloque="+contBlocks+" ImageHeight="+viewPortBlock.getImageHeight()+" ImageWidth="+viewPortBlock.getImageWidth());
|
116 |
//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());
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* Constructor
|
121 |
* @param flyrs capas
|
122 |
* @param vp viewport
|
123 |
* @param sizeBlock altura del bloque que se lee de una vez en la imagen de entrada
|
124 |
* @param mapCtrl Mapcontrol
|
125 |
*/
|
126 |
public RasterizerLayer(FLayers flyrs, ViewPort vp, int sB, MapControl mapCtrl, SaveRasterThread thread){ |
127 |
super(sB);
|
128 |
|
129 |
//Recorremos todas las capas comprobando si alguna de ellas implementa RasterOperations y tilea.
|
130 |
//En ese caso se obtiene el ancho de bloque. El ancho de bloque total ser? el menor obtenido.
|
131 |
//Esto lo hacemos para que las capas que tilean WMS, WCS, ... no hagan demasiadas peticiones al servidor
|
132 |
//por tener un ancho de bloque muy peque?o de modo que el ancho del bloque se ajuste al Tile menor
|
133 |
//soportado por los servidores que intervienen en el salvado.
|
134 |
int[] wBlock = null; |
135 |
boolean isTiling = false; |
136 |
int block = Integer.MAX_VALUE; |
137 |
for(int i = 0; i < flyrs.getLayersCount(); i++){ |
138 |
if(flyrs.getLayer(i) instanceof RasterOperations){ |
139 |
if(((RasterOperations)flyrs.getLayer(i)).isTiled()){
|
140 |
wBlock = ((RasterOperations)flyrs.getLayer(i)).getTileSize(); |
141 |
if((wBlock[0] - 1)< block){ |
142 |
block = wBlock[0] - 1; |
143 |
isTiling = true;
|
144 |
} |
145 |
} |
146 |
} |
147 |
} |
148 |
if(isTiling){
|
149 |
setSizeBlock(block); |
150 |
thread.setBlockSize(block); |
151 |
} |
152 |
|
153 |
//System.out.println("===>VP="+vp.getExtent().getMinX()+" "+vp.getExtent().getMinY()+" "+vp.getExtent().getMaxX()+" "+vp.getExtent().getMaxY());
|
154 |
backgroundColor = vp.getBackColor(); |
155 |
viewPort = new ViewPort(vp.getProjection());
|
156 |
viewPort.setImageSize(vp.getImageSize()); |
157 |
viewPort.setExtent(vp.getExtent()); |
158 |
|
159 |
//Calculo del viewPort del primer bloque
|
160 |
|
161 |
wcAlto = viewPort.getExtent().getMaxY() - viewPort.getExtent().getMinY(); |
162 |
wcIntervalo = (sizeBlock * wcAlto) / viewPort.getImageHeight(); |
163 |
dimension = new Dimension(viewPort.getImageWidth(), sizeBlock); |
164 |
|
165 |
nBlocks = (int)(vp.getImageHeight() / sizeBlock);
|
166 |
|
167 |
incrProgressBar = percentMax / ((double)nBlocks);
|
168 |
//System.out.println("===>NBLOCKS="+nBlocks+" INCR="+incrProgressBar);
|
169 |
|
170 |
//Tama?o de ?ltimo bloque en pixeles
|
171 |
lastBlock = vp.getImageHeight()-(nBlocks*sizeBlock); |
172 |
|
173 |
//System.out.println("1--->SizeBlock="+sizeBlock+" ImageHeight="+viewPort.getImageHeight()+" ImageWidth="+viewPort.getImageWidth());
|
174 |
//System.out.println("2--->wcAlto="+wcAlto+" wcIntervalo="+wcIntervalo);
|
175 |
//System.out.println("3--->nBlocks="+nBlocks+" lastBlock="+lastBlock);
|
176 |
//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());
|
177 |
|
178 |
calcViewPort(viewPort); |
179 |
|
180 |
this.flayers = flyrs;
|
181 |
this.sizeBlock = sizeBlock;
|
182 |
} |
183 |
|
184 |
|
185 |
/* (non-Javadoc)
|
186 |
* @see org.cresques.io.IDataWriter#readData(int, int, int)
|
187 |
*/
|
188 |
public int[] readData(int sX, int sY, int nBand){ |
189 |
if(nBand == 0){ //Con nBand==0 se devuelven las 3 bandas |
190 |
image = new BufferedImage(sX, sY, BufferedImage.TYPE_INT_RGB); |
191 |
g = (Graphics2D) image.getGraphics();
|
192 |
BufferedImage img = new BufferedImage( viewPortBlock.getImageWidth(), |
193 |
viewPortBlock.getImageHeight(), |
194 |
BufferedImage.TYPE_INT_ARGB);
|
195 |
g.setColor(backgroundColor); |
196 |
g.fillRect(0, 0, viewPortBlock.getImageWidth(), viewPortBlock.getImageHeight()); |
197 |
try{
|
198 |
//Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
|
199 |
//de realce si la imagen es de 16 bits.
|
200 |
if(firstRead){
|
201 |
for(int i=0;i<flayers.getLayersCount();i++){ |
202 |
if(flayers.getLayer(i) instanceof FLyrRaster){ |
203 |
FLyrRaster raster = (FLyrRaster)flayers.getLayer(i); |
204 |
if( raster.getSource().getDataType() == RasterBuf.TYPE_SHORT ||
|
205 |
raster.getSource().getDataType() == RasterBuf.TYPE_USHORT){ |
206 |
Statistic stats = raster.getSource().getFilterStack().getStats(); |
207 |
stats.history.add(stats.new History(raster.getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue)); |
208 |
} |
209 |
} |
210 |
} |
211 |
firstRead = false;
|
212 |
} |
213 |
|
214 |
Cancellable cancel = new Cancellable() {
|
215 |
public boolean isCanceled() { |
216 |
return false; |
217 |
} |
218 |
|
219 |
public void setCanceled(boolean canceled) { |
220 |
|
221 |
} |
222 |
}; |
223 |
|
224 |
flayers.setDirty(true);
|
225 |
flayers.draw(image, g, viewPortBlock, cancel, flayers.getMapContext().getScaleView()); |
226 |
|
227 |
//Si es el ?ltimo bloque vaciamos el historial de m?ximos y m?nimos
|
228 |
if((contBlocks+1)==nBlocks){ |
229 |
for(int i=0;i<flayers.getLayersCount();i++){ |
230 |
if(flayers.getLayer(i) instanceof FLyrRaster){ |
231 |
FLyrRaster raster = (FLyrRaster)flayers.getLayer(i); |
232 |
if( raster.getSource().getDataType() == RasterBuf.TYPE_SHORT ||
|
233 |
raster.getSource().getDataType() == RasterBuf.TYPE_USHORT){ |
234 |
raster.getSource().getFilterStack().getStats().history.clear(); |
235 |
Statistic stats = raster.getSource().getFilterStack().getStats(); |
236 |
} |
237 |
} |
238 |
} |
239 |
} |
240 |
|
241 |
|
242 |
}catch (DriverException e) {
|
243 |
e.printStackTrace(); |
244 |
} |
245 |
rasterData = image.getRGB(0, 0, sX, sY, rasterData, 0, sX); |
246 |
|
247 |
//Calculamos el viewPort del sgte bloque
|
248 |
|
249 |
if(((contBlocks+1)*sizeBlock)<=viewPort.getImageHeight()){ |
250 |
dimension = new Dimension(sX, sY); |
251 |
} |
252 |
else{ //Calculo de la altura del ?ltimo bloque |
253 |
dimension = new Dimension(sX, (int)(viewPort.getImageHeight()-(contBlocks*sizeBlock))); |
254 |
wcIntervalo = (lastBlock*wcAlto)/viewPort.getImageHeight(); |
255 |
} |
256 |
|
257 |
calcViewPort(viewPortBlock); |
258 |
|
259 |
if(progressBar != null){ |
260 |
this.incrTemp = (incrTemp - ((int)incrTemp)) + progressBar.getJProgressBar().getValue() + incrProgressBar; |
261 |
if(((int)incrTemp) != progressBar.getJProgressBar().getValue()){ |
262 |
progressBar.getJProgressBar().setValue((int)incrTemp);
|
263 |
if(incrTemp <= 100) |
264 |
progressBar.getLabelPercent().setText(((int)incrTemp)+"%"); |
265 |
} |
266 |
} |
267 |
|
268 |
contBlocks++; |
269 |
return rasterData;
|
270 |
} |
271 |
|
272 |
return null; |
273 |
} |
274 |
|
275 |
/**
|
276 |
* Asigna la barra de progreso
|
277 |
* @param bar Barra de progreso
|
278 |
*/
|
279 |
public void setProgressBar(ProgressSaveRasterPanel progress, double percentMax){ |
280 |
this.progressBar = progress;
|
281 |
if(percentMax != 0){ |
282 |
this.percentMax = percentMax;
|
283 |
incrProgressBar = percentMax/((double)nBlocks);
|
284 |
} |
285 |
//progressBar.setValue((int)Math.round(incrProgressBar));
|
286 |
} |
287 |
} |