From 74f5203ff35931d9e601974b8c271d1f0f64fe7d Mon Sep 17 00:00:00 2001 From: Tyler Hatch Date: Wed, 24 Mar 2021 22:09:38 -0500 Subject: [PATCH] Finalize Fragmenter Script --- Fragmenter.py | 26 +++++++++++++------- RCAT.pyt | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 10 deletions(-) diff --git a/Fragmenter.py b/Fragmenter.py index d0676c7..c1acdb6 100644 --- a/Fragmenter.py +++ b/Fragmenter.py @@ -18,22 +18,23 @@ def main(valley_bottom, streams, fragmenters, out_folder): arcpy.env.overwriteOutput = True arcpy.env.workspace = out_folder - # Convert Fragmenters to Python List - fragmenters = fragmenters.split(';') - # Copy Valley Bottom valley_copy = arcpy.CopyFeatures_management(valley_bottom, "Valley_Copy.shp") - # Union all fragmenters - fragmenters_union = arcpy.Union_analysis(fragmenters, "All_Fragmenters.shp") + # Merge all fragmenters + fragmenters_merge = arcpy.Merge_management(fragmenters, "All_Fragmenters.shp") # Convert everything to a cut polygon - mesh = arcpy.FeatureToPolygon_management([valley_copy, fragmenters_union], 'Polygon_Mesh.shp') + mesh = arcpy.FeatureToPolygon_management([valley_copy, fragmenters_merge], 'Polygon_Mesh.shp') # Clip that polygon with the orginal VB - new_valley = arcpy.Clip_analysis(mesh, valley_copy, "Prelim_Frag_Valley.shp") + new_valley = arcpy.Clip_analysis(mesh, valley_copy, "Prelim_Clip.shp") # Add Connected field + try: + arcpy.management.DeleteField(new_valley, 'Connected') + except: + pass arcpy.AddField_management(new_valley, 'Connected', 'SHORT') # Select all streams where network touches, set connected to 1 @@ -46,8 +47,15 @@ def main(valley_bottom, streams, fragmenters, out_folder): # Set remaining streams to zero arcpy.CalculateField_management(valley_layer, "Connected", "0", "PYTHON_9.3") - arcpy.SelectLayerByAttribute_management(valley_layer, "CLEAR SELECTION", "") - arcpy.CopyFeatures_management(valley_layer, "Valley_Final.shp") + arcpy.SelectLayerByAttribute_management(valley_layer, "CLEAR_SELECTION", "") + arcpy.CopyFeatures_management(valley_layer, "PrelimFragVB.shp") + + # Delete Temperary Shapefiles + for shape in [valley_copy, fragmenters_merge, mesh, new_valley]: + try: + arcpy.management.Delete(shape) + except: + pass if __name__ == '__main__': diff --git a/RCAT.pyt b/RCAT.pyt index cdb461d..670dd0f 100644 --- a/RCAT.pyt +++ b/RCAT.pyt @@ -12,6 +12,7 @@ import Promoter import segmentNetwork import LANDFIRE_RCAT_fields import Layer_Package_Generator +import Fragmenter class Toolbox(object): @@ -24,7 +25,7 @@ class Toolbox(object): # List of tool classes associated with this toolbox self.tools = [VBETBuilder, VBETtool, NHDNetworkBuildertool, RVDtool, RCATBuilder, RCAtool, BankfullChannelTool, ConfinementTool, Promotertool, SegmentNetworkTool, - LANDFIREfields, Layer_Package_Tool] + LANDFIREfields, Layer_Package_Tool, FragmentValleyBottom] class VBETBuilder(object): @@ -1320,3 +1321,67 @@ class Layer_Package_Tool(object): p[1].valueAsText, p[2].valueAsText) return + +class FragmentValleyBottom(object): + def __init__(self): + """Define the tool (tool name is the name of the class).""" + self.label = "Fragment Valley Bottom" + self.category = "Supporting Tools" + self.description = "Fragments the valley bottom by any input polyline features (roads, levees, etc.)" + self.canRunInBackground = False + + def getParameterInfo(self): + """Define parameter definitions""" + param0 = arcpy.Parameter( + displayName="Unfragmented Valley Bottom", + name="un_vb", + datatype="DEFeatureClass", + parameterType="Required", + direction="Input") + + param1 = arcpy.Parameter( + displayName="Stream Network", + name="streams", + datatype="DEFeatureClass", + parameterType="Required", + direction="Input") + + param2 = arcpy.Parameter( + displayName="Fragmenting Polylines", + name="fragmenters", + datatype="DEFeatureClass", + parameterType="Required", + direction="Input", + multiValue = True) + + param3 = arcpy.Parameter( + displayName="Out Folder", + name="out", + datatype="DEFolder", + parameterType="Required", + direction="Input") + + return [param0, param1, param2, param3] + + def isLicensed(self): + """Set whether tool is licensed to execute.""" + return True + + def updateParameters(self, parameters): + """Modify the values and properties of parameters before internal + validation is performed. This method is called whenever a parameter + has been changed.""" + return + + def updateMessages(self, parameters): + """Modify the messages created by internal validation for each tool + parameter. This method is called after internal validation.""" + return + + def execute(self, p, messages): + """The source code of the tool.""" + Fragmenter.main(p[0].valueAsText, + p[1].valueAsText, + p[2].valueAsText, + p[3].valueAsText) + return \ No newline at end of file