Help refining modelbuilder model to Python code

المشرف العام

Administrator
طاقم الإدارة
I have been working on creating a model in modelbuilder that will 1) take a specified DEM and split it into elevation masks at specified intervals (this step was done using a Python script tool), 2) iterate through the elevation masks and perform region groups, 3) convert the region group rasters to polygons, 4) add and calculate attributes to the polygon layers, and then 5) convert them back to rasters. The final step 6) sums an attribute field from the raster layers over a constant raster for a final single raster output with the summed values. The first and final steps (1 and 6) are python script tools that I run separately. Steps 2 through 5 are run in Modelbuilder.

I'm trying to streamline this process so that a user will not have to run two different scripts and then a model. My goal is to specify a single input DEM to the model, and have a single raster output with my summed values, while the model or script runs, generates my elevation masks, and removes the intermediate data as it goes. I am not sure how to do this in Modelbuilder, and think Python would probably do a better job integrating all of these steps.

As this will be a fine-tuning process, I think that what I asking for at this point is: how can I take my intermediate steps that are done in Modelbuilder and implement them in Python to run and then delete the intermediate data as it runs?

I know how to export a model to Python code-I'm just unfamiliar with how to set up a scratch workspace for my model to run through and clear out at each step using the exported code. I've included the Model and Python export for these steps below.



import arcpy

Check out any necessary licenses

arcpy.CheckOutExtension("spatial")

Load required toolboxes

arcpy.ImportToolbox("Model Functions")

Script arguments

test_gdb__2_ = arcpy.GetParameterAsText(0)if test_gdb__2_ == '#' or not test_gdb__2_: test_gdb__2_ = "U:\FGS-old\Projects\DEM_Sinkhole_Project\Data\Ctds_Amanda\Cumulative CI project\Arcpy_Code_Test\test.gdb" # provide a default value if unspecified

v_Name__RG = arcpy.GetParameterAsText(1)if v_Name__RG == '#' or not v_Name__RG: v_Name__RG = "C:\AdditiveCIR\Cumulative CI project\RegionGroup.gdb\%Name%_RG" # provide a default value if unspecified

v_Name__RtoP = arcpy.GetParameterAsText(2)if v_Name__RtoP == '#' or not v_Name__RtoP: v_Name__RtoP = "C:\AdditiveCIR\Cumulative CI project\RasterToPoly.gdb\%Name%_RtoP" # provide a default value if unspecified

v_Name__FtoR = arcpy.GetParameterAsText(3)if v_Name__FtoR == '#' or not v_Name__FtoR: v_Name__FtoR = "C:\AdditiveCIR\Cumulative CI project\FeatureToRaster.gdb\%Name%_FtoR" # provide a default value if unspecified

v_Name__RtoP__2_ = arcpy.GetParameterAsText(4)if v_Name__RtoP__2_ == '#' or not v_Name__RtoP__2_: v_Name__RtoP__2_ = "C:\AdditiveCIR\Cumulative CI project\RasterToPoly.gdb\%Name%_RtoP" # provide a default value if unspecified

v_Name__RtoP__3_ = arcpy.GetParameterAsText(5)if v_Name__RtoP__3_ == '#' or not v_Name__RtoP__3_: v_Name__RtoP__3_ = "C:\AdditiveCIR\Cumulative CI project\RasterToPoly.gdb\%Name%_RtoP" # provide a default value if unspecified

v_Name__RtoP__5_ = arcpy.GetParameterAsText(6)if v_Name__RtoP__5_ == '#' or not v_Name__RtoP__5_: v_Name__RtoP__5_ = "C:\AdditiveCIR\Cumulative CI project\RasterToPoly.gdb\%Name%_RtoP" # provide a default value if unspecified

Local variables:

test_gdb = "U:\FGS-old\Projects\DEM_Sinkhole_Project\Data\Ctds_Amanda\Cumulative CI project\Arcpy_Code_Test\test.gdb"test_gdb__3_ = "C:\AdditiveCIR\Cumulative CI project\Arcpy_Code_Test\test.gdb"test_gdb__4_ = "C:\AdditiveCIR\Cumulative CI project\Arcpy_Code_Test\test.gdb"Input_false_raster_or_constant_value__4_ = "1"test_gdb__5_ = "C:\AdditiveCIR\Cumulative CI project\Arcpy_Code_Test\test.gdb"v_Name__setnull = "C:\AdditiveCIR\Cumulative CI project\RegionGroup.gdb\%Name%_setnull"lessthan25 = "C:\AdditiveCIR\Cumulative CI project\Arcpy_Code_Test\test.gdb\lessthan200"

Process: Iterate Rasters

arcpy.IterateRasters_mb(test_gdb__5_, "", "", "RECURSIVE")

Process: Set Null

arcpy.gp.SetNull_sa(lessthan25, Input_false_raster_or_constant_value__4_, v_Name__setnull, "Value = 0")

Process: Region Group

arcpy.gp.RegionGroup_sa(v_Name__setnull, v_Name__RG, "EIGHT", "WITHIN", "ADD_LINK", "0")

Process: Raster to Polygon

arcpy.RasterToPolygon_conversion(v_Name__RG, v_Name__RtoP, "SIMPLIFY", "Value")

Process: Add Geometry Attributes

arcpy.AddGeometryAttributes_management(v_Name__RtoP, "PERIMETER_LENGTH", "FEET_US", "", "")

Process: Add Field

arcpy.AddField_management(v_Name__RtoP__2_, "cir_index", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

Process: Calculate Field

arcpy.CalculateField_management(v_Name__RtoP__3_, "cir_index", "(4*3.14159265359* [Shape_Area]) / ([PERIMETER] * [PERIMETER])", "VB", "")

Process: Feature to Raster

arcpy.FeatureToRaster_conversion(v_Name__RtoP__5_, "cir_index", v_Name__FtoR, "C:\AdditiveCIR\Cumulative CI project\Arcpy_Code_Test\alachua_test")



أكثر...
 
أعلى