comparison flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 1121:66783d957201

Close open shapefile transactions properly. flys-artifacts/trunk@2628 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 01 Sep 2011 07:27:44 +0000
parents faca1825818e
children c07e9e9c7482
comparison
equal deleted inserted replaced
1120:fb3947027e92 1121:66783d957201
6 import java.net.MalformedURLException; 6 import java.net.MalformedURLException;
7 import java.util.ArrayList; 7 import java.util.ArrayList;
8 import java.util.HashMap; 8 import java.util.HashMap;
9 import java.util.List; 9 import java.util.List;
10 import java.util.Map; 10 import java.util.Map;
11
12 import org.apache.log4j.Logger;
11 13
12 import com.vividsolutions.jts.geom.Coordinate; 14 import com.vividsolutions.jts.geom.Coordinate;
13 import com.vividsolutions.jts.geom.Geometry; 15 import com.vividsolutions.jts.geom.Geometry;
14 16
15 import org.opengis.feature.simple.SimpleFeature; 17 import org.opengis.feature.simple.SimpleFeature;
30 import org.geotools.geojson.feature.FeatureJSON; 32 import org.geotools.geojson.feature.FeatureJSON;
31 import org.geotools.referencing.CRS; 33 import org.geotools.referencing.CRS;
32 34
33 35
34 public class GeometryUtils { 36 public class GeometryUtils {
37
38 private static final Logger logger = Logger.getLogger(GeometryUtils.class);
39
35 40
36 private GeometryUtils() { 41 private GeometryUtils() {
37 } 42 }
38 43
39 44
131 136
132 137
133 public static boolean writeShapefile( 138 public static boolean writeShapefile(
134 File shape, 139 File shape,
135 SimpleFeatureType featureType, 140 SimpleFeatureType featureType,
136 FeatureCollection collection) 141 FeatureCollection collection
137 throws MalformedURLException, IOException 142 ) {
138 { 143 Transaction transaction = null;
139 Map<String, Serializable> params = new HashMap<String, Serializable>();
140 params.put("url", shape.toURI().toURL());
141 params.put("create spatial index", Boolean.TRUE);
142 144
143 DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory(); 145 try {
146 Map<String, Serializable> params =
147 new HashMap<String, Serializable>();
144 148
145 ShapefileDataStore newDataStore = 149 params.put("url", shape.toURI().toURL());
146 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params); 150 params.put("create spatial index", Boolean.TRUE);
147 newDataStore.createSchema(featureType);
148 151
149 Transaction transaction = new DefaultTransaction("create"); 152 DataStoreFactorySpi dataStoreFactory =
153 new ShapefileDataStoreFactory();
150 154
151 String typeName = newDataStore.getTypeNames()[0]; 155 ShapefileDataStore newDataStore =
156 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
157 newDataStore.createSchema(featureType);
152 158
153 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = 159 transaction = new DefaultTransaction("create");
154 (FeatureStore<SimpleFeatureType, SimpleFeature>)
155 newDataStore.getFeatureSource(typeName);
156 160
157 featureStore.setTransaction(transaction); 161 String typeName = newDataStore.getTypeNames()[0];
158 162
159 featureStore.addFeatures(collection); 163 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
160 transaction.commit(); 164 (FeatureStore<SimpleFeatureType, SimpleFeature>)
165 newDataStore.getFeatureSource(typeName);
161 166
162 return true; 167 featureStore.setTransaction(transaction);
168
169 featureStore.addFeatures(collection);
170 transaction.commit();
171
172 return true;
173 }
174 catch (MalformedURLException mue) {
175 logger.error("Unable to prepare shapefile: " + mue.getMessage());
176 }
177 catch (IOException ioe) {
178 logger.error("Unable to write shapefile: " + ioe.getMessage());
179 }
180 finally {
181 if (transaction != null) {
182 try {
183 logger.debug("XXX CLOSE TRANSACTION!");
184 transaction.close();
185 }
186 catch (IOException ioe) { /* do nothing */ }
187 }
188 }
189
190 return false;
163 } 191 }
164 } 192 }
165 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 193 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org