Search in sources :

Example 31 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class OSMAplyDiffOpTest method testApplyChangeset.

@Test
public void testApplyChangeset() throws Exception {
    String filename = getClass().getResource("nodes_for_changeset2.xml").getFile();
    File file = new File(filename);
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
    long unstaged = geogig.getRepository().workingTree().countUnstaged("node").count();
    assertTrue(unstaged > 0);
    Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/2059114068").call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    revFeature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/507464865").call(RevFeature.class);
    assertFalse(revFeature.isPresent());
    String changesetFilename = getClass().getResource("changeset.xml").getFile();
    geogig.command(OSMApplyDiffOp.class).setDiffFile(new File(changesetFilename)).call();
    revFeature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/2059114068").call(RevFeature.class);
    assertFalse(revFeature.isPresent());
    revFeature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/507464865").call(RevFeature.class);
    assertTrue(revFeature.isPresent());
}
Also used : RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) File(java.io.File) Test(org.junit.Test)

Example 32 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class ApplyPatchOp method applyPatch.

private void applyPatch(Patch patch) {
    final WorkingTree workTree = workingTree();
    final StagingDatabase indexDb = stagingDatabase();
    if (reverse) {
        patch = patch.reversed();
    }
    List<FeatureInfo> removed = patch.getRemovedFeatures();
    for (FeatureInfo feature : removed) {
        workTree.delete(NodeRef.parentPath(feature.getPath()), NodeRef.nodeFromPath(feature.getPath()));
    }
    List<FeatureInfo> added = patch.getAddedFeatures();
    for (FeatureInfo feature : added) {
        workTree.insert(NodeRef.parentPath(feature.getPath()), feature.getFeature());
    }
    List<FeatureDiff> diffs = patch.getModifiedFeatures();
    for (FeatureDiff diff : diffs) {
        String path = diff.getPath();
        DepthSearch depthSearch = new DepthSearch(indexDb);
        Optional<NodeRef> noderef = depthSearch.find(workTree.getTree(), path);
        RevFeatureType oldRevFeatureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
        String refSpec = Ref.WORK_HEAD + ":" + path;
        RevFeature feature = command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class).get();
        RevFeatureType newRevFeatureType = getFeatureType(diff, feature, oldRevFeatureType);
        ImmutableList<Optional<Object>> values = feature.getValues();
        ImmutableList<PropertyDescriptor> oldDescriptors = oldRevFeatureType.sortedDescriptors();
        ImmutableList<PropertyDescriptor> newDescriptors = newRevFeatureType.sortedDescriptors();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) newRevFeatureType.type());
        Map<Name, Optional<?>> attrs = Maps.newHashMap();
        for (int i = 0; i < oldDescriptors.size(); i++) {
            PropertyDescriptor descriptor = oldDescriptors.get(i);
            if (newDescriptors.contains(descriptor)) {
                Optional<Object> value = values.get(i);
                attrs.put(descriptor.getName(), value);
            }
        }
        Set<Entry<PropertyDescriptor, AttributeDiff>> featureDiffs = diff.getDiffs().entrySet();
        for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = featureDiffs.iterator(); iterator.hasNext(); ) {
            Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
            if (!entry.getValue().getType().equals(TYPE.REMOVED)) {
                Optional<?> oldValue = attrs.get(entry.getKey().getName());
                attrs.put(entry.getKey().getName(), entry.getValue().applyOn(oldValue));
            }
        }
        Set<Entry<Name, Optional<?>>> entries = attrs.entrySet();
        for (Iterator<Entry<Name, Optional<?>>> iterator = entries.iterator(); iterator.hasNext(); ) {
            Entry<Name, Optional<?>> entry = iterator.next();
            featureBuilder.set(entry.getKey(), entry.getValue().orNull());
        }
        SimpleFeature featureToInsert = featureBuilder.buildFeature(NodeRef.nodeFromPath(path));
        workTree.insert(NodeRef.parentPath(path), featureToInsert);
    }
    ImmutableList<FeatureTypeDiff> alteredTrees = patch.getAlteredTrees();
    for (FeatureTypeDiff diff : alteredTrees) {
        Optional<RevFeatureType> featureType;
        if (diff.getOldFeatureType().isNull()) {
            featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
            workTree.createTypeTree(diff.getPath(), featureType.get().type());
        } else if (diff.getNewFeatureType().isNull()) {
            workTree.delete(diff.getPath());
        } else {
            featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
            workTree.updateTypeTree(diff.getPath(), featureType.get().type());
        }
    }
}
Also used : FeatureInfo(org.locationtech.geogig.api.FeatureInfo) Name(org.opengis.feature.type.Name) WorkingTree(org.locationtech.geogig.repository.WorkingTree) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) NodeRef(org.locationtech.geogig.api.NodeRef) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) StagingDatabase(org.locationtech.geogig.storage.StagingDatabase) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DepthSearch(org.locationtech.geogig.repository.DepthSearch) FeatureTypeDiff(org.locationtech.geogig.api.plumbing.diff.FeatureTypeDiff) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 33 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class BlameOp method _call.

@Override
protected BlameReport _call() {
    String fullPath = (commit != null ? commit.toString() : Ref.HEAD) + ":" + path;
    Optional<ObjectId> id = command(RevParse.class).setRefSpec(fullPath).call();
    if (!id.isPresent()) {
        throw new BlameException(StatusCode.FEATURE_NOT_FOUND);
    }
    TYPE type = command(ResolveObjectType.class).setObjectId(id.get()).call();
    if (!type.equals(TYPE.FEATURE)) {
        throw new BlameException(StatusCode.PATH_NOT_FEATURE);
    }
    Optional<RevFeatureType> featureType = command(ResolveFeatureType.class).setRefSpec(path).call();
    BlameReport report = new BlameReport(featureType.get());
    Iterator<RevCommit> log = command(LogOp.class).addPath(path).setUntil(commit).call();
    RevCommit commit = log.next();
    RevObjectParse revObjectParse = command(RevObjectParse.class);
    DiffOp diffOp = command(DiffOp.class);
    DiffFeature diffFeature = command(DiffFeature.class);
    while (!report.isComplete()) {
        if (!log.hasNext()) {
            String refSpec = commit.getId().toString() + ":" + path;
            RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
            report.setFirstVersion(feature, commit);
            break;
        }
        RevCommit commitB = log.next();
        Iterator<DiffEntry> diffs = diffOp.setNewVersion(commit.getId()).setOldVersion(commitB.getId()).setReportTrees(false).call();
        while (diffs.hasNext()) {
            DiffEntry diff = diffs.next();
            if (path.equals(diff.newPath())) {
                if (diff.isAdd()) {
                    String refSpec = commit.getId().toString() + ":" + path;
                    RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
                    report.setFirstVersion(feature, commit);
                    break;
                }
                FeatureDiff featureDiff = diffFeature.setNewVersion(Suppliers.ofInstance(diff.getNewObject())).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).call();
                Map<PropertyDescriptor, AttributeDiff> attribDiffs = featureDiff.getDiffs();
                Iterator<PropertyDescriptor> iter = attribDiffs.keySet().iterator();
                while (iter.hasNext()) {
                    PropertyDescriptor key = iter.next();
                    Optional<?> value = attribDiffs.get(key).getNewValue();
                    String attribute = key.getName().toString();
                    report.addDiff(attribute, value, commit);
                }
            }
        }
        commit = commitB;
    }
    return report;
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ObjectId(org.locationtech.geogig.api.ObjectId) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) TYPE(org.locationtech.geogig.api.RevObject.TYPE) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 34 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class FeatureNodeRefFromRefspec method getFeatureFromRefSpec.

private Optional<RevFeature> getFeatureFromRefSpec() {
    Optional<RevObject> revObject = command(RevObjectParse.class).setRefSpec(ref).call(RevObject.class);
    if (!revObject.isPresent()) {
        // let's try to see if it is a feature in the working tree
        NodeRef.checkValidPath(ref);
        Optional<NodeRef> elementRef = command(FindTreeChild.class).setParent(workingTree().getTree()).setChildPath(ref).setIndex(true).call();
        Preconditions.checkArgument(elementRef.isPresent(), "Invalid reference: %s", ref);
        ObjectId id = elementRef.get().objectId();
        revObject = command(RevObjectParse.class).setObjectId(id).call(RevObject.class);
    }
    if (revObject.isPresent()) {
        Preconditions.checkArgument(TYPE.FEATURE.equals(revObject.get().getType()), "%s does not resolve to a feature", ref);
        return Optional.of(RevFeature.class.cast(revObject.get()));
    } else {
        return Optional.absent();
    }
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse)

Example 35 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class PatchSerializer method addElement.

private static void addElement(List<String> lines, Patch patch, Map<String, RevFeatureType> featureTypes) {
    String[] headerTokens = lines.get(0).split("\t");
    if (headerTokens.length == 4 || headerTokens.length == 3) {
        // modified // modification
        if (lines.size() == 1) {
            // feature type
            FeatureTypeDiff diff = new FeatureTypeDiff(headerTokens[0], ObjectId.valueOf(headerTokens[1]), ObjectId.valueOf(headerTokens[2]));
            patch.addAlteredTree(diff);
        } else {
            // feature
            String element = Joiner.on("\n").join(lines.subList(1, lines.size()));
            ByteArrayInputStream stream;
            stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8));
            String operation = headerTokens[0].trim();
            if (operation.equals("M")) {
                String fullPath = headerTokens[1].trim();
                String oldMetadataId = headerTokens[2].trim();
                String newMetadataId = headerTokens[3].trim();
                RevFeatureType newRevFeatureType = featureTypes.get(newMetadataId);
                RevFeatureType oldRevFeatureType = featureTypes.get(oldMetadataId);
                Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
                for (int i = 1; i < lines.size(); i++) {
                    addDifference(lines.get(i), map, oldRevFeatureType, newRevFeatureType);
                }
                FeatureDiff featureDiff = new FeatureDiff(fullPath, map, oldRevFeatureType, newRevFeatureType);
                patch.addModifiedFeature(featureDiff);
            } else if (operation.equals("A") || operation.equals("R")) {
                String fullPath = headerTokens[1].trim();
                String featureTypeId = headerTokens[2].trim();
                RevFeatureType revFeatureType;
                revFeatureType = featureTypes.get(featureTypeId);
                FeatureBuilder featureBuilder = new FeatureBuilder(revFeatureType);
                ObjectReader<RevFeature> reader = factory.createFeatureReader();
                RevFeature revFeature = reader.read(null, stream);
                Feature feature = featureBuilder.build(NodeRef.nodeFromPath(fullPath), revFeature);
                if (operation.equals("R")) {
                    patch.addRemovedFeature(fullPath, feature, revFeatureType);
                } else {
                    patch.addAddedFeature(fullPath, feature, revFeatureType);
                }
            } else {
                throw new IllegalArgumentException("Wrong patch content: " + lines.get(0));
            }
        }
    } else if (headerTokens.length == 1) {
        // feature type definition
        String element = Joiner.on("\n").join(lines);
        ByteArrayInputStream stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8));
        String[] tokens = lines.get(1).split("\t");
        ObjectReader<RevFeatureType> reader = factory.createFeatureTypeReader();
        RevFeatureType featureType = reader.read(null, stream);
        featureTypes.put(featureType.getId().toString(), featureType);
    } else {
        throw new IllegalArgumentException("Wrong patch content: " + lines.get(0));
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) RevFeature(org.locationtech.geogig.api.RevFeature) Feature(org.opengis.feature.Feature) ByteArrayInputStream(java.io.ByteArrayInputStream) RevFeature(org.locationtech.geogig.api.RevFeature) ObjectReader(org.locationtech.geogig.storage.ObjectReader) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Aggregations

RevFeature (org.locationtech.geogig.api.RevFeature)89 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)46 Test (org.junit.Test)45 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)43 Optional (com.google.common.base.Optional)40 NodeRef (org.locationtech.geogig.api.NodeRef)32 File (java.io.File)24 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)24 ObjectId (org.locationtech.geogig.api.ObjectId)21 AddOp (org.locationtech.geogig.api.porcelain.AddOp)21 ImmutableList (com.google.common.collect.ImmutableList)20 Feature (org.opengis.feature.Feature)20 List (java.util.List)19 RevObject (org.locationtech.geogig.api.RevObject)18 RevTree (org.locationtech.geogig.api.RevTree)16 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)16 SimpleFeature (org.opengis.feature.simple.SimpleFeature)16 ArrayList (java.util.ArrayList)15 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)15 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)15