Search in sources :

Example 41 with RevFeatureType

use of org.locationtech.geogig.api.RevFeatureType 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)

Example 42 with RevFeatureType

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

the class CommitOpTest method testCommitAddsFeatureTypeToObjectDatabase.

@Test
public void testCommitAddsFeatureTypeToObjectDatabase() throws Exception {
    insertAndAdd(points1);
    ObjectId id = RevFeatureTypeImpl.build(pointsType).getId();
    geogig.command(AddOp.class).addPattern(".").call();
    RevCommit commit = geogig.command(CommitOp.class).call();
    assertNotNull(commit);
    RevFeatureType type = geogig.getRepository().objectDatabase().getFeatureType(id);
    assertEquals(id, type.getId());
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 43 with RevFeatureType

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

the class ShpExportDiff method getFeatureType.

private SimpleFeatureType getFeatureType(String path, GeogigCLI cli) {
    checkParameter(path != null, "No path specified.");
    String refspec;
    if (path.contains(":")) {
        refspec = path;
    } else {
        refspec = "WORK_HEAD:" + path;
    }
    checkParameter(!refspec.endsWith(":"), "No path specified.");
    final GeoGIG geogig = cli.getGeogig();
    Optional<ObjectId> rootTreeId = geogig.command(ResolveTreeish.class).setTreeish(refspec.split(":")[0]).call();
    checkParameter(rootTreeId.isPresent(), "Couldn't resolve '" + refspec + "' to a treeish object");
    RevTree rootTree = geogig.getRepository().getTree(rootTreeId.get());
    Optional<NodeRef> featureTypeTree = geogig.command(FindTreeChild.class).setChildPath(refspec.split(":")[1]).setParent(rootTree).setIndex(true).call();
    checkParameter(featureTypeTree.isPresent(), "pathspec '" + refspec.split(":")[1] + "' did not match any valid path");
    Optional<RevObject> revObject = cli.getGeogig().command(RevObjectParse.class).setObjectId(featureTypeTree.get().getMetadataId()).call();
    if (revObject.isPresent() && revObject.get() instanceof RevFeatureType) {
        RevFeatureType revFeatureType = (RevFeatureType) revObject.get();
        if (revFeatureType.type() instanceof SimpleFeatureType) {
            return (SimpleFeatureType) revFeatureType.type();
        } else {
            throw new InvalidParameterException("Cannot find feature type for the specified path");
        }
    } else {
        throw new InvalidParameterException("Cannot find feature type for the specified path");
    }
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) GeoGIG(org.locationtech.geogig.api.GeoGIG) RevTree(org.locationtech.geogig.api.RevTree)

Example 44 with RevFeatureType

use of org.locationtech.geogig.api.RevFeatureType 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 45 with RevFeatureType

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

the class CachingModuleTest method ft.

private static RevFeatureType ft(String name) {
    SimpleFeatureType type;
    try {
        type = DataUtilities.createType("", name, RepositoryTestCase.pointsTypeSpec);
    } catch (SchemaException e) {
        throw Throwables.propagate(e);
    }
    RevFeatureType rft = RevFeatureTypeImpl.build(type);
    return rft;
}
Also used : SchemaException(org.geotools.feature.SchemaException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Aggregations

RevFeatureType (org.locationtech.geogig.api.RevFeatureType)88 RevFeature (org.locationtech.geogig.api.RevFeature)49 NodeRef (org.locationtech.geogig.api.NodeRef)40 ObjectId (org.locationtech.geogig.api.ObjectId)34 Test (org.junit.Test)31 Optional (com.google.common.base.Optional)28 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)26 RevObject (org.locationtech.geogig.api.RevObject)24 RevTree (org.locationtech.geogig.api.RevTree)24 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)22 SimpleFeature (org.opengis.feature.simple.SimpleFeature)19 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)17 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)14 ImmutableList (com.google.common.collect.ImmutableList)13 File (java.io.File)13 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)13 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)13 List (java.util.List)12 GeoGIG (org.locationtech.geogig.api.GeoGIG)12 AddOp (org.locationtech.geogig.api.porcelain.AddOp)12