]> git.argeo.org Git - lgpl/argeo-commons.git/blob - gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/GeoToolsUtils.java
Adapt for GeoTools 2.7.2
[lgpl/argeo-commons.git] / gis / runtime / org.argeo.gis.geotools / src / main / java / org / argeo / geotools / GeoToolsUtils.java
1 package org.argeo.geotools;
2
3 import java.io.IOException;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.NoSuchElementException;
7 import java.util.Set;
8
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;
23
24 /** Utilities related to the GeoTools framework */
25 public class GeoToolsUtils {
26
27 private final static Log log = LogFactory.getLog(GeoToolsUtils.class);
28
29 // TODO: use common factory finder?
30 private static FilterFactory2 filterFactory = new FilterFactoryImpl();
31
32 /** Opens a read/write feature store */
33 public static FeatureStore<SimpleFeatureType, SimpleFeature> getFeatureStore(
34 DataStore dataStore, Name name) {
35 FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
36 try {
37 featureSource = dataStore.getFeatureSource(name);
38 } catch (IOException e) {
39 throw new ArgeoException("Cannot open feature source " + name
40 + " in data store " + dataStore, e);
41 }
42 if (!(featureSource instanceof FeatureStore)) {
43 throw new ArgeoException("Feature source " + name
44 + " is not writable.");
45 }
46 return (FeatureStore<SimpleFeatureType, SimpleFeature>) featureSource;
47 }
48
49 /** Creates the provided schema in the data store. */
50 public static void createSchemaIfNeeded(DataStore dataStore,
51 SimpleFeatureType featureType) {
52 try {
53 dataStore.getSchema(featureType.getName());
54 } catch (IOException e) {
55 // assume it does not exist
56 try {
57 dataStore.createSchema(featureType);
58 } catch (IOException e1) {
59 throw new ArgeoException("Cannot create schema " + featureType,
60 e1);
61 }
62 }
63 }
64
65 public static FilterFactory2 ff() {
66 return filterFactory;
67 }
68
69 public static SimpleFeature querySingleFeature(
70 FeatureSource<SimpleFeatureType, SimpleFeature> featureSource,
71 String featureId) {
72 Set<FeatureId> ids = new HashSet<FeatureId>();
73 ids.add(ff().featureId(featureId));
74 Filter filter = ff().id(ids);
75 FeatureIterator<SimpleFeature> it = null;
76 try {
77 it = featureSource.getFeatures(filter).features();
78 if (!it.hasNext())
79 return null;
80 else {
81 SimpleFeature feature = it.next();
82 if (it.hasNext())
83 log.warn("More than one feature for feature id "
84 + featureId + " in feature source "
85 + featureSource.getName());
86 return feature;
87 }
88 } catch (Exception e) {
89 throw new ArgeoException("Cannot extract single feature "
90 + featureId + " from feature source "
91 + featureSource.getName(), e);
92 } finally {
93 closeQuietly(it);
94 }
95 }
96
97 public static void closeQuietly(FeatureIterator<?> featureIterator) {
98 if (featureIterator != null)
99 try {
100 featureIterator.close();
101 } catch (Exception e) {
102 // silent
103 }
104 }
105 }