comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java @ 657:af3f56758f59

merged gnv-artifacts/0.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:53 +0200 (2012-09-28)
parents 6eccb68a8b99
children 22dc921cd8b1
comparison
equal deleted inserted replaced
590:5f5f273c8566 657:af3f56758f59
1 package de.intevation.gnv.utils;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.Serializable;
6 import java.net.MalformedURLException;
7 import java.util.Collection;
8 import java.util.Date;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13 import org.apache.log4j.Logger;
14 import org.geotools.data.DataStoreFactorySpi;
15 import org.geotools.data.DataUtilities;
16 import org.geotools.data.DefaultTransaction;
17 import org.geotools.data.FeatureStore;
18 import org.geotools.data.Transaction;
19 import org.geotools.data.shapefile.ShapefileDataStore;
20 import org.geotools.data.shapefile.ShapefileDataStoreFactory;
21 import org.geotools.feature.FeatureCollection;
22 import org.geotools.feature.FeatureCollections;
23 import org.geotools.feature.SchemaException;
24 import org.geotools.feature.simple.SimpleFeatureBuilder;
25 import org.geotools.referencing.crs.DefaultGeographicCRS;
26 import org.opengis.feature.simple.SimpleFeature;
27 import org.opengis.feature.simple.SimpleFeatureType;
28
29 import com.vividsolutions.jts.geom.Geometry;
30 import com.vividsolutions.jts.geom.MultiLineString;
31 import com.vividsolutions.jts.geom.MultiPolygon;
32 import com.vividsolutions.jts.io.ParseException;
33 import com.vividsolutions.jts.io.WKTReader;
34
35 import de.intevation.gnv.geobackend.base.Result;
36
37 /**
38 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
39 */
40 public final class ShapeFileWriter
41 {
42 private static Logger log = Logger.getLogger(
43 ShapeFileWriter.class);
44
45 private ShapeFileWriter() {
46 }
47
48 public static boolean writeMultiLineStringsToFile(
49 File shapeFile,
50 Integer parameterId,
51 Integer layer,
52 Date date,
53 List<Pair<Object, MultiLineString>> multiLineStrings
54 ) {
55 return writeMultiLineStringsToFile(
56 shapeFile,
57 parameterId,
58 layer,
59 date,
60 multiLineStrings,
61 "isolines");
62 }
63
64 public static boolean writeMultiLineStringsToFile(
65 File shapeFile,
66 Integer parameterId,
67 Integer layer,
68 Date date,
69 List<Pair<Object, MultiLineString>> multiLineStrings,
70 String name
71 ) {
72 Map<String, Serializable> params = new HashMap<String, Serializable>();
73
74 try {
75 params.put("url", shapeFile.toURI().toURL());
76 }
77 catch (MalformedURLException mue) {
78 log.error(mue.getLocalizedMessage(), mue);
79 return false;
80 }
81
82 params.put("create spatial index", Boolean.TRUE);
83
84
85 if (name == null) {
86 name = shapeFile.getName();
87 }
88
89 SimpleFeatureType TYPE;
90
91 try {
92 TYPE = DataUtilities.createType(
93 name,
94 "geom:MultiLineString:srid=4326," +
95 "PARAMETER:Integer," +
96 "LAYER:Integer," +
97 "DATE:Date," +
98 "VALUE:Double");
99 }
100 catch (SchemaException se) {
101 log.error(se.getLocalizedMessage(), se);
102 return false;
103 }
104
105 SimpleFeatureBuilder featureBuilder =
106 new SimpleFeatureBuilder(TYPE);
107
108 FeatureCollection<SimpleFeatureType, SimpleFeature> collection =
109 FeatureCollections.newCollection();
110
111 for (Pair<Object, MultiLineString> pair: multiLineStrings) {
112 featureBuilder.add(pair.getB());
113 featureBuilder.add(parameterId);
114 featureBuilder.add(layer);
115 featureBuilder.add(date);
116 featureBuilder.add(pair.getA());
117 SimpleFeature feature = featureBuilder.buildFeature(null);
118 collection.add(feature);
119 }
120
121 DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
122
123 Transaction transaction = null;
124
125 boolean success = false;
126 try {
127 ShapefileDataStore newDataStore =
128 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
129
130 newDataStore.createSchema(TYPE);
131 newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
132
133 transaction = new DefaultTransaction("create");
134
135 String typeName = newDataStore.getTypeNames()[0];
136
137 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
138 (FeatureStore<SimpleFeatureType, SimpleFeature>)
139 newDataStore.getFeatureSource(typeName);
140
141 featureStore.setTransaction(transaction);
142
143 featureStore.addFeatures(collection);
144 transaction.commit();
145 success = true;
146 }
147 catch (IOException ioe) {
148 log.error(ioe.getLocalizedMessage(), ioe);
149 }
150 finally {
151 if (transaction != null) {
152 if (!success) {
153 try { transaction.rollback(); }
154 catch (IOException ioe) {}
155 }
156 try { transaction.close(); }
157 catch (IOException ioe) {}
158 }
159 }
160
161 return success;
162 }
163
164 public static boolean writeMultiPolygonsToFile(
165 File shapeFile,
166 Integer parameterId,
167 Integer layer,
168 Date date,
169 Map<Integer, MultiPolygon> multiPolygons
170 ) {
171 return writeMultiPolygonsToFile(
172 shapeFile,
173 parameterId,
174 layer,
175 date,
176 multiPolygons,
177 "polygons");
178 }
179
180
181 public static String writeDataToFile(File shapeFile,
182 String name,
183 Collection<Result> data){
184 String geomType = null;
185
186 WKTReader wktReader = new WKTReader();
187
188 Map<String, Serializable> params = new HashMap<String, Serializable>();
189
190 try {
191 params.put("url", shapeFile.toURI().toURL());
192 }
193 catch (MalformedURLException mue) {
194 log.error(mue.getLocalizedMessage(), mue);
195 return null;
196 }
197
198 params.put("create spatial index", Boolean.TRUE);
199
200
201 if (name == null) {
202 name = shapeFile.getName();
203 }
204
205 SimpleFeatureType type = null;
206 SimpleFeatureBuilder featureBuilder = null;
207 FeatureCollection<SimpleFeatureType, SimpleFeature> collection =
208 FeatureCollections.newCollection();
209
210 for (Result result: data) {
211 try {
212 Geometry g = wktReader.read(result.getString(0));
213 if (type == null){
214 try {
215 geomType = g.getGeometryType().toUpperCase();
216 type = DataUtilities.createType(
217 name,
218 "geom:"+g.getGeometryType()+":srid=4326");
219 // TODO add other AttributeTypes
220 }
221 catch (SchemaException se) {
222 log.error(se.getLocalizedMessage(), se);
223 return null;
224 }
225 featureBuilder = new SimpleFeatureBuilder(type);
226 }
227 featureBuilder.add(g);
228 SimpleFeature feature = featureBuilder.buildFeature(null);
229 collection.add(feature);
230 } catch (ParseException e) {
231 log.error(e,e);
232 } catch (java.lang.IllegalArgumentException e){
233 log.error("cannot create geometry for "+result.getString(0));
234 }
235 }
236
237 DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
238
239 Transaction transaction = null;
240
241 boolean success = false;
242 try {
243 ShapefileDataStore newDataStore =
244 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
245
246 newDataStore.createSchema(type);
247 newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
248
249 transaction = new DefaultTransaction("create");
250
251 String typeName = newDataStore.getTypeNames()[0];
252
253 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
254 (FeatureStore<SimpleFeatureType, SimpleFeature>)
255 newDataStore.getFeatureSource(typeName);
256
257 featureStore.setTransaction(transaction);
258
259 featureStore.addFeatures(collection);
260 transaction.commit();
261 success = true;
262 }
263 catch (IOException ioe) {
264 log.error(ioe.getLocalizedMessage(), ioe);
265 }
266 finally {
267 if (transaction != null) {
268 if (!success) {
269 try { transaction.rollback(); }
270 catch (IOException ioe) {}
271 }
272 try { transaction.close(); }
273 catch (IOException ioe) {}
274 }
275 }
276
277 return geomType;
278
279 }
280
281 public static boolean writeMultiPolygonsToFile(
282 File shapeFile,
283 Integer parameterId,
284 Integer layer,
285 Date date,
286 Map<Integer, MultiPolygon> multiPolygons,
287 String name
288 ) {
289 Map<String, Serializable> params = new HashMap<String, Serializable>();
290
291 try {
292 params.put("url", shapeFile.toURI().toURL());
293 }
294 catch (MalformedURLException mue) {
295 log.error(mue.getLocalizedMessage(), mue);
296 return false;
297 }
298
299 params.put("create spatial index", Boolean.TRUE);
300
301
302 if (name == null) {
303 name = shapeFile.getName();
304 }
305
306 SimpleFeatureType TYPE;
307
308 try {
309 TYPE = DataUtilities.createType(
310 name,
311 "geom:MultiPolygon:srid=4326," +
312 "PARAMETER:Integer," +
313 "LAYER:Integer," +
314 "DATE:Date," +
315 "CLASS:Integer");
316 }
317 catch (SchemaException se) {
318 log.error(se.getLocalizedMessage(), se);
319 return false;
320 }
321
322 SimpleFeatureBuilder featureBuilder =
323 new SimpleFeatureBuilder(TYPE);
324
325 FeatureCollection<SimpleFeatureType, SimpleFeature> collection =
326 FeatureCollections.newCollection();
327
328 for (Map.Entry<Integer, MultiPolygon> entry:
329 multiPolygons.entrySet()
330 ) {
331 featureBuilder.add(entry.getValue());
332 featureBuilder.add(parameterId);
333 featureBuilder.add(layer);
334 featureBuilder.add(date);
335 featureBuilder.add(entry.getKey());
336 SimpleFeature feature = featureBuilder.buildFeature(null);
337 collection.add(feature);
338 }
339
340 DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
341
342 Transaction transaction = null;
343
344 boolean success = false;
345 try {
346 ShapefileDataStore newDataStore =
347 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
348
349 newDataStore.createSchema(TYPE);
350 newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
351
352 transaction = new DefaultTransaction("create");
353
354 String typeName = newDataStore.getTypeNames()[0];
355
356 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
357 (FeatureStore<SimpleFeatureType, SimpleFeature>)
358 newDataStore.getFeatureSource(typeName);
359
360 featureStore.setTransaction(transaction);
361
362 featureStore.addFeatures(collection);
363 transaction.commit();
364 success = true;
365 }
366 catch (IOException ioe) {
367 log.error(ioe.getLocalizedMessage(), ioe);
368 }
369 finally {
370 if (transaction != null) {
371 if (!success) {
372 try { transaction.rollback(); }
373 catch (IOException ioe) {}
374 }
375 try { transaction.close(); }
376 catch (IOException ioe) {}
377 }
378 }
379
380 return success;
381 }
382
383 private static final Double asDouble(Object a) {
384 if (a instanceof Double) {
385 return (Double)a;
386 }
387 if (a instanceof Number) {
388 return Double.valueOf(((Number)a).doubleValue());
389 }
390 return 0d;
391 }
392 }
393 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org