Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / gui / wizards / FileOpenRaster.java @ 17503

History | View | Annotate | Download (7.01 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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.raster.gui.wizards;
20

    
21
import java.awt.geom.Rectangle2D;
22
import java.io.File;
23
import java.io.FileReader;
24
import java.io.IOException;
25
import java.util.ArrayList;
26

    
27
import org.cresques.cts.IProjection;
28
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
29
import org.gvsig.raster.util.RasterToolsUtil;
30
import org.gvsig.raster.util.RasterUtilities;
31
import org.gvsig.rastertools.RasterModule;
32
import org.gvsig.rastertools.geolocation.ui.GeoLocationOpeningRasterDialog;
33
import org.gvsig.rastertools.raw.ui.main.OpenRawFileDefaultView;
34
import org.kxml2.io.KXmlParser;
35
import org.xmlpull.v1.XmlPullParser;
36
import org.xmlpull.v1.XmlPullParserException;
37

    
38
import com.iver.andami.PluginServices;
39
import com.iver.cit.gvsig.addlayer.fileopen.AbstractFileOpen;
40
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
41
import com.iver.cit.gvsig.fmap.MapControl;
42
/**
43
 * Clase que indicar? que ficheros puede tratar al panel de apertura de
44
 * ficheros
45
 *
46
 * @version 04/09/2007
47
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
48
 */
49
public class FileOpenRaster extends AbstractFileOpen {
50

    
51
        private ArrayList          lyrsRaster = new ArrayList();
52
        
53
        /**
54
         * Constructor de FileOpenRaster
55
         */
56
        public FileOpenRaster() {
57
                getFileFilter().add(new DriverFileFilter());
58
        }
59

    
60
        /**
61
         * Comprueba si un fichero VRT esta en correcto estado, en caso contrario
62
         * lanza una excepcion indicando el tipo de error en la apertura.
63
         *
64
         * @param file
65
         * @throws FileOpenVRTException
66
         */
67
        private void checkFileVRT(File file) throws FileOpenVRTException {
68
                KXmlParser parser = new KXmlParser();
69

    
70
                FileReader fileReader = null;
71
                try {
72
                        fileReader = new FileReader(file);
73
                        parser.setInput(fileReader);
74

    
75
                        parser.nextTag();
76

    
77
                        parser.require(XmlPullParser.START_TAG, null, "VRTDataset");
78

    
79
                        while (parser.nextTag () != XmlPullParser.END_TAG) {
80
                                parser.require(XmlPullParser.START_TAG, null, "VRTRasterBand");
81

    
82
                                String name;
83
                                while (parser.nextTag() != XmlPullParser.END_TAG) {
84
                                        parser.require(XmlPullParser.START_TAG, null, null);
85
                                        boolean relativePath = false;
86
                                        for (int i=0; i<parser.getAttributeCount(); i++) {
87
                                                if (parser.getAttributeName(i).equals("relativetoVRT") &&
88
                                                                parser.getAttributeValue(i).equals("1"))
89
                                                        relativePath = true;
90
                                        }
91
                                        name = parser.getName();
92
                                        String nameFile = parser.nextText();
93
                                        if (name.equals("SourceFilename")) {
94
                                                if (relativePath)
95
                                                        nameFile = file.getParent() + File.separator + nameFile;
96
                                                File tryFile = new File(nameFile);
97
                                                if (!tryFile.exists())
98
                                                        throw new FileOpenVRTException(PluginServices.getText(this, "no_existe_fichero") + " " + nameFile);
99
                                        }
100
                                        parser.require(XmlPullParser.END_TAG, null, name);
101
                                }
102

    
103
                                parser.require(XmlPullParser.END_TAG, null, "VRTRasterBand");
104
                        }
105
                        parser.require(XmlPullParser.END_TAG, null, "VRTDataset");
106
                        parser.next();
107
                        parser.require(XmlPullParser.END_DOCUMENT, null, null);
108
                } catch (XmlPullParserException e) {
109
                        throw new FileOpenVRTException(PluginServices.getText(this, "el_fichero")+ " " + file.getName().toString() + " " + PluginServices.getText(this, "esta_formato_desconocido"));
110
                } catch (IOException e) {
111
                        throw new FileOpenVRTException(PluginServices.getText(this, "no_puede_abrir_fichero") + " " + file.getName().toString());
112
                } finally {
113
                        if (fileReader != null)
114
                                try {
115
                                        fileReader.close();
116
                                } catch (IOException e) {
117
                                }
118
                }
119
        }
120

    
121
        /*
122
         * (non-Javadoc)
123
         * @see org.gvsig.raster.gui.wizards.IFileOpen#post(java.io.File[])
124
         */
125
        public File post(File file) {
126
                
127
                //Si el fichero es raw lanzamos el dialogo de par?metros de raw
128
                if (RasterUtilities.getExtensionFromFileName(file.getAbsolutePath()).equals("raw")) {
129
                        OpenRawFileDefaultView view = new OpenRawFileDefaultView(file.getAbsolutePath());
130

    
131
                         file = view.getImageFile();
132
                }
133

    
134
                //Si el fichero es vrt chequeamos que sea correcto
135
                if (RasterUtilities.getExtensionFromFileName(file.getAbsolutePath()).equals("vrt")) {
136
                        try {
137
                                checkFileVRT(file);
138
                        } catch (FileOpenVRTException e) {
139
                                RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_abrir_fichero") + " " + file.getName() + "\n\n" + PluginServices.getText(this, "informacion_adicional") + ":\n\n  " + e.getMessage(), this, e);
140
                                return null;
141
                        }
142
                }
143
                
144
                //Creamos la capa y comprobamos si tiene extent. Solo en el caso de que la opci?n est? activa en el cuadro
145
                //de preferencias
146
                if(RasterModule.askCoordinates) {
147
                        FLyrRasterSE lyrRaster = null;
148
                        try {
149
                                lyrRaster = FLyrRasterSE.createLayer(file.getName(), file, null);
150
                                if(        lyrRaster.getFullExtent().getMinX() == 0 && 
151
                                                lyrRaster.getFullExtent().getMinY() == 0 &&
152
                                                lyrRaster.getFullExtent().getMaxX() == ((FLyrRasterSE)lyrRaster).getPxWidth() &&
153
                                                lyrRaster.getFullExtent().getMaxY() == ((FLyrRasterSE)lyrRaster).getPxHeight()) {
154
                                        if(RasterToolsUtil.messageBoxYesOrNot(lyrRaster.getName() + "\n" + PluginServices.getText(this, "layer_without_georref"), null)) {
155
                                                GeoLocationOpeningRasterDialog gld = new GeoLocationOpeningRasterDialog(lyrRaster);
156
                                                PluginServices.getMDIManager().addWindow(gld);
157
                                        }
158
                                }
159
                                lyrsRaster.add(lyrRaster);
160
                        } catch (LoadLayerException e) {
161
                                RasterToolsUtil.messageBoxError("error_carga_capa", this, e);
162
                        }
163
                }
164
                                
165
                return super.post(file);
166
        }
167

    
168
        /*
169
         * (non-Javadoc)
170
         * @see org.gvsig.raster.gui.wizards.IFileOpen#execute(java.io.File[])
171
         */
172
        public Rectangle2D createLayer(File file, MapControl mapControl, String driverName, IProjection proj) {
173
                FLyrRasterSE lyr = null;
174
                String layerName = file.getName();
175
                
176
                //Si hay capas en la lista la buscamos all?
177
                if(lyrsRaster.size() != 0) {   
178
                        for (int i = 0; i < lyrsRaster.size(); i++) {
179
                                if(((FLyrRasterSE)lyrsRaster.get(i)).getName().equals(layerName)) {
180
                                        lyr = (FLyrRasterSE)lyrsRaster.get(i);
181
                                        lyr.setProjection(proj);
182
                                }
183
                        }
184
                }
185
                
186
                //Si no hay capa la cargamos
187
                if(lyr == null) {
188
                        try {
189
                                lyr = FLyrRasterSE.createLayer(layerName, file, proj);
190
                        } catch (LoadLayerException e) {
191
                                RasterToolsUtil.messageBoxError("error_carga_capa", this, e);
192
                        }
193
                }
194
                
195
                //La cargamos en la vista
196
                if (lyr != null) {
197
                        lyr.setVisible(true);
198
                        mapControl.getMapContext().getLayers().addLayer(lyr);
199
                        return lyr.getFullExtent();
200
                }
201
                
202
                return null;
203
        }
204
        
205
}