]> git.argeo.org Git - lgpl/argeo-commons.git/blob - gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/GeoToolsUtils.java
88ab84995621b21abfdac286fab9fc32a4d70847
[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
5 import org.argeo.ArgeoException;
6 import org.geotools.data.DataStore;
7 import org.geotools.data.FeatureSource;
8 import org.geotools.data.FeatureStore;
9 import org.opengis.feature.simple.SimpleFeature;
10 import org.opengis.feature.simple.SimpleFeatureType;
11 import org.opengis.feature.type.Name;
12
13 /** Utilities related to the GeoTools framework */
14 public class GeoToolsUtils {
15
16 /** Opens a read/write feature store */
17 public static FeatureStore<SimpleFeatureType, SimpleFeature> getFeatureStore(
18 DataStore dataStore, Name name) {
19 FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;
20 try {
21 featureSource = dataStore.getFeatureSource(name);
22 } catch (IOException e) {
23 throw new ArgeoException("Cannot open feature source " + name
24 + " in data store " + dataStore, e);
25 }
26 if (!(featureSource instanceof FeatureStore)) {
27 throw new ArgeoException("Feature source " + name
28 + " is not writable.");
29 }
30 return (FeatureStore<SimpleFeatureType, SimpleFeature>) featureSource;
31 }
32
33 /** Creates the provided schema in the data store. */
34 public static void createSchemaIfNeeded(DataStore dataStore,
35 SimpleFeatureType featureType) {
36 try {
37 dataStore.getSchema(featureType.getName());
38 } catch (IOException e) {
39 // assume it does not exist
40 try {
41 dataStore.createSchema(featureType);
42 } catch (IOException e1) {
43 throw new ArgeoException("Cannot create schema " + featureType,
44 e);
45 }
46 }
47 }
48 }