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