Activity 3-2

October 11, 2014

Task 1: Open Google Earth

Google Earth is a mapping application that can load in data files that look like XML (they are "KML") and use that data to add locations, marks, or other pieces of information to the map visualization. Now that we know how to output text files using a Python code, imagine creating a KML file as output. Marker locations or visual properties, like the size or color of pins in the map, could be chosen based on some logic or analysis in your program. Then, you could open the KML in Google Earth and use the visualization to evaluate a claim about the data.

First, you'll need to get Google Earth up and running.

  1. Open Google Earth on your computer. If Google Earth is not installed, you can download it for free.
  2. Try rotating, zooming, and adding a pin to the map.

Task 2: Edit Properties of the Map File

Download CIT.kml. In Google Earth, choose File > Open... from the menu bar and select the KML file you downloaded.

  1. Locate the pins on the map. Click on them to read the name and description.
  2. In Notepad (or TextEdit on a Mac), open CIT.kml. Can you locate the properties of marks with this file?
  3. Edit some properties, including the pin scale, color, name, and description. Add new marks if you like. When you're finished, save this file and open it again in Google Earth. How did the map change?

If we have time...

Task 3: Write a Python Function that Outputs a KML File

  1. Write a function in Python that takes in a name and longitude and latitude, and outputs a string beginning with <Placemark> and ending with </Placemark>, and filled with valid KML for this "element". If you begin with a template of what the placemark code looks like (as a string), you can concatenate your input arguments into that string in place of the predefined text.
  2. Now write a function that calls your Placemark generating code and concatenates the return values into a bigger string that represents the whole KML document. Look at CIT.kml to see what the other, non-Placemark text in the file should look like. Your function should return the big string representing the document.
  3. Finally, write a function that takes in a string representing a new output filename and a second string representing the file content. You will call this function to write a KML file from the string generated by the function in the previous step. Your function should open a new file for writing using the given filename, then write the given contents into that file. Don't forget to use the .kml extension when you call this function and provide a filename. Also, remember to close the file after you have written the contents to it.