0

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?

  • 1
    Please consider using the geojson datastore instead of the featureSource – Ian Turton Sep 16 '16 at 11:09
  • Are you referring to the OGR plugin? – Patrick D Sep 16 '16 at 11:44
  • 1
    No, there is a geoJSON datastore that acts just like all the other datastores – Ian Turton Sep 16 '16 at 12:02
  • Can you please provide a link to that documentation? – Patrick D Sep 16 '16 at 12:27
  • 1
1

I was unable to remove the lock when loading via a File object.

However, I am able to achieve the same level of functionality passing an InputStream into GeoTools, rather than passing in the File directly.

        FeatureJSON featureJSON = new FeatureJSON();
        InputStream geoJsonInputStream = getGeoJsonInputStream(dataResource);
        SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) featureJSON.readFeatureCollection(geoJsonInputStream);
        FeatureSource<SimpleFeatureType, SimpleFeature> geojsonFeatureSource = new CollectionFeatureSource(featureCollection);

By never giving access to the File to GeoTools, a lock is never held. Once my logic is done, I can close the InputStream that I have passed to GeoTools, and then safely delete the original file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.