4

When working with shapefiles, i get this "warning"

    WARNING: There is code leaving shapefile readers unclosed, this might result in file system locks not being cleared. File is: chranene_uzemi_cr
Dub 08, 2018 11:23:13 ODP. org.geotools.data.shapefile.ShapefileFeatureReader finalize
WARNING: There is code leaving shapefile readers unclosed, this might result in file system locks not being cleared. File is: chranene_uzemi_cr
Dub 08, 2018 11:23:13 ODP. org.geotools.data.shapefile.ShapefileFeatureReader finalize
WARNING: There is code leaving shapefile readers unclosed, this might result in file system locks not being cleared. File is: chranene_uzemi_cr
Dub 08, 2018 11:23:13 ODP. org.geotools.data.shapefile.ShapefileFeatureReader finalize
WARNING: There is code leaving shapefile readers unclosed, this might result in file system locks not being cleared. File is: chranene_uzemi_cr
Dub 08, 2018 11:23:13 ODP. org.geotools.data.shapefile.ShapefileFeatureReader finalize
WARNING: There is code leaving shapefile readers unclosed, this might result in file system locks not being cleared. File is: chranene_uzemi_cr
Dub 08, 2018 11:23:13 ODP. org.geotools.data.shapefile.ShapefileFeatureReader finalize
WARNING: There is code leaving shapefile readers unclosed, this might result in file system locks not being cleared. File is: chranene_uzemi_cr
Dub 08, 2018 11:23:13 ODP. org.geotools.data.shapefile.files.ShpFiles logCurrentLockers
SEVERE: The following locker still has a lock: read on file:/F:/Škola/test_data/lesy_cr.shp by org.geotools.data.shapefile.shp.ShapefileReader
Dub 08, 2018 11:23:13 ODP. org.geotools.data.shapefile.files.ShpFiles logCurrentLockers
SEVERE: The following locker still has a lock: read on file:/F:/Škola/test_data/lesy_cr.shx by org.geotools.data.shapefile.shp.IndexFile
Dub 08, 2018 11:23:13 ODP. org.geotools.data.shapefile.files.ShpFiles logCurrentLockers
SEVERE: The following locker still has a lock: read on file:/F:/Škola/test_data/lesy_cr.dbf by org.geotools.data.shapefile.dbf.DbaseFileReader
Dub 08, 2018 11:23:13 ODP. org.geotools.data.shapefile.files.ShpFiles logCurrentLockers

But the project is build after that and i get results what i need. Not sure where is the problem. I am disposing datastore at the end but it dosnt seems to work.

String overlayPolygons(String shapeFileURL) throws IOException {

String areas = "Object : Area of overlay";

ShapefileDataStore sfds;
sfds = new ShapefileDataStore(new URL("file:///F:\\Škola\\test_data\\lesy_cr.shp"));

SimpleFeatureSource fs;
fs = sfds.getFeatureSource("lesy_cr");

ShapefileDataStore sfds2;
sfds2 = new ShapefileDataStore(new URL("file:///F:\\Škola\\test_data\\chranene_uzemi_cr.shp"));

SimpleFeatureSource fs2;
fs2 = sfds2.getFeatureSource("chranene_uzemi_cr");

SimpleFeatureIterator sfi = fs.getFeatures().features();
double sum = 0;
while (sfi.hasNext()) {
    SimpleFeature sf = sfi.next();
    MultiPolygon mp2 = (MultiPolygon) sf.getDefaultGeometry();
    Polygon p2 = (Polygon) mp2.getGeometryN(0);

    SimpleFeatureIterator sfi2 = fs2.getFeatures().features();
    while (sfi2.hasNext()) {
        SimpleFeature sf2 = sfi2.next();
        MultiPolygon mp3 = (MultiPolygon) sf2.getDefaultGeometry();
        Polygon p3 = (Polygon) mp3.getGeometryN(0);
        Geometry p4 = p2.intersection(p3);
        if (p4.getArea() != 0) {
            sum += p4.getArea();
            areas = areas + "\n" + p4.getArea()+ " : " + p2.getArea() + " : " + p3.getArea();
        }

    }
}

sfds.dispose();
sfds2.dispose();


return "Objects found: " + areas + "\nTotal sum: " + sum;
}

1 Answer 1

7

You must close the iterators sfi and sfi2 after their respective loops.

SimpleFeatureIterator sfi = fs.getFeatures().features();
double sum = 0;

while (sfi.hasNext()) {
    SimpleFeature sf = sfi.next();
    MultiPolygon mp2 = (MultiPolygon) sf.getDefaultGeometry();
    Polygon p2 = (Polygon) mp2.getGeometryN(0);

    SimpleFeatureIterator sfi2 = fs2.getFeatures().features();
    while (sfi2.hasNext()) {
        SimpleFeature sf2 = sfi2.next();
        MultiPolygon mp3 = (MultiPolygon) sf2.getDefaultGeometry();
        Polygon p3 = (Polygon) mp3.getGeometryN(0);
        Geometry p4 = p2.intersection(p3);
        if (p4.getArea() != 0) {
            sum += p4.getArea();
            areas = areas + "\n" + p4.getArea()+ " : " + p2.getArea() + " : " + p3.getArea();
        }
    }
    sfi2.close();
}
sfi.close();

sfds.dispose();
sfds2.dispose();
1
  • 1
    the easiest way is to use try(SimpleFeatureIterator sfi2 = fs2.getFeatures().features()){.......} to auto close the feature iterators
    – Ian Turton
    Apr 9, 2018 at 16:43

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.