Pages

Thursday, May 7, 2015

Geolocating Photos in QGIS

Photos taken with GPS-enabled cameras, including smartphones, store location information in
the header of the file, in a format called EXIF tags. These tags are largely based on the same
header tags used by the TIFF image standard. If you have a directory full of photos, you can easily sort them by date or by name. But if you want to sort them by location, only a map will do. In this recipe, we'll use these tags to create
locations on a map in QGIS for some photos and provide links to open them.

Getting ready

You need to make sure you have QGIS installed as well as the Python Imaging Library (PIL) for the Python distribution that QGIS uses. QGIS should already have PIL included. You can download some sample geotagged images here or you can use any photo taken outdoors with your smartphone. Unzip these photos and place them in a directory accessible to QGIS.

How to do it...

PIL can parse EXIF tags. We will gather the file names of the photos, parse the location information, convert it to decimal degrees, create the point vector layer, add the photo locations, and add a QGIS action link to the attributes. To do this, we need to perform the following steps:

In the QGIS Python Console, import the libraries that we'll need, including the Image module (PIL) and its ExifTags module for parsing image data and the glob module for doing wildcard file searches:

Finally, we'll add an action that allows you to click on it and open the photo. Actions are a simple yet powerful feature of QGIS that let you define the result of certain user actions like clicking on features:

How it works...

Using the included PIL EXIF parser, getting location information and adding it to a vector layer is relatively straight forward. This action is a default option for opening a URL. However, you can also use Python expressions as actions to perform a variety of tasks. The following screenshot shows an example of the data visualization and photo popup:

There's more...

You can download the complete code sample here. Another plugin called Photo2Shape is available that performs a similar function, but it requires you to install an external EXIF tag parser while this approach uses the parser in PIL included with QGIS.

All depends on how you give your path. Secondly I tried out the code it works but the coordinates land in Alaska for my images which I took in Fresno and Madera. Don't know what to do I am new to this. Please if there is any recommendations email me at nikhilsh.92@gmail.com