-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArcGISRoadSurfaceErosion.py
103 lines (82 loc) · 3.85 KB
/
ArcGISRoadSurfaceErosion.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
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
__author__ = 'Pabitra'
import os
import subprocess
import arcpy
# Set overwrite option
arcpy.env.overwriteOutput = True
# get the input parameters
dp_shapefile = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(dp_shapefile)
dp_shapefile = str(desc.catalogPath)
rd_shapefile = arcpy.GetParameterAsText(1)
desc = arcpy.Describe(rd_shapefile)
rd_shapefile = str(desc.catalogPath)
graip_db_file = arcpy.GetParameterAsText(2)
dem_grid_file = arcpy.GetParameterAsText(3)
desc = arcpy.Describe(dem_grid_file)
dem_grid_file = str(desc.catalogPath)
dpsi_raster_file = arcpy.GetParameterAsText(4)
is_stream_connected = arcpy.GetParameterAsText(5)
# construct command to execute
this_script_dir = os.path.dirname(os.path.realpath(__file__))
# put quotes around file paths in case they have spaces
dp_shapefile = '"' + dp_shapefile + '"'
rd_shapefile = '"' + rd_shapefile + '"'
graip_db_file = '"' + graip_db_file + '"'
dem_grid_file = '"' + dem_grid_file + '"'
dpsi_raster_file = '"' + dpsi_raster_file + '"'
py_script_to_execute = os.path.join(this_script_dir, 'RoadSurfaceErosion.py')
py_script_to_execute = '"' + py_script_to_execute + '"'
cmd = py_script_to_execute + \
' --dp ' + dp_shapefile + \
' --rd ' + rd_shapefile + \
' --mdb ' + graip_db_file + \
' --z ' + dem_grid_file + \
' --dpsi ' + dpsi_raster_file
if str(is_stream_connected) == 'true':
cmd += ' --sc '
# show executing command
arcpy.AddMessage('\nEXECUTING COMMAND:\n' + cmd)
# Capture the contents of shell command and print it to the arcgis dialog box
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
arcpy.AddMessage('\nProcess started:\n')
start_message = "Please wait a few minutes. Computation is in progress ..."
arcpy.AddMessage('\n' + start_message + '\n')
streamdata = process.communicate()[0]
messages = streamdata.split("\n")
for msg in messages:
arcpy.AddMessage(msg)
# if success running the above script
if process.returncode == 0:
# join roadlines shape file with the roadline graip database table
# Process: Make Feature Layer
rd_shapefile = rd_shapefile.replace('"', '')
dp_shapefile = dp_shapefile.replace('"', '')
roadlines_layer = 'RoadLines'
drainpoints_layer = 'DrainPoints'
# get the current map document
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
# remove if the shape file layers currently in the map document
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.name.lower() == roadlines_layer.lower() or lyr.name.lower() == drainpoints_layer.lower() :
# delete layer object from memory
arcpy.Delete_management(lyr.name)
# create feature layer from the roadlines shape file
arcpy.MakeFeatureLayer_management(rd_shapefile, roadlines_layer)
# create feature layer from the drainpoints shape file
arcpy.MakeFeatureLayer_management(dp_shapefile, drainpoints_layer)
addLayer_roadLines = arcpy.mapping.Layer(roadlines_layer)
addLayer_drainPoints = arcpy.mapping.Layer(drainpoints_layer)
graip_db_file = graip_db_file.replace('"', '')
graip_roadlines_table = os.path.join(graip_db_file, "RoadLines")
graip_drainpoints_table = os.path.join(graip_db_file, "DrainPoints")
# join the roasline layer to the roadlines db table
arcpy.AddJoin_management(roadlines_layer, "GRAIPRID", graip_roadlines_table, "GRAIPRID", "KEEP_ALL")
arcpy.mapping.AddLayer(df, addLayer_roadLines, "TOP")
# join the drainpoints layer to the drainpoints table
arcpy.AddJoin_management(drainpoints_layer, "GRAIPDID", graip_drainpoints_table, "GRAIPDID", "KEEP_ALL")
arcpy.mapping.AddLayer(df, addLayer_drainPoints, "TOP")
arcpy.RefreshActiveView()
arcpy.AddMessage("RoadLine shape file was joined with Roadlines database table")
arcpy.AddMessage("DrainPoints shape file was joined with DrainPoints database table")