Getting the map |
The getMap(String version, WMSMapParameter wmsMapParam) method of the WMS interface can get the map by passing in the WMS version number and WMS map query parameters. SuperMap iServer defines the WMSMapParameter object type to store WMS map service request parameters.
Add the getMap method in the WMSClientSample class, and call the getMap method in the Main function to output the picture binary stream to a PNG file. The code is as following.
// Get the map
public byte[] getMap() {
byte[] mapBytes = null;
// Construct the WMS map parameter object
WMSMapParameter wmsMapParameter = new WMSMapParameter();
// WMS service version number
String wmsVersion = "1.1.1";
// Assign values
wmsMapParameter.mapName = "China";
wmsMapParameter.entireBounds = new Rectangle2D(-180, -90, 180, 90);
wmsMapParameter.bounds = new Rectangle2D(-180, -90, 180, 90);
wmsMapParameter.layers = new String[1];
wmsMapParameter.layers[0] = "China_Boundary_ln@China#1";
wmsMapParameter.srs = "EPSG:4326";
wmsMapParameter.width = 800;
wmsMapParameter.height = 400;
wmsMapParameter.styles = new String[]{""};
try {
mapBytes = wmsClient.getMap(wmsVersion, wmsMapParameter);
} catch (Exception e) {
e.printStackTrace();
}
return mapBytes;
}
public static void main(String[] args) {
WMSClientSample wmsClientSample = new WMSClientSample();
// Output the service title and the map title
WMSCapabilities capabilities = wmsClientSample.getCapabilities();
String wmsTitle = capabilities.serviceDescription.title;
String mapTitle = capabilities.mapTitle;
System.out.println("WMS Title is " + wmsTitle);
System.out.println("MapTitle is " + mapTitle);
// Output the map
byte[] bytes = wmsClientSample.getMap();
String fileName = "d:/map.png";
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(fileName);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Please open d:/map.png to see the result");
}
Before running the WMSClientSample, you need to ensure that the service http://localhost:8090/iserver/services/map-world/wms111 has already been started.
After compiling the WMSClientSample project, right-click the WMSClientSample project, and then select Run as > Java Application to run the Java application. The console will prompt you to view map.png, and the map.png file will be generated in the WMSClient Sample project directory.