Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / geolocation / listener / GeoLocationPanelListener.java @ 16554

History | View | Annotate | Download (9.5 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.geolocation.listener;
20

    
21
import java.awt.event.ActionEvent;
22
import java.awt.event.ActionListener;
23
import java.awt.geom.AffineTransform;
24
import java.awt.geom.Rectangle2D;
25
import java.io.BufferedReader;
26
import java.io.FileInputStream;
27
import java.io.FileNotFoundException;
28
import java.io.IOException;
29
import java.io.InputStreamReader;
30
import java.util.EventObject;
31

    
32
import javax.swing.JFileChooser;
33

    
34
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
35
import org.gvsig.gui.beans.datainput.DataInputContainerListener;
36
import org.gvsig.raster.datastruct.Extent;
37
import org.gvsig.raster.util.ExtendedFileFilter;
38
import org.gvsig.raster.util.Historical;
39
import org.gvsig.raster.util.RasterToolsUtil;
40
import org.gvsig.rastertools.geolocation.ui.GeoLocationPanel;
41

    
42
import com.iver.andami.PluginServices;
43
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard;
44
import com.iver.cit.gvsig.fmap.ViewPort;
45

    
46
/**
47
 * Listener para el panel de geolocalizaci?n.
48
 * @version 31/07/2007
49
 * @author Nacho Brodin (nachobrodin@gmail.com)
50
 *
51
 */
52
public class GeoLocationPanelListener implements ActionListener, DataInputContainerListener {
53

    
54
        private GeoLocationPanel        panel = null;
55
        private boolean             enableValueChangeEvent = false;
56

    
57
        /**
58
         * Crea un nuevo <code>GeoLocationPanelListener</code>
59
         * @param panel
60
         */
61
        public GeoLocationPanelListener(GeoLocationPanel panel) {
62
                this.panel = panel;
63
        }
64

    
65
        /**
66
         * M?todo que se invoca cuando se disparan los eventos de los botones
67
         */
68
        public void actionPerformed(ActionEvent e) {
69
                if(e.getSource() == panel.getSaveButton()) {
70
                        if(RasterToolsUtil.messageBoxYesOrNot(PluginServices.getText(this,"aviso_write_transform"), panel)) {
71
                                try {
72
                                        panel.getLayer().saveGeoToRmf();
73
                                        panel.activeButtons();
74
                                } catch (IOException e1) {
75
                                        RasterToolsUtil.messageBoxError(PluginServices.getText(this,"error_salvando_rmf"), panel);
76
                                }
77
                                panel.setModify(false);
78
                        }
79
                        return;
80
                }
81

    
82
                //Asignamos la georreferenciaci?n que hay en ese momento en el dialogo
83
                if(e.getSource() == panel.getApplyButton()) {
84
                        try {
85
                                double ulx = Double.parseDouble(panel.getUlx().getValue());
86
                                double uly = Double.parseDouble(panel.getUly().getValue());
87
                                double psx = Double.parseDouble(panel.getPsx().getValue());
88
                                double psy = Double.parseDouble(panel.getPsy().getValue());
89
                                double rotx = Double.parseDouble(panel.getRotx().getValue());
90
                                double roty = Double.parseDouble(panel.getRoty().getValue());
91
                                AffineTransform at = new AffineTransform(psx, roty, rotx, psy, ulx, uly);
92
                                panel.getLayer().setAffineTransform(at);
93
                                panel.getMapCtrl().getMapContext().invalidate();
94
                                return;
95
                        } catch(NumberFormatException ex) {
96
                                RasterToolsUtil.messageBoxError(PluginServices.getText(this,"error_transformacion"), panel);
97
                                return;
98
                        }
99
                }
100

    
101
                Historical hist = panel.getHistorical();
102
                if(hist == null)
103
                        return;
104

    
105
                AffineTransform at = null;
106

    
107
                //Cargamos la primera transformaci?n
108
                if(e.getSource() == panel.getFirstButton())
109
                        at = (AffineTransform)hist.getFirst();
110

    
111
                //Cargamos la transformaci?n anterior
112
                if(e.getSource() == panel.getBackButton())
113
                        at = (AffineTransform)hist.getBack();
114

    
115
                ////Cargamos la transformaci?n siguiente
116
                if(e.getSource() == panel.getNextButton())
117
                        at = (AffineTransform)hist.getNext();
118

    
119
                //Cargamos la georreferenciaci?n del raster
120
                if(e.getSource() == panel.getResetButton())
121
                        at = panel.getLayer().getDataSource().getOwnAffineTransform();
122

    
123
                //Cargar la georreferenciaci?n desde tfw
124
                if(e.getSource() == panel.getTfwLoad()) {
125
                        JFileChooser chooser = new JFileChooser(FileOpenWizard.getLastPath());
126
                        chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
127
                        chooser.addChoosableFileFilter(new ExtendedFileFilter("tfw"));
128
                        chooser.addChoosableFileFilter(new ExtendedFileFilter("wld"));
129

    
130
                        ExtendedFileFilter allFilters = new ExtendedFileFilter();
131
                        allFilters.addExtension("tfw");
132
                        allFilters.addExtension("wld");
133
                        allFilters.setDescription(PluginServices.getText(this, "todos_soportados"));
134
                        chooser.addChoosableFileFilter(allFilters);
135
                        chooser.setFileFilter(allFilters);
136

    
137
                        int returnVal = chooser.showOpenDialog(panel);
138
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
139
                                String fName = chooser.getSelectedFile().toString();
140
                                at = readTfw(fName);
141
                        }
142
                }
143

    
144
                //Centrar el raster en la vista
145
                if(e.getSource() == panel.getCenterToView()) {
146
                        Extent extentView = calcCenterExtent(panel.getViewPort());
147
                        double x = extentView.minX();
148
                        double y = extentView.maxY();
149
                        double psX = extentView.width() / ((FLyrRasterSE)panel.getLayer()).getPxWidth();
150
                        double psY = -(extentView.height() / ((FLyrRasterSE)panel.getLayer()).getPxHeight());
151
                        at = new AffineTransform(psX, 0, 0, psY, x, y);
152
                        panel.setModify(true);
153
                }
154

    
155
                //Entrar? en el caso de que se haya seleccionado alguna transformaci?n
156
                if(at != null) {
157
                        panel.getLayer().setAT(at);
158
                        panel.loadTransform(at);
159
                        panel.activeButtons();
160
                        panel.getMapCtrl().getMapContext().invalidate();
161
                }
162
        }
163

    
164
        /**
165
         * Control del cambio de valor dentro de las cajas de texto. Cuando esto ocurre tiene
166
         * el mismo efecto que el bot?n "Aplicar"
167
         * @param e EventObject
168
         */
169
        public void actionValueChanged(EventObject e) {
170
                if(isEnableValueChangeEvent()) {
171
                        ActionEvent ev = new ActionEvent(panel.getApplyButton(), 0, null);
172
                        actionPerformed(ev);
173
                }
174
        }
175

    
176
        /**
177
         * Obtiene el valor de la variable que informa sobre la activaci?n y desactivaci?n del evento de cambio de valor
178
         * dentro de un campo de texto
179
         * @return enableValueChangeEvent
180
         */
181
        public boolean isEnableValueChangeEvent() {
182
                return enableValueChangeEvent;
183
        }
184

    
185
        /**
186
         * Asigna el valor para la activaci?n y desactivaci?n del evento de cambio de valor
187
         * dentro de un campo de texto
188
         * @param enableValueChangeEvent
189
         */
190
        public void setEnableValueChangeEvent(boolean enableValueChangeEvent) {
191
                this.enableValueChangeEvent = enableValueChangeEvent;
192
        }
193

    
194
        /**
195
         * Centra el raster asociado a la capa en al extent del viewport pasado
196
         * por par?metro.
197
         * @param vp ViewPort
198
         * @return        Extent para la imagen
199
         */
200
        private Extent calcCenterExtent(ViewPort vp) {
201
                Extent tempExtent = null;
202
                double widthPxImg, heightPxImg;
203

    
204
                widthPxImg = ((FLyrRasterSE)panel.getLayer()).getPxWidth();
205
                heightPxImg = ((FLyrRasterSE)panel.getLayer()).getPxHeight();
206

    
207
                if(vp == null || vp.getAdjustedExtent() == null) {
208
                        vp = new ViewPort(null);
209
                        Rectangle2D r2d = new Rectangle2D.Double(0, 0, widthPxImg, heightPxImg);
210
                        vp.setExtent(r2d);
211
                        tempExtent = new Extent(0, 0, widthPxImg, heightPxImg);
212
                } else
213
                        tempExtent = new Extent(vp.getAdjustedExtent());
214

    
215
                double ulX = 0D, ulY = 0D, lrX = 0D, lrY = 0D;
216
                if(widthPxImg > heightPxImg) {
217
                        double widthView = tempExtent.maxX() - tempExtent.minX();
218
                        ulX = tempExtent.minX() + (widthView / 4);
219
                        lrX = ulX + (widthView / 2);
220
                        double newAlto = ((heightPxImg * (widthView / 2)) / widthPxImg);
221
                        double centroY = tempExtent.minY()+((tempExtent.maxY() - tempExtent.minY())/2);
222
                        ulY = centroY - (newAlto / 2);
223
                        lrY = centroY + (newAlto / 2);
224
                } else {
225
                        double heightView = tempExtent.maxY() - tempExtent.minY();
226
                        ulY = tempExtent.minY() + (heightView / 4);
227
                        lrY = ulY + (heightView / 2);
228
                        double newAncho = ((widthPxImg * (heightView / 2)) / heightPxImg);
229
                        double centroX = tempExtent.minX()+((tempExtent.maxX() - tempExtent.minX())/2);
230
                        ulX = centroX - (newAncho / 2);
231
                        lrX = centroX + (newAncho / 2);
232
                }
233
                return new Extent(ulX, ulY, lrX, lrY);
234
        }
235

    
236
        /**
237
         * Lee las coordenadas de un fichero de tfw con una transformaci?n y
238
         * devuelve la clase AffineTransform con dicha transformaci?n. Esta llamada gestiona los
239
         * errores producidos actuando en consecuencia. Muestra los mensajes al usuario y retorna
240
         * null en caso de tener problemas.
241
         * @param fName Nombre del fichero tfw
242
         * @return AffineTransform
243
         */
244
        private AffineTransform readTfw(String fName) {
245
                BufferedReader inGrf = null;
246
                double[] result = new double[6];
247
                try {
248
                        inGrf = new BufferedReader(new InputStreamReader(new FileInputStream(fName)));
249
                        String str = inGrf.readLine();
250
                        int count = 0;
251
                        while(str != null && count < 6) {
252
                                try {
253
                                        Double value =  new Double(str);
254
                                        result[count] = value.doubleValue();
255
                                } catch (NumberFormatException ex) {
256
                                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_file_not_valid"), panel, ex);
257
                                        return null;
258
                                }
259
                                str = inGrf.readLine();
260
                                count ++;
261
                        }
262
                } catch (FileNotFoundException e) {
263
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_file_not_found"), panel, e);
264
                        return null;
265
                } catch (IOException ex) {
266
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_lectura"), panel, ex);
267
                        return null;
268
                }
269
                return new AffineTransform(result[0], result[1], result[2], result[3], result[4], result[5]);
270
        }
271
}