Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 2762:33aa37e6d98f
Reproject geometries before exporting them as shapefiles to filesystem.
flys-artifacts/trunk@4498 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 24 May 2012 07:58:47 +0000 |
parents | cbeeaaad1056 |
children | f062b5a90e26 |
comparison
equal
deleted
inserted
replaced
2761:28e7c1637f78 | 2762:33aa37e6d98f |
---|---|
18 import org.opengis.feature.simple.SimpleFeature; | 18 import org.opengis.feature.simple.SimpleFeature; |
19 import org.opengis.feature.simple.SimpleFeatureType; | 19 import org.opengis.feature.simple.SimpleFeatureType; |
20 import org.opengis.referencing.FactoryException; | 20 import org.opengis.referencing.FactoryException; |
21 import org.opengis.referencing.NoSuchAuthorityCodeException; | 21 import org.opengis.referencing.NoSuchAuthorityCodeException; |
22 import org.opengis.referencing.crs.CoordinateReferenceSystem; | 22 import org.opengis.referencing.crs.CoordinateReferenceSystem; |
23 import org.opengis.referencing.operation.MathTransform; | |
24 import org.opengis.referencing.operation.TransformException; | |
23 | 25 |
24 import org.geotools.data.DataStoreFactorySpi; | 26 import org.geotools.data.DataStoreFactorySpi; |
25 import org.geotools.data.FeatureStore; | |
26 import org.geotools.data.DefaultTransaction; | 27 import org.geotools.data.DefaultTransaction; |
28 import org.geotools.data.FeatureWriter; | |
27 import org.geotools.data.Transaction; | 29 import org.geotools.data.Transaction; |
28 import org.geotools.data.shapefile.ShapefileDataStore; | 30 import org.geotools.data.shapefile.ShapefileDataStore; |
29 import org.geotools.data.shapefile.ShapefileDataStoreFactory; | 31 import org.geotools.data.shapefile.ShapefileDataStoreFactory; |
32 import org.geotools.data.simple.SimpleFeatureIterator; | |
30 import org.geotools.feature.FeatureIterator; | 33 import org.geotools.feature.FeatureIterator; |
31 import org.geotools.feature.FeatureCollection; | 34 import org.geotools.feature.FeatureCollection; |
32 import org.geotools.feature.simple.SimpleFeatureTypeBuilder; | 35 import org.geotools.feature.simple.SimpleFeatureTypeBuilder; |
33 import org.geotools.geojson.feature.FeatureJSON; | 36 import org.geotools.geojson.feature.FeatureJSON; |
37 import org.geotools.geometry.jts.JTS; | |
34 import org.geotools.referencing.CRS; | 38 import org.geotools.referencing.CRS; |
35 | 39 |
36 import de.intevation.flys.model.RiverAxis; | 40 import de.intevation.flys.model.RiverAxis; |
37 | 41 |
38 | 42 |
39 public class GeometryUtils { | 43 public class GeometryUtils { |
40 | 44 |
41 private static final Logger logger = Logger.getLogger(GeometryUtils.class); | 45 private static final Logger logger = Logger.getLogger(GeometryUtils.class); |
46 | |
47 public static final String DEFAULT_EPSG = "EPSG:31467"; | |
42 | 48 |
43 | 49 |
44 private GeometryUtils() { | 50 private GeometryUtils() { |
45 } | 51 } |
46 | 52 |
204 public static boolean writeShapefile( | 210 public static boolean writeShapefile( |
205 File shape, | 211 File shape, |
206 SimpleFeatureType featureType, | 212 SimpleFeatureType featureType, |
207 FeatureCollection collection | 213 FeatureCollection collection |
208 ) { | 214 ) { |
215 return writeShapefile( | |
216 shape, featureType, collection, featureType.getCoordinateReferenceSystem()); | |
217 } | |
218 | |
219 | |
220 public static boolean writeShapefile( | |
221 File shape, | |
222 SimpleFeatureType featureType, | |
223 FeatureCollection collection, | |
224 CoordinateReferenceSystem crs | |
225 ) { | |
209 if (collection.isEmpty()) { | 226 if (collection.isEmpty()) { |
210 logger.warn("Shapefile is not written - no features given!"); | 227 logger.warn("Shapefile is not written - no features given!"); |
211 return false; | 228 return false; |
212 } | 229 } |
213 | 230 |
214 Transaction transaction = null; | 231 Transaction transaction = null; |
215 | 232 |
216 try { | 233 try { |
234 MathTransform transform = CRS.findMathTransform( | |
235 CRS.decode(DEFAULT_EPSG), crs); | |
236 | |
217 Map<String, Serializable> params = | 237 Map<String, Serializable> params = |
218 new HashMap<String, Serializable>(); | 238 new HashMap<String, Serializable>(); |
219 | 239 |
220 params.put("url", shape.toURI().toURL()); | 240 params.put("url", shape.toURI().toURL()); |
221 params.put("create spatial index", Boolean.TRUE); | 241 params.put("create spatial index", Boolean.TRUE); |
229 | 249 |
230 transaction = new DefaultTransaction("create"); | 250 transaction = new DefaultTransaction("create"); |
231 | 251 |
232 String typeName = newDataStore.getTypeNames()[0]; | 252 String typeName = newDataStore.getTypeNames()[0]; |
233 | 253 |
234 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = | 254 FeatureWriter<SimpleFeatureType, SimpleFeature> writer = |
235 (FeatureStore<SimpleFeatureType, SimpleFeature>) | 255 newDataStore.getFeatureWriter(typeName, transaction); |
236 newDataStore.getFeatureSource(typeName); | 256 |
237 | 257 SimpleFeatureIterator iterator = (SimpleFeatureIterator) collection.features(); |
238 featureStore.setTransaction(transaction); | 258 |
239 | 259 while (iterator.hasNext()){ |
240 featureStore.addFeatures(collection); | 260 SimpleFeature feature = iterator.next(); |
261 SimpleFeature copy = writer.next(); | |
262 | |
263 copy.setAttributes(feature.getAttributes()); | |
264 | |
265 Geometry orig = (Geometry) feature.getDefaultGeometry(); | |
266 Geometry reprojected = JTS.transform(orig, transform); | |
267 | |
268 copy.setDefaultGeometry(reprojected); | |
269 writer.write(); | |
270 } | |
271 | |
241 transaction.commit(); | 272 transaction.commit(); |
242 | 273 |
243 return true; | 274 return true; |
244 } | 275 } |
245 catch (MalformedURLException mue) { | 276 catch (MalformedURLException mue) { |
246 logger.error("Unable to prepare shapefile: " + mue.getMessage()); | 277 logger.error("Unable to prepare shapefile: " + mue.getMessage()); |
247 } | 278 } |
248 catch (IOException ioe) { | 279 catch (IOException ioe) { |
249 logger.error("Unable to write shapefile: " + ioe.getMessage()); | 280 logger.error("Unable to write shapefile: " + ioe.getMessage()); |
281 } | |
282 catch (NoSuchAuthorityCodeException nsae) { | |
283 logger.error("Cannot get CoordinateReferenceSystem for '" | |
284 + DEFAULT_EPSG + "'"); | |
285 } | |
286 catch (FactoryException fe) { | |
287 logger.error("Cannot get CoordinateReferenceSystem for '" | |
288 + DEFAULT_EPSG + "'"); | |
289 } | |
290 catch (TransformException te) { | |
291 logger.error("Was not able to transform geometry!", te); | |
250 } | 292 } |
251 finally { | 293 finally { |
252 if (transaction != null) { | 294 if (transaction != null) { |
253 try { | 295 try { |
254 logger.debug("XXX CLOSE TRANSACTION!"); | |
255 transaction.close(); | 296 transaction.close(); |
256 } | 297 } |
257 catch (IOException ioe) { /* do nothing */ } | 298 catch (IOException ioe) { /* do nothing */ } |
258 } | 299 } |
259 } | 300 } |