private boolean exportResource( Data data, IProgressMonitor monitor ) {
if (monitor == null)
monitor = new NullProgressMonitor();
final WizardDialog wizardDialog = (WizardDialog) getContainer();
IGeoResource resource = data.getResource();
try {
SimpleFeatureSource fs = resource.resolve(SimpleFeatureSource.class, null);
SimpleFeatureCollection fc = fs.getFeatures(data.getQuery());
// TODO: remove from catalog/close layers if open?
SimpleFeatureType schema = fs.getSchema();
if (data.getName() != null) {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.init(schema);
builder.setName(data.getName());
schema = builder.buildFeatureType();
}
File file = determineDestinationFile(data);
monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
CoordinateReferenceSystem fromCRS = schema.getCoordinateReferenceSystem();
CoordinateReferenceSystem crs = data.getCRS();
MathTransform mt;
if (fromCRS != null && crs != null) {
mt = CRS.findMathTransform(fromCRS, crs, true);
} else {
if (crs != null)
mt = IdentityTransform.create(crs.getCoordinateSystem().getDimension());
else if (fromCRS != null)
mt = IdentityTransform.create(fromCRS.getCoordinateSystem().getDimension());
else
mt = IdentityTransform.create(2);
}
if (isAbstractGeometryType(schema)) {
// possibly multiple geometry types
String geomName = schema.getGeometryDescriptor().getName().getLocalPart();
DefaultFeatureCollection pointCollection = new DefaultFeatureCollection();
DefaultFeatureCollection lineCollection = new DefaultFeatureCollection();
DefaultFeatureCollection polygonCollection = new DefaultFeatureCollection();
SimpleFeatureCollection featureCollection = fs.getFeatures();
FeatureIterator<SimpleFeature> featureIterator = featureCollection.features();
while( featureIterator.hasNext() ) {
SimpleFeature feature = featureIterator.next();
String geometryType = ((Geometry) feature.getDefaultGeometry())
.getGeometryType();
if (geometryType.endsWith("Point")) {
pointCollection.add(feature);
} else if (geometryType.endsWith("LineString")) {
lineCollection.add(feature);
} else if (geometryType.endsWith("Polygon")) {
polygonCollection.add(feature);
}
}
if (polygonCollection.size() > 0) {
exportPolygonFeatures(data, monitor, file, polygonCollection, schema, geomName,
mt);
}
if (pointCollection.size() > 0) {
exportPointFeatures(data, monitor, file, pointCollection, schema, geomName, mt);
}
if (lineCollection.size() > 0) {
exportLineFeatures(data, monitor, file, lineCollection, schema, geomName, mt);
}
} else {
// single geometry type
SimpleFeatureType destinationFeatureType = createFeatureType(schema,
(Class< ? extends Geometry>) schema.getGeometryDescriptor().getType()
.getBinding(), crs);
ReprojectingFeatureCollection processed = new ReprojectingFeatureCollection(fc,
monitor, destinationFeatureType, mt);
boolean success = writeToShapefile(processed, destinationFeatureType, file);
if (success) {
addToCatalog(file, data);
} else {
Display.getDefault().asyncExec(new Runnable(){
public void run() {
String msg = "No features were exported; did you select anything?"; //$NON-NLS-1$
CatalogUIPlugin.log(msg, null);
wizardDialog.setErrorMessage(msg);
}
});
return false;
}
}
} catch (IOException e) {
String msg = MessageFormat.format(Messages.CatalogExport_layerFail, resource
.getIdentifier());
CatalogUIPlugin.log(msg, e);
wizardDialog.setErrorMessage(msg);
return false;
} catch (IllegalFilterException e) {
String msg = MessageFormat.format(Messages.CatalogExport_layerFail, resource
.getIdentifier());
CatalogUIPlugin.log(msg, e);
wizardDialog.setErrorMessage(msg);
return false;
} catch (SchemaException e) {
String msg = MessageFormat.format(Messages.CatalogExport_layerFail, resource
.getIdentifier());
CatalogExport.setError(wizardDialog, msg, e);
return false;
} catch (FactoryException e) {
String msg = resource.getIdentifier() + Messages.CatalogExport_reprojectError;
CatalogExport.setError(wizardDialog, msg, e);
return false;
} catch (RuntimeException e) {
e.printStackTrace();
if (e.getCause() instanceof TransformException) {
String msg = resource.getIdentifier() + Messages.CatalogExport_reprojectError;
CatalogExport.setError(wizardDialog, msg, e);
} else {
String msg = MessageFormat.format(Messages.CatalogExport_layerFail, resource
.getIdentifier());
CatalogExport.setError(wizardDialog, msg, e);
}
return false;
}