svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / RasterLibrary.java @ 18322
History | View | Annotate | Download (10.8 KB)
1 | 11526 | bsanchez | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2006 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 | 10960 | nacho | package org.gvsig.raster; |
20 | |||
21 | import java.io.File; |
||
22 | 14134 | nbrodin | import java.io.FileFilter; |
23 | import java.io.IOException; |
||
24 | import java.net.MalformedURLException; |
||
25 | import java.net.URL; |
||
26 | 10960 | nacho | import java.util.ArrayList; |
27 | 14134 | nbrodin | import java.util.Enumeration; |
28 | import java.util.Hashtable; |
||
29 | import java.util.jar.JarException; |
||
30 | import java.util.zip.ZipEntry; |
||
31 | import java.util.zip.ZipException; |
||
32 | import java.util.zip.ZipFile; |
||
33 | 10960 | nacho | |
34 | 14134 | nbrodin | import org.apache.log4j.Logger; |
35 | 12712 | nacho | import org.gvsig.raster.dataset.io.ErmapperDriver; |
36 | 11505 | nacho | import org.gvsig.raster.dataset.io.ErmapperWriter; |
37 | 10960 | nacho | import org.gvsig.raster.dataset.io.GdalDriver; |
38 | import org.gvsig.raster.dataset.io.GdalWriter; |
||
39 | 12470 | nacho | import org.gvsig.raster.dataset.io.JpegWriter; |
40 | 11074 | nacho | import org.gvsig.raster.dataset.io.MemoryRasterDriver; |
41 | 11866 | nacho | import org.gvsig.raster.dataset.io.MrSidDriver; |
42 | 17514 | nbrodin | import org.gvsig.raster.dataset.io.PngWriter; |
43 | 17051 | nbrodin | import org.gvsig.raster.grid.filter.bands.ColorBalanceCMYManager; |
44 | import org.gvsig.raster.grid.filter.bands.ColorBalanceRGBManager; |
||
45 | 12218 | nacho | import org.gvsig.raster.grid.filter.bands.ColorTableListManager; |
46 | 15970 | nbrodin | import org.gvsig.raster.grid.filter.bands.HSLToRGBManager; |
47 | 17100 | bsanchez | import org.gvsig.raster.grid.filter.bands.NoDataListManager; |
48 | 15970 | nbrodin | import org.gvsig.raster.grid.filter.bands.RGBToHSLManager; |
49 | 17148 | nbrodin | import org.gvsig.raster.grid.filter.bands.ToLumSaManager; |
50 | 12095 | dguerrero | import org.gvsig.raster.grid.filter.convolution.ConvolutionListManager; |
51 | 12000 | bsanchez | import org.gvsig.raster.grid.filter.correction.MedianListManager; |
52 | 11526 | bsanchez | import org.gvsig.raster.grid.filter.enhancement.BrightnessContrastListManager; |
53 | import org.gvsig.raster.grid.filter.enhancement.EnhancementListManager; |
||
54 | 13385 | amunoz | import org.gvsig.raster.grid.filter.pansharp.PanSharpeningListManager; |
55 | 11986 | bsanchez | import org.gvsig.raster.grid.filter.segmentation.FirstDerivativeListManager; |
56 | 12333 | bsanchez | import org.gvsig.raster.grid.filter.statistics.StatisticsListManager; |
57 | 11237 | nacho | import org.gvsig.raster.util.extensionPoints.ExtensionPoint; |
58 | import org.gvsig.raster.util.extensionPoints.ExtensionPoints; |
||
59 | import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton; |
||
60 | 10960 | nacho | /**
|
61 | 11351 | nacho | * Clase principal de la libreria. En ella se definen variables globales con informaci?n
|
62 | * de uso general, as? como acciones a realizar al arracar la librer?a. El m?todo que
|
||
63 | * contiene las acciones de arranque es wakeUp. Las tareas principales de este m?todo
|
||
64 | 12333 | bsanchez | * son de registro de drivers de lectura y escritura y eliminaci?n del directorio de
|
65 | 11351 | nacho | * temporales.
|
66 | 12333 | bsanchez | *
|
67 | 10960 | nacho | * @author Nacho Brodin (nachobrodin@gmail.com)
|
68 | */
|
||
69 | public class RasterLibrary { |
||
70 | 17353 | nbrodin | /**
|
71 | * Control de librer?a ya inicializada.
|
||
72 | */
|
||
73 | public static boolean wakeup = false; |
||
74 | 16637 | nbrodin | public static ArrayList writersClassList = new ArrayList(); |
75 | 11986 | bsanchez | |
76 | 10960 | nacho | /**
|
77 | * En la generaci?n autom?tica de clases esta variable representa el n?mero de
|
||
78 | * clases en las que se hace la divisi?n.
|
||
79 | */
|
||
80 | 16637 | nbrodin | public static int defaultNumberOfClasses = 64; |
81 | 12504 | bsanchez | |
82 | 11207 | nacho | /**
|
83 | 12504 | bsanchez | * En la genraci?n de las paletas de color, esta variable representa el n?mero
|
84 | * de colores en las que se hace la divisi?n para generar la paleta nueva.
|
||
85 | * Con esto conseguimos velocidad de procesamiento. Cuanto menor sea, peor
|
||
86 | * ser? la calidad representada de la imagen.
|
||
87 | */
|
||
88 | 16637 | nbrodin | public static int defaultNumberOfColors = 256; |
89 | 12504 | bsanchez | /**
|
90 | 12333 | bsanchez | * Tama?o de bloque en los procesos que recorren un raster completo a base de ventanas con recorrido
|
91 | * descendente. Esta variable indica la altura de dicho bloque. Por lo tanto cada bloque ser? de
|
||
92 | * raster.width X blockHeight. Tipicamente recorridos de este tipo se usan para el calculo de estad?sticas,
|
||
93 | * histogramas, salvado a raster, etc... Es importante para el buen funcionamiento que este bloque sea
|
||
94 | 11386 | nacho | * potencia de dos.
|
95 | 11207 | nacho | */
|
96 | 16637 | nbrodin | public static int blockHeight = 512; |
97 | 11986 | bsanchez | |
98 | 10960 | nacho | //*************CACHE*******************
|
99 | /**
|
||
100 | 12333 | bsanchez | * Tama?o aproximado de cach? en Megas. Si este valor es alto cabr?n muchas p?ginas en memoria
|
101 | 10960 | nacho | * a la vez y si es bajo cabr?n pocas. Hay que tener en cuenta que al instanciar se convertira en bytes
|
102 | * para su mejor tratamiento. Al llamar al constructor esta variable contendr? el tama?o exacto
|
||
103 | * de la cache en bytes. El tama?o aqu? especificado es aproximado. Este variar? dependiendo de los
|
||
104 | * par?metros del raster a cachear ya que las p?ginas deben tener una altura potencia de 2.
|
||
105 | */
|
||
106 | 16637 | nbrodin | public static long cacheSize = 25; |
107 | 10960 | nacho | /**
|
108 | * Tama?o m?ximo de la p?gina en Megas. Hay que tener en cuenta que al instanciar se convertira en bytes
|
||
109 | * para su mejor tratamiento. Al llamar al constructor esta variable contendr? el tama?o exacto
|
||
110 | * de la p?gina en bytes
|
||
111 | */
|
||
112 | 16637 | nbrodin | public static double pageSize = 4; |
113 | 10960 | nacho | /**
|
114 | * N?mero de p?ginas que tiene cada conjunto de cach?
|
||
115 | */
|
||
116 | 16637 | nbrodin | public static int pagsPerGroup = 5; |
117 | 11986 | bsanchez | |
118 | 10960 | nacho | //*************PATHS*******************
|
119 | 11986 | bsanchez | |
120 | 10960 | nacho | /**
|
121 | * Directorio temporal para la cach?. Si gastamos el mismo que andami este se ocupar? de gestionar su
|
||
122 | * destrucci?n al cerrar gvSIG.
|
||
123 | */
|
||
124 | 16637 | nbrodin | public static String tempCacheDirectoryPath = System.getProperty("java.io.tmpdir") + File.separator + "tmp-andami"; |
125 | 10960 | nacho | /**
|
126 | 11483 | nacho | * Ruta o rutas donde busca jars con clases que incorporen elementos nuevos que extiendan
|
127 | * otros ya existentes. Estos pueden ser drivers o filtros.
|
||
128 | */
|
||
129 | 16637 | nbrodin | public static String[] pathExtensions = {"." + File.separator}; |
130 | 17040 | bsanchez | |
131 | 16637 | nbrodin | private static Hashtable clasesJar = new Hashtable(); |
132 | 17040 | bsanchez | |
133 | 11483 | nacho | /**
|
134 | 17660 | bsanchez | * Valor noData por defecto para la librer?a. En caso de no tener un valor asociado
|
135 | 16637 | nbrodin | * al raster se usar? este.
|
136 | */
|
||
137 | 17660 | bsanchez | public static double defaultNoDataValue = -99999; |
138 | 17040 | bsanchez | |
139 | 16637 | nbrodin | /**
|
140 | 10960 | nacho | * Ejecuta las acciones necesarias para arrancar la librer?a.
|
141 | */
|
||
142 | 11527 | bsanchez | public static void wakeUp() { |
143 | 17353 | nbrodin | if(wakeup)
|
144 | return;
|
||
145 | |||
146 | 11237 | nacho | //Punto de extensi?n para registro de drivers de lectura
|
147 | ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
||
148 | 11986 | bsanchez | if (!extensionPoints.containsKey("RasterReader")) |
149 | 11237 | nacho | extensionPoints.put( new ExtensionPoint( "RasterReader", "Raster Reader Classes")); |
150 | 11986 | bsanchez | |
151 | 11237 | nacho | //Punto de extensi?n para registro de drivers de escritura
|
152 | 11986 | bsanchez | if (!extensionPoints.containsKey("RasterWriter")) |
153 | 11237 | nacho | extensionPoints.put(new ExtensionPoint("RasterWriter", "Raster Writer Classes")); |
154 | 11648 | nacho | |
155 | 11237 | nacho | //Ejecuta el c?digo static de la clase cada driver
|
156 | 12122 | bsanchez | GdalDriver.register(); |
157 | 12712 | nacho | ErmapperDriver.register(); |
158 | 12124 | nacho | MrSidDriver.register(); |
159 | MemoryRasterDriver.register(); |
||
160 | 17040 | bsanchez | |
161 | 12124 | nacho | GdalWriter.register(); |
162 | ErmapperWriter.register(); |
||
163 | 12470 | nacho | JpegWriter.register(); |
164 | 17514 | nbrodin | PngWriter.register(); |
165 | 11986 | bsanchez | |
166 | 11528 | bsanchez | //Punto de extensi?n para registro de filtros
|
167 | 11526 | bsanchez | if (!extensionPoints.containsKey("RasterFilter")) { |
168 | extensionPoints.put(new ExtensionPoint("RasterFilter", "Raster Filter Classes")); |
||
169 | } |
||
170 | 11710 | bsanchez | |
171 | 12470 | nacho | // Invoca las llamadas est?ticas de cada clase para registrarlas en los
|
172 | 11710 | bsanchez | // puntos de extensi?n
|
173 | 12122 | bsanchez | BrightnessContrastListManager.register(); |
174 | EnhancementListManager.register(); |
||
175 | FirstDerivativeListManager.register(); |
||
176 | MedianListManager.register(); |
||
177 | ConvolutionListManager.register(); |
||
178 | 12218 | nacho | ColorTableListManager.register(); |
179 | 12333 | bsanchez | StatisticsListManager.register(); |
180 | 13385 | amunoz | PanSharpeningListManager.register(); |
181 | 15970 | nbrodin | RGBToHSLManager.register(); |
182 | HSLToRGBManager.register(); |
||
183 | 17100 | bsanchez | NoDataListManager.register(); |
184 | 17051 | nbrodin | ColorBalanceCMYManager.register(); |
185 | ColorBalanceRGBManager.register(); |
||
186 | 17148 | nbrodin | ToLumSaManager.register(); |
187 | 17051 | nbrodin | //RGBToCMYKManager.register();
|
188 | 11710 | bsanchez | // Registrar los nuevos filtros del directorio
|
189 | 14134 | nbrodin | //registerClasses();
|
190 | 11986 | bsanchez | |
191 | 10960 | nacho | //Limpiamos el directorio temporal
|
192 | RasterLibrary.cleanUpTempFiles(); |
||
193 | 17353 | nbrodin | wakeup = true;
|
194 | 10960 | nacho | } |
195 | 11986 | bsanchez | |
196 | 10960 | nacho | /**
|
197 | 11526 | bsanchez | * Elimina los ficheros del directorio temporal. Realizamos esta acci?n al
|
198 | * levantar la librer?a.
|
||
199 | 10960 | nacho | */
|
200 | 11526 | bsanchez | public static void cleanUpTempFiles() { |
201 | try {
|
||
202 | File tempDirectory = new File(tempCacheDirectoryPath); |
||
203 | 10960 | nacho | |
204 | 11526 | bsanchez | File[] files = tempDirectory.listFiles(); |
205 | if (files != null) { |
||
206 | for (int i = 0; i < files.length; i++) { |
||
207 | // s?lo por si en un futuro se necesitan crear directorios temporales
|
||
208 | if (files[i].isDirectory())
|
||
209 | deleteDirectory(files[i]); |
||
210 | files[i].delete(); |
||
211 | } |
||
212 | } |
||
213 | tempDirectory.delete(); |
||
214 | } catch (Exception e) { |
||
215 | } |
||
216 | } |
||
217 | 10960 | nacho | |
218 | 11526 | bsanchez | /**
|
219 | * Recursive directory delete.
|
||
220 | * @param f
|
||
221 | */
|
||
222 | private static void deleteDirectory(File f) { |
||
223 | File[] files = f.listFiles(); |
||
224 | for (int i = 0; i < files.length; i++) { |
||
225 | if (files[i].isDirectory())
|
||
226 | deleteDirectory(files[i]); |
||
227 | files[i].delete(); |
||
228 | } |
||
229 | } |
||
230 | 10960 | nacho | |
231 | 11526 | bsanchez | /**
|
232 | * Esta funci?n buscar? todos los jars en las rutas de pathExtensions y
|
||
233 | * registrar? todos las clases registrables. En este momento hay posibilidad
|
||
234 | * de registro de drivers y filtros.
|
||
235 | */
|
||
236 | 14134 | nbrodin | private static void registerClasses() throws Exception { |
237 | RasterClassLoader loader = new RasterClassLoader();
|
||
238 | 17040 | bsanchez | |
239 | 14134 | nbrodin | //Cargamos sobre jarList todos los File correspondientes a los jar contenidos en pathExtensions
|
240 | File[] jarList = null; |
||
241 | 11526 | bsanchez | for (int iPath = 0; iPath < pathExtensions.length; iPath++) { |
242 | 14134 | nbrodin | File directory = new File(pathExtensions[iPath]); |
243 | if (directory.isDirectory() && directory.canRead()) {
|
||
244 | jarList = directory.listFiles(new FileFilter() { |
||
245 | public boolean accept(File pathname) { |
||
246 | return (pathname.getName().toUpperCase().endsWith(".JAR")); |
||
247 | } |
||
248 | 17040 | bsanchez | }); |
249 | 11505 | nacho | } |
250 | } |
||
251 | 17040 | bsanchez | |
252 | 14134 | nbrodin | //Creamos las URL
|
253 | URL[] urls = new URL[jarList.length]; |
||
254 | |||
255 | for (int j = 0; j < jarList.length; j++) { |
||
256 | try {
|
||
257 | urls[j] = new URL("file:" + jarList[j]); |
||
258 | } catch (MalformedURLException e) { |
||
259 | Logger.getLogger(RasterLibrary.class.getName()).debug("Error formando la URL, jar incorrecto", e); |
||
260 | } |
||
261 | } |
||
262 | 17040 | bsanchez | |
263 | 14134 | nbrodin | //Comprobamos que no haya clases repetidas
|
264 | ZipFile[] jarFiles = new ZipFile[jarList.length]; |
||
265 | for (int i = 0; i < jarList.length; i++) { |
||
266 | try {
|
||
267 | jarFiles[i] = new ZipFile(jarList[i].getPath()); |
||
268 | |||
269 | Enumeration entradas = jarFiles[i].entries();
|
||
270 | |||
271 | while (entradas.hasMoreElements()) {
|
||
272 | ZipEntry file = (ZipEntry) entradas.nextElement(); |
||
273 | String fileName = file.getName();
|
||
274 | |||
275 | 17040 | bsanchez | if (!fileName.toLowerCase().endsWith(".class")) |
276 | 14134 | nbrodin | continue;
|
277 | 17040 | bsanchez | |
278 | 14134 | nbrodin | fileName = fileName.substring(0, fileName.length() - 6).replace('/', '.'); |
279 | |||
280 | if (clasesJar.get(fileName) != null) { |
||
281 | throw new JarException("CLASES REPETIDAS: " + fileName + " " + " en " + |
||
282 | jarFiles[i].getName() + " y en " + ((ZipFile) clasesJar.get(fileName)).getName()); |
||
283 | } |
||
284 | |||
285 | clasesJar.put(fileName, jarFiles[i]); |
||
286 | } |
||
287 | } catch (ZipException e) { |
||
288 | throw new IOException(" Jar: " + jarList[i].getPath() + ": " + jarFiles[i]); |
||
289 | } catch (IOException e) { |
||
290 | throw e;
|
||
291 | } |
||
292 | } |
||
293 | |||
294 | 11526 | bsanchez | } |
295 | 10960 | nacho | } |