Displaying Geotagged Photos on Google Earth with Python
I decided to write a Python script that would create a Google Earth KML file with the list of geotagged photos that can be showed in the application. My reasoning for it was that a while ago Google decided to shut down Panoramio, the service they had acquired previously.
The service was of a photo-sharing type; much like Google Photos or Flickr today. The added ability that set the service apart from others was that the Google Earth application had a map layer comprised of photos stored on Panoramio. With Panoramio gone, the layer from Google Earth was removed as well.
I was not a user of it, but I liked looking at photos on Google Earth, thinking of places I wanted to visit. I wanted to share where I was traveling with my friends and family. Of course, provided that they used the same set of tools.
To achieve this the media needs to be enriched with geolocation information. The practice itself is called geotagging. JPEG image format, common with a number of digital cameras, can have metadata added to it (in contrast to PNG for example). The specific metadata is called EXIF and can contain information like the timestamp when the image was taken, camera type, location, etc. All the information necessary to create a KML file that Google Earth can read.
Now, before I get into the details about the Python script, I want to point out that EXIF information has a number of privacy and security issues. A number of TLA agencies are targeting EXIF information and using it for their purposes. For example, thanks to Edward Snowden's whistleblowing, we know that the NSA is targeting EXIF tags.
A number of examples associated with geotagging concerns are presented by Friedland and Sommer. A burglar can track your geotagged information and find out where you live, when you're not at home and steal things from your house.
Social networks make it incredibly easy to collect information on you and distribute it among unwanted people. One should be careful about the use of it, especially with kids nowadays gaining access and doing things they don't understand, enabling predators to exploit the information.
In your travels, you might visit wilderness and snap some photos that you would post online. One of more curious examples of abuse is poachers looking for rare animal photo geotags to figure out where to go. You want to be careful and think twice before you share information in this manner. Your camera or smartphone may be using GPS technology to attach the coordinates to an image.
Having said all that, you might think that EXIF information is not for you or that you want to strip that information before posting it online. It can be done, both stripping the EXIF information and preventing it from being saved in the first place.
I store my photos locally on an external disk and look at them offline. I don't frequent social networks so I don't have much trouble with geotagging information. I wanted to see where and when I was for personal use. That's why I created the Python script mentioned in the beginning of the article. You are free to use it too.
The necessary information I got was mostly from the article showing how to create placemarks for use in Google Earth. I wanted to automate the process and save KML instead. I could do that with Python.
The resulting script works only for Python 2.7.x for now because the PyKML dependency is not Python 3 ready so make sure you have the latest Python 2.7.x installed on your system. I install the dependencies in the virtual environment with virtualenvwrapper and can run it without trouble, but you might want to install them differently. Anyway, they are provided in the requirements.txt file in the repo. You can sift through the README.rst file there for more information. The usage of the script is pretty straightforward and it goes something like this::
$ python geotag-gallery.py --folder=/path/to/folder/with/geotagged/images
Keep in mind that it will parse the folder and the nested folders located in it. I use WSL on Windows to point it there or Linux locally so I can find my way around. It is supposed to work on both. The result of the script processing is the KML file in the specified folder with relative path to the images so you're not supposed to move it from there. You can open the KML with Google Earth and see where the images were taken according to the provided coordinates.
It also takes the timestamp so you can replay your steps and see when you were taking the photos.
I hope you find it useful or that it gives you an idea for a more comprehensive system you might want to work on. The script is on the BitBucket repository and under the GPL license so you can do whatever you want with it provided you share the code and give credit where it's due. Without further ado, you can download the Python script for creating KML from geotagged images in the mentioned repo. I put the download information deliberately at the end because I want people to know what they are doing and read through the article first.