svn-gvsig-desktop / branches / v10 / extensions / extRasterTools / src / org / gvsig / rasterTools / saveRaster / operations / RasterizerLayer.java @ 9056
History | View | Annotate | Download (10.1 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 | 5982 | nacho | import org.cresques.filter.RasterBuf; |
56 | 4547 | nacho | import org.cresques.io.Rasterizer; |
57 | 9056 | nacho | import org.cresques.io.datastruct.Statistic; |
58 | import org.cresques.io.datastruct.Statistic.History; |
||
59 | 4547 | nacho | 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 | 6004 | nacho | import com.iver.cit.gvsig.fmap.layers.RasterOperations; |
67 | 5353 | nacho | import com.iver.utiles.swing.threads.Cancellable; |
68 | 4547 | nacho | |
69 | /**
|
||
70 | * @author Nacho Brodin <brodin_ign@gva.es>
|
||
71 | *
|
||
72 | 6641 | caballero | * 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 | 4547 | nacho | * necesite m?s datos.
|
76 | */
|
||
77 | public class RasterizerLayer extends Rasterizer{ |
||
78 | 6641 | caballero | |
79 | 4547 | nacho | 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 | 6641 | caballero | |
90 | 4547 | nacho | /**
|
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 | 6004 | nacho | vp.getExtent().getMaxY() - wcIntervalo, |
100 | 4547 | nacho | vp.getExtent().getWidth(), |
101 | wcIntervalo |
||
102 | ); |
||
103 | }else{
|
||
104 | ext = new Rectangle2D.Double( vp.getExtent().getMinX(), |
||
105 | 6004 | nacho | vp.getExtent().getMinY() - wcIntervalo, |
106 | 4547 | nacho | vp.getExtent().getWidth(), |
107 | wcIntervalo |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | viewPortBlock = new ViewPort(vp.getProjection());
|
||
112 | viewPortBlock.setExtent(ext); |
||
113 | 6641 | caballero | viewPortBlock.setImageSize(dimension); |
114 | viewPortBlock.refreshExtent(); |
||
115 | 4547 | nacho | //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 | 6641 | caballero | |
119 | 4547 | nacho | /**
|
120 | * Constructor
|
||
121 | * @param flyrs capas
|
||
122 | * @param vp viewport
|
||
123 | 6641 | caballero | * @param sizeBlock altura del bloque que se lee de una vez en la imagen de entrada
|
124 | 4547 | nacho | * @param mapCtrl Mapcontrol
|
125 | */
|
||
126 | 6004 | nacho | public RasterizerLayer(FLayers flyrs, ViewPort vp, int sB, MapControl mapCtrl, SaveRasterThread thread){ |
127 | super(sB);
|
||
128 | 6641 | caballero | |
129 | //Recorremos todas las capas comprobando si alguna de ellas implementa RasterOperations y tilea.
|
||
130 | 6004 | nacho | //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 | 6641 | caballero | |
153 | 4547 | nacho | //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 | 6641 | caballero | |
159 | 4547 | nacho | //Calculo del viewPort del primer bloque
|
160 | 6641 | caballero | |
161 | 4547 | nacho | wcAlto = viewPort.getExtent().getMaxY() - viewPort.getExtent().getMinY(); |
162 | 6641 | caballero | wcIntervalo = (sizeBlock * wcAlto) / viewPort.getImageHeight(); |
163 | 4547 | nacho | dimension = new Dimension(viewPort.getImageWidth(), sizeBlock); |
164 | 6641 | caballero | |
165 | 6004 | nacho | nBlocks = (int)(vp.getImageHeight() / sizeBlock);
|
166 | 6641 | caballero | |
167 | 6004 | nacho | incrProgressBar = percentMax / ((double)nBlocks);
|
168 | 4547 | nacho | //System.out.println("===>NBLOCKS="+nBlocks+" INCR="+incrProgressBar);
|
169 | 6641 | caballero | |
170 | 4547 | nacho | //Tama?o de ?ltimo bloque en pixeles
|
171 | lastBlock = vp.getImageHeight()-(nBlocks*sizeBlock); |
||
172 | 6641 | caballero | |
173 | 4547 | nacho | //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 | 6641 | caballero | |
178 | 4547 | nacho | calcViewPort(viewPort); |
179 | 6641 | caballero | |
180 | 4547 | nacho | this.flayers = flyrs;
|
181 | 6641 | caballero | this.sizeBlock = sizeBlock;
|
182 | 4547 | nacho | } |
183 | 6641 | caballero | |
184 | |||
185 | 4547 | nacho | /* (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 | 6641 | caballero | BufferedImage img = new BufferedImage( viewPortBlock.getImageWidth(), |
193 | 4547 | nacho | 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 | 6641 | caballero | if( raster.getSource().getDataType() == RasterBuf.TYPE_SHORT ||
|
205 | 4547 | nacho | raster.getSource().getDataType() == RasterBuf.TYPE_USHORT){ |
206 | 9056 | nacho | Statistic stats = raster.getSource().getFilterStack().getStats(); |
207 | 6641 | caballero | stats.history.add(stats.new History(raster.getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue)); |
208 | } |
||
209 | 4547 | nacho | } |
210 | } |
||
211 | firstRead = false;
|
||
212 | } |
||
213 | 6641 | caballero | |
214 | 4922 | nacho | Cancellable cancel = new Cancellable() {
|
215 | 4547 | nacho | public boolean isCanceled() { |
216 | return false; |
||
217 | } |
||
218 | 4922 | nacho | |
219 | public void setCanceled(boolean canceled) { |
||
220 | 6641 | caballero | |
221 | 4922 | nacho | } |
222 | }; |
||
223 | 6641 | caballero | |
224 | 6004 | nacho | flayers.setDirty(true);
|
225 | 6881 | cesar | flayers.draw(image, g, viewPortBlock, cancel, flayers.getMapContext().getScaleView()); |
226 | 6641 | caballero | |
227 | 4547 | nacho | //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 | 6641 | caballero | if( raster.getSource().getDataType() == RasterBuf.TYPE_SHORT ||
|
233 | 4547 | nacho | raster.getSource().getDataType() == RasterBuf.TYPE_USHORT){ |
234 | raster.getSource().getFilterStack().getStats().history.clear(); |
||
235 | 9056 | nacho | Statistic stats = raster.getSource().getFilterStack().getStats(); |
236 | 4547 | nacho | } |
237 | } |
||
238 | } |
||
239 | } |
||
240 | 6641 | caballero | |
241 | |||
242 | 4547 | nacho | }catch (DriverException e) {
|
243 | e.printStackTrace(); |
||
244 | } |
||
245 | 6641 | caballero | rasterData = image.getRGB(0, 0, sX, sY, rasterData, 0, sX); |
246 | |||
247 | 4547 | nacho | //Calculamos el viewPort del sgte bloque
|
248 | 6641 | caballero | |
249 | 4547 | nacho | 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 | 6641 | caballero | |
257 | calcViewPort(viewPortBlock); |
||
258 | |||
259 | 4547 | nacho | 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 | 6641 | caballero | |
268 | 4547 | nacho | contBlocks++; |
269 | return rasterData;
|
||
270 | } |
||
271 | 6641 | caballero | |
272 | 4547 | nacho | return null; |
273 | } |
||
274 | 6641 | caballero | |
275 | 4547 | nacho | /**
|
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 | } |