From 615c796e96fa5760507af6a4bd1db361b61cc92e Mon Sep 17 00:00:00 2001 From: ptaillandier Date: Tue, 24 Aug 2021 09:05:57 +0700 Subject: [PATCH] try to improve the robustness of shapefile read Avoid locked files --- .../src/msi/gama/util/file/GamaShapeFile.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/msi.gama.core/src/msi/gama/util/file/GamaShapeFile.java b/msi.gama.core/src/msi/gama/util/file/GamaShapeFile.java index 1c16f12ee1..fba02b3b0e 100644 --- a/msi.gama.core/src/msi/gama/util/file/GamaShapeFile.java +++ b/msi.gama.core/src/msi/gama/util/file/GamaShapeFile.java @@ -93,6 +93,7 @@ public ShapeInfo(final IScope scope, final URL url, final long modificationStamp int number = 0; try { store = getDataStore(url); + final SimpleFeatureSource source = store.getFeatureSource(); final SimpleFeatureCollection features = source.getFeatures(); try { @@ -111,12 +112,15 @@ public ShapeInfo(final IScope scope, final URL url, final long modificationStamp try { env = env.transform(new ProjectionFactory().getTargetCRS(scope), true); } catch (final Exception e) { + if (store != null) store.dispose(); throw e; } } try { number = features.size(); } catch (final Exception e) { + + if (store != null) store.dispose(); DEBUG.ERR("Error in loading shapefile: " + e.getMessage()); } final java.util.List att_list = store.getSchema().getAttributeDescriptors(); @@ -359,6 +363,7 @@ protected final void readShapes(final IScope scope) { } }, counter); } catch (final IOException e) { + throw GamaRuntimeException.create(e, scope); } // finally { @@ -382,7 +387,11 @@ protected SimpleFeatureCollection getFeatureCollection(final IScope scope) { query.getHints().put(Hints.JTS_COORDINATE_SEQUENCE_FACTORY, GamaGeometryFactory.COORDINATES_FACTORY); query.getHints().put(Hints.JTS_GEOMETRY_FACTORY, GeometryUtils.GEOMETRY_FACTORY); // AD - return source.getFeatures(query); + SimpleFeatureCollection collection = source.getFeatures(query); + if (source.getDataStore() != null) + source.getDataStore().dispose(); + return collection; + } catch (IOException e) { throw GamaRuntimeException.create(e, scope); }