PYTHON Using a 'for' loop (ex. for i in list:) to create a series of shapefiles to use to create TINs

1596
5
Jump to solution
11-10-2020 11:07 AM
petegillard
New Contributor III

i'm trying to select a set of depth values (points - ex. 2902, 2904, 2906, etc.)from surveyed points.  save those to their own point layer, convert those to a TIN, then calc. Volumes. My approach was to make a list of the values, use those values as VARIABLES to create a series of shapefiles (shp2906, shp2908, etc.) then create TINs from those. Hard coding the numbers it  works, but wanted to pass the depth values (using "select" (ex. grid_code le 2906)) name the shapefile  and the tin (ex. tin2904, tin 2906, etc) was hoping to use the iteration  as a variable, but it's not liking any of my attempts...... thanks in advance, for any ideas....

0 Kudos
1 Solution

Accepted Solutions
MarcGraham2
Occasional Contributor III

Hi there,

I think this may help. It looks through a list of depths and exports a feature class from the source data that is queried for each depth, and then also a tin. It's just a quick hack so there's no error handling etc. Also I'm not sure how to properly embed code, hopefully this works.

Cheers,

Marc

# IMPORT ARCPY
import arcpy
arcpy.OverwriteOutput = True

# SCRIPT VARIABLES
sr = arcpy.SpatialReference(2193)

# MAKE SURE INPUT AND OUTPUT DATA/GDB EXIST
out_fgdb = r"C:\Temp\out.gdb"
out_folder = r"C:\Temp"
surv_pts = r"C:\Temp\s_pts.shp"
surv_lyr = arcpy.MakeFeatureLayer_management(surv_pts)
depth_list = [1,2,3,4,5]

# VALUES FOR TIN CREATION
depth_field = "depth"
tin_type = "Mass_Points <None>"

# ITERATE THROUGH DEPTHS, OUTPUT FEATURE CLASSES AND TINS
for d in depth_list:
 fc_name = "points_{}".format(d)
 fc_path = "{}\{}".format(out_fgdb,fc_name)
 query = "depth = {}".format(d)
 tin_path = r"{}\tin_{}".format(out_folder,d)

 # CREATE A FEATURECLASS IN THE OUTPUT FGDB
 arcpy.FeatureClassToFeatureClass_conversion(surv_lyr, out_fgdb, fc_name, query)
 tin_def = "{} {} {}".format(fc_path, depth_field, tin_type)

 # CREATE A TIN IN THE OUTPUT FOLDER
 arcpy.CreateTin_3d(tin_path,
 sr,
 tin_def,
 constrained_delaunay="DELAUNAY")

View solution in original post

5 Replies
JoshuaBixby
MVP Esteemed Contributor

It is always best to share some of your code, often times it is easier to interpret code than someone's explanation of their code.

petegillard
New Contributor III

thanks, thought of that...…...everywhere in the code below "2922" is the depth. I was trying to pull from a  "list" of depths to use as a variable (we're looking at volume of water at 2ft. depth intervals ). This code worked, but was hoping to automate it.

# Import arcpy module
import arcpy
from arcpy import env

arcpy.CheckOutExtension("3D")


# Local variables:
UPDpts = r"Y:\GIS\Pete\resPTS_test\UPDFk_pts_Clip.shp"
le2922 = r"Y:\GIS\Pete\resPTS_test\le2922.shp"
le2922tin = r"Y:\GIS\Pete\resPTS_test\le2922tin"
#proj = r"Coordinate Systems/Projected Coordinate Systems/State Plane/State Plane NAD 1983 (2011) Montana FIPS 2500 (Intl Feet).prj"
proj = arcpy.SpatialReference ("NAD 1983 (2011) StatePlane Montana FIPS 2500 (Intl Feet).prj")


# Process: Select
arcpy.Select_analysis(UPDpts, le2922, "grid_code <= 2922")

# Process: Create Tin
arcpy.CreateTin_3d("le2922tin", proj,"le2922.shp grid_code masspoints" " ")
arcpy.DelineateTinDataArea_3d("le2922tin", 11, "PERIMETER_ONLY")


# Process: Calc. volume
arcpy.SurfaceVolume_3d("le2922tin", "tin2922_vol.txt",
                      "BELOW", 2922, 1)

 

0 Kudos
MarcGraham2
Occasional Contributor III

Hi there,

I think this may help. It looks through a list of depths and exports a feature class from the source data that is queried for each depth, and then also a tin. It's just a quick hack so there's no error handling etc. Also I'm not sure how to properly embed code, hopefully this works.

Cheers,

Marc

# IMPORT ARCPY
import arcpy
arcpy.OverwriteOutput = True

# SCRIPT VARIABLES
sr = arcpy.SpatialReference(2193)

# MAKE SURE INPUT AND OUTPUT DATA/GDB EXIST
out_fgdb = r"C:\Temp\out.gdb"
out_folder = r"C:\Temp"
surv_pts = r"C:\Temp\s_pts.shp"
surv_lyr = arcpy.MakeFeatureLayer_management(surv_pts)
depth_list = [1,2,3,4,5]

# VALUES FOR TIN CREATION
depth_field = "depth"
tin_type = "Mass_Points <None>"

# ITERATE THROUGH DEPTHS, OUTPUT FEATURE CLASSES AND TINS
for d in depth_list:
 fc_name = "points_{}".format(d)
 fc_path = "{}\{}".format(out_fgdb,fc_name)
 query = "depth = {}".format(d)
 tin_path = r"{}\tin_{}".format(out_folder,d)

 # CREATE A FEATURECLASS IN THE OUTPUT FGDB
 arcpy.FeatureClassToFeatureClass_conversion(surv_lyr, out_fgdb, fc_name, query)
 tin_def = "{} {} {}".format(fc_path, depth_field, tin_type)

 # CREATE A TIN IN THE OUTPUT FOLDER
 arcpy.CreateTin_3d(tin_path,
 sr,
 tin_def,
 constrained_delaunay="DELAUNAY")
petegillard
New Contributor III

thanks, i'll see if I can decipher this....appreciate the help

0 Kudos
Michael_Boyce
New Contributor

G'day Peter and Marc,

 

Marc, that script looked like it should work pretty well.  I haven't run it but what you have got down should go pretty well.

The only change that I would make would be to build the "Depth_List" dynamically by iterating through the original tables "Depth" field and adding them to the Depth List and converting it into a "set", which will ensure that there is only one value for each depth.
so...
adding in  the following should help.

depthList = [row[0] for row in arcpy.da.SearchCursor(originalTable, depthField)]
depthset = set(depthList)

or to make it even smaller:

depthSet = set([row[0] for row in arcpy.da.SearchCursor(originalTable, depthField)])

 

Hope you find the answer,

 

Michael

0 Kudos