1

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:

wrong map

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:

preview

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();
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
leomessi
  • 69
  • 6

1 Answers1

2

I solve it. The wrong is here :

ReferencedEnvelope mapArea = new ReferencedEnvelope(x1, y1, x2, y2, crs);

I use this statement to select the display area of map.But the order of parameter is wrong.

BBOX from request has 4 parameter,the order is [longitude,latitude,longitude,latitude] ,and in the CRS WGS 84 the longitude is define to y,latitude is define to x. But the ReferencedEnvelope Constructor is :

ReferencedEnvelope(double x1, double x2, double y1, double y2, CoordinateReferenceSystem crs)

More detail of ReferencedEnvelope:Document of ReferencedEnvelope

So I change the code:

double x1, y1, x2, y2;

        y1 = Double.parseDouble(BBOX[0]);
        x1 = Double.parseDouble(BBOX[1]);
        y2 = Double.parseDouble(BBOX[2]);
        x2 = Double.parseDouble(BBOX[3]);


        ReferencedEnvelope mapArea = new ReferencedEnvelope(x1, x2, y1, y2, crs);

Then it is ok.

Different coordinate systems have different coordinate sequences,we can view it by printing its text message,such as

GEOGCS["WGS84(DD)", 
  DATUM["WGS84", 
    SPHEROID["WGS84", 6378137.0, 298.257223563]], 
  PRIMEM["Greenwich", 0.0], 
  UNIT["degree", 0.017453292519943295], 
  AXIS["Geodetic longitude", EAST], 
  AXIS["Geodetic latitude", NORTH]]

define the longitude first, and

GEOGCS["WGS 84", 
  DATUM["World Geodetic System 1984", 
    SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]], 
    AUTHORITY["EPSG","6326"]], 
  PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], 
  UNIT["degree", 0.017453292519943295], 
  AXIS["Geodetic latitude", NORTH], 
  AXIS["Geodetic longitude", EAST], 
  AUTHORITY["EPSG","4326"]]

define the latitude first

leomessi
  • 69
  • 6