AnsweredAssumed Answered

Problems with AddLocations in Python Scripting

Question asked by RRG03CS on Dec 17, 2018
Latest reply on Dec 19, 2018 by RRG03CS

I have written a python script that iterates through a country list in order to generate OD matrices between origins and destinations. The network analyst shall generate one matrix per country. For each country, I use several thousands of origins and destinations.

I am running the script from Windows explorer via its context menue. If I open the script from the context menue with "Edit with Idle" and run the script from there, the script runs successfully (but it takes a lot of time). If I open the script with "Edit with Idle (ArcGIS Pro)" or with "Run with ArcGIS Pro", the script crashes with the error "ERROR 000192: Invalid value for sub layer.  Error when trying to run AddLocations".

I have no idea what causes the script to crash. The ESRI website contains a contribution to this error message (Error 000192: Invalid value for Sub layer—Help | ArcGIS Desktop ), but this refers to problems in Model Builder rather than in Python scripting.

I also tested my script with the tool 'AnalyzeToolForPro_Management', but this test returned no problem.

In parallel I developed a second Python script applying the same programme logic (i.e. iteration through the countries, declaration of variables etc.) and using the same input data, but this time using the GenerateNearTable command instead of the Network Analyst, and this script successfully finishes even when I call it through "EDit with Idel (ArcGIS Pro)" or "Run with ArcGIS Pro".

So, my conclusion is that the error message is not a matter of the general programme logic or is an issue of the input data, but it is probably directly related to the AddLocations tool.


Following is my Pyhton code. The error occurs in the line "arcpy.AddLocations_na(...)" (highlighted in bold). I appreciate any hint as how to fix the problem.



mport arcpy
from arcpy import env

     env.workspace = "Z:\\PP\\PopPotential.gdb"
     cclist = ('AD','AL','AT','BA','BE','BG','CH','CY','CZ')
     for ibu in cclist:
          Modelling_ND = "Net2001/" + ibu + "_Net2001_ND"                               
          OD_Cost_Matrix = "ODMatrix_Popul2001_" + ibu                                  
          ORIGENS = "Raster/Cells_" + ibu                                               
          DESTS = "Destinations/PopPlaces_" + ibu                                      
          OD_MatrixTest_lyr = ZL:/PP/LYRS/" + ibu + "_Mat_Places2001.lyr"
          OD_MatrixLine_lyr = "Z:/PP/LYRS/" + ibu + "_Matrix_Places2001.lyr"

          outMatrix = "Z:/PP/PopPotential.gdb/MATRICES2001/" + ibu + "_Matrix2001"
          outODRes = arcpy.MakeODCostMatrixLayer_na(Modelling_ND, OD_Cost_Matrix, "ZEIT", "45", "", "ZEIT", "ALLOW_UTURNS", "", "NO_HIERARCHY", "", "STRAIGHT_LINES", "")
          outNALayer = outODRes.getOutput(0)

          searchFC = "Roadnet2001_" + ibu + " END;" + ibu + "_Net2001_ND_Junctions NONE"
          searchQuery = [["Roadnet2001_" + ibu, '"WHERE_" <> "Road net 2001"']]        
          arcpy.AddLocations_na(OD_Cost_Matrix, "Origins", ORIGENS, "Name TARGET_FID #", "100 Meters", "TARGET_FID", searchFC, "MATCH_TO_CLOSEST", "CLEAR", "NO_SNAP", "5 Meters", "INCLUDE", "")
          arcpy.AddLocations_na(OD_Cost_Matrix, "Destinations", DESTS, "Name PopulID #", "100 Meters", "PopulID", searchFC, "MATCH_TO_CLOSEST", "CLEAR", "NO_SNAP", "5 Meters", "INCLUDE", "")
          arcpy.Solve_na(OD_Cost_Matrix, "SKIP", "TERMINATE", "")
          subLayers = dict((lyr.datasetName, lyr) for lyr in arcpy.mapping.ListLayers(outNALayer)[1:])
          LinesSubLayer = subLayers["ODLines"]
          arcpy.SaveToLayerFile_management(LinesSubLayer, OD_MatrixLine_lyr, "", "CURRENT")
          arcpy.CopyFeatures_management(OD_MatrixLine_lyr, outMatrix)