Created
June 23, 2018 16:30
-
-
Save OterLabb/00893d947629198ee087286416ed1f5b to your computer and use it in GitHub Desktop.
Add date and time to DJI seamlines generated from agisoft photoscan in arcgis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment