Search in sources :

Example 6 with FeatureBuilder

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

the class ExportOp method getFeatures.

private static Iterator<SimpleFeature> getFeatures(final RevTree typeTree, final ObjectDatabase database, final ObjectId defaultMetadataId, final ProgressListener progressListener) {
    Iterator<NodeRef> nodes = new DepthTreeIterator("", defaultMetadataId, typeTree, database, Strategy.FEATURES_ONLY);
    // progress reporting
    nodes = Iterators.transform(nodes, new Function<NodeRef, NodeRef>() {

        private AtomicInteger count = new AtomicInteger();

        @Override
        public NodeRef apply(NodeRef input) {
            progressListener.setProgress((count.incrementAndGet() * 100.f) / typeTree.size());
            return input;
        }
    });
    Function<NodeRef, SimpleFeature> asFeature = new Function<NodeRef, SimpleFeature>() {

        private Map<ObjectId, FeatureBuilder> ftCache = Maps.newHashMap();

        @Override
        @Nullable
        public SimpleFeature apply(final NodeRef input) {
            final ObjectId metadataId = input.getMetadataId();
            final RevFeature revFeature = database.getFeature(input.objectId());
            FeatureBuilder featureBuilder = getBuilderFor(metadataId);
            Feature feature = featureBuilder.build(input.name(), revFeature);
            feature.getUserData().put(Hints.USE_PROVIDED_FID, true);
            feature.getUserData().put(RevFeature.class, revFeature);
            feature.getUserData().put(RevFeatureType.class, featureBuilder.getType());
            if (feature instanceof SimpleFeature) {
                return (SimpleFeature) feature;
            }
            return null;
        }

        private FeatureBuilder getBuilderFor(final ObjectId metadataId) {
            FeatureBuilder featureBuilder = ftCache.get(metadataId);
            if (featureBuilder == null) {
                RevFeatureType revFtype = database.getFeatureType(metadataId);
                featureBuilder = new FeatureBuilder(revFtype);
                ftCache.put(metadataId, featureBuilder);
            }
            return featureBuilder;
        }
    };
    Iterator<SimpleFeature> asFeatures = Iterators.transform(nodes, asFeature);
    UnmodifiableIterator<SimpleFeature> filterNulls = Iterators.filter(asFeatures, Predicates.notNull());
    return filterNulls;
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RevFeature(org.locationtech.geogig.api.RevFeature) DepthTreeIterator(org.locationtech.geogig.api.plumbing.diff.DepthTreeIterator) Map(java.util.Map) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 7 with FeatureBuilder

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

the class CreatePatchOp method _call.

@Override
protected Patch _call() {
    Patch patch = new Patch();
    Map<ObjectId, RevFeatureType> featureTypes = Maps.newHashMap();
    while (diffs.hasNext()) {
        DiffEntry diffEntry = diffs.next();
        final NodeRef newObject = diffEntry.getNewObject();
        final NodeRef oldObject = diffEntry.getOldObject();
        if (diffEntry.changeType() == ChangeType.MODIFIED) {
            RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.newObjectId()).call().get();
            if (revObject instanceof RevFeature) {
                FeatureDiff diff = command(DiffFeature.class).setNewVersion(Suppliers.ofInstance(diffEntry.getNewObject())).setOldVersion(Suppliers.ofInstance(diffEntry.getOldObject())).call();
                patch.addModifiedFeature(diff);
            } else if (revObject instanceof RevTree) {
                RevFeatureType oldFeatureType = command(RevObjectParse.class).setObjectId(diffEntry.getOldObject().getMetadataId()).call(RevFeatureType.class).get();
                RevFeatureType newFeatureType = command(RevObjectParse.class).setObjectId(diffEntry.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
                patch.addFeatureType(oldFeatureType);
                patch.addFeatureType(newFeatureType);
                patch.addAlteredTree(diffEntry);
            }
        } else if (diffEntry.changeType() == ChangeType.ADDED) {
            RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.newObjectId()).call().get();
            if (revObject instanceof RevFeature) {
                RevFeatureType featureType;
                if (featureTypes.containsKey(newObject.getMetadataId())) {
                    featureType = featureTypes.get(newObject.getMetadataId());
                } else {
                    featureType = command(RevObjectParse.class).setObjectId(newObject.getMetadataId()).call(RevFeatureType.class).get();
                    featureTypes.put(newObject.getMetadataId(), featureType);
                }
                FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
                Feature feature = featureBuilder.build(diffEntry.newObjectId().toString(), (RevFeature) revObject);
                String name = diffEntry.newPath();
                patch.addAddedFeature(name, feature, featureType);
            } else if (revObject instanceof RevTree) {
                ObjectId metadataId = diffEntry.getNewObject().getMetadataId();
                if (!metadataId.isNull()) {
                    RevFeatureType featureType = command(RevObjectParse.class).setObjectId(metadataId).call(RevFeatureType.class).get();
                    patch.addAlteredTree(diffEntry);
                    patch.addFeatureType(featureType);
                }
            }
        } else if (diffEntry.changeType() == ChangeType.REMOVED) {
            RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.oldObjectId()).call().get();
            if (revObject instanceof RevFeature) {
                RevFeatureType featureType;
                if (featureTypes.containsKey(oldObject.getMetadataId())) {
                    featureType = featureTypes.get(oldObject.getMetadataId());
                } else {
                    featureType = command(RevObjectParse.class).setObjectId(oldObject.getMetadataId()).call(RevFeatureType.class).get();
                    featureTypes.put(oldObject.getMetadataId(), featureType);
                }
                FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
                Feature feature = featureBuilder.build(diffEntry.oldObjectId().toString(), (RevFeature) revObject);
                String name = diffEntry.oldPath();
                patch.addRemovedFeature(name, feature, featureType);
            } else if (revObject instanceof RevTree) {
                ObjectId metadataId = diffEntry.getOldObject().getMetadataId();
                if (!metadataId.isNull()) {
                    RevFeatureType featureType = command(RevObjectParse.class).setObjectId(metadataId).call(RevFeatureType.class).get();
                    patch.addAlteredTree(diffEntry);
                    patch.addFeatureType(featureType);
                }
            }
        }
    }
    return patch;
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 8 with FeatureBuilder

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

the class ImportOp method alter.

/**
     * Translates a feature pointed by a node from its original feature type to a given one, using
     * values from those attributes that exist in both original and destination feature type. New
     * attributes are populated with null values
     * 
     * @param node The node that points to the feature. No checking is performed to ensure the node
     *        points to a feature instead of other type
     * @param featureType the destination feature type
     * @return a feature with the passed feature type and data taken from the input feature
     */
private Feature alter(NodeRef node, RevFeatureType featureType) {
    RevFeature oldFeature = command(RevObjectParse.class).setObjectId(node.objectId()).call(RevFeature.class).get();
    RevFeatureType oldFeatureType;
    oldFeatureType = command(RevObjectParse.class).setObjectId(node.getMetadataId()).call(RevFeatureType.class).get();
    ImmutableList<PropertyDescriptor> oldAttributes = oldFeatureType.sortedDescriptors();
    ImmutableList<PropertyDescriptor> newAttributes = featureType.sortedDescriptors();
    ImmutableList<Optional<Object>> oldValues = oldFeature.getValues();
    List<Optional<Object>> newValues = Lists.newArrayList();
    for (int i = 0; i < newAttributes.size(); i++) {
        int idx = oldAttributes.indexOf(newAttributes.get(i));
        if (idx != -1) {
            Optional<Object> oldValue = oldValues.get(idx);
            newValues.add(oldValue);
        } else {
            newValues.add(Optional.absent());
        }
    }
    RevFeature newFeature = RevFeatureImpl.build(ImmutableList.copyOf(newValues));
    FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
    Feature feature = featureBuilder.build(node.name(), newFeature);
    return feature;
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) DecoratingFeature(org.geotools.feature.DecoratingFeature) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 9 with FeatureBuilder

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

the class ExportOp method alter.

private Iterator<SimpleFeature> alter(Iterator<SimpleFeature> plainFeatures, final ObjectId targetFeatureTypeId) {
    final RevFeatureType targetType = stagingDatabase().getFeatureType(targetFeatureTypeId);
    Function<SimpleFeature, SimpleFeature> alterFunction = new Function<SimpleFeature, SimpleFeature>() {

        @Override
        public SimpleFeature apply(SimpleFeature input) {
            final RevFeatureType oldFeatureType;
            oldFeatureType = (RevFeatureType) input.getUserData().get(RevFeatureType.class);
            final ObjectId metadataId = oldFeatureType.getId();
            if (targetType.getId().equals(metadataId)) {
                return input;
            }
            final RevFeature oldFeature;
            oldFeature = (RevFeature) input.getUserData().get(RevFeature.class);
            ImmutableList<PropertyDescriptor> oldAttributes = oldFeatureType.sortedDescriptors();
            ImmutableList<PropertyDescriptor> newAttributes = targetType.sortedDescriptors();
            ImmutableList<Optional<Object>> oldValues = oldFeature.getValues();
            List<Optional<Object>> newValues = Lists.newArrayList();
            for (int i = 0; i < newAttributes.size(); i++) {
                int idx = oldAttributes.indexOf(newAttributes.get(i));
                if (idx != -1) {
                    Optional<Object> oldValue = oldValues.get(idx);
                    newValues.add(oldValue);
                } else {
                    newValues.add(Optional.absent());
                }
            }
            RevFeature newFeature = RevFeatureImpl.build(ImmutableList.copyOf(newValues));
            FeatureBuilder featureBuilder = new FeatureBuilder(targetType);
            SimpleFeature feature = (SimpleFeature) featureBuilder.build(input.getID(), newFeature);
            return feature;
        }
    };
    return Iterators.transform(plainFeatures, alterFunction);
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Function(com.google.common.base.Function) RevFeature(org.locationtech.geogig.api.RevFeature) RevObject(org.locationtech.geogig.api.RevObject) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 10 with FeatureBuilder

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

the class WorkingTree method updateTypeTree.

/**
     * Updates the definition of a Feature type associated as default feature type to a given path.
     * It also modifies the metadataId associated to features under the passed path, which used the
     * previous default feature type.
     * 
     * @param path the path
     * @param featureType the new feature type definition to set as default for the passed path
     */
public NodeRef updateTypeTree(final String treePath, final FeatureType featureType) {
    // TODO: This is not the optimal way of doing this. A better solution should be found.
    final RevTree workHead = getTree();
    Optional<NodeRef> typeTreeRef = context.command(FindTreeChild.class).setIndex(true).setParent(workHead).setChildPath(treePath).call();
    Preconditions.checkArgument(typeTreeRef.isPresent(), "Tree does not exist: %s", treePath);
    Iterator<NodeRef> iter = context.command(LsTreeOp.class).setReference(treePath).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).call();
    final RevFeatureType revType = RevFeatureTypeImpl.build(featureType);
    indexDatabase.put(revType);
    final ObjectId metadataId = revType.getId();
    RevTreeBuilder treeBuilder = new RevTreeBuilder(indexDatabase);
    final RevTree newTree = treeBuilder.build();
    ObjectId newWorkHeadId = context.command(WriteBack.class).setToIndex(true).setAncestor(workHead.builder(indexDatabase)).setChildPath(treePath).setTree(newTree).setMetadataId(metadataId).call();
    updateWorkHead(newWorkHeadId);
    Map<ObjectId, FeatureBuilder> featureBuilders = Maps.newHashMap();
    while (iter.hasNext()) {
        NodeRef noderef = iter.next();
        RevFeature feature = context.command(RevObjectParse.class).setObjectId(noderef.objectId()).call(RevFeature.class).get();
        if (!featureBuilders.containsKey(noderef.getMetadataId())) {
            RevFeatureType ft = context.command(RevObjectParse.class).setObjectId(noderef.getMetadataId()).call(RevFeatureType.class).get();
            featureBuilders.put(noderef.getMetadataId(), new FeatureBuilder(ft));
        }
        FeatureBuilder fb = featureBuilders.get(noderef.getMetadataId());
        String parentPath = NodeRef.parentPath(NodeRef.appendChild(treePath, noderef.path()));
        insert(parentPath, fb.build(noderef.getNode().getName(), feature));
    }
    return context.command(FindTreeChild.class).setIndex(true).setParent(getTree()).setChildPath(treePath).call().get();
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) NodeRef(org.locationtech.geogig.api.NodeRef) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree)

Aggregations

FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)15 RevFeature (org.locationtech.geogig.api.RevFeature)15 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)13 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)9 Feature (org.opengis.feature.Feature)8 ObjectId (org.locationtech.geogig.api.ObjectId)7 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)6 NodeRef (org.locationtech.geogig.api.NodeRef)5 RevFeatureBuilder (org.locationtech.geogig.api.RevFeatureBuilder)5 RevObject (org.locationtech.geogig.api.RevObject)5 Optional (com.google.common.base.Optional)4 RevTree (org.locationtech.geogig.api.RevTree)4 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)4 SimpleFeature (org.opengis.feature.simple.SimpleFeature)4 Function (com.google.common.base.Function)3 Geometry (com.vividsolutions.jts.geom.Geometry)3 Collection (java.util.Collection)3 GeogigSimpleFeature (org.locationtech.geogig.api.GeogigSimpleFeature)3 FindTreeChild (org.locationtech.geogig.api.plumbing.FindTreeChild)3 GeometryType (org.opengis.feature.type.GeometryType)3