# HG changeset patch # User Ingo Weinzierl # Date 1314862064 0 # Node ID 66783d957201ae17c1a0e08e7e55e7f6238c8065 # Parent fb3947027e9254b61027c51592dc62507007e825 Close open shapefile transactions properly. flys-artifacts/trunk@2628 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r fb3947027e92 -r 66783d957201 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Thu Sep 01 07:06:14 2011 +0000 +++ b/flys-artifacts/ChangeLog Thu Sep 01 07:27:44 2011 +0000 @@ -1,3 +1,14 @@ +2011-09-01 Ingo Weinzierl + + * src/main/java/de/intevation/flys/utils/GeometryUtils.java: Close an open + shapefile transaction and catch exceptions which are thrown while + shapefile creation here. If there occured an error, this functions + returns FALSE, otherwise TRUE. + + * src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java: + Removed exception handling while shapefile creation - this is done in + GeometryUtils now. + 2011-09-01 Ingo Weinzierl * src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java: diff -r fb3947027e92 -r 66783d957201 flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java Thu Sep 01 07:06:14 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java Thu Sep 01 07:27:44 2011 +0000 @@ -4,7 +4,6 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.net.MalformedURLException; import java.util.List; import javax.xml.xpath.XPathConstants; @@ -300,25 +299,23 @@ File shapeLines = new File(dir, WSPLGEN_BARRIERS_LINES); File shapePolys = new File(dir, WSPLGEN_BARRIERS_POLY); - try { - GeometryUtils.writeShapefile( - shapeLines, - GeometryUtils.buildFeatureType("lines", srs, LineString.class), - fcs[0]); - job.addLin(shapeLines.getAbsolutePath()); + boolean l = GeometryUtils.writeShapefile( + shapeLines, + GeometryUtils.buildFeatureType("lines", srs, LineString.class), + fcs[0]); - GeometryUtils.writeShapefile( - shapePolys, - GeometryUtils.buildFeatureType("polygons", srs, Polygon.class), - fcs[1]); + if (l) { + job.addLin(shapeLines.getAbsolutePath()); + } + + boolean p = GeometryUtils.writeShapefile( + shapePolys, + GeometryUtils.buildFeatureType("polygons", srs, Polygon.class), + fcs[1]); + + if (p) { job.addLin(shapePolys.getAbsolutePath()); } - catch (MalformedURLException mue) { - logger.error("Error while writing shapefile: " + mue.getMessage()); - } - catch (IOException ioe) { - logger.error("Error while writing shapefile: " + ioe.getMessage()); - } } @@ -379,19 +376,14 @@ File axisShape = new File(dir, WSPLGEN_AXIS); - try { - GeometryUtils.writeShapefile( - axisShape, - GeometryUtils.buildFeatureType("axis", srs, LineString.class), - collection); + boolean a = GeometryUtils.writeShapefile( + axisShape, + GeometryUtils.buildFeatureType("axis", srs, LineString.class), + collection); + + if (a) { job.setAxis(axisShape.getAbsolutePath()); } - catch (MalformedURLException mue) { - logger.error("Error while writing shapefile: " + mue.getMessage()); - } - catch (IOException ioe) { - logger.error("Error while writing shapefile: " + ioe.getMessage()); - } } @@ -427,20 +419,14 @@ File qpsShape = new File(dir, WSPLGEN_QPS); - try { - GeometryUtils.writeShapefile( - qpsShape, - GeometryUtils.buildFeatureType( - "qps", srs, LineString.class, attrs), - collection); + boolean q = GeometryUtils.writeShapefile( + qpsShape, + GeometryUtils.buildFeatureType("qps", srs, LineString.class, attrs), + collection); + + if (q) { job.setPro(qpsShape.getAbsolutePath()); } - catch (MalformedURLException mue) { - logger.error("Error while writing shapefile: " + mue.getMessage()); - } - catch (IOException ioe) { - logger.error("Error while writing shapefile: " + ioe.getMessage()); - } } @@ -480,20 +466,14 @@ File talaueShape = new File(dir, WSPLGEN_FLOODPLAIN); - try { - GeometryUtils.writeShapefile( - talaueShape, - GeometryUtils.buildFeatureType( - "talaue", srs, MultiPolygon.class), - collection); + boolean t = GeometryUtils.writeShapefile( + talaueShape, + GeometryUtils.buildFeatureType("talaue", srs, MultiPolygon.class), + collection); + + if (t) { job.setArea(talaueShape.getAbsolutePath()); } - catch (MalformedURLException mue) { - logger.error("Error while writing shapefile: " + mue.getMessage()); - } - catch (IOException ioe) { - logger.error("Error while writing shapefile: " + ioe.getMessage()); - } } diff -r fb3947027e92 -r 66783d957201 flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java --- a/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java Thu Sep 01 07:06:14 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java Thu Sep 01 07:27:44 2011 +0000 @@ -9,6 +9,8 @@ import java.util.List; import java.util.Map; +import org.apache.log4j.Logger; + import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; @@ -33,6 +35,9 @@ public class GeometryUtils { + private static final Logger logger = Logger.getLogger(GeometryUtils.class); + + private GeometryUtils() { } @@ -133,33 +138,56 @@ public static boolean writeShapefile( File shape, SimpleFeatureType featureType, - FeatureCollection collection) - throws MalformedURLException, IOException - { - Map params = new HashMap(); - params.put("url", shape.toURI().toURL()); - params.put("create spatial index", Boolean.TRUE); - - DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory(); - - ShapefileDataStore newDataStore = - (ShapefileDataStore)dataStoreFactory.createNewDataStore(params); - newDataStore.createSchema(featureType); + FeatureCollection collection + ) { + Transaction transaction = null; - Transaction transaction = new DefaultTransaction("create"); - - String typeName = newDataStore.getTypeNames()[0]; + try { + Map params = + new HashMap(); - FeatureStore featureStore = - (FeatureStore) - newDataStore.getFeatureSource(typeName); + params.put("url", shape.toURI().toURL()); + params.put("create spatial index", Boolean.TRUE); - featureStore.setTransaction(transaction); + DataStoreFactorySpi dataStoreFactory = + new ShapefileDataStoreFactory(); - featureStore.addFeatures(collection); - transaction.commit(); + ShapefileDataStore newDataStore = + (ShapefileDataStore)dataStoreFactory.createNewDataStore(params); + newDataStore.createSchema(featureType); - return true; + transaction = new DefaultTransaction("create"); + + String typeName = newDataStore.getTypeNames()[0]; + + FeatureStore featureStore = + (FeatureStore) + newDataStore.getFeatureSource(typeName); + + featureStore.setTransaction(transaction); + + featureStore.addFeatures(collection); + transaction.commit(); + + return true; + } + catch (MalformedURLException mue) { + logger.error("Unable to prepare shapefile: " + mue.getMessage()); + } + catch (IOException ioe) { + logger.error("Unable to write shapefile: " + ioe.getMessage()); + } + finally { + if (transaction != null) { + try { + logger.debug("XXX CLOSE TRANSACTION!"); + transaction.close(); + } + catch (IOException ioe) { /* do nothing */ } + } + } + + return false; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :