use of org.locationtech.geogig.api.RevObject 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;
}
use of org.locationtech.geogig.api.RevObject in project GeoGig by boundlessgeo.
the class WorkingTree method insert.
/**
* Inserts the given {@code features} into the working tree, using the {@code treePathResolver}
* function to determine to which tree each feature is added.
*
* @param treePathResolver a function that determines the path of the tree where each feature
* node is stored
* @param features the features to insert, possibly of different schema and targetted to
* different tree paths
* @param listener a progress listener
* @param insertedTarget if provided, all nodes created will be added to this list. Beware of
* possible memory implications when inserting a lot of features.
* @param collectionSize if given, used to determine progress and notify the {@code listener}
* @return the total number of inserted features
*/
public void insert(final Function<Feature, String> treePathResolver, Iterator<? extends Feature> features, final ProgressListener listener, @Nullable final List<Node> insertedTarget, @Nullable final Integer collectionSize) {
checkArgument(collectionSize == null || collectionSize.intValue() > -1);
final int nTreeThreads = Math.max(2, Runtime.getRuntime().availableProcessors() / 2);
final ExecutorService treeBuildingService = Executors.newFixedThreadPool(nTreeThreads, new ThreadFactoryBuilder().setNameFormat("WorkingTree-tree-builder-%d").build());
final WorkingTreeInsertHelper insertHelper;
insertHelper = new WorkingTreeInsertHelper(indexDatabase, context, getTree(), treePathResolver, treeBuildingService);
UnmodifiableIterator<? extends Feature> filtered = Iterators.filter(features, new Predicate<Feature>() {
@Override
public boolean apply(Feature feature) {
if (listener.isCanceled()) {
return false;
}
if (feature instanceof FeatureToDelete) {
insertHelper.remove((FeatureToDelete) feature);
return false;
} else {
return true;
}
}
});
Iterator<RevObject> objects = Iterators.transform(filtered, new Function<Feature, RevObject>() {
private int count;
@Override
public RevFeature apply(Feature feature) {
final RevFeature revFeature = RevFeatureBuilder.build(feature);
ObjectId id = revFeature.getId();
final Node node = insertHelper.put(id, feature);
if (insertedTarget != null) {
insertedTarget.add(node);
}
count++;
if (collectionSize == null) {
listener.setProgress(count);
} else {
listener.setProgress((float) (count * 100) / collectionSize.intValue());
}
return revFeature;
}
});
try {
listener.started();
indexDatabase.putAll(objects);
if (listener.isCanceled()) {
return;
}
listener.setDescription("Building trees for " + new TreeSet<String>(insertHelper.getTreeNames()));
Stopwatch sw = Stopwatch.createStarted();
Map<NodeRef, RevTree> trees = insertHelper.buildTrees();
listener.setDescription(String.format("Trees built in %s", sw.stop()));
for (Map.Entry<NodeRef, RevTree> treeEntry : trees.entrySet()) {
if (!listener.isCanceled()) {
NodeRef treeRef = treeEntry.getKey();
RevTree newFeatureTree = treeEntry.getValue();
String treePath = treeRef.path();
ObjectId newRootTree = context.command(WriteBack.class).setAncestor(getTreeSupplier()).setChildPath(treePath).setMetadataId(treeRef.getMetadataId()).setToIndex(true).setTree(newFeatureTree).call();
updateWorkHead(newRootTree);
}
}
listener.complete();
} finally {
treeBuildingService.shutdownNow();
}
}
use of org.locationtech.geogig.api.RevObject in project GeoGig by boundlessgeo.
the class SparseCloneTest method testFeatureMovingOutOfAOI.
@Test
public void testFeatureMovingOutOfAOI() throws Exception {
Map<String, String> filter = new HashMap<String, String>();
filter.put("default", "BBOX(pp,9, -80, 15, -70,'EPSG:4326')");
createFilterFile(filter);
// Commit several features to the remote
List<Feature> features = Arrays.asList(city1, city1_modified);
LinkedList<RevCommit> expected = new LinkedList<RevCommit>();
Map<Feature, ObjectId> oids = new HashMap<Feature, ObjectId>();
for (Feature f : features) {
ObjectId oId = insertAndAdd(remoteGeogig.geogig, f);
oids.put(f, oId);
final RevCommit commit = remoteGeogig.geogig.command(CommitOp.class).setMessage(f.getIdentifier().toString()).call();
expected.addFirst(commit);
Optional<RevObject> childObject = remoteGeogig.geogig.command(RevObjectParse.class).setObjectId(oId).call();
assertTrue(childObject.isPresent());
}
// Make sure the remote has all of the commits
Iterator<RevCommit> logs = remoteGeogig.geogig.command(LogOp.class).call();
List<RevCommit> logged = new ArrayList<RevCommit>();
for (; logs.hasNext(); ) {
logged.add(logs.next());
}
assertEquals(expected, logged);
// Make sure the local repository has no commits prior to clone
logs = localGeogig.geogig.command(LogOp.class).call();
assertNotNull(logs);
assertFalse(logs.hasNext());
// clone from the remote
CloneOp clone = clone();
clone.setDepth(0);
clone.setRepositoryURL(remoteGeogig.envHome.getCanonicalPath()).setBranch("master").call();
// Because Cities.1 is first in our filter, then is modified to be outside the filter, it
// should continue to be tracked. Therefore our histories should match.
// Make sure the local repository got the correct commits
logs = localGeogig.geogig.command(LogOp.class).call();
logged = new ArrayList<RevCommit>();
for (; logs.hasNext(); ) {
logged.add(logs.next());
}
assertEquals(expected, logged);
assertExists(localGeogig, oids.get(city1), oids.get(city1_modified));
}
use of org.locationtech.geogig.api.RevObject in project GeoGig by boundlessgeo.
the class MergeFeatureResource method post.
public void post(Representation entity) {
InputStream input = null;
try {
input = getRequest().getEntity().getStream();
final GeoGIG ggit = getGeogig(getRequest()).get();
final Reader body = new InputStreamReader(input);
final JsonParser parser = new JsonParser();
final JsonElement conflictJson = parser.parse(body);
if (conflictJson.isJsonObject()) {
final JsonObject conflict = conflictJson.getAsJsonObject();
String featureId = null;
RevFeature ourFeature = null;
RevFeatureType ourFeatureType = null;
RevFeature theirFeature = null;
RevFeatureType theirFeatureType = null;
JsonObject merges = null;
if (conflict.has("path") && conflict.get("path").isJsonPrimitive()) {
featureId = conflict.get("path").getAsJsonPrimitive().getAsString();
}
Preconditions.checkState(featureId != null);
if (conflict.has("ours") && conflict.get("ours").isJsonPrimitive()) {
String ourCommit = conflict.get("ours").getAsJsonPrimitive().getAsString();
Optional<NodeRef> ourNode = parseID(ObjectId.valueOf(ourCommit), featureId, ggit);
if (ourNode.isPresent()) {
Optional<RevObject> object = ggit.command(RevObjectParse.class).setObjectId(ourNode.get().objectId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature);
ourFeature = (RevFeature) object.get();
object = ggit.command(RevObjectParse.class).setObjectId(ourNode.get().getMetadataId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType);
ourFeatureType = (RevFeatureType) object.get();
}
}
if (conflict.has("theirs") && conflict.get("theirs").isJsonPrimitive()) {
String theirCommit = conflict.get("theirs").getAsJsonPrimitive().getAsString();
Optional<NodeRef> theirNode = parseID(ObjectId.valueOf(theirCommit), featureId, ggit);
if (theirNode.isPresent()) {
Optional<RevObject> object = ggit.command(RevObjectParse.class).setObjectId(theirNode.get().objectId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature);
theirFeature = (RevFeature) object.get();
object = ggit.command(RevObjectParse.class).setObjectId(theirNode.get().getMetadataId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType);
theirFeatureType = (RevFeatureType) object.get();
}
}
if (conflict.has("merges") && conflict.get("merges").isJsonObject()) {
merges = conflict.get("merges").getAsJsonObject();
}
Preconditions.checkState(merges != null);
Preconditions.checkState(ourFeatureType != null || theirFeatureType != null);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) (ourFeatureType != null ? ourFeatureType.type() : theirFeatureType.type()));
ImmutableList<PropertyDescriptor> descriptors = (ourFeatureType == null ? theirFeatureType : ourFeatureType).sortedDescriptors();
for (Entry<String, JsonElement> entry : merges.entrySet()) {
int descriptorIndex = getDescriptorIndex(entry.getKey(), descriptors);
if (descriptorIndex != -1 && entry.getValue().isJsonObject()) {
PropertyDescriptor descriptor = descriptors.get(descriptorIndex);
JsonObject attributeObject = entry.getValue().getAsJsonObject();
if (attributeObject.has("ours") && attributeObject.get("ours").isJsonPrimitive() && attributeObject.get("ours").getAsBoolean()) {
featureBuilder.set(descriptor.getName(), ourFeature == null ? null : ourFeature.getValues().get(descriptorIndex).orNull());
} else if (attributeObject.has("theirs") && attributeObject.get("theirs").isJsonPrimitive() && attributeObject.get("theirs").getAsBoolean()) {
featureBuilder.set(descriptor.getName(), theirFeature == null ? null : theirFeature.getValues().get(descriptorIndex).orNull());
} else if (attributeObject.has("value") && attributeObject.get("value").isJsonPrimitive()) {
JsonPrimitive primitive = attributeObject.get("value").getAsJsonPrimitive();
if (primitive.isString()) {
try {
Object object = valueFromString(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsString());
featureBuilder.set(descriptor.getName(), object);
} catch (Exception e) {
throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
}
} else if (primitive.isNumber()) {
try {
Object value = valueFromNumber(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsNumber());
featureBuilder.set(descriptor.getName(), value);
} catch (Exception e) {
throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
}
} else if (primitive.isBoolean()) {
try {
Object value = valueFromBoolean(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsBoolean());
featureBuilder.set(descriptor.getName(), value);
} catch (Exception e) {
throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
}
} else if (primitive.isJsonNull()) {
featureBuilder.set(descriptor.getName(), null);
} else {
throw new Exception("Unsupported JSON type for attribute value (" + entry.getKey() + ")");
}
}
}
}
SimpleFeature feature = featureBuilder.buildFeature(NodeRef.nodeFromPath(featureId));
RevFeature revFeature = RevFeatureBuilder.build(feature);
ggit.getRepository().stagingDatabase().put(revFeature);
getResponse().setEntity(new StringRepresentation(revFeature.getId().toString(), MediaType.TEXT_PLAIN));
}
} catch (Exception e) {
throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e);
} finally {
if (input != null)
Closeables.closeQuietly(input);
}
}
use of org.locationtech.geogig.api.RevObject in project GeoGig by boundlessgeo.
the class MergeFeatureResource method parseID.
private Optional<NodeRef> parseID(ObjectId commitId, String path, GeoGIG geogig) {
Optional<RevObject> object = geogig.command(RevObjectParse.class).setObjectId(commitId).call();
RevCommit commit = null;
if (object.isPresent() && object.get() instanceof RevCommit) {
commit = (RevCommit) object.get();
} else {
throw new CommandSpecException("Couldn't resolve id: " + commitId.toString() + " to a commit");
}
object = geogig.command(RevObjectParse.class).setObjectId(commit.getTreeId()).call();
if (object.isPresent()) {
RevTree tree = (RevTree) object.get();
return geogig.command(FindTreeChild.class).setParent(tree).setChildPath(path).call();
} else {
throw new CommandSpecException("Couldn't resolve commit's treeId");
}
}
Aggregations