comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java @ 540:80630520e25a

merged gnv-artifacts/0.4
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:49 +0200
parents 4080b57dcb52
children 4fc97074eb90
comparison
equal deleted inserted replaced
415:9f4a0b990d27 540:80630520e25a
1 package de.intevation.gnv.utils;
2
3 import com.vividsolutions.jts.geom.MultiLineString;
4 import com.vividsolutions.jts.geom.MultiPolygon;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.io.Serializable;
9
10 import java.net.MalformedURLException;
11
12 import java.util.Date;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.apache.log4j.Logger;
18
19 import org.geotools.data.DataStoreFactorySpi;
20 import org.geotools.data.DataUtilities;
21 import org.geotools.data.DefaultTransaction;
22 import org.geotools.data.FeatureStore;
23 import org.geotools.data.Transaction;
24
25 import org.geotools.data.shapefile.ShapefileDataStore;
26 import org.geotools.data.shapefile.ShapefileDataStoreFactory;
27
28 import org.geotools.feature.FeatureCollection;
29 import org.geotools.feature.FeatureCollections;
30 import org.geotools.feature.SchemaException;
31
32 import org.geotools.feature.simple.SimpleFeatureBuilder;
33
34 import org.geotools.referencing.crs.DefaultGeographicCRS;
35
36 import org.opengis.feature.simple.SimpleFeature;
37 import org.opengis.feature.simple.SimpleFeatureType;
38
39 /**
40 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
41 */
42 public final class ShapeFileWriter
43 {
44 private static Logger log = Logger.getLogger(
45 ShapeFileWriter.class);
46
47 private ShapeFileWriter() {
48 }
49
50 public static boolean writeMultiLineStringsToFile(
51 File shapeFile,
52 Integer parameterId,
53 Integer layer,
54 Date date,
55 List<Pair<Object, MultiLineString>> multiLineStrings
56 ) {
57 return writeMultiLineStringsToFile(
58 shapeFile,
59 parameterId,
60 layer,
61 date,
62 multiLineStrings,
63 "isolines");
64 }
65
66 public static boolean writeMultiLineStringsToFile(
67 File shapeFile,
68 Integer parameterId,
69 Integer layer,
70 Date date,
71 List<Pair<Object, MultiLineString>> multiLineStrings,
72 String name
73 ) {
74 Map<String, Serializable> params = new HashMap<String, Serializable>();
75
76 try {
77 params.put("url", shapeFile.toURI().toURL());
78 }
79 catch (MalformedURLException mue) {
80 log.error(mue.getLocalizedMessage(), mue);
81 return false;
82 }
83
84 params.put("create spatial index", Boolean.TRUE);
85
86
87 if (name == null) {
88 name = shapeFile.getName();
89 }
90
91 SimpleFeatureType TYPE;
92
93 try {
94 TYPE = DataUtilities.createType(
95 name,
96 "geom:MultiLineString:srid=4326," +
97 "PARAMETER:Integer," +
98 "LAYER:Integer," +
99 "DATE:Date," +
100 "VALUE:Double");
101 }
102 catch (SchemaException se) {
103 log.error(se.getLocalizedMessage(), se);
104 return false;
105 }
106
107 SimpleFeatureBuilder featureBuilder =
108 new SimpleFeatureBuilder(TYPE);
109
110 FeatureCollection<SimpleFeatureType, SimpleFeature> collection =
111 FeatureCollections.newCollection();
112
113 for (Pair<Object, MultiLineString> pair: multiLineStrings) {
114 featureBuilder.add(pair.getB());
115 featureBuilder.add(parameterId);
116 featureBuilder.add(layer);
117 featureBuilder.add(date);
118 featureBuilder.add(pair.getA());
119 SimpleFeature feature = featureBuilder.buildFeature(null);
120 collection.add(feature);
121 }
122
123 DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
124
125 Transaction transaction = null;
126
127 boolean success = false;
128 try {
129 ShapefileDataStore newDataStore =
130 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
131
132 newDataStore.createSchema(TYPE);
133 newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
134
135 transaction = new DefaultTransaction("create");
136
137 String typeName = newDataStore.getTypeNames()[0];
138
139 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
140 (FeatureStore<SimpleFeatureType, SimpleFeature>)
141 newDataStore.getFeatureSource(typeName);
142
143 featureStore.setTransaction(transaction);
144
145 featureStore.addFeatures(collection);
146 transaction.commit();
147 success = true;
148 }
149 catch (IOException ioe) {
150 log.error(ioe.getLocalizedMessage(), ioe);
151 }
152 finally {
153 if (transaction != null) {
154 if (!success) {
155 try { transaction.rollback(); }
156 catch (IOException ioe) {}
157 }
158 try { transaction.close(); }
159 catch (IOException ioe) {}
160 }
161 }
162
163 return success;
164 }
165
166 public static boolean writeMultiPolygonsToFile(
167 File shapeFile,
168 Integer parameterId,
169 Integer layer,
170 Date date,
171 Map<Integer, MultiPolygon> multiPolygons
172 ) {
173 return writeMultiPolygonsToFile(
174 shapeFile,
175 parameterId,
176 layer,
177 date,
178 multiPolygons,
179 "polygons");
180 }
181
182 public static boolean writeMultiPolygonsToFile(
183 File shapeFile,
184 Integer parameterId,
185 Integer layer,
186 Date date,
187 Map<Integer, MultiPolygon> multiPolygons,
188 String name
189 ) {
190 Map<String, Serializable> params = new HashMap<String, Serializable>();
191
192 try {
193 params.put("url", shapeFile.toURI().toURL());
194 }
195 catch (MalformedURLException mue) {
196 log.error(mue.getLocalizedMessage(), mue);
197 return false;
198 }
199
200 params.put("create spatial index", Boolean.TRUE);
201
202
203 if (name == null) {
204 name = shapeFile.getName();
205 }
206
207 SimpleFeatureType TYPE;
208
209 try {
210 TYPE = DataUtilities.createType(
211 name,
212 "geom:MultiPolygon:srid=4326," +
213 "PARAMETER:Integer," +
214 "LAYER:Integer," +
215 "DATE:Date," +
216 "CLASS:Integer");
217 }
218 catch (SchemaException se) {
219 log.error(se.getLocalizedMessage(), se);
220 return false;
221 }
222
223 SimpleFeatureBuilder featureBuilder =
224 new SimpleFeatureBuilder(TYPE);
225
226 FeatureCollection<SimpleFeatureType, SimpleFeature> collection =
227 FeatureCollections.newCollection();
228
229 for (Map.Entry<Integer, MultiPolygon> entry:
230 multiPolygons.entrySet()
231 ) {
232 featureBuilder.add(entry.getValue());
233 featureBuilder.add(parameterId);
234 featureBuilder.add(layer);
235 featureBuilder.add(date);
236 featureBuilder.add(entry.getKey());
237 SimpleFeature feature = featureBuilder.buildFeature(null);
238 collection.add(feature);
239 }
240
241 DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
242
243 Transaction transaction = null;
244
245 boolean success = false;
246 try {
247 ShapefileDataStore newDataStore =
248 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
249
250 newDataStore.createSchema(TYPE);
251 newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
252
253 transaction = new DefaultTransaction("create");
254
255 String typeName = newDataStore.getTypeNames()[0];
256
257 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
258 (FeatureStore<SimpleFeatureType, SimpleFeature>)
259 newDataStore.getFeatureSource(typeName);
260
261 featureStore.setTransaction(transaction);
262
263 featureStore.addFeatures(collection);
264 transaction.commit();
265 success = true;
266 }
267 catch (IOException ioe) {
268 log.error(ioe.getLocalizedMessage(), ioe);
269 }
270 finally {
271 if (transaction != null) {
272 if (!success) {
273 try { transaction.rollback(); }
274 catch (IOException ioe) {}
275 }
276 try { transaction.close(); }
277 catch (IOException ioe) {}
278 }
279 }
280
281 return success;
282 }
283
284 private static final Double asDouble(Object a) {
285 if (a instanceof Double) {
286 return (Double)a;
287 }
288 if (a instanceof Number) {
289 return Double.valueOf(((Number)a).doubleValue());
290 }
291 return 0d;
292 }
293 }
294 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org