Search in sources :

Example 6 with SimpleFeatureIterator

use of org.geotools.data.simple.SimpleFeatureIterator 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 7 with SimpleFeatureIterator

use of org.geotools.data.simple.SimpleFeatureIterator 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 8 with SimpleFeatureIterator

use of org.geotools.data.simple.SimpleFeatureIterator in project spatial-portal by AtlasOfLivingAustralia.

the class AreaUploadShapefileWizardController method loadShape.

private void loadShape(String filename) {
    CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints());
    try {
        FileDataStore store = FileDataStoreFinder.getDataStore(new File(filename));
        source = store.getFeatureSource();
        features = source.getFeatures();
        Listhead lhd = new Listhead();
        SimpleFeatureType schema = features.getSchema();
        Listheader lh = new Listheader(StringConstants.ID);
        lh.setParent(lhd);
        for (AttributeType at : schema.getTypes()) {
            if (schema.getDescriptor(at.getName()) == null) {
                continue;
            }
            lh = new Listheader(at.getName().toString());
            lh.setParent(lhd);
        }
        lhd.setParent(lAttributes);
        SimpleFeatureIterator fi = features.features();
        while (fi.hasNext()) {
            SimpleFeature f = fi.next();
            Listitem li = new Listitem();
            Listcell lc;
            String value;
            //add identifier
            lc = new Listcell(f.getIdentifier().getID());
            lc.setParent(li);
            for (AttributeType at : schema.getTypes()) {
                if (schema.getDescriptor(at.getName()) == null) {
                    continue;
                }
                Object obj = f.getAttribute(at.getName());
                if (obj == null) {
                    value = f.getID();
                } else {
                    value = String.valueOf(obj);
                }
                lc = new Listcell(value);
                lc.setParent(li);
            }
            li.setValue(f.getIdentifier());
            li.setParent(lAttributes);
        }
        // loadFeatures
        // check if only a single feature,
        // if so, then select it and map it automatically
        LOGGER.debug("features.size(): " + features.size());
        if (features.size() > 1) {
            executeShapeImageRenderer(null);
        } else {
            LOGGER.debug("only a single feature, bypassing wizard...");
            fi = features.features();
            Set<FeatureId> ids = new HashSet<FeatureId>();
            ids.add(fi.next().getIdentifier());
            loadOnMap(ids, filename);
            //echo detach
            Events.echoEvent("onClick$btnCancel", this, null);
        }
        try {
            fi.close();
        } catch (Exception e) {
        }
    } catch (IOException e) {
        LOGGER.debug("IO Exception ", e);
    } catch (Exception e) {
        LOGGER.debug("Generic exception", e);
    }
}
Also used : IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) FeatureId(org.opengis.filter.identity.FeatureId) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeType(org.opengis.feature.type.AttributeType) FileDataStore(org.geotools.data.FileDataStore) File(java.io.File)

Example 9 with SimpleFeatureIterator

use of org.geotools.data.simple.SimpleFeatureIterator in project GeoGig by boundlessgeo.

the class ExportOpTest method testExportingUsingFunction.

@Test
public void testExportingUsingFunction() throws Exception {
    // Testing export of points feature type into a simplified feature type that
    // does not contain the integer attribute.
    String simplifiedPointsName = "simplifiedPoints";
    String simplifiedPointsTypeSpec = "sp:String,pp:Point:srid=4326";
    SimpleFeatureType simplifiedPointsType = DataUtilities.createType(pointsNs, simplifiedPointsName, simplifiedPointsTypeSpec);
    Feature simplifiedPoints1 = feature(simplifiedPointsType, ((SimpleFeature) points1).getID(), ((SimpleFeature) points1).getAttribute(0), ((SimpleFeature) points1).getAttribute(2));
    Feature simplifiedPoints2 = feature(simplifiedPointsType, ((SimpleFeature) points2).getID(), ((SimpleFeature) points2).getAttribute(0), ((SimpleFeature) points2).getAttribute(2));
    Feature simplifiedPoints3 = feature(simplifiedPointsType, ((SimpleFeature) points3).getID(), ((SimpleFeature) points3).getAttribute(0), ((SimpleFeature) points3).getAttribute(2));
    Feature[] simplifiedPoints = new Feature[] { simplifiedPoints1, simplifiedPoints2, simplifiedPoints3 };
    final SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(simplifiedPointsType);
    Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

        @Override
        @Nullable
        public Optional<Feature> apply(@Nullable Feature feature) {
            SimpleFeature simpleFeature = (SimpleFeature) feature;
            featureBuilder.add(simpleFeature.getAttribute(0));
            featureBuilder.add(simpleFeature.getAttribute(2));
            return Optional.of((Feature) featureBuilder.buildFeature(null));
        }
    };
    Feature[] points = new Feature[] { points1, points2, points3 };
    for (Feature feature : points) {
        insert(feature);
    }
    MemoryDataStore dataStore = new MemoryDataStore(simplifiedPointsType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setFeatureTypeConversionFunction(function).call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(featureCollection.size(), points.length);
    SimpleFeatureIterator features = featureCollection.features();
    assertTrue(collectionsAreEqual(features, simplifiedPoints));
    // check for exceptions when using a function that returns features with a wrong featuretype
    try {
        String wrongFeaturesName = "wrongFeatures";
        String wrongFeaturesTypeSpec = "sp:String";
        SimpleFeatureType wrongFeaturesType = DataUtilities.createType(pointsNs, wrongFeaturesName, wrongFeaturesTypeSpec);
        final SimpleFeatureBuilder wrongFeatureBuilder = new SimpleFeatureBuilder(wrongFeaturesType);
        Function<Feature, Optional<Feature>> wrongFunction = new Function<Feature, Optional<Feature>>() {

            @Override
            @Nullable
            public Optional<Feature> apply(@Nullable Feature feature) {
                SimpleFeature simpleFeature = (SimpleFeature) feature;
                wrongFeatureBuilder.add(simpleFeature.getAttribute(0));
                return Optional.of((Feature) wrongFeatureBuilder.buildFeature(null));
            }
        };
        geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setFeatureTypeConversionFunction(wrongFunction).call();
        fail();
    } catch (GeoToolsOpException e) {
        assertEquals(e.statusCode, StatusCode.UNABLE_TO_ADD);
    }
}
Also used : Optional(com.google.common.base.Optional) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Function(com.google.common.base.Function) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Test(org.junit.Test)

Example 10 with SimpleFeatureIterator

use of org.geotools.data.simple.SimpleFeatureIterator in project GeoGig by boundlessgeo.

the class ExportOpTest method testExportWithAlterUsingFeatureTypeId.

@Test
public void testExportWithAlterUsingFeatureTypeId() throws Exception {
    Feature[] points = new Feature[] { points2, points1B, points3 };
    for (Feature feature : points) {
        insert(feature);
    }
    MemoryDataStore dataStore = new MemoryDataStore(modifiedPointsType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setAlter(true).setFilterFeatureTypeId(RevFeatureTypeImpl.build(modifiedPointsType).getId()).call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(featureCollection.size(), points.length);
    SimpleFeatureIterator features = featureCollection.features();
    while (features.hasNext()) {
        List<Object> attributes = features.next().getAttributes();
        assertEquals(4, attributes.size());
    }
}
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)

Aggregations

SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)13 SimpleFeature (org.opengis.feature.simple.SimpleFeature)13 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)11 Test (org.junit.Test)10 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)9 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)9 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)9 Feature (org.opengis.feature.Feature)9 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)5 ParseException (com.vividsolutions.jts.io.ParseException)2 File (java.io.File)2 IOException (java.io.IOException)2 SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)2 RevCommit (org.locationtech.geogig.api.RevCommit)2 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)2 FeatureId (org.opengis.filter.identity.FeatureId)2 UserDataDTO (au.org.ala.spatial.dto.UserDataDTO)1 MapLayer (au.org.emii.portal.menu.MapLayer)1 Function (com.google.common.base.Function)1 Optional (com.google.common.base.Optional)1