I am using GeoTools to load a GeoJSON file into a PostGIS table.
My code looks like this:
FeatureJSON featureJSON = new FeatureJSON();
File geojsonFile = getFile(dataResource); // Loads the GeoJSON as a File
SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) featureJSON.readFeatureCollection(geojsonFile);
SimpleFeatureType featureSchema = featureJSON.readFeatureCollectionSchema(geojsonFile, true);
FeatureSource<SimpleFeatureType, SimpleFeature> geojsonFeatureSource = new CollectionFeatureSource(featureCollection);
Once I get the geojsonFeatureSource
I can then use this to load the data into PostGIS using a DataStore
object for PostGIS.
My problem occurs when I try to delete the GeoJSON file afterwards (held by the geojsonFile
variable in my example). This file has a lock on it and cannot be deleted.
Now, when using previous Data Stores (such as Shapefiles) I am always able to call .dispose()
on the FeatureSource
. However, in this GeoJSON case, I am not able to call .dispose()
on the CollectionFeatureSource
because it is an "In Memory Data Store" - whatever that means.
My cleanup logic looks like:
featureJSON = null;
geojsonFeatureSource = null;
featureCollection = null;
featureSchema = null;
geojsonFile.delete();
How can I get these objects to release their lock on the GeoJSON file so that I can clean up this input GeoJSON file?