1 package org
.argeo
.geotools
;
3 import java
.io
.IOException
;
4 import java
.util
.HashSet
;
5 import java
.util
.Iterator
;
6 import java
.util
.NoSuchElementException
;
9 import org
.apache
.commons
.logging
.Log
;
10 import org
.apache
.commons
.logging
.LogFactory
;
11 import org
.argeo
.ArgeoException
;
12 import org
.geotools
.data
.DataStore
;
13 import org
.geotools
.data
.FeatureSource
;
14 import org
.geotools
.data
.FeatureStore
;
15 import org
.geotools
.feature
.FeatureIterator
;
16 import org
.geotools
.filter
.FilterFactoryImpl
;
17 import org
.opengis
.feature
.simple
.SimpleFeature
;
18 import org
.opengis
.feature
.simple
.SimpleFeatureType
;
19 import org
.opengis
.feature
.type
.Name
;
20 import org
.opengis
.filter
.Filter
;
21 import org
.opengis
.filter
.FilterFactory2
;
22 import org
.opengis
.filter
.identity
.FeatureId
;
24 /** Utilities related to the GeoTools framework */
25 public class GeoToolsUtils
{
27 private final static Log log
= LogFactory
.getLog(GeoToolsUtils
.class);
29 // TODO: use common factory finder?
30 private static FilterFactory2 filterFactory
= new FilterFactoryImpl();
32 /** Opens a read/write feature store */
33 public static FeatureStore
<SimpleFeatureType
, SimpleFeature
> getFeatureStore(
34 DataStore dataStore
, Name name
) {
35 FeatureSource
<SimpleFeatureType
, SimpleFeature
> featureSource
;
37 featureSource
= dataStore
.getFeatureSource(name
);
38 } catch (IOException e
) {
39 throw new ArgeoException("Cannot open feature source " + name
40 + " in data store " + dataStore
, e
);
42 if (!(featureSource
instanceof FeatureStore
)) {
43 throw new ArgeoException("Feature source " + name
44 + " is not writable.");
46 return (FeatureStore
<SimpleFeatureType
, SimpleFeature
>) featureSource
;
49 /** Creates the provided schema in the data store. */
50 public static void createSchemaIfNeeded(DataStore dataStore
,
51 SimpleFeatureType featureType
) {
53 dataStore
.getSchema(featureType
.getName());
54 } catch (IOException e
) {
55 // assume it does not exist
57 dataStore
.createSchema(featureType
);
58 } catch (IOException e1
) {
59 throw new ArgeoException("Cannot create schema " + featureType
,
65 public static FilterFactory2
ff() {
69 public static SimpleFeature
querySingleFeature(
70 FeatureSource
<SimpleFeatureType
, SimpleFeature
> featureSource
,
72 Set
<FeatureId
> ids
= new HashSet
<FeatureId
>();
73 ids
.add(ff().featureId(featureId
));
74 Filter filter
= ff().id(ids
);
75 FeatureIterator
<SimpleFeature
> it
= null;
77 it
= featureSource
.getFeatures(filter
).features();
81 SimpleFeature feature
= it
.next();
83 log
.warn("More than one feature for feature id "
84 + featureId
+ " in feature source "
85 + featureSource
.getName());
88 } catch (Exception e
) {
89 throw new ArgeoException("Cannot extract single feature "
90 + featureId
+ " from feature source "
91 + featureSource
.getName(), e
);
97 public static void closeQuietly(FeatureIterator
<?
> featureIterator
) {
98 if (featureIterator
!= null)
100 featureIterator
.close();
101 } catch (Exception e
) {