I have a polyline shapefile. The shapefile has a field called "NAME" enter image description here
I would like to make a loop over each line (A,B,C,D,...).
This is the python code for only first line named "A"
profileLine = WORK_DIRECTORY + r'\CrossSections_onlyA.shp'
profileTargets = WORK_DIRECTORY + r'\DTM.tif'
OutTable = WORK_DIRECTORY + r'\OutTableA.dbf'
arcpy.StackProfile_3d(profileLine, profileTargets, OutTable)
I don't know how to do it over all of them with a loop. Also I would like to save the table with the name of each Line. for example: OutTableA.dbf, OutTableB.dbf, OutTableC.dbf and so on.
1 Answer 1
Use the da.SearchCursor to iterate over each line/row of the profilelines and create a feature layer for each using objectid. Then pass this feature layer to StackProfile. One output folder will also be created for each output table.
import arcpy,os
WORK_DIRECTORY = r'C:\somefolder'
arcpy.env.workspace = WORK_DIRECTORY
arcpy.env.overwriteOutput = True
profileLine = r'CrossSections_onlyA.shp'
profileTargets = r'DTM.tif'
with arcpy.da.SearchCursor(profileLine,['OID@','Name']) as cursor:
for oid,name in cursor:
sql = """{0} = {1}""".format(arcpy.Describe(profileLine).OIDFieldName, oid)
arcpy.MakeFeatureLayer_management(in_features=profileLine, out_layer='templine',
where_clause=sql)
OutTable = "OutTable{0}.dbf".format(name)
OutFolder = os.path.join(arcpy.env.workspace,name)
os.mkdir(OutFolder)
arcpy.StackProfile_3d('templine', profileTargets, os.path.join(OutFolder,OutTable))
-
Thank you so much. it works but ".dbf" was necessary.Anahita Kp– Anahita Kp2018年10月10日 09:09:50 +00:00Commented Oct 10, 2018 at 9:09
arcpy.da.SearchCursor()
.