I'm using geotools to develop a wms service. I create a map layer and add it into MapContent
. Then I develop a GetMap service. I display the map on the browser successfully, but the map location is always wrong. Like this:
And the request parameters are:
SERVICE: WMS
VERSION: 1.1.0
REQUEST: GetMap
FORMAT: image/png
TRANSPARENT: true
LAYERS: layer
SRS: EPSG:4326
STYLES:
WIDTH: 997
HEIGHT: 499
BBOX: 63.2958984375,3.1494140625,150.9228515625,47.0068359375
The response preview is like:
I want to display the points at the right location.
Here is my code to deal with GetMap request:
double x1, y1, x2, y2;
String[] BBOX = request.getParameter("BBOX").split(",");
x1 = Double.parseDouble(BBOX[0]);
y1 = Double.parseDouble(BBOX[1]);
x2 = Double.parseDouble(BBOX[2]);
y2 = Double.parseDouble(BBOX[3]);
int width = Integer.parseInt(request.getParameter("WIDTH")),
height = Integer.parseInt(request.getParameter("HEIGHT"));
CoordinateReferenceSystem crs = CRS.decode(request.getParameter("SRS"));
ReferencedEnvelope mapArea = new ReferencedEnvelope(x1, y1, x2, y2, crs);//I think some wrong here,but I try to modify many times,it is not work
response.setContentType("image/png");
ServletOutputStream out = response.getOutputStream();
WMSRequest wmsRequest = new WMSRequest();
wmsRequest.getGetMap();
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = bi.getGraphics();
Rectangle rectangle = new Rectangle(0, 0, width, height);
MapViewport mv = new MapViewport();
mv.setBounds(mapArea);
mv.setCoordinateReferenceSystem(crs);
mv.setScreenArea(rectangle);
mapContent.setViewport(mv);
//AffineTransform atf = geometryManage.getRenderingTransform(width, height, mv.getBounds());
StreamingRenderer sr = new StreamingRenderer();
sr.setMapContent(mapContent);
sr.paint((Graphics2D) graphics, rectangle, mapArea);
ImageIO.write(bi, "png", out);
out.close();