Statistics
| Revision:

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 @ 245

History | View | Annotate | Download (6.43 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

    
32
import org.gvsig.app.ApplicationLocator;
33
import org.gvsig.app.project.ProjectManager;
34
import org.gvsig.app.project.ProjectPreferences;
35
import org.gvsig.app.project.documents.table.TableDocument;
36
import org.gvsig.app.project.documents.table.TableManager;
37
import org.gvsig.fmap.dal.DALLocator;
38
import org.gvsig.fmap.dal.DataManager;
39
import org.gvsig.fmap.dal.DataStoreParameters;
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(
95
                        sFilename));
96

    
97
                    if (LAYER_RASTER[i]) {
98
                        params.setDynValue("SRS", projection);
99
                    } else {
100
                        params.setDynValue("crs", projection);
101
                    }
102
                    layer =
103
                        MapContextLocator.getMapContextManager().createLayer(
104
                            sName, params);
105

    
106
                    if ((layer != null) && layer.isAvailable()) {
107
                        return layer;
108
                    } else {
109
                        return null;
110
                    }
111
                } catch (final Exception e) {
112
                    Sextante.addErrorToLog(e);
113
                    return null;
114
                }
115
            }
116
        }
117
        return null;
118
    }
119

    
120
    public static TableDocument openTable(final String sFilename,
121
        final String sName) {
122
        final DataManager dm = DALLocator.getDataManager();
123
        try {
124
            DataStoreParameters storeParameters =
125
                dm.createStoreParameters(DBFStoreProvider.NAME);
126
            storeParameters.setDynValue(
127
                DBFStoreParameters.DBFFILE_PARAMTER_NAME, sFilename);
128
            final FeatureStore store =
129
                (FeatureStore) dm.openStore(DBFStoreProvider.NAME,
130
                    storeParameters);
131
            final TableDocument pt =
132
                (TableDocument) ProjectManager.getInstance().createDocument(
133
                    TableManager.TYPENAME, sName);
134
            pt.setStore(store);
135
            return pt;
136
        } catch (final Exception e) {
137
            Sextante.addErrorToLog(e);
138
            return null;
139
        }
140
    }
141

    
142
    public static Object open(final String sFilename) {
143

    
144
        final IProjection projection =
145
            ((ProjectPreferences) ApplicationLocator.getManager()
146
                .getPreferences("project")).getDefaultProjection();
147
        FLayer layer = openLayer(sFilename, sFilename, projection);
148
        if (layer != null) {
149
            return layer;
150
        }
151

    
152
        final TableDocument table = openTable(sFilename, sFilename);
153

    
154
        return table;
155
    }
156

    
157
}