Guest User

Untitled

a guest
Jun 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import arcpy
  2. import exifread
  3. from datetime import datetime
  4.  
  5. # Variables
  6. fc = arcpy.GetParameterAsText(0)
  7. folderLocation = arcpy.GetParameterAsText(1) + '/'
  8.  
  9. # Check if feature class is shapefile or in a geodatabase
  10. dataset = arcpy.Describe(fc).extension
  11.  
  12. # Create row/cursors
  13. rows = arcpy.SearchCursor(fc)
  14. cursor = arcpy.da.InsertCursor(fc, "ImageDate")
  15.  
  16. # Create one or two fields, depending on fc is shapefile or in geodatabase
  17. if dataset == 'shp':
  18. arcpy.AddMessage("Feature class is a shapfile, creating both date and time fields")
  19. if len(arcpy.ListFields(fc, "ImageDate")) > 0 and len(arcpy.ListFields(fc, "ImageTime")) > 0:
  20. arcpy.AddMessage('Feature class already has date/time fields')
  21. else:
  22. arcpy.AddField_management(fc, "ImageDate", "DATE")
  23. arcpy.AddField_management(fc, "ImageTime", "STRING")
  24. arcpy.AddMessage('Creating ImageDate/ImageTime field')
  25.  
  26. else:
  27. arcpy.AddMessage("Feature class is in a geodatabase, will create only one datetime field")
  28. if len(arcpy.ListFields(fc, "ImageDate")) > 0:
  29. arcpy.AddMessage('Feature class already has datetime field')
  30. else:
  31. arcpy.AddField_management(fc, "ImageDate", "DATE")
  32. arcpy.AddMessage('Creating ImageDate field')
  33.  
  34. # Update fields with date and time
  35. if dataset == 'shp':
  36. fields = ["NAME", "ImageDate", "ImageTime"]
  37. with arcpy.da.UpdateCursor(fc, fields) as cursor:
  38. for row in cursor:
  39. try:
  40. imageName = folderLocation + row[0] + ".JPG"
  41. im = open(imageName, 'rb')
  42. tags = exifread.process_file(im, details=False)
  43. time = tags["Image DateTime"]
  44. val = time.values
  45.  
  46. date = val.split(" ")[0]
  47. year = date.split(":")[0]
  48. month = date.split(":")[1]
  49. day = date.split(":")[2]
  50. time = val.split(" ")[1]
  51.  
  52. date = day + "/" + month + "/" + year
  53. arcpy.AddMessage('Updating ' + row[0] + ' with date/time: ' + date + time)
  54. row[1] = date
  55. row[2] = time
  56. cursor.updateRow(row)
  57. except:
  58. print('error')
  59.  
  60. else:
  61. fields = ["NAME", "ImageDate"]
  62. with arcpy.da.UpdateCursor(fc, fields) as cursor:
  63. for row in cursor:
  64. imageName = folderLocation + row[0] + ".JPG"
  65. im = open(imageName, 'rb')
  66. tags = exifread.process_file(im, details=False)
  67. time = tags["Image DateTime"]
  68. val = time.values
  69.  
  70. date = val.split(" ")[0]
  71. year = date.split(":")[0]
  72. month = date.split(":")[1]
  73. day = date.split(":")[2]
  74. time = val.split(" ")[1]
  75.  
  76. date = day + "/" + month + "/" + year + " " + time
  77. arcpy.AddMessage('Updating ' + row[0] + ' with date/time: ' + date)
  78. row[1] = date
  79. cursor.updateRow(row)
Add Comment
Please, Sign In to add comment