I want to calculate a route consisting of 4 points with a script tool and save the results to disk. I need the route to appear as layer on ArcCatalog for continuing working with it (modification of its attribute table). The script tool completes successfully but no output is saved. So, I tried to add the Save To Layer File tool.
I receive the following error message:
File "C:\Users\Manu\Desktop\SS 2015\Analyse mit GI-Technologien\Abgabe\gruppe1\Teil1_240915.py", line 84, in arcpy.management.SaveToLayerFile(outNALayer,outLayerFile,"","CURRENT") File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6640, in SaveToLayerFile raise e
ExecuteError: ERROR 999999: Error executing function. Failed to execute (SaveToLayerFile).
This is my code:
#Create a new Route layer.
outRouteResultObject=arcpy.na.MakeRouteLayer(in_network_dataset,out_network_analysis_layer=outNALayerName,impedance_attribute=impedanceAttribute,find_best_order="USE_INPUT_ORDER",accumulate_attribute_name=["Length"],output_path_shape="TRUE_LINES_WITH_MEASURES")
#Get the layer object from the result object. The route layer can now be referenced using the layer object.
outNALayer=outRouteResultObject.getOutput(0)
#Get the names of all the sublayers within the route layer.
subLayerNames=arcpy.na.GetNAClassNames(outNALayer)
#Store the layer names that we will use later
stopsLayerName=subLayerNames["Stops"]
routeLayerName=subLayerNames["Routes"]
fieldMappings=arcpy.na.NAClassFieldMappings(outNALayer,stopsLayerName)
fieldMappings["RouteName"].mappedFieldName="stopover"
arcpy.na.AddLocations(outNALayer,stopsLayerName,"Stations",fieldMappings,"","Order","SNAP")
arcpy.na.Solve(outNALayer)
arcpy.management.SaveToLayerFile(outNALayer,outLayerFile,"","CURRENT")
1 Answer 1
I'm pretty sure you are almost there with your code. You just need to retrieve the routing result sublayer from your solved Routing Analysis layer, and then save it to a permanent location on disk:
#....starting here from your code above
# Get a path to the routes sub layer
outNARoutesSubLayer = arcpy.mapping.ListLayers(outNALayer, subLayerNames["Routes"])[0]
# Proceed with solving the layer as you were before...
arcpy.na.AddLocations(outNALayer,stopsLayerName,"Stations",fieldMappings,"","Order","SNAP")
arcpy.na.Solve(outNALayer)
# Save the resulting route polyline (the Routes Sublayer) to disk
arcpy.CopyFeatures_management(outNARoutesSubLayer, "Output_Routing_FeatureClass")
-
Thank you so much Jim!!!! It works perfectly. You made two students very happy. We were working on this problem for days.S.Tutte– S.Tutte2015年09月27日 13:09:07 +00:00Commented Sep 27, 2015 at 13:09