gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / util / DefaultProviderServices.java @ 2616
History | View | Annotate | Download (25.8 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.raster.util; |
23 |
|
24 |
import java.awt.geom.AffineTransform; |
25 |
import java.io.File; |
26 |
import java.io.IOException; |
27 |
import java.lang.reflect.Constructor; |
28 |
import java.lang.reflect.InvocationTargetException; |
29 |
import java.util.ArrayList; |
30 |
import java.util.HashMap; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import java.util.Map; |
34 |
import java.util.Set; |
35 |
import java.util.TreeMap; |
36 |
|
37 |
import org.gvsig.fmap.dal.DALFileLocator; |
38 |
import org.gvsig.fmap.dal.DALLocator; |
39 |
import org.gvsig.fmap.dal.DataManager; |
40 |
import org.gvsig.fmap.dal.DataStoreParameters; |
41 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
42 |
import org.gvsig.fmap.dal.coverage.RasterManager; |
43 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
44 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
45 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
46 |
import org.gvsig.fmap.dal.coverage.exception.FileNotSupportedException; |
47 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
48 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
49 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
50 |
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException; |
51 |
import org.gvsig.fmap.dal.coverage.process.overview.OverviewBuilder; |
52 |
import org.gvsig.fmap.dal.coverage.store.DataServerWriter; |
53 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
54 |
import org.gvsig.fmap.dal.coverage.store.RasterWriter; |
55 |
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters; |
56 |
import org.gvsig.fmap.dal.coverage.store.parameter.RasterFileStoreParameters; |
57 |
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation; |
58 |
import org.gvsig.fmap.dal.coverage.util.CRSUtils; |
59 |
import org.gvsig.fmap.dal.coverage.util.ProviderServices; |
60 |
import org.gvsig.fmap.dal.exception.InitializeException; |
61 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
62 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
63 |
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider; |
64 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
65 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
66 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
67 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
68 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
69 |
import org.gvsig.fmap.dal.spi.DataStoreProvider; |
70 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
71 |
import org.gvsig.raster.impl.DefaultRasterManager; |
72 |
import org.gvsig.raster.impl.provider.AbstractRasterProvider; |
73 |
import org.gvsig.raster.impl.provider.RasterProvider; |
74 |
import org.gvsig.raster.impl.store.AbstractRasterDataParameters; |
75 |
import org.gvsig.raster.impl.store.DefaultRasterStore; |
76 |
import org.gvsig.raster.impl.store.WriteFileFormatFeatures; |
77 |
import org.gvsig.raster.impl.store.rmf.ClassSerializer; |
78 |
import org.gvsig.raster.impl.store.rmf.RmfBlocksManager; |
79 |
import org.gvsig.tools.ToolsLocator; |
80 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
81 |
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
82 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
83 |
import org.slf4j.Logger; |
84 |
import org.slf4j.LoggerFactory; |
85 |
|
86 |
/**
|
87 |
*
|
88 |
* 31/10/2007
|
89 |
* @author Nacho Brodin nachobrodin@gmail.com
|
90 |
*/
|
91 |
public class DefaultProviderServices implements ProviderServices { |
92 |
public TreeMap<String, WriteFileFormatFeatures> |
93 |
fileFeature = new TreeMap<String, WriteFileFormatFeatures>(); |
94 |
private Logger log = LoggerFactory.getLogger(DefaultProviderServices.class); |
95 |
private static List<Class<?>> tiledProviders = new ArrayList<Class<?>>(); |
96 |
private static Class<?> tileclass = null; |
97 |
private static HashMap<String, ArrayList<Class<?>>> |
98 |
formatsRegistry = new HashMap<String, ArrayList<Class<?>>>(); |
99 |
|
100 |
public DefaultRasterManager getManager() {
|
101 |
return (DefaultRasterManager)RasterLocator.getManager();
|
102 |
} |
103 |
|
104 |
public TreeMap<String, WriteFileFormatFeatures> getFileFeature() { |
105 |
return fileFeature;
|
106 |
} |
107 |
|
108 |
public RasterDataStore open(CoverageStoreProvider prov, DataStoreParameters params) throws NotSupportedExtensionException, RasterDriverException { |
109 |
if(prov instanceof RasterProvider) { |
110 |
DefaultRasterStore store = new DefaultRasterStore();
|
111 |
store.setProvider((RasterProvider)prov); |
112 |
try {
|
113 |
store.intializePhase1(null, params);
|
114 |
} catch (InitializeException e) {
|
115 |
throw new RasterDriverException(e.getMessage()); |
116 |
} |
117 |
return store;
|
118 |
} |
119 |
return null; |
120 |
} |
121 |
|
122 |
/**
|
123 |
* Abre un dataset pasando como par?metros la proyecci?n y un objeto identificador del dataset. Este
|
124 |
* objeto puede ser una ruta a un fichero en disco. En este caso la extensi?n del fichero servir? para
|
125 |
* buscar el driver que lo gestiona. Si proporcionamos un array de cadenas se tratar?n como la ruta a N ficheros
|
126 |
* de disco. Tambi?n puede ser un buffer de datos en memoria o cualquier otro objeto
|
127 |
* que pueda aceptar un driver.
|
128 |
* @param param Par?metros al driver
|
129 |
* @return RasterMultiDatset
|
130 |
* @throws NotSupportedExtensionException
|
131 |
* @throws RasterDriverException
|
132 |
*/
|
133 |
public RasterDataStore open(DataStoreParameters params) throws NotSupportedExtensionException, RasterDriverException { |
134 |
if(params == null || !(params instanceof AbstractRasterDataParameters)) |
135 |
return null; |
136 |
DefaultRasterStore dSet = new DefaultRasterStore();
|
137 |
DataManagerProviderServices dataManager = (DataManagerProviderServices)DALLocator.getDataManager(); |
138 |
try {
|
139 |
AbstractRasterProvider provider = (AbstractRasterProvider)dataManager.createProvider(dSet, (AbstractRasterDataParameters)params); |
140 |
dSet.setProvider(provider); |
141 |
} catch (InitializeException e) {
|
142 |
throw new RasterDriverException("Error initializing provider", e); |
143 |
} catch (ProviderNotRegisteredException e) {
|
144 |
throw new RasterDriverException("Provider not register error", e); |
145 |
} |
146 |
return dSet;
|
147 |
} |
148 |
|
149 |
public RasterDataStore open(Object datasetOpenParam) throws NotSupportedExtensionException, RasterDriverException { |
150 |
DefaultRasterStore dSet = new DefaultRasterStore();
|
151 |
|
152 |
if (datasetOpenParam instanceof String) { |
153 |
AbstractRasterProvider provider = AbstractRasterProvider.singleDatasetInstance(dSet, (String)datasetOpenParam);
|
154 |
dSet.setProvider(provider); |
155 |
return dSet;
|
156 |
} |
157 |
|
158 |
/*if (datasetOpenParam instanceof String[]) {
|
159 |
String[] param = (String[]) datasetOpenParam;
|
160 |
try {
|
161 |
for (int dataset = 0; dataset < param.length; dataset++) {
|
162 |
dSet.addDataStore(DefaultRasterProvider.singleDatasetInstance(dSet, param[dataset]) );
|
163 |
}
|
164 |
} catch (OperationNotSupportedException e) {
|
165 |
return open(param[0]);
|
166 |
}
|
167 |
return dSet;
|
168 |
}
|
169 |
|
170 |
if (datasetOpenParam instanceof Buffer[]) {
|
171 |
Buffer[] param = (Buffer[]) datasetOpenParam;
|
172 |
for (int dataset = 0; dataset < param.length; dataset++) {
|
173 |
dSet.addDataStore(DefaultRasterProvider.singleDatasetInstance(param[dataset]));
|
174 |
}
|
175 |
return dSet;
|
176 |
}
|
177 |
|
178 |
if(datasetOpenParam instanceof String[][]) {
|
179 |
String[][] param = (String[][])datasetOpenParam;
|
180 |
DefaultMultiRasterStore[][] mosaic = new DefaultMultiRasterStore[param.length][param[0].length];
|
181 |
for (int i = 0; i < param.length; i++)
|
182 |
for (int j = 0; j < param[i].length; j++)
|
183 |
mosaic[i][j] = (DefaultMultiRasterStore)open(param[i][j]);
|
184 |
DefaultMosaicRasterStore cd;
|
185 |
try {
|
186 |
cd = new DefaultMosaicRasterStore(mosaic);
|
187 |
} catch (MosaicNotValidException e) {
|
188 |
return null;
|
189 |
}
|
190 |
return cd;
|
191 |
}*/
|
192 |
|
193 |
return null; |
194 |
} |
195 |
|
196 |
public RasterDataStore open(String file) throws NotSupportedExtensionException, RasterDriverException, InitializeException, ProviderNotRegisteredException { |
197 |
RasterDataParameters params = createNotTiledParameters(file); |
198 |
return open(params);
|
199 |
} |
200 |
|
201 |
|
202 |
/**
|
203 |
* Loads the specific provider to download data
|
204 |
* @param file
|
205 |
* @return
|
206 |
* @throws NotSupportedExtensionException
|
207 |
* @throws FileNotSupportedException
|
208 |
*/
|
209 |
@SuppressWarnings("unchecked") |
210 |
public static AbstractRasterProvider loadProvider(File file) throws ProviderNotRegisteredException, InitializeException { |
211 |
DataManagerProviderServices dataManager = (DataManagerProviderServices)DALLocator.getDataManager(); |
212 |
DataStoreProviderServices storeServices = null;
|
213 |
DataStoreProvider prov = null;
|
214 |
|
215 |
if(file != null) { |
216 |
//We have to locate a provider's name which manages the selected file
|
217 |
//A FilesystemServerExplorer will give a getProviderNames service
|
218 |
FilesystemServerExplorerParameters paramsExplorer = (FilesystemServerExplorerParameters)dataManager.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
219 |
FilesystemServerExplorer serverExplorer = null;
|
220 |
try {
|
221 |
paramsExplorer.setRoot(File.separator);
|
222 |
serverExplorer = (FilesystemServerExplorer)dataManager.openServerExplorer(FilesystemServerExplorer.NAME, paramsExplorer); |
223 |
} catch (ValidateDataParametersException e) {
|
224 |
throw new InitializeException(e); |
225 |
} |
226 |
|
227 |
//Gets the list of provider's name to manage the file
|
228 |
List<String> provNameList = serverExplorer.getProviderNameList(file); |
229 |
if(provNameList.size() > 0) { |
230 |
for (int i = 0; i < provNameList.size(); i++) { |
231 |
//Gets the first provider what is not a TileProvider
|
232 |
if(provNameList.get(i).compareTo("Tile Store") != 0) { |
233 |
DataStoreParameters newparams = dataManager.createStoreParameters(provNameList.get(i)); |
234 |
((FilesystemStoreParameters)newparams).setFile(file); |
235 |
prov = dataManager.createProvider(storeServices, newparams); |
236 |
} |
237 |
} |
238 |
} else { //Default provider. This should be Gdal |
239 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
240 |
ExtensionPoint point = extensionPoints.get("DefaultRasterProvider");
|
241 |
Extension ext = point.get("reader");
|
242 |
String provName = ext.getDescription();
|
243 |
DataStoreParameters newparams = dataManager.createStoreParameters(provName); |
244 |
((FilesystemStoreParameters)newparams).setFile(file); |
245 |
prov = dataManager.createProvider(storeServices, newparams); |
246 |
} |
247 |
} |
248 |
|
249 |
if(prov != null && prov instanceof RasterProvider) { |
250 |
if(((RasterProvider)prov).isRotated())
|
251 |
throw new InitializeException("Rotation not supported tiling files", new Throwable()); |
252 |
|
253 |
return (AbstractRasterProvider)prov;
|
254 |
} |
255 |
|
256 |
return null; |
257 |
} |
258 |
|
259 |
public RasterDataParameters createNotTiledParameters(String id) throws InitializeException, ProviderNotRegisteredException { |
260 |
DataManagerProviderServices dataManager = (DataManagerProviderServices)DALLocator.getDataManager(); |
261 |
ArrayList<RasterDataParameters> parametersList = createParametersList(id);
|
262 |
for (int i = 0; i < parametersList.size(); i++) { |
263 |
DataStoreProvider p = dataManager.createProvider(null, parametersList.get(i));
|
264 |
if(p instanceof RasterProvider) { |
265 |
if(!((RasterProvider)p).isTiled())
|
266 |
return parametersList.get(i);
|
267 |
} |
268 |
} |
269 |
return null; |
270 |
} |
271 |
|
272 |
public RasterDataParameters createParameters(String id) { |
273 |
Iterator<?> it = DALFileLocator.getFilesystemServerExplorerManager().getRegisteredProviders();
|
274 |
while (it.hasNext()) {
|
275 |
ExtensionPoint.Extension o = (ExtensionPoint.Extension)it.next(); |
276 |
Class<?> c = o.getExtension();
|
277 |
try {
|
278 |
Constructor<?> hazNuevo = c.getConstructor();
|
279 |
Object [] args2 = {}; |
280 |
Object obj = hazNuevo.newInstance(args2);
|
281 |
if(obj instanceof FilesystemServerExplorerProvider) { |
282 |
FilesystemServerExplorerProvider prov = (FilesystemServerExplorerProvider)obj; |
283 |
File file = new File(id); |
284 |
if(prov.accept(file)) {
|
285 |
DataManager manager = DALLocator.getDataManager(); |
286 |
RasterFileStoreParameters params = null;
|
287 |
try {
|
288 |
params = (RasterFileStoreParameters) manager |
289 |
.createStoreParameters(prov.getDataStoreProviderName()); |
290 |
} catch (InitializeException e) {
|
291 |
} catch (ProviderNotRegisteredException e) {
|
292 |
} |
293 |
params.setFile(file); |
294 |
return params;
|
295 |
} |
296 |
} |
297 |
} catch (SecurityException e) { |
298 |
log.debug("Error SecurityException in open", e);
|
299 |
} catch (NoSuchMethodException e) { |
300 |
log.debug("Error NoSuchMethodException in open", e);
|
301 |
} catch (IllegalArgumentException e) { |
302 |
log.debug("Error IllegalArgumentException in open", e);
|
303 |
} catch (InstantiationException e) { |
304 |
log.debug("Error InstantiationException in open", e);
|
305 |
} catch (IllegalAccessException e) { |
306 |
log.debug("Error IllegalAccessException in open", e);
|
307 |
} catch (InvocationTargetException e) { |
308 |
log.debug("Error opening this image with ", e);
|
309 |
} |
310 |
} |
311 |
return null; |
312 |
} |
313 |
|
314 |
public ArrayList<RasterDataParameters> createParametersList(String id) { |
315 |
ArrayList<RasterDataParameters> list = new ArrayList<RasterDataParameters>(); |
316 |
|
317 |
Iterator<?> it = DALFileLocator.getFilesystemServerExplorerManager().getRegisteredProviders();
|
318 |
while (it.hasNext()) {
|
319 |
ExtensionPoint.Extension o = (ExtensionPoint.Extension)it.next(); |
320 |
Class<?> c = o.getExtension();
|
321 |
try {
|
322 |
Constructor<?> hazNuevo = c.getConstructor();
|
323 |
Object [] args2 = {}; |
324 |
Object obj = hazNuevo.newInstance(args2);
|
325 |
if(obj instanceof FilesystemServerExplorerProvider) { |
326 |
FilesystemServerExplorerProvider prov = (FilesystemServerExplorerProvider)obj; |
327 |
File file = new File(id); |
328 |
if(prov.accept(file)) {
|
329 |
DataManager manager = DALLocator.getDataManager(); |
330 |
RasterFileStoreParameters params = null;
|
331 |
try {
|
332 |
params = (RasterFileStoreParameters) manager |
333 |
.createStoreParameters(prov.getDataStoreProviderName()); |
334 |
} catch (InitializeException e) {
|
335 |
} catch (ProviderNotRegisteredException e) {
|
336 |
} |
337 |
params.setFile(file); |
338 |
list.add(params); |
339 |
} |
340 |
} |
341 |
} catch (SecurityException e) { |
342 |
log.debug("Error SecurityException in open", e);
|
343 |
} catch (NoSuchMethodException e) { |
344 |
log.debug("Error NoSuchMethodException in open", e);
|
345 |
} catch (IllegalArgumentException e) { |
346 |
log.debug("Error IllegalArgumentException in open", e);
|
347 |
} catch (InstantiationException e) { |
348 |
log.debug("Error InstantiationException in open", e);
|
349 |
} catch (IllegalAccessException e) { |
350 |
log.debug("Error IllegalAccessException in open", e);
|
351 |
} catch (InvocationTargetException e) { |
352 |
log.debug("Error opening this image with", e);
|
353 |
} |
354 |
} |
355 |
return list;
|
356 |
} |
357 |
|
358 |
public RasterDataParameters createParametersForTiledFiled(String id) { |
359 |
DataManager manager = DALLocator.getDataManager(); |
360 |
RasterFileStoreParameters params = null;
|
361 |
try {
|
362 |
params = (RasterFileStoreParameters) manager.createStoreParameters("Tile Store");
|
363 |
} catch (InitializeException e) {
|
364 |
} catch (ProviderNotRegisteredException e) {
|
365 |
} |
366 |
File file = new File(id); |
367 |
params.setFile(file); |
368 |
return params;
|
369 |
} |
370 |
|
371 |
@SuppressWarnings("unchecked") |
372 |
public String[] getDriversExtensions() { |
373 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
374 |
ExtensionPoint point = extensionPoints.get("RasterWriter");
|
375 |
return (String[])point.getNames().toArray(new String[0]); |
376 |
} |
377 |
|
378 |
public String[] getRODriversExtensions() { |
379 |
return getReadOnlyFormatList();
|
380 |
} |
381 |
|
382 |
public ArrayList<String> getExtensionsSupported(int dataType, int bands) throws RasterDriverException { |
383 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
384 |
ExtensionPoint point = extensionPoints.get("RasterWriter");
|
385 |
Iterator<?> iterator = point.iterator();
|
386 |
ArrayList<String> result = new ArrayList<String>(); |
387 |
while (iterator.hasNext()) {
|
388 |
ExtensionPoint.Extension extension = (ExtensionPoint.Extension) iterator.next(); |
389 |
String ext = extension.getName();
|
390 |
try {
|
391 |
if (isSupportedThisFileToWrite(ext, dataType, bands))
|
392 |
result.add(ext); |
393 |
} catch (SecurityException e) { |
394 |
throw new RasterDriverException("Error SecurityException in open"); |
395 |
} catch (IllegalArgumentException e) { |
396 |
throw new RasterDriverException("Error IllegalArgumentException in open"); |
397 |
} |
398 |
} |
399 |
return result;
|
400 |
} |
401 |
|
402 |
@SuppressWarnings("rawtypes") |
403 |
public String[] getWriteDriversType() { |
404 |
if (fileFeature.size() == 0) |
405 |
return null; |
406 |
String[] list = new String[fileFeature.size()]; |
407 |
Set<?> values = fileFeature.entrySet();
|
408 |
int i = 0; |
409 |
for (Iterator<?> it = values.iterator(); it.hasNext();) { |
410 |
list[i] = ((WriteFileFormatFeatures) ((Map.Entry) it.next()).getValue()).getDriverName();
|
411 |
i++; |
412 |
} |
413 |
|
414 |
return list;
|
415 |
} |
416 |
|
417 |
public String getWriteDriverType(String ext) { |
418 |
return ((WriteFileFormatFeatures) fileFeature.get(ext)).getDriverName();
|
419 |
} |
420 |
|
421 |
public int getWriteNDrivers() { |
422 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
423 |
ExtensionPoint point = extensionPoints.get("RasterWriter");
|
424 |
return point.getCount();
|
425 |
} |
426 |
|
427 |
public int getWriteNTypes() { |
428 |
return fileFeature.size();
|
429 |
} |
430 |
|
431 |
public boolean isSupportedThisFileToWrite(String ext, int dataType, int bands) { |
432 |
WriteFileFormatFeatures features = (WriteFileFormatFeatures) fileFeature.get(ext); |
433 |
if (features == null) |
434 |
return false; |
435 |
int[] bandsSupported = features.getNBandsSupported(); |
436 |
if (bandsSupported[0] != -1 && bandsSupported[0] < bands) |
437 |
return false; |
438 |
int[] dt = features.getDataTypesSupported(); |
439 |
for (int i = 0; i < dt.length; i++) |
440 |
if (dataType == dt[i])
|
441 |
return true; |
442 |
return false; |
443 |
} |
444 |
|
445 |
/**
|
446 |
* Guarda en el RMF que trata el BlocksManager el valor value usando el serializador que soporta
|
447 |
* la clase class1.
|
448 |
*
|
449 |
* @param blocksManager
|
450 |
* @param class1
|
451 |
* @param value
|
452 |
* @throws RmfSerializerException
|
453 |
*/
|
454 |
public void saveObjectToRmfFile(RmfBlocksManager blocksManager, Class<?> class1, Object value) throws RmfSerializerException { |
455 |
ClassSerializer serializerObject = getSerializerObject(class1, value); |
456 |
|
457 |
if (serializerObject == null) |
458 |
throw new RmfSerializerException("No se ha podido encontrar el serializador para el Rmf"); |
459 |
|
460 |
if (!blocksManager.checkRmf()) {
|
461 |
log.info("Error al comprobar el fichero Rmf");
|
462 |
} |
463 |
|
464 |
blocksManager.addClient(serializerObject); |
465 |
try {
|
466 |
blocksManager.write(true);
|
467 |
} catch (IOException e) { |
468 |
log.info("Error al escribir el fichero Rmf", e);
|
469 |
} |
470 |
blocksManager.removeAllClients(); |
471 |
} |
472 |
|
473 |
public void saveObjectToRmfFile(String file, Class<?> class1, Object value) throws RmfSerializerException { |
474 |
String fileRMF = RasterLocator.getManager().getFileUtils().getNameWithoutExtension(file) + ".rmf"; |
475 |
RmfBlocksManager blocksManager = new RmfBlocksManager(fileRMF);
|
476 |
saveObjectToRmfFile(blocksManager, class1, value); |
477 |
} |
478 |
|
479 |
public void saveObjectToRmfFile(String file, Object value) throws RmfSerializerException { |
480 |
saveObjectToRmfFile(file, value.getClass(), value); |
481 |
} |
482 |
|
483 |
/**
|
484 |
* Construye un serializador que soporte la clase class1. El constructor se invoca
|
485 |
* con el parametro value.
|
486 |
*
|
487 |
* @param class1
|
488 |
* @param value
|
489 |
* @return
|
490 |
* @throws RmfSerializerException
|
491 |
*/
|
492 |
public ClassSerializer getSerializerObject(Class<?> class1, Object value) throws RmfSerializerException { |
493 |
try {
|
494 |
Class<?>[] args = { class1 }; |
495 |
Constructor<?> constructor = getSerializerClass(class1).getConstructor(args);
|
496 |
Object[] args2 = { value }; |
497 |
return (ClassSerializer) constructor.newInstance(args2);
|
498 |
} catch (Exception e) { |
499 |
throw new RmfSerializerException("No se ha podido crear el serializador para el Rmf", e); |
500 |
} |
501 |
} |
502 |
|
503 |
/**
|
504 |
* Gets the serializer which has a constructor with the class selected in the
|
505 |
* parameters of this function
|
506 |
* @throws RmfSerializerException
|
507 |
*/
|
508 |
private Class<?> getSerializerClass(Class<?> class1) |
509 |
throws RmfSerializerException {
|
510 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
511 |
ExtensionPoint point = extensionPoints.get("Serializer");
|
512 |
Iterator<?> iterator = point.iterator();
|
513 |
while (iterator.hasNext()) {
|
514 |
ExtensionPoint.Extension extension = (ExtensionPoint.Extension) iterator.next(); |
515 |
if (extension != null) { |
516 |
Class<?>[] args = { class1 }; |
517 |
Class<?> clase = extension.getExtension();
|
518 |
try {
|
519 |
clase.getConstructor(args); |
520 |
return clase;
|
521 |
} catch (Exception e) { |
522 |
// Si no encuentra un constructor para esta clase, seguir? buscando en los dem?s elementos
|
523 |
} |
524 |
} |
525 |
} |
526 |
throw new RmfSerializerException("No se ha encontrado la clase para el serializador"); |
527 |
} |
528 |
|
529 |
@SuppressWarnings("deprecation") |
530 |
public void exportBufferToFile(Buffer bufResult, |
531 |
double pixelSize,
|
532 |
String extension,
|
533 |
boolean alphaBand,
|
534 |
File file,
|
535 |
Extent extent, |
536 |
ColorInterpretation colorInterpretation) throws NotSupportedExtensionException, RasterDriverException, ProcessInterruptedException, IOException { |
537 |
//Escritura en disco del tile
|
538 |
RasterManager rManager = RasterLocator.getManager(); |
539 |
DataServerWriter dataWriter = RasterLocator.getManager().createDataServerWriter(); |
540 |
dataWriter.setBuffer(bufResult, -1);
|
541 |
Params params = rManager.createWriter("_." + extension).getParams();
|
542 |
AffineTransform affineTransform = new AffineTransform(pixelSize, 0, 0, -pixelSize, |
543 |
extent.getULX(), |
544 |
extent.getULY()); |
545 |
RasterWriter rw = rManager.createWriter(dataWriter, file.getAbsolutePath(), |
546 |
bufResult.getBandCount(), affineTransform, bufResult.getWidth(), |
547 |
bufResult.getHeight(), bufResult.getDataType(), params, null);
|
548 |
|
549 |
if(colorInterpretation != null) { |
550 |
String[] values = colorInterpretation.getValues(); |
551 |
if(alphaBand) {
|
552 |
String[] newValues = values; |
553 |
boolean exists = false; |
554 |
for (int i = 0; i < values.length; i++) { |
555 |
if(values[i] != null && values[i].compareTo("Alpha") == 0) |
556 |
exists = true;
|
557 |
} |
558 |
if(!exists) {
|
559 |
newValues = new String[values.length + 1]; |
560 |
for (int i = 0; i < values.length; i++) { |
561 |
newValues[i] = values[i]; |
562 |
} |
563 |
newValues[newValues.length - 1] = "Alpha"; |
564 |
} |
565 |
rw.setColorBandsInterpretation(newValues); |
566 |
} |
567 |
|
568 |
} else {
|
569 |
if(alphaBand) {
|
570 |
String[] ci = new String[bufResult.getBandCount()]; |
571 |
if(bufResult.getBandCount() == 4) { |
572 |
ci[0] = "Red"; |
573 |
ci[1] = "Green"; |
574 |
ci[2] = "Blue"; |
575 |
ci[3] = "Alpha"; |
576 |
} else {
|
577 |
for (int i = 0; i < bufResult.getBandCount(); i++) { |
578 |
ci[i] = "Gray";
|
579 |
} |
580 |
} |
581 |
rw.setColorBandsInterpretation(ci); |
582 |
} |
583 |
} |
584 |
rw.dataWrite(); |
585 |
rw.writeClose(); |
586 |
} |
587 |
|
588 |
public void addFormat(String ext, Class<?> c) { |
589 |
ArrayList<Class<?>> list = formatsRegistry.get(ext); |
590 |
if(list == null) { |
591 |
list = new ArrayList<Class<?>>(); |
592 |
list.add(c); |
593 |
formatsRegistry.put(ext, list); |
594 |
} else {
|
595 |
if(!list.contains(c))
|
596 |
list.add(c); |
597 |
} |
598 |
} |
599 |
|
600 |
public boolean isExtensionSupported(String ext, Class<?> c) { |
601 |
String extension = getManager().getFileUtils().getExtensionFromFileName(ext);
|
602 |
|
603 |
ArrayList<Class<?>> list = formatsRegistry.get(extension); |
604 |
if(list == null) { |
605 |
return false; |
606 |
} else {
|
607 |
for (int i = 0; i < list.size(); i++) { |
608 |
if(c == list.get(i))
|
609 |
return true; |
610 |
} |
611 |
} |
612 |
return false; |
613 |
} |
614 |
|
615 |
public boolean isExtensionSupported(String ext) { |
616 |
String extension = getManager().getFileUtils().getExtensionFromFileName(ext);
|
617 |
|
618 |
ArrayList<Class<?>> list = formatsRegistry.get(extension); |
619 |
if(list == null) { |
620 |
return false; |
621 |
} |
622 |
|
623 |
return true; |
624 |
} |
625 |
|
626 |
public String[] getReadOnlyFormatList() { |
627 |
return formatsRegistry.keySet().toArray(new String[0]); |
628 |
} |
629 |
|
630 |
public void registerFileProvidersTiled(Class<?> provider) { |
631 |
if(!tiledProviders.contains(provider))
|
632 |
tiledProviders.add(provider); |
633 |
} |
634 |
|
635 |
public void registerTileProviderFormats(Class<?> c) { |
636 |
if(tileclass == null) { |
637 |
if(RasterProvider.class.isAssignableFrom(c)) {
|
638 |
List<Object> pList = getTiledProvidersInstance(); |
639 |
for (int i = 0; i < pList.size(); i++) { |
640 |
String[] fList = ((RasterProvider)pList.get(i)).getFormatList(); |
641 |
for (int j = 0; j < fList.length; j++) { |
642 |
String value = fList[j];
|
643 |
addFormat(value, c); |
644 |
} |
645 |
} |
646 |
} |
647 |
tileclass = c; |
648 |
} |
649 |
} |
650 |
|
651 |
private List<Object> getTiledProvidersInstance() { |
652 |
List<Object> list = new ArrayList<Object>(); |
653 |
for (int i = 0; i < tiledProviders.size(); i++) { |
654 |
Class<?> provider = tiledProviders.get(i);
|
655 |
try {
|
656 |
Constructor<?> c = provider.getConstructor();
|
657 |
list.add(c.newInstance()); |
658 |
} catch (SecurityException e) { |
659 |
log.info("Error SecurityException in open", e);
|
660 |
} catch (NoSuchMethodException e) { |
661 |
log.info("Error NoSuchMethodException in open", e);
|
662 |
} catch (IllegalArgumentException e) { |
663 |
log.info("Error IllegalArgumentException in open", e);
|
664 |
} catch (InstantiationException e) { |
665 |
log.info("Error InstantiationException in open", e);
|
666 |
} catch (IllegalAccessException e) { |
667 |
log.info("Error IllegalAccessException in open", e);
|
668 |
} catch (InvocationTargetException e) { |
669 |
log.info("Error in open. Problemas con las librer?as nativas.", e);
|
670 |
} |
671 |
} |
672 |
return list;
|
673 |
} |
674 |
|
675 |
public void registerOverviewBuilderImplementation(String id, OverviewBuilder ovBuilder) { |
676 |
DefaultRasterManager.overviewBuilderRegistry.put(id, ovBuilder); |
677 |
} |
678 |
|
679 |
public void registerCRSUtilImplementation(CRSUtils crsUtil) { |
680 |
DefaultRasterManager.crsUtilRegistry = crsUtil; |
681 |
} |
682 |
} |