I'm trying to export Orders dataset after solving a Vehicle Routing Problem (VRP) into a shapefile using Python only. The problem is that apparently arcpy without mxd open doesn't recognise my VRP layer. Below is the code I'm working on:
outNALayer = arcpy.na.MakeVehicleRoutingProblemLayer (inNetworkDataset,
outNALayerName,
"Time",
"Length",
"Minutes",
"Meters",
"",
"",
"High",
"Low",
"ALLOW_DEAD_ENDS_ONLY",
["Oneway","Speed"],
"NO_HIERARCHY",
"",
"NO_LINES")
outNALayer = outNALayer.getOutput(0)
#Get the names of all the sublayers within the VRP layer.
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
#Stores the layer names that we will use later
ordersLayerName = subLayerNames["Orders"]
depotsLayerName = subLayerNames["Depots"]
routesLayerName = subLayerNames["Routes"]
pairsLayerName = subLayerNames["OrderPairs"]
arcpy.na.AddLocations(outNALayer, ordersLayerName, inOrders, orderFieldMappings,"")
#List name of attributes in Depot layer
depotFieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, depotsLayerName)
arcpy.na.AddLocations(outNALayer, depotsLayerName, inDepots, depotFieldMappings, "")
#List name of attributes in Route layer
routeFieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, routesLayerName)
arcpy.na.AddLocations(outNALayer, routesLayerName, inRoutes, routeFieldMappings, "")
#List name of attributes in Route layer
pairFieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, pairsLayerName)
arcpy.na.AddLocations(outNALayer, pairsLayerName, inPairs, pairFieldMappings, "")
arcpy.na.Solve(outNALayer, "SKIP")
#arcpy.management.SaveToLayerFile(outNALayer,outLayerFile,"ABSOLUTE")
arcpy.CopyFeatures_management(outNALayerName + r"\Orders", inOrders + '_' + str(iterator))
The error message is that the Orders does not exist or is not supported. I have tried saving the layer file on the disk and accessing Orders from that layer but get the same error. Is there a solution to access the actual sublayers within solved layer just in python. I think Select function is available in model builder only. Note: the above code runs just fine when is run in the mxd python window.
The error is at the last line. The error message is:
Traceback (most recent call last): File "C:\Users\zarem\OneDrive\Total_Transport_Home\Code\Vehicle Routing Problem_Only - Copy.py", line 143, in arcpy.CopyFeatures_management(outNALayerName + r"\Orders", inOrders + '_' + str(iterator)) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 2435, in CopyFeatures raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Features: Dataset VRP_Lyr_Grp_74\Orders does not exist or is not supported