gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / dataObjects / FileTools.java @ 469
History | View | Annotate | Download (6.35 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2012 gvSIG Association.
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.geoprocess.lib.sextante.dataObjects; |
25 |
|
26 |
import java.io.File; |
27 |
|
28 |
import es.unex.sextante.core.Sextante; |
29 |
|
30 |
import org.cresques.cts.IProjection; |
31 |
import org.gvsig.app.ApplicationLocator; |
32 |
import org.gvsig.app.project.ProjectManager; |
33 |
import org.gvsig.app.project.ProjectPreferences; |
34 |
import org.gvsig.app.project.documents.table.TableDocument; |
35 |
import org.gvsig.app.project.documents.table.TableManager; |
36 |
import org.gvsig.fmap.dal.DALLocator; |
37 |
import org.gvsig.fmap.dal.DataManager; |
38 |
import org.gvsig.fmap.dal.DataStoreParameters; |
39 |
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters; |
40 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
41 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
42 |
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters; |
43 |
import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider; |
44 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
45 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
46 |
|
47 |
public class FileTools { |
48 |
|
49 |
public final static String[] RASTER3D_EXT_IN = { "asc3d" }; |
50 |
|
51 |
public final static String[] RASTER_EXT_IN = { "tif", "asc", "dat", "tiff", |
52 |
"bmp", "gif", "img", "jpg", "png", "vrt", "lan", "gis", "pix", "aux", |
53 |
"adf", "mpr", "mpl", "map", "hdr" }; |
54 |
public final static String[] RASTER_DRIVERS_IN = { "RasterStore", |
55 |
"RasterStore", "RasterStore", "RasterStore", "RasterStore", |
56 |
"RasterStore", "RasterStore", "RasterStore", "RasterStore", |
57 |
"RasterStore", "RasterStore", "RasterStore", "RasterStore", |
58 |
"RasterStore", "RasterStore", "RasterStore", "RasterStore", |
59 |
"RasterStore", "RasterStore" }; |
60 |
|
61 |
public final static String[] VECTOR_EXT_IN = { "shp", "gml", "dxf", "dgn", |
62 |
"dwg" };
|
63 |
public final static String[] VECTOR_DRIVERS_IN = { "GML", "DXF", "DGN", |
64 |
"DWG" };
|
65 |
|
66 |
public final static String[] TABLE_EXT = { "dbf" }; |
67 |
|
68 |
public static final String[] LAYERS_EXT_IN = { "tif", "asc", "dat", "tiff", |
69 |
"bmp", "gif", "img", "jpg", "png", "vrt", "lan", "gis", "pix", "aux", |
70 |
"adf", "mpr", "mpl", "map", "shp", "gml", "dxf", "dgn", "dwg" }; |
71 |
public static final String[] LAYER_DRIVERS_IN = { "Gdal Store", |
72 |
"Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store", |
73 |
"Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store", |
74 |
"Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store", |
75 |
"Gdal Store", "Gdal Store", "Shape", "GML", "DXF", "DGN", "DWG" }; |
76 |
public static final boolean[] LAYER_RASTER = { true, true, true, true, |
77 |
true, true, true, true, true, true, true, true, true, true, true, true, |
78 |
true, true, false, false, false, false, false, }; |
79 |
|
80 |
public static FLayer openLayer(final String sFilename, |
81 |
final String sName, final IProjection projection) { |
82 |
|
83 |
final String sExtension = |
84 |
sFilename.substring(sFilename.lastIndexOf('.') + 1, |
85 |
sFilename.length()); |
86 |
|
87 |
for (int i = 0; i < LAYERS_EXT_IN.length; i++) { |
88 |
if (sExtension.equals(LAYERS_EXT_IN[i])) {
|
89 |
try {
|
90 |
FLayer layer; |
91 |
final DataManager dm = DALLocator.getDataManager();
|
92 |
final DataStoreParameters params =
|
93 |
dm.createStoreParameters(FileTools.LAYER_DRIVERS_IN[i]); |
94 |
((FilesystemStoreParameters) params).setFile(new File(sFilename)); |
95 |
|
96 |
if(!(params instanceof RasterDataParameters)) { |
97 |
params.setDynValue("crs", projection);
|
98 |
} |
99 |
layer = MapContextLocator.getMapContextManager().createLayer(sName, params); |
100 |
|
101 |
if ((layer != null) && layer.isAvailable()) { |
102 |
return layer;
|
103 |
} else {
|
104 |
return null; |
105 |
} |
106 |
} catch (final Exception e) { |
107 |
Sextante.addErrorToLog(e); |
108 |
return null; |
109 |
} |
110 |
} |
111 |
} |
112 |
return null; |
113 |
} |
114 |
|
115 |
public static TableDocument openTable(final String sFilename, |
116 |
final String sName) { |
117 |
final DataManager dm = DALLocator.getDataManager();
|
118 |
try {
|
119 |
DataStoreParameters storeParameters = |
120 |
dm.createStoreParameters(DBFStoreProvider.NAME); |
121 |
storeParameters.setDynValue( |
122 |
DBFStoreParameters.DBFFILE_PARAMTER_NAME, sFilename); |
123 |
final FeatureStore store =
|
124 |
(FeatureStore) dm.openStore(DBFStoreProvider.NAME, |
125 |
storeParameters); |
126 |
final TableDocument pt =
|
127 |
(TableDocument) ProjectManager.getInstance().createDocument( |
128 |
TableManager.TYPENAME, sName); |
129 |
pt.setStore(store); |
130 |
return pt;
|
131 |
} catch (final Exception e) { |
132 |
Sextante.addErrorToLog(e); |
133 |
return null; |
134 |
} |
135 |
} |
136 |
|
137 |
public static Object open(final String sFilename) { |
138 |
|
139 |
final IProjection projection =
|
140 |
((ProjectPreferences) ApplicationLocator.getManager() |
141 |
.getPreferences("project")).getDefaultProjection();
|
142 |
FLayer layer = openLayer(sFilename, sFilename, projection); |
143 |
if (layer != null) { |
144 |
return layer;
|
145 |
} |
146 |
|
147 |
final TableDocument table = openTable(sFilename, sFilename);
|
148 |
|
149 |
return table;
|
150 |
} |
151 |
|
152 |
} |