gvsig-gdal / trunk / org.gvsig.gdal2 / org.gvsig.gdal2.prov / org.gvsig.gdal2.prov.raster / src / main / java / org / gvsig / raster / gdal / provider / RasterGdalLibrary.java @ 370
History | View | Annotate | Download (8.35 KB)
1 |
package org.gvsig.raster.gdal.provider; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Arrays; |
5 |
import java.util.List; |
6 |
|
7 |
import org.apache.commons.lang3.StringUtils; |
8 |
import org.gdal.gdal.Driver; |
9 |
import org.gdal.gdal.gdal; |
10 |
import org.gdal.ogr.ogr; |
11 |
import org.gvsig.fmap.dal.DALFileLibrary; |
12 |
import org.gvsig.fmap.dal.DALFileLocator; |
13 |
import org.gvsig.fmap.dal.DALLibrary; |
14 |
import org.gvsig.fmap.dal.FileHelper; |
15 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderFactory; |
16 |
import org.gvsig.fmap.dal.spi.DALSPILocator; |
17 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
18 |
import org.gvsig.metadata.exceptions.MetadataException; |
19 |
import org.gvsig.tools.ToolsLocator; |
20 |
import org.gvsig.tools.dynobject.DynStruct_v2; |
21 |
import org.gvsig.tools.dynobject.Tags; |
22 |
import org.gvsig.tools.library.AbstractLibrary; |
23 |
import org.gvsig.tools.library.LibraryException; |
24 |
|
25 |
/**
|
26 |
* Raster GDAL Provider Library to register provider commponents
|
27 |
*
|
28 |
* @author dmartinezizquierdo
|
29 |
*
|
30 |
*/
|
31 |
public class RasterGdalLibrary extends AbstractLibrary { |
32 |
|
33 |
/**
|
34 |
* Driver name that must not be used to open files
|
35 |
*/
|
36 |
public static final String[] EXCLUDED_OPEN_DRIVERS={"netCDF"}; |
37 |
/**
|
38 |
* Driver name that must not be used to create new files
|
39 |
*/
|
40 |
public static final String[] EXCLUDED_STORE_DRIVERS={}; |
41 |
|
42 |
/**
|
43 |
* This drivers will be not used by GDAL
|
44 |
*/
|
45 |
public static final String[] NOT_REGISTERED_DRIVERS={"PostGISRaster", "WMS", "WCS"}; |
46 |
|
47 |
|
48 |
@Override
|
49 |
public void doRegistration() { |
50 |
registerAsServiceOf(DALLibrary.class); |
51 |
require(DALFileLibrary.class); |
52 |
} |
53 |
|
54 |
@Override
|
55 |
protected void doInitialize() throws LibraryException { |
56 |
} |
57 |
|
58 |
@Override
|
59 |
protected void doPostInitialize() throws LibraryException { |
60 |
List<Throwable> exs = new ArrayList<Throwable>(); |
61 |
|
62 |
// initialize gdal drivers
|
63 |
try {
|
64 |
gdal.AllRegister(); |
65 |
ogr.RegisterAll(); |
66 |
} catch (RuntimeException e) { |
67 |
exs.add(e); |
68 |
} |
69 |
|
70 |
DataManagerProviderServices dataman = |
71 |
DALSPILocator.getDataManagerProviderServices(); |
72 |
|
73 |
//Register supported parameters
|
74 |
try {
|
75 |
FileHelper.registerParametersDefinition( |
76 |
RasterGdalFileStoreParameters.PARAMETERS_DEFINITION_NAME, |
77 |
RasterGdalFileStoreParameters.class, "RasterGdalParameters.xml");
|
78 |
} catch (Exception e) { |
79 |
exs.add(e); |
80 |
} |
81 |
|
82 |
try {
|
83 |
FileHelper.registerMetadataDefinition( |
84 |
AbstractFileRasterGdalStoreProvider.METADATA_DEFINITION_NAME, |
85 |
AbstractFileRasterGdalStoreProvider.class, "RasterGdalMetadata.xml");
|
86 |
} catch (MetadataException e) {
|
87 |
exs.add(e); |
88 |
} |
89 |
|
90 |
try {
|
91 |
FileHelper.registerParametersDefinition( |
92 |
NewRasterGdalStoreParameters.PARAMETERS_DEFINITION_NAME, |
93 |
NewRasterGdalStoreParameters.class, "NewRasterGdalStoreParameters.xml");
|
94 |
} catch (Exception e) { |
95 |
exs.add(e); |
96 |
} |
97 |
|
98 |
try {
|
99 |
FileHelper.registerParametersDefinition( |
100 |
RasterGdalStoreParameters.PARAMETERS_DEFINITION_NAME, |
101 |
RasterGdalStoreParameters.class, "RasterGdalParameters.xml");
|
102 |
} catch (Exception e) { |
103 |
exs.add(e); |
104 |
} |
105 |
|
106 |
List<String> notRegisteredDrivers=Arrays.asList(NOT_REGISTERED_DRIVERS); |
107 |
try {
|
108 |
for (int i = 0; i < gdal.GetDriverCount(); i++) { |
109 |
//For each driver must be a StoreProviderFactory and a FilesystemProviderFactory
|
110 |
Driver gdalDriver = gdal.GetDriver(i);
|
111 |
String driverName = gdalDriver.getShortName();
|
112 |
if (notRegisteredDrivers.contains(driverName)){
|
113 |
continue;
|
114 |
} |
115 |
|
116 |
String provName =driverName;
|
117 |
//RasterGdalStoreProvider.NAME + "_" + driverName;
|
118 |
String provDescription = AbstractFileRasterGdalStoreProvider.DESCRIPTION
|
119 |
+ " for driver " + driverName;
|
120 |
|
121 |
String openParams;
|
122 |
//Try to load specific params with file
|
123 |
try {
|
124 |
openParams=RasterGdalFileStoreParameters.PARAMETERS_DEFINITION_NAME+"_"+driverName;
|
125 |
FileHelper.registerParametersDefinition( |
126 |
openParams, |
127 |
RasterGdalFileStoreParameters.class, "RasterGdalParameters.xml");
|
128 |
// FileHelper.registerParametersDefinition(
|
129 |
// openParams,
|
130 |
// RasterGdalFileStoreParameters.class, "RasterGdalParameters.xml");
|
131 |
} catch (Exception e) { |
132 |
try {
|
133 |
//Try to load specific params without file
|
134 |
openParams=RasterGdalStoreParameters.PARAMETERS_DEFINITION_NAME+"_"+driverName;
|
135 |
FileHelper.registerParametersDefinition( |
136 |
openParams, |
137 |
RasterGdalStoreParameters.class, "RasterGdalParameters.xml");
|
138 |
} catch (Exception e1) { |
139 |
//If specific params are not found, generic ones with file are used
|
140 |
openParams=RasterGdalFileStoreParameters.PARAMETERS_DEFINITION_NAME; |
141 |
} |
142 |
|
143 |
} |
144 |
|
145 |
//The store provider factory is created
|
146 |
RasterGdalStoreProviderFactory providerFactory=null;
|
147 |
try {
|
148 |
if (!dataman.getStoreProviders().contains(provName)) {
|
149 |
providerFactory= new RasterGdalStoreProviderFactory(provName,
|
150 |
provDescription, |
151 |
gdalDriver, openParams); |
152 |
dataman.registerStoreProviderFactory(providerFactory); |
153 |
|
154 |
} |
155 |
} catch (RuntimeException e) { |
156 |
exs.add(e); |
157 |
} |
158 |
|
159 |
|
160 |
String newParams;
|
161 |
//Try to load specific params
|
162 |
try {
|
163 |
newParams=NewRasterGdalStoreParameters.PARAMETERS_DEFINITION_NAME+"_"+driverName;
|
164 |
FileHelper.registerParametersDefinition( |
165 |
newParams, |
166 |
NewRasterGdalStoreParameters.class, "NewRasterGdalStoreParameters.xml");
|
167 |
} catch (Exception e) { |
168 |
//If specific params are not found, generic ones are used
|
169 |
newParams=NewRasterGdalStoreParameters.PARAMETERS_DEFINITION_NAME; |
170 |
} |
171 |
|
172 |
// The FilesystemServerExplorerProvider Factory is created
|
173 |
FilesystemServerExplorerProviderFactory filesystemProviderFactory =null;
|
174 |
try {
|
175 |
if (providerFactory!=null){ |
176 |
filesystemProviderFactory = (FilesystemServerExplorerProviderFactory) |
177 |
new RasterGdalFileSystemServerProviderFactory(
|
178 |
providerFactory, newParams); |
179 |
|
180 |
DALFileLocator.getFilesystemServerExplorerManager() |
181 |
.registerProviderFactory(filesystemProviderFactory); |
182 |
} |
183 |
} catch (RuntimeException e) { |
184 |
exs.add(e); |
185 |
} |
186 |
|
187 |
//The valid extensions from the parameters are added to the filesystem server factory
|
188 |
String validExtensions=null; |
189 |
DynStruct_v2 params = (DynStruct_v2)ToolsLocator.getPersistenceManager().getDefinition(openParams); |
190 |
Tags paramsTags=params.getTags(); |
191 |
if (paramsTags.has(RasterGdalFileStoreParameters.VALID_EXTENSIONS_TAG_NAME)){
|
192 |
validExtensions=(String)paramsTags.get(RasterGdalFileStoreParameters.VALID_EXTENSIONS_TAG_NAME);
|
193 |
} |
194 |
if (filesystemProviderFactory!=null && |
195 |
filesystemProviderFactory instanceof RasterGdalFileSystemServerProviderFactory &&
|
196 |
StringUtils.isNotEmpty(validExtensions)){ |
197 |
|
198 |
String[] extensions=validExtensions.split(","); |
199 |
((RasterGdalFileSystemServerProviderFactory)filesystemProviderFactory). |
200 |
addAcceptedExtensions(extensions); |
201 |
} |
202 |
} |
203 |
|
204 |
} catch (RuntimeException e) { |
205 |
exs.add(e); |
206 |
} |
207 |
|
208 |
if (exs.size() > 0) { |
209 |
throw new LibraryException(this.getClass(), exs); |
210 |
} |
211 |
|
212 |
} |
213 |
|
214 |
} |