22 |
22 |
package org.gvsig.exportto.swing.prov.shape;
|
23 |
23 |
|
24 |
24 |
import java.io.File;
|
|
25 |
import java.util.ArrayList;
|
|
26 |
import java.util.List;
|
25 |
27 |
|
26 |
28 |
import org.cresques.cts.ICoordTrans;
|
27 |
29 |
import org.cresques.cts.IProjection;
|
|
30 |
import org.slf4j.Logger;
|
|
31 |
import org.slf4j.LoggerFactory;
|
28 |
32 |
|
29 |
33 |
import org.gvsig.exportto.ExporttoService;
|
30 |
34 |
import org.gvsig.exportto.ExporttoServiceException;
|
... | ... | |
46 |
50 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
|
47 |
51 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
|
48 |
52 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
|
49 |
|
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
|
50 |
53 |
import org.gvsig.fmap.geom.Geometry;
|
|
54 |
import org.gvsig.fmap.geom.GeometryLocator;
|
|
55 |
import org.gvsig.fmap.geom.GeometryManager;
|
|
56 |
import org.gvsig.fmap.geom.aggregate.Aggregate;
|
|
57 |
import org.gvsig.fmap.geom.exception.CreateGeometryException;
|
|
58 |
import org.gvsig.fmap.geom.operation.GeometryOperationException;
|
|
59 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
|
|
60 |
import org.gvsig.fmap.geom.primitive.Curve;
|
|
61 |
import org.gvsig.fmap.geom.primitive.Point;
|
|
62 |
import org.gvsig.fmap.geom.primitive.Surface;
|
|
63 |
import org.gvsig.fmap.geom.type.GeometryType;
|
51 |
64 |
import org.gvsig.tools.dispose.DisposableIterator;
|
52 |
65 |
import org.gvsig.tools.dispose.DisposeUtils;
|
53 |
66 |
import org.gvsig.tools.task.AbstractMonitorableTask;
|
... | ... | |
60 |
73 |
public class ExporttoShapeService extends AbstractMonitorableTask implements
|
61 |
74 |
ExporttoService {
|
62 |
75 |
|
63 |
|
private File shapeFile;
|
|
76 |
private static Logger logger = LoggerFactory.getLogger(ExporttoShapeService.class);
|
|
77 |
|
|
78 |
private File theShapeFile;
|
64 |
79 |
private IProjection projection;
|
65 |
80 |
private FeatureStore featureStore;
|
66 |
81 |
|
67 |
82 |
private int geometryType = -1;
|
68 |
83 |
private NewFeatureStoreParameters newFeatureStoreParameters;
|
69 |
84 |
private FilesystemServerExplorer filesystemServerExplorer;
|
|
85 |
|
|
86 |
private static GeometryManager geoManager = GeometryLocator.getGeometryManager();
|
70 |
87 |
|
71 |
88 |
private ExporttoServiceFinishAction exporttoServiceFinishAction;
|
72 |
89 |
|
... | ... | |
74 |
91 |
IProjection projection) {
|
75 |
92 |
super("Export to shape");
|
76 |
93 |
this.featureStore = featureStore;
|
77 |
|
this.shapeFile = shapeFile;
|
|
94 |
this.theShapeFile = shapeFile;
|
78 |
95 |
this.projection = projection;
|
79 |
96 |
}
|
80 |
97 |
|
81 |
98 |
public void export(FeatureSet featureSet) throws ExporttoServiceException {
|
82 |
99 |
|
83 |
|
initializeParams(featureSet);
|
|
100 |
ExporttoServiceException throw_exp = null;
|
|
101 |
File item_shp = null;
|
|
102 |
String pathFile = theShapeFile.getAbsolutePath();
|
|
103 |
String withoutShp = pathFile.replaceAll("\\.shp", "");
|
|
104 |
|
|
105 |
File single_file = new File(withoutShp + ".shp");
|
|
106 |
initializeParams(featureSet, single_file);
|
84 |
107 |
|
85 |
|
File file =
|
86 |
|
((FilesystemStoreParameters) newFeatureStoreParameters).getFile();
|
87 |
|
String pathFile = file.getAbsolutePath();
|
88 |
|
|
89 |
108 |
if (geometryType == Geometry.TYPES.GEOMETRY) {
|
90 |
|
String withoutShp = pathFile.replaceAll("\\.shp", "");
|
91 |
|
|
92 |
109 |
// POINT
|
93 |
110 |
String fileName = withoutShp + "_point" + ".shp";
|
94 |
|
newFeatureStoreParameters
|
95 |
|
.setDynValue("shpfile", new File(fileName));
|
|
111 |
item_shp = new File(fileName);
|
|
112 |
initializeParams(featureSet, item_shp);
|
|
113 |
|
|
114 |
newFeatureStoreParameters.setDynValue("shpfile", item_shp);
|
96 |
115 |
taskStatus.setTittle("Exporting points");
|
97 |
|
export(filesystemServerExplorer, newFeatureStoreParameters,
|
98 |
|
featureSet, Geometry.TYPES.POINT, true);
|
|
116 |
|
|
117 |
try {
|
|
118 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
|
119 |
featureSet, Geometry.TYPES.POINT, true);
|
|
120 |
} catch (ExporttoServiceException ee) {
|
|
121 |
throw_exp = ee;
|
|
122 |
}
|
|
123 |
|
99 |
124 |
finishAction(fileName, newFeatureStoreParameters);
|
100 |
125 |
|
101 |
126 |
// CURVE
|
102 |
127 |
fileName = withoutShp + "_curve" + ".shp";
|
103 |
|
newFeatureStoreParameters
|
104 |
|
.setDynValue("shpfile", new File(fileName));
|
|
128 |
item_shp = new File(fileName);
|
|
129 |
initializeParams(featureSet, item_shp);
|
|
130 |
newFeatureStoreParameters.setDynValue("shpfile", item_shp);
|
105 |
131 |
taskStatus.setTittle("Exporting curves");
|
106 |
|
export(filesystemServerExplorer, newFeatureStoreParameters,
|
107 |
|
featureSet, Geometry.TYPES.CURVE, true);
|
|
132 |
|
|
133 |
try {
|
|
134 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
|
135 |
featureSet, Geometry.TYPES.CURVE, true);
|
|
136 |
} catch (ExporttoServiceException ee) {
|
|
137 |
throw_exp = ee;
|
|
138 |
}
|
|
139 |
|
108 |
140 |
finishAction(fileName, newFeatureStoreParameters);
|
109 |
141 |
|
110 |
142 |
// SURFACE
|
111 |
143 |
fileName = withoutShp + "_surface" + ".shp";
|
112 |
|
newFeatureStoreParameters
|
113 |
|
.setDynValue("shpfile", new File(fileName));
|
|
144 |
item_shp = new File(fileName);
|
|
145 |
initializeParams(featureSet, item_shp);
|
|
146 |
newFeatureStoreParameters.setDynValue("shpfile", item_shp);
|
114 |
147 |
taskStatus.setTittle("Exporting surfaces");
|
115 |
|
export(filesystemServerExplorer, newFeatureStoreParameters,
|
116 |
|
featureSet, Geometry.TYPES.SURFACE, true);
|
|
148 |
|
|
149 |
try {
|
|
150 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
|
151 |
featureSet, Geometry.TYPES.SURFACE, true);
|
|
152 |
} catch (ExporttoServiceException ee) {
|
|
153 |
throw_exp = ee;
|
|
154 |
}
|
117 |
155 |
finishAction(fileName, newFeatureStoreParameters);
|
118 |
156 |
|
119 |
157 |
} else {
|
120 |
|
export(filesystemServerExplorer, newFeatureStoreParameters,
|
121 |
|
featureSet, geometryType, false);
|
122 |
|
finishAction(file.getName(), newFeatureStoreParameters);
|
|
158 |
|
|
159 |
// params already initialized
|
|
160 |
newFeatureStoreParameters.setDynValue("shpfile", single_file);
|
|
161 |
try {
|
|
162 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
|
163 |
featureSet, geometryType, false);
|
|
164 |
} catch (ExporttoServiceException ee) {
|
|
165 |
throw_exp = ee;
|
|
166 |
}
|
|
167 |
finishAction(single_file.getName(), newFeatureStoreParameters);
|
123 |
168 |
}
|
124 |
169 |
this.taskStatus.terminate();
|
125 |
170 |
this.taskStatus.remove();
|
|
171 |
|
|
172 |
if (throw_exp != null) {
|
|
173 |
throw throw_exp;
|
|
174 |
}
|
126 |
175 |
}
|
127 |
176 |
|
128 |
|
private void initializeParams(FeatureSet featureSet)
|
|
177 |
private void initializeParams(
|
|
178 |
FeatureSet featureSet, File out_shp_file)
|
129 |
179 |
throws ExporttoServiceException {
|
130 |
|
String path = shapeFile.getAbsolutePath();
|
131 |
180 |
|
132 |
|
if (!(path.toLowerCase().endsWith(".shp"))) {
|
133 |
|
path = path + ".shp";
|
134 |
|
}
|
135 |
|
|
136 |
|
File newFile = new File(path);
|
137 |
181 |
DataManager dataManager = DALLocator.getDataManager();
|
138 |
182 |
|
139 |
183 |
FilesystemServerExplorerParameters explorerParams;
|
... | ... | |
146 |
190 |
} catch (ProviderNotRegisteredException e) {
|
147 |
191 |
throw new ExporttoServiceException(e);
|
148 |
192 |
}
|
149 |
|
explorerParams.setRoot(newFile.getParent());
|
|
193 |
explorerParams.setRoot(out_shp_file.getParent());
|
150 |
194 |
|
151 |
195 |
try {
|
152 |
196 |
filesystemServerExplorer =
|
... | ... | |
163 |
207 |
try {
|
164 |
208 |
newFeatureStoreParameters =
|
165 |
209 |
(NewFeatureStoreParameters) filesystemServerExplorer
|
166 |
|
.getAddParameters(newFile);
|
|
210 |
.getAddParameters(out_shp_file);
|
167 |
211 |
} catch (DataException e) {
|
168 |
212 |
throw new ExporttoServiceException(e);
|
169 |
213 |
}
|
... | ... | |
172 |
216 |
|
173 |
217 |
geometryType =
|
174 |
218 |
featureSet.getDefaultFeatureType().getDefaultGeometryAttribute()
|
175 |
|
.getGeometryType();
|
|
219 |
.getGeomType().getType();
|
176 |
220 |
|
177 |
221 |
}
|
178 |
222 |
|
... | ... | |
182 |
226 |
|
183 |
227 |
String providerName = params.getDataStoreName();
|
184 |
228 |
String explorerName = explorer.getProviderName();
|
|
229 |
boolean there_was_error = false;
|
185 |
230 |
|
186 |
231 |
DisposableIterator it = null;
|
187 |
232 |
try {
|
... | ... | |
194 |
239 |
EditableFeatureAttributeDescriptor efad =
|
195 |
240 |
type.add(fad.getName(), fad.getType(), fad.getSize());
|
196 |
241 |
efad.setDefaultValue(fad.getDefaultValue());
|
197 |
|
efad.setGeometryType(geometryType);
|
|
242 |
|
|
243 |
GeometryType gty = null;
|
|
244 |
try {
|
|
245 |
gty = geoManager.getGeometryType(
|
|
246 |
geometryType, Geometry.SUBTYPES.GEOM2D);
|
|
247 |
} catch (Exception e) {
|
|
248 |
throw new ExporttoServiceException(e);
|
|
249 |
}
|
|
250 |
|
|
251 |
efad.setGeometryType(gty);
|
|
252 |
|
198 |
253 |
efad.setPrecision(fad.getPrecision());
|
199 |
254 |
type.setDefaultGeometryAttributeName(fad.getName());
|
200 |
255 |
params.setDefaultFeatureType(type);
|
... | ... | |
214 |
269 |
target.edit(FeatureStore.MODE_APPEND);
|
215 |
270 |
it = featureSet.fastIterator();
|
216 |
271 |
int featureCount = 0;
|
217 |
|
int geometryattribute =
|
218 |
|
featureSet.getDefaultFeatureType()
|
219 |
|
.getDefaultGeometryAttributeIndex();
|
220 |
|
|
221 |
|
|
222 |
272 |
|
223 |
273 |
// ================================================
|
224 |
274 |
// Reprojection stuff
|
... | ... | |
236 |
286 |
}
|
237 |
287 |
// ================================================
|
238 |
288 |
|
|
289 |
List<Geometry> extracted = null;
|
|
290 |
Geometry gitem = null;
|
|
291 |
|
239 |
292 |
while (it.hasNext()) {
|
|
293 |
|
240 |
294 |
Feature feature = (Feature) it.next();
|
241 |
|
if (checkType
|
242 |
|
&& (feature.getGeometry(geometryattribute).getType() != geometryType)) {
|
243 |
|
continue;
|
|
295 |
gitem = feature.getDefaultGeometry();
|
|
296 |
|
|
297 |
if (checkType) {
|
|
298 |
extracted = getGeometriesFrom(gitem, geometryType);
|
|
299 |
if (extracted.size() == 0) {
|
|
300 |
// found no geometries of correct type
|
|
301 |
continue;
|
|
302 |
} else {
|
|
303 |
if (geometryType != Geometry.TYPES.POINT) {
|
|
304 |
// If not points, merge geometries
|
|
305 |
// (curves or surfaces)
|
|
306 |
try {
|
|
307 |
gitem = union(extracted);
|
|
308 |
extracted = new ArrayList<Geometry>();
|
|
309 |
extracted.add(gitem);
|
|
310 |
} catch (Exception ex) {
|
|
311 |
there_was_error = true;
|
|
312 |
logger.info("Error in union.", ex);
|
|
313 |
}
|
|
314 |
} else {
|
|
315 |
// only in the case of points, we can have several
|
|
316 |
// geometries if source is multipoint
|
|
317 |
}
|
|
318 |
}
|
|
319 |
} else {
|
|
320 |
extracted = new ArrayList<Geometry>();
|
|
321 |
extracted.add(gitem);
|
244 |
322 |
}
|
245 |
323 |
|
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);
|
|
324 |
for (int i=0; i<extracted.size(); i++) {
|
|
325 |
gitem = extracted.get(i);
|
|
326 |
gitem = force2D(gitem, geometryType);
|
|
327 |
edit_feat = target.createNewFeature(true);
|
|
328 |
there_was_error = there_was_error |
|
|
329 |
// accumulate error boolean
|
|
330 |
setNonNulls(targetType, feature, edit_feat);
|
|
331 |
edit_feat.setDefaultGeometry(gitem);
|
|
332 |
// ================================================
|
|
333 |
// Reprojection stuff
|
|
334 |
if (coord_trans != null) {
|
|
335 |
reproj_geom = edit_feat.getDefaultGeometry();
|
|
336 |
reproj_geom = reproj_geom.cloneGeometry();
|
|
337 |
reproj_geom.reProject(coord_trans);
|
|
338 |
edit_feat.setDefaultGeometry(reproj_geom);
|
|
339 |
}
|
|
340 |
// ================================================
|
|
341 |
target.insert(edit_feat);
|
254 |
342 |
}
|
255 |
|
// ================================================
|
256 |
|
|
257 |
|
target.insert(edit_feat);
|
258 |
343 |
|
259 |
344 |
featureCount++;
|
260 |
345 |
this.taskStatus.setCurValue(featureCount);
|
... | ... | |
265 |
350 |
}
|
266 |
351 |
target.finishEditing();
|
267 |
352 |
target.dispose();
|
268 |
|
} catch (DataException e) {
|
|
353 |
} catch (Exception e) {
|
269 |
354 |
throw new ExporttoServiceException(e);
|
270 |
|
} catch (ValidateDataParametersException e) {
|
271 |
|
throw new ExporttoServiceException(e);
|
272 |
355 |
} finally {
|
273 |
356 |
DisposeUtils.dispose(it);
|
274 |
357 |
}
|
|
358 |
|
|
359 |
if (there_was_error) {
|
|
360 |
Exception cause = new Exception(
|
|
361 |
"_Issues_with_attributes_or_geometries");
|
|
362 |
throw new ExporttoServiceException(cause);
|
|
363 |
}
|
275 |
364 |
}
|
276 |
365 |
|
|
366 |
|
|
367 |
/**
|
|
368 |
* @param gitem
|
|
369 |
* @param geometryType2
|
|
370 |
* @return
|
|
371 |
*/
|
|
372 |
private Geometry force2D(Geometry ge, int gt) throws CreateGeometryException {
|
|
373 |
|
|
374 |
if (ge.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM2D) {
|
|
375 |
return ge;
|
|
376 |
} else {
|
|
377 |
switch (gt) {
|
|
378 |
case Geometry.TYPES.POINT:
|
|
379 |
Point p = (Point) ge;
|
|
380 |
return geoManager.createPoint(
|
|
381 |
p.getX(), p.getY(),
|
|
382 |
Geometry.SUBTYPES.GEOM2D);
|
|
383 |
case Geometry.TYPES.CURVE:
|
|
384 |
return geoManager.createCurve(ge.getGeneralPath(), Geometry.SUBTYPES.GEOM2D);
|
|
385 |
case Geometry.TYPES.SURFACE:
|
|
386 |
return geoManager.createSurface(ge.getGeneralPath(), Geometry.SUBTYPES.GEOM2D);
|
|
387 |
default:
|
|
388 |
return ge;
|
|
389 |
}
|
|
390 |
}
|
|
391 |
}
|
|
392 |
|
|
393 |
/**
|
|
394 |
* @param feature
|
|
395 |
* @param edit_feat
|
|
396 |
*/
|
|
397 |
private boolean setNonNulls(
|
|
398 |
FeatureType ft,
|
|
399 |
Feature feat, EditableFeature edit_f) {
|
|
400 |
|
|
401 |
boolean error = false;
|
|
402 |
FeatureAttributeDescriptor[] atts = ft.getAttributeDescriptors();
|
|
403 |
for (int i=0; i<atts.length; i++) {
|
|
404 |
if (atts[i].getType() != org.gvsig.fmap.geom.DataTypes.GEOMETRY) {
|
|
405 |
|
|
406 |
Object val = null;
|
|
407 |
|
|
408 |
try {
|
|
409 |
val = feat.get(atts[i].getName());
|
|
410 |
if (val != null) {
|
|
411 |
edit_f.set(
|
|
412 |
atts[i].getName(),
|
|
413 |
feat.get(atts[i].getName()));
|
|
414 |
}
|
|
415 |
} catch (Exception ex) {
|
|
416 |
logger.info("Error while getting/setting value", ex);
|
|
417 |
error = true;
|
|
418 |
}
|
|
419 |
}
|
|
420 |
}
|
|
421 |
return error;
|
|
422 |
}
|
|
423 |
|
|
424 |
private Geometry union(List<Geometry> geoms)
|
|
425 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
|
426 |
|
|
427 |
if (geoms == null || geoms.size() == 0) {
|
|
428 |
return null;
|
|
429 |
}
|
|
430 |
|
|
431 |
if (geoms.size() == 1) {
|
|
432 |
return geoms.get(0);
|
|
433 |
}
|
|
434 |
|
|
435 |
Geometry resp = geoms.get(0);
|
|
436 |
for (int i=1; i<geoms.size(); i++) {
|
|
437 |
resp = resp.union(geoms.get(i));
|
|
438 |
}
|
|
439 |
return resp;
|
|
440 |
}
|
|
441 |
/**
|
|
442 |
* @param feat_geom_type
|
|
443 |
* @param type must be POINT, LINE or POLYGON (Geometry.TYPES)
|
|
444 |
* @return
|
|
445 |
*/
|
|
446 |
private List<Geometry> getGeometriesFrom(Geometry in_geom, int type) {
|
|
447 |
|
|
448 |
List<Geometry> resp = new ArrayList<Geometry>();
|
|
449 |
Aggregate agg = null;
|
|
450 |
|
|
451 |
/*
|
|
452 |
* If input geometry is aggregate, search its
|
|
453 |
* primitives
|
|
454 |
*/
|
|
455 |
if (in_geom instanceof Aggregate) {
|
|
456 |
agg = (Aggregate) in_geom;
|
|
457 |
Geometry item = null;
|
|
458 |
List<Geometry> add_parts = new ArrayList<Geometry>();
|
|
459 |
for (int i=0; i<agg.getPrimitivesNumber(); i++) {
|
|
460 |
item = agg.getPrimitiveAt(i);
|
|
461 |
add_parts = getGeometriesFrom(item, type);
|
|
462 |
resp.addAll(add_parts);
|
|
463 |
}
|
|
464 |
return resp;
|
|
465 |
}
|
|
466 |
|
|
467 |
// ============================================
|
|
468 |
|
|
469 |
switch (type) {
|
|
470 |
case Geometry.TYPES.POINT:
|
|
471 |
if (in_geom instanceof Point) {
|
|
472 |
resp.add(in_geom);
|
|
473 |
}
|
|
474 |
// =======================================================
|
|
475 |
break;
|
|
476 |
case Geometry.TYPES.CURVE:
|
|
477 |
if (in_geom instanceof Curve) {
|
|
478 |
resp.add(in_geom);
|
|
479 |
}
|
|
480 |
// =======================================================
|
|
481 |
break;
|
|
482 |
case Geometry.TYPES.SURFACE:
|
|
483 |
if (in_geom instanceof Surface) {
|
|
484 |
resp.add(in_geom);
|
|
485 |
}
|
|
486 |
// =======================================================
|
|
487 |
break;
|
|
488 |
}
|
|
489 |
return resp;
|
|
490 |
}
|
|
491 |
|
277 |
492 |
private void finishAction(String layerName,
|
278 |
493 |
NewFeatureStoreParameters newFeatureStoreParameters) {
|
279 |
494 |
if (exporttoServiceFinishAction != null) {
|