Search in sources :

Example 91 with Feature

use of org.opengis.feature.Feature in project GeoGig by boundlessgeo.

the class GlobalState method insert.

public static List<ObjectId> insert(Feature... features) throws Exception {
    geogigCLI.close();
    GeoGIG geogig = geogigCLI.newGeoGIG(Hints.readWrite());
    Preconditions.checkNotNull(geogig);
    List<ObjectId> ids = Lists.newArrayListWithCapacity(features.length);
    try {
        Repository repository = geogig.getRepository();
        final WorkingTree workTree = repository.workingTree();
        for (Feature f : features) {
            Name name = f.getType().getName();
            String parentPath = name.getLocalPart();
            Node ref = workTree.insert(parentPath, f);
            ObjectId objectId = ref.getObjectId();
            ids.add(objectId);
        }
    } finally {
        geogig.close();
    }
    return ids;
}
Also used : WorkingTree(org.locationtech.geogig.repository.WorkingTree) Repository(org.locationtech.geogig.repository.Repository) ObjectId(org.locationtech.geogig.api.ObjectId) Node(org.locationtech.geogig.api.Node) Feature(org.opengis.feature.Feature) GeoGIG(org.locationtech.geogig.api.GeoGIG) Name(org.opengis.feature.type.Name)

Example 92 with Feature

use of org.opengis.feature.Feature in project GeoGig by boundlessgeo.

the class ShpExport method getTransformingFunction.

private Function<Feature, Optional<Feature>> getTransformingFunction(final SimpleFeatureType featureType) {
    Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

        @Override
        @Nullable
        public Optional<Feature> apply(@Nullable Feature feature) {
            SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType);
            for (Property property : feature.getProperties()) {
                if (property instanceof GeometryAttribute) {
                    builder.set(featureType.getGeometryDescriptor().getName(), property.getValue());
                } else {
                    String name = property.getName().getLocalPart();
                    if (name.length() > 10) {
                        name = name.substring(0, 10);
                    }
                    builder.set(name, property.getValue());
                }
            }
            Feature modifiedFeature = builder.buildFeature(feature.getIdentifier().getID());
            return Optional.fromNullable(modifiedFeature);
        }
    };
    return function;
}
Also used : Function(com.google.common.base.Function) Optional(com.google.common.base.Optional) GeometryAttribute(org.opengis.feature.GeometryAttribute) Feature(org.opengis.feature.Feature) Property(org.opengis.feature.Property) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 93 with Feature

use of org.opengis.feature.Feature in project GeoGig by boundlessgeo.

the class ShpExportDiff method getTransformingFunction.

private Function<Feature, Optional<Feature>> getTransformingFunction(final SimpleFeatureType featureType) {
    Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

        @Override
        @Nullable
        public Optional<Feature> apply(@Nullable Feature feature) {
            SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType);
            for (Property property : feature.getProperties()) {
                if (property instanceof GeometryAttribute) {
                    builder.set(featureType.getGeometryDescriptor().getName(), property.getValue());
                } else {
                    builder.set(property.getName(), property.getValue());
                }
            }
            Feature modifiedFeature = builder.buildFeature(feature.getIdentifier().getID());
            return Optional.fromNullable(modifiedFeature);
        }
    };
    return function;
}
Also used : Function(com.google.common.base.Function) Optional(com.google.common.base.Optional) GeometryAttribute(org.opengis.feature.GeometryAttribute) Feature(org.opengis.feature.Feature) Property(org.opengis.feature.Property) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 94 with Feature

use of org.opengis.feature.Feature 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)

Example 95 with Feature

use of org.opengis.feature.Feature in project GeoGig by boundlessgeo.

the class ImportOp method _call.

/**
     * Executes the import operation using the parameters that have been specified. Features will be
     * added to the working tree, and a new working tree will be constructed. Either {@code all} or
     * {@code table}, but not both, must be set prior to the import process.
     * 
     * @return RevTree the new working tree
     */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected RevTree _call() {
    // check preconditions and get the actual list of type names to import
    final String[] typeNames = checkPreconditions();
    for (int i = 0; i < typeNames.length; i++) {
        try {
            typeNames[i] = URLDecoder.decode(typeNames[i], Charsets.UTF_8.displayName());
        } catch (UnsupportedEncodingException e) {
        // shouldn't reach here.
        }
    }
    ProgressListener progressListener = getProgressListener();
    progressListener.started();
    // use a local variable not to alter the command's state
    boolean overwrite = this.overwrite;
    if (alter) {
        overwrite = false;
    }
    final WorkingTree workTree = workingTree();
    RevFeatureType destPathFeatureType = null;
    final boolean destPathProvided = destPath != null;
    if (destPathProvided) {
        destPathFeatureType = this.command(ResolveFeatureType.class).setRefSpec(destPath).call().orNull();
        // only the last one will be imported.
        if (overwrite) {
            try {
                workTree.delete(destPath);
            } catch (Exception e) {
                throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_INSERT);
            }
            overwrite = false;
        }
    }
    int tableCount = 0;
    for (String typeName : typeNames) {
        {
            tableCount++;
            String tableName = String.format("%-16s", typeName);
            if (typeName.length() > 16) {
                tableName = tableName.substring(0, 13) + "...";
            }
            progressListener.setDescription("Importing " + tableName + " (" + tableCount + "/" + typeNames.length + ")... ");
        }
        FeatureSource featureSource = getFeatureSource(typeName);
        SimpleFeatureType featureType = (SimpleFeatureType) featureSource.getSchema();
        final String fidPrefix = featureType.getTypeName() + ".";
        String path;
        if (destPath == null) {
            path = featureType.getTypeName();
        } else {
            NodeRef.checkValidPath(destPath);
            path = destPath;
            featureType = forceFeatureTypeName(featureType, path);
        }
        featureType = overrideGeometryName(featureType);
        featureSource = new ForceTypeAndFidFeatureSource<FeatureType, Feature>(featureSource, featureType, fidPrefix);
        boolean hasPrimaryKey = hasPrimaryKey(typeName);
        boolean forbidSorting = !usePaging || !hasPrimaryKey;
        ((ForceTypeAndFidFeatureSource) featureSource).setForbidSorting(forbidSorting);
        if (destPathFeatureType != null && adaptToDefaultFeatureType && !alter) {
            featureSource = new FeatureTypeAdapterFeatureSource<FeatureType, Feature>(featureSource, destPathFeatureType.type());
        }
        ProgressListener taskProgress = subProgress(100.f / typeNames.length);
        if (overwrite) {
            try {
                workTree.delete(path);
                workTree.createTypeTree(path, featureType);
            } catch (Exception e) {
                throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_INSERT);
            }
        }
        if (alter) {
            // first we modify the feature type and the existing features, if needed
            workTree.updateTypeTree(path, featureType);
            Iterator<Feature> transformedIterator = transformFeatures(featureType, path);
            try {
                final Integer collectionSize = collectionSize(featureSource);
                workTree.insert(path, transformedIterator, taskProgress, null, collectionSize);
            } catch (Exception e) {
                throw new GeoToolsOpException(StatusCode.UNABLE_TO_INSERT);
            }
        }
        try {
            insert(workTree, path, featureSource, taskProgress);
        } catch (GeoToolsOpException e) {
            throw e;
        } catch (Exception e) {
            throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_INSERT);
        }
    }
    progressListener.setProgress(100.f);
    progressListener.complete();
    return workTree.getTree();
}
Also used : ForwardingFeatureSource(org.locationtech.geogig.api.data.ForwardingFeatureSource) JDBCFeatureSource(org.geotools.jdbc.JDBCFeatureSource) FeatureSource(org.geotools.data.FeatureSource) ResolveFeatureType(org.locationtech.geogig.api.plumbing.ResolveFeatureType) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureType(org.opengis.feature.type.FeatureType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) DecoratingFeature(org.geotools.feature.DecoratingFeature) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) WorkingTree(org.locationtech.geogig.repository.WorkingTree) ProgressListener(org.locationtech.geogig.api.ProgressListener) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Aggregations

Feature (org.opengis.feature.Feature)128 Test (org.junit.Test)90 RevCommit (org.locationtech.geogig.api.RevCommit)52 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)43 SimpleFeature (org.opengis.feature.simple.SimpleFeature)38 ObjectId (org.locationtech.geogig.api.ObjectId)35 RevFeature (org.locationtech.geogig.api.RevFeature)32 NodeRef (org.locationtech.geogig.api.NodeRef)29 LinkedList (java.util.LinkedList)27 LogOp (org.locationtech.geogig.api.porcelain.LogOp)23 Ref (org.locationtech.geogig.api.Ref)21 RefParse (org.locationtech.geogig.api.plumbing.RefParse)19 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)18 ArrayList (java.util.ArrayList)16 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)16 RevObject (org.locationtech.geogig.api.RevObject)16 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)14 Function (com.google.common.base.Function)13 Optional (com.google.common.base.Optional)12 HashMap (java.util.HashMap)12