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