Mercurial > dive4elements > gnv-client
diff 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 |
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java Fri Mar 05 09:13:17 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java Fri Mar 05 12:35:24 2010 +0000 @@ -1,41 +1,39 @@ package de.intevation.gnv.utils; -import com.vividsolutions.jts.geom.MultiLineString; -import com.vividsolutions.jts.geom.MultiPolygon; - import java.io.File; import java.io.IOException; import java.io.Serializable; - import java.net.MalformedURLException; - +import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; - import org.geotools.data.DataStoreFactorySpi; import org.geotools.data.DataUtilities; import org.geotools.data.DefaultTransaction; import org.geotools.data.FeatureStore; import org.geotools.data.Transaction; - import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.shapefile.ShapefileDataStoreFactory; - import org.geotools.feature.FeatureCollection; import org.geotools.feature.FeatureCollections; import org.geotools.feature.SchemaException; - import org.geotools.feature.simple.SimpleFeatureBuilder; - import org.geotools.referencing.crs.DefaultGeographicCRS; - import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; +import com.vividsolutions.jts.geom.Geometry; +import com.vividsolutions.jts.geom.MultiLineString; +import com.vividsolutions.jts.geom.MultiPolygon; +import com.vividsolutions.jts.io.ParseException; +import com.vividsolutions.jts.io.WKTReader; + +import de.intevation.gnv.geobackend.base.Result; + /** * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) */ @@ -179,6 +177,104 @@ "polygons"); } + + public static boolean writeDataToFile(File shapeFile, + String name, + Collection<Result> data){ + WKTReader wktReader = new WKTReader(); + + Map<String, Serializable> params = new HashMap<String, Serializable>(); + + try { + params.put("url", shapeFile.toURI().toURL()); + } + catch (MalformedURLException mue) { + log.error(mue.getLocalizedMessage(), mue); + return false; + } + + params.put("create spatial index", Boolean.TRUE); + + + if (name == null) { + name = shapeFile.getName(); + } + + SimpleFeatureType type = null; + SimpleFeatureBuilder featureBuilder = null; + FeatureCollection<SimpleFeatureType, SimpleFeature> collection = + FeatureCollections.newCollection(); + + for (Result result: data) { + try { + Geometry g = wktReader.read(result.getString(0)); + if (type == null){ + try { + type = DataUtilities.createType( + name, + "geom:"+g.getGeometryType()+":srid=4326"); + // TODO add other AttributeTypes + } + catch (SchemaException se) { + log.error(se.getLocalizedMessage(), se); + return false; + } + featureBuilder = new SimpleFeatureBuilder(type); + } + featureBuilder.add(g); + SimpleFeature feature = featureBuilder.buildFeature(null); + collection.add(feature); + } catch (ParseException e) { + log.error(e,e); + } catch (java.lang.IllegalArgumentException e){ + log.error("cannot create geometry for "+result.getString(0)); + } + } + + DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory(); + + Transaction transaction = null; + + boolean success = false; + try { + ShapefileDataStore newDataStore = + (ShapefileDataStore)dataStoreFactory.createNewDataStore(params); + + newDataStore.createSchema(type); + newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); + + transaction = new DefaultTransaction("create"); + + String typeName = newDataStore.getTypeNames()[0]; + + FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = + (FeatureStore<SimpleFeatureType, SimpleFeature>) + newDataStore.getFeatureSource(typeName); + + featureStore.setTransaction(transaction); + + featureStore.addFeatures(collection); + transaction.commit(); + success = true; + } + catch (IOException ioe) { + log.error(ioe.getLocalizedMessage(), ioe); + } + finally { + if (transaction != null) { + if (!success) { + try { transaction.rollback(); } + catch (IOException ioe) {} + } + try { transaction.close(); } + catch (IOException ioe) {} + } + } + + return success; + + } + public static boolean writeMultiPolygonsToFile( File shapeFile, Integer parameterId,