Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java @ 649:4fc97074eb90
Added Support for writing Shapefiles and Export them as an Zipfile for the Product Layer.
gnv-artifacts/trunk@738 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Fri, 05 Mar 2010 12:35:24 +0000 |
parents | 4080b57dcb52 |
children | 6eccb68a8b99 |
comparison
equal
deleted
inserted
replaced
648:7c67ff162e87 | 649:4fc97074eb90 |
---|---|
1 package de.intevation.gnv.utils; | 1 package de.intevation.gnv.utils; |
2 | |
3 import com.vividsolutions.jts.geom.MultiLineString; | |
4 import com.vividsolutions.jts.geom.MultiPolygon; | |
5 | 2 |
6 import java.io.File; | 3 import java.io.File; |
7 import java.io.IOException; | 4 import java.io.IOException; |
8 import java.io.Serializable; | 5 import java.io.Serializable; |
9 | |
10 import java.net.MalformedURLException; | 6 import java.net.MalformedURLException; |
11 | 7 import java.util.Collection; |
12 import java.util.Date; | 8 import java.util.Date; |
13 import java.util.HashMap; | 9 import java.util.HashMap; |
14 import java.util.List; | 10 import java.util.List; |
15 import java.util.Map; | 11 import java.util.Map; |
16 | 12 |
17 import org.apache.log4j.Logger; | 13 import org.apache.log4j.Logger; |
18 | |
19 import org.geotools.data.DataStoreFactorySpi; | 14 import org.geotools.data.DataStoreFactorySpi; |
20 import org.geotools.data.DataUtilities; | 15 import org.geotools.data.DataUtilities; |
21 import org.geotools.data.DefaultTransaction; | 16 import org.geotools.data.DefaultTransaction; |
22 import org.geotools.data.FeatureStore; | 17 import org.geotools.data.FeatureStore; |
23 import org.geotools.data.Transaction; | 18 import org.geotools.data.Transaction; |
24 | |
25 import org.geotools.data.shapefile.ShapefileDataStore; | 19 import org.geotools.data.shapefile.ShapefileDataStore; |
26 import org.geotools.data.shapefile.ShapefileDataStoreFactory; | 20 import org.geotools.data.shapefile.ShapefileDataStoreFactory; |
27 | |
28 import org.geotools.feature.FeatureCollection; | 21 import org.geotools.feature.FeatureCollection; |
29 import org.geotools.feature.FeatureCollections; | 22 import org.geotools.feature.FeatureCollections; |
30 import org.geotools.feature.SchemaException; | 23 import org.geotools.feature.SchemaException; |
31 | |
32 import org.geotools.feature.simple.SimpleFeatureBuilder; | 24 import org.geotools.feature.simple.SimpleFeatureBuilder; |
33 | |
34 import org.geotools.referencing.crs.DefaultGeographicCRS; | 25 import org.geotools.referencing.crs.DefaultGeographicCRS; |
35 | |
36 import org.opengis.feature.simple.SimpleFeature; | 26 import org.opengis.feature.simple.SimpleFeature; |
37 import org.opengis.feature.simple.SimpleFeatureType; | 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; | |
38 | 36 |
39 /** | 37 /** |
40 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) | 38 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) |
41 */ | 39 */ |
42 public final class ShapeFileWriter | 40 public final class ShapeFileWriter |
177 date, | 175 date, |
178 multiPolygons, | 176 multiPolygons, |
179 "polygons"); | 177 "polygons"); |
180 } | 178 } |
181 | 179 |
180 | |
181 public static boolean writeDataToFile(File shapeFile, | |
182 String name, | |
183 Collection<Result> data){ | |
184 WKTReader wktReader = new WKTReader(); | |
185 | |
186 Map<String, Serializable> params = new HashMap<String, Serializable>(); | |
187 | |
188 try { | |
189 params.put("url", shapeFile.toURI().toURL()); | |
190 } | |
191 catch (MalformedURLException mue) { | |
192 log.error(mue.getLocalizedMessage(), mue); | |
193 return false; | |
194 } | |
195 | |
196 params.put("create spatial index", Boolean.TRUE); | |
197 | |
198 | |
199 if (name == null) { | |
200 name = shapeFile.getName(); | |
201 } | |
202 | |
203 SimpleFeatureType type = null; | |
204 SimpleFeatureBuilder featureBuilder = null; | |
205 FeatureCollection<SimpleFeatureType, SimpleFeature> collection = | |
206 FeatureCollections.newCollection(); | |
207 | |
208 for (Result result: data) { | |
209 try { | |
210 Geometry g = wktReader.read(result.getString(0)); | |
211 if (type == null){ | |
212 try { | |
213 type = DataUtilities.createType( | |
214 name, | |
215 "geom:"+g.getGeometryType()+":srid=4326"); | |
216 // TODO add other AttributeTypes | |
217 } | |
218 catch (SchemaException se) { | |
219 log.error(se.getLocalizedMessage(), se); | |
220 return false; | |
221 } | |
222 featureBuilder = new SimpleFeatureBuilder(type); | |
223 } | |
224 featureBuilder.add(g); | |
225 SimpleFeature feature = featureBuilder.buildFeature(null); | |
226 collection.add(feature); | |
227 } catch (ParseException e) { | |
228 log.error(e,e); | |
229 } catch (java.lang.IllegalArgumentException e){ | |
230 log.error("cannot create geometry for "+result.getString(0)); | |
231 } | |
232 } | |
233 | |
234 DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory(); | |
235 | |
236 Transaction transaction = null; | |
237 | |
238 boolean success = false; | |
239 try { | |
240 ShapefileDataStore newDataStore = | |
241 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params); | |
242 | |
243 newDataStore.createSchema(type); | |
244 newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); | |
245 | |
246 transaction = new DefaultTransaction("create"); | |
247 | |
248 String typeName = newDataStore.getTypeNames()[0]; | |
249 | |
250 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = | |
251 (FeatureStore<SimpleFeatureType, SimpleFeature>) | |
252 newDataStore.getFeatureSource(typeName); | |
253 | |
254 featureStore.setTransaction(transaction); | |
255 | |
256 featureStore.addFeatures(collection); | |
257 transaction.commit(); | |
258 success = true; | |
259 } | |
260 catch (IOException ioe) { | |
261 log.error(ioe.getLocalizedMessage(), ioe); | |
262 } | |
263 finally { | |
264 if (transaction != null) { | |
265 if (!success) { | |
266 try { transaction.rollback(); } | |
267 catch (IOException ioe) {} | |
268 } | |
269 try { transaction.close(); } | |
270 catch (IOException ioe) {} | |
271 } | |
272 } | |
273 | |
274 return success; | |
275 | |
276 } | |
277 | |
182 public static boolean writeMultiPolygonsToFile( | 278 public static boolean writeMultiPolygonsToFile( |
183 File shapeFile, | 279 File shapeFile, |
184 Integer parameterId, | 280 Integer parameterId, |
185 Integer layer, | 281 Integer layer, |
186 Date date, | 282 Date date, |