use of org.geotools.data.simple.SimpleFeatureStore in project GeoGig by boundlessgeo.
the class SLExport method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
if (args.isEmpty()) {
printUsage(cli);
throw new CommandFailedException();
}
String path = args.get(0);
String tableName = args.get(1);
checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified");
DataStore dataStore = getDataStore();
ObjectId featureTypeId = null;
if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
SimpleFeatureType outputFeatureType;
if (sFeatureTypeId != null) {
// Check the feature type id string is a correct id
Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call();
checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call();
checkParameter(type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId);
outputFeatureType = (SimpleFeatureType) cli.getGeogig().command(RevObjectParse.class).setObjectId(id.get()).call(RevFeatureType.class).get().type();
featureTypeId = id.get();
} else {
try {
SimpleFeatureType sft = getFeatureType(path, cli);
outputFeatureType = new SimpleFeatureTypeImpl(new NameImpl(tableName), sft.getAttributeDescriptors(), sft.getGeometryDescriptor(), sft.isAbstract(), sft.getRestrictions(), sft.getSuper(), sft.getDescription());
} catch (GeoToolsOpException e) {
throw new CommandFailedException("No features to export.", e);
}
}
try {
dataStore.createSchema(outputFeatureType);
} catch (IOException e) {
throw new CommandFailedException("Cannot create new table in database", e);
}
} else {
if (!overwrite) {
throw new CommandFailedException("The selected table already exists. Use -o to overwrite");
}
}
SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
if (!(featureSource instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Can't write to the selected table");
}
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
if (overwrite) {
try {
featureStore.removeFeatures(Filter.INCLUDE);
} catch (IOException e) {
throw new CommandFailedException("Error truncating table: " + e.getMessage(), e);
}
}
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
if (defaultType) {
op.exportDefaultFeatureType();
}
try {
op.setProgressListener(cli.getProgressListener()).call();
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
switch(e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException("The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
}
cli.getConsole().println(path + " exported successfully to " + tableName);
}
use of org.geotools.data.simple.SimpleFeatureStore in project GeoGig by boundlessgeo.
the class OSMExportPG method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) {
Preconditions.checkNotNull(mappingFile != null, "A data mapping file must be specified");
final Mapping mapping = Mapping.fromFile(mappingFile);
List<MappingRule> rules = mapping.getRules();
checkParameter(!rules.isEmpty(), "No rules are defined in the specified mapping");
for (final MappingRule rule : mapping.getRules()) {
Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {
@Override
@Nullable
public Optional<Feature> apply(@Nullable Feature feature) {
Optional<Feature> mapped = rule.apply(feature);
if (mapped.isPresent()) {
return Optional.of(mapped.get());
}
return Optional.absent();
}
};
SimpleFeatureType outputFeatureType = rule.getFeatureType();
String path = getOriginTreesFromOutputFeatureType(outputFeatureType);
DataStore dataStore = null;
try {
dataStore = getDataStore();
String tableName = outputFeatureType.getName().getLocalPart();
if (Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
if (!overwrite) {
throw new CommandFailedException("A table named '" + tableName + "'already exists. Use -o to overwrite");
}
} else {
try {
dataStore.createSchema(outputFeatureType);
} catch (IOException e) {
throw new CommandFailedException("Cannot create new table in database", e);
}
}
final SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
if (!(featureSource instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Could not create feature store. Data source is read only.");
}
final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
if (overwrite) {
featureStore.removeFeatures(Filter.INCLUDE);
}
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFeatureTypeConversionFunction(function);
try {
op.setProgressListener(cli.getProgressListener()).call();
cli.getConsole().println("OSM data exported successfully to " + tableName);
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
} catch (IOException e) {
throw new IllegalStateException("Cannot connect to database: " + e.getMessage(), e);
} finally {
if (dataStore != null) {
dataStore.dispose();
}
}
}
}
use of org.geotools.data.simple.SimpleFeatureStore in project GeoGig by boundlessgeo.
the class ExportDiffOpTest method testExportDiffUsingOldVersion.
@Test
public void testExportDiffUsingOldVersion() throws Exception {
insertAndAdd(points1);
final RevCommit insertCommit = geogig.command(CommitOp.class).setAll(true).call();
final String featureId = points1.getIdentifier().getID();
final Feature modifiedFeature = feature((SimpleFeatureType) points1.getType(), featureId, "changedProp", new Integer(1500));
insertAndAdd(modifiedFeature, points2);
final RevCommit changeCommit = geogig.command(CommitOp.class).setAll(true).call();
Feature[] points = new Feature[] { points1 };
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.add("geogig_fid", String.class);
for (AttributeDescriptor descriptor : pointsType.getAttributeDescriptors()) {
builder.add(descriptor);
}
builder.setName(pointsType.getName());
builder.setCRS(pointsType.getCoordinateReferenceSystem());
SimpleFeatureType outputFeatureType = builder.buildFeatureType();
MemoryDataStore dataStore = new MemoryDataStore(outputFeatureType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
geogig.command(ExportDiffOp.class).setFeatureStore(featureStore).setPath(pointsName).setNewRef(changeCommit.getId().toString()).setOldRef(insertCommit.getId().toString()).setUseOld(true).call();
featureSource = dataStore.getFeatureSource(typeName);
featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection featureCollection = featureStore.getFeatures();
assertEquals(featureCollection.size(), points.length);
SimpleFeatureIterator features = featureCollection.features();
assertTrue(collectionsAreEqual(features, points));
}
use of org.geotools.data.simple.SimpleFeatureStore in project GeoGig by boundlessgeo.
the class ExportDiffOpTest method testExportDiff.
@Test
public void testExportDiff() throws Exception {
insertAndAdd(points1);
final RevCommit insertCommit = geogig.command(CommitOp.class).setAll(true).call();
final String featureId = points1.getIdentifier().getID();
final Feature modifiedFeature = feature((SimpleFeatureType) points1.getType(), featureId, "changedProp", new Integer(1500));
insertAndAdd(modifiedFeature, points2);
final RevCommit changeCommit = geogig.command(CommitOp.class).setAll(true).call();
Feature[] points = new Feature[] { modifiedFeature, points2 };
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.add("geogig_fid", String.class);
for (AttributeDescriptor descriptor : pointsType.getAttributeDescriptors()) {
builder.add(descriptor);
}
builder.setName(pointsType.getName());
builder.setCRS(pointsType.getCoordinateReferenceSystem());
SimpleFeatureType outputFeatureType = builder.buildFeatureType();
MemoryDataStore dataStore = new MemoryDataStore(outputFeatureType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
geogig.command(ExportDiffOp.class).setFeatureStore(featureStore).setPath(pointsName).setNewRef(changeCommit.getId().toString()).setOldRef(insertCommit.getId().toString()).call();
featureSource = dataStore.getFeatureSource(typeName);
featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection featureCollection = featureStore.getFeatures();
assertEquals(featureCollection.size(), points.length);
SimpleFeatureIterator features = featureCollection.features();
assertTrue(collectionsAreEqual(features, points));
}
use of org.geotools.data.simple.SimpleFeatureStore in project GeoGig by boundlessgeo.
the class ExportOpTest method testExportFromWrongFeatureType.
@Test
public void testExportFromWrongFeatureType() throws Exception {
MemoryDataStore dataStore = new MemoryDataStore(pointsType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
try {
geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).call();
fail();
} catch (IllegalArgumentException e) {
assertTrue(true);
}
}
Aggregations