use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class GeogigFeatureStore method addFeatures.
@Override
public final List<FeatureId> addFeatures(FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection) throws IOException {
if (Transaction.AUTO_COMMIT.equals(getTransaction())) {
throw new UnsupportedOperationException("GeoGIG does not support AUTO_COMMIT");
}
Preconditions.checkState(getDataStore().isAllowTransactions(), "Transactions not supported; head is not a local branch");
final WorkingTree workingTree = delegate.getWorkingTree();
final String path = delegate.getTypeTreePath();
ProgressListener listener = new DefaultProgressListener();
final List<FeatureId> insertedFids = Lists.newArrayList();
List<Node> deferringTarget = new AbstractList<Node>() {
@Override
public boolean add(Node node) {
String fid = node.getName();
String version = node.getObjectId().toString();
insertedFids.add(new FeatureIdVersionedImpl(fid, version));
return true;
}
@Override
public Node get(int index) {
throw new UnsupportedOperationException();
}
@Override
public int size() {
return 0;
}
};
Integer count = (Integer) null;
FeatureIterator<SimpleFeature> featureIterator = featureCollection.features();
try {
Iterator<SimpleFeature> features;
features = new FeatureIteratorIterator<SimpleFeature>(featureIterator);
/*
* Make sure to transform the incoming features to the native schema to avoid situations
* where geogig would change the metadataId of the RevFeature nodes due to small
* differences in the default and incoming schema such as namespace or missing
* properties
*/
final SimpleFeatureType nativeSchema = delegate.getNativeType();
features = Iterators.transform(features, new SchemaInforcer(nativeSchema));
workingTree.insert(path, features, listener, deferringTarget, count);
} catch (Exception e) {
throw new IOException(e);
} finally {
featureIterator.close();
}
return insertedFids;
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class GeogigFeatureStore method modifyFeatures.
@Override
public void modifyFeatures(Name[] names, Object[] values, Filter filter) throws IOException {
Preconditions.checkState(getDataStore().isAllowTransactions(), "Transactions not supported; head is not a local branch");
final WorkingTree workingTree = delegate.getWorkingTree();
final String path = delegate.getTypeTreePath();
Iterator<SimpleFeature> features = modifyingFeatureIterator(names, values, filter);
/*
* Make sure to transform the incoming features to the native schema to avoid situations
* where geogig would change the metadataId of the RevFeature nodes due to small differences
* in the default and incoming schema such as namespace or missing properties
*/
final SimpleFeatureType nativeSchema = delegate.getNativeType();
features = Iterators.transform(features, new SchemaInforcer(nativeSchema));
try {
ProgressListener listener = new DefaultProgressListener();
Integer count = (Integer) null;
List<Node> target = (List<Node>) null;
workingTree.insert(path, features, listener, target, count);
} catch (Exception e) {
throw new IOException(e);
}
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class GeogigFeatureSource method getBoundsInternal.
@Override
protected ReferencedEnvelope getBoundsInternal(Query query) throws IOException {
final Filter filter = (Filter) query.getFilter().accept(new SimplifyingFilterVisitor(), null);
final CoordinateReferenceSystem crs = getSchema().getCoordinateReferenceSystem();
if (Filter.INCLUDE.equals(filter) && oldRoot == null && ChangeType.ADDED.equals(changeType())) {
NodeRef typeRef = getTypeRef();
ReferencedEnvelope bounds = new ReferencedEnvelope(crs);
typeRef.getNode().expand(bounds);
return bounds;
}
if (Filter.EXCLUDE.equals(filter)) {
return ReferencedEnvelope.create(crs);
}
FeatureReader<SimpleFeatureType, SimpleFeature> features;
if (isNaturalOrder(query.getSortBy())) {
Integer offset = query.getStartIndex();
Integer maxFeatures = query.getMaxFeatures() == Integer.MAX_VALUE ? null : query.getMaxFeatures();
ScreenMap screenMap = (ScreenMap) query.getHints().get(Hints.SCREENMAP);
features = getNativeReader(Query.NO_NAMES, filter, offset, maxFeatures, screenMap);
} else {
features = getReader(query);
}
ReferencedEnvelope bounds = new ReferencedEnvelope(crs);
try {
while (features.hasNext()) {
bounds.expandToInclude((ReferencedEnvelope) features.next().getBounds());
}
} finally {
features.close();
}
return bounds;
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class ExportOp method alter.
private Iterator<SimpleFeature> alter(Iterator<SimpleFeature> plainFeatures, final ObjectId targetFeatureTypeId) {
final RevFeatureType targetType = stagingDatabase().getFeatureType(targetFeatureTypeId);
Function<SimpleFeature, SimpleFeature> alterFunction = new Function<SimpleFeature, SimpleFeature>() {
@Override
public SimpleFeature apply(SimpleFeature input) {
final RevFeatureType oldFeatureType;
oldFeatureType = (RevFeatureType) input.getUserData().get(RevFeatureType.class);
final ObjectId metadataId = oldFeatureType.getId();
if (targetType.getId().equals(metadataId)) {
return input;
}
final RevFeature oldFeature;
oldFeature = (RevFeature) input.getUserData().get(RevFeature.class);
ImmutableList<PropertyDescriptor> oldAttributes = oldFeatureType.sortedDescriptors();
ImmutableList<PropertyDescriptor> newAttributes = targetType.sortedDescriptors();
ImmutableList<Optional<Object>> oldValues = oldFeature.getValues();
List<Optional<Object>> newValues = Lists.newArrayList();
for (int i = 0; i < newAttributes.size(); i++) {
int idx = oldAttributes.indexOf(newAttributes.get(i));
if (idx != -1) {
Optional<Object> oldValue = oldValues.get(idx);
newValues.add(oldValue);
} else {
newValues.add(Optional.absent());
}
}
RevFeature newFeature = RevFeatureImpl.build(ImmutableList.copyOf(newValues));
FeatureBuilder featureBuilder = new FeatureBuilder(targetType);
SimpleFeature feature = (SimpleFeature) featureBuilder.build(input.getID(), newFeature);
return feature;
}
};
return Iterators.transform(plainFeatures, alterFunction);
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class ExportOp method _call.
/**
* Executes the export operation using the parameters that have been specified.
*
* @return a FeatureCollection with the specified features
*/
@Override
protected SimpleFeatureStore _call() {
final StagingDatabase database = stagingDatabase();
if (filterFeatureTypeId != null) {
RevObject filterType = database.getIfPresent(filterFeatureTypeId);
checkArgument(filterType instanceof RevFeatureType, "Provided filter feature type is does not exist");
}
final SimpleFeatureStore targetStore = getTargetStore();
final String refspec = resolveRefSpec();
final String treePath = refspec.substring(refspec.indexOf(':') + 1);
final RevTree rootTree = resolveRootTree(refspec);
final NodeRef typeTreeRef = resolTypeTreeRef(refspec, treePath, rootTree);
final ObjectId defaultMetadataId = typeTreeRef.getMetadataId();
final RevTree typeTree = database.getTree(typeTreeRef.objectId());
final ProgressListener progressListener = getProgressListener();
progressListener.started();
progressListener.setDescription("Exporting from " + path + " to " + targetStore.getName().getLocalPart() + "... ");
FeatureCollection<SimpleFeatureType, SimpleFeature> asFeatureCollection = new BaseFeatureCollection<SimpleFeatureType, SimpleFeature>() {
@Override
public FeatureIterator<SimpleFeature> features() {
final Iterator<SimpleFeature> plainFeatures = getFeatures(typeTree, database, defaultMetadataId, progressListener);
Iterator<SimpleFeature> adaptedFeatures = adaptToArguments(plainFeatures, defaultMetadataId);
Iterator<Optional<Feature>> transformed = Iterators.transform(adaptedFeatures, ExportOp.this.function);
Iterator<SimpleFeature> filtered = Iterators.filter(Iterators.transform(transformed, new Function<Optional<Feature>, SimpleFeature>() {
@Override
public SimpleFeature apply(Optional<Feature> input) {
return (SimpleFeature) (input.isPresent() ? input.get() : null);
}
}), Predicates.notNull());
return new DelegateFeatureIterator<SimpleFeature>(filtered);
}
};
// add the feature collection to the feature store
final Transaction transaction;
if (transactional) {
transaction = new DefaultTransaction("create");
} else {
transaction = Transaction.AUTO_COMMIT;
}
try {
targetStore.setTransaction(transaction);
try {
targetStore.addFeatures(asFeatureCollection);
transaction.commit();
} catch (final Exception e) {
if (transactional) {
transaction.rollback();
}
Throwables.propagateIfInstanceOf(e, GeoToolsOpException.class);
throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
} finally {
transaction.close();
}
} catch (IOException e) {
throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
}
progressListener.complete();
return targetStore;
}
Aggregations