Monday 20 January 2020

python - How to save geodatabase attachments to disk with updated exif data


I have a folder of photos without xy information. Using a spreadsheet and lots of patience, I attached the photos to their corresponding GPS point as geodatabase attachments. I now need to produce a kmz file of these photos. Normally I would add the folder to Picasa and export to Google Earth, but since the original photos are not geotagged I cannot do that.


After looking at this question, it would appear that gaining access to the temporary files created when previewed in ArcMap would solve the problem. However, this would entail clicking on each point with the HTML popup on so that a temporary jpg is generated. Upon inspecting the properties of the temporary jpg, I saw that the coordinates of the associated point is not written into the EXIF data, so even if I were to save these temporary files, they still would not have location information.


Is there a way to geotag these attachments with the coordinates of their attached points, and save these files back to disk? I would like to do this in Python, but the part about getting the attached jpg files to generate without physically clicking on each point is a concern.


Edit I found a post on the ESRI forum which extracts the attachments to disk exactly as requested. I will add in the solution to extracting the coordinates and geotagging the photos, and post it here.



Answer



I put a solution together by relying heavily on code given in the ESRI forum post mentioned above, as well as code written using the pexif library.



import arcpy
import os
import pexif

fc = "C:\Mygdb.gdb\fc"
tbl = "C:\Mygdb.gdb\fc__ATTACH"
field_list = ["DATA", "ATT_NAME", "REL_OBJECTID"]
outFolder = "C:\test"

with arcpy.da.SearchCursor(tbl, field_list) as cursor:

for row in cursor:
binaryRep = row[0]
fileName = row[1]
#Query the current point and retrieve the coordinates
with arcpy.da.SearchCursor(fc, ["SHAPE@XY"],""" "OBJECTID" = {} """.format(row[2])) as curs:
for c in curs:
new_y = c[0][0]
new_x = c[0][1]
print c[0]
jpg = outFolder + os.sep + fileName

open(jpg, 'wb').write(binaryRep.tobytes()) #Save the attachment to disk
prim = pexif.JpegFile.fromFile(jpg)
prim.set_geo(new_x, new_y) #Set the correct gps coordinates
prim.writeFile(jpg)

This is not optimised at all, but I had to get something working today and this works perfectly. I suspect there is better way to get the current coordinates than using a second cursor inside the first one.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...