X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=gis%2Fruntime%2Forg.argeo.gis.geotools%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fgeotools%2FGeoToolsUtils.java;h=fb5ad5b76a816844b15f494f3d47228010ef4f5e;hb=78a52c7ee4e476b3fe366346ed26315f7237a6bb;hp=88ab84995621b21abfdac286fab9fc32a4d70847;hpb=977a7a352131b082a98739f15e421f2bff747567;p=lgpl%2Fargeo-commons.git diff --git a/gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/GeoToolsUtils.java b/gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/GeoToolsUtils.java index 88ab84995..fb5ad5b76 100644 --- a/gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/GeoToolsUtils.java +++ b/gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/GeoToolsUtils.java @@ -1,18 +1,34 @@ package org.argeo.geotools; import java.io.IOException; +import java.util.HashSet; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Set; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.ArgeoException; import org.geotools.data.DataStore; import org.geotools.data.FeatureSource; import org.geotools.data.FeatureStore; +import org.geotools.feature.FeatureIterator; +import org.geotools.filter.FilterFactoryImpl; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.feature.type.Name; +import org.opengis.filter.Filter; +import org.opengis.filter.FilterFactory2; +import org.opengis.filter.identity.FeatureId; /** Utilities related to the GeoTools framework */ public class GeoToolsUtils { + private final static Log log = LogFactory.getLog(GeoToolsUtils.class); + + // TODO: use common factory finder? + private static FilterFactory2 filterFactory = new FilterFactoryImpl(); + /** Opens a read/write feature store */ public static FeatureStore getFeatureStore( DataStore dataStore, Name name) { @@ -41,8 +57,49 @@ public class GeoToolsUtils { dataStore.createSchema(featureType); } catch (IOException e1) { throw new ArgeoException("Cannot create schema " + featureType, - e); + e1); } } } + + public static FilterFactory2 ff() { + return filterFactory; + } + + public static SimpleFeature querySingleFeature( + FeatureSource featureSource, + String featureId) { + Set ids = new HashSet(); + ids.add(ff().featureId(featureId)); + Filter filter = ff().id(ids); + FeatureIterator it = null; + try { + it = featureSource.getFeatures(filter).features(); + if (!it.hasNext()) + return null; + else { + SimpleFeature feature = it.next(); + if (it.hasNext()) + log.warn("More than one feature for feature id " + + featureId + " in feature source " + + featureSource.getName()); + return feature; + } + } catch (Exception e) { + throw new ArgeoException("Cannot extract single feature " + + featureId + " from feature source " + + featureSource.getName(), e); + } finally { + closeQuietly(it); + } + } + + public static void closeQuietly(FeatureIterator featureIterator) { + if (featureIterator != null) + try { + featureIterator.close(); + } catch (Exception e) { + // silent + } + } }