Search in sources :

Example 6 with AttributeDiff

use of org.locationtech.geogig.api.plumbing.diff.AttributeDiff in project GeoGig by boundlessgeo.

the class DefaultStepDefinitions method I_have_a_patch_file.

@Given("^I have a patch file$")
public void I_have_a_patch_file() throws Throwable {
    Patch patch = new Patch();
    String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID());
    Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
    Optional<?> oldValue = Optional.fromNullable(points1.getProperty("sp").getValue());
    GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, Optional.of("new"));
    map.put(pointsType.getDescriptor("sp"), diff);
    FeatureDiff feaureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(pointsType), RevFeatureTypeImpl.build(pointsType));
    patch.addModifiedFeature(feaureDiff);
    File file = new File(platform.pwd(), "test.patch");
    BufferedWriter writer = Files.newWriter(file, Charsets.UTF_8);
    PatchSerializer.write(writer, patch);
    writer.flush();
    writer.close();
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Given(cucumber.annotation.en.Given)

Example 7 with AttributeDiff

use of org.locationtech.geogig.api.plumbing.diff.AttributeDiff 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 8 with AttributeDiff

use of org.locationtech.geogig.api.plumbing.diff.AttributeDiff in project GeoGig by boundlessgeo.

the class ApplyPatchOp method getFeatureType.

private RevFeatureType getFeatureType(FeatureDiff diff, RevFeature oldFeature, RevFeatureType oldRevFeatureType) {
    List<String> removed = Lists.newArrayList();
    List<AttributeDescriptor> added = Lists.newArrayList();
    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() == TYPE.REMOVED) {
            removed.add(entry.getKey().getName().getLocalPart());
        } else if (entry.getValue().getType() == TYPE.ADDED) {
            PropertyDescriptor pd = entry.getKey();
            added.add((AttributeDescriptor) pd);
        }
    }
    SimpleFeatureType sft = (SimpleFeatureType) oldRevFeatureType.type();
    List<AttributeDescriptor> descriptors = (sft).getAttributeDescriptors();
    SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
    featureTypeBuilder.setCRS(sft.getCoordinateReferenceSystem());
    featureTypeBuilder.setDefaultGeometry(sft.getGeometryDescriptor().getLocalName());
    featureTypeBuilder.setName(sft.getName());
    for (int i = 0; i < descriptors.size(); i++) {
        AttributeDescriptor descriptor = descriptors.get(i);
        if (!removed.contains(descriptor.getName().getLocalPart())) {
            featureTypeBuilder.add(descriptor);
        }
    }
    for (AttributeDescriptor descriptor : added) {
        featureTypeBuilder.add(descriptor);
    }
    SimpleFeatureType featureType = featureTypeBuilder.buildFeatureType();
    return RevFeatureTypeImpl.build(featureType);
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) Entry(java.util.Map.Entry) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff)

Example 9 with AttributeDiff

use of org.locationtech.geogig.api.plumbing.diff.AttributeDiff 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 10 with AttributeDiff

use of org.locationtech.geogig.api.plumbing.diff.AttributeDiff in project GeoGig by boundlessgeo.

the class ResponseWriter method writeFeatureDiffResponse.

/**
     * Writes a set of feature diffs to the stream.
     * 
     * @param diffs a map of {@link PropertyDescriptor} to {@link AttributeDiffs} that specify the
     *        difference between two features
     * @throws XMLStreamException
     */
public void writeFeatureDiffResponse(Map<PropertyDescriptor, AttributeDiff> diffs) throws XMLStreamException {
    Set<Entry<PropertyDescriptor, AttributeDiff>> entries = diffs.entrySet();
    Iterator<Entry<PropertyDescriptor, AttributeDiff>> iter = entries.iterator();
    while (iter.hasNext()) {
        Entry<PropertyDescriptor, AttributeDiff> entry = iter.next();
        out.writeStartElement("diff");
        PropertyType attrType = entry.getKey().getType();
        if (attrType instanceof GeometryType) {
            writeElement("geometry", "true");
            GeometryType gt = (GeometryType) attrType;
            CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
            if (crs != null) {
                String crsCode = null;
                try {
                    crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
                } catch (FactoryException e) {
                    crsCode = null;
                }
                if (crsCode != null) {
                    writeElement("crs", "EPSG:" + crsCode);
                }
            }
        }
        writeElement("attributename", entry.getKey().getName().toString());
        writeElement("changetype", entry.getValue().getType().toString());
        if (entry.getValue().getOldValue() != null && entry.getValue().getOldValue().isPresent()) {
            writeElement("oldvalue", entry.getValue().getOldValue().get().toString());
        }
        if (entry.getValue().getNewValue() != null && entry.getValue().getNewValue().isPresent() && !entry.getValue().getType().equals(TYPE.NO_CHANGE)) {
            writeElement("newvalue", entry.getValue().getNewValue().get().toString());
        }
        out.writeEndElement();
    }
}
Also used : GeometryType(org.opengis.feature.type.GeometryType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) FactoryException(org.opengis.referencing.FactoryException) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) PropertyType(org.opengis.feature.type.PropertyType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Aggregations

AttributeDiff (org.locationtech.geogig.api.plumbing.diff.AttributeDiff)17 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)17 FeatureDiff (org.locationtech.geogig.api.plumbing.diff.FeatureDiff)15 GenericAttributeDiffImpl (org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl)11 Patch (org.locationtech.geogig.api.plumbing.diff.Patch)10 Test (org.junit.Test)9 RevFeature (org.locationtech.geogig.api.RevFeature)8 Optional (com.google.common.base.Optional)5 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)5 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)5 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)5 Entry (java.util.Map.Entry)4 NodeRef (org.locationtech.geogig.api.NodeRef)4 CannotApplyPatchException (org.locationtech.geogig.api.porcelain.CannotApplyPatchException)4 Node (org.locationtech.geogig.api.Node)3 ObjectId (org.locationtech.geogig.api.ObjectId)3 RevObject (org.locationtech.geogig.api.RevObject)3 RevTree (org.locationtech.geogig.api.RevTree)3 DiffFeature (org.locationtech.geogig.api.plumbing.DiffFeature)3 TYPE (org.locationtech.geogig.api.RevObject.TYPE)2