use of org.opengis.feature.simple.SimpleFeatureType 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.opengis.feature.simple.SimpleFeatureType 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.opengis.feature.simple.SimpleFeatureType 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.opengis.feature.simple.SimpleFeatureType in project GeoGig by boundlessgeo.
the class ImportOpTest method testImportAllWithDifferentFeatureTypesAndDestPathAndAdd.
@Test
public void testImportAllWithDifferentFeatureTypesAndDestPathAndAdd() throws Exception {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setCRS(CRS.decode("EPSG:4326"));
builder.add("geom", Point.class);
builder.add("label", String.class);
builder.setName("dest");
SimpleFeatureType type = builder.buildFeatureType();
GeometryFactory gf = new GeometryFactory();
SimpleFeature feature = SimpleFeatureBuilder.build(type, new Object[] { gf.createPoint(new Coordinate(0, 0)), "feature0" }, "feature");
geogig.getRepository().workingTree().insert("dest", feature);
ImportOp importOp = geogig.command(ImportOp.class);
importOp.setDataStore(TestHelper.createTestFactory().createDataStore(null));
importOp.setAll(true);
importOp.setOverwrite(false);
importOp.setDestinationPath("dest");
importOp.setAdaptToDefaultFeatureType(false);
importOp.call();
Iterator<NodeRef> features = geogig.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).call();
ArrayList<NodeRef> list = Lists.newArrayList(features);
assertEquals(5, list.size());
TreeSet<ObjectId> set = Sets.newTreeSet();
ArrayList<RevFeatureType> ftlist = new ArrayList<RevFeatureType>();
for (NodeRef node : list) {
Optional<RevFeatureType> ft = geogig.command(RevObjectParse.class).setObjectId(node.getMetadataId()).call(RevFeatureType.class);
assertTrue(ft.isPresent());
ftlist.add(ft.get());
set.add(node.getMetadataId());
}
assertEquals(4, set.size());
}
use of org.opengis.feature.simple.SimpleFeatureType 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();
}
}
Aggregations