Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / ROIFromFeaturesExtension.java @ 20780

History | View | Annotate | Download (5.15 KB)

1
/* gvSIG. Sistema de Informacin Geogrfica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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 Ibez, 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;
42

    
43
import javax.swing.JOptionPane;
44

    
45
import org.gvsig.fmap.raster.grid.roi.VectorialROI;
46
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
47
import org.gvsig.raster.buffer.BufferFactory;
48
import org.gvsig.raster.buffer.RasterBufferInvalidException;
49
import org.gvsig.raster.grid.Grid;
50
import org.gvsig.raster.grid.GridException;
51
import org.gvsig.raster.util.RasterToolsUtil;
52

    
53
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
54
import com.iver.andami.PluginServices;
55
import com.iver.andami.plugins.Extension;
56
import com.iver.cit.gvsig.fmap.MapContext;
57
import com.iver.cit.gvsig.fmap.layers.FBitSet;
58
import com.iver.cit.gvsig.fmap.layers.FLayer;
59
import com.iver.cit.gvsig.fmap.layers.FLayers;
60
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
61
import com.iver.cit.gvsig.project.documents.view.IProjectView;
62
import com.iver.cit.gvsig.project.documents.view.gui.View;
63

    
64
/**
65
 * Extensi?n para el C?lculo de Raster (Band Math)
66
 * 
67
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
68
 *
69
 */
70
public class ROIFromFeaturesExtension extends Extension {
71

    
72
        public void initialize() {
73
                
74
        }
75

    
76
        public void execute(String actionCommand) {
77
                if (actionCommand.equals("roi_from_features")){
78
                        com.iver.andami.ui.mdiManager.IWindow activeWindow = PluginServices.getMDIManager().getActiveWindow();
79

    
80
                        //si la ventana activa es de tipo Vista 
81
                        if (activeWindow instanceof View) {
82
                                View view = (View) activeWindow;
83
                                MapContext mapContext = view.getModel().getMapContext();
84
                                FLyrVect flyrVect = null;
85
                                FLyrRasterSE flyrRaster = null;
86
                                for (int i = 0; i < mapContext.getLayers().getActives().length; i++){
87
                                        if (mapContext.getLayers().getActives()[i] instanceof FLyrVect)
88
                                                flyrVect = (FLyrVect) mapContext.getLayers().getActives()[i];
89
                                        if (mapContext.getLayers().getActives()[i] instanceof FLyrRasterSE)
90
                                                flyrRaster = (FLyrRasterSE) mapContext.getLayers().getActives()[i];
91
                                }
92
                                
93
                                if (flyrRaster != null && flyrVect != null){
94
                                        Grid grid = null;
95
                            BufferFactory dataSource = flyrRaster.getBufferFactory();
96
                                         
97
                                        int bands[]=null;        
98
                                        bands = new  int [flyrRaster.getBandCount()];
99
                                        for(int i=0; i<flyrRaster.getBandCount();i++)
100
                                                bands[i]=i;        
101
                                        try {
102
                                                grid = new Grid(dataSource, bands);        
103
                                        } catch (RasterBufferInvalidException e) {
104
                                                        e.printStackTrace();
105
                                        }
106
                                        VectorialROI roi = new VectorialROI(grid);
107
                                        try {
108
                                                FBitSet fbs = flyrVect.getRecordset().getSelection();
109
                                                if(fbs.cardinality()>0){
110
                                                        for(int i=fbs.nextSetBit(0); i>=0; i=fbs.nextSetBit(i+1)) {
111
                                                                roi.addGeometry(flyrVect.getSource().getFeature(i).getGeometry());
112
                                                        }
113
                                                        JOptionPane.showMessageDialog(null,
114
                                                                        "Mean: "+roi.getMeanValue()+"Max: "+roi.getMaxValue()+"Min: "+roi.getMinValue(), "ROI Statistics",
115
                                                                        JOptionPane.WARNING_MESSAGE);
116
                                                }
117
                                        } catch (ReadDriverException e) {
118
                                                e.printStackTrace();
119
                                        }catch (GridException e) {
120
                                                RasterToolsUtil.messageBoxError("error_cargar_capa", this, e);
121
                                        }
122
                                        
123
                                }
124
                                else{
125
                                        
126
                                }
127
                        }
128
                }
129
        }
130

    
131
        public boolean isEnabled() {
132
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow();
133
                if (f == null) {
134
                        return false;
135
                }
136
                if (f.getClass() == View.class) {
137
                        View vista = (View) f;
138
                        IProjectView model = vista.getModel();
139
                        MapContext mapa = model.getMapContext();
140
                        FLayers layers = mapa.getLayers();
141
                        for (int i = 0; i < layers.getLayersCount(); i++) 
142
                                if (layers.getLayer(i) instanceof FLyrRasterSE) 
143
                                        return true;
144
                }
145
                return false;
146
        }
147

    
148
        public boolean isVisible() {
149
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow();
150
                if (f == null) {
151
                        return false;
152
                }
153
                if (f instanceof View) {
154
                        View vista = (View) f;
155
                        IProjectView model = vista.getModel();
156
                        MapContext mapa = model.getMapContext();
157
                        return mapa.getLayers().getLayersCount() > 0;
158
                } else {
159
                        return false;
160
                }
161
        }
162
}