-
Notifications
You must be signed in to change notification settings - Fork 0
/
HumanTraffickingRouting.py
228 lines (214 loc) · 12.3 KB
/
HumanTraffickingRouting.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# HumanTrafficking_Rounting
# Last Update: 05/14/2014
# Author: Satoshi Miyazawa
# Solve Route analysis using ArcGIS Network Analyst
# Parameters: inNetworkDataset, inFacilities, inIncidents
# Require: arcpy(ArcGIS)
# Developed for ArcGIS 10.2.1
try:
import arcpy, os, datetime
arcpy.env.overwriteOutput = 1 # enable overwriting
arcpy.env.workspace = "C:/Users/koitaroh/Documents/ArcGIS/Default.gdb"
# Check out ArcGIS Network Analyst license
arcpy.CheckOutExtension("Network")
# Set local variables
inNetworkDataset = arcpy.GetParameterAsText(0)
# Incidents: Origing, Facilities: Destinations
inIncidents = arcpy.GetParameterAsText(1)
inFacilities = arcpy.GetParameterAsText(2)
# Name layers for each scenario
outNALayerName = "BestRoutes"
outNALayerName_1 = "Scenario_1"
outNALayerName_2 = "Scenario_2"
outNALayerName_3 = "Scenario_3"
outNALayerName_4 = "Scenario_4"
outNALayerName_5 = "Scenario_5"
outNALayerName_6 = "Scenario_6"
outNALayerName_6 = "Scenario_7"
impedanceAttribute = "Time"
accumulateAttributeName = ["Length","Time"]
outRouteFile = "F:/HumanTrafficking/HumanTrafficking_SubArea6.gdb/"
outLayerFile = "F:/HumanTrafficking/BestRoutes" + "/"
# define restrictions variables
arcpy.AddMessage("Creating Closest Facility Layer")
outNALayer = arcpy.na.MakeClosestFacilityLayer(inNetworkDataset,outNALayerName,
impedanceAttribute,"TRAVEL_TO",
"",26, accumulateAttributeName,
"NO_UTURNS", "")
arcpy.AddMessage("Closest Facility Layer created")
arcpy.AddMessage(datetime.datetime.now())
outNALayer = outNALayer.getOutput(0)
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
facilitiesLayerName = subLayerNames["Facilities"]
incidentsLayerName = subLayerNames["Incidents"]
fieldMappings1 = arcpy.na.NAClassFieldMappings(outNALayer, incidentsLayerName)
fieldMappings1["Name"].mappedFieldName = "Name"
fieldMappings2 = arcpy.na.NAClassFieldMappings(outNALayer, incidentsLayerName)
fieldMappings2["Name"].mappedFieldName = "Name"
# Run with Senario 1: Unorganized Smuggling 1
arcpy.AddMessage("Running Scenario_1.")
solverProperties = arcpy.na.GetSolverProperties(outNALayer)
defaultRestrictions = solverProperties.restrictions
defaultRestrictions += ["Avoid Off-road driving", "Avoid On-road driving"]
solverProperties.restrictions = defaultRestrictions
arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities,
fieldMappings1,"","","","","CLEAR","","","EXCLUDE")
arcpy.na.AddLocations(outNALayer, incidentsLayerName, inIncidents,
fieldMappings2,"","","","","CLEAR","","","EXCLUDE")
arcpy.na.Solve(outNALayer)
arcpy.AddMessage("Solving.")
arcpy.AddMessage(datetime.datetime.now())
for lyr in arcpy.mapping.ListLayers(outNALayer):
if lyr.name == "Routes":
arcpy.CopyFeatures_management(lyr, outRouteFile + "Scenario_1")
arcpy.AddField_management(outRouteFile + "Scenario_1", "scenarioid", "SHORT")
arcpy.CalculateField_management(outRouteFile + "Scenario_1", "scenarioid", 1)
arcpy.AddMessage("Layer for Scenario_1 saved.")
arcpy.AddMessage(datetime.datetime.now())
# Run with Scenario 2: Unorganized Smuggling 2
arcpy.AddMessage("Running Scenario_2.")
solverProperties = arcpy.na.GetSolverProperties(outNALayer)
defaultRestrictions = solverProperties.restrictions
defaultRestrictions += ["Avoid City", "Avoid Open Area", "Prefer Ephemeral River", "Prefer Federal Land", "Prefer Indian Reservation", "Prefer Northern Visibility"]
solverProperties.restrictions = defaultRestrictions
arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities,
fieldMappings1,"","","","","CLEAR","","","EXCLUDE")
arcpy.na.AddLocations(outNALayer, incidentsLayerName, inIncidents,
fieldMappings2,"","","","","CLEAR","","","EXCLUDE")
arcpy.AddMessage("Solving.")
arcpy.AddMessage(datetime.datetime.now())
arcpy.na.Solve(outNALayer)
for lyr in arcpy.mapping.ListLayers(outNALayer):
if lyr.name == "Routes":
arcpy.CopyFeatures_management(lyr, outRouteFile + "Scenario_2")
arcpy.AddField_management(outRouteFile + "Scenario_2", "scenarioid", "SHORT")
arcpy.CalculateField_management(outRouteFile + "Scenario_2", "scenarioid", 2)
arcpy.AddMessage("Layer for Scenario_2 saved. ")
arcpy.AddMessage(datetime.datetime.now())
# Run with Scenario 3: Organized Smuggling 1
arcpy.AddMessage("Running Scenario_3.")
solverProperties = arcpy.na.GetSolverProperties(outNALayer)
defaultRestrictions = solverProperties.restrictions
defaultRestrictions.remove("Avoid City")
defaultRestrictions.remove("Avoid Off-road driving")
defaultRestrictions += ["Avoid Off-road walking"]
defaultRestrictions.remove("Avoid On-road driving")
defaultRestrictions.remove("Avoid Open Area")
defaultRestrictions.remove("Prefer Ephemeral River")
defaultRestrictions.remove("Prefer Federal Land")
defaultRestrictions.remove("Prefer Indian Reservation")
defaultRestrictions.remove("Prefer Northern Visibility")
solverProperties.restrictions = defaultRestrictions
arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities,
fieldMappings1,"","","","","CLEAR","","","EXCLUDE")
arcpy.na.AddLocations(outNALayer, incidentsLayerName, inIncidents,
fieldMappings2,"","","","","CLEAR","","","EXCLUDE")
arcpy.AddMessage("Solving.")
arcpy.AddMessage(datetime.datetime.now())
arcpy.na.Solve(outNALayer)
for lyr in arcpy.mapping.ListLayers(outNALayer):
if lyr.name == "Routes":
arcpy.CopyFeatures_management(lyr, outRouteFile + "Scenario_3")
arcpy.AddField_management(outRouteFile + "Scenario_3", "scenarioid", "SHORT")
arcpy.CalculateField_management(outRouteFile + "Scenario_3", "scenarioid", 3)
arcpy.AddMessage("Layer for Scenario_3 saved.")
arcpy.AddMessage(datetime.datetime.now())
# Run with Scenario 4: Organized Smuggling 2
arcpy.AddMessage("Running Scenario_4.")
solverProperties = arcpy.na.GetSolverProperties(outNALayer)
defaultRestrictions = solverProperties.restrictions
defaultRestrictions += ["Avoid City"]
defaultRestrictions += ["Avoid Open Area"]
defaultRestrictions += ["Prefer Ephemeral River"]
defaultRestrictions += ["Prefer Federal Land"]
defaultRestrictions += ["Prefer Indian Reservation"]
defaultRestrictions += ["Prefer Northern Visibility"]
solverProperties.restrictions = defaultRestrictions
arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities,
fieldMappings1,"","","","","CLEAR","","","EXCLUDE")
arcpy.na.AddLocations(outNALayer, incidentsLayerName, inIncidents,
fieldMappings2,"","","","","CLEAR","","","EXCLUDE")
arcpy.AddMessage("Solving.")
arcpy.AddMessage(datetime.datetime.now())
arcpy.na.Solve(outNALayer)
for lyr in arcpy.mapping.ListLayers(outNALayer):
if lyr.name == "Routes":
arcpy.CopyFeatures_management(lyr, outRouteFile + "Scenario_4")
arcpy.AddField_management(outRouteFile + "Scenario_4", "scenarioid", "SHORT")
arcpy.CalculateField_management(outRouteFile + "Scenario_4", "scenarioid", 4)
arcpy.AddMessage("Layer for Scenario_4 saved.")
arcpy.AddMessage(datetime.datetime.now())
# Run with Scenario 5: Organized Trafficking 1
arcpy.AddMessage("Running Scenario_5.")
arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities,
fieldMappings1,"","","","","CLEAR","","","EXCLUDE")
arcpy.na.AddLocations(outNALayer, incidentsLayerName, inIncidents,
fieldMappings2,"","","","","CLEAR","","","EXCLUDE")
solverProperties = arcpy.na.GetSolverProperties(outNALayer)
defaultRestrictions = solverProperties.restrictions
solverProperties.restrictions = defaultRestrictions
arcpy.na.UpdateAnalysisLayerAttributeParameter(outNALayer, "Avoid City", "Restriction Usage", "AVOID_MEDIUM")
arcpy.na.UpdateAnalysisLayerAttributeParameter(outNALayer, "Avoid Open Area", "Restriction Usage", "AVOID_MEDIUM")
arcpy.na.UpdateAnalysisLayerAttributeParameter(outNALayer, "Prefer Ephemeral River", "Restriction Usage", "PREFER_MEDIUM")
arcpy.na.UpdateAnalysisLayerAttributeParameter(outNALayer, "Prefer Federal Land", "Restriction Usage", "PREFER_MEDIUM")
arcpy.na.UpdateAnalysisLayerAttributeParameter(outNALayer, "Prefer Indian Reservation", "Restriction Usage", "PREFER_MEDIUM")
arcpy.na.UpdateAnalysisLayerAttributeParameter(outNALayer, "Prefer Northern Visibility", "Restriction Usage", "PREFER_MEDIUM")
arcpy.AddMessage("Solving.")
arcpy.AddMessage(datetime.datetime.now())
arcpy.na.Solve(outNALayer)
for lyr in arcpy.mapping.ListLayers(outNALayer):
if lyr.name == "Routes":
arcpy.CopyFeatures_management(lyr, outRouteFile + "Scenario_5")
# arcpy.AddField_management(outRouteFile + "Scenario_5", "scenarioid", "SHORT")
# arcpy.CalculateField_management(outRouteFile + "Scenario_5", "scenarioid", 5)
arcpy.AddMessage("Layer for Scenario_5 saved.")
arcpy.AddMessage(datetime.datetime.now())
# Run with Scenario 6: Organized Trafficking 2
arcpy.AddMessage("Running Scenario_6.")
solverProperties = arcpy.na.GetSolverProperties(outNALayer)
defaultRestrictions = solverProperties.restrictions
solverProperties.restrictions = defaultRestrictions
arcpy.na.UpdateAnalysisLayerAttributeParameter(outNALayer, "Avoid City", "Restriction Usage", "AVOID_LOW")
arcpy.na.UpdateAnalysisLayerAttributeParameter(outNALayer, "Prefer Ephemeral River", "Restriction Usage", "PREFER_LOW")
arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities,
fieldMappings1,"","","","","CLEAR","","","EXCLUDE")
arcpy.na.AddLocations(outNALayer, incidentsLayerName, inIncidents,
fieldMappings2,"","","","","CLEAR","","","EXCLUDE")
arcpy.AddMessage("Solving.")
arcpy.AddMessage(datetime.datetime.now())
arcpy.na.Solve(outNALayer)
for lyr in arcpy.mapping.ListLayers(outNALayer):
if lyr.name == "Routes":
arcpy.CopyFeatures_management(lyr, outRouteFile + "Scenario_6")
# arcpy.AddField_management(outRouteFile + "Scenario_6", "scenarioid", "SHORT")
# arcpy.CalculateField_management(outRouteFile + "Scenario_6", "scenarioid", 6)
arcpy.AddMessage("Layer for Scenario_6 saved.")
arcpy.AddMessage(datetime.datetime.now())
# Run with Scenario 7: Organized Trafficking 3
arcpy.AddMessage("Running Scenario_7.")
solverProperties = arcpy.na.GetSolverProperties(outNALayer)
defaultRestrictions = solverProperties.restrictions
defaultRestrictions += ["Avoid Off-road driving"]
solverProperties.restrictions = defaultRestrictions
arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities,
fieldMappings1,"","","","","CLEAR","","","EXCLUDE")
arcpy.na.AddLocations(outNALayer, incidentsLayerName, inIncidents,
fieldMappings2,"","","","","CLEAR","","","EXCLUDE")
arcpy.AddMessage("Solving.")
arcpy.AddMessage(datetime.datetime.now())
arcpy.na.Solve(outNALayer)
for lyr in arcpy.mapping.ListLayers(outNALayer):
if lyr.name == "Routes":
arcpy.CopyFeatures_management(lyr, outRouteFile + "Scenario_7")
# arcpy.AddField_management(outRouteFile + "Scenario_7", "scenarioid", "SHORT")
# arcpy.CalculateField_management(outRouteFile + "Scenario_7", "scenarioid", 7)
arcpy.AddMessage("Layer for Scenario_7 saved.")
arcpy.AddMessage(datetime.datetime.now())
except Exception as e:
print(arcpy.GetMessages(2))
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print(("An error occured on line %i" % tb.tb_lineno))
print((str(e)))