# Name: UpdateAnalysisLayerAttributeParameter_Workflow.py
# Description: Use the network dataset's length and height restriction attribute
# parameters to find a route suitable for transporting a large
# wind turbine blade. The results are saved to a layer file.
# Requirements: Network Analyst Extension
#Import system modules
import arcpy
from arcpy import env
import os
try:
#Check out Network Analyst license if available. Fail if the Network Analyst license is not available.
if arcpy.CheckExtension("network") == "Available":
arcpy.CheckOutExtension("network")
else:
raise arcpy.ExecuteError("Network Analyst Extension license is not available.")
#Set environment settings
output_dir = "C:/Data"
#The NA layer's data will be saved to the workspace specified here
env.workspace = os.path.join(output_dir, "Output.gdb")
env.overwriteOutput = True
#Set local variables
input_gdb = "C:/Data/SanDiego.gdb"
network = os.path.join(input_gdb, "Transportation", "Streets_ND")
layer_name = "WindTurbineRoute"
impedance = "Meters"
restrictions = ["Driving a Truck", "Height Restriction", "Oneway",
"Length Restriction", "National STAA and Locally Preferred Routes"]
seaport = os.path.join(input_gdb, "Analysis", "Port")
wind_farm = os.path.join(input_gdb, "Analysis", "WindFarm")
output_layer_file = os.path.join(output_dir, layer_name + ".lyrx")
#Make a new route layer. Use restriction attributes relevant to trucking
#oversize loads
result_object = arcpy.na.MakeRouteLayer(network, layer_name, impedance,
restriction_attribute_name=restrictions)
#Get the layer object from the result object. The route layer can
#now be referenced using the layer object.
layer_object = result_object.getOutput(0)
#Set the vehicle height and length attribute parameters to the dimensions of
#the wind turbine transport truck. If these dimensions exceed the limits
#associated with a street feature, that street will be restricted, and the
#resulting route will avoid it.
arcpy.na.UpdateAnalysisLayerAttributeParameter(layer_object,
"Height Restriction", "Vehicle Height (feet)", 13.25)
arcpy.na.UpdateAnalysisLayerAttributeParameter(layer_object,
"Length Restriction", "Vehicle Length (feet)", 80)
#Load the origin and destination points as Stops in the Route
sublayer_names = arcpy.na.GetNAClassNames(layer_object)
stops_layer_name = sublayer_names["Stops"]
arcpy.na.AddLocations(layer_object, stops_layer_name, seaport, "", "")
arcpy.na.AddLocations(layer_object, stops_layer_name, wind_farm, "", "",
append="APPEND")
#Solve the route layer
arcpy.na.Solve(layer_object)
#Save the solved route layer as a layer file on disk
layer_object.saveACopy(output_layer_file)
print("Script completed successfully")
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
print(("An error occurred on line %i" % tb.tb_lineno))
print((str(e)))