root / tags / v2_0_0_Build_2056 / 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 @ 39032
History | View | Annotate | Download (11.2 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.ICoordTrans; |
27 |
import org.cresques.cts.IProjection; |
28 |
|
29 |
import org.gvsig.exportto.ExporttoService; |
30 |
import org.gvsig.exportto.ExporttoServiceException; |
31 |
import org.gvsig.exportto.ExporttoServiceFinishAction; |
32 |
import org.gvsig.fmap.dal.DALLocator; |
33 |
import org.gvsig.fmap.dal.DataManager; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.exception.InitializeException; |
36 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
37 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
38 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
40 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
41 |
import org.gvsig.fmap.dal.feature.Feature; |
42 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
43 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.fmap.dal.feature.FeatureType; |
46 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
47 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
48 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
49 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
50 |
import org.gvsig.fmap.geom.Geometry; |
51 |
import org.gvsig.tools.dispose.DisposableIterator; |
52 |
import org.gvsig.tools.dispose.DisposeUtils; |
53 |
import org.gvsig.tools.task.AbstractMonitorableTask; |
54 |
|
55 |
/**
|
56 |
* @author gvSIG Team
|
57 |
* @version $Id$
|
58 |
*
|
59 |
*/
|
60 |
public class ExporttoShapeService extends AbstractMonitorableTask implements |
61 |
ExporttoService { |
62 |
|
63 |
private File shapeFile; |
64 |
private IProjection projection;
|
65 |
private FeatureStore featureStore;
|
66 |
|
67 |
private int geometryType = -1; |
68 |
private NewFeatureStoreParameters newFeatureStoreParameters;
|
69 |
private FilesystemServerExplorer filesystemServerExplorer;
|
70 |
|
71 |
private ExporttoServiceFinishAction exporttoServiceFinishAction;
|
72 |
|
73 |
public ExporttoShapeService(File shapeFile, FeatureStore featureStore, |
74 |
IProjection projection) { |
75 |
super("Export to shape"); |
76 |
this.featureStore = featureStore;
|
77 |
this.shapeFile = shapeFile;
|
78 |
this.projection = projection;
|
79 |
} |
80 |
|
81 |
public void export(FeatureSet featureSet) throws ExporttoServiceException { |
82 |
|
83 |
initializeParams(featureSet); |
84 |
|
85 |
File file =
|
86 |
((FilesystemStoreParameters) newFeatureStoreParameters).getFile(); |
87 |
String pathFile = file.getAbsolutePath();
|
88 |
|
89 |
if (geometryType == Geometry.TYPES.GEOMETRY) {
|
90 |
String withoutShp = pathFile.replaceAll("\\.shp", ""); |
91 |
|
92 |
// POINT
|
93 |
String fileName = withoutShp + "_point" + ".shp"; |
94 |
newFeatureStoreParameters |
95 |
.setDynValue("shpfile", new File(fileName)); |
96 |
taskStatus.setTittle("Exporting points");
|
97 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
98 |
featureSet, Geometry.TYPES.POINT, true);
|
99 |
finishAction(fileName, newFeatureStoreParameters); |
100 |
|
101 |
// CURVE
|
102 |
fileName = withoutShp + "_curve" + ".shp"; |
103 |
newFeatureStoreParameters |
104 |
.setDynValue("shpfile", new File(fileName)); |
105 |
taskStatus.setTittle("Exporting curves");
|
106 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
107 |
featureSet, Geometry.TYPES.CURVE, true);
|
108 |
finishAction(fileName, newFeatureStoreParameters); |
109 |
|
110 |
// SURFACE
|
111 |
fileName = withoutShp + "_surface" + ".shp"; |
112 |
newFeatureStoreParameters |
113 |
.setDynValue("shpfile", new File(fileName)); |
114 |
taskStatus.setTittle("Exporting surfaces");
|
115 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
116 |
featureSet, Geometry.TYPES.SURFACE, true);
|
117 |
finishAction(fileName, newFeatureStoreParameters); |
118 |
|
119 |
} else {
|
120 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
121 |
featureSet, geometryType, false);
|
122 |
finishAction(file.getName(), newFeatureStoreParameters); |
123 |
} |
124 |
this.taskStatus.terminate();
|
125 |
this.taskStatus.remove();
|
126 |
} |
127 |
|
128 |
private void initializeParams(FeatureSet featureSet) |
129 |
throws ExporttoServiceException {
|
130 |
String path = shapeFile.getAbsolutePath();
|
131 |
|
132 |
if (!(path.toLowerCase().endsWith(".shp"))) { |
133 |
path = path + ".shp";
|
134 |
} |
135 |
|
136 |
File newFile = new File(path); |
137 |
DataManager dataManager = DALLocator.getDataManager(); |
138 |
|
139 |
FilesystemServerExplorerParameters explorerParams; |
140 |
try {
|
141 |
explorerParams = |
142 |
(FilesystemServerExplorerParameters) dataManager |
143 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
144 |
} catch (InitializeException e) {
|
145 |
throw new ExporttoServiceException(e); |
146 |
} catch (ProviderNotRegisteredException e) {
|
147 |
throw new ExporttoServiceException(e); |
148 |
} |
149 |
explorerParams.setRoot(newFile.getParent()); |
150 |
|
151 |
try {
|
152 |
filesystemServerExplorer = |
153 |
(FilesystemServerExplorer) dataManager.openServerExplorer( |
154 |
"FilesystemExplorer", explorerParams);
|
155 |
} catch (ValidateDataParametersException e) {
|
156 |
throw new ExporttoServiceException(e); |
157 |
} catch (InitializeException e) {
|
158 |
throw new ExporttoServiceException(e); |
159 |
} catch (ProviderNotRegisteredException e) {
|
160 |
throw new ExporttoServiceException(e); |
161 |
} |
162 |
|
163 |
try {
|
164 |
newFeatureStoreParameters = |
165 |
(NewFeatureStoreParameters) filesystemServerExplorer |
166 |
.getAddParameters(newFile); |
167 |
} catch (DataException e) {
|
168 |
throw new ExporttoServiceException(e); |
169 |
} |
170 |
|
171 |
newFeatureStoreParameters.setDynValue("CRS", projection);
|
172 |
|
173 |
geometryType = |
174 |
featureSet.getDefaultFeatureType().getDefaultGeometryAttribute() |
175 |
.getGeometryType(); |
176 |
|
177 |
} |
178 |
|
179 |
private void export(FilesystemServerExplorer explorer, |
180 |
NewFeatureStoreParameters params, FeatureSet featureSet, |
181 |
int geometryType, boolean checkType) throws ExporttoServiceException { |
182 |
|
183 |
String providerName = params.getDataStoreName();
|
184 |
String explorerName = explorer.getProviderName();
|
185 |
|
186 |
DisposableIterator it = null;
|
187 |
try {
|
188 |
EditableFeatureType type = |
189 |
featureStore.getDefaultFeatureType().getEditable(); |
190 |
FeatureAttributeDescriptor fad = |
191 |
(FeatureAttributeDescriptor) type.get(type |
192 |
.getDefaultGeometryAttributeName()); |
193 |
type.remove(fad.getName()); |
194 |
EditableFeatureAttributeDescriptor efad = |
195 |
type.add(fad.getName(), fad.getType(), fad.getSize()); |
196 |
efad.setDefaultValue(fad.getDefaultValue()); |
197 |
efad.setGeometryType(geometryType); |
198 |
efad.setPrecision(fad.getPrecision()); |
199 |
type.setDefaultGeometryAttributeName(fad.getName()); |
200 |
params.setDefaultFeatureType(type); |
201 |
|
202 |
params.setDynValue("geometryType", null); |
203 |
|
204 |
DataManager manager = DALLocator.getDataManager(); |
205 |
|
206 |
manager.newStore(explorerName, providerName, params, true);
|
207 |
FeatureStore target = |
208 |
(FeatureStore) manager.openStore(providerName, params); |
209 |
|
210 |
FeatureType targetType = target.getDefaultFeatureType(); |
211 |
|
212 |
taskStatus.setRangeOfValues(0, featureSet.getSize());
|
213 |
|
214 |
target.edit(FeatureStore.MODE_APPEND); |
215 |
it = featureSet.fastIterator(); |
216 |
int featureCount = 0; |
217 |
int geometryattribute =
|
218 |
featureSet.getDefaultFeatureType() |
219 |
.getDefaultGeometryAttributeIndex(); |
220 |
|
221 |
|
222 |
|
223 |
// ================================================
|
224 |
// Reprojection stuff
|
225 |
Geometry reproj_geom = null;
|
226 |
EditableFeature edit_feat = null;
|
227 |
IProjection sourceProjection = |
228 |
featureStore.getDefaultFeatureType().getDefaultGeometryAttribute().getSRS(); |
229 |
|
230 |
ICoordTrans coord_trans = null;
|
231 |
// this comparison is perhaps too preventive
|
232 |
// we could have two instances of same projection
|
233 |
// so we would do more computations than needed
|
234 |
if (sourceProjection != this.projection) { |
235 |
coord_trans = sourceProjection.getCT(this.projection);
|
236 |
} |
237 |
// ================================================
|
238 |
|
239 |
while (it.hasNext()) {
|
240 |
Feature feature = (Feature) it.next(); |
241 |
if (checkType
|
242 |
&& (feature.getGeometry(geometryattribute).getType() != geometryType)) { |
243 |
continue;
|
244 |
} |
245 |
|
246 |
edit_feat = target.createNewFeature(targetType, feature); |
247 |
// ================================================
|
248 |
// Reprojection stuff
|
249 |
if (coord_trans != null) { |
250 |
reproj_geom = edit_feat.getDefaultGeometry(); |
251 |
reproj_geom = reproj_geom.cloneGeometry(); |
252 |
reproj_geom.reProject(coord_trans); |
253 |
edit_feat.setDefaultGeometry(reproj_geom); |
254 |
} |
255 |
// ================================================
|
256 |
|
257 |
target.insert(edit_feat); |
258 |
|
259 |
featureCount++; |
260 |
this.taskStatus.setCurValue(featureCount);
|
261 |
|
262 |
if (this.taskStatus.isCancellationRequested()) { |
263 |
return;
|
264 |
} |
265 |
} |
266 |
target.finishEditing(); |
267 |
target.dispose(); |
268 |
} catch (DataException e) {
|
269 |
throw new ExporttoServiceException(e); |
270 |
} catch (ValidateDataParametersException e) {
|
271 |
throw new ExporttoServiceException(e); |
272 |
} finally {
|
273 |
DisposeUtils.dispose(it); |
274 |
} |
275 |
} |
276 |
|
277 |
private void finishAction(String layerName, |
278 |
NewFeatureStoreParameters newFeatureStoreParameters) { |
279 |
if (exporttoServiceFinishAction != null) { |
280 |
exporttoServiceFinishAction.finished(layerName, |
281 |
newFeatureStoreParameters); |
282 |
} |
283 |
} |
284 |
|
285 |
public void setFinishAction( |
286 |
ExporttoServiceFinishAction exporttoServiceFinishAction) { |
287 |
this.exporttoServiceFinishAction = exporttoServiceFinishAction;
|
288 |
} |
289 |
} |