I have a TIFF file and add it to GeoServer. A WMS request is as follows:
http://localhost:8080/geoserver/Qadir/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image/png8&TRANSPARENT=true&tiled=true&STYLES&LAYERS=Qadir:shomale_qharb3&exceptions=application/vnd.ogc.se_inimage&WIDTH=256&HEIGHT=256&SRS=EPSG:4326&BBOX=38.2763671875,46.494140625,38.29833984375,46.51611328125
The response:
Now I want to simulate this request with Geotools:
Double minX = 46.494140625;
Double maxX = 46.51611328125;
Double minY = 38.2763671875;
Double maxY = 38.29833984375;
GeoTiffReader reader = new GeoTiffReader("C:/layer.tif");
GridCoverage2D coverage = reader.read(null);
Envelope envelope = new Envelope(minX, maxX, minY, maxY);
ReferencedEnvelope bbox = new ReferencedEnvelope(envelope, DefaultGeographicCRS.WGS84);
Rectangle rectangle = new Rectangle();
rectangle.width = 256;
rectangle.height = 256;
AffineTransform worldToScreen = RendererUtilities.worldToScreenTransform(bbox, rectangle);
InterpolationNearest nearest = new InterpolationNearest();
RenderingHints hints = new RenderingHints(JAI.KEY_INTERPOLATION, nearest);
GridCoverageRenderer gcr = new GridCoverageRenderer(coverage.getCoordinateReferenceSystem2D(), bbox, rectangle, worldToScreen, hints);
gcr.setAdvancedProjectionHandlingEnabled(true);
gcr.setWrapEnabled(true);
RasterSymbolizerImpl symbolizer = new RasterSymbolizerImpl();
RenderedImage image = gcr.renderImage(reader, null, symbolizer, nearest, null, 256, 256);
File output = new File("C:/output.png");
ImageIO.write(image, "png", output);
The problem is where there is no data, The Geoserver response as above, but my code just returns the following:
How can I create a 256x256 tile with Geotools?
Note: For the left boundary, no data is on the left of the image.