use of org.geotools.data.simple.SimpleFeatureSource in project GeoGig by boundlessgeo.
the class GeoGigDataStoreTest method testGetFeatureSourceName.
@Test
public void testGetFeatureSourceName() throws Exception {
try {
dataStore.getFeatureSource(RepositoryTestCase.linesTypeName);
fail("Expected IOException");
} catch (IOException e) {
assertTrue(true);
}
SimpleFeatureSource source;
insertAndAdd(lines1);
try {
dataStore.getFeatureSource(RepositoryTestCase.linesTypeName);
fail("Expected IOE as feature typ is not committed yet");
} catch (IOException e) {
assertTrue(e.getMessage().contains("does not exist"));
}
commit();
source = dataStore.getFeatureSource(RepositoryTestCase.linesTypeName);
assertTrue(source instanceof GeogigFeatureStore);
try {
dataStore.getFeatureSource(RepositoryTestCase.pointsTypeName);
fail("Expected IOException");
} catch (IOException e) {
assertTrue(true);
}
insertAndAdd(points1);
commit();
source = dataStore.getFeatureSource(RepositoryTestCase.pointsTypeName);
assertTrue(source instanceof GeogigFeatureStore);
}
use of org.geotools.data.simple.SimpleFeatureSource 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.SimpleFeatureSource 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.SimpleFeatureSource 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.SimpleFeatureSource 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