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 | 34981 | jpiera | /* 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 | 37780 | cordinyana | *
|
57 | 34981 | jpiera | */
|
58 | 37780 | cordinyana | public class ExporttoShapeService extends AbstractMonitorableTask implements |
59 | ExporttoService { |
||
60 | 34981 | jpiera | |
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 | 37780 | cordinyana | |
69 | 34981 | jpiera | private ExporttoServiceFinishAction exporttoServiceFinishAction;
|
70 | 37780 | cordinyana | |
71 | public ExporttoShapeService(File shapeFile, FeatureStore featureStore, |
||
72 | IProjection projection) { |
||
73 | super("Export to shape"); |
||
74 | 34981 | jpiera | this.featureStore = featureStore;
|
75 | 37780 | cordinyana | this.shapeFile = shapeFile;
|
76 | 34981 | jpiera | this.projection = projection;
|
77 | } |
||
78 | |||
79 | public void export(FeatureSet featureSet) throws ExporttoServiceException { |
||
80 | 37780 | cordinyana | |
81 | 34981 | jpiera | initializeParams(featureSet); |
82 | 37780 | cordinyana | |
83 | File file =
|
||
84 | ((FilesystemStoreParameters) newFeatureStoreParameters).getFile(); |
||
85 | String pathFile = file.getAbsolutePath();
|
||
86 | |||
87 | if (geometryType == Geometry.TYPES.GEOMETRY) {
|
||
88 | 34981 | jpiera | String withoutShp = pathFile.replaceAll("\\.shp", ""); |
89 | |||
90 | 37780 | cordinyana | // POINT
|
91 | 34981 | jpiera | String fileName = withoutShp + "_point" + ".shp"; |
92 | 37780 | cordinyana | newFeatureStoreParameters |
93 | .setDynValue("shpfile", new File(fileName)); |
||
94 | 34981 | jpiera | taskStatus.setTittle("Exporting points");
|
95 | 37780 | cordinyana | export(filesystemServerExplorer, newFeatureStoreParameters,
|
96 | featureSet, Geometry.TYPES.POINT, true);
|
||
97 | 34981 | jpiera | finishAction(fileName, newFeatureStoreParameters); |
98 | 37780 | cordinyana | |
99 | // CURVE
|
||
100 | 34981 | jpiera | fileName = withoutShp + "_curve" + ".shp"; |
101 | 37780 | cordinyana | newFeatureStoreParameters |
102 | .setDynValue("shpfile", new File(fileName)); |
||
103 | 34981 | jpiera | taskStatus.setTittle("Exporting curves");
|
104 | 37780 | cordinyana | export(filesystemServerExplorer, newFeatureStoreParameters,
|
105 | featureSet, Geometry.TYPES.CURVE, true);
|
||
106 | 34981 | jpiera | finishAction(fileName, newFeatureStoreParameters); |
107 | |||
108 | 37780 | cordinyana | // SURFACE
|
109 | 34981 | jpiera | fileName = withoutShp + "_surface" + ".shp"; |
110 | 37780 | cordinyana | newFeatureStoreParameters |
111 | .setDynValue("shpfile", new File(fileName)); |
||
112 | 34981 | jpiera | taskStatus.setTittle("Exporting surfaces");
|
113 | 37780 | cordinyana | export(filesystemServerExplorer, newFeatureStoreParameters,
|
114 | featureSet, Geometry.TYPES.SURFACE, true);
|
||
115 | 34981 | jpiera | finishAction(fileName, newFeatureStoreParameters); |
116 | |||
117 | 37780 | cordinyana | } else {
|
118 | export(filesystemServerExplorer, newFeatureStoreParameters,
|
||
119 | featureSet, geometryType, false);
|
||
120 | 35630 | fdiaz | finishAction(file.getName(), newFeatureStoreParameters); |
121 | 34981 | jpiera | } |
122 | this.taskStatus.terminate();
|
||
123 | this.taskStatus.remove();
|
||
124 | } |
||
125 | |||
126 | 37780 | cordinyana | private void initializeParams(FeatureSet featureSet) |
127 | throws ExporttoServiceException {
|
||
128 | 34981 | jpiera | String path = shapeFile.getAbsolutePath();
|
129 | |||
130 | 37780 | cordinyana | if (!(path.toLowerCase().endsWith(".shp"))) { |
131 | 34981 | jpiera | path = path + ".shp";
|
132 | } |
||
133 | |||
134 | File newFile = new File(path); |
||
135 | DataManager dataManager = DALLocator.getDataManager(); |
||
136 | |||
137 | FilesystemServerExplorerParameters explorerParams; |
||
138 | try {
|
||
139 | 37780 | cordinyana | explorerParams = |
140 | (FilesystemServerExplorerParameters) dataManager |
||
141 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
142 | 34981 | jpiera | } catch (InitializeException e) {
|
143 | throw new ExporttoServiceException(e); |
||
144 | } catch (ProviderNotRegisteredException e) {
|
||
145 | throw new ExporttoServiceException(e); |
||
146 | } |
||
147 | explorerParams.setRoot(newFile.getParent()); |
||
148 | 37780 | cordinyana | |
149 | 34981 | jpiera | try {
|
150 | 37780 | cordinyana | filesystemServerExplorer = |
151 | (FilesystemServerExplorer) dataManager.openServerExplorer( |
||
152 | "FilesystemExplorer", explorerParams);
|
||
153 | 34981 | jpiera | } 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 | 37780 | cordinyana | |
161 | 34981 | jpiera | try {
|
162 | 37780 | cordinyana | newFeatureStoreParameters = |
163 | (NewFeatureStoreParameters) filesystemServerExplorer |
||
164 | .getAddParameters(newFile); |
||
165 | 34981 | jpiera | } catch (DataException e) {
|
166 | throw new ExporttoServiceException(e); |
||
167 | } |
||
168 | |||
169 | newFeatureStoreParameters.setDynValue("CRS", projection);
|
||
170 | |||
171 | 37780 | cordinyana | geometryType = |
172 | featureSet.getDefaultFeatureType().getDefaultGeometryAttribute() |
||
173 | .getGeometryType(); |
||
174 | |||
175 | 34981 | jpiera | } |
176 | |||
177 | 37780 | cordinyana | private void export(FilesystemServerExplorer explorer, |
178 | NewFeatureStoreParameters params, FeatureSet featureSet, |
||
179 | int geometryType, boolean checkType) throws ExporttoServiceException { |
||
180 | 34981 | jpiera | |
181 | String providerName = params.getDataStoreName();
|
||
182 | String explorerName = explorer.getProviderName();
|
||
183 | |||
184 | DisposableIterator it = null;
|
||
185 | try {
|
||
186 | 37780 | cordinyana | EditableFeatureType type = |
187 | featureStore.getDefaultFeatureType().getEditable(); |
||
188 | FeatureAttributeDescriptor fad = |
||
189 | (FeatureAttributeDescriptor) type.get(type |
||
190 | .getDefaultGeometryAttributeName()); |
||
191 | 34981 | jpiera | type.remove(fad.getName()); |
192 | 37780 | cordinyana | EditableFeatureAttributeDescriptor efad = |
193 | type.add(fad.getName(), fad.getType(), fad.getSize()); |
||
194 | 34981 | jpiera | efad.setDefaultValue(fad.getDefaultValue()); |
195 | efad.setGeometryType(geometryType); |
||
196 | efad.setPrecision(fad.getPrecision()); |
||
197 | type.setDefaultGeometryAttributeName(fad.getName()); |
||
198 | params.setDefaultFeatureType(type); |
||
199 | 38411 | jldominguez | |
200 | params.setDynValue("geometryType", null); |
||
201 | 34981 | jpiera | |
202 | DataManager manager = DALLocator.getDataManager(); |
||
203 | |||
204 | manager.newStore(explorerName, providerName, params, true);
|
||
205 | 37780 | cordinyana | FeatureStore target = |
206 | (FeatureStore) manager.openStore(providerName, params); |
||
207 | 34981 | jpiera | |
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 | 37780 | cordinyana | int geometryattribute =
|
216 | featureSet.getDefaultFeatureType() |
||
217 | .getDefaultGeometryAttributeIndex(); |
||
218 | 34981 | jpiera | |
219 | while (it.hasNext()) {
|
||
220 | Feature feature = (Feature) it.next(); |
||
221 | 37780 | cordinyana | if (checkType
|
222 | && (feature.getGeometry(geometryattribute).getType() != geometryType)) { |
||
223 | 34981 | jpiera | continue;
|
224 | } |
||
225 | |||
226 | target.insert(target.createNewFeature(targetType, feature)); |
||
227 | |||
228 | 37780 | cordinyana | featureCount++; |
229 | 34981 | jpiera | 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 | 37780 | cordinyana | |
246 | private void finishAction(String layerName, |
||
247 | NewFeatureStoreParameters newFeatureStoreParameters) { |
||
248 | if (exporttoServiceFinishAction != null) { |
||
249 | exporttoServiceFinishAction.finished(layerName, |
||
250 | newFeatureStoreParameters); |
||
251 | 34981 | jpiera | } |
252 | } |
||
253 | |||
254 | public void setFinishAction( |
||
255 | ExporttoServiceFinishAction exporttoServiceFinishAction) { |
||
256 | this.exporttoServiceFinishAction = exporttoServiceFinishAction;
|
||
257 | 37780 | cordinyana | } |
258 | 34981 | jpiera | } |