3

I have a method where I open a geotiff file and print its dimensions

    public void parseAndSaveFile(String pathFileToParse) throws IOException, Exception {

        GridCoverage2DReader reader = null;
        GridCoverage2D image        = null;
        BufferedImage img           = null;

        try {

            File f = new File(pathFileToParse);

            ParameterValue<OverviewPolicy> policy = AbstractGridFormat.OVERVIEW_POLICY.createValue();
            policy.setValue(OverviewPolicy.IGNORE);

            // this will basically read 4 tiles worth of data at once from the disk...
            ParameterValue<String> gridsize = AbstractGridFormat.SUGGESTED_TILE_SIZE.createValue();
            //gridsize.setValue(512 * 4 + "," + 512);

            // Setting read type: use JAI ImageRead (true) or ImageReaders read methods (false)
            ParameterValue<Boolean> useJaiRead = AbstractGridFormat.USE_JAI_IMAGEREAD.createValue();
            useJaiRead.setValue(true);

            reader = new GeoTiffReader(f);      
            //reader.read(new GeneralParameterValue[] { policy, gridsize, useJaiRead });
            image = reader.read(new GeneralParameterValue[]{policy, gridsize, useJaiRead});
            //Rectangle2D bounds2D = image.getEnvelope2D().getBounds2D();
            // calculate zoom level for the image
            GridGeometry2D geometry = image.getGridGeometry();

            img = ImageIO.read(f);
            //WritableRaster raster = img.getRaster();
            //int numBands = raster.getNumBands();

            int width   = img.getWidth();
            int height  = img.getHeight();


            logger.info("Width: {}, Height: {}", width, height);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            reader.dispose();
            image.dispose(true);
        }
    }

In the calling method, I try to delete the file

Files.deleteIfExists(Paths.get(pathFileToParse));

but I get the error:

Unable to access the file. The file is used by another process.

Because? Did I correctly release the geotiff file?

1

That code works perfectly for me on Linux, so unless you are doing something odd in the rest of the program it is a windows issue.

I added this main method:

public static void main(String[] args) throws IOException, Exception {
    ParseAndDelete me = new ParseAndDelete();

    String pathFileToParse = "test3.tif";
    me.parseAndSaveFile(pathFileToParse);
    Files.deleteIfExists(Paths.get(pathFileToParse));
  }

Does that work for you?

  • I don't know to explain why it doesn't work for me – Alessio Frabotta Apr 16 '18 at 15:57
  • even the changes you proposed to me do not work – Alessio Frabotta Apr 19 '18 at 9:53
  • so a program of just that main and your method fails on windows? It may be a bug but afaik none of the GeoTools devs uses windows so it is unlikely to get fixed. – Ian Turton Apr 19 '18 at 10:07
  • the process that blocks the file is javaw.exe – Alessio Frabotta Apr 19 '18 at 13:19
  • I solved with these instructions: FileInputStream tiffFile = new FileInputStream(pathFileToParse); ImageInputStream iis = ImageIO.createImageInputStream(tiffFile); BufferedImage img = ImageIO.read(iis); Just close the FileInputStream and I no longer have problems deleting the file. If I try to close the ImageInputStream instead I have an error because it is already closed – Alessio Frabotta Apr 19 '18 at 14:09

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.