svn-gvsig-desktop / branches / gvSIG_GisPlanet / applications / appgvSIG / src / com / iver / cit / gvsig / RasterizerLayer.java @ 2174
History | View | Annotate | Download (8.41 KB)
1 | 1970 | igbrotru | /*
|
---|---|---|---|
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 com.iver.cit.gvsig; |
||
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 | 2167 | igbrotru | import java.util.ArrayList; |
55 | 1970 | igbrotru | |
56 | 2123 | igbrotru | import javax.swing.JProgressBar; |
57 | |||
58 | 1970 | igbrotru | import org.cresques.io.Rasterizer; |
59 | 2167 | igbrotru | import org.cresques.io.raster.RasterBuf; |
60 | import org.cresques.io.raster.RasterStats; |
||
61 | import org.cresques.io.raster.RasterStats.History; |
||
62 | 1970 | igbrotru | |
63 | import com.iver.cit.gvsig.fmap.DriverException; |
||
64 | import com.iver.cit.gvsig.fmap.MapControl; |
||
65 | import com.iver.cit.gvsig.fmap.ViewPort; |
||
66 | import com.iver.cit.gvsig.fmap.layers.FLayers; |
||
67 | 2167 | igbrotru | import com.iver.cit.gvsig.fmap.layers.FLyrRaster; |
68 | 1970 | igbrotru | import com.iver.cit.gvsig.fmap.operations.Cancellable; |
69 | |||
70 | /**
|
||
71 | * @author Nacho Brodin <brodin_ign@gva.es>
|
||
72 | *
|
||
73 | * Sirve datos solicitados por los drivers que salvan a raster.
|
||
74 | * Hereda de Rasterizer y reescribe el m?todo readData que es el que
|
||
75 | * ser? llamado desde el driver cada vez que vacie el buffer y
|
||
76 | * necesite m?s datos.
|
||
77 | */
|
||
78 | public class RasterizerLayer extends Rasterizer{ |
||
79 | |||
80 | 2123 | igbrotru | private ViewPort viewPort = null; |
81 | private ViewPort viewPortBlock = null; |
||
82 | private FLayers flayers = null; |
||
83 | private Color backgroundColor = null; |
||
84 | private JProgressBar progressBar = null; |
||
85 | private double incrProgressBar = 0; |
||
86 | 2167 | igbrotru | private boolean firstRead = true; |
87 | private int nBlocks = 0; |
||
88 | |||
89 | 1970 | igbrotru | /**
|
90 | * Calculo del viewPort
|
||
91 | * @param vp
|
||
92 | */
|
||
93 | private void calcViewPort(ViewPort vp){ |
||
94 | Rectangle2D ext = null; |
||
95 | |||
96 | if(viewPortBlock == null){ |
||
97 | 2107 | igbrotru | ext = new Rectangle2D.Double( Math.round(vp.getExtent().getMinX()), |
98 | Math.round(vp.getExtent().getMaxY()-wcIntervalo),
|
||
99 | Math.round(vp.getExtent().getWidth()),
|
||
100 | Math.round(wcIntervalo)
|
||
101 | 1970 | igbrotru | ); |
102 | }else{
|
||
103 | 2107 | igbrotru | ext = new Rectangle2D.Double( Math.round(vp.getExtent().getMinX()), |
104 | Math.round(vp.getExtent().getMinY()-wcIntervalo),
|
||
105 | Math.round(vp.getExtent().getWidth()),
|
||
106 | Math.round(wcIntervalo)
|
||
107 | 1970 | igbrotru | ); |
108 | } |
||
109 | |||
110 | viewPortBlock = new ViewPort(vp.getProjection());
|
||
111 | viewPortBlock.setExtent(ext); |
||
112 | viewPortBlock.setImageSize(dimension); |
||
113 | viewPortBlock.setScale(); |
||
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 sizeBlock, MapControl mapCtrl){ |
||
126 | |||
127 | super(sizeBlock);
|
||
128 | backgroundColor = vp.getBackColor(); |
||
129 | viewPort = new ViewPort(vp.getProjection());
|
||
130 | viewPort.setImageSize(vp.getImageSize()); |
||
131 | viewPort.setExtent(vp.getExtent()); |
||
132 | |||
133 | //Calculo del viewPort del primer bloque
|
||
134 | |||
135 | wcAlto = viewPort.getExtent().getMaxY() - viewPort.getExtent().getMinY(); |
||
136 | wcIntervalo = (sizeBlock*wcAlto)/viewPort.getImageHeight(); |
||
137 | dimension = new Dimension(viewPort.getImageWidth(), sizeBlock); |
||
138 | |||
139 | calcViewPort(viewPort); |
||
140 | |||
141 | 2167 | igbrotru | nBlocks = (int)(vp.getImageHeight()/sizeBlock);
|
142 | 2123 | igbrotru | incrProgressBar = 100.0/((double)nBlocks); |
143 | 2167 | igbrotru | //System.out.println("===>NBLOCKS="+nBlocks+" INCR="+incrProgressBar);
|
144 | 1970 | igbrotru | |
145 | //Tama?o de ?ltimo bloque en pixeles
|
||
146 | lastBlock = Math.round(vp.getImageHeight()-(nBlocks*sizeBlock));
|
||
147 | |||
148 | /*System.out.println("1--->SizeBlock="+sizeBlock+" ImageHeight="+viewPort.getImageHeight()+" ImageWidth="+viewPort.getImageWidth());
|
||
149 | System.out.println("2--->wcAlto="+wcAlto+" wcIntervalo="+wcIntervalo);
|
||
150 | System.out.println("3--->nBlocks="+nBlocks+" lastBlock="+lastBlock);
|
||
151 | 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());
|
||
152 | */
|
||
153 | this.flayers = flyrs;
|
||
154 | this.sizeBlock = sizeBlock;
|
||
155 | } |
||
156 | |||
157 | |||
158 | /* (non-Javadoc)
|
||
159 | * @see org.cresques.io.IDataWriter#readData(int, int, int)
|
||
160 | */
|
||
161 | public int[] readData(int sX, int sY, int nBand){ |
||
162 | if(nBand == 0){ //Con nBand==0 se devuelven las 3 bandas |
||
163 | image = new BufferedImage(sX, sY, BufferedImage.TYPE_INT_RGB); |
||
164 | g = (Graphics2D) image.getGraphics();
|
||
165 | BufferedImage img = new BufferedImage( viewPortBlock.getImageWidth(), |
||
166 | viewPortBlock.getImageHeight(), |
||
167 | BufferedImage.TYPE_INT_ARGB
|
||
168 | ); |
||
169 | g.setColor(backgroundColor); |
||
170 | g.fillRect(0, 0, viewPortBlock.getImageWidth(), viewPortBlock.getImageHeight()); |
||
171 | try{
|
||
172 | 2167 | igbrotru | //Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
|
173 | //de realce si la imagen es de 16 bits.
|
||
174 | if(firstRead){
|
||
175 | for(int i=0;i<flayers.getLayersCount();i++){ |
||
176 | if(flayers.getLayer(i) instanceof FLyrRaster){ |
||
177 | FLyrRaster raster = (FLyrRaster)flayers.getLayer(i); |
||
178 | if( raster.getSource().getDataType() == RasterBuf.TYPE_SHORT ||
|
||
179 | raster.getSource().getDataType() == RasterBuf.TYPE_USHORT){ |
||
180 | RasterStats stats = raster.getSource().getFilterStack().getStats(); |
||
181 | stats.history.add(stats.new History(raster.getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue)); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | firstRead = false;
|
||
186 | } |
||
187 | |||
188 | 1970 | igbrotru | flayers.draw(img, g, viewPortBlock,new Cancellable() {
|
189 | public boolean isCanceled() { |
||
190 | return false; |
||
191 | } |
||
192 | }); |
||
193 | 2167 | igbrotru | |
194 | //Si es el ?ltimo bloque vaciamos el historial de m?ximos y m?nimos
|
||
195 | if((contBlocks+1)==nBlocks){ |
||
196 | for(int i=0;i<flayers.getLayersCount();i++){ |
||
197 | if(flayers.getLayer(i) instanceof FLyrRaster){ |
||
198 | FLyrRaster raster = (FLyrRaster)flayers.getLayer(i); |
||
199 | if( raster.getSource().getDataType() == RasterBuf.TYPE_SHORT ||
|
||
200 | raster.getSource().getDataType() == RasterBuf.TYPE_USHORT){ |
||
201 | raster.getSource().getFilterStack().getStats().history.clear(); |
||
202 | RasterStats stats = raster.getSource().getFilterStack().getStats(); |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | |||
208 | |||
209 | 1970 | igbrotru | }catch (DriverException e) {
|
210 | e.printStackTrace(); |
||
211 | } |
||
212 | rasterData = image.getRGB(0, 0, sX, sY, rasterData, 0, sX); |
||
213 | |||
214 | //Calculamos el viewPort del sgte bloque
|
||
215 | |||
216 | if(((contBlocks+1)*sizeBlock)<=viewPort.getImageHeight()){ |
||
217 | dimension = new Dimension(sX, sY); |
||
218 | } |
||
219 | else{ //Calculo de la altura del ?ltimo bloque |
||
220 | dimension = new Dimension(sX, (int)(viewPort.getImageHeight()-(contBlocks*sizeBlock))); |
||
221 | wcIntervalo = (lastBlock*wcAlto)/viewPort.getImageHeight(); |
||
222 | } |
||
223 | |||
224 | calcViewPort(viewPortBlock); |
||
225 | 2123 | igbrotru | |
226 | if(progressBar != null) |
||
227 | progressBar.setValue((int)Math.round(progressBar.getValue()+incrProgressBar)); |
||
228 | |||
229 | 1970 | igbrotru | contBlocks++; |
230 | return rasterData;
|
||
231 | } |
||
232 | |||
233 | return null; |
||
234 | } |
||
235 | |||
236 | 2123 | igbrotru | /**
|
237 | * Asigna la barra de progreso
|
||
238 | * @param bar Barra de progreso
|
||
239 | */
|
||
240 | public void setProgressBar(JProgressBar bar){ |
||
241 | this.progressBar = bar;
|
||
242 | //progressBar.setValue((int)Math.round(incrProgressBar));
|
||
243 | } |
||
244 | 1970 | igbrotru | } |