Skip to main content
  1. Projects/

Software Development: Dynamic Google Earth Launcher

<span>423 words</span><span class="px-2 text-primary-500">&middot;</span><span title="Reading time">2 mins</span><span class="px-2 text-primary-500">&middot;</span> <span class="mb-[2px]"> <a href="#/Projects/Software%20Development:%20Dynamic%20Google%20Earth%20Launcher/index.md" class="text-lg hover:text-primary-500" rel="noopener noreferrer" target="_blank" title="Edit content" ><span class="inline-block align-text-bottom"> <span class="relative block icon"> <svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512"><path fill="currentColor" d="M441 58.9L453.1 71c9.4 9.4 9.4 24.6 0 33.9L424 134.1 377.9 88 407 58.9c9.4-9.4 24.6-9.4 33.9 0zM209.8 256.2L344 121.9 390.1 168 255.8 302.2c-2.9 2.9-6.5 5-10.4 6.1l-58.5 16.7 16.7-58.5c1.1-3.9 3.2-7.5 6.1-10.4zM373.1 25L175.8 222.2c-8.7 8.7-15 19.4-18.3 31.1l-28.6 100c-2.4 8.4-.1 17.4 6.1 23.6s15.2 8.5 23.6 6.1l100-28.6c11.8-3.4 22.5-9.7 31.1-18.3L487 138.9c28.1-28.1 28.1-73.7 0-101.8L474.9 25C446.8-3.1 401.2-3.1 373.1 25zM88 64C39.4 64 0 103.4 0 152V424c0 48.6 39.4 88 88 88H360c48.6 0 88-39.4 88-88V312c0-13.3-10.7-24-24-24s-24 10.7-24 24V424c0 22.1-17.9 40-40 40H88c-22.1 0-40-17.9-40-40V152c0-22.1 17.9-40 40-40H200c13.3 0 24-10.7 24-24s-10.7-24-24-24H88z"/></svg> </span> </span></a > </span>
Author
Adian Dawuda
Master’s student in Applied Geoinformatics focused on earth observation and spatial data science

The following showcases a Java program that generates and opens a Google Earth KML file with a set of placemarks, each corresponding to a geographic location specified by a pair of latitude and longitude coordinates.

The program begins by importing the necessary Java classes for file writing and input/output exception handling. Next, the program declares a string array called coordinates that contains five elements, each representing a latitude and longitude pair in the format x,y. These coordinates will be used to create the placemarks in the KML file.

The addPlacemark method iterates through the elements in the coordinates array and creates a placemark element for each one. It generates a name and description for each placemark, and specifies the coordinates using the latitude and longitude pairs in the coordinates array. The placemarks are then combined into a single string and returned by the method.

The main method of the program starts by defining the beginning and ending tags of a KML file, which are stored in the variables placeMarkBeginning and placemarkEnd respectively. Then, it calls the addPlacemark method and stores the result in the fileContent variable. The fileContent variable is then written to a KML file using the FileWriter class, and the file is saved to the file system.

Finally, the program launches Google Earth and opens the generated KML file by calling the Runtime.getRuntime().exec method and passing it the path to the Google Earth executable and the path to the KML file as arguments.

import java.io.FileWriter;
import java.io.IOException;

public class DynamicGoogleEarthLauncher {

    static String[] coordinates = {"13.03978,47.82281", "13.06024,47.78861", "13.03364,47.80569", "13.05354,47.80628", "13.04747,47.81912"};

    public static void main(String[] args) throws IOException {
        String placeMarkBeginning = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" +
                "<Document>";
        String placemarkEnd = "</Document>\n" +
                "</kml>";
        //combine all kml elements
        String fileContent = placeMarkBeginning + addPlacemark() + placemarkEnd;
        //write to the kml file
        FileWriter writer = new FileWriter("/home/adian/Work/PLUS/SWE/unit3dynamic.kml");
        writer.write(fileContent);
        writer.flush();
        writer.close();
        String googleEarthPath = "google-earth-pro";
        String kml1 = "/home/adian/Work/PLUS/SWE/unit3dynamic.kml";
        //open the kml file with google earth
        try {
            Runtime.getRuntime().exec(googleEarthPath + " " + kml1);
            System.out.println("launching Google Earth...");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //add coordinates from the string[] into the variable "placemark"
    public static String addPlacemark() {
        String placemark = "";
        for (int i = 0; i < coordinates.length; i++) {
            int point = i+1;
            placemark += "<Placemark>\n" +
                    "    <name>Point " + point + "</name>\n" +
                    "    <description>placemark</description>\n" +
                    "    <Point>\n" +
                    "\t    <coordinates>" + coordinates[i] + "</coordinates>\n" +
                    "    </Point>\n" +
                    "  </Placemark>\n";
        }
        return placemark;
    }
}