Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / georeferencing / Georeferencing.java @ 18277

History | View | Annotate | Download (11.4 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.georeferencing;
20

    
21
import java.awt.geom.Rectangle2D;
22
import java.awt.image.BufferedImage;
23
import java.util.ArrayList;
24

    
25
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
26
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
27
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
28
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
29
import org.gvsig.raster.util.RasterToolsUtil;
30
import org.gvsig.rastertools.georeferencing.ui.launcher.GeorefLauncherDialog;
31
import org.gvsig.rastertools.georeferencing.ui.table.GCPTablePanel;
32
import org.gvsig.rastertools.georeferencing.ui.zoom.InvalidRequestException;
33
import org.gvsig.rastertools.georeferencing.ui.zoom.ViewMapRequestManager;
34
import org.gvsig.rastertools.georeferencing.ui.zoom.ViewRasterRequestManager;
35
import org.gvsig.rastertools.georeferencing.ui.zoom.layers.ZoomCursorGraphicLayer;
36
import org.gvsig.rastertools.georeferencing.ui.zoom.tools.ToolEvent;
37
import org.gvsig.rastertools.georeferencing.ui.zoom.tools.ToolListener;
38
import org.gvsig.rastertools.georeferencing.view.ViewDialog;
39
import org.gvsig.rastertools.georeferencing.view.ZoomMapDialog;
40
import org.gvsig.rastertools.georeferencing.view.ZoomRasterDialog;
41

    
42
import com.iver.andami.PluginServices;
43
import com.iver.andami.ui.mdiFrame.MDIFrame;
44
import com.iver.andami.ui.mdiManager.IWindow;
45
import com.iver.andami.ui.mdiManager.WindowInfo;
46
import com.iver.cit.gvsig.ProjectExtension;
47
import com.iver.cit.gvsig.fmap.MapControl;
48
import com.iver.cit.gvsig.fmap.ViewPort;
49
import com.iver.cit.gvsig.project.Project;
50
import com.iver.cit.gvsig.project.documents.view.ProjectView;
51
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
52
import com.iver.cit.gvsig.project.documents.view.gui.IView;
53

    
54
/**
55
 * Clase principal para la georreferenciaci?n. Se encarga de la inicializaci?n
56
 * de la funcionalidad. Las acciones a realizar son:
57
 * <UL>
58
 * <LI>Lanzamiento del dialogo de inicializaci?n.</LI>
59
 * <LI>Carga de la capa raster a georreferenciar.</LI>
60
 * <LI>Creaci?n de las ventanas si no han sido creadas previamente.</LI>
61
 * <LI>Asignaci?n de tama?os y posiciones a las ventanas.</LI>
62
 * </UL>
63
 * 26/12/2007
64
 * @author Nacho Brodin (nachobrodin@gmail.com)
65
 */
66
public class Georeferencing implements ButtonsPanelListener, ToolListener {
67
        public static final int               UNDEFINED                      = -1;
68
        //Algoritmo
69
        public static final int               POLYNOMIAL                     = 0;
70
        public static final int               AFFINE                         = 1;
71
        
72
        //Tipo de georreferenciacion
73
        public static final int               WITH_MAP                       = 0;
74
        public static final int               WITHOUT_MAP                    = 1;
75
        
76
        private static int                    DEFAULT_DEGREE                 = 3;
77
        
78
        private GeorefLauncherDialog          initDialog                     = null;
79
        private FLyrRasterSE                  lyrToGeoref                    = null;
80
        private String                        selectedView                   = null;
81
        private static GCPTablePanel          table                          = null;
82
        
83
        private static final int              smallWindowsHeight             = 100;
84
        private static final double           smallWindowsWidthPercent       = 0.5;
85
        
86
        private static ViewDialog             viewRaster                     = null;
87
        private static ViewDialog             viewMap                        = null;
88
        
89
        private static ZoomMapDialog          zoomMap                        = null;
90
        private static ZoomRasterDialog       zoomPixel                      = null;
91
        
92
        private ViewRasterRequestManager      viewRasterRequestManager       = null;
93
        private ViewMapRequestManager         viewMapRequestManager          = null;
94
        
95
        private ViewRasterRequestManager      zoomRasterRequestManager       = null;
96
        private ViewMapRequestManager         zoomMapRequestManager          = null;
97
        
98
        public Georeferencing() {
99
                
100
        }
101
                
102
        public void initialize() {
103
                launcher();
104
        }
105
        
106
        /**
107
         * Colocaci?n inicial de las ventanas del espacio de trabajo
108
         */
109
        private void initWindows() {
110
                MDIFrame p = (MDIFrame)PluginServices.getMainFrame();
111
                int wViews = p.getWidth() >> 1;
112
                int hViews = p.getHeight() - 178 - smallWindowsHeight;
113
                int posYViews = p.getHeight() - 138 - smallWindowsHeight;
114
                int smallWindowsWidth = (int)(p.getWidth() * (smallWindowsWidthPercent * 0.5));
115
                int tableWidth = (int)(p.getWidth() * (1 - smallWindowsWidthPercent));
116
                
117
                //Si hay ventanas de georreferenciaci?n abiertas las cerramos
118
                if(zoomMap != null)
119
                        PluginServices.getMDIManager().closeWindow(zoomMap);
120
                if(zoomPixel != null)
121
                        PluginServices.getMDIManager().closeWindow(zoomPixel);
122
                if(viewRaster != null)
123
                        PluginServices.getMDIManager().closeWindow(viewRaster);
124
                if(viewRaster != null)
125
                        PluginServices.getMDIManager().closeWindow(viewMap);
126
                if(table != null)
127
                        PluginServices.getMDIManager().closeWindow(table);
128
                
129
                MapControl mapControl = null;
130
                IWindow[] windowList = PluginServices.getMDIManager().getAllWindows();
131
                BufferedImage imgView = null;
132
                ViewPort vp = null;
133
                for (int i = 0; i < windowList.length; i++) {
134
                        if(windowList[i] instanceof IView && windowList[i].getWindowInfo().getTitle().endsWith(": " + selectedView)) {
135
                                WindowInfo info = windowList[i].getWindowInfo();
136
                                
137
                                info.setX(0);
138
                                info.setY(0);
139
                                info.setWidth(wViews);
140
                                info.setHeight(posYViews);
141
                                
142
                                mapControl = ((IView)windowList[i]).getMapControl();
143
                                imgView = ((IView)windowList[i]).getMapControl().getImage();
144
                                vp = ((IView)windowList[i]).getMapControl().getViewPort();
145
                        }
146
                }
147
                
148
                table = new GCPTablePanel(smallWindowsWidth, posYViews, tableWidth, smallWindowsHeight);
149
                
150
                //Inicializaci?n de la vista mapa
151
                viewMap = new ViewDialog(0, 0, wViews, hViews, this);
152
                viewMap.setShowInfo(true);
153
                viewMapRequestManager = new ViewMapRequestManager(viewMap, mapControl);
154
                viewMap.setExtensionRequest(viewMapRequestManager);
155
                
156
                //Inicializaci?n de la vista raster
157
                viewRaster = new ViewDialog(wViews, 0, wViews, hViews, this);
158
                viewRaster.setShowInfo(true);
159
                viewRasterRequestManager = new ViewRasterRequestManager(viewRaster, lyrToGeoref);
160
                viewRaster.setExtensionRequest(viewRasterRequestManager);
161
                
162
                //Inicializaci?n del zoom de la vista raster
163
                zoomPixel = new ZoomRasterDialog(smallWindowsWidth + tableWidth, posYViews, smallWindowsWidth, smallWindowsHeight);
164
                zoomRasterRequestManager = new ViewRasterRequestManager(zoomPixel, lyrToGeoref);
165
                zoomPixel.setExtensionRequest(zoomRasterRequestManager);
166
                
167
                //Inicializaci?n del zoom de la vista de referencia
168
                zoomMap = new ZoomMapDialog(0, posYViews, smallWindowsWidth, smallWindowsHeight);
169
                zoomMapRequestManager = new ViewMapRequestManager(zoomMap, mapControl);
170
                zoomMap.setExtensionRequest(zoomMapRequestManager);
171
                
172
                /*GeoPoint gp = new GeoPoint(new Point2D.Double(100, 100), null);
173
                GPGraphic gpg = new GPGraphic(GPGraphic.PIXEL, gp);
174
                view.getPixelGraphicLayer().addGeoPoint(gpg);
175
                */
176
                
177
                //A?adimos las ventanas a gvSIG
178
                PluginServices.getMDIManager().addWindow(viewRaster);
179
                PluginServices.getMDIManager().addWindow(viewMap);
180
                PluginServices.getMDIManager().addWindow(table);
181
                PluginServices.getMDIManager().addWindow(zoomMap);
182
                PluginServices.getMDIManager().addWindow(zoomPixel);
183
                
184
                try {
185
                        //Inicializamos la vista de mapa
186
                        viewMapRequestManager.initRequest(mapControl.getMapContext().getLayers().getFullExtent());
187
                        viewMap.setZoomCursorSize(zoomMap.getCanvasWidth() / 3, zoomMap.getCanvasHeight() / 3);
188

    
189
                        //Inicializamos la vista de raster
190
                        viewRasterRequestManager.initRequest(lyrToGeoref.getFullExtent());
191
                        viewRaster.setZoomCursorSize(zoomPixel.getCanvasWidth() / 3, zoomPixel.getCanvasHeight() / 3);
192
                        
193
                        //Inicializamos el zoom de mapa
194
                        Rectangle2D mapExtent = mapControl.getMapContext().getFullExtent();
195
                        Rectangle2D ext = viewMap.getCursorAdjustedWorldCoordinates(zoomMap.getCanvasWidth(), zoomMap.getCanvasHeight());
196
                        zoomMapRequestManager.initRequest(mapControl.getMapContext().getFullExtent());
197
                        zoomMapRequestManager.request(new Rectangle2D.Double(ext.getX(), mapExtent.getMaxY() - (mapExtent.getMinY() - ext.getY()), ext.getWidth(), ext.getHeight()));
198

    
199
                        //Inicializamos el zoom de raster
200
                        ext = viewRaster.getCursorAdjustedWorldCoordinates(zoomPixel.getCanvasWidth(), zoomPixel.getCanvasHeight());
201
                        zoomRasterRequestManager.initRequest(lyrToGeoref.getFullExtent());
202
                        zoomRasterRequestManager.request(new Rectangle2D.Double(ext.getX(), lyrToGeoref.getFullRasterExtent().maxY() - (lyrToGeoref.getFullRasterExtent().minY() - ext.getY()), ext.getWidth(), ext.getHeight()));
203
                } catch (InvalidRequestException e1) {
204
                        RasterToolsUtil.messageBoxError("error_setview_preview", this);
205
                }catch (Exception e1) {
206
                        RasterToolsUtil.messageBoxError("error_setview_preview", this);
207
                }
208
                
209
        }
210
        
211
        /**
212
         * Lanzador del cuadro de inicializaci?n de propiedades de georreferenciaci?n.
213
         */
214
        private void launcher() {
215
                Project p = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
216
                ArrayList docs = p.getDocumentsByType(ProjectViewFactory.registerName);
217
                String[] viewList = new String[docs.size()];
218
                for (int i = 0; i < docs.size(); i++) 
219
                        viewList[i] = (((ProjectView)docs.get(i)).getName());
220
                initDialog = new GeorefLauncherDialog(viewList, DEFAULT_DEGREE, this);
221
                PluginServices.getMDIManager().addWindow(initDialog);
222
        }
223
        
224
        /**
225
         * Captura de eventos de los botones Aceptar y Cancelar del cuadro
226
         * inicial con las propiedades de georreferenciaci?n.
227
         */
228
        public void actionButtonPressed(ButtonsPanelEvent e) {
229
                // Bot?n de Aceptar
230
                if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
231
                        if(initDialog != null) {
232
                                selectedView = initDialog.getSelectedView();
233
                                lyrToGeoref = initDialog.getLayer();
234
                                if(lyrToGeoref == null) 
235
                                        RasterToolsUtil.messageBoxError("layer_not_loaded", this);
236
                                else {
237
                                        PluginServices.getMDIManager().closeWindow(initDialog);
238
                                        initWindows();
239
                                }
240
                        }
241
                }
242
                // Bot?n de Cerrar
243
                if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
244
                        if(initDialog != null)
245
                                PluginServices.getMDIManager().closeWindow(initDialog);
246
                }
247
        }
248

    
249
        /**
250
         * Evento de finalizaci?n de la capa con el 
251
         * cursor gr?fico que controla el ?rea de la miniimagen asociada
252
         */
253
        public void endAction(ToolEvent ev) {
254
                if(ev.getSource() instanceof ZoomCursorGraphicLayer) {
255
                        try {                                
256
                                Rectangle2D ext = viewRaster.getCursorAdjustedWorldCoordinates(zoomPixel.getCanvasWidth(), zoomPixel.getCanvasHeight());
257
                                zoomRasterRequestManager.request(ext);
258
                                ext = viewMap.getCursorAdjustedWorldCoordinates(zoomMap.getCanvasWidth(), zoomMap.getCanvasHeight());
259
                                zoomMapRequestManager.request(ext);
260
                        } catch (InvalidRequestException e1) {
261
                                RasterToolsUtil.messageBoxError("error_setview_preview", this);
262
                        }
263
                }
264
        }
265
}