Search in sources :

Example 51 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.

the class GeogigFeatureStore method addFeatures.

@Override
public final List<FeatureId> addFeatures(FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection) throws IOException {
    if (Transaction.AUTO_COMMIT.equals(getTransaction())) {
        throw new UnsupportedOperationException("GeoGIG does not support AUTO_COMMIT");
    }
    Preconditions.checkState(getDataStore().isAllowTransactions(), "Transactions not supported; head is not a local branch");
    final WorkingTree workingTree = delegate.getWorkingTree();
    final String path = delegate.getTypeTreePath();
    ProgressListener listener = new DefaultProgressListener();
    final List<FeatureId> insertedFids = Lists.newArrayList();
    List<Node> deferringTarget = new AbstractList<Node>() {

        @Override
        public boolean add(Node node) {
            String fid = node.getName();
            String version = node.getObjectId().toString();
            insertedFids.add(new FeatureIdVersionedImpl(fid, version));
            return true;
        }

        @Override
        public Node get(int index) {
            throw new UnsupportedOperationException();
        }

        @Override
        public int size() {
            return 0;
        }
    };
    Integer count = (Integer) null;
    FeatureIterator<SimpleFeature> featureIterator = featureCollection.features();
    try {
        Iterator<SimpleFeature> features;
        features = new FeatureIteratorIterator<SimpleFeature>(featureIterator);
        /*
             * Make sure to transform the incoming features to the native schema to avoid situations
             * where geogig would change the metadataId of the RevFeature nodes due to small
             * differences in the default and incoming schema such as namespace or missing
             * properties
             */
        final SimpleFeatureType nativeSchema = delegate.getNativeType();
        features = Iterators.transform(features, new SchemaInforcer(nativeSchema));
        workingTree.insert(path, features, listener, deferringTarget, count);
    } catch (Exception e) {
        throw new IOException(e);
    } finally {
        featureIterator.close();
    }
    return insertedFids;
}
Also used : AbstractList(java.util.AbstractList) Node(org.locationtech.geogig.api.Node) DefaultProgressListener(org.locationtech.geogig.api.DefaultProgressListener) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) IOException(java.io.IOException) FeatureId(org.opengis.filter.identity.FeatureId) WorkingTree(org.locationtech.geogig.repository.WorkingTree) DefaultProgressListener(org.locationtech.geogig.api.DefaultProgressListener) ProgressListener(org.locationtech.geogig.api.ProgressListener) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureIdVersionedImpl(org.geotools.filter.identity.FeatureIdVersionedImpl)

Example 52 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.

the class GeogigFeatureStore method modifyFeatures.

@Override
public void modifyFeatures(Name[] names, Object[] values, Filter filter) throws IOException {
    Preconditions.checkState(getDataStore().isAllowTransactions(), "Transactions not supported; head is not a local branch");
    final WorkingTree workingTree = delegate.getWorkingTree();
    final String path = delegate.getTypeTreePath();
    Iterator<SimpleFeature> features = modifyingFeatureIterator(names, values, filter);
    /*
         * Make sure to transform the incoming features to the native schema to avoid situations
         * where geogig would change the metadataId of the RevFeature nodes due to small differences
         * in the default and incoming schema such as namespace or missing properties
         */
    final SimpleFeatureType nativeSchema = delegate.getNativeType();
    features = Iterators.transform(features, new SchemaInforcer(nativeSchema));
    try {
        ProgressListener listener = new DefaultProgressListener();
        Integer count = (Integer) null;
        List<Node> target = (List<Node>) null;
        workingTree.insert(path, features, listener, target, count);
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : Node(org.locationtech.geogig.api.Node) DefaultProgressListener(org.locationtech.geogig.api.DefaultProgressListener) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) IOException(java.io.IOException) WorkingTree(org.locationtech.geogig.repository.WorkingTree) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DefaultProgressListener(org.locationtech.geogig.api.DefaultProgressListener) ProgressListener(org.locationtech.geogig.api.ProgressListener) AbstractList(java.util.AbstractList) List(java.util.List)

Example 53 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.

the class GeogigFeatureSource method getBoundsInternal.

@Override
protected ReferencedEnvelope getBoundsInternal(Query query) throws IOException {
    final Filter filter = (Filter) query.getFilter().accept(new SimplifyingFilterVisitor(), null);
    final CoordinateReferenceSystem crs = getSchema().getCoordinateReferenceSystem();
    if (Filter.INCLUDE.equals(filter) && oldRoot == null && ChangeType.ADDED.equals(changeType())) {
        NodeRef typeRef = getTypeRef();
        ReferencedEnvelope bounds = new ReferencedEnvelope(crs);
        typeRef.getNode().expand(bounds);
        return bounds;
    }
    if (Filter.EXCLUDE.equals(filter)) {
        return ReferencedEnvelope.create(crs);
    }
    FeatureReader<SimpleFeatureType, SimpleFeature> features;
    if (isNaturalOrder(query.getSortBy())) {
        Integer offset = query.getStartIndex();
        Integer maxFeatures = query.getMaxFeatures() == Integer.MAX_VALUE ? null : query.getMaxFeatures();
        ScreenMap screenMap = (ScreenMap) query.getHints().get(Hints.SCREENMAP);
        features = getNativeReader(Query.NO_NAMES, filter, offset, maxFeatures, screenMap);
    } else {
        features = getReader(query);
    }
    ReferencedEnvelope bounds = new ReferencedEnvelope(crs);
    try {
        while (features.hasNext()) {
            bounds.expandToInclude((ReferencedEnvelope) features.next().getBounds());
        }
    } finally {
        features.close();
    }
    return bounds;
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Filter(org.opengis.filter.Filter) ScreenMap(org.geotools.renderer.ScreenMap) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) SimplifyingFilterVisitor(org.geotools.filter.visitor.SimplifyingFilterVisitor) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Example 54 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature 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 55 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.

the class ExportOp method _call.

/**
     * Executes the export operation using the parameters that have been specified.
     * 
     * @return a FeatureCollection with the specified features
     */
@Override
protected SimpleFeatureStore _call() {
    final StagingDatabase database = stagingDatabase();
    if (filterFeatureTypeId != null) {
        RevObject filterType = database.getIfPresent(filterFeatureTypeId);
        checkArgument(filterType instanceof RevFeatureType, "Provided filter feature type is does not exist");
    }
    final SimpleFeatureStore targetStore = getTargetStore();
    final String refspec = resolveRefSpec();
    final String treePath = refspec.substring(refspec.indexOf(':') + 1);
    final RevTree rootTree = resolveRootTree(refspec);
    final NodeRef typeTreeRef = resolTypeTreeRef(refspec, treePath, rootTree);
    final ObjectId defaultMetadataId = typeTreeRef.getMetadataId();
    final RevTree typeTree = database.getTree(typeTreeRef.objectId());
    final ProgressListener progressListener = getProgressListener();
    progressListener.started();
    progressListener.setDescription("Exporting from " + path + " to " + targetStore.getName().getLocalPart() + "... ");
    FeatureCollection<SimpleFeatureType, SimpleFeature> asFeatureCollection = new BaseFeatureCollection<SimpleFeatureType, SimpleFeature>() {

        @Override
        public FeatureIterator<SimpleFeature> features() {
            final Iterator<SimpleFeature> plainFeatures = getFeatures(typeTree, database, defaultMetadataId, progressListener);
            Iterator<SimpleFeature> adaptedFeatures = adaptToArguments(plainFeatures, defaultMetadataId);
            Iterator<Optional<Feature>> transformed = Iterators.transform(adaptedFeatures, ExportOp.this.function);
            Iterator<SimpleFeature> filtered = Iterators.filter(Iterators.transform(transformed, new Function<Optional<Feature>, SimpleFeature>() {

                @Override
                public SimpleFeature apply(Optional<Feature> input) {
                    return (SimpleFeature) (input.isPresent() ? input.get() : null);
                }
            }), Predicates.notNull());
            return new DelegateFeatureIterator<SimpleFeature>(filtered);
        }
    };
    // add the feature collection to the feature store
    final Transaction transaction;
    if (transactional) {
        transaction = new DefaultTransaction("create");
    } else {
        transaction = Transaction.AUTO_COMMIT;
    }
    try {
        targetStore.setTransaction(transaction);
        try {
            targetStore.addFeatures(asFeatureCollection);
            transaction.commit();
        } catch (final Exception e) {
            if (transactional) {
                transaction.rollback();
            }
            Throwables.propagateIfInstanceOf(e, GeoToolsOpException.class);
            throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
        } finally {
            transaction.close();
        }
    } catch (IOException e) {
        throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
    }
    progressListener.complete();
    return targetStore;
}
Also used : DelegateFeatureIterator(org.geotools.feature.collection.DelegateFeatureIterator) Optional(com.google.common.base.Optional) RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) IOException(java.io.IOException) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) ProgressListener(org.locationtech.geogig.api.ProgressListener) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) BaseFeatureCollection(org.geotools.feature.collection.BaseFeatureCollection) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree) StagingDatabase(org.locationtech.geogig.storage.StagingDatabase)

Aggregations

SimpleFeature (org.opengis.feature.simple.SimpleFeature)66 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)32 Test (org.junit.Test)27 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)21 RevFeature (org.locationtech.geogig.api.RevFeature)17 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)16 Optional (com.google.common.base.Optional)15 Coordinate (com.vividsolutions.jts.geom.Coordinate)15 NodeRef (org.locationtech.geogig.api.NodeRef)13 ObjectId (org.locationtech.geogig.api.ObjectId)13 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)12 File (java.io.File)10 ArrayList (java.util.ArrayList)10 DefaultTransaction (org.geotools.data.DefaultTransaction)10 WorkingTree (org.locationtech.geogig.repository.WorkingTree)10 IOException (java.io.IOException)9 List (java.util.List)9 Feature (org.opengis.feature.Feature)9 Function (com.google.common.base.Function)8 Transaction (org.geotools.data.Transaction)8