-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoadPLine.py
More file actions
110 lines (75 loc) · 3.77 KB
/
RoadPLine.py
File metadata and controls
110 lines (75 loc) · 3.77 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#### PARAMETERS____________________________________________________________________________________________________________________
#Imports
import arcpy as a
import math
#Environment Parameters
a.env.workspace = 'in_memory'
a.env.overwriteOutput = True
#Tool Feature Inputs
basefeat = a.GetParameterAsText(0)
contours = a.GetParameterAsText(1)
outputfeat = a.GetParameterAsText(2)
spatial = a.Describe(basefeat).spatialReference
#Initial Copy to Temp Data
a.CopyFeatures_management(basefeat, 'base_temp')
#Add helper field to base temp to delete original polyline(s) later
a.AddField_management('base_temp', "helper", "SHORT", 1)
a.CalculateField_management('base_temp', "helper", "1", "PYTHON_9.3")
a.AddMessage("Completed Initial Data Load")
#### Breaking the polyline(s) into segments from the polyline vertices__________________________________________________________________
geoarray = []
linearray = []
#Getting the X and Y of the beginning and end of each vertices segment
with a.da.SearchCursor('base_temp', "SHAPE@") as cur_xy:
for row in cur_xy:
temp = []
for part in row[0]:
for point in part:
if point:
temp.append((float(point.X), float(point.Y)))
linearray.append(temp)
#Aligning the list into lists of XY points
for part in linearray:
for i in range(len(part)):
if i != len(part) - 1:
geoarray.append([part[i], part[i+1]])
a.AddMessage("Completed Gathering Road Segment Points")
#Creating new line features from the vertices XYs and inserting them into base temp
with a.da.InsertCursor('base_temp', "SHAPE@") as line_cursor:
for i in geoarray:
arr = a.Array([a.Point(i[0][0], i[0][1]), a.Point(i[1][0], i[1][1])])
line_cursor.insertRow([a.Polyline(arr, spatial)])
a.AddMessage("Completed Creating New Road Segments")
#### Length and Road Percent Calculations of newly created segments_____________________________________________________________________
#Adding Length and Percent Fields and calculating Length in feet
a.AddField_management('base_temp', "LENGTH", "DOUBLE", 7, 1)
a.AddField_management('base_temp', "PERCENT", "SHORT", 3)
a.CalculateField_management('base_temp', "LENGTH", "!shape.Length@feet!", "PYTHON_9.3")
a.MakeFeatureLayer_management('base_temp', 'base_temp1')
a.SelectLayerByAttribute_management('base_temp1', "NEW_SELECTION", '"helper" = 1')
a.DeleteFeatures_management('base_temp1')
a.SelectLayerByAttribute_management('base_temp1', "NEW_SELECTION", '"LENGTH" IS NULL')
a.DeleteFeatures_management('base_temp1')
a.DeleteField_management('base_temp1', "helper")
a.AddMessage("Completed New Road Attribute Tables Updates")
#Calculating road percent from intersecting contours
with a.da.UpdateCursor('base_temp1', ["SHAPE@", "LENGTH", "PERCENT"]) as cursor:
for line in cursor:
try:
a.SelectLayerByLocation_management(contours, "INTERSECT", line[0], '', "NEW_SELECTION")
cont_list = []
with a.da.SearchCursor(contours, "CONTOUR") as cont_cursor:
for cont in cont_cursor:
cont_list.append(cont[0])
cmax = max(cont_list)
cmin = min(cont_list)
elev = cmax - cmin
line[2] = elev / int(line[1]) * 100
cursor.updateRow(line)
except:
next
a.AddMessage("Completed New Road Segment Percents from Contours")
####Cleaning of final output shapefile___________________________________________________________________________________________
a.SelectLayerByAttribute_management(contours, "CLEAR_SELECTION")
a.CopyFeatures_management('base_temp1', outputfeat)
a.AddMessage("Completed New Road Feature Output")