Search in sources :

Example 81 with RevFeature

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

the class ResponseWriter method writeConflicts.

/**
     * Writes the response for a set of conflicts while also supplying the geometry.
     * 
     * @param geogig - a CommandLocator to call commands from
     * @param conflicts - a Conflict iterator to build the response from
     * @throws XMLStreamException
     */
public void writeConflicts(final Context geogig, Iterator<Conflict> conflicts, final ObjectId ours, final ObjectId theirs) throws XMLStreamException {
    Iterator<GeometryConflict> conflictIterator = Iterators.transform(conflicts, new Function<Conflict, GeometryConflict>() {

        @Override
        public GeometryConflict apply(Conflict input) {
            ObjectId commitId = ours;
            if (input.getOurs().equals(ObjectId.NULL)) {
                commitId = theirs;
            }
            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();
            Optional<NodeRef> node = Optional.absent();
            if (object.isPresent()) {
                RevTree tree = (RevTree) object.get();
                node = geogig.command(FindTreeChild.class).setParent(tree).setChildPath(input.getPath()).call();
            } else {
                throw new CommandSpecException("Couldn't resolve commit's treeId");
            }
            RevFeatureType type = null;
            RevFeature feature = null;
            if (node.isPresent()) {
                object = geogig.command(RevObjectParse.class).setObjectId(node.get().getMetadataId()).call();
                if (object.isPresent() && object.get() instanceof RevFeatureType) {
                    type = (RevFeatureType) object.get();
                } else {
                    throw new CommandSpecException("Couldn't resolve newCommit's featureType");
                }
                object = geogig.command(RevObjectParse.class).setObjectId(node.get().objectId()).call();
                if (object.isPresent() && object.get() instanceof RevFeature) {
                    feature = (RevFeature) object.get();
                } else {
                    throw new CommandSpecException("Couldn't resolve newCommit's feature");
                }
            }
            GeometryConflict conflict = null;
            if (feature != null && type != null) {
                String crsCode = null;
                Collection<PropertyDescriptor> attribs = type.type().getDescriptors();
                for (PropertyDescriptor attrib : attribs) {
                    PropertyType attrType = attrib.getType();
                    if (attrType instanceof GeometryType) {
                        GeometryType gt = (GeometryType) attrType;
                        CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
                        if (crs != null) {
                            try {
                                crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
                            } catch (FactoryException e) {
                                crsCode = null;
                            }
                            if (crsCode != null) {
                                crsCode = "EPSG:" + crsCode;
                            }
                        }
                        break;
                    }
                }
                FeatureBuilder builder = new FeatureBuilder(type);
                GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(feature.getId().toString(), feature);
                Geometry geom = null;
                List<Object> attributes = simpleFeature.getAttributes();
                for (Object attribute : attributes) {
                    if (attribute instanceof Geometry) {
                        geom = (Geometry) attribute;
                        break;
                    }
                }
                conflict = new GeometryConflict(input, geom, crsCode);
            }
            return conflict;
        }
    });
    while (conflictIterator.hasNext()) {
        GeometryConflict next = conflictIterator.next();
        if (next != null) {
            out.writeStartElement("Feature");
            writeElement("change", "CONFLICT");
            writeElement("id", next.getConflict().getPath());
            writeElement("ourvalue", next.getConflict().getOurs().toString());
            writeElement("theirvalue", next.getConflict().getTheirs().toString());
            writeElement("geometry", next.getGeometry().toText());
            if (next.getCRS() != null) {
                writeElement("crs", next.getCRS());
            }
            out.writeEndElement();
        }
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) FactoryException(org.opengis.referencing.FactoryException) PropertyType(org.opengis.feature.type.PropertyType) GeometryType(org.opengis.feature.type.GeometryType) RevFeature(org.locationtech.geogig.api.RevFeature) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeogigSimpleFeature(org.locationtech.geogig.api.GeogigSimpleFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevCommit(org.locationtech.geogig.api.RevCommit) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ObjectId(org.locationtech.geogig.api.ObjectId) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) Geometry(com.vividsolutions.jts.geom.Geometry) Conflict(org.locationtech.geogig.api.plumbing.merge.Conflict) Collection(java.util.Collection) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) RevTree(org.locationtech.geogig.api.RevTree)

Example 82 with RevFeature

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

the class GeoGigAPI method getFeaturesToCommit.

/**
     * Returns an array with the features that are staged and ready to be commited. If noDeletions
     * is true, it doesn't include features to be removed, only those ones to be added or modified,
     * so it can be used to check the new data that will get commited into the repository
     * 
     * @return
     */
public Feature[] getFeaturesToCommit(String path, boolean noDeletions) {
    DiffOp diffOp = repository.command(DiffOp.class);
    diffOp.setCompareIndex(true);
    diffOp.setFilter(path);
    Iterator<DiffEntry> diffs = diffOp.call();
    List<Feature> list = Lists.newArrayList();
    while (diffs.hasNext()) {
        DiffEntry diff = diffs.next();
        if (!diff.changeType().equals(ChangeType.REMOVED) || !noDeletions) {
            RevFeature revFeature = repository.command(RevObjectParse.class).setObjectId(diff.newObjectId()).call(RevFeature.class).get();
            RevFeatureType revFeatureType = repository.command(RevObjectParse.class).setObjectId(diff.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
            FeatureBuilder builder = new FeatureBuilder(revFeatureType);
            list.add(builder.build(diff.getNewObject().name(), revFeature));
        }
    }
    return list.toArray(new Feature[0]);
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 83 with RevFeature

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

the class GeoGigAPI method getFeatureFromHead.

/**
     * Returns a feature from the Head of the repository, given its full path
     * 
     * Returns null if the given path doesn't resolve to a feature
     * 
     * @param path the path to the feature to return
     */
public Feature getFeatureFromHead(String path) {
    String name = NodeRef.nodeFromPath(path);
    String refSpec = "HEAD:" + path;
    Optional<RevFeature> revFeature = repository.command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class);
    if (revFeature.isPresent()) {
        RevFeatureType revFeatureType = repository.command(ResolveFeatureType.class).setRefSpec(refSpec).call().get();
        FeatureBuilder builder = new FeatureBuilder(revFeatureType);
        return builder.build(name, revFeature.get());
    } else {
        return null;
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ResolveFeatureType(org.locationtech.geogig.api.plumbing.ResolveFeatureType) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 84 with RevFeature

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

the class ApplyPatchOpTest method testReversedPatch.

@Test
public void testReversedPatch() throws Exception {
    insert(points1, points2);
    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);
    String removedPath = NodeRef.appendChild(pointsName, points2.getIdentifier().getID());
    patch.addRemovedFeature(removedPath, points2, RevFeatureTypeImpl.build(pointsType));
    String addedPath = NodeRef.appendChild(pointsName, points3.getIdentifier().getID());
    patch.addAddedFeature(addedPath, points3, RevFeatureTypeImpl.build(pointsType));
    geogig.command(ApplyPatchOp.class).setPatch(patch).call();
    geogig.command(ApplyPatchOp.class).setPatch(patch.reversed()).call();
    RevTree root = repo.workingTree().getTree();
    Optional<Node> featureBlobId = findTreeChild(root, removedPath);
    assertTrue(featureBlobId.isPresent());
    featureBlobId = findTreeChild(root, addedPath);
    assertFalse(featureBlobId.isPresent());
    Optional<RevFeature> feature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + path).call(RevFeature.class);
    assertTrue(feature.isPresent());
    assertEquals(oldValue, feature.get().getValues().get(0));
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) Node(org.locationtech.geogig.api.Node) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Example 85 with RevFeature

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

the class ApplyPatchOpTest method testRemoveFeatureAttributePatch.

@Test
public void testRemoveFeatureAttributePatch() throws Exception {
    insert(points1B);
    Patch patch = new Patch();
    String path = NodeRef.appendChild(pointsName, points1B.getIdentifier().getID());
    Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
    Optional<?> oldValue = Optional.fromNullable(points1B.getProperty("extra").getValue());
    GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, null);
    map.put(modifiedPointsType.getDescriptor("extra"), diff);
    FeatureDiff featureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(modifiedPointsType), RevFeatureTypeImpl.build(pointsType));
    patch.addModifiedFeature(featureDiff);
    geogig.command(ApplyPatchOp.class).setPatch(patch).call();
    Optional<RevFeature> feature = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + path).call(RevFeature.class);
    assertTrue(feature.isPresent());
    ImmutableList<Optional<Object>> values = feature.get().getValues();
    assertEquals(points1.getProperties().size(), values.size());
    assertFalse(values.contains("ExtraString"));
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) Test(org.junit.Test)

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