use of org.geotools.data.simple.SimpleFeatureSource in project GeoGig by boundlessgeo.
the class ExportOpTest method testExportFromTreeWithSeveralFeatureTypesUsingNonexistantTypeId.
@Test
public void testExportFromTreeWithSeveralFeatureTypesUsingNonexistantTypeId() throws Exception {
Feature[] points = new Feature[] { points2, points1B, points3 };
for (Feature feature : points) {
insert(feature);
}
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).setFilterFeatureTypeId(ObjectId.forString("fake")).call();
fail();
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("filter feature type"));
}
}
use of org.geotools.data.simple.SimpleFeatureSource in project GeoGig by boundlessgeo.
the class ExportOpTest method testExportFromWorkingTree.
@Test
public void testExportFromWorkingTree() throws Exception {
Feature[] points = new Feature[] { points1, points2, points3 };
for (Feature feature : points) {
insert(feature);
}
MemoryDataStore dataStore = new MemoryDataStore(pointsType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).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.SimpleFeatureSource in project GeoGig by boundlessgeo.
the class ExportOpTest method testExportFromTreeWithSeveralFeatureTypesUsingFeatureTypeId.
@Test
public void testExportFromTreeWithSeveralFeatureTypesUsingFeatureTypeId() throws Exception {
Feature[] points = new Feature[] { points2, points1B, points3 };
for (Feature feature : points) {
insert(feature);
}
Feature[] expectedPoints = new Feature[] { points1B };
MemoryDataStore dataStore = new MemoryDataStore(pointsType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setFilterFeatureTypeId(RevFeatureTypeImpl.build(modifiedPointsType).getId()).call();
featureSource = dataStore.getFeatureSource(typeName);
featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection featureCollection = featureStore.getFeatures();
assertEquals(expectedPoints.length, featureCollection.size());
SimpleFeatureIterator features = featureCollection.features();
assertTrue(collectionsAreEqual(features, expectedPoints));
}
use of org.geotools.data.simple.SimpleFeatureSource in project GeoGig by boundlessgeo.
the class OSMExportSL method exportRule.
private void exportRule(final MappingRule rule, final GeogigCLI cli) throws IOException {
Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {
@Override
public Optional<Feature> apply(Feature feature) {
Optional<Feature> mapped = rule.apply(feature);
return mapped;
}
};
SimpleFeatureType outputFeatureType = rule.getFeatureType();
String path = getOriginTreesFromOutputFeatureType(outputFeatureType);
DataStore dataStore = getDataStore();
try {
final String tableName = ensureTableExists(outputFeatureType, dataStore);
final SimpleFeatureSource source = dataStore.getFeatureSource(tableName);
if (!(source instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Could not create feature store.");
}
final SimpleFeatureStore store = (SimpleFeatureStore) source;
if (overwrite) {
try {
store.removeFeatures(Filter.INCLUDE);
} catch (IOException e) {
throw new CommandFailedException("Error truncating table " + tableName, e);
}
}
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(store).setPath(path).setFeatureTypeConversionFunction(function);
try {
op.setProgressListener(cli.getProgressListener()).call();
cli.getConsole().println("OSM data exported successfully to " + tableName);
} catch (IllegalArgumentException iae) {
throw new InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
} finally {
dataStore.dispose();
}
}
use of org.geotools.data.simple.SimpleFeatureSource 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