Search in sources :

Example 1 with WFSException

use of org.geotools.data.wfs.protocol.wfs.WFSException 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)

Aggregations

URL (java.net.URL)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Query (org.geotools.data.Query)1 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)1 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)1 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)1 WFSDataStore (org.geotools.data.wfs.WFSDataStore)1 WFSException (org.geotools.data.wfs.protocol.wfs.WFSException)1 SimpleFeature (org.opengis.feature.simple.SimpleFeature)1