svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / clipping / ui / listener / ClippingMouseViewListener.java @ 30938
History | View | Annotate | Download (5.61 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 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.geom.AffineTransform; |
22 |
import java.awt.geom.NoninvertibleTransformException; |
23 |
import java.awt.geom.Point2D; |
24 |
|
25 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
26 |
import org.gvsig.fmap.raster.tools.SaveRasterListenerImpl; |
27 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
28 |
import org.gvsig.raster.util.RasterToolsUtil; |
29 |
import org.gvsig.raster.util.RasterUtilities; |
30 |
import org.gvsig.rastertools.clipping.ClippingData; |
31 |
import org.gvsig.rastertools.clipping.ui.ClippingPanel; |
32 |
|
33 |
import com.iver.cit.gvsig.fmap.MapControl; |
34 |
import com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent; |
35 |
/**
|
36 |
* <code>ClippingMouseViewListener</code> es un listener del recorte de raster.
|
37 |
* Al seleccionar un ?rea sobre la vista debe cargar el cuadro con los datos de
|
38 |
* coordenadas pixel, coordenadas reales, ancho y alto del raster resultante,
|
39 |
* tama?o de celda.
|
40 |
*
|
41 |
* @version 19/04/2007
|
42 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
43 |
*/
|
44 |
public class ClippingMouseViewListener extends SaveRasterListenerImpl { |
45 |
private FLyrRasterSE layer = null; |
46 |
private ClippingPanel clippingPanel = null; |
47 |
private ClippingData data = null; |
48 |
private MapControl mapControl = null; |
49 |
private String currentTool = null; |
50 |
|
51 |
/**
|
52 |
* Crea un nuevo <code>ClippingMouseViewListener</code>
|
53 |
*
|
54 |
* @param mapCtrl
|
55 |
* @param cutRasterDialog
|
56 |
*/
|
57 |
public ClippingMouseViewListener(MapControl mapControl, ClippingPanel clippingPanel, ClippingData data, FLyrRasterSE lyr) {
|
58 |
super(mapControl);
|
59 |
this.clippingPanel = clippingPanel;
|
60 |
this.data = data;
|
61 |
this.layer = lyr;
|
62 |
this.mapControl = mapControl;
|
63 |
this.currentTool = mapControl.getCurrentTool();
|
64 |
} |
65 |
|
66 |
/**
|
67 |
* Asigna la capa raster.
|
68 |
* @param flyr Capa raster
|
69 |
*/
|
70 |
public void setRasterLayer(FLyrRasterSE flyr) { |
71 |
layer = flyr; |
72 |
} |
73 |
|
74 |
/**
|
75 |
* Realiza las acciones de selecci?n del ?rea de recorte por medio de un rectangulo
|
76 |
* sobre la vista.
|
77 |
*/
|
78 |
public void rectangle(RectangleEvent event) { |
79 |
super.rectangle(event);
|
80 |
clippingPanel.getButtonsPanel().getButton(ButtonsPanel.BUTTON_APPLY).setEnabled(false);
|
81 |
clippingPanel.getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT).setEnabled(false);
|
82 |
|
83 |
if(((FLyrRasterSE) layer).getAffineTransform() == null) |
84 |
return;
|
85 |
|
86 |
AffineTransform at = ((FLyrRasterSE) layer).getAffineTransform();
|
87 |
//Temporalmente cargamos las coordenadas reales para luego transformarlas a pixel
|
88 |
Point2D ulWc = new Point2D.Double(rect.getMinX(), rect.getMaxY()); |
89 |
Point2D lrWc = new Point2D.Double(rect.getMaxX(), rect.getMinY()); |
90 |
Point2D urWc = new Point2D.Double(rect.getMaxX(), rect.getMaxY()); |
91 |
Point2D llWc = new Point2D.Double(rect.getMinX(), rect.getMinY()); |
92 |
Point2D ulPx = new Point2D.Double(); |
93 |
Point2D lrPx = new Point2D.Double(); |
94 |
Point2D urPx = new Point2D.Double(); |
95 |
Point2D llPx = new Point2D.Double(); |
96 |
try {
|
97 |
at.inverseTransform(ulWc, ulPx); |
98 |
at.inverseTransform(lrWc, lrPx); |
99 |
at.inverseTransform(urWc, urPx); |
100 |
at.inverseTransform(llWc, llPx); |
101 |
} catch (NoninvertibleTransformException e) { |
102 |
RasterToolsUtil.messageBoxError(RasterToolsUtil.getText(this, "coordenadas_erroneas"), null); |
103 |
return;
|
104 |
} |
105 |
|
106 |
Point2D[] pointList = new Point2D[]{ulPx, lrPx, urPx, llPx}; |
107 |
Point2D dim = new Point2D.Double(layer.getPxWidth(), layer.getPxHeight()); |
108 |
|
109 |
//Comprobamos si la selecci?n est? completamente fuera del ?rea
|
110 |
if(isOutside(ulPx, lrPx)) {
|
111 |
data.setOutOfArea(); |
112 |
return;
|
113 |
} |
114 |
|
115 |
//Ajustamos los puntos al ?rea en pixeles del raster
|
116 |
RasterUtilities.adjustToPixelSize(pointList, dim); |
117 |
|
118 |
//Convertimos nuevamente a coordenadas reales despu?s de ajustar
|
119 |
at.transform(ulPx, ulWc); |
120 |
at.transform(lrPx, lrWc); |
121 |
at.transform(llPx, llWc); |
122 |
at.transform(urPx, urWc); |
123 |
|
124 |
data.setCoorPixel(ulPx, lrPx, llPx, urPx); |
125 |
data.setCoorReal(ulWc, lrWc, llWc, urWc); |
126 |
data.setAffineTransform(at); |
127 |
data.initSize(); |
128 |
|
129 |
clippingPanel.saveStatus(data); |
130 |
|
131 |
clippingPanel.getButtonsPanel().getButton(ButtonsPanel.BUTTON_APPLY).setEnabled(true);
|
132 |
clippingPanel.getButtonsPanel().getButton(ButtonsPanel.BUTTON_ACCEPT).setEnabled(true);
|
133 |
|
134 |
if (currentTool != null) |
135 |
mapControl.setTool(currentTool); |
136 |
} |
137 |
|
138 |
/**
|
139 |
* Comprueba si la selecci?n del punto est? fuera del ?rea del raster.
|
140 |
* @param ulPx Coordenada superior izquierda en pixeles
|
141 |
* @param lrPx Corrdenada inferior derecha en pixeles
|
142 |
* @return true si la selecci?n del punto est? fuera del raster y false si no lo est?
|
143 |
*/
|
144 |
private boolean isOutside(Point2D ulPx, Point2D lrPx) { |
145 |
double minX = Math.min(ulPx.getX(), lrPx.getX()); |
146 |
double minY = Math.min(ulPx.getY(), lrPx.getY()); |
147 |
double maxX = Math.max(ulPx.getX(), lrPx.getX()); |
148 |
double maxY = Math.max(ulPx.getY(), lrPx.getY()); |
149 |
if(minX >= layer.getPxWidth() || minY >= layer.getPxHeight() || maxX < 0 || maxY < 0) |
150 |
return true; |
151 |
return false; |
152 |
} |
153 |
} |