Update End of Term Assignment authored by Zorenböhmer Christina's avatar Zorenböhmer Christina
![header3](uploads/cd89be07b4371d5e6c68dbbe383a1843/header3.png)
**Contributors:** Edah Sahinovic, Katharina Wöhs, Christina Zorenböhmer
**.java Files:** All .java files can be found in our ["Codes" folder](https://git.sbg.ac.at/s1080384/EoT/-/tree/main/Java%20Codes).
## Background and Aim
......@@ -151,6 +153,115 @@ First, we created a [non-executable class "wms_GetMap"](https://git.sbg.ac.at/s1
> 5. asks the user if they would like to see information on the service (and either shows it or moves on)
> 6. writes the image in .png to the local directory
<details><summary> See the Code for the "main "wms_GetMap" class here</summary>
```
package Test2;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import javax.imageio.ImageIO;
import org.geotools.ows.ServiceException;
import org.geotools.ows.wms.WMSCapabilities;
import org.geotools.ows.wms.WebMapServer;
import org.geotools.ows.wms.request.GetMapRequest;
import org.geotools.ows.wms.response.GetMapResponse;
public class wms_GetMap {
static void getMap() {
String wms_url = execute_programme.wms_url;
String directory = execute_programme.directory;
String wms_png = execute_programme.wms_png;
System.out.println("Accessing WMS and downloading .png of Boston to local directory at: " + wms_png + "... ");
// Constructing a WebMapServer object:
URL url = null;
try {
System.out.println("Testing Heigit's wms service URL... ");
url = new URL(wms_url);
System.out.println("URL vaid.");
} catch (MalformedURLException e) {
System.out.println("Error related to URL.");
}
WebMapServer wms = null;
try {
wms = new WebMapServer(url);
} catch (IOException e) {
System.out.println("There was an error communicating with the server, e.g. the server is down.");
} catch (ServiceException e) {
System.out.println("The server returned a ServiceException (unusual in this case).");
}
System.out.println("No errors in communicating with the server.");
// You can retrieve a WMSCapabilities directly from your WebMapService. This capabilities bean is split into three sections: Service, Request, and Layer.
WMSCapabilities capabilities = wms.getCapabilities();
// Get information on the service
String serverName = capabilities.getService().getName();
String serverTitle = capabilities.getService().getTitle();
// Get Map Request: ask the client to create a GetMapRequest object. MAKE SURE TO IMPORT THE CORRECT GETMAPREQUEST: import org.geotools.ows.wms.request.GetMapRequest;
GetMapRequest request = wms.createGetMapRequest();
// Configure the request object:
request.addLayer("osm_auto:all", "default");
request.setFormat("image/png");
request.setDimensions("1000", "1000");
request.setTransparent(true);
request.setSRS("EPSG:4326");
request.setVersion("1.1.1");
request.setBBox("-71.13,42.32,-71.03,42.42");
Scanner askServiceInfo = new Scanner(System.in); //warning coming from non-closed scanner
String answer;
System.out.println("Would you like additional information on the WMS service while the programme runs? Type Y for yes or anything else for no and hit enter.");
answer = askServiceInfo.next();
// askServiceInfo.close();
if(answer.contains("Y") || answer.contains("y") || answer.contains("yes") || answer.contains("Yes")) {
System.out.println("The WMS capabilities are being retreived from a server called: " + serverName +"\nAnd the server's title is :" + serverTitle +".\n\n" +
"The requested image will be stored as a .png with dimensions of 1000 x 1000 pixels." +
"The coordinate reference system is set to ESPG:4326");
} else {
System.out.println("Alright.");
}
System.out.println("\n\nRequested URL: " + request.getFinalURL());
try {
GetMapResponse response = (GetMapResponse) wms.issueRequest(request);
BufferedImage image = ImageIO.read(response.getInputStream());
ImageIO.write(image, "png", new File(directory + "\\boston.png"));
} catch (IOException e) {
System.out.println("There was an error writing the image.");
} catch (ServiceException e) {
e.printStackTrace();
}
System.out.println("Image was successfully saved at: " + directory + "\\boston.png");
}
}
```
</details>
When this method is called from the main class, the user will be kept updated and will see the following on the console:
<details><summary>See Console Output</summary>
......@@ -188,6 +299,85 @@ Here, we created a [non-executable class "wms_ImageToKML"](https://git.sbg.ac.at
> - finds the local boston.png image
> - writes a kml file (containing the image) to a local wms_kml_structure.kml
<details><summary>See Code for the "wms_ImageToKML" class here</summary>
```
package Test2;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class wms_ImageToKML {
static void wmsTOkml() {
String wms_kml = execute_programme.wms_kml;
String wms_png = execute_programme.wms_png;
System.out.println("Turning local .png of Boston into kml format at: " + wms_kml + "... ");
// find image of boston on local PC:
File wms_image = new File(wms_png);
if(wms_image.exists()) {
System.out.println("Image found..");
}else {
System.out.println("Image not found..");
}
// write kml:
String [] kmlArray = {"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
,"<kml xmlns=\"http://www.opengis.net/kml/2.2\">\r\n"
," <Folder> "
," <name>Ground Overlays</name>\r\n"
," <description>Examples of ground overlays</description>\r\n"
," <GroundOverlay>\r\n"
," <name>Boston WMS image</name>\r\n"
," <description>Output WMS image</description>\r\n"
," <color>B3ffffff</color>\r\n"
," <Icon>\r\n"
," <href>" + wms_png + "</href>\r\n"
," </Icon>\r\n"
," <LatLonBox>\r\n"
, "<north>42.42</north>\r\n"
, "<south>42.32</south>\r\n"
, "<east>-71.03</east>\r\n"
, "<west>-71.13</west>\r\n"
, "<rotation>0</rotation>\r\n"
, "</LatLonBox>\r\n"
, "</GroundOverlay>\r\n"
, "</Folder>"
, "</kml>\r\n"
};
//-71.13,42.32,-71.03,42.4 -71.13,42.32,-71.03,42.42
try {
File kml_file = new File(wms_kml);
kml_file.createNewFile();
FileWriter writer = new FileWriter(kml_file);
for(String i : kmlArray){
writer.write(i);
}
writer.close();
//System.out.println(kml_file.toString());
}catch(IOException e) {
System.out.println("Something went wrong in: TryCatch method");
}
System.out.println("Image converted to kml and stored at: " + wms_kml);
}
}
```
</details>
When this method is called from the main class, the user will be kept updated and will see the following on the console:
<details><summary>See Console Output</summary>
......
......