sascha@465: package de.intevation.gnv.utils; sascha@465: sascha@498: import com.vividsolutions.jts.geom.MultiLineString; sascha@498: import com.vividsolutions.jts.geom.MultiPolygon; sascha@465: sascha@465: import java.io.File; sascha@465: import java.io.IOException; sascha@498: import java.io.Serializable; sascha@465: sascha@465: import java.net.MalformedURLException; sascha@465: sascha@498: import java.util.Date; sascha@498: import java.util.HashMap; sascha@498: import java.util.List; sascha@498: import java.util.Map; sascha@465: sascha@498: import org.apache.log4j.Logger; sascha@498: sascha@498: import org.geotools.data.DataStoreFactorySpi; sascha@465: import org.geotools.data.DataUtilities; sascha@465: import org.geotools.data.DefaultTransaction; sascha@498: import org.geotools.data.FeatureStore; sascha@498: import org.geotools.data.Transaction; sascha@465: sascha@465: import org.geotools.data.shapefile.ShapefileDataStore; sascha@465: import org.geotools.data.shapefile.ShapefileDataStoreFactory; sascha@465: sascha@498: import org.geotools.feature.FeatureCollection; sascha@498: import org.geotools.feature.FeatureCollections; sascha@498: import org.geotools.feature.SchemaException; sascha@498: sascha@498: import org.geotools.feature.simple.SimpleFeatureBuilder; sascha@498: sascha@498: import org.geotools.referencing.crs.DefaultGeographicCRS; sascha@498: sascha@498: import org.opengis.feature.simple.SimpleFeature; sascha@498: import org.opengis.feature.simple.SimpleFeatureType; sascha@465: sascha@465: /** sascha@465: * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) sascha@465: */ sascha@465: public final class ShapeFileWriter sascha@465: { sascha@465: private static Logger log = Logger.getLogger( sascha@465: ShapeFileWriter.class); sascha@465: sascha@465: private ShapeFileWriter() { sascha@465: } sascha@465: sascha@465: public static boolean writeMultiLineStringsToFile( sascha@465: File shapeFile, sascha@498: Integer parameterId, sascha@498: Integer layer, sascha@498: Date date, sascha@465: List> multiLineStrings sascha@465: ) { sascha@498: return writeMultiLineStringsToFile( sascha@498: shapeFile, sascha@498: parameterId, sascha@498: layer, sascha@498: date, sascha@498: multiLineStrings, sascha@498: "isolines"); sascha@465: } sascha@465: sascha@465: public static boolean writeMultiLineStringsToFile( sascha@465: File shapeFile, sascha@498: Integer parameterId, sascha@498: Integer layer, sascha@498: Date date, sascha@465: List> multiLineStrings, sascha@498: String name sascha@465: ) { sascha@498: Map params = new HashMap(); sascha@465: sascha@465: try { sascha@498: params.put("url", shapeFile.toURI().toURL()); sascha@465: } sascha@465: catch (MalformedURLException mue) { sascha@465: log.error(mue.getLocalizedMessage(), mue); sascha@465: return false; sascha@465: } sascha@465: sascha@498: params.put("create spatial index", Boolean.TRUE); sascha@465: sascha@465: sascha@498: if (name == null) { sascha@498: name = shapeFile.getName(); sascha@498: } sascha@465: sascha@498: SimpleFeatureType TYPE; sascha@498: sascha@498: try { sascha@498: TYPE = DataUtilities.createType( sascha@498: name, sascha@498: "geom:MultiLineString:srid=4326," + sascha@498: "PARAMETER:Integer," + sascha@498: "LAYER:Integer," + sascha@498: "DATE:Date," + sascha@498: "VALUE:Double"); sascha@465: } sascha@465: catch (SchemaException se) { sascha@465: log.error(se.getLocalizedMessage(), se); sascha@498: return false; sascha@465: } sascha@498: sascha@498: SimpleFeatureBuilder featureBuilder = sascha@498: new SimpleFeatureBuilder(TYPE); sascha@498: sascha@498: FeatureCollection collection = sascha@498: FeatureCollections.newCollection(); sascha@498: sascha@498: for (Pair pair: multiLineStrings) { sascha@498: featureBuilder.add(pair.getB()); sascha@498: featureBuilder.add(parameterId); sascha@498: featureBuilder.add(layer); sascha@498: featureBuilder.add(date); sascha@498: featureBuilder.add(pair.getA()); sascha@498: SimpleFeature feature = featureBuilder.buildFeature(null); sascha@498: collection.add(feature); sascha@498: } sascha@498: sascha@498: DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory(); sascha@498: sascha@498: Transaction transaction = null; sascha@498: sascha@498: boolean success = false; sascha@498: try { sascha@498: ShapefileDataStore newDataStore = sascha@498: (ShapefileDataStore)dataStoreFactory.createNewDataStore(params); sascha@498: sascha@498: newDataStore.createSchema(TYPE); sascha@498: newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); sascha@498: sascha@498: transaction = new DefaultTransaction("create"); sascha@498: sascha@498: String typeName = newDataStore.getTypeNames()[0]; sascha@498: sascha@498: FeatureStore featureStore = sascha@498: (FeatureStore) sascha@498: newDataStore.getFeatureSource(typeName); sascha@498: sascha@498: featureStore.setTransaction(transaction); sascha@498: sascha@498: featureStore.addFeatures(collection); sascha@498: transaction.commit(); sascha@498: success = true; sascha@498: } sascha@465: catch (IOException ioe) { sascha@465: log.error(ioe.getLocalizedMessage(), ioe); sascha@465: } sascha@465: finally { sascha@498: if (transaction != null) { sascha@498: if (!success) { sascha@498: try { transaction.rollback(); } sascha@498: catch (IOException ioe) {} sascha@498: } sascha@498: try { transaction.close(); } sascha@465: catch (IOException ioe) {} sascha@465: } sascha@465: } sascha@465: sascha@465: return success; sascha@465: } sascha@465: sascha@465: public static boolean writeMultiPolygonsToFile( sascha@465: File shapeFile, sascha@498: Integer parameterId, sascha@498: Integer layer, sascha@498: Date date, sascha@465: Map multiPolygons sascha@465: ) { sascha@498: return writeMultiPolygonsToFile( sascha@498: shapeFile, sascha@498: parameterId, sascha@498: layer, sascha@498: date, sascha@498: multiPolygons, sascha@498: "polygons"); sascha@465: } sascha@465: sascha@465: public static boolean writeMultiPolygonsToFile( sascha@465: File shapeFile, sascha@498: Integer parameterId, sascha@498: Integer layer, sascha@498: Date date, sascha@465: Map multiPolygons, sascha@498: String name sascha@465: ) { sascha@498: Map params = new HashMap(); sascha@465: sascha@465: try { sascha@498: params.put("url", shapeFile.toURI().toURL()); sascha@465: } sascha@465: catch (MalformedURLException mue) { sascha@465: log.error(mue.getLocalizedMessage(), mue); sascha@465: return false; sascha@465: } sascha@465: sascha@498: params.put("create spatial index", Boolean.TRUE); sascha@465: sascha@465: sascha@498: if (name == null) { sascha@498: name = shapeFile.getName(); sascha@498: } sascha@465: sascha@498: SimpleFeatureType TYPE; sascha@498: sascha@498: try { sascha@498: TYPE = DataUtilities.createType( sascha@498: name, sascha@498: "geom:MultiPolygon:srid=4326," + sascha@498: "PARAMETER:Integer," + sascha@498: "LAYER:Integer," + sascha@498: "DATE:Date," + sascha@498: "CLASS:Integer"); sascha@465: } sascha@465: catch (SchemaException se) { sascha@465: log.error(se.getLocalizedMessage(), se); sascha@498: return false; sascha@465: } sascha@498: sascha@498: SimpleFeatureBuilder featureBuilder = sascha@498: new SimpleFeatureBuilder(TYPE); sascha@498: sascha@498: FeatureCollection collection = sascha@498: FeatureCollections.newCollection(); sascha@498: sascha@498: for (Map.Entry entry: sascha@498: multiPolygons.entrySet() sascha@498: ) { sascha@498: featureBuilder.add(entry.getValue()); sascha@498: featureBuilder.add(parameterId); sascha@498: featureBuilder.add(layer); sascha@498: featureBuilder.add(date); sascha@498: featureBuilder.add(entry.getKey()); sascha@498: SimpleFeature feature = featureBuilder.buildFeature(null); sascha@498: collection.add(feature); sascha@498: } sascha@498: sascha@498: DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory(); sascha@498: sascha@498: Transaction transaction = null; sascha@498: sascha@498: boolean success = false; sascha@498: try { sascha@498: ShapefileDataStore newDataStore = sascha@498: (ShapefileDataStore)dataStoreFactory.createNewDataStore(params); sascha@498: sascha@498: newDataStore.createSchema(TYPE); sascha@498: newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); sascha@498: sascha@498: transaction = new DefaultTransaction("create"); sascha@498: sascha@498: String typeName = newDataStore.getTypeNames()[0]; sascha@498: sascha@498: FeatureStore featureStore = sascha@498: (FeatureStore) sascha@498: newDataStore.getFeatureSource(typeName); sascha@498: sascha@498: featureStore.setTransaction(transaction); sascha@498: sascha@498: featureStore.addFeatures(collection); sascha@498: transaction.commit(); sascha@498: success = true; sascha@498: } sascha@465: catch (IOException ioe) { sascha@465: log.error(ioe.getLocalizedMessage(), ioe); sascha@465: } sascha@465: finally { sascha@498: if (transaction != null) { sascha@498: if (!success) { sascha@498: try { transaction.rollback(); } sascha@498: catch (IOException ioe) {} sascha@498: } sascha@498: try { transaction.close(); } sascha@465: catch (IOException ioe) {} sascha@465: } sascha@465: } sascha@465: sascha@465: return success; sascha@465: } sascha@465: sascha@465: private static final Double asDouble(Object a) { sascha@498: if (a instanceof Double) { sascha@465: return (Double)a; sascha@498: } sascha@498: if (a instanceof Number) { sascha@465: return Double.valueOf(((Number)a).doubleValue()); sascha@498: } sascha@465: return 0d; sascha@465: } sascha@465: } sascha@465: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :