Select to view content in your preferred language

Create Time Field in Raster Mosaic

1091
3
Jump to solution
12-20-2013 01:14 AM
JonathanMulder
Occasional Contributor
Greeting,

I have a Raster Mosaic in a geodatabase of daily groundwater surfaces.  I've come across a web page that explains how to insert a time field in the Raster Mosaic in ArcMap.  But, I'd really like to do the process automatically in Arcpy.

Does anyone know the Arcpy steps for doing this?

Thanks!

Jon Mulder
California Department of Water Resources
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Alum
    #set the mosaic dataset   ws_mosaic = r'C:\MyFolder\MyGDB.gdb\MosaicDS'       # add the date field   arcpy.AddField_management (ws_mosaic, "_calcDate", "DATE")   print "Date field added"        # calcuate the date field   ras = str(ws_mosaic)   fld = "_calcDate"   cursor = arcpy.UpdateCursor(ras, '', '', '', 'Name')     # in this case, the actual date to start from was not important   startDate = datetime.datetime(1901,1,1)   step = datetime.timedelta(days=1)   d = startDate       # now populate the date field   for row in cursor:        retVal = datetime.datetime.strptime(str(d), '%Y-%m-%d %H:%M:%S')       row.setValue(fld, retVal)       cursor.updateRow(row)          d += step 

View solution in original post

0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Jonathan,

Essentially all you will need to do is add a field to your mosaic dataset and calcualte the desired date.  Is your mosaic dataset updated on a daily basis with new raster imagery?  Can you describe was steps you would like to automate?
0 Kudos
JamesCrandall
MVP Alum
    #set the mosaic dataset   ws_mosaic = r'C:\MyFolder\MyGDB.gdb\MosaicDS'       # add the date field   arcpy.AddField_management (ws_mosaic, "_calcDate", "DATE")   print "Date field added"        # calcuate the date field   ras = str(ws_mosaic)   fld = "_calcDate"   cursor = arcpy.UpdateCursor(ras, '', '', '', 'Name')     # in this case, the actual date to start from was not important   startDate = datetime.datetime(1901,1,1)   step = datetime.timedelta(days=1)   d = startDate       # now populate the date field   for row in cursor:        retVal = datetime.datetime.strptime(str(d), '%Y-%m-%d %H:%M:%S')       row.setValue(fld, retVal)       cursor.updateRow(row)          d += step 
0 Kudos
JonathanMulder
Occasional Contributor
Thanks very much for showing me how to do this!

I have a Raster Mosaic Dataset within a geodatabase of groundwater surfaces.  They are named as follows:
GWSurface_20130701
GWSurface_20130702
...
GWSurface_20130731

So, I modified my code to add a field name (called "SurfaceDate") to my Raster Mosaic Dataset table, then looped through the table and updated the SurfaceDate field with the last 8 characters of the surface name.  Works great!

Jon Mulder
California Department of Water Resources

##Create Raster Mosaic Dataset to contain Surfaces.
print "Creating Surfaces Raster Dataset."
FeatureDatasetName = "Surfaces"
arcpy.CreateMosaicDataset_management(OutDatasetPath,FeatureDatasetName,Spatial_Reference)
##Add an 8 character Text field to hold Date as YYYYMMDD.
RasterDatasetLocation = os.path.join(OutFolderPath,GeoDatabaseName,"Surfaces")
arcpy.AddField_management(RasterDatasetLocation, "SurfaceDate","Text","","",8)

DateStart = date(2013,7,1)
DateEnd = date(2013,7,31)
for dt in rrule(DAILY, dtstart=DateStart, until=DateEnd):

    ##Do lots of processing in this loop.

##Populate Raster Mosaic Table with Dates.
arcpy.env.workspace = os.path.join(OutFolderPath,GeoDatabaseName,"Surfaces")
ras = str(os.path.join(OutFolderPath,GeoDatabaseName,"Surfaces"))
flds = ["Name","SurfaceDate"]
cursor = arcpy.da.UpdateCursor(ras,flds)
for row in cursor:
    RasterName = row[0]
    DateString = RasterName[-8:]
    row[1] = DateString
    cursor.updateRow(row)
print "Contours Completed."
0 Kudos