gvsig-gdal / branches / org.gvsig.gdal-2018a / org.gvsig.gdal.prov / org.gvsig.gdal.raster / org.gvsig.gdal.raster.provider / src / main / java / org / gvsig / raster / gdal / provider / RasterGdalFileSystemServerProvider.java @ 361
History | View | Annotate | Download (7.07 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 extends AbstractFilesystemServerExplorerProvider |
57 |
implements FilesystemServerExplorerProvider, ResourceConsumer{
|
58 |
|
59 |
private static final Logger logger = LoggerFactory.getLogger(RasterGdalFileSystemServerProvider.class); |
60 |
protected FilesystemServerExplorerProviderServices serverExplorer;
|
61 |
private RasterGdalFileSystemServerProviderFactory factory;
|
62 |
//private ArrayList<String> acceptedExtensions;
|
63 |
|
64 |
RasterGdalFileSystemServerProvider (RasterGdalFileSystemServerProviderFactory factory){ |
65 |
this.factory=factory;
|
66 |
} |
67 |
|
68 |
public int getMode() { |
69 |
return factory.getMode();
|
70 |
} |
71 |
|
72 |
@Override
|
73 |
public boolean canCreate() { |
74 |
return factory.allowCreate()==FilesystemProviderServices.YES;
|
75 |
} |
76 |
|
77 |
@Override
|
78 |
public boolean canCreate(NewDataStoreParameters newDataStoreParameters) { |
79 |
if (canCreate()){
|
80 |
if (newDataStoreParameters instanceof NewRasterGdalStoreParameters){ |
81 |
if ( newDataStoreParameters.isValid() ){
|
82 |
Driver gdalDriver=factory.getProviderFactory().getDriver();
|
83 |
|
84 |
Buffer buffer=((NewRasterGdalStoreParameters)newDataStoreParameters).getBuffer();
|
85 |
String creationTypes=(String)gdalDriver.GetMetadata_Dict().get(gdalconstConstants.GDAL_DMD_CREATIONDATATYPES); |
86 |
if (creationTypes == null || (creationTypes!=null && creationTypes.contains(getBufferGDALDataType(buffer)))){ |
87 |
return true; |
88 |
} |
89 |
} |
90 |
} |
91 |
} |
92 |
return false; |
93 |
} |
94 |
|
95 |
private String getBufferGDALDataType(Buffer buffer){ |
96 |
int[] bufferTypes=buffer.getBandTypes(); |
97 |
int type=BufferManager.TYPE_UNDEFINED;
|
98 |
for (int i = 0; i < bufferTypes.length; i++) { |
99 |
if(type==BufferManager.TYPE_UNDEFINED){
|
100 |
type = bufferTypes[i]; |
101 |
} else if(type != bufferTypes[i]){ |
102 |
type = BufferManager.TYPE_UNDEFINED; |
103 |
break;
|
104 |
} |
105 |
} |
106 |
//Byte UInt16 Int16 UInt32 Int32 Float32 Float64 CInt16 CInt32 CFloat32 CFloat64
|
107 |
switch (type) {
|
108 |
case BufferManager.TYPE_BYTE:
|
109 |
return "Byte"; |
110 |
|
111 |
case BufferManager.TYPE_SHORT:
|
112 |
return "Int16"; |
113 |
|
114 |
case BufferManager.TYPE_USHORT:
|
115 |
return "UInt16"; // OJO |
116 |
|
117 |
case BufferManager.TYPE_INT:
|
118 |
return "Int32"; |
119 |
|
120 |
case BufferManager.TYPE_FLOAT:
|
121 |
return "Float32"; |
122 |
|
123 |
case BufferManager.TYPE_DOUBLE:
|
124 |
return "Float64"; |
125 |
|
126 |
default:
|
127 |
return "Unsupported"; |
128 |
} |
129 |
|
130 |
} |
131 |
|
132 |
@Override
|
133 |
public void create(NewDataStoreParameters params, boolean overwrite) |
134 |
throws CreateException {
|
135 |
if ( !canCreate(params) ){
|
136 |
throw new CreateException(params.getDataStoreName(), new IllegalArgumentException()); |
137 |
} |
138 |
if (params instanceof NewRasterGdalStoreParameters){ |
139 |
RasterGdalStoreProviderFactory providerFactory = factory.getProviderFactory(); |
140 |
AbstractFileRasterGdalStoreProvider provider; |
141 |
try {
|
142 |
provider = (AbstractFileRasterGdalStoreProvider) providerFactory.createProvider(params); |
143 |
} catch (InitializeException e) {
|
144 |
throw new CreateException(params.getDataStoreName(), e); |
145 |
} |
146 |
provider.store(overwrite); |
147 |
DisposeUtils.dispose(provider); |
148 |
} |
149 |
} |
150 |
|
151 |
@Override
|
152 |
public NewDataStoreParameters getCreateParameters() throws DataException { |
153 |
return new NewRasterGdalStoreParameters(factory.getNewParamsDefinitionName(), factory.getName()); |
154 |
} |
155 |
|
156 |
@Override
|
157 |
public void initialize(FilesystemServerExplorerProviderServices serverExplorer) { |
158 |
this.serverExplorer = serverExplorer;
|
159 |
} |
160 |
|
161 |
@Override
|
162 |
public void remove(DataStoreParameters parameters) throws RemoveException { |
163 |
RasterGdalFileStoreParameters params = (RasterGdalFileStoreParameters) parameters; |
164 |
File file = params.getFile();
|
165 |
if (!file.exists()) {
|
166 |
throw new RemoveException(this.getDataStoreProviderName(), |
167 |
new FileNotFoundException(params.getFile())); |
168 |
} |
169 |
if (!file.delete()) {
|
170 |
throw new RemoveException(this.getDataStoreProviderName(), |
171 |
new IOException("Error deleting file: "+file.getName())); |
172 |
} |
173 |
|
174 |
} |
175 |
|
176 |
@Override
|
177 |
public String getDataStoreProviderName() { |
178 |
return factory.getName();
|
179 |
} |
180 |
|
181 |
@Override
|
182 |
public String getDescription() { |
183 |
return factory.getDescription();
|
184 |
} |
185 |
|
186 |
@Override
|
187 |
public boolean accept(File pathname) { |
188 |
return factory.accept(pathname);
|
189 |
} |
190 |
|
191 |
@Override
|
192 |
public boolean closeResourceRequested(ResourceProvider resource) { |
193 |
return !(this.equals(resource)); |
194 |
} |
195 |
|
196 |
@Override
|
197 |
public void resourceChanged(ResourceProvider resource) { |
198 |
//Do nothing
|
199 |
} |
200 |
|
201 |
} |