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