4

I am working with reading and writing shapefiles. I can read and write a shapefile but while reading it the warning message below is coming in the console, no error is coming.

reading

29 Aug, 2013 12:03:08 PM org.geotools.data.shapefile.ShpFiles logCurrentLockers SEVERE: The following locker still has a lock: read on file:/C:/Users/Administrator/Desktop/irumbaishp/irumbaishp/Irumbai_Lands.shp by org.geotools.data.shapefile.shp.ShapefileReader 29 Aug, 2013 12:03:08 PM org.geotools.data.shapefile.ShpFiles logCurrentLockers SEVERE: The following locker still has a lock: read on file:/C:/Users/Administrator/Desktop/irumbaishp/irumbaishp/Irumbai_Lands.shx by org.geotools.data.shapefile.shp.IndexFile 29 Aug, 2013 12:03:08 PM org.geotools.data.shapefile.ShpFiles logCurrentLockers SEVERE: The following locker still has a lock: read on file:/C:/Users/Administrator/Desktop/irumbaishp/irumbaishp/Irumbai_Lands.dbf by org.geotools.data.shapefile.dbf.DbaseFileReader Stopped reading 29 Aug, 2013 12:03:08 PM org.geotools.data.shapefile.ShpFiles logCurrentLockers SEVERE: The following locker still has a lock: read on file:/C:/Users/Administrator/Desktop/irumbaishp/irumbaishp/Irumbai_Lands.shp by org.geotools.data.shapefile.shp.ShapefileReader 29 Aug, 2013 12:03:08 PM org.geotools.data.shapefile.ShpFiles logCurrentLockers SEVERE: The following locker still has a lock: read on file:/C:/Users/Administrator/Desktop/irumbaishp/irumbaishp/Irumbai_Lands.shx by org.geotools.data.shapefile.shp.IndexFile 29 Aug, 2013 12:03:08 PM org.geotools.data.shapefile.ShpFiles logCurrentLockers SEVERE: The following locker still has a lock: read on file:/C:/Users/Administrator/Desktop/irumbaishp/irumbaishp/Irumbai_Lands.dbf by org.geotools.data.shapefile.dbf.DbaseFileReader Stopped reading Stopped reading 29 Aug, 2013 12:03:08 PM org.geotools.data.shapefile.ShpFiles logCurrentLockers SEVERE: The following locker still has a lock: read on file:/C:/Users/Administrator/Desktop/irumbaishp/irumbaishp/Irumbai_Lands.shp by org.geotools.data.shapefile.shp.ShapefileReader 29 Aug, 2013 12:03:08 PM org.geotools.data.shapefile.ShpFiles logCurrentLockers SEVERE: The following locker still has a lock: read on file:/C:/Users/Administrator/Desktop/irumbaishp/irumbaishp/Irumbai_Lands.shx by org.geotools.data.shapefile.shp.IndexFile 29 Aug, 2013 12:03:08 PM org.geotools.data.shapefile.ShpFiles logCurrentLockers SEVERE: The following locker still has a lock: read on file:/C:/Users/Administrator/Desktop/irumbaishp/irumbaishp/Irumbai_Lands.dbf by org.geotools.data.shapefile.dbf.DbaseFileReader Stopped reading Stopped

and the code to read the shapefile is:

import java.io.File;

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;

public class GeoOpen 
{

    private static SimpleFeatureIterator simpleFeatureIterator;
    private static String fileName = "C:\\Users\\Administrator\\Desktop\\irumbaishp\\irumbaishp\\Irumbai_Lands.shp";
    public static boolean openShapeFile() throws Exception
    {
        File dataFile = new File(fileName);
        dataFile.setReadOnly();
        ShapefileDataStore store = new ShapefileDataStore(dataFile.toURL());
        SimpleFeatureSource source = store.getFeatureSource();
        SimpleFeatureCollection featureCollection = source.getFeatures();
        simpleFeatureIterator = featureCollection.features();
        return true;
    }
    public static  boolean iterate()
    {
        while(simpleFeatureIterator.hasNext())
        {
            simpleFeatureIterator.next();
        }

    simpleFeatureIterator.close();
        return true;
    }
    public static void main(String[] args) {
        try {
            for(int i=0;i<5;i++){
            System.out.println("reading");
            GeoOpen.openShapeFile();
            GeoOpen.iterate();
            System.out.println("Stopped");
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  • Do you close the shape file when you finish reading it? – Ian Turton Aug 24 '13 at 13:27
  • thanks for your reply Iam using geotools 9.4 version but there is no reference for closing a shape file i tried hard on it i have n't got that. – Suri Aug 26 '13 at 6:10
  • Please let me know is there any problem in my code – Suri Aug 26 '13 at 6:19
  • you need to close featureCollections - docs.geotools.org/latest/userguide/library/main/collection.html – Ian Turton Aug 26 '13 at 17:28
  • 1
    I just ran into the same problem, wanted to add that you also need to close FeatureSource, FeatureCollection and FeatureIterator if you have them. – Fluffy May 25 '17 at 5:53
6

Disposing the datastore after use solves the problem.

import java.io.File;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.simple.SimpleFeature;

public class GeoOpen
{

    private static SimpleFeatureIterator simpleFeatureIterator;
    private static String fileName = "/home/sunish/Temp/shape.shp";
    static FileDataStore store ;

    public  static boolean openShapeFile() throws Exception
    {
        File dataFile = new File(fileName);
        dataFile.setReadOnly();
        store = FileDataStoreFinder.getDataStore(dataFile);
       // ShapefileDataStore store = new ShapefileDataStore(dataFile.toURL());

        SimpleFeatureSource source = store.getFeatureSource();
        SimpleFeatureCollection featureCollection = source.getFeatures();
        simpleFeatureIterator = featureCollection.features();


        return true;
    }
    public  static boolean iterate()
    {
        try{
        while(simpleFeatureIterator.hasNext())
        {

            SimpleFeature f = simpleFeatureIterator.next();

           System.out.println(""+f.getID());
        }
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        finally{
            simpleFeatureIterator.close();
            store.dispose();

        }


        return true;
    }
    public static void main(String[] args) {
        try {
            for(int i=0;i<5;i++){
            System.out.println("reading "+ i);

            GeoOpen.openShapeFile();
            GeoOpen.iterate();

            System.out.println("Stopped");
            //Thread.sleep(1000);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
|improve this answer|||||

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.