Statistics
| Revision:

root / tags / v2_0_0_Build_2051 / libraries / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.prov / org.gvsig.exportto.swing.prov.shape / src / main / java / org / gvsig / exportto / swing / prov / shape / ExporttoShapeService.java @ 38749

History | View | Annotate | Download (9.83 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22
package org.gvsig.exportto.swing.prov.shape;
23

    
24
import java.io.File;
25

    
26
import org.cresques.cts.IProjection;
27

    
28
import org.gvsig.exportto.ExporttoService;
29
import org.gvsig.exportto.ExporttoServiceException;
30
import org.gvsig.exportto.ExporttoServiceFinishAction;
31
import org.gvsig.fmap.dal.DALLocator;
32
import org.gvsig.fmap.dal.DataManager;
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.exception.InitializeException;
35
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
36
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
37
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.EditableFeatureType;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.FeatureSet;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
45
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
46
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
47
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.tools.dispose.DisposableIterator;
50
import org.gvsig.tools.dispose.DisposeUtils;
51
import org.gvsig.tools.task.AbstractMonitorableTask;
52

    
53
/**
54
 * @author gvSIG Team
55
 * @version $Id$
56
 * 
57
 */
58
public class ExporttoShapeService extends AbstractMonitorableTask implements
59
    ExporttoService {
60

    
61
    private File shapeFile;
62
    private IProjection projection;
63
    private FeatureStore featureStore;
64

    
65
    private int geometryType = -1;
66
    private NewFeatureStoreParameters newFeatureStoreParameters;
67
    private FilesystemServerExplorer filesystemServerExplorer;
68

    
69
    private ExporttoServiceFinishAction exporttoServiceFinishAction;
70

    
71
    public ExporttoShapeService(File shapeFile, FeatureStore featureStore,
72
        IProjection projection) {
73
        super("Export to shape");
74
        this.featureStore = featureStore;
75
        this.shapeFile = shapeFile;
76
        this.projection = projection;
77
    }
78

    
79
    public void export(FeatureSet featureSet) throws ExporttoServiceException {
80

    
81
        initializeParams(featureSet);
82

    
83
        File file =
84
            ((FilesystemStoreParameters) newFeatureStoreParameters).getFile();
85
        String pathFile = file.getAbsolutePath();
86

    
87
        if (geometryType == Geometry.TYPES.GEOMETRY) {
88
            String withoutShp = pathFile.replaceAll("\\.shp", "");
89

    
90
            // POINT
91
            String fileName = withoutShp + "_point" + ".shp";
92
            newFeatureStoreParameters
93
                .setDynValue("shpfile", new File(fileName));
94
            taskStatus.setTittle("Exporting points");
95
            export(filesystemServerExplorer, newFeatureStoreParameters,
96
                featureSet, Geometry.TYPES.POINT, true);
97
            finishAction(fileName, newFeatureStoreParameters);
98

    
99
            // CURVE
100
            fileName = withoutShp + "_curve" + ".shp";
101
            newFeatureStoreParameters
102
                .setDynValue("shpfile", new File(fileName));
103
            taskStatus.setTittle("Exporting curves");
104
            export(filesystemServerExplorer, newFeatureStoreParameters,
105
                featureSet, Geometry.TYPES.CURVE, true);
106
            finishAction(fileName, newFeatureStoreParameters);
107

    
108
            // SURFACE
109
            fileName = withoutShp + "_surface" + ".shp";
110
            newFeatureStoreParameters
111
                .setDynValue("shpfile", new File(fileName));
112
            taskStatus.setTittle("Exporting surfaces");
113
            export(filesystemServerExplorer, newFeatureStoreParameters,
114
                featureSet, Geometry.TYPES.SURFACE, true);
115
            finishAction(fileName, newFeatureStoreParameters);
116

    
117
        } else {
118
            export(filesystemServerExplorer, newFeatureStoreParameters,
119
                featureSet, geometryType, false);
120
            finishAction(file.getName(), newFeatureStoreParameters);
121
        }
122
        this.taskStatus.terminate();
123
        this.taskStatus.remove();
124
    }
125

    
126
    private void initializeParams(FeatureSet featureSet)
127
        throws ExporttoServiceException {
128
        String path = shapeFile.getAbsolutePath();
129

    
130
        if (!(path.toLowerCase().endsWith(".shp"))) {
131
            path = path + ".shp";
132
        }
133

    
134
        File newFile = new File(path);
135
        DataManager dataManager = DALLocator.getDataManager();
136

    
137
        FilesystemServerExplorerParameters explorerParams;
138
        try {
139
            explorerParams =
140
                (FilesystemServerExplorerParameters) dataManager
141
                    .createServerExplorerParameters(FilesystemServerExplorer.NAME);
142
        } catch (InitializeException e) {
143
            throw new ExporttoServiceException(e);
144
        } catch (ProviderNotRegisteredException e) {
145
            throw new ExporttoServiceException(e);
146
        }
147
        explorerParams.setRoot(newFile.getParent());
148

    
149
        try {
150
            filesystemServerExplorer =
151
                (FilesystemServerExplorer) dataManager.openServerExplorer(
152
                    "FilesystemExplorer", explorerParams);
153
        } catch (ValidateDataParametersException e) {
154
            throw new ExporttoServiceException(e);
155
        } catch (InitializeException e) {
156
            throw new ExporttoServiceException(e);
157
        } catch (ProviderNotRegisteredException e) {
158
            throw new ExporttoServiceException(e);
159
        }
160

    
161
        try {
162
            newFeatureStoreParameters =
163
                (NewFeatureStoreParameters) filesystemServerExplorer
164
                    .getAddParameters(newFile);
165
        } catch (DataException e) {
166
            throw new ExporttoServiceException(e);
167
        }
168

    
169
        newFeatureStoreParameters.setDynValue("CRS", projection);
170

    
171
        geometryType =
172
            featureSet.getDefaultFeatureType().getDefaultGeometryAttribute()
173
                .getGeometryType();
174

    
175
    }
176

    
177
    private void export(FilesystemServerExplorer explorer,
178
        NewFeatureStoreParameters params, FeatureSet featureSet,
179
        int geometryType, boolean checkType) throws ExporttoServiceException {
180

    
181
        String providerName = params.getDataStoreName();
182
        String explorerName = explorer.getProviderName();
183

    
184
        DisposableIterator it = null;
185
        try {
186
            EditableFeatureType type =
187
                featureStore.getDefaultFeatureType().getEditable();
188
            FeatureAttributeDescriptor fad =
189
                (FeatureAttributeDescriptor) type.get(type
190
                    .getDefaultGeometryAttributeName());
191
            type.remove(fad.getName());
192
            EditableFeatureAttributeDescriptor efad =
193
                type.add(fad.getName(), fad.getType(), fad.getSize());
194
            efad.setDefaultValue(fad.getDefaultValue());
195
            efad.setGeometryType(geometryType);
196
            efad.setPrecision(fad.getPrecision());
197
            type.setDefaultGeometryAttributeName(fad.getName());
198
            params.setDefaultFeatureType(type);
199
            
200
            params.setDynValue("geometryType", null);
201

    
202
            DataManager manager = DALLocator.getDataManager();
203

    
204
            manager.newStore(explorerName, providerName, params, true);
205
            FeatureStore target =
206
                (FeatureStore) manager.openStore(providerName, params);
207

    
208
            FeatureType targetType = target.getDefaultFeatureType();
209

    
210
            taskStatus.setRangeOfValues(0, featureSet.getSize());
211

    
212
            target.edit(FeatureStore.MODE_APPEND);
213
            it = featureSet.fastIterator();
214
            int featureCount = 0;
215
            int geometryattribute =
216
                featureSet.getDefaultFeatureType()
217
                    .getDefaultGeometryAttributeIndex();
218

    
219
            while (it.hasNext()) {
220
                Feature feature = (Feature) it.next();
221
                if (checkType
222
                    && (feature.getGeometry(geometryattribute).getType() != geometryType)) {
223
                    continue;
224
                }
225

    
226
                target.insert(target.createNewFeature(targetType, feature));
227

    
228
                featureCount++;
229
                this.taskStatus.setCurValue(featureCount);
230

    
231
                if (this.taskStatus.isCancellationRequested()) {
232
                    return;
233
                }
234
            }
235
            target.finishEditing();
236
            target.dispose();
237
        } catch (DataException e) {
238
            throw new ExporttoServiceException(e);
239
        } catch (ValidateDataParametersException e) {
240
            throw new ExporttoServiceException(e);
241
        } finally {
242
            DisposeUtils.dispose(it);
243
        }
244
    }
245

    
246
    private void finishAction(String layerName,
247
        NewFeatureStoreParameters newFeatureStoreParameters) {
248
        if (exporttoServiceFinishAction != null) {
249
            exporttoServiceFinishAction.finished(layerName,
250
                newFeatureStoreParameters);
251
        }
252
    }
253

    
254
    public void setFinishAction(
255
        ExporttoServiceFinishAction exporttoServiceFinishAction) {
256
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
257
    }
258
}