Search in sources :

Example 56 with Feature

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

the class ExportOpTest method testExportFromTreeWithSeveralFeatureTypesUsingNonexistantTypeId.

@Test
public void testExportFromTreeWithSeveralFeatureTypesUsingNonexistantTypeId() throws Exception {
    Feature[] points = new Feature[] { points2, points1B, points3 };
    for (Feature feature : points) {
        insert(feature);
    }
    MemoryDataStore dataStore = new MemoryDataStore(pointsType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    try {
        geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setFilterFeatureTypeId(ObjectId.forString("fake")).call();
        fail();
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage(), e.getMessage().contains("filter feature type"));
    }
}
Also used : SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Example 57 with Feature

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

the class ExportOpTest method testExportFromWorkingTree.

@Test
public void testExportFromWorkingTree() throws Exception {
    Feature[] points = new Feature[] { points1, points2, points3 };
    for (Feature feature : points) {
        insert(feature);
    }
    MemoryDataStore dataStore = new MemoryDataStore(pointsType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(featureCollection.size(), points.length);
    SimpleFeatureIterator features = featureCollection.features();
    assertTrue(collectionsAreEqual(features, points));
}
Also used : SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

Example 58 with Feature

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

the class ExportOpTest method testExportFromTreeWithSeveralFeatureTypesUsingFeatureTypeId.

@Test
public void testExportFromTreeWithSeveralFeatureTypesUsingFeatureTypeId() throws Exception {
    Feature[] points = new Feature[] { points2, points1B, points3 };
    for (Feature feature : points) {
        insert(feature);
    }
    Feature[] expectedPoints = new Feature[] { points1B };
    MemoryDataStore dataStore = new MemoryDataStore(pointsType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setFilterFeatureTypeId(RevFeatureTypeImpl.build(modifiedPointsType).getId()).call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(expectedPoints.length, featureCollection.size());
    SimpleFeatureIterator features = featureCollection.features();
    assertTrue(collectionsAreEqual(features, expectedPoints));
}
Also used : SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

Example 59 with Feature

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

the class OSMExportSL method exportRule.

private void exportRule(final MappingRule rule, final GeogigCLI cli) throws IOException {
    Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

        @Override
        public Optional<Feature> apply(Feature feature) {
            Optional<Feature> mapped = rule.apply(feature);
            return mapped;
        }
    };
    SimpleFeatureType outputFeatureType = rule.getFeatureType();
    String path = getOriginTreesFromOutputFeatureType(outputFeatureType);
    DataStore dataStore = getDataStore();
    try {
        final String tableName = ensureTableExists(outputFeatureType, dataStore);
        final SimpleFeatureSource source = dataStore.getFeatureSource(tableName);
        if (!(source instanceof SimpleFeatureStore)) {
            throw new CommandFailedException("Could not create feature store.");
        }
        final SimpleFeatureStore store = (SimpleFeatureStore) source;
        if (overwrite) {
            try {
                store.removeFeatures(Filter.INCLUDE);
            } catch (IOException e) {
                throw new CommandFailedException("Error truncating table " + tableName, e);
            }
        }
        ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(store).setPath(path).setFeatureTypeConversionFunction(function);
        try {
            op.setProgressListener(cli.getProgressListener()).call();
            cli.getConsole().println("OSM data exported successfully to " + tableName);
        } catch (IllegalArgumentException iae) {
            throw new InvalidParameterException(iae.getMessage(), iae);
        } catch (GeoToolsOpException e) {
            throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
        }
    } finally {
        dataStore.dispose();
    }
}
Also used : Optional(com.google.common.base.Optional) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) IOException(java.io.IOException) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) Function(com.google.common.base.Function) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp)

Example 60 with Feature

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

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