I'm still a little unclear on what you are trying to do but if all you need is to simply reproject the native bounds to WGS84 (as GeoServer does) then the following code will work for you:
String wkt = "PROJCS[\"unnamed\"," +
" GEOGCS[\"WGS 84\"," +
" DATUM[\"WGS_1984\"," +
" SPHEROID[\"WGS 84\",6378137,298.257223563," +
" AUTHORITY[\"EPSG\",\"7030\"]]," +
" AUTHORITY[\"EPSG\",\"6326\"]]," +
" PRIMEM[\"Greenwich\",0]," +
" UNIT[\"degree\",0.0174532925199433]," +
" AUTHORITY[\"EPSG\",\"4326\"]]," +
" PROJECTION[\"Mercator_1SP\"]," +
" PARAMETER[\"latitude_of_origin\",-15]," +
" PARAMETER[\"central_meridian\",0]," +
" PARAMETER[\"scale_factor\",1]," +
" PARAMETER[\"false_easting\",0]," +
" PARAMETER[\"false_northing\",0]," +
" UNIT[\"metre\",1," +
" AUTHORITY[\"EPSG\",\"9001\"]]]";
CoordinateReferenceSystem crs= CRS.parseWKT(wkt);
ReferencedEnvelope bbox = new ReferencedEnvelope(-6345422.834,-4399127.001,
-2688622.834, 965672.999, crs);
System.out.println(bbox);
boolean lenient = false;
MathTransform transform = CRS.findMathTransform(crs, DefaultGeographicCRS.WGS84, lenient );
Envelope res = JTS.transform(bbox, transform);
System.out.println(res);
Which gives me a slightly different answer (I suspect a copy and paste error somewhere as I've compared it to your solution above and the answers are the same for my inputs):
ReferencedEnvelope[-6345422.834303701 : -4399127.000735723, -2688622.8343037013 : 965672.9992642764]
Env[-58.999479314465475 : -40.90290107674037, -24.38570281128449 : 9.001542880512202]
Update
There are two issues with your code (in the question):
Hints hint = new Hints();
hint.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs );
hint.put(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
The first line tells GeoTools to use the WGS84 crs you defined as the default CRS this overrides the one the coverage reads in so it looked like your tif was in WGS84 (and hence reprojecting it to WGS84 did nothing). The second line tells GeoTools to consider projections as longitude, latitude pairs (rather than asking the crs), I think it is unnecessary here. But it just adjusts the order of the resulting bbox axis either without it:
ReferencedEnvelope[-38.00123605669721 : 9.001542880512217, -58.999479314465475 : -24.998704016910057]
or with it on:
ReferencedEnvelope[-58.999479314465475 : -24.998704016910057, -38.00123605669721 : 9.001542880512217]
As you see the answers now match with the GeoTiff providing the CRS.
GeoTiffFormat format = new GeoTiffFormat();
Hints hint = new Hints();
// hint.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, DefaultGeographicCRS.WGS84 );
hint.put(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
File geoTiffFile = new File("1geotiff.tif");
GeoTiffReader tiffReader = format.getReader( geoTiffFile, hint );
GridCoverage2D coverage = tiffReader.read(null);
CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem();
System.out.println(crs);
ReferencedEnvelope bbox = new ReferencedEnvelope(crs);
bbox.setBounds(coverage.getEnvelope2D());
boolean lenient = false;
CoordinateReferenceSystem target = DefaultGeographicCRS.WGS84;
MathTransform transform = CRS.findMathTransform(crs, target, lenient );
ReferencedEnvelope res = new ReferencedEnvelope(JTS.transform(bbox, transform),target);
System.out.println(bbox);
System.out.println(res);
Best Answer
GeoServer creates tiles of rendered WMS maps using GeoWebCache where as the GeoTools tutorial you found is splitting up an existing GeoTiff. These are different processes with as you note different outputs.
However, there is no difficulty in using GeoTools to split a GeoTiff into png or jpg images using the tutorial code. You simply need to change the output format when writing the tiles back to disk.
At the end of the
tile
method are the output lines:format
is the same object as was used to read in the coverage (a GeoTiff) but you can get other formats, for example aWorldImageWriter
will output georeferenced png or jpg tiles:Or you can simply use an
ImageWriter
In both these cases you are writing out the raw data values with no styling applied, this may or may not be what you want. If you need to apply styling then you should look at the image tutorial for an example of how to style a raster and draw it to screen, you would need to provide an
Image
to draw on instead of a screen and pass thatGraphics2D
object to the renderer.