I can extract the locations of my photos in QGIS using Photo2Shape or Geotag and import photos, but I want to be able to delete the photos that do not have location information with them (they are backed up elsewhere).

At the moment I have to look in the attribute table which photos get imported with a latitude and longitude value of 0. Then I have to find these files manually and delete them. This is very time consuming and it would be great if it was possible to do this automatically somehow.

I guess a another possible workaround would be for the photos that have locations to be copied to a sub folder.

I just wondered if anyone knew of a tool (GIS or otherwise) that could achieve this before I set about writing a script to do it for me.

2 Answers 2


Try something like this in the Python console:

import os
layer.setSubsetString('"latitude" = 0 AND "longitude" = 0')
for feature in layer.getFeatures():
    path = feature['path']
    except IOError:
        print "Can't delete {}".format(path)

We use layer.setSubsetString() just to reduce the features to only 0,0 ones. You could also loop each feature and check for 0,0 but this is simple enough.

enter image description here

  • This worked really well, thanks Nathan. Just need to add a line to delete the record from the table as well. Maybe it would be good to have this as an option in one of the photo import plugins?
    – James S
    Commented Nov 13, 2013 at 13:06
  • When using with the Photo2Shape plugin, the path field is 'filepath', not 'path' just incase anyone has problems with that.
    – James S
    Commented Nov 13, 2013 at 13:08
  • Feel free to update my code on here. I was just going from memory.
    – Nathan W
    Commented Nov 13, 2013 at 13:20

this script might help you in making a python script to do the task, instead of writing you can read :

def set_gps_location(file_name, lat, lng):
    """Adds GPS position as EXIF metadata

    Keyword arguments:
    file_name -- image file
    lat -- latitude (as float)
    lng -- longitude (as float)

    lat_deg = to_deg(lat, ["S", "N"])
    lng_deg = to_deg(lng, ["W", "E"])

    print lat_deg
    print lng_deg

    # convert decimal coordinates into degrees, munutes and seconds
    exiv_lat = (pyexiv2.Rational(lat_deg[0]*60+lat_deg[1],60),pyexiv2.Rational(lat_deg[2]*100,6000), pyexiv2.Rational(0, 1))
    exiv_lng = (pyexiv2.Rational(lng_deg[0]*60+lng_deg[1],60),pyexiv2.Rational(lng_deg[2]*100,6000), pyexiv2.Rational(0, 1))

    exiv_image = pyexiv2.ImageMetadata(file_name)
    exif_keys = exiv_image.exif_keys

    exiv_image["Exif.GPSInfo.GPSLatitude"] = exiv_lat
    exiv_image["Exif.GPSInfo.GPSLatitudeRef"] = lat_deg[3]
    exiv_image["Exif.GPSInfo.GPSLongitude"] = exiv_lng
    exiv_image["Exif.GPSInfo.GPSLongitudeRef"] = lng_deg[3]
    exiv_image["Exif.Image.GPSTag"] = 654
    exiv_image["Exif.GPSInfo.GPSMapDatum"] = "WGS-84"
    exiv_image["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.