It seems odd that you are dealing with java.awt.Rectangles - I'm not sure where you are getting them from as there is no mention in the code. I modified your code to this:
public static void main(String[] args) throws IOException { File input = new File("/home/ian/code/geotools-cookbook/code/modules/spike/test.png"); AbstractGridFormat format = GridFormatFinder.findFormat(input); Hints hints = null; if (format instanceof GeoTiffFormat) { hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE); } AbstractGridCoverage2DReader reader = format.getReader(input, hints); GridCoverage2D grid = reader.read(null); System.out.println( grid.getEnvelope2D()); System.out.println( grid.getEnvelope()); reader.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GeoTiffWriter writer = new GeoTiffWriter(baos, null); GeoTiffWriteParams wp = new GeoTiffWriteParams(); //wp.setSourceRegion(grid.getGridGeometry().getGridRange2D().getBounds()); final ParameterValueGroup params = new GeoTiffFormat().getWriteParameters(); params.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()).setValue(wp); writer.write(grid, null/*params.values().toArray(new GeneralParameterValue[1])*/); writer.dispose(); byte[] bytes = baos.toByteArray(); //Deserialization: ByteArrayInputStream bais = new ByteArrayInputStream(bytes); GeoTiffReader reader2 = null; hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE); reader2 = new GeoTiffReader(bais, hints); GridCoverage2D coverage = reader2.read(null); bais.close(); System.out.println( coverage.getEnvelope2D()); System.out.println( coverage.getEnvelope()); } which gives me Envelope2D[(833207.8127810268, 9785025.96070077), (833286.9776217749, 9785090.075567605)] GeneralEnvelope[(833207.8127810268, 9785025.96070077), (833286.9776217749, 9785090.075567605)] Envelope2D[(833207.8127810268, 9785025.96070077), (833286.9776217749, 9785090.075567605)] GeneralEnvelope[(833207.8127810268, 9785025.96070077), (833286.9776217749, 9785090.075567605)] Where the bounds of the output and input grid match exactly. Ian On Thu, 7 Jan 2021 at 16:28, Lorenzo Di Giacomo <loridi...@gmail.com> wrote: > Both X and Y are set to 0, in every GridCoverage2D deserialized. > I think that maybe some READ_GRIDCOVERAGE2D parameters are needed (?) > Original: java.awt.Rectangle[x=209,y=333,width=334,height=148] > After serialization / deserialization: > java.awt.Rectangle[x=0,y=0,width=334,height=148] > > Serialization: > > ByteArrayOutputStream baos = new ByteArrayOutputStream(); > GeoTiffWriter writer = new GeoTiffWriter(baos, null); > GeoTiffWriteParams wp = new GeoTiffWriteParams(); > wp.setSourceRegion(grid.getGridGeometry().getGridRange2D().getBounds()); > final ParameterValueGroup params = new > GeoTiffFormat().getWriteParameters(); > > params.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()).setValue(wp); > writer.write(grid, params.values().toArray(new GeneralParameterValue[1])); > writer.dispose(); > byte[] bytes = baos.toByteArray(); > > Deserialization: > > ByteArrayInputStream bais = new ByteArrayInputStream(bytes); > GeoTiffReader reader = null; > Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, > Boolean.TRUE); > reader = new GeoTiffReader(bais, hints); > GridCoverage2D grid = reader.read(null); > bais.close(); > return grid; > > Il giorno gio 7 gen 2021 alle ore 15:06 Ian Turton > <ijtur...@gmail.com> ha scritto: > > > > How different are we talking about here? You'll probably need to share > some code and example data to allow anyone to look into this. > > > > > > Ian > > > > On Thu, 7 Jan 2021 at 11:58, Lorenzo Di Giacomo <loridi...@gmail.com> > wrote: > >> > >> Hello, i'm try to serialize a GridCoverage2D using GeoTiffWriter > >> writing in a ByteArrayOutputStream and deserializing using > >> GeoTiffReader from the ByteArrayInputStream... The problem is that the > >> Bounds (Rectangle object) of the deserialized GridCoverage2D has > >> different X,Y then the original (the width and height are the same) > >> ... I also tried to use GeoTiffWriteParams setSourceRegion method but > >> nothing. Do you know if that's a problem or some other params must be > >> set in order to have the same bounds from the 2 grid? Thanks again!! > >> > >> > >> _______________________________________________ > >> GeoTools-Devel mailing list > >> GeoTools-Devel@lists.sourceforge.net > >> https://lists.sourceforge.net/lists/listinfo/geotools-devel > > > > > > > > -- > > Ian Turton > -- Ian Turton
_______________________________________________ GeoTools-Devel mailing list GeoTools-Devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/geotools-devel