1

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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Oct 10, 2018 at 7:31
1
  • 1
    What happens when you run the code that you have presented? In any event, I think your starting point should be to use arcpy.da.SearchCursor(). Commented Oct 10, 2018 at 8:10

1 Answer 1

2

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))
answered Oct 10, 2018 at 8:15
1
  • Thank you so much. it works but ".dbf" was necessary. Commented Oct 10, 2018 at 9:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.