Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / branches / refactor-2018 / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / dataObjects / FileTools.java @ 1055

History | View | Annotate | Download (6.62 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
import java.util.logging.Level;
30
import java.util.logging.Logger;
31

    
32
import org.cresques.cts.IProjection;
33
import org.gvsig.app.ApplicationLocator;
34
import org.gvsig.app.project.ProjectManager;
35
import org.gvsig.app.project.ProjectPreferences;
36
import org.gvsig.app.project.documents.table.TableDocument;
37
import org.gvsig.app.project.documents.table.TableManager;
38
import org.gvsig.fmap.dal.DALLocator;
39
import org.gvsig.fmap.dal.DataManager;
40
import org.gvsig.fmap.dal.DataServerExplorer;
41
import org.gvsig.fmap.dal.DataServerExplorerParameters;
42
import org.gvsig.fmap.dal.DataStore;
43
import org.gvsig.fmap.dal.DataStoreParameters;
44
import org.gvsig.fmap.dal.NewDataStoreParameters;
45
import org.gvsig.fmap.dal.exception.DataException;
46
import org.gvsig.fmap.dal.exception.InitializeException;
47
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
48
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
49
import org.gvsig.fmap.dal.feature.FeatureStore;
50
import org.gvsig.fmap.dal.raster.api.NewRasterStoreParameters;
51
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
52
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
53
import org.gvsig.fmap.mapcontext.MapContextLocator;
54
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
55
import org.gvsig.fmap.mapcontext.layers.FLayer;
56

    
57
public class FileTools {
58

    
59
    public final static String[] RASTER3D_EXT_IN = { "asc3d" };
60

    
61
    public final static String[] RASTER_EXT_IN = { "tif", "asc", "dat", "tiff",
62
        "bmp", "gif", "img", "jpg", "png", "vrt", "lan", "gis", "pix", "aux",
63
        "adf", "mpr", "mpl", "map", "hdr" };
64
//    public final static String[] RASTER_DRIVERS_IN = { "RasterStore",
65
//        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
66
//        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
67
//        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
68
//        "RasterStore", "RasterStore", "RasterStore", "RasterStore",
69
//        "RasterStore", "RasterStore" };
70

    
71
    public final static String[] VECTOR_EXT_IN = { "shp", "gml", "dxf", "dgn",
72
        "dwg" };
73
    public final static String[] VECTOR_DRIVERS_IN = { "GML", "DXF", "DGN",
74
        "DWG" };
75

    
76
    public final static String[] TABLE_EXT = { "dbf" };
77

    
78
    public static final String[] LAYERS_EXT_IN = { "tif", "asc", "dat", "tiff",
79
        "bmp", "gif", "img", "jpg", "png", "vrt", "lan", "gis", "pix", "aux",
80
        "adf", "mpr", "mpl", "map", "shp", "gml", "dxf", "dgn", "dwg" };
81
//    public static final String[] LAYER_DRIVERS_IN = { "Gdal Store",
82
//        "Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store",
83
//        "Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store",
84
//        "Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store", "Gdal Store",
85
//        "Gdal Store", "Gdal Store", "Shape", "GML", "DXF", "DGN", "DWG" };
86
//    public static final boolean[] LAYER_RASTER = { true, true, true, true,
87
//        true, true, true, true, true, true, true, true, true, true, true, true,
88
//        true, true, false, false, false, false, false, };
89

    
90
    public static FLayer openLayer(final String sFilename,
91
            final String sName, final IProjection projection) {
92

    
93
        try {
94

    
95
            final DataManager dm = DALLocator.getDataManager();
96
            DataServerExplorerParameters serverParameters = dm.createServerExplorerParameters(FilesystemServerExplorer.NAME);
97
            FilesystemServerExplorer server = (FilesystemServerExplorer) dm.openServerExplorer(FilesystemServerExplorer.NAME, serverParameters);
98
            File file = new File(sFilename);
99
            DataStoreParameters parameters = server.createStoreParameters(file);
100
            if (!(parameters instanceof NewRasterStoreParameters)) {
101
                parameters.setDynValue("crs", projection);
102
            }
103
            DataStore store = dm.openStore(parameters.getDataStoreName(), parameters);
104
            FLayer layer = MapContextLocator.getMapContextManager().createLayer(sName, store);
105
            if ((layer != null) && layer.isAvailable()) {
106
                return layer;
107
            } else {
108
                return null;
109
            }
110
        } catch (Exception e) {
111
            Sextante.addErrorToLog(e);
112
        }
113
        return null;
114
    }
115

    
116
    public static TableDocument openTable(final String sFilename,
117
        final String sName) {
118
        final DataManager dm = DALLocator.getDataManager();
119
        try {
120
            DataStoreParameters storeParameters =
121
                dm.createStoreParameters("DBF");
122
            storeParameters.setDynValue("dbfFile", sFilename);
123
            final FeatureStore store =
124
                (FeatureStore) dm.openStore("DBF",storeParameters);
125
            final TableDocument pt =
126
                (TableDocument) ProjectManager.getInstance().createDocument(
127
                    TableManager.TYPENAME, sName);
128
            pt.setStore(store);
129
            return pt;
130
        } catch (final Exception e) {
131
            Sextante.addErrorToLog(e);
132
            return null;
133
        }
134
    }   
135

    
136
    public static Object open(final String sFilename) {
137

    
138
        final IProjection projection =
139
            ((ProjectPreferences) ApplicationLocator.getManager()
140
                .getPreferences("project")).getDefaultProjection();
141
        FLayer layer = openLayer(sFilename, sFilename, projection);
142
        if (layer != null) {
143
            return layer;
144
        }
145

    
146
        final TableDocument table = openTable(sFilename, sFilename);
147

    
148
        return table;
149
    }
150

    
151
}