1

I have a program where given an image I want to read the coordinates for each pixel. I have implemented the code if the given image is a TIFF image and it works fine. The code that gives me the object with the coordinates is as follow:

File f = new File("image.tiff");
GridCoverage2D image = new GeoTiffReader(f).read(new GeneralParameterValue[]{policy, gridsize, useJaiRead});
Rectangle2D bounds2D = image.getEnvelope2D().getBounds2D();
bounds2D.getCenterX();
GridGeometry2D geometry = image.getGridGeometry();

I want to try now to read the same image but in another format (JPEG and PNG). I have downloaded the images and each has a world file (pgw/jpw) and an aux file.

How can I obtain the same output I have on the TIFF file for these formats?

I am new to Java and geotools so I am a little lost.

2 Answers 2

0

You need to add the world image plugin and everything should be fine with no changes.

1
  • But my question is how to call it in Java.I think I have the necessaries dependencies in my program. I am just not sure how to read the images and the world file and put those together in order to obtain an object with the coordinates associated to each pixel.
    – Rute
    Commented Nov 10, 2022 at 9:53
0

I have found the answer! As Ian said I just needed the dependency for the world file. The code that reads the TIFF, JPEG or PNG is below:

try {
            ParameterValue<OverviewPolicy> policy = AbstractGridFormat.OVERVIEW_POLICY.createValue();
            policy.setValue(OverviewPolicy.IGNORE);
            ParameterValue<String> gridsize = AbstractGridFormat.SUGGESTED_TILE_SIZE.createValue();
            File file = new File(path);
            ParameterValue<Boolean> useJaiRead = AbstractGridFormat.USE_JAI_IMAGEREAD.createValue();
            useJaiRead.setValue(true);

            //starts reading here
            AbstractGridFormat format = GridFormatFinder.findFormat( file );
            GridCoverage2DReader reader = format.getReader( file );
            GridCoverage2D image = reader.read(new GeneralParameterValue[]{policy,gridsize,useJaiRead});
            GridGeometry2D geometry = image.getGridGeometry();
            BufferedImage img = ImageIO.read(file);
            WritableRaster raster =img.getRaster();
            return new Pair<>(geometry,raster);

        }catch(Exception e) {throw new Exception("Error reading the image: "+e);}
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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