svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / clipping / ui / listener / ClippingPanelListener.java @ 22529
History | View | Annotate | Download (28.2 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 IVER T.I. 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 |
package org.gvsig.rastertools.clipping.ui.listener; |
20 |
|
21 |
import java.awt.Component; |
22 |
import java.awt.Cursor; |
23 |
import java.awt.Dimension; |
24 |
import java.awt.event.ActionEvent; |
25 |
import java.awt.event.ActionListener; |
26 |
import java.awt.geom.AffineTransform; |
27 |
import java.awt.geom.NoninvertibleTransformException; |
28 |
import java.awt.geom.Point2D; |
29 |
import java.awt.geom.Rectangle2D; |
30 |
import java.io.File; |
31 |
import java.util.ArrayList; |
32 |
import java.util.EventObject; |
33 |
|
34 |
import javax.swing.JOptionPane; |
35 |
|
36 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
37 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
38 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent; |
39 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener; |
40 |
import org.gvsig.gui.beans.coordinatespanel.CoordinatesEvent; |
41 |
import org.gvsig.gui.beans.coordinatespanel.CoordinatesListener; |
42 |
import org.gvsig.gui.beans.coordinatespanel.CoordinatesPanel; |
43 |
import org.gvsig.gui.beans.datainput.DataInputContainerListener; |
44 |
import org.gvsig.gui.beans.table.models.CheckBoxModel; |
45 |
import org.gvsig.raster.IProcessActions; |
46 |
import org.gvsig.raster.RasterLibrary; |
47 |
import org.gvsig.raster.RasterProcess; |
48 |
import org.gvsig.raster.buffer.WriterBufferServer; |
49 |
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation; |
50 |
import org.gvsig.raster.datastruct.Extent; |
51 |
import org.gvsig.raster.grid.roi.ROI; |
52 |
import org.gvsig.raster.hierarchy.IRasterGeoOperations; |
53 |
import org.gvsig.raster.hierarchy.IRasterOperations; |
54 |
import org.gvsig.raster.util.RasterToolsUtil; |
55 |
import org.gvsig.raster.util.process.ClippingProcess; |
56 |
import org.gvsig.rastertools.clipping.ClippingData; |
57 |
import org.gvsig.rastertools.clipping.panels.ClippingCoordinatesPanel; |
58 |
import org.gvsig.rastertools.clipping.panels.ClippingOptionsPanel; |
59 |
import org.gvsig.rastertools.clipping.panels.ClippingResolutionPanel; |
60 |
import org.gvsig.rastertools.clipping.panels.ClippingSelectionPanel; |
61 |
import org.gvsig.rastertools.clipping.ui.ClippingPanel; |
62 |
import org.gvsig.rastertools.saveraster.ui.info.EndInfoDialog; |
63 |
|
64 |
import com.iver.andami.PluginServices; |
65 |
import com.iver.andami.Utilities; |
66 |
import com.iver.andami.ui.mdiManager.IWindow; |
67 |
import com.iver.cit.gvsig.fmap.MapControl; |
68 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
69 |
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior; |
70 |
import com.iver.cit.gvsig.fmap.tools.Behavior.MouseMovementBehavior; |
71 |
import com.iver.cit.gvsig.fmap.tools.Behavior.RectangleBehavior; |
72 |
import com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent; |
73 |
import com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener; |
74 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
75 |
import com.iver.cit.gvsig.project.documents.view.toolListeners.StatusBarListener; |
76 |
/**
|
77 |
* <code>ClippingPanelListener</code> es una clase donde se recoger?n y
|
78 |
* tratar?n todos los eventos del panel de recorte
|
79 |
*
|
80 |
* @version 19/04/2007
|
81 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
82 |
*/
|
83 |
public class ClippingPanelListener implements ActionListener, RectangleListener, ButtonsPanelListener, CoordinatesListener, DataInputContainerListener, IProcessActions { |
84 |
private Dimension dim = new Dimension(); |
85 |
private AffineTransform at = null; |
86 |
private ClippingPanel clippingPanel = null; |
87 |
private ClippingData data = null; |
88 |
private boolean enableValueChangedEvent = true; |
89 |
|
90 |
private FLyrRasterSE fLayer = null; |
91 |
private MapControl mapControl = null; |
92 |
|
93 |
/**
|
94 |
* Herramienta seleccionada en el momento de la apertura del dialogo
|
95 |
*/
|
96 |
private String lastTool = null; |
97 |
|
98 |
private String viewName = null; |
99 |
|
100 |
/**
|
101 |
* Crea un nuevo <code>ClippingPanelListener</code> con el
|
102 |
* <code>ClippingPanelListener</code> asociado
|
103 |
* @param panel
|
104 |
*/
|
105 |
public ClippingPanelListener(ClippingPanel clippingPanel) {
|
106 |
this.clippingPanel = clippingPanel;
|
107 |
} |
108 |
|
109 |
/**
|
110 |
* Asigna el modelo de datos
|
111 |
* @param data
|
112 |
*/
|
113 |
public void setData(ClippingData data) { |
114 |
this.data = data;
|
115 |
} |
116 |
|
117 |
/**
|
118 |
* Asigna la matriz de transformaci?n entre puntos en coordenadas del raster y
|
119 |
* puntos en coordenadas reales.
|
120 |
* @param AffineTransform
|
121 |
*/
|
122 |
private void setAffineTransform(AffineTransform at) { |
123 |
this.at = at;
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* Asigna la dimensi?n del raster
|
128 |
* @param dim
|
129 |
*/
|
130 |
public void setDimension(Dimension dim) { |
131 |
this.dim = dim;
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* M?todo que se invoca cuando se disparan los eventos de los botones de
|
136 |
* extensi?n completa o de seleccion de extensi?n con el rat?n
|
137 |
*/
|
138 |
public void actionPerformed(ActionEvent e) { |
139 |
// Bot?n de selecci?n del extent completo
|
140 |
// Modificamos las coordenadas reales y recalculamos las coordenadas pixel
|
141 |
if (e.getSource() == getResolutionPanel().getButtonRestore()) {
|
142 |
getClippingPanel().restoreStatus(data); |
143 |
} |
144 |
|
145 |
//Bot?n de selecci?n del ?rea m?xima asociada a los ROIs
|
146 |
if (e.getSource() == getCoordinatesPanel().getButtonBarContainer().getButton(0)) { |
147 |
ArrayList roiList = getFLayer().getRois();
|
148 |
if(roiList != null && roiList.size() > 0) { |
149 |
Extent ext = ROI.getROIsMaximunExtent(roiList); |
150 |
assignROISExtent(ext, getFLayer()); |
151 |
} else
|
152 |
assignFullExtent(); |
153 |
return;
|
154 |
} |
155 |
|
156 |
// Bot?n de selecci?n del extent completo
|
157 |
// Modificamos las coordenadas reales y recalculamos las coordenadas pixel
|
158 |
if (e.getSource() == getCoordinatesPanel().getButtonBarContainer().getButton(1)) { |
159 |
assignFullExtent(); |
160 |
return;
|
161 |
} |
162 |
|
163 |
// Bot?n de selecci?n de la herramienta de seleccionar desde la vista
|
164 |
if (e.getSource() == getCoordinatesPanel().getButtonBarContainer().getButton(2)) { |
165 |
selectToolButton(); |
166 |
return;
|
167 |
} |
168 |
|
169 |
} |
170 |
|
171 |
/**
|
172 |
* Asigna el extent completo a los cuadros de texto donde se introducen las coordenadas
|
173 |
* reales y p?xel.
|
174 |
*/
|
175 |
private void assignROISExtent(Extent ext, FLyrRasterSE layer) { |
176 |
AffineTransform at = layer.getAffineTransform();
|
177 |
Point2D ulWc = new Point2D.Double(ext.minX(), ext.maxY()); |
178 |
Point2D lrWc = new Point2D.Double(ext.maxX(), ext.minY()); |
179 |
|
180 |
ulWc = getFLayer().adjustWorldRequest(ulWc); |
181 |
lrWc = getFLayer().adjustWorldRequest(lrWc); |
182 |
|
183 |
Point2D ulPx = new Point2D.Double(); |
184 |
Point2D lrPx = new Point2D.Double(); |
185 |
|
186 |
try {
|
187 |
at.inverseTransform(ulWc, ulPx); |
188 |
at.inverseTransform(lrWc, lrPx); |
189 |
} catch (NoninvertibleTransformException e) { |
190 |
JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), PluginServices.getText(this, "coordenadas_erroneas")); |
191 |
return;
|
192 |
} |
193 |
|
194 |
data.setCoorRealFromDouble(ulWc.getX(), ulWc.getY(), lrWc.getX(), lrWc.getY()); |
195 |
data.setCoorPixelFromDouble(ulPx.getX(), ulPx.getY(), lrPx.getX() - 1, lrPx.getY() - 1); |
196 |
data.initSize(); |
197 |
getClippingPanel().saveStatus(data); |
198 |
|
199 |
getClippingPanel().getButtonsPanel().getButton(ButtonsPanel.BUTTON_APPLY).setEnabled(true);
|
200 |
getClippingPanel().getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT).setEnabled(true);
|
201 |
} |
202 |
|
203 |
/**
|
204 |
* Asigna el extent completo a los cuadros de texto donde se introducen las coordenadas
|
205 |
* reales y p?xel.
|
206 |
*/
|
207 |
private void assignFullExtent() { |
208 |
Point2D ulPx = new Point2D.Double(0, 0); |
209 |
Point2D lrPx = new Point2D.Double(dim.width, dim.height); |
210 |
|
211 |
//Convertimos nuevamente a coordenadas reales
|
212 |
Point2D ulWc = new Point2D.Double(); |
213 |
Point2D lrWc = new Point2D.Double(); |
214 |
at.transform(ulPx, ulWc); |
215 |
at.transform(lrPx, lrWc); |
216 |
|
217 |
data.setCoorRealFromDouble(ulWc.getX(), ulWc.getY(), lrWc.getX(), lrWc.getY()); |
218 |
data.setCoorPixelFromDouble(0, 0, dim.width - 1, dim.height - 1); |
219 |
data.initSize(); |
220 |
getClippingPanel().saveStatus(data); |
221 |
|
222 |
getClippingPanel().getButtonsPanel().getButton(ButtonsPanel.BUTTON_APPLY).setEnabled(true);
|
223 |
getClippingPanel().getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT).setEnabled(true);
|
224 |
} |
225 |
|
226 |
/**
|
227 |
* Al producirse un evento de perdida de foco o pulsaci?n de "enter" en un campo de texto de coordenadas
|
228 |
* hay que asignar el nuevo valor introducido.
|
229 |
* @param obj
|
230 |
*/
|
231 |
private void eventJTextField(CoordinatesEvent e) { |
232 |
try {
|
233 |
if (e.getSource() == getCoordinatesPanel().getPixelCoordinates()) {
|
234 |
if (e.getName().equals("11")) |
235 |
data.setUlxPx(Double.valueOf(getCoordinatesPanel().getPixelCoordinates().getValue11()).doubleValue());
|
236 |
if (e.getName().equals("12")) |
237 |
data.setUlyPx(Double.valueOf(getCoordinatesPanel().getPixelCoordinates().getValue12()).doubleValue());
|
238 |
if (e.getName().equals("21")) |
239 |
data.setLrxPx(Double.valueOf(getCoordinatesPanel().getPixelCoordinates().getValue21()).doubleValue());
|
240 |
if (e.getName().equals("22")) |
241 |
data.setLryPx(Double.valueOf(getCoordinatesPanel().getPixelCoordinates().getValue22()).doubleValue());
|
242 |
data.updateObservers(); |
243 |
} |
244 |
|
245 |
if (e.getSource() == getCoordinatesPanel().getRealCoordinates()) {
|
246 |
if (e.getName().equals("11")) |
247 |
data.setUlxWc(Double.valueOf(getCoordinatesPanel().getRealCoordinates().getValue11()).doubleValue());
|
248 |
if (e.getName().equals("12")) |
249 |
data.setUlyWc(Double.valueOf(getCoordinatesPanel().getRealCoordinates().getValue12()).doubleValue());
|
250 |
if (e.getName().equals("21")) |
251 |
data.setLrxWc(Double.valueOf(getCoordinatesPanel().getRealCoordinates().getValue21()).doubleValue());
|
252 |
if (e.getName().equals("22")) |
253 |
data.setLryWc(Double.valueOf(getCoordinatesPanel().getRealCoordinates().getValue22()).doubleValue());
|
254 |
data.updateObservers(); |
255 |
} |
256 |
} catch (NumberFormatException ex1) { |
257 |
// No hay valores parseables a decimal en las cajas de texto. No hacemos nada
|
258 |
} |
259 |
} |
260 |
|
261 |
|
262 |
|
263 |
/**
|
264 |
* Recalcula el valor de los campos de coordenadas reales y pixel. Cuando modificamos alg?n campo
|
265 |
* de las coordenadas reales se modifican los pixeles y viceversa.
|
266 |
* @param modifyPx true si se ha modificado alg?n campo de coordenadas pixel y false si se ha modificado
|
267 |
* alg?n campo de las coordenadas reales.
|
268 |
*/
|
269 |
private void recalcCoordFields(boolean modifyPx) { |
270 |
try {
|
271 |
getClippingPanel().getButtonsPanel().getButton(ButtonsPanel.BUTTON_APPLY).setEnabled(false);
|
272 |
getClippingPanel().getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT).setEnabled(false);
|
273 |
|
274 |
if (modifyPx) {
|
275 |
Point2D ulPx = new Point2D.Double(data.getUlxPx(), data.getUlyPx()); |
276 |
Point2D lrPx = new Point2D.Double(data.getLrxPx(), data.getLryPx()); |
277 |
|
278 |
//Comprobamos que las esquinas no esten cambiadas de sitio
|
279 |
if(ulPx.getX() > lrPx.getX()) {
|
280 |
double ulTmp = ulPx.getX();
|
281 |
ulPx.setLocation(lrPx.getX(), ulPx.getY()); |
282 |
lrPx.setLocation(ulTmp, lrPx.getY()); |
283 |
} |
284 |
if(ulPx.getY() > lrPx.getY()) {
|
285 |
double ulTmp = ulPx.getY();
|
286 |
ulPx.setLocation(ulPx.getX(), lrPx.getY()); |
287 |
lrPx.setLocation(lrPx.getX(), ulTmp); |
288 |
} |
289 |
|
290 |
//Ajustamos la selecci?n al ?rea
|
291 |
ulPx = adjustPixelRequest(ulPx); |
292 |
lrPx = adjustPixelRequest(lrPx); |
293 |
|
294 |
Point2D ulWc = new Point2D.Double(); |
295 |
Point2D lrWc = new Point2D.Double(); |
296 |
at.transform(ulPx, ulWc); |
297 |
at.transform(new Point2D.Double(lrPx.getX() + 1, lrPx.getY() + 1), lrWc); |
298 |
|
299 |
data.setCoorRealFromDouble(ulWc.getX(), ulWc.getY(), lrWc.getX(), lrWc.getY()); |
300 |
data.setCoorPixelFromDouble(ulPx.getX(), ulPx.getY(), lrPx.getX(), lrPx.getY()); |
301 |
data.initSize(); |
302 |
getClippingPanel().saveStatus(data); |
303 |
|
304 |
getClippingPanel().getButtonsPanel().getButton(ButtonsPanel.BUTTON_APPLY).setEnabled(true);
|
305 |
getClippingPanel().getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT).setEnabled(true);
|
306 |
} else {
|
307 |
Point2D ulWc = new Point2D.Double(data.getUlxWc(), data.getUlyWc()); |
308 |
Point2D lrWc = new Point2D.Double(data.getLrxWc(), data.getLryWc()); |
309 |
|
310 |
//Ajustamos la selecci?n al ?rea
|
311 |
ulWc = getFLayer().adjustWorldRequest(ulWc); |
312 |
lrWc = getFLayer().adjustWorldRequest(lrWc); |
313 |
|
314 |
Point2D ulPx = new Point2D.Double(); |
315 |
Point2D lrPx = new Point2D.Double(); |
316 |
|
317 |
try {
|
318 |
at.inverseTransform(ulWc, ulPx); |
319 |
at.inverseTransform(lrWc, lrPx); |
320 |
} catch (NoninvertibleTransformException e) { |
321 |
JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), PluginServices.getText(this, "coordenadas_erroneas")); |
322 |
return;
|
323 |
} |
324 |
|
325 |
data.setCoorRealFromDouble(ulWc.getX(), ulWc.getY(), lrWc.getX(), lrWc.getY()); |
326 |
data.setCoorPixelFromDouble(Math.abs(ulPx.getX()), Math.abs(ulPx.getY()), Math.abs(lrPx.getX() - 1), Math.abs(lrPx.getY() - 1)); |
327 |
data.initSize(); |
328 |
getClippingPanel().saveStatus(data); |
329 |
|
330 |
getClippingPanel().getButtonsPanel().getButton(ButtonsPanel.BUTTON_APPLY).setEnabled(true);
|
331 |
getClippingPanel().getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT).setEnabled(true);
|
332 |
} |
333 |
} catch (NumberFormatException ex) { |
334 |
return;
|
335 |
} |
336 |
|
337 |
} |
338 |
|
339 |
/**
|
340 |
* Ajusta las coordenadas especificadas en los par?metros al ?rea m?xima
|
341 |
* del raster en p?xeles
|
342 |
* @param req
|
343 |
*/
|
344 |
private Point2D adjustPixelRequest(Point2D req) { |
345 |
req.setLocation(Math.max(0, req.getX()), Math.max(0, req.getY())); |
346 |
req.setLocation(Math.min(dim.width - 1, req.getX()), Math.min(dim.height - 1, req.getY())); |
347 |
return req;
|
348 |
} |
349 |
|
350 |
/**
|
351 |
* Invocaci?n de los eventos de la ventana de <code>DefaultButtonsPanel</code>
|
352 |
*/
|
353 |
public void actionButtonPressed(ButtonsPanelEvent e) { |
354 |
// Bot?n de Aceptar
|
355 |
if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
|
356 |
accept(); |
357 |
close(); |
358 |
} |
359 |
|
360 |
// Bot?n de Aplicar
|
361 |
if (e.getButton() == ButtonsPanel.BUTTON_APPLY)
|
362 |
accept(); |
363 |
|
364 |
// Bot?n de Cerrar
|
365 |
if (e.getButton() == ButtonsPanel.BUTTON_CANCEL)
|
366 |
close(); |
367 |
|
368 |
getFLayer().getMapContext().invalidate(); |
369 |
} |
370 |
|
371 |
/**
|
372 |
* Cerrar la ventana del recorte
|
373 |
*/
|
374 |
private void close() { |
375 |
try {
|
376 |
if (getLastTool() != null) |
377 |
getMapControl().setTool(getLastTool()); |
378 |
PluginServices.getMDIManager().closeWindow(getClippingPanel().getClippingDialog()); |
379 |
} catch (ArrayIndexOutOfBoundsException ex) { |
380 |
// Si la ventana no se puede eliminar no hacemos nada
|
381 |
} |
382 |
} |
383 |
|
384 |
/**
|
385 |
* Obtener el <code>ClippingPanel</code> asociado
|
386 |
* @return ClippingPanel
|
387 |
*/
|
388 |
private ClippingPanel getClippingPanel() {
|
389 |
return clippingPanel;
|
390 |
} |
391 |
|
392 |
private ClippingCoordinatesPanel getCoordinatesPanel() {
|
393 |
return getClippingPanel().getCoordinatesPanel();
|
394 |
} |
395 |
|
396 |
private ClippingResolutionPanel getResolutionPanel() {
|
397 |
return getClippingPanel().getResolutionPanel();
|
398 |
} |
399 |
|
400 |
private ClippingOptionsPanel getOptionsPanel() {
|
401 |
return getClippingPanel().getOptionsPanel();
|
402 |
} |
403 |
|
404 |
private ClippingSelectionPanel getSelectionPanel() {
|
405 |
return getClippingPanel().getSelectionPanel();
|
406 |
} |
407 |
|
408 |
/**
|
409 |
* Acciones realizadas cuando se acepta en el dialogo. Se obtendr?n los datos
|
410 |
* de recorte desde el dialogo, crearemos el objeto ClippingProcess que
|
411 |
* gestiona el recortado, ajustamos el tama?o del grid de salida y procesamos.
|
412 |
*/
|
413 |
private void accept() { |
414 |
// Controlamos las coordenadas del recorte que no se salgan de la imagen.
|
415 |
// De ser as? mostramos un error
|
416 |
CoordinatesPanel coordinatesReales = getCoordinatesPanel().getRealCoordinates(); |
417 |
double ulx = 0; |
418 |
double lrx = 0; |
419 |
double lry = 0; |
420 |
double uly = 0; |
421 |
try {
|
422 |
ulx = Double.parseDouble(coordinatesReales.getValue11());
|
423 |
lry = Double.parseDouble(coordinatesReales.getValue22());
|
424 |
lrx = Double.parseDouble(coordinatesReales.getValue21());
|
425 |
uly = Double.parseDouble(coordinatesReales.getValue12());
|
426 |
Rectangle2D ext = getFLayer().getFullExtent();
|
427 |
if (((int) ulx) > ((int) ext.getMaxX()) || ((int) lrx) < ((int) ext.getMinX()) || ((int) uly) > ((int) ext.getMaxY()) || ((int) lry) < ((int) ext.getMinY())) { |
428 |
JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), PluginServices.getText(this, "coordenadas_erroneas")); |
429 |
return;
|
430 |
} |
431 |
} catch (NumberFormatException e) { |
432 |
JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), PluginServices.getText(this, "coordenadas_erroneas")); |
433 |
return;
|
434 |
} |
435 |
|
436 |
// Obtenemos las coordenadas del recorte
|
437 |
CoordinatesPanel coordinatesPixel = getCoordinatesPanel().getPixelCoordinates(); |
438 |
int[] dValues = new int[4]; |
439 |
try {
|
440 |
dValues[0] = (int) Math.round(Double.valueOf(coordinatesPixel.getValue11()).doubleValue()); |
441 |
dValues[1] = (int) Math.round(Double.valueOf(coordinatesPixel.getValue22()).doubleValue()); |
442 |
dValues[2] = (int) Math.round(Double.valueOf(coordinatesPixel.getValue21()).doubleValue()); |
443 |
dValues[3] = (int) Math.round(Double.valueOf(coordinatesPixel.getValue12()).doubleValue()); |
444 |
} catch (NumberFormatException exc) { |
445 |
// Los valores de las cajas son incorrectos. Terminamos la funci?n
|
446 |
return;
|
447 |
} |
448 |
|
449 |
// Seleccionamos las bandas que se usaran en el recorte a partir de la tabla
|
450 |
int countBands = 0; |
451 |
int rowCount = ((CheckBoxModel) getSelectionPanel().getTableContainer().getModel()).getRowCount();
|
452 |
for (int iRow = 0; iRow < rowCount; iRow++) |
453 |
if ((((Boolean) ((CheckBoxModel) getSelectionPanel().getTableContainer().getModel()).getValueAt(iRow, 0))).booleanValue()) |
454 |
countBands++; |
455 |
|
456 |
int[] drawableBands = new int[countBands]; |
457 |
int i = 0; |
458 |
for (int iRow = 0; iRow < rowCount; iRow++) { |
459 |
if ((((Boolean) ((CheckBoxModel) getSelectionPanel().getTableContainer().getModel()).getValueAt(iRow, 0))).booleanValue()) { |
460 |
int row = ((Integer) ((CheckBoxModel) getSelectionPanel().getTableContainer().getModel()).getValueAt(iRow, 2)).intValue(); |
461 |
drawableBands[i++] = row; |
462 |
} |
463 |
} |
464 |
|
465 |
/**
|
466 |
* Donde se va a guardar el fichero
|
467 |
*/
|
468 |
String path;
|
469 |
if (getOptionsPanel().getCbSaveFile().isSelected())
|
470 |
path = getOptionsPanel().getDirectoryTextField().getText(); |
471 |
else
|
472 |
path = Utilities.createTempDirectory();
|
473 |
|
474 |
String file = getOptionsPanel().getFilenameTextField().getText();
|
475 |
if (file.compareTo(RasterLibrary.getOnlyLayerName()) == 0) |
476 |
RasterLibrary.usesOnlyLayerName(); |
477 |
|
478 |
if (file == "") |
479 |
file = "cutlayer";
|
480 |
|
481 |
String filename = path + File.separator + file; |
482 |
|
483 |
if (new File(filename + ".tif").exists()) |
484 |
if (!RasterToolsUtil.messageBoxYesOrNot("raster_error_file_exists", getOptionsPanel())) |
485 |
return;
|
486 |
|
487 |
/**
|
488 |
* Preparacion para la generacion del proceso del recorte
|
489 |
*/
|
490 |
if (getFLayer() == null) |
491 |
return;
|
492 |
|
493 |
WriterBufferServer dataWriter1 = new WriterBufferServer();
|
494 |
|
495 |
AffineTransform transf = calcAffineTransform(ulx, uly, lrx, lry,
|
496 |
Math.round(data.getPxWidth()), Math.round(data.getPxHeight()), at); |
497 |
|
498 |
int interpMethod = getResolutionPanel().getSelectedInterpolationMethod();
|
499 |
|
500 |
// Creamos la interpretaci?n de color para el caso de que la salida tenga
|
501 |
// m?s de una banda por fichero. Siempre creamos RED, GREEN y BLUE
|
502 |
String[] ci = new String[drawableBands.length]; |
503 |
for (int j = 0; j < ci.length; j++) { |
504 |
switch (j) {
|
505 |
case 0: |
506 |
if (ci.length >= 3) |
507 |
ci[j] = DatasetColorInterpretation.RED_BAND; |
508 |
else
|
509 |
ci[j] = DatasetColorInterpretation.GRAY_BAND; |
510 |
break;
|
511 |
case 1: |
512 |
if (ci.length >= 3) |
513 |
ci[j] = DatasetColorInterpretation.GREEN_BAND; |
514 |
else
|
515 |
ci[j] = DatasetColorInterpretation.UNDEF_BAND; |
516 |
break;
|
517 |
case 2: |
518 |
ci[j] = DatasetColorInterpretation.BLUE_BAND; |
519 |
break;
|
520 |
default:
|
521 |
ci[j] = DatasetColorInterpretation.UNDEF_BAND; |
522 |
break;
|
523 |
} |
524 |
} |
525 |
|
526 |
RasterProcess clippingProcess = new ClippingProcess();
|
527 |
clippingProcess.setActions(this);
|
528 |
clippingProcess.addParam("viewname", getViewName());
|
529 |
//clippingProcess.addParam("pixelcoordinates", dValues);
|
530 |
clippingProcess.addParam("realcoordinates", new double[]{ulx, uly, lrx, lry}); |
531 |
clippingProcess.addParam("filename", filename);
|
532 |
clippingProcess.addParam("datawriter", dataWriter1);
|
533 |
clippingProcess.addParam("layer", getFLayer());
|
534 |
clippingProcess.addParam("drawablebands", drawableBands);
|
535 |
clippingProcess.addParam("onelayerperband", new Boolean(getOptionsPanel().getCbOneLyrPerBand().isSelected())); |
536 |
clippingProcess.addParam("interpolationmethod", new Integer(interpMethod)); |
537 |
clippingProcess.addParam("affinetransform", transf);
|
538 |
clippingProcess.addParam("colorInterpretation", new DatasetColorInterpretation(ci)); |
539 |
clippingProcess.addParam("resolution", new int[]{(int) Math.round(data.getPxWidth()), |
540 |
(int) Math.round(data.getPxHeight())}); |
541 |
clippingProcess.start(); |
542 |
} |
543 |
|
544 |
/**
|
545 |
* Calcula la matriz de transformaci?n que se usar? para el nuevo raster generado.
|
546 |
* @param ulx Coordenada X real de la esquina superior izquierda
|
547 |
* @param uly Coordenada Y real de la esquina superior izquierda
|
548 |
* @param lrx Coordenada X real de la esquina inferior derecha
|
549 |
* @param lry Coordenada Y real de la esquina inferior derecha
|
550 |
* @param width Ancho en p?xeles del nuevo raster
|
551 |
* @param height Alto en p?xeles del nuevo raster
|
552 |
* @param trans Matriz de transformaci?n de la nueva capa
|
553 |
* @return Matriz de transformaci?n para el nuevo raster
|
554 |
*/
|
555 |
private AffineTransform calcAffineTransform(double ulx, double uly, double lrx, double lry, |
556 |
double width, double height, AffineTransform trans) { |
557 |
Point2D ul = new Point2D.Double(ulx, uly); |
558 |
Point2D lr = new Point2D.Double(lrx, lry); |
559 |
try {
|
560 |
trans.inverseTransform(ul, ul); |
561 |
trans.inverseTransform(lr, lr); |
562 |
} catch (NoninvertibleTransformException e) { |
563 |
JOptionPane.showMessageDialog(null, RasterToolsUtil.getText(this, "coordenadas_erroneas")); |
564 |
return new AffineTransform(); |
565 |
} |
566 |
double w = Math.abs(lr.getX() - ul.getX()); |
567 |
|
568 |
Point2D ur = new Point2D.Double(ul.getX() + w, ul.getY()); |
569 |
Point2D ll = new Point2D.Double(lr.getX() - w, lr.getY() ); |
570 |
|
571 |
//Obtenemos la georreferenciaci?n de las cuatro esquinas del nuevo raster
|
572 |
trans.transform(ul, ul); |
573 |
trans.transform(ur, ur); |
574 |
trans.transform(lr, lr); |
575 |
trans.transform(ll, ll); |
576 |
|
577 |
double pixelSizeX = (ur.getX() - ul.getX()) / width;
|
578 |
double pixelSizeY = (ll.getY() - ul.getY()) / height;
|
579 |
double rotX = trans.getShearX();
|
580 |
double rotY = trans.getShearY();
|
581 |
return new AffineTransform(pixelSizeX, rotY, rotX, pixelSizeY, ulx, uly); |
582 |
} |
583 |
|
584 |
/*
|
585 |
* (non-Javadoc)
|
586 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
587 |
*/
|
588 |
public boolean cancelDrawing() { |
589 |
return false; |
590 |
} |
591 |
|
592 |
/*
|
593 |
* (non-Javadoc)
|
594 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
595 |
*/
|
596 |
public Cursor getCursor() { |
597 |
return null; |
598 |
} |
599 |
|
600 |
/*
|
601 |
* (non-Javadoc)
|
602 |
* @see org.gvsig.gui.beans.coordinatespanel.CoordinatesListener#actionValueChanged(org.gvsig.gui.beans.coordinatespanel.CoordinatesEvent)
|
603 |
*/
|
604 |
public void actionValueChanged(CoordinatesEvent e) { |
605 |
if (e.getSource() == getCoordinatesPanel().getPixelCoordinates()) {
|
606 |
eventJTextField(e); |
607 |
recalcCoordFields(true);
|
608 |
} |
609 |
if (e.getSource() == getCoordinatesPanel().getRealCoordinates()) {
|
610 |
eventJTextField(e); |
611 |
recalcCoordFields(false);
|
612 |
} |
613 |
} |
614 |
|
615 |
/*
|
616 |
* (non-Javadoc)
|
617 |
* @see org.gvsig.gui.beans.datainput.DataInputContainerListener#actionValueChanged(java.util.EventObject)
|
618 |
*/
|
619 |
public void actionValueChanged(EventObject e) { |
620 |
if(!enableValueChangedEvent)
|
621 |
return;
|
622 |
|
623 |
enableValueChangedEvent = false; //Desactivamos el evento de cambio de valor de las cajas de texto para que no se bucle |
624 |
|
625 |
if (e.getSource() == getResolutionPanel().getCCellSize().getDataInputField()) {
|
626 |
// Cambiamos PS ==> wPx=wWC/PS & hPx=wPx/rel
|
627 |
double ps = 0; |
628 |
try {
|
629 |
ps = Double.parseDouble(getResolutionPanel().getCCellSize().getValue());
|
630 |
} catch (NumberFormatException ex) { |
631 |
return;
|
632 |
} |
633 |
data.setPxWidth(data.getWcWidth() / ps); |
634 |
data.setPxHeight(data.getWcHeight() / ps); |
635 |
data.updateObservers(); |
636 |
} else if (e.getSource() == getResolutionPanel().getCWidth().getDataInputField()) { |
637 |
// Cambiamos wPx ==> hPx=wPx/rel & PS=wWC/wPx
|
638 |
double wPx = 0; |
639 |
try {
|
640 |
wPx = Double.parseDouble(getResolutionPanel().getCWidth().getValue());
|
641 |
} catch (NumberFormatException ex) { |
642 |
return;
|
643 |
} |
644 |
data.setPxWidth(wPx); |
645 |
data.setPxHeight(Math.round(wPx / data.getRatio()));
|
646 |
data.updateObservers(); |
647 |
} else if (e.getSource() == getResolutionPanel().getCHeight().getDataInputField()) { |
648 |
// Cambiamos hPx ==> wPx=rel*wPx & PS=hWC/hPx
|
649 |
double hPx = 0; |
650 |
try {
|
651 |
hPx = Double.parseDouble(getResolutionPanel().getCHeight().getValue());
|
652 |
} catch (NumberFormatException ex) { |
653 |
return;
|
654 |
} |
655 |
data.setPxHeight(hPx); |
656 |
data.setPxWidth(Math.round(Math.round(hPx * data.getRatio()))); |
657 |
data.updateObservers(); |
658 |
} |
659 |
enableValueChangedEvent = true;
|
660 |
} |
661 |
|
662 |
/**
|
663 |
* Asigna el valor para la activaci?n y desactivaci?n del evento de cambio de valor en
|
664 |
* las cajas de texto.
|
665 |
* @param enableValueChangedEvent
|
666 |
*/
|
667 |
public void setEnableValueChangedEvent(boolean enableValueChangedEvent) { |
668 |
this.enableValueChangedEvent = enableValueChangedEvent;
|
669 |
} |
670 |
|
671 |
/*
|
672 |
* (non-Javadoc)
|
673 |
* @see org.gvsig.raster.IProcessActions#end(java.lang.Object)
|
674 |
*/
|
675 |
public void end(Object params) { |
676 |
if( params instanceof Object[] && |
677 |
((Object[])params).length == 2 && |
678 |
((Object[])params)[0] instanceof String && |
679 |
((Object[])params)[1] instanceof Long) { |
680 |
|
681 |
String fName = (String)((Object[])params)[0]; |
682 |
long milis = ((Long)((Object[])params)[1]).longValue(); |
683 |
|
684 |
EndInfoDialog.show(fName, milis); |
685 |
} |
686 |
} |
687 |
|
688 |
/**
|
689 |
* Obtener la capa de un raster.
|
690 |
* @return
|
691 |
*/
|
692 |
public FLyrRasterSE getFLayer() {
|
693 |
return fLayer;
|
694 |
} |
695 |
|
696 |
/**
|
697 |
* Obtiene la ultima herramienta seleccionada antes de cargar el recorte
|
698 |
* @return
|
699 |
*/
|
700 |
public String getLastTool() { |
701 |
return lastTool;
|
702 |
} |
703 |
|
704 |
/**
|
705 |
* Obtiene el nombre de la vista
|
706 |
* @return
|
707 |
*/
|
708 |
public String getViewName() { |
709 |
return viewName;
|
710 |
} |
711 |
|
712 |
/**
|
713 |
* Establecer la capa para usarla en el recorte
|
714 |
* @param fLayer
|
715 |
*/
|
716 |
public void setLayer(FLyrRasterSE fLayer) { |
717 |
this.fLayer = fLayer;
|
718 |
View view = (View) PluginServices.getMDIManager().getActiveWindow(); |
719 |
viewName = PluginServices.getMDIManager().getWindowInfo(view).getTitle(); |
720 |
mapControl = view.getMapControl(); |
721 |
|
722 |
lastTool = mapControl.getCurrentTool(); |
723 |
|
724 |
// Listener de eventos de movimiento que pone las coordenadas del rat?n en
|
725 |
// la barra de estado
|
726 |
StatusBarListener sbl = new StatusBarListener(mapControl);
|
727 |
|
728 |
// Cortar Raster
|
729 |
ClippingMouseViewListener clippingMouseViewListener = new ClippingMouseViewListener(mapControl, getClippingPanel(), data, fLayer);
|
730 |
mapControl.addMapTool("cutRaster", new Behavior[] { |
731 |
new RectangleBehavior(clippingMouseViewListener), new MouseMovementBehavior(sbl) |
732 |
} |
733 |
); |
734 |
|
735 |
getSelectionPanel().setLayer(fLayer); |
736 |
|
737 |
// Obtener la extension completa de la capa
|
738 |
|
739 |
if(fLayer instanceof IRasterGeoOperations && fLayer instanceof IRasterOperations) { |
740 |
setAffineTransform(((IRasterGeoOperations)fLayer).getAffineTransform()); |
741 |
setDimension(new Dimension((int)((IRasterOperations)fLayer).getPxWidth(), (int)((IRasterOperations)fLayer).getPxHeight())); |
742 |
} else {
|
743 |
RasterToolsUtil.messageBoxError("Error obteniendo el extent.", this); |
744 |
return;
|
745 |
} |
746 |
|
747 |
} |
748 |
|
749 |
/**
|
750 |
* Acciones que se realizan para seleccionar la tool CutRaster
|
751 |
*/
|
752 |
public void selectToolButton() { |
753 |
// seleccionamos la vista de gvSIG
|
754 |
com.iver.cit.gvsig.project.documents.view.gui.View theView = null;
|
755 |
try {
|
756 |
IWindow[] allViews = PluginServices.getMDIManager().getAllWindows();
|
757 |
for (int i = 0; i < allViews.length; i++) { |
758 |
if (allViews[i] instanceof com.iver.cit.gvsig.project.documents.view.gui.View |
759 |
&& PluginServices.getMDIManager().getWindowInfo((View) allViews[i])
|
760 |
.getTitle().equals(viewName)) |
761 |
theView = (com.iver.cit.gvsig.project.documents.view.gui.View) allViews[i]; |
762 |
} |
763 |
if (theView == null) |
764 |
return;
|
765 |
} catch (ClassCastException ex) { |
766 |
// RasterToolsUtil.messageBoxError("cant_get_view "), this, ex);
|
767 |
return;
|
768 |
} |
769 |
MapControl m_MapControl = theView.getMapControl(); |
770 |
|
771 |
// Listener de eventos de movimiento que pone las coordenadas del rat?n en
|
772 |
// la barra de estado
|
773 |
StatusBarListener sbl = new StatusBarListener(m_MapControl);
|
774 |
|
775 |
// Cortar Raster
|
776 |
ClippingMouseViewListener clippingMouseViewListener = new ClippingMouseViewListener(m_MapControl, getClippingPanel(), data, getFLayer());
|
777 |
m_MapControl.addMapTool("cutRaster", new Behavior[] { |
778 |
new RectangleBehavior(clippingMouseViewListener), new MouseMovementBehavior(sbl) |
779 |
} |
780 |
); |
781 |
|
782 |
m_MapControl.setTool("cutRaster");
|
783 |
} |
784 |
|
785 |
/**
|
786 |
* Obtiene el <code>MapControl</code> de gvSIG
|
787 |
* @return <code>MapControl</code>
|
788 |
*/
|
789 |
public MapControl getMapControl() {
|
790 |
return mapControl;
|
791 |
} |
792 |
|
793 |
|
794 |
public void rectangle(RectangleEvent event) throws BehaviorException {} |
795 |
public void interrupted() {} |
796 |
} |