Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import arcpy
- import exifread
- from datetime import datetime
- # Variables
- fc = arcpy.GetParameterAsText(0)
- folderLocation = arcpy.GetParameterAsText(1) + '/'
- # Check if feature class is shapefile or in a geodatabase
- dataset = arcpy.Describe(fc).extension
- # Create row/cursors
- rows = arcpy.SearchCursor(fc)
- cursor = arcpy.da.InsertCursor(fc, "ImageDate")
- # Create one or two fields, depending on fc is shapefile or in geodatabase
- if dataset == 'shp':
- arcpy.AddMessage("Feature class is a shapfile, creating both date and time fields")
- if len(arcpy.ListFields(fc, "ImageDate")) > 0 and len(arcpy.ListFields(fc, "ImageTime")) > 0:
- arcpy.AddMessage('Feature class already has date/time fields')
- else:
- arcpy.AddField_management(fc, "ImageDate", "DATE")
- arcpy.AddField_management(fc, "ImageTime", "STRING")
- arcpy.AddMessage('Creating ImageDate/ImageTime field')
- else:
- arcpy.AddMessage("Feature class is in a geodatabase, will create only one datetime field")
- if len(arcpy.ListFields(fc, "ImageDate")) > 0:
- arcpy.AddMessage('Feature class already has datetime field')
- else:
- arcpy.AddField_management(fc, "ImageDate", "DATE")
- arcpy.AddMessage('Creating ImageDate field')
- # Update fields with date and time
- if dataset == 'shp':
- fields = ["NAME", "ImageDate", "ImageTime"]
- with arcpy.da.UpdateCursor(fc, fields) as cursor:
- for row in cursor:
- try:
- imageName = folderLocation + row[0] + ".JPG"
- im = open(imageName, 'rb')
- tags = exifread.process_file(im, details=False)
- time = tags["Image DateTime"]
- val = time.values
- date = val.split(" ")[0]
- year = date.split(":")[0]
- month = date.split(":")[1]
- day = date.split(":")[2]
- time = val.split(" ")[1]
- date = day + "/" + month + "/" + year
- arcpy.AddMessage('Updating ' + row[0] + ' with date/time: ' + date + time)
- row[1] = date
- row[2] = time
- cursor.updateRow(row)
- except:
- print('error')
- else:
- fields = ["NAME", "ImageDate"]
- with arcpy.da.UpdateCursor(fc, fields) as cursor:
- for row in cursor:
- imageName = folderLocation + row[0] + ".JPG"
- im = open(imageName, 'rb')
- tags = exifread.process_file(im, details=False)
- time = tags["Image DateTime"]
- val = time.values
- date = val.split(" ")[0]
- year = date.split(":")[0]
- month = date.split(":")[1]
- day = date.split(":")[2]
- time = val.split(" ")[1]
- date = day + "/" + month + "/" + year + " " + time
- arcpy.AddMessage('Updating ' + row[0] + ' with date/time: ' + date)
- row[1] = date
- cursor.updateRow(row)
Add Comment
Please, Sign In to add comment