-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFragmenter.py
65 lines (50 loc) · 2.13 KB
/
Fragmenter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#-------------------------------------------------------------------------------
# Name: Fragmenter.py
# Purpose: Fragments the Valley Bottom by any inputs
# Author: Tyler Hatch
#
# Created: 3/21/21
#-------------------------------------------------------------------------------
# Import modules
import arcpy
import sys
import os
def main(valley_bottom, streams, fragmenters, out_folder):
# Set environment variables
arcpy.env.overwriteOutput = True
arcpy.env.workspace = out_folder
# Copy Valley Bottom
valley_copy = arcpy.CopyFeatures_management(valley_bottom, "Valley_Copy.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_merge], 'Polygon_Mesh.shp')
# Clip that polygon with the orginal VB
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
valley_layer = arcpy.MakeFeatureLayer_management(new_valley, "valley_layer")
arcpy.SelectLayerByLocation_management(valley_layer, 'intersect', streams)
arcpy.CalculateField_management(valley_layer, "Connected", "1", "PYTHON_9.3")
# Invert selection
arcpy.SelectLayerByAttribute_management(valley_layer, "SWITCH_SELECTION", "")
# 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, "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__':
main(sys.argv[1],
sys.argv[2],
sys.argv[3],
sys.argv[4])