use of org.locationtech.geogig.geotools.plumbing.ExportDiffOp in project GeoGig by boundlessgeo.
the class ShpExportDiff method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
if (args.size() != 4) {
printUsage(cli);
throw new CommandFailedException();
}
String commitOld = args.get(0);
String commitNew = args.get(1);
String path = args.get(2);
String shapefile = args.get(3);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
File file = new File(shapefile);
if (file.exists() && !overwrite) {
throw new CommandFailedException("The selected shapefile already exists. Use -o to overwrite");
}
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);
ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
SimpleFeatureType outputFeatureType;
try {
outputFeatureType = getFeatureType(path, cli);
} catch (GeoToolsOpException e) {
cli.getConsole().println("No features to export.");
return;
}
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.add("geogig_fid", String.class);
for (AttributeDescriptor descriptor : outputFeatureType.getAttributeDescriptors()) {
builder.add(descriptor);
}
builder.setName(outputFeatureType.getName());
builder.setCRS(outputFeatureType.getCoordinateReferenceSystem());
outputFeatureType = builder.buildFeatureType();
dataStore.createSchema(outputFeatureType);
final String typeName = dataStore.getTypeNames()[0];
final SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
if (!(featureSource instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Could not create feature store.");
}
final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
Function<Feature, Optional<Feature>> function = getTransformingFunction(dataStore.getSchema());
ExportDiffOp op = cli.getGeogig().command(ExportDiffOp.class).setFeatureStore(featureStore).setPath(path).setOldRef(commitOld).setNewRef(commitNew).setUseOld(old).setTransactional(false).setFeatureTypeConversionFunction(function);
try {
op.setProgressListener(cli.getProgressListener()).call();
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
file.delete();
switch(e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException("Error: The selected tree contains mixed feature types.", e);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
}
cli.getConsole().println(path + " exported successfully to " + shapefile);
}
Aggregations