root / branches / v2_0_0_prep / extensions / extRemoteSensing / src / org / gvsig / remotesensing / ROIFromFeaturesExtension.java @ 31496
History | View | Annotate | Download (5.59 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.andami.PluginServices; |
46 |
import org.gvsig.andami.plugins.Extension; |
47 |
import org.gvsig.app.project.documents.view.ViewDocument; |
48 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
49 |
import org.gvsig.fmap.dal.exception.DataException; |
50 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
51 |
import org.gvsig.fmap.dal.feature.Feature; |
52 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
53 |
import org.gvsig.fmap.mapcontext.MapContext; |
54 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
55 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
56 |
import org.gvsig.fmap.raster.grid.roi.VectorialROI; |
57 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
58 |
import org.gvsig.raster.buffer.BufferFactory; |
59 |
import org.gvsig.raster.buffer.RasterBufferInvalidException; |
60 |
import org.gvsig.raster.grid.Grid; |
61 |
import org.gvsig.raster.grid.GridException; |
62 |
import org.gvsig.raster.util.RasterToolsUtil; |
63 |
|
64 |
|
65 |
/**
|
66 |
* Extensi?n para el C?lculo de Raster (Band Math)
|
67 |
*
|
68 |
* @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
69 |
*
|
70 |
*/
|
71 |
public class ROIFromFeaturesExtension extends Extension { |
72 |
|
73 |
public void initialize() { |
74 |
|
75 |
} |
76 |
|
77 |
public void execute(String actionCommand) { |
78 |
if (actionCommand.equals("roi_from_features")){ |
79 |
org.gvsig.andami.ui.mdiManager.IWindow activeWindow = PluginServices.getMDIManager().getActiveWindow(); |
80 |
|
81 |
//si la ventana activa es de tipo Vista
|
82 |
if (activeWindow instanceof DefaultViewPanel) { |
83 |
DefaultViewPanel view = (DefaultViewPanel) activeWindow; |
84 |
MapContext mapContext = view.getModel().getMapContext(); |
85 |
FLyrVect flyrVect = null;
|
86 |
FLyrRasterSE flyrRaster = null;
|
87 |
for (int i = 0; i < mapContext.getLayers().getActives().length; i++){ |
88 |
if (mapContext.getLayers().getActives()[i] instanceof FLyrVect) { |
89 |
flyrVect = (FLyrVect) mapContext.getLayers().getActives()[i]; |
90 |
} |
91 |
if (mapContext.getLayers().getActives()[i] instanceof FLyrRasterSE) { |
92 |
flyrRaster = (FLyrRasterSE) mapContext.getLayers().getActives()[i]; |
93 |
} |
94 |
} |
95 |
|
96 |
if (flyrRaster != null && flyrVect != null){ |
97 |
Grid grid = null;
|
98 |
BufferFactory dataSource = flyrRaster.getBufferFactory(); |
99 |
|
100 |
int bands[]=null; |
101 |
bands = new int [flyrRaster.getBandCount()]; |
102 |
for(int i=0; i<flyrRaster.getBandCount();i++) { |
103 |
bands[i]=i; |
104 |
} |
105 |
try {
|
106 |
grid = new Grid(dataSource, bands);
|
107 |
} catch (RasterBufferInvalidException e) {
|
108 |
e.printStackTrace(); |
109 |
} |
110 |
VectorialROI roi = new VectorialROI(grid);
|
111 |
DisposableIterator iterator = null;
|
112 |
try {
|
113 |
FeatureSelection fs = (FeatureSelection)flyrVect.getFeatureStore().getSelection(); |
114 |
iterator = fs.iterator(); |
115 |
boolean hasFeatures=false; |
116 |
while (iterator.hasNext()) {
|
117 |
hasFeatures=true;
|
118 |
Feature feature = (Feature) iterator.next(); |
119 |
roi.addGeometry(feature.getDefaultGeometry()); |
120 |
} |
121 |
if (hasFeatures){
|
122 |
JOptionPane.showMessageDialog(null, |
123 |
"Mean: "+roi.getMeanValue()+"Max: "+roi.getMaxValue()+"Min: "+roi.getMinValue(), "ROI Statistics", |
124 |
JOptionPane.WARNING_MESSAGE);
|
125 |
} |
126 |
}catch (GridException e) {
|
127 |
RasterToolsUtil.messageBoxError("error_cargar_capa", this, e); |
128 |
} catch (DataException e) {
|
129 |
RasterToolsUtil.messageBoxError("error_cargar_capa", this, e); |
130 |
} finally {
|
131 |
if (iterator != null) { |
132 |
iterator.dispose(); |
133 |
} |
134 |
} |
135 |
} |
136 |
else{
|
137 |
|
138 |
} |
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
public boolean isEnabled() { |
144 |
org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
145 |
if (f == null) { |
146 |
return false; |
147 |
} |
148 |
if (f.getClass() == DefaultViewPanel.class) {
|
149 |
DefaultViewPanel vista = (DefaultViewPanel) f; |
150 |
ViewDocument model = vista.getModel(); |
151 |
MapContext mapa = model.getMapContext(); |
152 |
FLayers layers = mapa.getLayers(); |
153 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
154 |
if (layers.getLayer(i) instanceof FLyrRasterSE) { |
155 |
return true; |
156 |
} |
157 |
} |
158 |
} |
159 |
return false; |
160 |
} |
161 |
|
162 |
public boolean isVisible() { |
163 |
org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
164 |
if (f == null) { |
165 |
return false; |
166 |
} |
167 |
if (f instanceof DefaultViewPanel) { |
168 |
DefaultViewPanel vista = (DefaultViewPanel) f; |
169 |
ViewDocument model = vista.getModel(); |
170 |
MapContext mapa = model.getMapContext(); |
171 |
return mapa.getLayers().getLayersCount() > 0; |
172 |
} else {
|
173 |
return false; |
174 |
} |
175 |
} |
176 |
} |