Search in sources :

Example 66 with RevObject

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;
}
Also used : DelegateFeatureIterator(org.geotools.feature.collection.DelegateFeatureIterator) Optional(com.google.common.base.Optional) RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) IOException(java.io.IOException) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) ProgressListener(org.locationtech.geogig.api.ProgressListener) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) BaseFeatureCollection(org.geotools.feature.collection.BaseFeatureCollection) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree) StagingDatabase(org.locationtech.geogig.storage.StagingDatabase)

Example 67 with RevObject

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();
    }
}
Also used : RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) Node(org.locationtech.geogig.api.Node) Stopwatch(com.google.common.base.Stopwatch) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) NodeRef(org.locationtech.geogig.api.NodeRef) TreeSet(java.util.TreeSet) ExecutorService(java.util.concurrent.ExecutorService) RevFeature(org.locationtech.geogig.api.RevFeature) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Map(java.util.Map) RevTree(org.locationtech.geogig.api.RevTree)

Example 68 with RevObject

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));
}
Also used : CloneOp(org.locationtech.geogig.api.porcelain.CloneOp) HashMap(java.util.HashMap) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) LogOp(org.locationtech.geogig.api.porcelain.LogOp) ArrayList(java.util.ArrayList) Feature(org.opengis.feature.Feature) LinkedList(java.util.LinkedList) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 69 with RevObject

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);
    }
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) NodeRef(org.locationtech.geogig.api.NodeRef) StringRepresentation(org.restlet.resource.StringRepresentation) RestletException(org.locationtech.geogig.rest.RestletException) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) JsonParser(com.google.gson.JsonParser) InputStreamReader(java.io.InputStreamReader) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) RevObject(org.locationtech.geogig.api.RevObject) InputStream(java.io.InputStream) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RestletException(org.locationtech.geogig.rest.RestletException) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) JsonElement(com.google.gson.JsonElement) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) JsonObject(com.google.gson.JsonObject) RevObject(org.locationtech.geogig.api.RevObject) GeoGIG(org.locationtech.geogig.api.GeoGIG) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 70 with RevObject

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");
    }
}
Also used : RevObject(org.locationtech.geogig.api.RevObject) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit)

Aggregations

RevObject (org.locationtech.geogig.api.RevObject)84 ObjectId (org.locationtech.geogig.api.ObjectId)51 RevCommit (org.locationtech.geogig.api.RevCommit)30 NodeRef (org.locationtech.geogig.api.NodeRef)22 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)22 RevTree (org.locationtech.geogig.api.RevTree)21 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)17 RevFeature (org.locationtech.geogig.api.RevFeature)17 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)16 Feature (org.opengis.feature.Feature)16 IOException (java.io.IOException)15 LinkedList (java.util.LinkedList)15 LogOp (org.locationtech.geogig.api.porcelain.LogOp)14 GeoGIG (org.locationtech.geogig.api.GeoGIG)13 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)11 HashMap (java.util.HashMap)10 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)10 Optional (com.google.common.base.Optional)8 ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)8