gvsig-raster / org.gvsig.raster.gdal / tags / pre-remove-jgdal / org.gvsig.raster.gdal / org.gvsig.raster.gdal.io / src / main / java / org / gvsig / jgdal / GdalDriver.java @ 3497
History | View | Annotate | Download (5.36 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.jgdal; |
23 |
|
24 |
import java.io.File; |
25 |
import java.util.StringTokenizer; |
26 |
|
27 |
import org.gdal.gdal.Dataset; |
28 |
import org.gdal.gdal.Driver; |
29 |
import org.gdal.gdal.gdal; |
30 |
/**
|
31 |
* Representa un driver de un tipo de im?gen
|
32 |
*
|
33 |
* @author Nacho Brodin (nachobrodin@gmail.com).<BR> Equipo de desarrollo gvSIG.<BR> http://www.gvsig.gva.es
|
34 |
* @version 0.0
|
35 |
* @link http://www.gvsig.gva.es
|
36 |
*/
|
37 |
public class GdalDriver extends Driver { |
38 |
// private native long createCopyNat(long cPtr, String file, long src, int bstrict);
|
39 |
// private native long createCopyParamsNat(long cPtr, String file, long src, int bstrict, Options opc);
|
40 |
// private native long createNat(long cPtr, String filename, int nXSize, int nYSize, int nBands, int nType, Options opc);
|
41 |
|
42 |
/**
|
43 |
* Constructor de Driver pasandole como par?metro la referencia al objeto
|
44 |
* GdalDriver en C
|
45 |
*
|
46 |
* @param cPtr direcci?n de memoria del objeto
|
47 |
*/
|
48 |
public GdalDriver(long cPtr) { |
49 |
super(cPtr, true); |
50 |
} |
51 |
|
52 |
/**
|
53 |
* Crea una copia de una im?gen a partir de un dataset de origen especificado.
|
54 |
* @param file Nombre del fichero sobre el cual se guardar? la copia
|
55 |
* @param src Dataset fuente a copiar
|
56 |
* @param bstrict TRUE si la copia debe ser estrictamente equivalente y FALSE indica que la copia puede
|
57 |
* adaptarse a las necesidades del formato de salida
|
58 |
* @return Gdal Dataset de la im?gen de salida
|
59 |
* @throws GdalException
|
60 |
*/
|
61 |
public Gdal createCopy(String file, Gdal src, boolean bstrict) throws GdalException { |
62 |
String path = file.substring(0, file.lastIndexOf(File.separator)); |
63 |
File f = new File(path); |
64 |
if (!f.canWrite())
|
65 |
throw new GdalException("Ruta de archivo incorrecta."); |
66 |
f = null;
|
67 |
|
68 |
if (src == null) |
69 |
throw new GdalException("El objeto Gdal es null"); |
70 |
|
71 |
// if (cPtr == 0)
|
72 |
// throw new GdalException("No se ha podido acceder al archivo.");
|
73 |
|
74 |
Dataset data = gdal.Open(file, bstrict ? 1 : 0); |
75 |
Dataset copyDataset = CreateCopy(file, data, bstrict ? 1 : 0); |
76 |
|
77 |
// if (ptr == 0)
|
78 |
// throw new GdalException("No se ha podido crear la copia");
|
79 |
|
80 |
return ((Gdal)copyDataset);
|
81 |
} |
82 |
|
83 |
|
84 |
/**
|
85 |
* A partir de las opciones en forma de vector de Strings pasadas por el usuario donde cada
|
86 |
* elemento del vector tiene la forma VARIABLE=VALOR crea el objeto Options para que sea accesible
|
87 |
* a las funciones JNI desde C.
|
88 |
* @param params Vector de strigs con las opciones
|
89 |
* @return Options Objeto de opciones
|
90 |
*/
|
91 |
private Options selectOptions(String[] params) { |
92 |
if (params == null) |
93 |
return null; |
94 |
|
95 |
Options opc = new Options(params.length);
|
96 |
StringTokenizer st;
|
97 |
for (int i = 0; i < params.length; i++) { |
98 |
st = new StringTokenizer(params[i], "="); |
99 |
String var = st.nextToken();
|
100 |
String dato = st.nextToken();
|
101 |
opc.addOption(var, dato); |
102 |
} |
103 |
return opc;
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* Crea una copia de una im?gen a partir de un dataset de origen especificado y unos par?metros dados.
|
108 |
* @param file Nombre del fichero sobre el cual se guardar? la copia
|
109 |
* @param src Dataset fuente a copiar
|
110 |
* @param bstrict TRUE si la copia debe ser estrictamente equivalente y FALSE indica que la copia puede
|
111 |
* adaptarse a las necesidades del formato de salida
|
112 |
* @param params Vector de strigs con las opciones de la copia
|
113 |
* @return Gdal Dataset de la im?gen de salida
|
114 |
* @throws GdalException
|
115 |
*/
|
116 |
public Gdal createCopy(String file, Gdal src, boolean bstrict, String[] params) throws GdalException { |
117 |
String path = file.substring(0, file.lastIndexOf(File.separator)); |
118 |
File f = new File(path); |
119 |
if (!f.canWrite())
|
120 |
throw new GdalException("Ruta de archivo incorrecta."); |
121 |
f = null;
|
122 |
|
123 |
// if (cPtr == 0)
|
124 |
// throw new GdalException("No se ha podido acceder al archivo.");
|
125 |
|
126 |
// if (src == null)
|
127 |
// throw new GdalException("El objeto Gdal es null");
|
128 |
|
129 |
Dataset data = gdal.Open(file, bstrict ? 1 : 0); |
130 |
Dataset copyDataset = CreateCopy(file, data, bstrict ? 1 : 0, params); |
131 |
|
132 |
if (copyDataset == null) |
133 |
throw new GdalException("No se ha podido crear la copia"); |
134 |
|
135 |
return (Gdal)copyDataset;
|
136 |
} |
137 |
|
138 |
|
139 |
/**
|
140 |
* Crea un nuevo dataset con el driver actual
|
141 |
*
|
142 |
* @param filename Nombre del dataset a crear
|
143 |
* @param nXSize Ancho en pixels
|
144 |
* @param nYSize Alto en pixels
|
145 |
* @param nBands N?mero de bandas
|
146 |
* @param nType Tipo de raster
|
147 |
* @param params lista de par?metros especificos del driver
|
148 |
*/
|
149 |
public Gdal create(String filename, int nXSize, int nYSize, int nBands, int nType, String[] params) throws GdalException { |
150 |
// if (cPtr == 0)
|
151 |
// throw new GdalException("No se ha podido acceder al archivo.");
|
152 |
|
153 |
Dataset data = Create(filename, nXSize, nYSize, nBands, nType, params); |
154 |
|
155 |
return (Gdal)data;
|
156 |
} |
157 |
} |