root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / cutting / ui / listener / CuttingPanelListener.java @ 13021
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.cutting.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.event.FocusEvent; |
27 |
import java.awt.event.FocusListener; |
28 |
import java.awt.event.KeyEvent; |
29 |
import java.awt.event.KeyListener; |
30 |
import java.awt.geom.AffineTransform; |
31 |
import java.awt.geom.NoninvertibleTransformException; |
32 |
import java.awt.geom.Point2D; |
33 |
import java.awt.geom.Rectangle2D; |
34 |
import java.io.File; |
35 |
|
36 |
import javax.swing.JOptionPane; |
37 |
|
38 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
39 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel; |
40 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent; |
41 |
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener; |
42 |
import org.gvsig.gui.beans.coorddatainput.CoordDataInputContainer; |
43 |
import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
44 |
import org.gvsig.gui.beans.table.models.CheckBoxModel; |
45 |
import org.gvsig.raster.dataset.MultiRasterDataset; |
46 |
import org.gvsig.raster.datastruct.Extent; |
47 |
import org.gvsig.raster.util.RasterUtilities; |
48 |
import org.gvsig.rastertools.RasterModule; |
49 |
import org.gvsig.rastertools.cutting.CuttingProcess; |
50 |
import org.gvsig.rastertools.cutting.WriterBufferServer; |
51 |
import org.gvsig.rastertools.cutting.ui.CuttingPanel; |
52 |
|
53 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
54 |
import com.iver.andami.PluginServices; |
55 |
import com.iver.andami.Utilities; |
56 |
import com.iver.andami.messages.NotificationManager; |
57 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
58 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
59 |
import com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent; |
60 |
import com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener; |
61 |
/**
|
62 |
* <code>CuttingPanelListener</code> es una clase donde se recoger?n y
|
63 |
* tratar?n todos los eventos del panel de recorte
|
64 |
*
|
65 |
* @version 19/04/2007
|
66 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
67 |
*/
|
68 |
public class CuttingPanelListener implements KeyListener, FocusListener, ActionListener, RectangleListener, ButtonsPanelListener { |
69 |
private Dimension dim = null; |
70 |
private AffineTransform at = null; |
71 |
CuttingPanel cuttingPanel = null;
|
72 |
private int widthPx = 0; |
73 |
private int heightPx = 0; |
74 |
|
75 |
/**
|
76 |
* Crea un nuevo <code>CuttingPanelListener</code> con el
|
77 |
* <code>CuttingPanel</code> asociado
|
78 |
*
|
79 |
* @param panel
|
80 |
*/
|
81 |
public CuttingPanelListener(CuttingPanel panel) {
|
82 |
this.cuttingPanel = panel;
|
83 |
} |
84 |
|
85 |
/**
|
86 |
* Asigna la matriz de transformaci?n entre puntos en coordenadas del raster
|
87 |
* y puntos en coordenadas reales.
|
88 |
* @param AffineTransform
|
89 |
*/
|
90 |
public void setAffineTransform(AffineTransform at) { |
91 |
this.at = at;
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* Asigna la dimensi?n del raster
|
96 |
* @param dim
|
97 |
*/
|
98 |
public void setDimension(Dimension dim) { |
99 |
this.dim = dim;
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* Cuando se presiona la tecla enter sobre los campos de texto se recalculan
|
104 |
* las coordenadas. Si se ha presionado sobre los cuadro correspondientes a
|
105 |
* las coordenadas pixel se recalculan las coordenadas del mundo real y
|
106 |
* viceversa.
|
107 |
*/
|
108 |
public void keyReleased(KeyEvent e) { |
109 |
if (e.getKeyCode() == 10) { |
110 |
if (e.getSource() == getCuttingPanel().getCoor_pixel().getPDataInput11().getJTextField()
|
111 |
|| e.getSource() == getCuttingPanel().getCoor_pixel().getPDataInput12().getJTextField() |
112 |
|| e.getSource() == getCuttingPanel().getCoor_pixel().getPDataInput21().getJTextField() |
113 |
|| e.getSource() == getCuttingPanel().getCoor_pixel().getPDataInput22().getJTextField()) { |
114 |
eventJTextField(e.getSource()); |
115 |
recalcCoordFields(true);
|
116 |
} |
117 |
if (e.getSource() == getCuttingPanel().getCoor_reales().getPDataInput11().getJTextField()
|
118 |
|| e.getSource() == getCuttingPanel().getCoor_reales().getPDataInput12().getJTextField() |
119 |
|| e.getSource() == getCuttingPanel().getCoor_reales().getPDataInput21().getJTextField() |
120 |
|| e.getSource() == getCuttingPanel().getCoor_reales().getPDataInput22().getJTextField()) { |
121 |
eventJTextField(e.getSource()); |
122 |
recalcCoordFields(false);
|
123 |
} |
124 |
if (e.getSource() == getCuttingPanel().getPResolution().getCCellSize().getJTextField()) {
|
125 |
// Cambiamos PS ==> wPx=wWC/PS & hPx=wPx/rel
|
126 |
double ps = 0; |
127 |
try {
|
128 |
ps = Double.parseDouble(getCuttingPanel().getPResolution().getCCellSize().getJTextField().getText());
|
129 |
} catch (NumberFormatException ex) { |
130 |
return;
|
131 |
} |
132 |
Extent mapWindow = getMapWindow(); |
133 |
Rectangle2D pxWindow = getPxWindow();
|
134 |
if (mapWindow == null || pxWindow == null) |
135 |
return;
|
136 |
getCuttingPanel().setWidthText(mapWindow.width() / ps, 0);
|
137 |
getCuttingPanel().setHeightText((mapWindow.width() / ps) / getCuttingPanel().getRelWidthHeight(), 0);
|
138 |
} |
139 |
|
140 |
if (e.getSource() == getCuttingPanel().getPResolution().getCWidth().getJTextField()) {
|
141 |
// Cambiamos wPx ==> hPx=wPx/rel & PS=wWC/wPx
|
142 |
double wPx = 0; |
143 |
try {
|
144 |
wPx = Double.parseDouble(getCuttingPanel().getPResolution().getCWidth().getJTextField().getText());
|
145 |
} catch (NumberFormatException ex) { |
146 |
return;
|
147 |
} |
148 |
Extent mapWindow = getMapWindow(); |
149 |
Rectangle2D pxWindow = getPxWindow();
|
150 |
if (mapWindow == null || pxWindow == null) |
151 |
return;
|
152 |
getCuttingPanel().setWidthText(wPx, 0);
|
153 |
getCuttingPanel().setHeightText(wPx / getCuttingPanel().getRelWidthHeight(), 0);
|
154 |
getCuttingPanel().setCellSizeText((mapWindow.width() / wPx), 4);
|
155 |
} |
156 |
|
157 |
if (e.getSource() == getCuttingPanel().getPResolution().getCHeight().getJTextField()) {
|
158 |
// Cambiamos hPx ==> wPx=rel*wPx & PS=hWC/hPx
|
159 |
double hPx = 0; |
160 |
try {
|
161 |
hPx = Double.parseDouble(getCuttingPanel().getPResolution().getCHeight().getJTextField().getText());
|
162 |
} catch (NumberFormatException ex) { |
163 |
return;
|
164 |
} |
165 |
Extent mapWindow = getMapWindow(); |
166 |
Rectangle2D pxWindow = getPxWindow();
|
167 |
if (mapWindow == null || pxWindow == null) |
168 |
return;
|
169 |
getCuttingPanel().setWidthText(hPx * getCuttingPanel().getRelWidthHeight(), 0);
|
170 |
getCuttingPanel().setHeightText(hPx, 0);
|
171 |
getCuttingPanel().setCellSizeText((mapWindow.height() / hPx), 4);
|
172 |
} |
173 |
} |
174 |
} |
175 |
|
176 |
/**
|
177 |
* Metodo que se ejecuta cuando se sale de un cuadro de texto para
|
178 |
* actualizarlos todos a los valores correctos
|
179 |
*/
|
180 |
public void focusLost(FocusEvent e) { |
181 |
if (e.getSource() == getCuttingPanel().getCoor_pixel().getPDataInput11().getJTextField()
|
182 |
|| e.getSource() == getCuttingPanel().getCoor_pixel().getPDataInput12().getJTextField() |
183 |
|| e.getSource() == getCuttingPanel().getCoor_pixel().getPDataInput21().getJTextField() |
184 |
|| e.getSource() == getCuttingPanel().getCoor_pixel().getPDataInput22().getJTextField()) { |
185 |
eventJTextField(e.getSource()); |
186 |
recalcCoordFields(true);
|
187 |
} |
188 |
if (e.getSource() == getCuttingPanel().getCoor_reales().getPDataInput11().getJTextField()
|
189 |
|| e.getSource() == getCuttingPanel().getCoor_reales().getPDataInput12().getJTextField() |
190 |
|| e.getSource() == getCuttingPanel().getCoor_reales().getPDataInput21().getJTextField() |
191 |
|| e.getSource() == getCuttingPanel().getCoor_reales().getPDataInput22().getJTextField()) { |
192 |
eventJTextField(e.getSource()); |
193 |
recalcCoordFields(false);
|
194 |
} |
195 |
} |
196 |
|
197 |
/**
|
198 |
* M?todo que se invoca cuando se disparan los eventos de los botones de
|
199 |
* extensi?n completa o de seleccion de extensi?n con el rat?n
|
200 |
*/
|
201 |
public void actionPerformed(ActionEvent e) { |
202 |
// Bot?n de selecci?n del extent completo
|
203 |
// Modificamos las coordenadas reales y recalculamos las coordenadas pixel
|
204 |
if (e.getSource() == getCuttingPanel().getButtonBarContainer().getButton(0)) { |
205 |
Point2D ulPx = new Point2D.Double(0, 0); |
206 |
Point2D lrPx = new Point2D.Double(dim.width, dim.height); |
207 |
|
208 |
double minX = Math.min(ulPx.getX(), lrPx.getX()); |
209 |
double maxX = Math.max(ulPx.getX(), lrPx.getX()); |
210 |
double minY = Math.min(ulPx.getY(), lrPx.getY()); |
211 |
double maxY = Math.max(ulPx.getY(), lrPx.getY()); |
212 |
|
213 |
//Convertimos nuevamente a coordenadas reales
|
214 |
Point2D ulWc = new Point2D.Double(); |
215 |
Point2D lrWc = new Point2D.Double(); |
216 |
at.transform(new Point2D.Double(minX, minY), ulWc); |
217 |
at.transform(new Point2D.Double(maxX, maxY), lrWc); |
218 |
|
219 |
getCuttingPanel().setCoorRealFromDouble(ulWc.getX(), ulWc.getY(), lrWc.getX(), lrWc.getY(), 3);
|
220 |
getCuttingPanel().setCoorPixelFromDouble(0, 0, dim.width - 1, dim.height - 1, 3); |
221 |
getCuttingPanel().setWidthText(dim.width, 0);
|
222 |
getCuttingPanel().setHeightText(dim.height, 0);
|
223 |
getCuttingPanel().setCellSizeText(((lrWc.getX() - ulWc.getX()) / dim.width), 4);
|
224 |
getCuttingPanel().setRelWidthHeight((double) (dim.getWidth() / dim.getHeight()));
|
225 |
return;
|
226 |
} |
227 |
|
228 |
// Bot?n de selecci?n de la herramienta de seleccionar desde la vista
|
229 |
if (e.getSource() == getCuttingPanel().getButtonBarContainer().getButton(1)) { |
230 |
getCuttingPanel().selectToolButton(); |
231 |
return;
|
232 |
} |
233 |
} |
234 |
|
235 |
/**
|
236 |
* Al producirse un evento de perdida de foco o pulsaci?n de "enter" en un campo de texto de coordenadas
|
237 |
* hay que asignar el nuevo valor introducido.
|
238 |
* @param obj
|
239 |
*/
|
240 |
private void eventJTextField(Object obj) { |
241 |
try {
|
242 |
if (obj == getCuttingPanel().getCoor_pixel().getPDataInput11().getJTextField())
|
243 |
getCuttingPanel().setCoorPixelFromDouble(Double.valueOf(getCuttingPanel().getCoor_pixel().getPDataInput11().getJTextField().getText()).doubleValue(),
|
244 |
getCuttingPanel().getPxMinY(), getCuttingPanel().getPxMaxX(), getCuttingPanel().getPxMaxY(), 3);
|
245 |
if (obj == getCuttingPanel().getCoor_pixel().getPDataInput12().getJTextField())
|
246 |
getCuttingPanel().setCoorPixelFromDouble(getCuttingPanel().getPxMinX(), |
247 |
Double.valueOf(getCuttingPanel().getCoor_pixel().getPDataInput12().getJTextField().getText()).doubleValue(), getCuttingPanel().getPxMaxX(),
|
248 |
getCuttingPanel().getPxMaxY(), 3);
|
249 |
if (obj == getCuttingPanel().getCoor_pixel().getPDataInput21().getJTextField())
|
250 |
getCuttingPanel().setCoorPixelFromDouble(getCuttingPanel().getPxMinX(), getCuttingPanel().getPxMinY(), |
251 |
Double.valueOf(getCuttingPanel().getCoor_pixel().getPDataInput21().getJTextField().getText()).doubleValue(), getCuttingPanel().getPxMaxY(), 3); |
252 |
if (obj == getCuttingPanel().getCoor_pixel().getPDataInput22().getJTextField())
|
253 |
getCuttingPanel().setCoorPixelFromDouble(getCuttingPanel().getPxMinX(), getCuttingPanel().getPxMinY(), getCuttingPanel().getPxMaxX(), |
254 |
Double.valueOf(getCuttingPanel().getCoor_pixel().getPDataInput22().getJTextField().getText()).doubleValue(), 3); |
255 |
|
256 |
Extent ex = getMapWindow(); |
257 |
if (obj == getCuttingPanel().getCoor_reales().getPDataInput11().getJTextField())
|
258 |
getCuttingPanel().setCoorRealFromDouble(Double.valueOf(getCuttingPanel().getCoor_reales().getPDataInput11().getJTextField().getText()).doubleValue(),
|
259 |
ex.getULY(),//getCuttingPanel().getWcMinY(),
|
260 |
ex.getLRX(),//getCuttingPanel().getWcMaxX(),
|
261 |
ex.getLRY(), 6);//getCuttingPanel().getWcMaxY(), 6); |
262 |
if (obj == getCuttingPanel().getCoor_reales().getPDataInput12().getJTextField())
|
263 |
getCuttingPanel().setCoorRealFromDouble(ex.getULX(),//getCuttingPanel().getWcMinX(),
|
264 |
Double.valueOf(getCuttingPanel().getCoor_reales().getPDataInput12().getJTextField().getText()).doubleValue(),
|
265 |
ex.getLRX(),//getCuttingPanel().getWcMaxX(),
|
266 |
ex.getLRY(), 6);//getCuttingPanel().getWcMaxY(), 6); |
267 |
if (obj == getCuttingPanel().getCoor_reales().getPDataInput21().getJTextField())
|
268 |
getCuttingPanel().setCoorRealFromDouble(ex.getULX(),//getCuttingPanel().getWcMinX(),
|
269 |
ex.getULY(),//getCuttingPanel().getWcMinY(),
|
270 |
Double.valueOf(getCuttingPanel().getCoor_reales().getPDataInput21().getJTextField().getText()).doubleValue(),
|
271 |
ex.getLRY(), 6);//getCuttingPanel().getWcMaxY(), 6); |
272 |
if (obj == getCuttingPanel().getCoor_reales().getPDataInput22().getJTextField())
|
273 |
getCuttingPanel().setCoorRealFromDouble(ex.getULX(), //getCuttingPanel().getWcMinX(),
|
274 |
ex.getURY(), //getCuttingPanel().getWcMinY(),
|
275 |
ex.getLRX(), //getCuttingPanel().getWcMaxX(),
|
276 |
Double.valueOf(getCuttingPanel().getCoor_reales().getPDataInput22().getJTextField().getText()).doubleValue(), 6); |
277 |
} catch (NumberFormatException ex1) { |
278 |
// No hay valores parseables a decimal en las cajas de texto. No hacemos
|
279 |
// nada
|
280 |
} |
281 |
} |
282 |
|
283 |
/**
|
284 |
* Comprueba si todos los campos de coordenadas est?n llenos
|
285 |
* @return true si todos los campos de coordenadas est?n llenos y false si hay alguno vacio.
|
286 |
*/
|
287 |
private boolean isFieldsFill() { |
288 |
if (getCuttingPanel().getCoor_pixel().getPDataInput11().getJTextField().equals("") |
289 |
|| getCuttingPanel().getCoor_pixel().getPDataInput12().getJTextField().equals("")
|
290 |
|| getCuttingPanel().getCoor_pixel().getPDataInput21().getJTextField().equals("")
|
291 |
|| getCuttingPanel().getCoor_pixel().getPDataInput22().getJTextField().equals("")
|
292 |
|| getCuttingPanel().getCoor_reales().getPDataInput11().getJTextField().equals("")
|
293 |
|| getCuttingPanel().getCoor_reales().getPDataInput12().getJTextField().equals("")
|
294 |
|| getCuttingPanel().getCoor_reales().getPDataInput21().getJTextField().equals("")
|
295 |
|| getCuttingPanel().getCoor_reales().getPDataInput22().getJTextField().equals(""))
|
296 |
return false; |
297 |
return true; |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Obtiene un rectangulo con la ventana en pixeles. Estos datos son leidos desde el interfaz.
|
302 |
* @return Rectangle2D
|
303 |
*/
|
304 |
private Rectangle2D getPxWindow(){ |
305 |
if (getCuttingPanel().getPxMinX() == 0 |
306 |
&& getCuttingPanel().getPxMinY() == 0
|
307 |
&& getCuttingPanel().getPxMaxX() == 0
|
308 |
&& getCuttingPanel().getPxMaxY() == 0)
|
309 |
return null; |
310 |
return new Rectangle2D.Double(getCuttingPanel().getPxMinX(), |
311 |
getCuttingPanel().getPxMinY(), |
312 |
Math.abs(getCuttingPanel().getPxMaxX() - getCuttingPanel().getPxMinX()),
|
313 |
Math.abs(getCuttingPanel().getPxMaxY() - getCuttingPanel().getPxMinY()));
|
314 |
} |
315 |
|
316 |
/**
|
317 |
* Obtiene un rectangulo con la ventana en coordenadas reales. Estos datos son leidos desde el interfaz.
|
318 |
* @return Rectangle2D
|
319 |
*/
|
320 |
private Extent getMapWindow(){
|
321 |
if (getCuttingPanel().getULX() == 0 |
322 |
&& getCuttingPanel().getULY() == 0
|
323 |
&& getCuttingPanel().getLRX() == 0
|
324 |
&& getCuttingPanel().getLRY() == 0)
|
325 |
return null; |
326 |
return new Extent( getCuttingPanel().getULX(), |
327 |
getCuttingPanel().getULY(), |
328 |
getCuttingPanel().getLRX(), |
329 |
getCuttingPanel().getLRY()); |
330 |
} |
331 |
|
332 |
/**
|
333 |
* Recalcula el valor de los campos de coordenadas reales y pixel. Cuando modificamos alg?n campo
|
334 |
* de las coordenadas reales se modifican los pixeles y viceversa.
|
335 |
* @param modifyPx true si se ha modificado alg?n campo de coordenadas pixel y false si se ha modificado
|
336 |
* alg?n campo de las coordenadas reales.
|
337 |
*/
|
338 |
private void recalcCoordFields(boolean modifyPx) { |
339 |
if (isFieldsFill()) {
|
340 |
try {
|
341 |
if (modifyPx) {
|
342 |
Rectangle2D pxWindow = getPxWindow();
|
343 |
if (pxWindow == null) |
344 |
return;
|
345 |
Point2D ulPx = new Point2D.Double(pxWindow.getMinX(), pxWindow.getMinY()); |
346 |
Point2D lrPx = new Point2D.Double(pxWindow.getMaxX(), pxWindow.getMaxY()); |
347 |
|
348 |
//Comprobamos si la selecci?n est? fuera del ?rea
|
349 |
if(isOutside(ulPx, lrPx)) {
|
350 |
cuttingPanel.setCoorPixelFromDouble(0, 0, 0, 0, 0); |
351 |
cuttingPanel.setWidthText(0, 0); |
352 |
cuttingPanel.setHeightText(0, 0); |
353 |
cuttingPanel.setCellSizeText(0, 0); |
354 |
cuttingPanel.setRelWidthHeight(0);
|
355 |
cuttingPanel.setCoorRealFromDouble(0, 0, 0, 0, 0); |
356 |
return;
|
357 |
} |
358 |
|
359 |
//Comprobamos que las esquinas no esten cambiadas de sitio
|
360 |
if(ulPx.getX() > lrPx.getX()) {
|
361 |
double ulTmp = ulPx.getX();
|
362 |
ulPx.setLocation(lrPx.getX(), ulPx.getY()); |
363 |
lrPx.setLocation(ulTmp, lrPx.getY()); |
364 |
} |
365 |
if(ulPx.getY() > lrPx.getY()) {
|
366 |
double ulTmp = ulPx.getY();
|
367 |
ulPx.setLocation(ulPx.getX(), lrPx.getY()); |
368 |
lrPx.setLocation(lrPx.getX(), ulTmp); |
369 |
} |
370 |
|
371 |
Point2D[] pointList = new Point2D[]{ulPx, lrPx}; |
372 |
Point2D dim = new Point2D.Double(this.dim.width, this.dim.height); |
373 |
|
374 |
//Ajustamos los puntos al ?rea en pixeles del raster
|
375 |
RasterUtilities.adjustToPixelSize(pointList, dim); |
376 |
|
377 |
Point2D ulWc = new Point2D.Double(); |
378 |
Point2D lrWc = new Point2D.Double(); |
379 |
at.transform(ulPx, ulWc); |
380 |
at.transform(new Point2D.Double(lrPx.getX() + 1, lrPx.getY() + 1), lrWc); |
381 |
|
382 |
cuttingPanel.setCoorRealFromDouble(ulWc.getX(), ulWc.getY(), lrWc.getX(), lrWc.getY(), 3);
|
383 |
cuttingPanel.setCoorPixelFromDouble(ulPx.getX(), ulPx.getY(), lrPx.getX(), lrPx.getY(), 3);
|
384 |
cuttingPanel.setWidthText(dim.getX(), 0);
|
385 |
cuttingPanel.setHeightText(dim.getY(), 0);
|
386 |
cuttingPanel.setCellSizeText((Math.abs(lrWc.getX() - ulWc.getX()) / dim.getX()), 4); |
387 |
cuttingPanel.setRelWidthHeight((double) (dim.getX() / dim.getY()));
|
388 |
} else {
|
389 |
Extent mapWindow = getMapWindow(); |
390 |
if (mapWindow == null) |
391 |
return;
|
392 |
|
393 |
Point2D ulPx = new Point2D.Double(mapWindow.getULX(), mapWindow.getULY()); |
394 |
Point2D lrPx = new Point2D.Double(mapWindow.getLRX(), mapWindow.getLRY()); |
395 |
|
396 |
try {
|
397 |
at.inverseTransform(ulPx, ulPx); |
398 |
at.inverseTransform(lrPx, lrPx); |
399 |
} catch (NoninvertibleTransformException e) { |
400 |
JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), PluginServices.getText(this, "coordenadas_erroneas")); |
401 |
return;
|
402 |
} |
403 |
|
404 |
Point2D[] pointList = new Point2D[]{ulPx, lrPx}; |
405 |
Point2D dim = new Point2D.Double(this.dim.width, this.dim.height); |
406 |
|
407 |
//Comprobamos si la selecci?n est? fuera del ?rea
|
408 |
if(isOutside(ulPx, lrPx)) {
|
409 |
cuttingPanel.setCoorPixelFromDouble(0, 0, 0, 0, 0); |
410 |
cuttingPanel.setWidthText(0, 0); |
411 |
cuttingPanel.setHeightText(0, 0); |
412 |
cuttingPanel.setCellSizeText(0, 0); |
413 |
cuttingPanel.setRelWidthHeight(0);
|
414 |
cuttingPanel.setCoorRealFromDouble(0, 0, 0, 0, 0); |
415 |
return;
|
416 |
} |
417 |
//Ajustamos los puntos al ?rea en pixeles del raster
|
418 |
RasterUtilities.adjustToPixelSize(pointList, dim); |
419 |
|
420 |
double minX = Math.min(ulPx.getX(), lrPx.getX()); |
421 |
double maxX = Math.max(ulPx.getX(), lrPx.getX()); |
422 |
double minY = Math.min(ulPx.getY(), lrPx.getY()); |
423 |
double maxY = Math.max(ulPx.getY(), lrPx.getY()); |
424 |
|
425 |
//Convertimos nuevamente a coordenadas reales
|
426 |
Point2D ulWc = new Point2D.Double(); |
427 |
Point2D lrWc = new Point2D.Double(); |
428 |
at.transform(new Point2D.Double(minX, minY), ulWc); |
429 |
at.transform(new Point2D.Double(maxX + 1, maxY + 1), lrWc); |
430 |
|
431 |
cuttingPanel.setCoorRealFromDouble(ulWc.getX(), ulWc.getY(), lrWc.getX(), lrWc.getY(), 3);
|
432 |
cuttingPanel.setCoorPixelFromDouble(minX, minY, maxX, maxY, 3);
|
433 |
cuttingPanel.setWidthText(dim.getX(), 0);
|
434 |
cuttingPanel.setHeightText(dim.getY(), 0);
|
435 |
cuttingPanel.setCellSizeText(((maxX - minX) / dim.getX()), 4);
|
436 |
cuttingPanel.setRelWidthHeight((double) (dim.getX() / dim.getY()));
|
437 |
} |
438 |
} catch (NumberFormatException ex) { |
439 |
return;
|
440 |
} |
441 |
} |
442 |
} |
443 |
|
444 |
/**
|
445 |
* Comprueba si la selecci?n del punto est? fuera del ?rea del raster.
|
446 |
* @param ulPx Coordenada superior izquierda en pixeles
|
447 |
* @param lrPx Corrdenada inferior derecha en pixeles
|
448 |
* @return true si la selecci?n del punto est? fuera del raster y false si no lo est?
|
449 |
*/
|
450 |
private boolean isOutside(Point2D ulPx, Point2D lrPx) { |
451 |
double minX = Math.min(ulPx.getX(), lrPx.getX()); |
452 |
double minY = Math.min(ulPx.getY(), lrPx.getY()); |
453 |
double maxX = Math.max(ulPx.getX(), lrPx.getX()); |
454 |
double maxY = Math.max(ulPx.getY(), lrPx.getY()); |
455 |
if(minX >= dim.width || minY >= dim.height || maxX < 0 || maxY < 0) |
456 |
return true; |
457 |
return false; |
458 |
} |
459 |
|
460 |
/**
|
461 |
* Definir el ancho de la vista en pixeles
|
462 |
* @param value
|
463 |
*/
|
464 |
public void setWidthPx(int value) { |
465 |
widthPx = value; |
466 |
} |
467 |
|
468 |
/**
|
469 |
* Definir el alto de la vista en pixeles
|
470 |
* @param value
|
471 |
*/
|
472 |
public void setHeightPx(int value) { |
473 |
heightPx = value; |
474 |
} |
475 |
|
476 |
/**
|
477 |
* Invocaci?n de los eventos de la ventana de <code>DefaultButtonsPanel</code>
|
478 |
*/
|
479 |
public void actionButtonPressed(ButtonsPanelEvent e) { |
480 |
// Bot?n de Aceptar
|
481 |
if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
|
482 |
accept(); |
483 |
close(); |
484 |
} |
485 |
|
486 |
// Bot?n de Aplicar
|
487 |
if (e.getButton() == ButtonsPanel.BUTTON_APPLY) {
|
488 |
accept(); |
489 |
getCuttingPanel().setNewLayerText(); |
490 |
} |
491 |
|
492 |
// Bot?n de Cerrar
|
493 |
if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
|
494 |
close(); |
495 |
} |
496 |
|
497 |
getCuttingPanel().getFLayer().getMapContext().invalidate(); |
498 |
} |
499 |
|
500 |
/**
|
501 |
* Cerrar la ventana del recorte
|
502 |
*/
|
503 |
private void close() { |
504 |
try {
|
505 |
if (getCuttingPanel().getLastTool() != null) |
506 |
getCuttingPanel().getMapControl().setTool(getCuttingPanel().getLastTool()); |
507 |
PluginServices.getMDIManager().closeWindow(getCuttingPanel().getCuttingDialog()); |
508 |
} catch (ArrayIndexOutOfBoundsException ex) { |
509 |
// Si la ventana no se puede eliminar no hacemos nada
|
510 |
} |
511 |
} |
512 |
|
513 |
/**
|
514 |
* Obtener el <code>CuttingPanel</code> asociado
|
515 |
* @return CuttingPanel
|
516 |
*/
|
517 |
private CuttingPanel getCuttingPanel() {
|
518 |
return cuttingPanel;
|
519 |
} |
520 |
|
521 |
/**
|
522 |
* Acciones realizadas cuando se acepta en el dialogo. Se obtendr?n los datos
|
523 |
* de recorte desde el dialogo, crearemos el objeto CuttingProcess que
|
524 |
* gestiona el recortado, ajustamos el tama?o del grid de salida y procesamos.
|
525 |
*/
|
526 |
private void accept() { |
527 |
// Controlamos las coordenadas del recorte que no se salgan de la imagen.
|
528 |
// De ser as? mostramos un error
|
529 |
CoordDataInputContainer values = getCuttingPanel().getCoor_reales(); |
530 |
double ulx = 0; |
531 |
double lrx = 0; |
532 |
double lry = 0; |
533 |
double uly = 0; |
534 |
try {
|
535 |
ulx = Double.parseDouble(values.getPDataInput11().getValue());
|
536 |
lry = Double.parseDouble(values.getPDataInput22().getValue());
|
537 |
lrx = Double.parseDouble(values.getPDataInput21().getValue());
|
538 |
uly = Double.parseDouble(values.getPDataInput12().getValue());
|
539 |
Rectangle2D ext = getCuttingPanel().getFLayer().getFullExtent();
|
540 |
if (ulx > ext.getMaxX() || lrx < ext.getMinX()
|
541 |
|| uly > ext.getMaxY() || lry < ext.getMinY()) { |
542 |
JOptionPane.showMessageDialog(
|
543 |
(Component) PluginServices.getMainFrame(), PluginServices.getText(
|
544 |
this, "coordenadas_erroneas")); |
545 |
return;
|
546 |
} |
547 |
} catch (NumberFormatException e) { |
548 |
JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), |
549 |
PluginServices.getText(this, "coordenadas_erroneas")); |
550 |
return;
|
551 |
} catch (ExpansionFileReadException e) {
|
552 |
NotificationManager.addError("Error dibujando", e);
|
553 |
} catch (ReadDriverException e) {
|
554 |
NotificationManager.addError("Error dibujando", e);
|
555 |
} |
556 |
|
557 |
// Obtenemos las coordenadas del recorte
|
558 |
CoordDataInputContainer sValues = getCuttingPanel().getCoor_pixel(); |
559 |
int[] dValues = new int[4]; |
560 |
try {
|
561 |
dValues[0] = (int) Math.round(Double.valueOf(sValues.getPDataInput11().getValue()).doubleValue()); |
562 |
dValues[1] = (int) Math.round(Double.valueOf(sValues.getPDataInput22().getValue()).doubleValue()); |
563 |
dValues[2] = (int) Math.round(Double.valueOf(sValues.getPDataInput21().getValue()).doubleValue()); |
564 |
dValues[3] = (int) Math.round(Double.valueOf(sValues.getPDataInput12().getValue()).doubleValue()); |
565 |
} catch (NumberFormatException exc) { |
566 |
// Los valores de las cajas son incorrectos. Terminamos la funci?n
|
567 |
return;
|
568 |
} |
569 |
|
570 |
// Seleccionamos las bandas que se usaran en el recorte a partir de la tabla
|
571 |
int countBands = 0; |
572 |
int rowCount = ((CheckBoxModel) getCuttingPanel().getTSelection().getModel()).getRowCount();
|
573 |
for (int iRow = 0; iRow < rowCount; iRow++) |
574 |
if ((((Boolean) ((CheckBoxModel) getCuttingPanel().getTSelection().getModel()).getValueAt(iRow, 0))).booleanValue()) |
575 |
countBands++; |
576 |
|
577 |
int[] drawableBands = new int[countBands]; |
578 |
int i = 0; |
579 |
for (int iRow = 0; iRow < rowCount; iRow++) { |
580 |
if ((((Boolean) ((CheckBoxModel) getCuttingPanel().getTSelection().getModel()).getValueAt(iRow, 0))).booleanValue()) |
581 |
drawableBands[i++] = iRow; |
582 |
} |
583 |
|
584 |
/**
|
585 |
* Donde se va a guardar el fichero
|
586 |
*/
|
587 |
String path = RasterModule.getPath();
|
588 |
if (getCuttingPanel().getCbSaveFile().isSelected()) {
|
589 |
path = RasterModule.getPath(); |
590 |
} else {
|
591 |
path = Utilities.createTempDirectory();
|
592 |
} |
593 |
|
594 |
String file = getCuttingPanel().getFilenameTextField().getText();
|
595 |
if (file == "") file = "cutlayer"; |
596 |
|
597 |
String filename = path + File.separator + file; |
598 |
|
599 |
/**
|
600 |
* Preparacion para la generacion del proceso del recorte
|
601 |
*/
|
602 |
MultiRasterDataset multiRasterDataset = (MultiRasterDataset)((FLyrRasterSE) getCuttingPanel().getFLayer()).getMultiRasterDataset(); |
603 |
|
604 |
if(multiRasterDataset == null) |
605 |
return;
|
606 |
|
607 |
WriterBufferServer dataWriter1 = new WriterBufferServer();
|
608 |
//TODO: FUNCIONALIDAD: Falta asignar la matriz de transformaci?n para que se salve con georreferenciaci?n.
|
609 |
|
610 |
AffineTransform transf = calcAffineTransform(ulx, uly, lrx, lry, getCuttingPanel().getWidthText(), getCuttingPanel().getHeightText());
|
611 |
|
612 |
int interpMethod = getCuttingPanel().getPResolution().getSelectedInterpolationMethod();
|
613 |
|
614 |
CuttingProcess cuttingProcess = new CuttingProcess( getCuttingPanel().getCbLoadLayerInToc().isSelected(),
|
615 |
getCuttingPanel().getViewName() , dValues, filename, |
616 |
dataWriter1, multiRasterDataset, transf, drawableBands, |
617 |
getCuttingPanel().getCbOneLyrPerBand().isSelected(), interpMethod); |
618 |
cuttingProcess.setResolution((int) getCuttingPanel().getWidthText(),
|
619 |
(int) getCuttingPanel().getHeightText());
|
620 |
IncrementableTask incrementableTask = new IncrementableTask(dataWriter1);
|
621 |
cuttingProcess.setIncrementableTask(incrementableTask); |
622 |
incrementableTask.showWindow(); |
623 |
cuttingProcess.start(); |
624 |
incrementableTask.start(); |
625 |
} |
626 |
|
627 |
/**
|
628 |
* CAlcula la matriz de transformaci?n que se usar? para el nuevo raster generado.
|
629 |
* @param ulx Coordenada X real de la esquina superior izquierda
|
630 |
* @param uly Coordenada Y real de la esquina superior izquierda
|
631 |
* @param lrx Coordenada X real de la esquina inferior derecha
|
632 |
* @param lry Coordenada Y real de la esquina inferior derecha
|
633 |
* @param width Ancho en p?xeles del nuevo raster
|
634 |
* @param height Alto en p?xeles del nuevo raster
|
635 |
* @return Matriz de transformaci?n para el nuevo raster
|
636 |
*/
|
637 |
private AffineTransform calcAffineTransform(double ulx, double uly, double lrx, double lry, double width, double height) { |
638 |
Point2D ul = new Point2D.Double(ulx, uly); |
639 |
Point2D lr = new Point2D.Double(lrx, lry); |
640 |
try {
|
641 |
at.inverseTransform(ul, ul); |
642 |
at.inverseTransform(lr, lr); |
643 |
} catch (NoninvertibleTransformException e) { |
644 |
JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), PluginServices.getText(this, "coordenadas_erroneas")); |
645 |
return new AffineTransform(); |
646 |
} |
647 |
double w = Math.abs(lr.getX() - ul.getX()); |
648 |
|
649 |
Point2D ur = new Point2D.Double(ul.getX() + w, ul.getY()); |
650 |
Point2D ll = new Point2D.Double(lr.getX() - w, lr.getY() ); |
651 |
|
652 |
//Obtenemos la georreferenciaci?n de las cuatro esquinas del nuevo raster
|
653 |
at.transform(ul, ul); |
654 |
at.transform(ur, ur); |
655 |
at.transform(lr, lr); |
656 |
at.transform(ll, ll); |
657 |
|
658 |
double pixelSizeX = (ur.getX() - ul.getX()) / width;
|
659 |
double pixelSizeY = (ll.getY() - ul.getY()) / height;
|
660 |
double rotX = at.getShearX();
|
661 |
double rotY = at.getShearY();
|
662 |
return new AffineTransform(pixelSizeX, rotY, rotX, pixelSizeY, ulx, uly); |
663 |
} |
664 |
|
665 |
/*
|
666 |
* (non-Javadoc)
|
667 |
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
|
668 |
*/
|
669 |
public void keyPressed(KeyEvent e) { |
670 |
} |
671 |
|
672 |
/*
|
673 |
* (non-Javadoc)
|
674 |
* @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
|
675 |
*/
|
676 |
public void keyTyped(KeyEvent e) { |
677 |
} |
678 |
|
679 |
/*
|
680 |
* (non-Javadoc)
|
681 |
* @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
|
682 |
*/
|
683 |
public void focusGained(FocusEvent e) { |
684 |
} |
685 |
|
686 |
/*
|
687 |
* (non-Javadoc)
|
688 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener#rectangle(com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent)
|
689 |
*/
|
690 |
public void rectangle(RectangleEvent event) throws BehaviorException { |
691 |
} |
692 |
|
693 |
/*
|
694 |
* (non-Javadoc)
|
695 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
696 |
*/
|
697 |
public boolean cancelDrawing() { |
698 |
return false; |
699 |
} |
700 |
|
701 |
/*
|
702 |
* (non-Javadoc)
|
703 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
704 |
*/
|
705 |
public Cursor getCursor() { |
706 |
return null; |
707 |
} |
708 |
|
709 |
/**
|
710 |
* @return the widthPx
|
711 |
*/
|
712 |
public int getWidthPx() { |
713 |
return widthPx;
|
714 |
} |
715 |
|
716 |
/**
|
717 |
* @return the heightPx
|
718 |
*/
|
719 |
public int getHeightPx() { |
720 |
return heightPx;
|
721 |
} |
722 |
} |