Skip to content

Commit

Permalink
try to improve the robustness of shapefile read
Browse files Browse the repository at this point in the history
Avoid locked files
  • Loading branch information
ptaillandier committed Aug 24, 2021
1 parent 19fd59e commit 615c796
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion msi.gama.core/src/msi/gama/util/file/GamaShapeFile.java
Expand Up @@ -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 {
Expand All @@ -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<AttributeDescriptor> att_list = store.getSchema().getAttributeDescriptors();
Expand Down Expand Up @@ -359,6 +363,7 @@ protected final void readShapes(final IScope scope) {
}
}, counter);
} catch (final IOException e) {

throw GamaRuntimeException.create(e, scope);
}
// finally {
Expand All @@ -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);
}
Expand Down

0 comments on commit 615c796

Please sign in to comment.