gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / util / DefaultProviderServices.java @ 2348
History | View | Annotate | Download (25.9 KB)
1 | 134 | nbrodin | /* 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 | 1141 | nbrodin | import java.awt.geom.AffineTransform; |
25 | 142 | nbrodin | import java.io.File; |
26 | 135 | nbrodin | import java.io.IOException; |
27 | 134 | nbrodin | import java.lang.reflect.Constructor; |
28 | import java.lang.reflect.InvocationTargetException; |
||
29 | import java.util.ArrayList; |
||
30 | 2348 | nbrodin | import java.util.HashMap; |
31 | 134 | nbrodin | import java.util.Iterator; |
32 | 1938 | nbrodin | import java.util.List; |
33 | 134 | nbrodin | import java.util.Map; |
34 | import java.util.Set; |
||
35 | import java.util.TreeMap; |
||
36 | |||
37 | 140 | nbrodin | import org.gvsig.fmap.dal.DALFileLocator; |
38 | 142 | nbrodin | import org.gvsig.fmap.dal.DALLocator; |
39 | import org.gvsig.fmap.dal.DataManager; |
||
40 | 505 | nbrodin | import org.gvsig.fmap.dal.DataStoreParameters; |
41 | 134 | nbrodin | import org.gvsig.fmap.dal.coverage.RasterLocator; |
42 | 1141 | nbrodin | 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 | 505 | nbrodin | import org.gvsig.fmap.dal.coverage.exception.FileNotSupportedException; |
47 | import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
||
48 | 1141 | nbrodin | import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
49 | 134 | nbrodin | import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
50 | 135 | nbrodin | import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException; |
51 | 1967 | nbrodin | import org.gvsig.fmap.dal.coverage.process.overview.OverviewBuilder; |
52 | 1141 | nbrodin | import org.gvsig.fmap.dal.coverage.store.DataServerWriter; |
53 | 1862 | nbrodin | import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
54 | 1141 | nbrodin | import org.gvsig.fmap.dal.coverage.store.RasterWriter; |
55 | 471 | nbrodin | import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters; |
56 | import org.gvsig.fmap.dal.coverage.store.parameter.RasterFileStoreParameters; |
||
57 | 1141 | nbrodin | import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation; |
58 | 1971 | nbrodin | import org.gvsig.fmap.dal.coverage.util.CRSUtils; |
59 | 135 | nbrodin | import org.gvsig.fmap.dal.coverage.util.ProviderServices; |
60 | 142 | nbrodin | import org.gvsig.fmap.dal.exception.InitializeException; |
61 | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
||
62 | 505 | nbrodin | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
63 | 1862 | nbrodin | import org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider; |
64 | 505 | nbrodin | 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 | 140 | nbrodin | import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
68 | 505 | nbrodin | import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
69 | import org.gvsig.fmap.dal.spi.DataStoreProvider; |
||
70 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
71 | 1862 | nbrodin | import org.gvsig.raster.impl.DefaultRasterManager; |
72 | 2232 | nbrodin | import org.gvsig.raster.impl.provider.AbstractRasterProvider; |
73 | 505 | nbrodin | import org.gvsig.raster.impl.provider.RasterProvider; |
74 | 2348 | nbrodin | import org.gvsig.raster.impl.store.AbstractRasterDataParameters; |
75 | import org.gvsig.raster.impl.store.DefaultRasterStore; |
||
76 | 134 | nbrodin | import org.gvsig.raster.impl.store.WriteFileFormatFeatures; |
77 | 135 | nbrodin | import org.gvsig.raster.impl.store.rmf.ClassSerializer; |
78 | import org.gvsig.raster.impl.store.rmf.RmfBlocksManager; |
||
79 | 134 | nbrodin | import org.gvsig.tools.ToolsLocator; |
80 | import org.gvsig.tools.extensionpoint.ExtensionPoint; |
||
81 | 1938 | nbrodin | import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
82 | 134 | nbrodin | import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
83 | 2172 | nbrodin | import org.slf4j.Logger; |
84 | import org.slf4j.LoggerFactory; |
||
85 | 134 | nbrodin | |
86 | /**
|
||
87 | *
|
||
88 | * 31/10/2007
|
||
89 | * @author Nacho Brodin nachobrodin@gmail.com
|
||
90 | */
|
||
91 | 135 | nbrodin | public class DefaultProviderServices implements ProviderServices { |
92 | 134 | nbrodin | public TreeMap<String, WriteFileFormatFeatures> |
93 | 2348 | nbrodin | 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 | 134 | nbrodin | |
100 | 1862 | nbrodin | public DefaultRasterManager getManager() {
|
101 | return (DefaultRasterManager)RasterLocator.getManager();
|
||
102 | } |
||
103 | |||
104 | 134 | nbrodin | public TreeMap<String, WriteFileFormatFeatures> getFileFeature() { |
105 | return fileFeature;
|
||
106 | } |
||
107 | |||
108 | 2348 | nbrodin | 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 | 505 | nbrodin | /**
|
123 | 1938 | nbrodin | * 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 | 1862 | nbrodin | * que pueda aceptar un driver.
|
128 | 1938 | nbrodin | * @param param Par?metros al driver
|
129 | 1862 | nbrodin | * @return RasterMultiDatset
|
130 | * @throws NotSupportedExtensionException
|
||
131 | * @throws RasterDriverException
|
||
132 | */
|
||
133 | 2348 | nbrodin | 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 | 1862 | nbrodin | } |
148 | |||
149 | 2348 | nbrodin | 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 | 1862 | nbrodin | } |
195 | |||
196 | 2172 | nbrodin | public RasterDataStore open(String file) throws NotSupportedExtensionException, RasterDriverException, InitializeException, ProviderNotRegisteredException { |
197 | RasterDataParameters params = createNotTiledParameters(file); |
||
198 | return open(params);
|
||
199 | } |
||
200 | 1862 | nbrodin | |
201 | 2172 | nbrodin | |
202 | 1862 | nbrodin | /**
|
203 | 505 | nbrodin | * Loads the specific provider to download data
|
204 | * @param file
|
||
205 | * @return
|
||
206 | * @throws NotSupportedExtensionException
|
||
207 | * @throws FileNotSupportedException
|
||
208 | */
|
||
209 | 2172 | nbrodin | @SuppressWarnings("unchecked") |
210 | 2232 | nbrodin | public static AbstractRasterProvider loadProvider(File file) throws ProviderNotRegisteredException, InitializeException { |
211 | 505 | nbrodin | 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 | 1938 | nbrodin | List<String> provNameList = serverExplorer.getProviderNameList(file); |
229 | 505 | nbrodin | 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 | 1078 | nbrodin | ExtensionPoint point = extensionPoints.get("DefaultRasterProvider");
|
241 | 505 | nbrodin | 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 | 2232 | nbrodin | return (AbstractRasterProvider)prov;
|
254 | 505 | nbrodin | } |
255 | |||
256 | return null; |
||
257 | } |
||
258 | |||
259 | 746 | nbrodin | 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 | 484 | nbrodin | public RasterDataParameters createParameters(String id) { |
273 | 2172 | nbrodin | Iterator<?> it = DALFileLocator.getFilesystemServerExplorerManager().getRegisteredProviders();
|
274 | 140 | nbrodin | while (it.hasNext()) {
|
275 | ExtensionPoint.Extension o = (ExtensionPoint.Extension)it.next(); |
||
276 | 2172 | nbrodin | Class<?> c = o.getExtension();
|
277 | 140 | nbrodin | try {
|
278 | 2172 | nbrodin | Constructor<?> hazNuevo = c.getConstructor();
|
279 | 140 | nbrodin | Object [] args2 = {}; |
280 | Object obj = hazNuevo.newInstance(args2);
|
||
281 | if(obj instanceof FilesystemServerExplorerProvider) { |
||
282 | FilesystemServerExplorerProvider prov = (FilesystemServerExplorerProvider)obj; |
||
283 | 142 | nbrodin | File file = new File(id); |
284 | if(prov.accept(file)) {
|
||
285 | DataManager manager = DALLocator.getDataManager(); |
||
286 | 178 | nbrodin | RasterFileStoreParameters params = null;
|
287 | 142 | nbrodin | try {
|
288 | 178 | nbrodin | params = (RasterFileStoreParameters) manager |
289 | 142 | nbrodin | .createStoreParameters(prov.getDataStoreProviderName()); |
290 | } catch (InitializeException e) {
|
||
291 | } catch (ProviderNotRegisteredException e) {
|
||
292 | } |
||
293 | params.setFile(file); |
||
294 | return params;
|
||
295 | } |
||
296 | 180 | nbrodin | } |
297 | 140 | nbrodin | } catch (SecurityException e) { |
298 | 2172 | nbrodin | log.debug("Error SecurityException in open", e);
|
299 | 140 | nbrodin | } catch (NoSuchMethodException e) { |
300 | 2172 | nbrodin | log.debug("Error NoSuchMethodException in open", e);
|
301 | 140 | nbrodin | } catch (IllegalArgumentException e) { |
302 | 2172 | nbrodin | log.debug("Error IllegalArgumentException in open", e);
|
303 | 140 | nbrodin | } catch (InstantiationException e) { |
304 | 2172 | nbrodin | log.debug("Error InstantiationException in open", e);
|
305 | 140 | nbrodin | } catch (IllegalAccessException e) { |
306 | 2172 | nbrodin | log.debug("Error IllegalAccessException in open", e);
|
307 | 140 | nbrodin | } catch (InvocationTargetException e) { |
308 | 2172 | nbrodin | log.debug("Error opening this image with ", e);
|
309 | 140 | nbrodin | } |
310 | } |
||
311 | return null; |
||
312 | } |
||
313 | 134 | nbrodin | |
314 | 689 | nbrodin | public ArrayList<RasterDataParameters> createParametersList(String id) { |
315 | ArrayList<RasterDataParameters> list = new ArrayList<RasterDataParameters>(); |
||
316 | |||
317 | 2172 | nbrodin | Iterator<?> it = DALFileLocator.getFilesystemServerExplorerManager().getRegisteredProviders();
|
318 | 689 | nbrodin | while (it.hasNext()) {
|
319 | ExtensionPoint.Extension o = (ExtensionPoint.Extension)it.next(); |
||
320 | 2172 | nbrodin | Class<?> c = o.getExtension();
|
321 | 689 | nbrodin | try {
|
322 | 2172 | nbrodin | Constructor<?> hazNuevo = c.getConstructor();
|
323 | 689 | nbrodin | 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 | 2172 | nbrodin | log.debug("Error SecurityException in open", e);
|
343 | 689 | nbrodin | } catch (NoSuchMethodException e) { |
344 | 2172 | nbrodin | log.debug("Error NoSuchMethodException in open", e);
|
345 | 689 | nbrodin | } catch (IllegalArgumentException e) { |
346 | 2172 | nbrodin | log.debug("Error IllegalArgumentException in open", e);
|
347 | 689 | nbrodin | } catch (InstantiationException e) { |
348 | 2172 | nbrodin | log.debug("Error InstantiationException in open", e);
|
349 | 689 | nbrodin | } catch (IllegalAccessException e) { |
350 | 2172 | nbrodin | log.debug("Error IllegalAccessException in open", e);
|
351 | 689 | nbrodin | } catch (InvocationTargetException e) { |
352 | 2172 | nbrodin | log.debug("Error opening this image with", e);
|
353 | 689 | nbrodin | } |
354 | } |
||
355 | return list;
|
||
356 | } |
||
357 | |||
358 | 1816 | nbrodin | 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 | 134 | nbrodin | @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 | 1786 | nbrodin | |
378 | public String[] getRODriversExtensions() { |
||
379 | 1862 | nbrodin | return getReadOnlyFormatList();
|
380 | 1786 | nbrodin | } |
381 | 134 | nbrodin | |
382 | 1754 | nbrodin | public ArrayList<String> getExtensionsSupported(int dataType, int bands) throws RasterDriverException { |
383 | 134 | nbrodin | ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
384 | ExtensionPoint point = extensionPoints.get("RasterWriter");
|
||
385 | 2172 | nbrodin | Iterator<?> iterator = point.iterator();
|
386 | 135 | nbrodin | ArrayList<String> result = new ArrayList<String>(); |
387 | 134 | nbrodin | while (iterator.hasNext()) {
|
388 | 1754 | nbrodin | ExtensionPoint.Extension extension = (ExtensionPoint.Extension) iterator.next(); |
389 | 134 | nbrodin | String ext = extension.getName();
|
390 | try {
|
||
391 | if (isSupportedThisFileToWrite(ext, dataType, bands))
|
||
392 | 1754 | nbrodin | result.add(ext); |
393 | 134 | nbrodin | } catch (SecurityException e) { |
394 | throw new RasterDriverException("Error SecurityException in open"); |
||
395 | 1754 | nbrodin | } catch (IllegalArgumentException e) { |
396 | 134 | nbrodin | throw new RasterDriverException("Error IllegalArgumentException in open"); |
397 | } |
||
398 | } |
||
399 | return result;
|
||
400 | } |
||
401 | |||
402 | 2348 | nbrodin | @SuppressWarnings("rawtypes") |
403 | 134 | nbrodin | public String[] getWriteDriversType() { |
404 | if (fileFeature.size() == 0) |
||
405 | return null; |
||
406 | String[] list = new String[fileFeature.size()]; |
||
407 | 2172 | nbrodin | Set<?> values = fileFeature.entrySet();
|
408 | 134 | nbrodin | int i = 0; |
409 | 2172 | nbrodin | for (Iterator<?> it = values.iterator(); it.hasNext();) { |
410 | 134 | nbrodin | 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 | 144 | nbrodin | if (bandsSupported[0] != -1 && bandsSupported[0] < bands) |
437 | 134 | nbrodin | 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 | 135 | nbrodin | |
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 | 2172 | nbrodin | public void saveObjectToRmfFile(RmfBlocksManager blocksManager, Class<?> class1, Object value) throws RmfSerializerException { |
455 | 135 | nbrodin | 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 | throw new RmfSerializerException("Error al comprobar el fichero Rmf"); |
||
462 | |||
463 | blocksManager.addClient(serializerObject); |
||
464 | try {
|
||
465 | blocksManager.write(true);
|
||
466 | } catch (IOException e) { |
||
467 | throw new RmfSerializerException("Error al escribir el fichero Rmf", e); |
||
468 | } |
||
469 | blocksManager.removeAllClients(); |
||
470 | } |
||
471 | |||
472 | 2172 | nbrodin | public void saveObjectToRmfFile(String file, Class<?> class1, Object value) throws RmfSerializerException { |
473 | 135 | nbrodin | String fileRMF = RasterLocator.getManager().getFileUtils().getNameWithoutExtension(file) + ".rmf"; |
474 | RmfBlocksManager blocksManager = new RmfBlocksManager(fileRMF);
|
||
475 | saveObjectToRmfFile(blocksManager, class1, value); |
||
476 | } |
||
477 | |||
478 | public void saveObjectToRmfFile(String file, Object value) throws RmfSerializerException { |
||
479 | saveObjectToRmfFile(file, value.getClass(), value); |
||
480 | } |
||
481 | |||
482 | /**
|
||
483 | * Construye un serializador que soporte la clase class1. El constructor se invoca
|
||
484 | * con el parametro value.
|
||
485 | *
|
||
486 | * @param class1
|
||
487 | * @param value
|
||
488 | * @return
|
||
489 | * @throws RmfSerializerException
|
||
490 | */
|
||
491 | 2172 | nbrodin | public ClassSerializer getSerializerObject(Class<?> class1, Object value) throws RmfSerializerException { |
492 | 135 | nbrodin | try {
|
493 | 2172 | nbrodin | Class<?>[] args = { class1 }; |
494 | Constructor<?> constructor = getSerializerClass(class1).getConstructor(args);
|
||
495 | 135 | nbrodin | Object[] args2 = { value }; |
496 | return (ClassSerializer) constructor.newInstance(args2);
|
||
497 | } catch (Exception e) { |
||
498 | throw new RmfSerializerException("No se ha podido crear el serializador para el Rmf", e); |
||
499 | } |
||
500 | } |
||
501 | |||
502 | /**
|
||
503 | 1735 | nbrodin | * Gets the serializer which has a constructor with the class selected in the
|
504 | * parameters of this function
|
||
505 | 135 | nbrodin | * @throws RmfSerializerException
|
506 | */
|
||
507 | 2172 | nbrodin | private Class<?> getSerializerClass(Class<?> class1) |
508 | 135 | nbrodin | throws RmfSerializerException {
|
509 | ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
||
510 | ExtensionPoint point = extensionPoints.get("Serializer");
|
||
511 | 2172 | nbrodin | Iterator<?> iterator = point.iterator();
|
512 | 135 | nbrodin | while (iterator.hasNext()) {
|
513 | ExtensionPoint.Extension extension = (ExtensionPoint.Extension) iterator.next(); |
||
514 | if (extension != null) { |
||
515 | 2172 | nbrodin | Class<?>[] args = { class1 }; |
516 | Class<?> clase = extension.getExtension();
|
||
517 | 135 | nbrodin | try {
|
518 | clase.getConstructor(args); |
||
519 | return clase;
|
||
520 | } catch (Exception e) { |
||
521 | 1938 | nbrodin | // Si no encuentra un constructor para esta clase, seguir? buscando en los dem?s elementos
|
522 | 135 | nbrodin | } |
523 | } |
||
524 | } |
||
525 | throw new RmfSerializerException("No se ha encontrado la clase para el serializador"); |
||
526 | } |
||
527 | 1141 | nbrodin | |
528 | 1862 | nbrodin | @SuppressWarnings("deprecation") |
529 | 1141 | nbrodin | public void exportBufferToFile(Buffer bufResult, |
530 | double pixelSize,
|
||
531 | String extension,
|
||
532 | boolean alphaBand,
|
||
533 | File file,
|
||
534 | Extent extent, |
||
535 | ColorInterpretation colorInterpretation) throws NotSupportedExtensionException, RasterDriverException, ProcessInterruptedException, IOException { |
||
536 | //Escritura en disco del tile
|
||
537 | RasterManager rManager = RasterLocator.getManager(); |
||
538 | DataServerWriter dataWriter = RasterLocator.getManager().createDataServerWriter(); |
||
539 | dataWriter.setBuffer(bufResult, -1);
|
||
540 | Params params = rManager.createWriter("_." + extension).getParams();
|
||
541 | AffineTransform affineTransform = new AffineTransform(pixelSize, 0, 0, -pixelSize, |
||
542 | extent.getULX(), |
||
543 | extent.getULY()); |
||
544 | RasterWriter rw = rManager.createWriter(dataWriter, file.getAbsolutePath(), |
||
545 | bufResult.getBandCount(), affineTransform, bufResult.getWidth(), |
||
546 | bufResult.getHeight(), bufResult.getDataType(), params, null);
|
||
547 | |||
548 | if(colorInterpretation != null) { |
||
549 | String[] values = colorInterpretation.getValues(); |
||
550 | if(alphaBand) {
|
||
551 | String[] newValues = values; |
||
552 | boolean exists = false; |
||
553 | for (int i = 0; i < values.length; i++) { |
||
554 | if(values[i] != null && values[i].compareTo("Alpha") == 0) |
||
555 | exists = true;
|
||
556 | } |
||
557 | if(!exists) {
|
||
558 | newValues = new String[values.length + 1]; |
||
559 | for (int i = 0; i < values.length; i++) { |
||
560 | newValues[i] = values[i]; |
||
561 | } |
||
562 | newValues[newValues.length - 1] = "Alpha"; |
||
563 | } |
||
564 | rw.setColorBandsInterpretation(newValues); |
||
565 | } |
||
566 | |||
567 | } else {
|
||
568 | if(alphaBand) {
|
||
569 | String[] ci = new String[bufResult.getBandCount()]; |
||
570 | if(bufResult.getBandCount() == 4) { |
||
571 | ci[0] = "Red"; |
||
572 | ci[1] = "Green"; |
||
573 | ci[2] = "Blue"; |
||
574 | ci[3] = "Alpha"; |
||
575 | } else {
|
||
576 | for (int i = 0; i < bufResult.getBandCount(); i++) { |
||
577 | ci[i] = "Gray";
|
||
578 | } |
||
579 | } |
||
580 | rw.setColorBandsInterpretation(ci); |
||
581 | } |
||
582 | } |
||
583 | rw.dataWrite(); |
||
584 | rw.writeClose(); |
||
585 | } |
||
586 | 1862 | nbrodin | |
587 | public void addFormat(String ext, Class<?> c) { |
||
588 | 2348 | nbrodin | ArrayList<Class<?>> list = formatsRegistry.get(ext); |
589 | if(list == null) { |
||
590 | list = new ArrayList<Class<?>>(); |
||
591 | list.add(c); |
||
592 | formatsRegistry.put(ext, list); |
||
593 | } else {
|
||
594 | if(!list.contains(c))
|
||
595 | list.add(c); |
||
596 | } |
||
597 | 1862 | nbrodin | } |
598 | 2348 | nbrodin | |
599 | 1862 | nbrodin | public boolean isExtensionSupported(String ext, Class<?> c) { |
600 | 2348 | nbrodin | String extension = getManager().getFileUtils().getExtensionFromFileName(ext);
|
601 | |||
602 | ArrayList<Class<?>> list = formatsRegistry.get(extension); |
||
603 | if(list == null) { |
||
604 | return false; |
||
605 | } else {
|
||
606 | for (int i = 0; i < list.size(); i++) { |
||
607 | if(c == list.get(i))
|
||
608 | return true; |
||
609 | } |
||
610 | } |
||
611 | return false; |
||
612 | 1862 | nbrodin | } |
613 | 2348 | nbrodin | |
614 | 1862 | nbrodin | public boolean isExtensionSupported(String ext) { |
615 | 2348 | nbrodin | String extension = getManager().getFileUtils().getExtensionFromFileName(ext);
|
616 | |||
617 | ArrayList<Class<?>> list = formatsRegistry.get(extension); |
||
618 | if(list == null) { |
||
619 | return false; |
||
620 | } |
||
621 | |||
622 | return true; |
||
623 | 1862 | nbrodin | } |
624 | |||
625 | 2348 | nbrodin | public String[] getReadOnlyFormatList() { |
626 | return formatsRegistry.keySet().toArray(new String[0]); |
||
627 | } |
||
628 | |||
629 | 1862 | nbrodin | public void registerFileProvidersTiled(Class<?> provider) { |
630 | 2348 | nbrodin | if(!tiledProviders.contains(provider))
|
631 | tiledProviders.add(provider); |
||
632 | 1862 | nbrodin | } |
633 | |||
634 | public void registerTileProviderFormats(Class<?> c) { |
||
635 | 2348 | nbrodin | if(tileclass == null) { |
636 | if(RasterProvider.class.isAssignableFrom(c)) {
|
||
637 | List<Object> pList = getTiledProvidersInstance(); |
||
638 | for (int i = 0; i < pList.size(); i++) { |
||
639 | String[] fList = ((RasterProvider)pList.get(i)).getFormatList(); |
||
640 | for (int j = 0; j < fList.length; j++) { |
||
641 | String value = fList[j];
|
||
642 | addFormat(value, c); |
||
643 | } |
||
644 | } |
||
645 | } |
||
646 | tileclass = c; |
||
647 | } |
||
648 | 1862 | nbrodin | } |
649 | 1967 | nbrodin | |
650 | 2348 | nbrodin | private List<Object> getTiledProvidersInstance() { |
651 | List<Object> list = new ArrayList<Object>(); |
||
652 | for (int i = 0; i < tiledProviders.size(); i++) { |
||
653 | Class<?> provider = tiledProviders.get(i);
|
||
654 | try {
|
||
655 | Constructor<?> c = provider.getConstructor();
|
||
656 | list.add(c.newInstance()); |
||
657 | } catch (SecurityException e) { |
||
658 | log.info("Error SecurityException in open", e);
|
||
659 | } catch (NoSuchMethodException e) { |
||
660 | log.info("Error NoSuchMethodException in open", e);
|
||
661 | } catch (IllegalArgumentException e) { |
||
662 | log.info("Error IllegalArgumentException in open", e);
|
||
663 | } catch (InstantiationException e) { |
||
664 | log.info("Error InstantiationException in open", e);
|
||
665 | } catch (IllegalAccessException e) { |
||
666 | log.info("Error IllegalAccessException in open", e);
|
||
667 | } catch (InvocationTargetException e) { |
||
668 | log.info("Error in open. Problemas con las librer?as nativas.", e);
|
||
669 | } |
||
670 | } |
||
671 | return list;
|
||
672 | } |
||
673 | |||
674 | 1967 | nbrodin | public void registerOverviewBuilderImplementation(String id, OverviewBuilder ovBuilder) { |
675 | DefaultRasterManager.overviewBuilderRegistry.put(id, ovBuilder); |
||
676 | } |
||
677 | 1971 | nbrodin | |
678 | public void registerCRSUtilImplementation(CRSUtils crsUtil) { |
||
679 | DefaultRasterManager.crsUtilRegistry = crsUtil; |
||
680 | } |
||
681 | 134 | nbrodin | } |