I want the output file names of the raster split tool (ArcGis;http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000009v000000) to carry a specific attribute (not FID) of the polygon I used to split the raster file. How can I achieve this?
Solved! Go to Solution.
Lara,
I'm guessing you are getting an error because of the search cursor. I think I forgot to finish the code for the search cursor the last time I gave it to you. It should be this:
arcpy.da.SearchCursor(Catchment,["OID@","Outlet_ID"],where_clause = '{0} = {1}'.format(arcpy.AddFieldDelimiters(CHM_Input, oidField),Raster_Number)).next()[1]
I'm not sure why you are getting a "\\Service" tagged to your output name. If it was working fine for you before then I would suggest going back the way you had it before.
On another note it might be helpful for us to see an arc catalog screen shot of your workspace folder with the raster files.
The only way as far as I can see would be to run the Split Raster tool with your polygon grid, then rename each one afterwards with your attribute id. You would have to make sure that these are unique of course. Are these map sheet names / ids or something.
The attribute is an ID which is different from the FID. Can the rename be done automatically? Renaming all of them manually would be laborious as the Split Raster tool created more than 100 files.
The attribute is an ID which is different from the FID. Can the rename be done automatically? Renaming all of them manually would be laborious as the Split Raster tool created more than 100 files.
Maybe this can be done using an overlap query?
You could use the iterate rasters with the rename tool in a model builder. I almost think it would be easier to make a tool that iterates features and then clips the rasters. You could use get field value tool to generate the naming output.
How can I rename the raster files based on a vector attribute with the iterate raster - rename approach?
The second approach worked (see script below) but using the Clip tool in a loop is substantially slower than the Split Raster tool:
cursor = arcpy.SearchCursor(Catchment)
for poly in cursor:
print(poly.getValue(Outlet_ID))
OutletID_poly = poly.getValue(Outlet_ID)
Output_poly = Tile_chm_catchment2 + "/Tile_chm_catchment" + str(OutletID_poly) + ".tif "
arcpy.Clip_management (CHM, "281898 706162 249670 761440", Output_poly, CHM, "", "NONE", "NO_MAINTAIN_EXTENT")
print "Tile_chm_catchment_" + str(OutletID_poly) + " is done"
The iterate raster is a model builder tool only. If you want to use python. You would need to use either List Rasters or walk to get a list of rasters you want to rename. You would need to run a Data Access Search Cursor on your polygon to match the FID row and get the field value that you want. Finally you would construct a string name using the field value and run rename for each raster.
Using the split tool your output rasters are named something like "Raster_0" correct? So you would need to split that name to get the FID number and then run a da search cursor on your polygon feature like this:
import arcpy
from arcpy import env
env.workspace = #workspace path for your raster folder
polygon = # path to your polygon
fieldName = # Name of the field you want the value from
for raster in arcpy.ListRasters():
id = int(raster.split("_")[-1])
oidField = arcpy.Describe(feature).oidFieldName
fieldValue = arcpy.da.SearchCursor(feature,["OID@",fieldName],where_clause = '{0} = {1}'.format(arcpy.AddFieldDelimiters(feature,oidField),id)).next()[1]
newName = "Raster_"+fieldValue
arcpy.Rename_management(raster,newName)
EDITED:I edited the code in hopes that it might clarify things a bit better. The raster.split("_") is assuming the raster names are something like 'Raster_0", but you may need to modify this to pull out the FID number.
I tried to do this but I am doing something wrong... I think i did not implement the arcpy.da.SearchCursor correctly:
# CHM = Raster Dataset # Catchment = Vector Dataset # Outlet_ID = Polygon_ID with I want to add to the Output of the Raster Split Tool workspace = Tile_chm_catchment walk = arcpy.da.Walk(workspace, datatype="RasterDataset", type="TIF") feature_classes = [] for dirpath, dirnames, filenames in walk: for filename in filenames: feature_classes.append(os.path.join(dirpath, filename)) #print feature_classes # Output = u'//.../Data_out/CHM/Tile_chm_catchment\\Tile_chm_catchment_99.TIF # unicode to string feature_class_string = [x.encode('UTF8') for x in feature_classes] # print feature_class_string # Output = '/.../Data_out/CHM/Tile_chm_catchment\\Tile_chm_catchment_99.TIF' for feature in feature_class_string: #print feature Raster_Number = re.findall('\d+', feature) # Raster_99 would lead to Raster_Number = 99 #print Raster_Number # Output = ['99'] feature_class_string_NoNumber = ''.join([i for i in feature if not i.isdigit()]) #print feature_class_string_NoNumber # Output = //.../Data_out/CHM/Tile_chm_catchment\Tile_chm_catchment_.TIF feature_class_string_NoNumber_NoTif = feature_class_string_NoNumber[:-4] #print feature_class_string_NoNumber_NoTif # Output = //.../Data_out/CHM/Tile_chm_catchment\Tile_chm_catchment_ CHM_Input = feature oidField = arcpy.Describe(CHM_Input).oidFieldName #print oid_Field fieldValue = arcpy.da.SearchCursor(Catchment,["OID@","Outlet_ID"],where_clause = '{0} = {1}'.format(arcpy.AddFieldDelimiters(CHM_Input, oidField),id)) # ID@ —The value of the ObjectID field #printfieldValue Rename_Output = str(feature_class_string_NoNumber_NoTif) + str(fieldValue) + ".tif" arcpy.rename_management(fieldname_string, Rename_Output)
It looks like you did not assign your 'id' for the search cursor. Try something like this:
Raster_Number = int(re.findall('\d+', feature)[0])
then your search cursor would be
fieldValue = arcpy.da.SearchCursor(Catchment,["OID@","Outlet_ID"],where_clause = '{0} = {1}'.format(arcpy.AddFieldDelimiters(CHM_Input, oidField),Raster_Number)).next()[1]
I think you can skip a step or two in creating your output string like removing the '.tif'. You could just set
feature_class_string_NoNumber_NoTif = ''.join([i for i in feature if not i.isdigit()]).split(".")[0]
Thank you so much for your help!!
See updated code:
# Catchment = Vector Dataset
# Outlet_ID = Polygon_ID with I want to add to the Output of the Raster Split Tool
workspace = Tile_chm_catchment
walk = arcpy.da.Walk(workspace, datatype="RasterDataset", type="TIF")
feature_classes = []
for dirpath, dirnames, filenames in walk:
for filename in filenames:
feature_classes.append(os.path.join(dirpath, filename))
print feature_classes # Output = u'///Data_out/CHM/Tile_chm_catchment\\Tile_chm_catchment_99.TIF
# unicode to string
feature_class_string = [x.encode('UTF8') for x in feature_classes]
# print feature_class_string # Output = '//.../Data_out/CHM/Tile_chm_catchment\\Tile_chm_catchment_99.TIF'
for feature in feature_class_string:
#print feature # Output = //..../Data_out/CHM/Tile_chm_catchment\Tile_chm_catchment_99.TIF
Raster_Number = int(re.findall('\d+', feature)[0]) # Raster_99 would lead to Raster_Number = 99
print Raster_Number # Output = ['99']
## feature_class_string_NoNumber_NoTif = ''.join([i for i in feature if not i.isdigit()]).split(".")
## print feature_class_string_NoNumber_NoTif[0]
feature_class_string_NoNumber = ''.join([i for i in feature if not i.isdigit()])
#print feature_class_string_NoNumber # Output = //.../Data_out/CHM/Tile_chm_catchment\Tile_chm_catchment_.TIF
feature_class_string_NoNumber_NoTif = feature_class_string_NoNumber[:-4]
#print feature_class_string_NoNumber_NoTif # Output = ///Data_out/CHM/Tile_chm_catchment\Tile_chm_catchment_
CHM_Input = feature
oidField = arcpy.Describe(Catchment).oidFieldName
#print oidField
fieldValue = arcpy.da.SearchCursor(Catchment,["OID@","Outlet_ID"],where_clause = '{0} = {1}'.format(arcpy.AddFieldDelimiters(CHM_Input, oidField),Raster_Number)) # ID@ —The value of the ObjectID field fieldValue = arcpy.da.SearchCursor(Catchment,["OID@","Outlet_ID"],where_clause = '{0} = {1}'.format(arcpy.AddFieldDelimiters(CHM_Input, oidField),id)) # ID@ —The value of the ObjectID field
#printfieldValue
Rename_Output = str(feature_class_string_NoNumber_NoTif) + str(fieldValue) + ".tif"
print Rename_Output
arcpy.Rename_management(feature, Rename_Output)