Search in sources :

Example 26 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project coastal-hazards by USGS-CIDA.

the class WFSGetDomain method getDomainValuesAsStrings.

public Set<String> getDomainValuesAsStrings(WFSService service, String attribute) throws IOException {
    Set<String> domain = new HashSet<>();
    URL getCapsUrl = WFSDataStoreFactory.createGetCapabilitiesRequest(new URL(service.getEndpoint()), Version.v1_1_0);
    log.debug("Getting domains from wfs at " + getCapsUrl);
    Map params = new HashMap<>();
    params.put(WFSDataStoreFactory.URL.key, getCapsUrl);
    params.put(WFSDataStoreFactory.TIMEOUT.key, TIMEOUT_MILLISECONDS);
    WFSDataStore wfs = datastore.createDataStore(params);
    if (wfs == null) {
        log.debug("Could not set up WFS datastore");
        throw new WFSException("Could not set up WFS datastore");
    }
    try {
        Query query = new Query(service.getTypeName(), Filter.INCLUDE, new String[] { attribute });
        SimpleFeatureSource featureSource = wfs.getFeatureSource(service.getTypeName());
        SimpleFeatureCollection features = featureSource.getFeatures(query);
        SimpleFeatureIterator iterator = features.features();
        while (iterator.hasNext()) {
            SimpleFeature next = iterator.next();
            Object attr = next.getAttribute(attribute);
            if (attr instanceof String) {
                String attrVal = (String) attr;
                domain.add(attrVal);
            } else {
                throw new UnsupportedOperationException("Currently only string attributes are allowed");
            }
        }
    } finally {
        wfs.dispose();
    }
    return domain;
}
Also used : Query(org.geotools.data.Query) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) WFSDataStore(org.geotools.data.wfs.WFSDataStore) URL(java.net.URL) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) WFSException(org.geotools.data.wfs.protocol.wfs.WFSException) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 27 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project coastal-hazards by USGS-CIDA.

the class FeatureCollectionExportTest method testWriteToShapefile.

/**
 * Test of writeToShapefile method, of class FeatureCollectionExport.
 * ignoring this so it doesn't hit the server too much, mock this out for real test
 */
@Test
@Ignore
public void testWriteToShapefile() throws Exception {
    WFSDataStoreFactory datastore = new WFSDataStoreFactory();
    Map params = new HashMap<>();
    params.put(WFSDataStoreFactory.URL.key, new URL("http://coastalmap.marine.usgs.gov/cmgp/National/cvi_WFS/MapServer/WFSServer?service=WFS&request=GetCapabilities&version=1.0.0"));
    params.put(WFSDataStoreFactory.WFS_STRATEGY.key, "arcgis");
    params.put(WFSDataStoreFactory.TIMEOUT.key, 15000);
    params.put(WFSDataStoreFactory.TRY_GZIP.key, "true");
    WFSDataStore wfs = datastore.createDataStore(params);
    String[] typeNames = wfs.getTypeNames();
    SimpleFeatureSource featureSource = wfs.getFeatureSource(typeNames[2]);
    FeatureCollectionExport featureCollectionExport = new FeatureCollectionExport(featureSource.getFeatures(), new File("/tmp/shpfile"), "test3");
    featureCollectionExport.addAttribute("CVIRISK");
    featureCollectionExport.writeToShapefile();
}
Also used : HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) WFSDataStore(org.geotools.data.wfs.WFSDataStore) WFSDataStoreFactory(org.geotools.data.wfs.WFSDataStoreFactory) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) URL(java.net.URL) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 28 with SimpleFeatureSource

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

the class ExportOpTest method testExportFromTreeWithSeveralFeatureTypes.

@Test
public void testExportFromTreeWithSeveralFeatureTypes() 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).call();
        fail();
    } catch (GeoToolsOpException e) {
        assertEquals(GeoToolsOpException.StatusCode.MIXED_FEATURE_TYPES, e.statusCode);
    }
}
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 29 with SimpleFeatureSource

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

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

SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)54 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)29 IOException (java.io.IOException)28 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)25 SimpleFeature (org.opengis.feature.simple.SimpleFeature)23 DataStore (org.geotools.data.DataStore)22 Test (org.junit.Test)21 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)20 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)19 Feature (org.opengis.feature.Feature)16 HashMap (java.util.HashMap)15 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)13 URL (java.net.URL)10 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)10 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)10 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)9 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)8 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)8