Statistics
| Revision:

root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / scatterplot / listener / RoiFromChartProcess.java @ 19529

History | View | Annotate | Download (7.45 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.listener;
42

    
43
import java.util.ArrayList;
44
import java.util.Iterator;
45

    
46
import org.gvsig.fmap.raster.grid.roi.VectorialROI;
47
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
48
import org.gvsig.raster.buffer.BufferFactory;
49
import org.gvsig.raster.buffer.RasterBuffer;
50
import org.gvsig.raster.buffer.RasterBufferInvalidException;
51
import org.gvsig.raster.dataset.IBuffer;
52
import org.gvsig.raster.dataset.IRasterDataSource;
53
import org.gvsig.raster.grid.Grid;
54
import org.gvsig.raster.grid.GridException;
55
import org.gvsig.raster.grid.roi.ROI;
56
import org.gvsig.remotesensing.scatterplot.chart.ROIChart;
57
import org.jfree.data.Range;
58

    
59
import com.iver.andami.PluginServices;
60
import com.iver.cit.gvsig.fmap.MapControl;
61
import com.iver.cit.gvsig.fmap.core.FShape;
62
import com.iver.cit.gvsig.fmap.core.IGeometry;
63
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
64
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
65
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
66
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
67
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
68
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
69
import com.iver.cit.gvsig.project.documents.view.gui.View;
70

    
71
/**
72
 * Clase que implementa el proceso de construcci?n de una roi a partir de una 
73
 * ROIChart definida en el gr?fico de dispersion.
74
 * 
75
 * @author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)  
76
 * @version 11/12/2007
77
 */
78
public class RoiFromChartProcess {
79

    
80
        private Grid grid=null;
81
        private ROIChart roiToExport= null;
82
        private FLyrRasterSE rasterSE =null;
83
        
84
        /**
85
         *  Constructor
86
         *  @param roiToExport roiChart  que va a ser a exportar
87
         *  @grid        grid asociado (unicamente dispone de las dos bandas seleccionadas en el grafico)
88
         *  
89
         **/
90
        
91
        public RoiFromChartProcess(ROIChart roiToExport, FLyrRasterSE raster){
92
                this.roiToExport= roiToExport;
93
                this.rasterSE= raster;
94
        }
95
        
96
        /**
97
         *  Metodo que construye la ROI Vectorial a?adiendo un punto por
98
         *  cada pixel que se encuentra en el conjunto de rangos especificados.
99
         *  
100
         *  @return roi vectorial creada
101
         * */
102
        public ROI createVectorialRoi(){
103
                
104
                IRasterDataSource dsetCopy = null; 
105
                dsetCopy = rasterSE.getDataSource().newDataset();
106
                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
107
                if (!RasterBuffer.loadInMemory(dsetCopy))
108
                        bufferFactory.setReadOnly(true);
109
                
110
                try {
111
                        grid=new Grid(bufferFactory,rasterSE.getRenderBands());
112
                } catch (RasterBufferInvalidException e1) {
113
                        e1.printStackTrace();
114
                }
115
                
116
                
117
                
118
                VectorialROI newRoi = new VectorialROI(grid);
119
                newRoi.setColor(roiToExport.getColor());
120
                newRoi.setName(roiToExport.getName());
121
                
122
                double valorBandX= 0;
123
                double valorBandY=0;
124
                Range rango[] = null;
125
                int nX = (int) grid.getRasterBuf().getWidth();
126
                int nY = (int) grid.getRasterBuf().getHeight();
127
                IBuffer buffer = grid.getRasterBuf();
128
                ArrayList rangos= roiToExport.getRanges();
129
                double mapX=0;
130
                double mapY=0;
131
                
132
//                 Caso de buffer tipo BYTE
133
                if(buffer.getDataType() == RasterBuffer.TYPE_BYTE)
134
                {
135
                                for (int j=0; j<nY; j++){
136
                                        for(int i=0; i<nX; i++)
137
                                        {
138
                                                try {
139
                                                        grid.setBandToOperate(0);
140
                                                        valorBandX= grid.getCellValueAsByte(i,j)&0xff;
141
                                                        grid.setBandToOperate(1);
142
                                                        valorBandY=        grid.getCellValueAsByte(i,j)&0xff;
143
                                                } catch (GridException e) {
144
                                                        e.printStackTrace();
145
                                                }
146
                                                
147
                                                Iterator iterator =rangos.iterator();
148
                                                while(iterator.hasNext()){
149
                                                        rango=(Range[]) iterator.next();
150
                                                        if(isInside(valorBandX,rango[0]) && isInside(valorBandY,rango[1])){                                         
151
                                                                 mapX = grid.getGridExtent().getMin().getX()+i*grid.getCellSize();
152
                                                                 mapY = grid.getGridExtent().getMax().getY()-j*grid.getCellSize();
153
                                                                IGeometry geometry = ShapeFactory.createPoint2D(mapX,mapY);
154
                                                                newRoi.addGeometry(geometry); // Geometria                                                
155
                                                        }
156
                                                }
157
                                        }
158
                                }
159
                }
160
                
161
                // Caso de buffer tipo SHORT
162
                if(buffer.getDataType() == RasterBuffer.TYPE_SHORT)
163
                {
164
                        for (int j=0; j<nY; j++){
165
                                for(int i=0; i<nX; i++)
166
                                        {
167
                                        try {
168
                                        grid.setBandToOperate(0);
169
                                        valorBandX= grid.getCellValueAsShort(i,j);
170
                                        grid.setBandToOperate(1);
171
                                        valorBandY=        grid.getCellValueAsShort(i,j);
172
                                        
173
                                        } catch (GridException e) {
174
                                                // TODO Auto-generated catch block
175
                                                e.printStackTrace();
176
                                        }
177
                                                Iterator iterator =rangos.iterator();
178
                                                while(iterator.hasNext()){
179
                                                        rango=(Range[]) iterator.next();
180
                                                        if(isInside(valorBandX,rango[0]) && isInside(valorBandY,rango[1])){                                         
181
                                                                 mapX = grid.getGridExtent().getMin().getX()+i*grid.getCellSize();
182
                                                                 mapY = grid.getGridExtent().getMax().getY()-j*grid.getCellSize();
183
                                                                IGeometry geometry = ShapeFactory.createPoint2D(mapX,mapY);
184
                                                                newRoi.addGeometry(geometry);
185
                                                        }
186
                                                }
187
                                        }
188
                                }
189
                }
190
                
191
                drawRoi(newRoi);
192
                return newRoi;
193
        }
194
        
195
        
196
        
197
        public ROI createRasterRoi(){
198
                // creaccion de la roiRaster
199
                return null;
200
        }
201
        
202
        
203
        
204
        /***
205
         *  Metodo que determina si el valor pasado esta dentro del intervalo
206
         *  definido en rango.
207
         * @param valor valor a comprobar
208
         * @rango rango intervalo de tipo double
209
         * 
210
         */
211
        public boolean isInside(double valor, Range rango){
212
                
213
                double minValue= rango.getLowerBound();
214
                double maxValue= rango.getUpperBound();
215
                if( ( minValue<= valor) && (maxValue>=valor)) 
216
                        return true;
217
                else
218
                        return false;
219
                
220
        }
221
        
222
        
223
        /**Esto tiene que cambiar!!!!!!!! Actualmente solo pinta los pixeles que pertenecen a la roi
224
         * Dibujado de la roi
225
         * */        
226
        void drawRoi(VectorialROI roi){
227
                
228
                VectorialROI vectorialROI = (VectorialROI) roi;
229
                ISymbol symbol = null;
230
                FGraphic fGraphic = null;
231
                View view = (View) PluginServices.getMDIManager().getActiveWindow();
232
                MapControl mapControl = view.getMapControl();
233
                GraphicLayer graphicLayer =mapControl.getMapContext().getGraphicsLayer();
234
        
235
                for (Iterator iterator = vectorialROI.getGeometries()
236
                                .iterator(); iterator.hasNext();) {
237
                        IGeometry geometry = (IGeometry) iterator.next();
238
                        switch (geometry.getGeometryType()) {
239
                        case FShape.POINT:
240
                                symbol = SymbologyFactory.createDefaultMarkerSymbol();
241
                                ((IMarkerSymbol) symbol).setColor(roi.getColor());
242
                                break;
243
                        }
244
                        fGraphic = new FGraphic(geometry, graphicLayer.addSymbol(symbol));
245
                        graphicLayer.addGraphic(fGraphic);
246
                        //tablePanel.getRoiGraphics(roi.getName()).add(fGraphic);
247
                        mapControl.drawGraphics();
248
                
249
                }
250
                
251
        }
252
                
253
}