Statistics
| Revision:

root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / scatterplot / gui / ChartScaterPlotPanel.java @ 19511

History | View | Annotate | Download (8.74 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2007 Instituto de Desarrollo Regional and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*
19
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib??ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
*   Campus Universitario s/n
35
*   02071 Alabacete
36
*   Spain
37
*
38
*   +34 967 599 200
39
*/
40

    
41
package org.gvsig.remotesensing.scatterplot.gui;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Color;
45

    
46
import javax.swing.JPanel;
47

    
48
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
49
import org.gvsig.raster.buffer.BufferFactory;
50
import org.gvsig.raster.buffer.RasterBuffer;
51
import org.gvsig.raster.buffer.RasterBufferInvalidException;
52
import org.gvsig.raster.dataset.IBuffer;
53
import org.gvsig.raster.dataset.IRasterDataSource;
54
import org.gvsig.raster.grid.Grid;
55
import org.gvsig.remotesensing.scatterplot.chart.ScatterPlotChart;
56
import org.gvsig.remotesensing.scatterplot.chart.ScatterPlotDiagram;
57
import org.jfree.chart.JFreeChart;
58
import org.jfree.chart.axis.NumberAxis;
59

    
60
import com.iver.andami.PluginServices;
61
import com.iver.cit.gvsig.fmap.layers.FLayer;
62

    
63
/**
64
 * Clase que define el panel donde aparece el grafico de dispersion para dos bandas, determinadas por
65
 * las variables bandaX y bandaY. El grafico es un FastScatterPlot de la libreria jfreeChart (personalizada para este
66
 * apartado). 
67
 * 
68
 * @author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)  
69
 * @version 11/12/2007
70
 */
71

    
72
public class ChartScaterPlotPanel extends JPanel {
73

    
74
        private static final long                 serialVersionUID = 1L;
75
        private JFreeChart                                 chart;
76
        private ScatterPlotDiagram                                 jPanelChart         = null;
77
        private  ScatterPlotChart                plot                         = null;
78
        private int                                         bandaX                        = 0;
79
        private int                                         bandaY                        = 1;
80
        private Grid                                         grid                        = null;
81
        private float                                         data[][]                = null;
82
        FLayer                                                         fLayer                        = null;
83
        int initialRenderBand[] = null;
84
        
85
        /**
86
         *   Constructor
87
         *   @param flayer capa 
88
         *   @param band1        banda que se representa en el eje x.
89
         *   @param band2        banda que se representa en el eje y.
90
         * */
91
        public ChartScaterPlotPanel(FLayer fLayer, int band1, int band2){
92
                this.fLayer= fLayer;
93
                initialRenderBand = ((FLyrRasterSE) fLayer).getRenderBands();
94
                bandaX= band1;
95
                bandaY= band2;
96
                createChart();
97
                initialize();
98
        }
99
        
100
        
101
        /**
102
         *  Inicializacion del panel que contiene el grafico
103
         * */
104
        private void initialize() {
105
                this.setLayout(new BorderLayout());
106
                this.add(getChart(), BorderLayout.CENTER);
107
        }
108

    
109
        
110
        /**
111
         *         @return grafico actual
112
         * */
113
        public ScatterPlotDiagram getChart(){
114
                if(jPanelChart == null){
115
                        jPanelChart = new ScatterPlotDiagram(chart,(FLyrRasterSE)fLayer);
116
                        chart=null;
117
                        jPanelChart.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
118
                }
119
                return         jPanelChart;
120
        }
121

    
122

    
123

    
124
        /**
125
         *         Metodo que construye el grafico inicialmente. Antes de construirlo, llama a
126
         *         setDataChart() para cargar los datos a representar 
127
         *         
128
         * */
129
        private void createChart() {
130
                
131
                 NumberAxis domainAxis = new NumberAxis(PluginServices.getText(this,"banda")+" "+bandaX);
132
             domainAxis.setAutoRangeIncludesZero(false);
133
             NumberAxis rangeAxis = new NumberAxis(PluginServices.getText(this,"banda")+" "+bandaY);
134
             rangeAxis.setAutoRangeIncludesZero(false);
135
                         
136
             // Se cargan los datos antes de construir el grafico
137
             setDataChart(fLayer);
138
             plot = new ScatterPlotChart(this.data, domainAxis, rangeAxis);
139
             
140
         
141
             chart = new JFreeChart(PluginServices.getText(this,"diagrama_dispersion"), plot);
142
             data= null;
143
                 chart.getRenderingHints().clear();
144
                
145
                 plot.setBackgroundPaint(null);
146
                 chart.setBackgroundPaint(Color.white);
147

    
148
                 plot.setBackgroundPaint(new Color(245, 245, 245));
149
                 plot=null;
150
                 
151
        }
152

    
153
        
154
        /**
155
         *         Actualizacion del panel que contiene el grafico
156
         * */
157
        public void updateChartPanel(){
158
                jPanelChart.setChart(chart);
159
                // hace un clear cuando se cambian las bandas
160
                jPanelChart.getROIChartList().getListRois().clear();
161
                jPanelChart.repaint();
162
                updateUI();
163
                
164
        }
165
        
166
        
167
        /**
168
         *         Actualizacion del grafico. Esta actualizacion se producira cuando haya variacion en la
169
         *  seleccion de las bandas por parte del usuario.
170
         *  
171
         * */
172
        public void updateChart() {
173
                
174
                 NumberAxis domainAxis = new NumberAxis(PluginServices.getText(this,"banda")+bandaX);
175
             domainAxis.setAutoRangeIncludesZero(false);
176
             NumberAxis rangeAxis = new NumberAxis(PluginServices.getText(this,"banda")+bandaY);
177
             rangeAxis.setAutoRangeIncludesZero(false);
178
             chart=null;
179
             plot=null; 
180
             
181
             // Se cargan los datos correspondiesntes a las bandas seleccionadas
182
             setDataChart(fLayer);
183
             plot = new ScatterPlotChart(this.data, domainAxis, rangeAxis);
184
             
185
                 chart = new JFreeChart(PluginServices.getText(this,"diagrama_dispersion"), plot);
186
                 chart.getRenderingHints().clear();
187
                 data=null;
188
                 plot.setBackgroundPaint(null);
189
                 chart.setBackgroundPaint(Color.white);
190
                 
191
                 plot.setBackgroundPaint(new Color(245, 245, 245));
192
                 plot=null;
193
                 updateChartPanel();
194
            
195
                }
196

    
197
                
198
                
199
        /**
200
         *         Metodo que establece los datos a representar.  
201
         *         El vector data[][] se rellena con los valores de cada punto para las bandas
202
         *  bandX y bandY.
203
         * */
204
        public void setDataChart(FLayer fLayer){
205
                                
206
                try {
207
                        this.fLayer = fLayer;
208
                } catch (Exception e) {
209
                        e.printStackTrace();
210
                }
211
                FLyrRasterSE rasterLayer = (FLyrRasterSE) fLayer;
212

    
213
                IRasterDataSource dsetCopy = null; 
214
                dsetCopy = rasterLayer.getDataSource().newDataset();
215
                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
216
                if (!RasterBuffer.loadInMemory(dsetCopy))
217
                        bufferFactory.setReadOnly(true);
218

    
219
                // grid renderizado
220
                grid=rasterLayer.getRender().getGrid();
221
                Grid dataGrid= null;
222
                try {
223
                         dataGrid= new Grid(bufferFactory,new int[]{bandaX,bandaY});
224
                } catch (RasterBufferInvalidException e) {
225
                        e.printStackTrace();
226
                }
227
                int width= grid.getRasterBuf().getWidth();
228
                int height= grid.getRasterBuf().getHeight();
229
                int indice=0;
230
                        
231
                data= new float[2][width*height];
232
                        
233
                if (dataGrid.getDataType()== IBuffer.TYPE_BYTE){
234
                        for(int i=0; i<width;i++){
235
                                for(int j=0; j<height; j++){
236
                                        data[0][indice]= dataGrid.getRasterBuf().getElemByte(j,i,0)&0xff;
237
                                        data[1][indice]= dataGrid.getRasterBuf().getElemByte(j,i,1)&0xff;
238
                                        indice++;
239
                                }        
240
                        }
241
                }
242
                        
243
                if (dataGrid.getDataType()== IBuffer.TYPE_SHORT){
244
                                
245
                        for(int i=0; i<width;i++){
246
                                for(int j=0; j<height; j++){
247
                                        data[0][indice]= dataGrid.getRasterBuf().getElemShort(j,i,0);
248
                                        data[1][indice]= dataGrid.getRasterBuf().getElemShort(j,i,1);
249
                                        indice++;
250
                                }
251
                        }
252
                }
253
                        
254
                if (dataGrid.getDataType()== IBuffer.TYPE_INT){
255
                                
256
                        for(int i=0; i<width;i++){
257
                                for(int j=0; j<height; j++){
258
                                        data[0][indice]= dataGrid.getRasterBuf().getElemInt(j,i,0);
259
                                        data[1][indice]= dataGrid.getRasterBuf().getElemInt(j,i,1);
260
                                        indice++;
261
                                }        
262
                        }
263
                }
264
                
265
                if (dataGrid.getDataType()== IBuffer.TYPE_FLOAT){
266
                        for(int i=0; i<width;i++){
267
                                for(int j=0; j<height; j++){
268
                                        data[0][indice]= dataGrid.getRasterBuf().getElemFloat(j,i,0);
269
                                        data[1][indice]= dataGrid.getRasterBuf().getElemFloat(j,i,1);
270
                                        indice++;
271
                                }
272
                        }
273
                }
274
                // OJO AL CASTINTG DE DOUBLE A FLOAT
275
                if (dataGrid.getDataType()== IBuffer.TYPE_DOUBLE){
276
                        for(int i=0; i<width;i++){
277
                                for(int j=0; j<height; j++){
278
                                        data[0][indice]= (float)dataGrid.getRasterBuf().getElemDouble(j,i,0);
279
                                        data[1][indice]= (float)dataGrid.getRasterBuf().getElemDouble(j,i,1);
280
                                        indice++;
281
                                }
282
                        }
283
                }
284
                
285
        } 
286
                
287
                
288
        /**
289
         * Asignacion de la banda X
290
         * */
291
        public void setBandX(int band){
292
                bandaX= band;
293
        }
294
                
295
                
296
        /**
297
         * Asignacion de la banda Y
298
         * */
299
        public void setBandY(int band){
300
                bandaY= band;
301
        }
302
                
303
        
304
        /**
305
         * @return banda X
306
         * */
307
        public int getBandX(){
308
                return bandaX;
309
        }
310
                
311
        
312
        /**
313
         * @return banda Y
314
         * */
315
        public int getBandY(){
316
                return bandaY;
317
        }
318
        
319
        
320
        /**
321
         *  @return grid asociado
322
         * 
323
         * */
324
        public Grid getGrid(){
325
                return grid;
326
        }
327

    
328

    
329
        /**
330
         * @return capa asociada al panel
331
         * 
332
         * */
333
        
334
        public FLayer getFLayer(){
335
                return fLayer;
336
        }
337
} 
338

    
339

    
340