svn-gvsig-desktop / tags / v1_0_2_Build_894 / extensions / extRasterTools / src / org / gvsig / rasterTools / saveRaster / operations / RasterizerLayer.java @ 10309
History | View | Annotate | Download (10 KB)
1 | 4547 | nacho | /*
|
---|---|---|---|
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 | 10238 | nacho | import org.cresques.io.data.RasterBuf; |
57 | 9056 | nacho | import org.cresques.io.datastruct.Statistic; |
58 | 4547 | nacho | 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 | 6004 | nacho | import com.iver.cit.gvsig.fmap.layers.RasterOperations; |
66 | 5353 | nacho | import com.iver.utiles.swing.threads.Cancellable; |
67 | 4547 | nacho | |
68 | /**
|
||
69 | * @author Nacho Brodin <brodin_ign@gva.es>
|
||
70 | *
|
||
71 | 6641 | caballero | * 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 | 4547 | nacho | * necesite m?s datos.
|
75 | */
|
||
76 | public class RasterizerLayer extends Rasterizer{ |
||
77 | 6641 | caballero | |
78 | 4547 | nacho | 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 | 6641 | caballero | |
89 | 4547 | nacho | /**
|
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 | 6004 | nacho | vp.getExtent().getMaxY() - wcIntervalo, |
99 | 4547 | nacho | vp.getExtent().getWidth(), |
100 | wcIntervalo |
||
101 | ); |
||
102 | }else{
|
||
103 | ext = new Rectangle2D.Double( vp.getExtent().getMinX(), |
||
104 | 6004 | nacho | vp.getExtent().getMinY() - wcIntervalo, |
105 | 4547 | nacho | vp.getExtent().getWidth(), |
106 | wcIntervalo |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | viewPortBlock = new ViewPort(vp.getProjection());
|
||
111 | viewPortBlock.setExtent(ext); |
||
112 | 6641 | caballero | viewPortBlock.setImageSize(dimension); |
113 | viewPortBlock.refreshExtent(); |
||
114 | 4547 | nacho | //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 | 6641 | caballero | |
118 | 4547 | nacho | /**
|
119 | * Constructor
|
||
120 | * @param flyrs capas
|
||
121 | * @param vp viewport
|
||
122 | 6641 | caballero | * @param sizeBlock altura del bloque que se lee de una vez en la imagen de entrada
|
123 | 4547 | nacho | * @param mapCtrl Mapcontrol
|
124 | */
|
||
125 | 6004 | nacho | public RasterizerLayer(FLayers flyrs, ViewPort vp, int sB, MapControl mapCtrl, SaveRasterThread thread){ |
126 | super(sB);
|
||
127 | 6641 | caballero | |
128 | //Recorremos todas las capas comprobando si alguna de ellas implementa RasterOperations y tilea.
|
||
129 | 6004 | nacho | //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 | 6641 | caballero | |
152 | 4547 | nacho | //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 | 6641 | caballero | |
158 | 4547 | nacho | //Calculo del viewPort del primer bloque
|
159 | 6641 | caballero | |
160 | 4547 | nacho | wcAlto = viewPort.getExtent().getMaxY() - viewPort.getExtent().getMinY(); |
161 | 6641 | caballero | wcIntervalo = (sizeBlock * wcAlto) / viewPort.getImageHeight(); |
162 | 4547 | nacho | dimension = new Dimension(viewPort.getImageWidth(), sizeBlock); |
163 | 6641 | caballero | |
164 | 6004 | nacho | nBlocks = (int)(vp.getImageHeight() / sizeBlock);
|
165 | 6641 | caballero | |
166 | 6004 | nacho | incrProgressBar = percentMax / ((double)nBlocks);
|
167 | 4547 | nacho | //System.out.println("===>NBLOCKS="+nBlocks+" INCR="+incrProgressBar);
|
168 | 6641 | caballero | |
169 | 4547 | nacho | //Tama?o de ?ltimo bloque en pixeles
|
170 | lastBlock = vp.getImageHeight()-(nBlocks*sizeBlock); |
||
171 | 6641 | caballero | |
172 | 4547 | nacho | //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 | 6641 | caballero | |
177 | 4547 | nacho | calcViewPort(viewPort); |
178 | 6641 | caballero | |
179 | 4547 | nacho | this.flayers = flyrs;
|
180 | 6641 | caballero | this.sizeBlock = sizeBlock;
|
181 | 4547 | nacho | } |
182 | 6641 | caballero | |
183 | |||
184 | 4547 | nacho | /* (non-Javadoc)
|
185 | * @see org.cresques.io.IDataWriter#readData(int, int, int)
|
||
186 | */
|
||
187 | public int[] readData(int sX, int sY, int nBand){ |
||
188 | if(nBand == 0){ //Con nBand==0 se devuelven las 3 bandas |
||
189 | image = new BufferedImage(sX, sY, BufferedImage.TYPE_INT_RGB); |
||
190 | g = (Graphics2D) image.getGraphics();
|
||
191 | 6641 | caballero | BufferedImage img = new BufferedImage( viewPortBlock.getImageWidth(), |
192 | 4547 | nacho | viewPortBlock.getImageHeight(), |
193 | BufferedImage.TYPE_INT_ARGB);
|
||
194 | g.setColor(backgroundColor); |
||
195 | g.fillRect(0, 0, viewPortBlock.getImageWidth(), viewPortBlock.getImageHeight()); |
||
196 | try{
|
||
197 | //Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
|
||
198 | //de realce si la imagen es de 16 bits.
|
||
199 | if(firstRead){
|
||
200 | for(int i=0;i<flayers.getLayersCount();i++){ |
||
201 | if(flayers.getLayer(i) instanceof FLyrRaster){ |
||
202 | FLyrRaster raster = (FLyrRaster)flayers.getLayer(i); |
||
203 | 6641 | caballero | if( raster.getSource().getDataType() == RasterBuf.TYPE_SHORT ||
|
204 | 4547 | nacho | raster.getSource().getDataType() == RasterBuf.TYPE_USHORT){ |
205 | 9056 | nacho | Statistic stats = raster.getSource().getFilterStack().getStats(); |
206 | 6641 | caballero | stats.history.add(stats.new History(raster.getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue)); |
207 | } |
||
208 | 4547 | nacho | } |
209 | } |
||
210 | firstRead = false;
|
||
211 | } |
||
212 | 6641 | caballero | |
213 | 4922 | nacho | Cancellable cancel = new Cancellable() {
|
214 | 4547 | nacho | public boolean isCanceled() { |
215 | return false; |
||
216 | } |
||
217 | 4922 | nacho | |
218 | public void setCanceled(boolean canceled) { |
||
219 | 6641 | caballero | |
220 | 4922 | nacho | } |
221 | }; |
||
222 | 6641 | caballero | |
223 | 6004 | nacho | flayers.setDirty(true);
|
224 | 6881 | cesar | flayers.draw(image, g, viewPortBlock, cancel, flayers.getMapContext().getScaleView()); |
225 | 6641 | caballero | |
226 | 4547 | nacho | //Si es el ?ltimo bloque vaciamos el historial de m?ximos y m?nimos
|
227 | if((contBlocks+1)==nBlocks){ |
||
228 | for(int i=0;i<flayers.getLayersCount();i++){ |
||
229 | if(flayers.getLayer(i) instanceof FLyrRaster){ |
||
230 | FLyrRaster raster = (FLyrRaster)flayers.getLayer(i); |
||
231 | 6641 | caballero | if( raster.getSource().getDataType() == RasterBuf.TYPE_SHORT ||
|
232 | 4547 | nacho | raster.getSource().getDataType() == RasterBuf.TYPE_USHORT){ |
233 | raster.getSource().getFilterStack().getStats().history.clear(); |
||
234 | 9056 | nacho | Statistic stats = raster.getSource().getFilterStack().getStats(); |
235 | 4547 | nacho | } |
236 | } |
||
237 | } |
||
238 | } |
||
239 | 6641 | caballero | |
240 | |||
241 | 4547 | nacho | }catch (DriverException e) {
|
242 | e.printStackTrace(); |
||
243 | } |
||
244 | 6641 | caballero | rasterData = image.getRGB(0, 0, sX, sY, rasterData, 0, sX); |
245 | |||
246 | 4547 | nacho | //Calculamos el viewPort del sgte bloque
|
247 | 6641 | caballero | |
248 | 4547 | nacho | if(((contBlocks+1)*sizeBlock)<=viewPort.getImageHeight()){ |
249 | dimension = new Dimension(sX, sY); |
||
250 | } |
||
251 | else{ //Calculo de la altura del ?ltimo bloque |
||
252 | dimension = new Dimension(sX, (int)(viewPort.getImageHeight()-(contBlocks*sizeBlock))); |
||
253 | wcIntervalo = (lastBlock*wcAlto)/viewPort.getImageHeight(); |
||
254 | } |
||
255 | 6641 | caballero | |
256 | calcViewPort(viewPortBlock); |
||
257 | |||
258 | 4547 | nacho | if(progressBar != null){ |
259 | this.incrTemp = (incrTemp - ((int)incrTemp)) + progressBar.getJProgressBar().getValue() + incrProgressBar; |
||
260 | if(((int)incrTemp) != progressBar.getJProgressBar().getValue()){ |
||
261 | progressBar.getJProgressBar().setValue((int)incrTemp);
|
||
262 | if(incrTemp <= 100) |
||
263 | progressBar.getLabelPercent().setText(((int)incrTemp)+"%"); |
||
264 | } |
||
265 | } |
||
266 | 6641 | caballero | |
267 | 4547 | nacho | contBlocks++; |
268 | return rasterData;
|
||
269 | } |
||
270 | 6641 | caballero | |
271 | 4547 | nacho | return null; |
272 | } |
||
273 | 6641 | caballero | |
274 | 4547 | nacho | /**
|
275 | * Asigna la barra de progreso
|
||
276 | * @param bar Barra de progreso
|
||
277 | */
|
||
278 | public void setProgressBar(ProgressSaveRasterPanel progress, double percentMax){ |
||
279 | this.progressBar = progress;
|
||
280 | if(percentMax != 0){ |
||
281 | this.percentMax = percentMax;
|
||
282 | incrProgressBar = percentMax/((double)nBlocks);
|
||
283 | } |
||
284 | //progressBar.setValue((int)Math.round(incrProgressBar));
|
||
285 | } |
||
286 | } |