Search in sources :

Example 36 with PropertyDescriptor

use of org.opengis.feature.type.PropertyDescriptor in project GeoGig by boundlessgeo.

the class VerifyPatchOp method _call.

/**
     * Executes the verify command
     * 
     * @return the result of checking if the patch can be applied
     */
protected VerifyPatchResults _call() throws RuntimeException {
    Preconditions.checkArgument(patch != null, "No patch file provided");
    Patch patch = reverse ? this.patch.reversed() : this.patch;
    Patch toApply = new Patch();
    Patch toReject = new Patch();
    for (RevFeatureType ft : patch.getFeatureTypes()) {
        toApply.addFeatureType(ft);
        toReject.addFeatureType(ft);
    }
    String path;
    Optional<RevObject> obj;
    List<FeatureDiff> diffs = patch.getModifiedFeatures();
    for (FeatureDiff diff : diffs) {
        path = diff.getPath();
        String refSpec = Ref.WORK_HEAD + ":" + path;
        obj = command(RevObjectParse.class).setRefSpec(refSpec).call();
        if (!obj.isPresent()) {
            toReject.addModifiedFeature(diff);
            break;
        }
        RevFeature feature = (RevFeature) obj.get();
        DepthSearch depthSearch = new DepthSearch(stagingDatabase());
        Optional<NodeRef> noderef = depthSearch.find(workingTree().getTree(), path);
        RevFeatureType featureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
        ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
        Set<Entry<PropertyDescriptor, AttributeDiff>> attrDiffs = diff.getDiffs().entrySet();
        boolean ok = true;
        for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = attrDiffs.iterator(); iterator.hasNext(); ) {
            Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
            AttributeDiff attrDiff = entry.getValue();
            PropertyDescriptor descriptor = entry.getKey();
            switch(attrDiff.getType()) {
                case ADDED:
                    if (descriptors.contains(descriptor)) {
                        ok = false;
                    }
                    break;
                case REMOVED:
                case MODIFIED:
                    if (!descriptors.contains(descriptor)) {
                        ok = false;
                        break;
                    }
                    for (int i = 0; i < descriptors.size(); i++) {
                        if (descriptors.get(i).equals(descriptor)) {
                            Optional<Object> value = feature.getValues().get(i);
                            if (!attrDiff.canBeAppliedOn(value)) {
                                ok = false;
                            }
                            break;
                        }
                    }
            }
        }
        if (!ok) {
            toReject.addModifiedFeature(diff);
        } else {
            toApply.addModifiedFeature(diff);
        }
    }
    List<FeatureInfo> added = patch.getAddedFeatures();
    for (FeatureInfo feature : added) {
        String refSpec = Ref.WORK_HEAD + ":" + feature.getPath();
        obj = command(RevObjectParse.class).setRefSpec(refSpec).call();
        if (obj.isPresent()) {
            toReject.addAddedFeature(feature.getPath(), feature.getFeature(), feature.getFeatureType());
        } else {
            toApply.addAddedFeature(feature.getPath(), feature.getFeature(), feature.getFeatureType());
        }
    }
    List<FeatureInfo> removed = patch.getRemovedFeatures();
    for (FeatureInfo feature : removed) {
        String refSpec = Ref.WORK_HEAD + ":" + feature.getPath();
        obj = command(RevObjectParse.class).setRefSpec(refSpec).call();
        if (!obj.isPresent()) {
            toReject.addRemovedFeature(feature.getPath(), feature.getFeature(), feature.getFeatureType());
        } else {
            RevFeature revFeature = (RevFeature) obj.get();
            DepthSearch depthSearch = new DepthSearch(stagingDatabase());
            Optional<NodeRef> noderef = depthSearch.find(workingTree().getTree(), feature.getPath());
            RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
            RevFeature patchRevFeature = RevFeatureBuilder.build(feature.getFeature());
            if (revFeature.equals(patchRevFeature) && revFeatureType.equals(feature.getFeatureType())) {
                toApply.addRemovedFeature(feature.getPath(), feature.getFeature(), feature.getFeatureType());
            } else {
                toReject.addRemovedFeature(feature.getPath(), feature.getFeature(), feature.getFeatureType());
            }
        }
    }
    ImmutableList<FeatureTypeDiff> alteredTrees = patch.getAlteredTrees();
    for (FeatureTypeDiff diff : alteredTrees) {
        DepthSearch depthSearch = new DepthSearch(stagingDatabase());
        Optional<NodeRef> noderef = depthSearch.find(workingTree().getTree(), diff.getPath());
        ObjectId metadataId = noderef.isPresent() ? noderef.get().getMetadataId() : ObjectId.NULL;
        if (Objects.equal(metadataId, diff.getOldFeatureType())) {
            toApply.addAlteredTree(diff);
        } else {
            toReject.addAlteredTree(diff);
        }
    }
    return new VerifyPatchResults(toApply, toReject);
}
Also used : FeatureInfo(org.locationtech.geogig.api.FeatureInfo) NodeRef(org.locationtech.geogig.api.NodeRef) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) DepthSearch(org.locationtech.geogig.repository.DepthSearch) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject)

Example 37 with PropertyDescriptor

use of org.opengis.feature.type.PropertyDescriptor in project GeoGig by boundlessgeo.

the class MergeFeaturesOp method merge.

@SuppressWarnings("unchecked")
private Feature merge(RevFeature featureA, RevFeature featureB, RevFeature ancestor, RevFeatureType featureType) {
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) featureType.type());
    ImmutableList<Optional<Object>> valuesA = featureA.getValues();
    ImmutableList<Optional<Object>> valuesB = featureB.getValues();
    ImmutableList<Optional<Object>> valuesAncestor = ancestor.getValues();
    ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
    for (int i = 0; i < descriptors.size(); i++) {
        PropertyDescriptor descriptor = descriptors.get(i);
        boolean isGeom = Geometry.class.isAssignableFrom(descriptor.getType().getBinding());
        Name name = descriptor.getName();
        Optional<Object> valueAncestor = valuesAncestor.get(i);
        Optional<Object> valueA = valuesA.get(i);
        Optional<Object> valueB = valuesB.get(i);
        if (!valueA.equals(valueAncestor)) {
            Optional<Object> merged = valueA;
            if (isGeom && !valueB.equals(valueAncestor)) {
                // true merge is only done with
                // geometries
                GeometryAttributeDiff diffB = new GeometryAttributeDiff(Optional.fromNullable((Geometry) valueAncestor.orNull()), Optional.fromNullable((Geometry) valueB.orNull()));
                merged = (Optional<Object>) diffB.applyOn(valueA);
            }
            featureBuilder.set(name, merged.orNull());
        } else {
            featureBuilder.set(name, valueB.orNull());
        }
    }
    return featureBuilder.buildFeature(nodeRefA.name());
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GeometryAttributeDiff(org.locationtech.geogig.api.plumbing.diff.GeometryAttributeDiff) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Name(org.opengis.feature.type.Name)

Example 38 with PropertyDescriptor

use of org.opengis.feature.type.PropertyDescriptor in project GeoGig by boundlessgeo.

the class ReportCommitConflictsOp method _call.

@Override
protected MergeScenarioReport _call() {
    MergeScenarioReport report = new MergeScenarioReport();
    ObjectId parentCommitId = ObjectId.NULL;
    if (commit.getParentIds().size() > 0) {
        parentCommitId = commit.getParentIds().get(0);
    }
    ObjectId parentTreeId = ObjectId.NULL;
    Repository repository = repository();
    if (repository.commitExists(parentCommitId)) {
        parentTreeId = repository.getCommit(parentCommitId).getTreeId();
    }
    // get changes
    Iterator<DiffEntry> diffs = command(DiffTree.class).setOldTree(parentTreeId).setNewTree(commit.getTreeId()).setReportTrees(true).call();
    while (diffs.hasNext()) {
        DiffEntry diff = diffs.next();
        String path = diff.oldPath() == null ? diff.newPath() : diff.oldPath();
        Optional<RevObject> obj = command(RevObjectParse.class).setRefSpec(Ref.HEAD + ":" + path).call();
        switch(diff.changeType()) {
            case ADDED:
                if (obj.isPresent()) {
                    TYPE type = command(ResolveObjectType.class).setObjectId(diff.getNewObject().objectId()).call();
                    if (TYPE.TREE.equals(type)) {
                        NodeRef headVersion = command(FindTreeChild.class).setChildPath(path).setParent(repository.getOrCreateHeadTree()).call().get();
                        if (!headVersion.getMetadataId().equals(diff.getNewObject().getMetadataId())) {
                            report.addConflict(new Conflict(path, ObjectId.NULL, diff.getNewObject().getMetadataId(), headVersion.getMetadataId()));
                        }
                    } else {
                        if (!obj.get().getId().equals(diff.newObjectId())) {
                            report.addConflict(new Conflict(path, ObjectId.NULL, diff.newObjectId(), obj.get().getId()));
                        }
                    }
                } else {
                    report.addUnconflicted(diff);
                }
                break;
            case REMOVED:
                if (obj.isPresent()) {
                    if (obj.get().getId().equals(diff.oldObjectId())) {
                        report.addUnconflicted(diff);
                    } else {
                        report.addConflict(new Conflict(path, diff.oldObjectId(), ObjectId.NULL, obj.get().getId()));
                    }
                }
                break;
            case MODIFIED:
                TYPE type = command(ResolveObjectType.class).setObjectId(diff.getNewObject().objectId()).call();
                if (TYPE.TREE.equals(type)) {
                    // one
                    if (!diff.isChange()) {
                        report.addUnconflicted(diff);
                    }
                } else {
                    String refSpec = Ref.HEAD + ":" + path;
                    obj = command(RevObjectParse.class).setRefSpec(refSpec).call();
                    if (!obj.isPresent()) {
                        // git reports this as a conflict but does not mark as conflicted, just adds
                        // the missing file.
                        // We add it and consider it unconflicted
                        report.addUnconflicted(diff);
                        break;
                    }
                    RevFeature feature = (RevFeature) obj.get();
                    DepthSearch depthSearch = new DepthSearch(repository.objectDatabase());
                    Optional<NodeRef> noderef = depthSearch.find(this.workingTree().getTree(), path);
                    RevFeatureType featureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
                    ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
                    FeatureDiff featureDiff = command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).setNewVersion(Suppliers.ofInstance(diff.getNewObject())).call();
                    Set<Entry<PropertyDescriptor, AttributeDiff>> attrDiffs = featureDiff.getDiffs().entrySet();
                    RevFeature newFeature = command(RevObjectParse.class).setObjectId(diff.newObjectId()).call(RevFeature.class).get();
                    boolean ok = true;
                    for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = attrDiffs.iterator(); iterator.hasNext() && ok; ) {
                        Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
                        AttributeDiff attrDiff = entry.getValue();
                        PropertyDescriptor descriptor = entry.getKey();
                        switch(attrDiff.getType()) {
                            case ADDED:
                                if (descriptors.contains(descriptor)) {
                                    ok = false;
                                }
                                break;
                            case REMOVED:
                            case MODIFIED:
                                if (!descriptors.contains(descriptor)) {
                                    ok = false;
                                    break;
                                }
                                for (int i = 0; i < descriptors.size(); i++) {
                                    if (descriptors.get(i).equals(descriptor)) {
                                        Optional<Object> value = feature.getValues().get(i);
                                        Optional<Object> newValue = newFeature.getValues().get(i);
                                        if (!newValue.equals(value)) {
                                            // check
                                            if (!attrDiff.canBeAppliedOn(value)) {
                                                ok = false;
                                            }
                                            break;
                                        }
                                    }
                                }
                        }
                    }
                    if (ok) {
                        report.addUnconflicted(diff);
                    } else {
                        report.addConflict(new Conflict(path, diff.oldObjectId(), diff.newObjectId(), obj.get().getId()));
                    }
                }
                break;
        }
    }
    return report;
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) TYPE(org.locationtech.geogig.api.RevObject.TYPE) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) Repository(org.locationtech.geogig.repository.Repository) DepthSearch(org.locationtech.geogig.repository.DepthSearch) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject)

Example 39 with PropertyDescriptor

use of org.opengis.feature.type.PropertyDescriptor in project GeoGig by boundlessgeo.

the class PatchSerializer method addDifference.

private static void addDifference(String s, Map<PropertyDescriptor, AttributeDiff> map, RevFeatureType oldRevFeatureType, RevFeatureType newRevFeatureType) {
    String[] tokens = s.split("\t");
    PropertyDescriptor descriptor = oldRevFeatureType.type().getDescriptor(tokens[0]);
    if (descriptor == null) {
        descriptor = newRevFeatureType.type().getDescriptor(tokens[0]);
    }
    AttributeDiff ad = AttributeDiffFactory.attributeDiffFromText(descriptor.getType().getBinding(), s.substring(s.indexOf("\t") + 1));
    map.put(descriptor, ad);
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor)

Example 40 with PropertyDescriptor

use of org.opengis.feature.type.PropertyDescriptor in project GeoGig by boundlessgeo.

the class FeatureDiffWeb method run.

/**
     * Runs the command and builds the appropriate response
     * 
     * @param context - the context to use for this command
     * 
     * @throws CommandSpecException
     */
@Override
public void run(CommandContext context) {
    if (path == null || path.trim().isEmpty()) {
        throw new CommandSpecException("No path for feature name specifed");
    }
    final Context geogig = this.getCommandLocator(context);
    ObjectId newId = geogig.command(ResolveTreeish.class).setTreeish(newTreeish).call().get();
    ObjectId oldId = geogig.command(ResolveTreeish.class).setTreeish(oldTreeish).call().get();
    RevFeature newFeature = null;
    RevFeatureType newFeatureType = null;
    RevFeature oldFeature = null;
    RevFeatureType oldFeatureType = null;
    final Map<PropertyDescriptor, AttributeDiff> diffs;
    Optional<NodeRef> ref = parseID(newId, geogig);
    Optional<RevObject> object;
    // need these to determine if the feature was added or removed so I can build the diffs
    // myself until the FeatureDiff supports null values
    boolean removed = false;
    boolean added = false;
    if (ref.isPresent()) {
        object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call();
        if (object.isPresent() && object.get() instanceof RevFeatureType) {
            newFeatureType = (RevFeatureType) object.get();
        } else {
            throw new CommandSpecException("Couldn't resolve newCommit's featureType");
        }
        object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call();
        if (object.isPresent() && object.get() instanceof RevFeature) {
            newFeature = (RevFeature) object.get();
        } else {
            throw new CommandSpecException("Couldn't resolve newCommit's feature");
        }
    } else {
        removed = true;
    }
    if (!oldId.equals(ObjectId.NULL)) {
        ref = parseID(oldId, geogig);
        if (ref.isPresent()) {
            object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call();
            if (object.isPresent() && object.get() instanceof RevFeatureType) {
                oldFeatureType = (RevFeatureType) object.get();
            } else {
                throw new CommandSpecException("Couldn't resolve oldCommit's featureType");
            }
            object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call();
            if (object.isPresent() && object.get() instanceof RevFeature) {
                oldFeature = (RevFeature) object.get();
            } else {
                throw new CommandSpecException("Couldn't resolve oldCommit's feature");
            }
        } else {
            added = true;
        }
    } else {
        added = true;
    }
    if (removed) {
        Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>();
        ImmutableList<PropertyDescriptor> attributes = oldFeatureType.sortedDescriptors();
        ImmutableList<Optional<Object>> values = oldFeature.getValues();
        for (int index = 0; index < attributes.size(); index++) {
            Optional<Object> value = values.get(index);
            if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) {
                Optional<Geometry> temp = Optional.absent();
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(Optional.fromNullable((Geometry) value.orNull()), temp));
                }
            } else {
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(value, Optional.absent()));
                }
            }
        }
        diffs = tempDiffs;
    } else if (added) {
        Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>();
        ImmutableList<PropertyDescriptor> attributes = newFeatureType.sortedDescriptors();
        ImmutableList<Optional<Object>> values = newFeature.getValues();
        for (int index = 0; index < attributes.size(); index++) {
            Optional<Object> value = values.get(index);
            if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) {
                Optional<Geometry> temp = Optional.absent();
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(temp, Optional.fromNullable((Geometry) value.orNull())));
                }
            } else {
                if (value.isPresent() || all) {
                    tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(Optional.absent(), value));
                }
            }
        }
        diffs = tempDiffs;
    } else {
        FeatureDiff diff = new FeatureDiff(path, newFeature, oldFeature, newFeatureType, oldFeatureType, all);
        diffs = diff.getDiffs();
    }
    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start();
            out.writeFeatureDiffResponse(diffs);
            out.finish();
        }
    });
}
Also used : HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) ResolveTreeish(org.locationtech.geogig.api.plumbing.ResolveTreeish) RevFeature(org.locationtech.geogig.api.RevFeature) GeometryAttributeDiff(org.locationtech.geogig.api.plumbing.diff.GeometryAttributeDiff) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) GeometryAttributeDiff(org.locationtech.geogig.api.plumbing.diff.GeometryAttributeDiff) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) Geometry(com.vividsolutions.jts.geom.Geometry) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)47 RevFeature (org.locationtech.geogig.api.RevFeature)24 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)23 Optional (com.google.common.base.Optional)18 AttributeDiff (org.locationtech.geogig.api.plumbing.diff.AttributeDiff)17 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)16 FeatureDiff (org.locationtech.geogig.api.plumbing.diff.FeatureDiff)15 Test (org.junit.Test)12 RevObject (org.locationtech.geogig.api.RevObject)12 GenericAttributeDiffImpl (org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl)11 NodeRef (org.locationtech.geogig.api.NodeRef)10 Patch (org.locationtech.geogig.api.plumbing.diff.Patch)10 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)9 ObjectId (org.locationtech.geogig.api.ObjectId)8 Entry (java.util.Map.Entry)7 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)6 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)6 GeometryType (org.opengis.feature.type.GeometryType)6 PropertyType (org.opengis.feature.type.PropertyType)6