Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.extension / src / main / java / org / gvsig / geoprocess / core / FileTools.java @ 172

History | View | Annotate | Download (4.95 KB)

1
package org.gvsig.geoprocess.core;
2

    
3
import java.io.File;
4

    
5
import es.unex.sextante.core.Sextante;
6

    
7
import org.cresques.cts.IProjection;
8

    
9
import org.gvsig.app.ApplicationLocator;
10
import org.gvsig.app.project.ProjectManager;
11
import org.gvsig.app.project.ProjectPreferences;
12
import org.gvsig.app.project.documents.table.TableDocument;
13
import org.gvsig.app.project.documents.table.TableManager;
14
import org.gvsig.fmap.dal.DALLocator;
15
import org.gvsig.fmap.dal.DataManager;
16
import org.gvsig.fmap.dal.DataStoreParameters;
17
import org.gvsig.fmap.dal.feature.FeatureStore;
18
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
19
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters;
20
import org.gvsig.fmap.mapcontext.MapContextLocator;
21
import org.gvsig.fmap.mapcontext.layers.FLayer;
22

    
23

    
24
public class FileTools {
25

    
26
    public final static String[] RASTER3D_EXT_IN = { "asc3d" };
27

    
28
    public final static String[] RASTER_EXT_IN = { "tif", "asc", "dat", "tiff",
29
        "bmp", "gif", "img", "jpg", "png", "vrt", "lan", "gis", "pix", "aux",
30
        "adf", "mpr", "mpl", "map", "hdr" };
31
    public final static String[] RASTER_DRIVERS_IN = { "RasterStore",
32
        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
33
        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
34
        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
35
        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
36
        "RasterStore", "RasterStore" };
37

    
38
    public final static String[] VECTOR_EXT_IN = { "shp", "gml", "dxf", "dgn",
39
        "dwg" };
40
    public final static String[] VECTOR_DRIVERS_IN = { "GML", "DXF", "DGN",
41
        "DWG" };
42

    
43
    public final static String[] TABLE_EXT = { "dbf" };
44

    
45
    public static final String[] LAYERS_EXT_IN = { "tif", "asc", "dat", "tiff",
46
        "bmp", "gif", "img", "jpg", "png", "vrt", "lan", "gis", "pix", "aux",
47
        "adf", "mpr", "mpl", "map", "shp", "gml", "dxf", "dgn", "dwg" };
48
    public static final String[] LAYER_DRIVERS_IN = { "RasterStore",
49
        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
50
        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
51
        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
52
        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
53
        "RasterStore", "Shape", "GML", "DXF", "DGN", "DWG" };
54

    
55

    
56
    public static FLayer openLayer(final String sFilename, final String sName,
57
        final IProjection projection) {
58

    
59
        final String sExtension =
60
            sFilename.substring(sFilename.lastIndexOf('.') + 1,
61
                sFilename.length());
62

    
63
        for (int i = 0; i < LAYERS_EXT_IN.length; i++) {
64
            if (sExtension.equals(LAYERS_EXT_IN[i])) {
65
                try {
66
                    FLayer layer;
67
                    final DataManager dm = DALLocator.getDataManager();
68
                    final DataStoreParameters params =
69
                        dm.createStoreParameters(FileTools.LAYER_DRIVERS_IN[i]);
70
                    ((FilesystemStoreParameters) params).setFile(new File(
71
                        sFilename));
72

    
73
                    params.setDynValue("crs", projection);
74
                    layer =
75
                        MapContextLocator.getMapContextManager().createLayer(
76
                            sName, params);
77

    
78
                    if ((layer != null) && layer.isAvailable()) {
79
                        return layer;
80
                    } else {
81
                        return null;
82
                    }
83
                } catch (final Exception e) {
84
                    Sextante.addErrorToLog(e);
85
                    return null;
86
                }
87
            }
88
        }
89
        return null;
90
    }
91

    
92

    
93
    public static TableDocument openTable(final String sFilename,
94
        final String sName) {
95
        final DBFStoreParameters dbfParams = new DBFStoreParameters();
96
        dbfParams.setDBFFile(sFilename);
97
        final DataManager dm = DALLocator.getDataManager();
98
        try {
99
            final FeatureStore store =
100
                (FeatureStore) dm.openStore(dbfParams.getDataStoreName(),
101
                    dbfParams);
102
            final TableDocument pt =
103
                (TableDocument) ProjectManager.getInstance().createDocument(
104
                    TableManager.TYPENAME, sName);
105
            pt.setStore(store);
106
            return pt;
107
        } catch (final Exception e) {
108
            Sextante.addErrorToLog(e);
109
            return null;
110
        }
111

    
112
    }
113

    
114

    
115
    public static Object open(final String sFilename) {
116

    
117
        final IProjection projection =
118
            ((ProjectPreferences) ApplicationLocator.getManager()
119
                .getPreferences("project")).getDefaultProjection();
120
        final FLayer layer = openLayer(sFilename, sFilename, projection);
121
        if (layer != null) {
122
            return layer;
123
        }
124

    
125
        final TableDocument table = openTable(sFilename, sFilename);
126

    
127
        return table;
128
    }
129

    
130
}