Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / reproject / Reproject.java @ 30541

History | View | Annotate | Download (5.61 KB)

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

    
21
import java.io.File;
22

    
23
import org.cresques.cts.IProjection;
24
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
25
import org.gvsig.raster.dataset.GeoRasterWriter;
26
import org.gvsig.raster.dataset.NotSupportedExtensionException;
27
import org.gvsig.raster.dataset.io.RasterDriverException;
28

    
29
import es.gva.cit.jgdal.GdalWarp;
30
/**
31
 * Clase encargada de la reproyecci?n. Se le asigna una capa raster y la ruta de
32
 * destino
33
 * 
34
 * @version 30/04/2008
35
 * @author Nacho Brodin nachobrodin@gmail.com
36
 */
37
public class Reproject {
38
        private FLyrRasterSE lyr      = null;
39
        private String       pathDest = null;
40
        private GdalWarp     warper   = null;
41

    
42
        /**
43
         * Constructor de la clase.
44
         * @param lyr
45
         * @param pathDest Ruta de destino
46
         */
47
        public Reproject(FLyrRasterSE lyr, String pathDest) {
48
                this.lyr = lyr;
49
                this.pathDest = pathDest;
50
        }
51
        
52
        /**
53
         * M?todo para la transformaci?n del raster.
54
         * @param proj Proyecci?n destino
55
         */
56
        public int warp(IProjection proj, IProjection sourceSrs) throws ReprojectException {
57
                if (lyr == null)
58
                        throw new ReprojectException("Capa no valida.");
59
                if (!lyr.isReproyectable())
60
                        throw new ReprojectException("Esta capa no se puede reproyectar.");
61
                if (proj == null || proj.getAbrev() == null)
62
                        throw new ReprojectException("Proyecci?n de destino no valida.");
63

    
64
                String epsg = proj.getAbrev();
65
                String s_srs = null;
66
                if (sourceSrs != null) {
67
                        s_srs = sourceSrs.getAbrev();
68
                }
69
                if (sourceSrs == null) {
70
                        if (lyr.getProjection() != null) {
71
                                s_srs = lyr.getProjection().getAbrev();
72
                        } else {
73
                                throw new ReprojectException("No se encuentra la proyeccion de origen.");
74
                        }
75
                }
76
                String source = null;
77
                if (lyr.getFile() != null)
78
                        source = lyr.getFile().getPath();
79

    
80
                if (source == null)
81
                        throw new ReprojectException("Fichero fuente no valido.");
82
                
83
                if (source.endsWith(".hdr"))
84
                        source = source.substring(0, source.lastIndexOf(".hdr"));
85

    
86
                File file = new File(pathDest);
87
                if (!file.getParentFile().canWrite())
88
                        throw new ReprojectException("Ruta de destino no valida.");
89

    
90
                warper = new GdalWarp();
91

    
92
                try {
93
                        warper.setSsrs(s_srs);
94
                        
95
//                        GTiff: GeoTIFF
96
//                        GTiff_FORMAT = 1;     // Reproyecta .tif
97
//                        VRT: Virtual Raster
98
//                        VRT_FORMAT = 2;       // No reproyecta
99
//                        NITF: National Imagery Transmission Format
100
//                        NITF_FORMAT = 3;      // Reproyecta .nitf
101
//                        HFA: Erdas Imagine Images (.img)
102
//                        HFA_FORMAT = 4;       // Reproyecta .img
103
//                        ELAS: ELAS
104
//                        ELAS_FORMAT = 5;      // No reproyecta
105
//                        MEM: In Memory Raster
106
//                        MEM_FORMAT = 6;       // No usarlo, aunque reproyecta
107
//                        BMP: MS Windows Device Independent Bitmap
108
//                        BMP_FORMAT = 7;       // No reproyecta
109
//                        PCIDSK: PCIDSK Database File
110
//                        PCIDSK_FORMAT = 8;    // Reproyecta
111
//                        ILWIS: ILWIS Raster Map
112
//                        ILWIS_FORMAT = 9;     // No reproyecta
113

    
114
//                        HDF4_FORMAT = 10;     // No reproyecta y se cierra gvSIG
115

    
116
//                        PNM: Portable Pixmap Format (netpbm)
117
//                        PNM_FORMAT = 11;      // No reproyecta
118
//                        ENVI: ENVI .hdr Labelled
119
//                        ENVI_FORMAT = 12;     // Reproyecta
120
//                        EHdr: ESRI .hdr Labelled
121
//                        EHDR_FORMAT = 13;     // Reproyecta
122
//                        PAux: PCI .aux Labelled
123
//                        PAUX_FORMAT = 14;     // Reproyecta
124
//                        MFF: Vexcel MFF Raster
125
//                        MFF_FORMAT = 15;      // No reproyecta
126
//                        MFF2: Vexcel MFF2 (HKV) Raster
127
//                        MFF2_FORMAT = 16;     // No reproyecta
128
//                        BT: VTP .bt (Binary Terrain) 1.3 Format
129
//                        BT_FORMAT = 17;       // No reproyecta
130
//                        IDA: Image Data and Analysis
131
//                        IDA_FORMAT = 18;      // No reproyecta
132
//                        RMF: Raster Matrix Format
133
//                        RMF_FORMAT = 19;      // No reproyecta
134
//                        RST: Idrisi Raster A.1
135
//                        RST_FORMAT = 20;      // No reproyecta
136

    
137
//                        Leveller: Leveller heightfield
138
//                        LEVELLER_FORMAT = 21; // No reproyecta
139
//                        Terragen: Terragen heightfield
140
//                        TERRAGEN_FORMAT = 22; // No reproyecta
141
//                        ERS: ERMapper .ers Labelled
142
//                        ERS_FORMAT = 23;      // No reproyecta
143
//                        INGR: Intergraph Raster
144
//                        INGR_FORMAT = 24;     // No reproyecta
145
//                        GSAG: Golden Software ASCII Grid (.grd)
146
//                        GSAG_FORMAT = 25;     // No reproyecta
147
//                        GSBG: Golden Software Binary Grid (.grd)
148
//                        GSBG_FORMAT = 26;     // No reproyecta
149
//                        ADRG: ARC Digitized Raster Graphics
150
//                        ADRG_FORMAT = 27;     // No reproyecta
151
                        
152
//                        warper.setFormat(27);
153
                        return warper.warp(epsg, source, pathDest, GeoRasterWriter.getWriter(pathDest).getDriverName());
154
                } catch (NotSupportedExtensionException e) {
155
                        throw new ReprojectException("error_reprojecting");
156
                } catch (RasterDriverException e) {
157
                        throw new ReprojectException("error_reprojecting");
158
                } catch (Exception e) {
159
                        throw new ReprojectException("transformation_not_possible");
160
                }
161
        }
162

    
163
        /**
164
         * Obtiene el porcentaje de progreso de la tarea de reproyecci?n para
165
         * mostrarlo por pantalla.
166
         * @return
167
         */
168
        public int getPercent() {
169
                if (warper != null)
170
                        return warper.getPercent();
171
                return 0;
172
        }
173
}