gvsig-gdal / trunk / org.gvsig.gdal2 / org.gvsig.gdal2.prov / org.gvsig.gdal2.prov.raster / src / main / java / org / gvsig / raster / gdal / provider / RasterGdalFileSystemServerProvider.java @ 370
History | View | Annotate | Download (7.09 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2016 gvSIG Association
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.raster.gdal.provider; |
24 |
|
25 |
import java.io.File; |
26 |
import java.io.IOException; |
27 |
|
28 |
import org.gdal.gdal.Driver; |
29 |
import org.gdal.gdalconst.gdalconstConstants; |
30 |
|
31 |
import org.gvsig.fmap.dal.DataStoreParameters; |
32 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
33 |
import org.gvsig.fmap.dal.exception.CreateException; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.exception.FileNotFoundException; |
36 |
import org.gvsig.fmap.dal.exception.InitializeException; |
37 |
import org.gvsig.fmap.dal.exception.RemoveException; |
38 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
39 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
40 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider; |
41 |
//import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemProviderServices;
|
42 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
43 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices; |
44 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
45 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
46 |
import org.gvsig.tools.dispose.DisposeUtils; |
47 |
|
48 |
import org.slf4j.Logger; |
49 |
import org.slf4j.LoggerFactory; |
50 |
|
51 |
/**
|
52 |
* Filesystem Provider for Raster GDAL.
|
53 |
* @author dmartinezizquierdo
|
54 |
*
|
55 |
*/
|
56 |
public class RasterGdalFileSystemServerProvider |
57 |
extends AbstractFilesystemServerExplorerProvider
|
58 |
implements FilesystemServerExplorerProvider, ResourceConsumer{
|
59 |
|
60 |
private static final Logger logger = LoggerFactory.getLogger(RasterGdalFileSystemServerProvider.class); |
61 |
protected FilesystemServerExplorerProviderServices serverExplorer;
|
62 |
private RasterGdalFileSystemServerProviderFactory factory;
|
63 |
//private ArrayList<String> acceptedExtensions;
|
64 |
|
65 |
RasterGdalFileSystemServerProvider (RasterGdalFileSystemServerProviderFactory factory){ |
66 |
this.factory=factory;
|
67 |
} |
68 |
|
69 |
public int getMode() { |
70 |
return factory.getMode();
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
public boolean canCreate() { |
75 |
return factory.allowCreate()==FilesystemProviderServices.YES;
|
76 |
} |
77 |
|
78 |
@Override
|
79 |
public boolean canCreate(NewDataStoreParameters newDataStoreParameters) { |
80 |
if (canCreate()){
|
81 |
if (newDataStoreParameters instanceof NewRasterGdalStoreParameters){ |
82 |
if ( newDataStoreParameters.isValid() ){
|
83 |
Driver gdalDriver=factory.getProviderFactory().getDriver();
|
84 |
|
85 |
Buffer buffer=((NewRasterGdalStoreParameters)newDataStoreParameters).getBuffer();
|
86 |
String creationTypes=(String)gdalDriver.GetMetadata_Dict().get(gdalconstConstants.GDAL_DMD_CREATIONDATATYPES); |
87 |
if (creationTypes == null || (creationTypes!=null && creationTypes.contains(getBufferGDALDataType(buffer)))){ |
88 |
return true; |
89 |
} |
90 |
} |
91 |
} |
92 |
} |
93 |
return false; |
94 |
} |
95 |
|
96 |
private String getBufferGDALDataType(Buffer buffer){ |
97 |
int[] bufferTypes=buffer.getBandTypes(); |
98 |
int type=BufferManager.TYPE_UNDEFINED;
|
99 |
for (int i = 0; i < bufferTypes.length; i++) { |
100 |
if(type==BufferManager.TYPE_UNDEFINED){
|
101 |
type = bufferTypes[i]; |
102 |
} else if(type != bufferTypes[i]){ |
103 |
type = BufferManager.TYPE_UNDEFINED; |
104 |
break;
|
105 |
} |
106 |
} |
107 |
//Byte UInt16 Int16 UInt32 Int32 Float32 Float64 CInt16 CInt32 CFloat32 CFloat64
|
108 |
switch (type) {
|
109 |
case BufferManager.TYPE_BYTE:
|
110 |
return "Byte"; |
111 |
|
112 |
case BufferManager.TYPE_SHORT:
|
113 |
return "Int16"; |
114 |
|
115 |
case BufferManager.TYPE_USHORT:
|
116 |
return "UInt16"; // OJO |
117 |
|
118 |
case BufferManager.TYPE_INT:
|
119 |
return "Int32"; |
120 |
|
121 |
case BufferManager.TYPE_FLOAT:
|
122 |
return "Float32"; |
123 |
|
124 |
case BufferManager.TYPE_DOUBLE:
|
125 |
return "Float64"; |
126 |
|
127 |
default:
|
128 |
return "Unsupported"; |
129 |
} |
130 |
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
public void create(NewDataStoreParameters params, boolean overwrite) |
135 |
throws CreateException {
|
136 |
if ( !canCreate(params) ){
|
137 |
throw new CreateException(params.getDataStoreName(), new IllegalArgumentException()); |
138 |
} |
139 |
if (params instanceof NewRasterGdalStoreParameters){ |
140 |
RasterGdalStoreProviderFactory providerFactory = factory.getProviderFactory(); |
141 |
AbstractFileRasterGdalStoreProvider provider; |
142 |
try {
|
143 |
provider = (AbstractFileRasterGdalStoreProvider) providerFactory.createProvider(params); |
144 |
} catch (InitializeException e) {
|
145 |
throw new CreateException(params.getDataStoreName(), e); |
146 |
} |
147 |
provider.store(overwrite); |
148 |
DisposeUtils.dispose(provider); |
149 |
} |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public NewDataStoreParameters getCreateParameters() throws DataException { |
154 |
return new NewRasterGdalStoreParameters(factory.getNewParamsDefinitionName(), factory.getName()); |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public void initialize(FilesystemServerExplorerProviderServices serverExplorer) { |
159 |
this.serverExplorer = serverExplorer;
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
public void remove(DataStoreParameters parameters) throws RemoveException { |
164 |
RasterGdalFileStoreParameters params = (RasterGdalFileStoreParameters) parameters; |
165 |
File file = params.getFile();
|
166 |
if (!file.exists()) {
|
167 |
throw new RemoveException(this.getDataStoreProviderName(), |
168 |
new FileNotFoundException(params.getFile())); |
169 |
} |
170 |
if (!file.delete()) {
|
171 |
throw new RemoveException(this.getDataStoreProviderName(), |
172 |
new IOException("Error deleting file: "+file.getName())); |
173 |
} |
174 |
|
175 |
} |
176 |
|
177 |
@Override
|
178 |
public String getDataStoreProviderName() { |
179 |
return factory.getName();
|
180 |
} |
181 |
|
182 |
@Override
|
183 |
public String getDescription() { |
184 |
return factory.getDescription();
|
185 |
} |
186 |
|
187 |
@Override
|
188 |
public boolean accept(File pathname) { |
189 |
return factory.accept(pathname);
|
190 |
} |
191 |
|
192 |
@Override
|
193 |
public boolean closeResourceRequested(ResourceProvider resource) { |
194 |
return !(this.equals(resource)); |
195 |
} |
196 |
|
197 |
@Override
|
198 |
public void resourceChanged(ResourceProvider resource) { |
199 |
//Do nothing
|
200 |
} |
201 |
|
202 |
} |