1

I have hundreds of geotagged JPEG photos, with camera default naming, that are taken somewhere out there but always inside the borders of a shapefile. I need to rename them all with the shapefile id field (or other field).

Is there any reasonable way to do this? Other than manually go through every (shocking job). I have QGIS 3.24.2.

2
  • You have a shapefile representing the point locations of the images and another polygon file for the areas you want to rename? Does your image location shapefile include the paths and file names of the images? If so, can you post a picture of the attribute table?
    – GBG
    Jun 16, 2022 at 20:02
  • I saw your comments and I think that's not necessary to now python to do this but it's impossible to do it with out scripting. I post the code and you just only need to change one variable. I suggest you to follow step by step my answer and if you have problems or question you can search in the QGIS documentation or ask here.
    – Mayo
    Jun 17, 2022 at 21:21

1 Answer 1

2

Note: make a copy of your photos for security

  1. Generate points geometries for every geotagged photo with the Import geotagged photos tool.

  2. Add the attribute (that will be used to rename the photo) of the polygon to the point. There are meany ways to do this, but the easiest maybe is to use the Join attributes by location tool. It should look like this: Join attributes by location

    where New scratch layer is the polygon layer and in Fields to add you must set the field that contains the new name (in my screenshot is empty because i had only one field).

  3. Now you have a table with some properties of the photos (one of this the path) and the polygon to which it belongs. There's a problem, when more than one image has been taken in the same polygon they will have the same value in the field that will be used to rename them. So it will be necessary to create an Id that will be joined with the value of the field from the polygon. To do this, open the field calculator in the joined layer and calculate a new string field (please name it New_name for simplicity) and paste this expression:

    format('%1_%2',
        "Polygon",
        array_count(
            array_slice(
                array_agg(
                    "Polygon",
                    order_by:=$id
                    ),
                0,
                $id - 1
                ),
            "Polygon"
            ) - 1
        )
    

    Replace Polygon with the name of the field that was joined from the polygons layer. It should look something like this:

Calculating field

  1. Now we have a table containing the path to the photo and the new name. To rename this photos I will use a simple script in python. Paste the following python script in the QGIS python console and remember to set the joined layer name.
    import os
    project = QgsProject.instance()
    
    p_layer_name = '' # put inside the quotes the name of the joined layer
    p_layer = project.mapLayersByName(p_layer_name)[0]
    
    for point in p_layer.getFeatures():
      os.rename(point['photo'], os.path.join(point['directory'], point['New_name'] + '.jpg'))
    

Your Answer

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

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