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
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Network"</SPAN><SPAN class="punctuation token">)</SPAN>
env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Z:\\PP\\PopPotential.gdb"</SPAN>
cclist <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'AD'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'AL'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'AT'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'BA'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'BE'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'BG'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'CH'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'CY'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'CZ'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> ibu <SPAN class="keyword token">in</SPAN> cclist<SPAN class="punctuation token">:</SPAN>
Modelling_ND <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Net2001/"</SPAN> <SPAN class="operator token">+</SPAN> ibu <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_Net2001_ND"</SPAN>
OD_Cost_Matrix <SPAN class="operator token">=</SPAN> <SPAN class="string token">"ODMatrix_Popul2001_"</SPAN> <SPAN class="operator token">+</SPAN> ibu
ORIGENS <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Raster/Cells_"</SPAN> <SPAN class="operator token">+</SPAN> ibu
DESTS <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Destinations/PopPlaces_"</SPAN> <SPAN class="operator token">+</SPAN> ibu
OD_MatrixTest_lyr <SPAN class="operator token">=</SPAN> ZL<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">/</SPAN>PP<SPAN class="operator token">/</SPAN>LYRS<SPAN class="operator token">/</SPAN><SPAN class="string token">" + ibu + "</SPAN>_Mat_Places2001<SPAN class="punctuation token">.</SPAN>lyr"
OD_MatrixLine_lyr <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Z:/PP/LYRS/"</SPAN> <SPAN class="operator token">+</SPAN> ibu <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_Matrix_Places2001.lyr"</SPAN>
outMatrix <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Z:/PP/PopPotential.gdb/MATRICES2001/"</SPAN> <SPAN class="operator token">+</SPAN> ibu <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_Matrix2001"</SPAN>
outODRes <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeODCostMatrixLayer_na<SPAN class="punctuation token">(</SPAN>Modelling_ND<SPAN class="punctuation token">,</SPAN> OD_Cost_Matrix<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ZEIT"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"45"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ZEIT"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ALLOW_UTURNS"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NO_HIERARCHY"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"STRAIGHT_LINES"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN>
outNALayer <SPAN class="operator token">=</SPAN> outODRes<SPAN class="punctuation token">.</SPAN>getOutput<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
searchFC <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Roadnet2001_"</SPAN> <SPAN class="operator token">+</SPAN> ibu <SPAN class="operator token">+</SPAN> <SPAN class="string token">" END;"</SPAN> <SPAN class="operator token">+</SPAN> ibu <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_Net2001_ND_Junctions NONE"</SPAN>
searchQuery <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Roadnet2001_"</SPAN> <SPAN class="operator token">+</SPAN> ibu<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'"WHERE_" <> "Road net 2001"'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddLocations_na<SPAN class="punctuation token">(</SPAN>OD_Cost_Matrix<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Origins"</SPAN><SPAN class="punctuation token">,</SPAN> ORIGENS<SPAN class="punctuation token">,</SPAN> "Name TARGET_FID <SPAN class="comment token">#", "100 Meters", "TARGET_FID", searchFC, "MATCH_TO_CLOSEST", "CLEAR", "NO_SNAP", "5 Meters", "INCLUDE", "")</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddLocations_na<SPAN class="punctuation token">(</SPAN>OD_Cost_Matrix<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Destinations"</SPAN><SPAN class="punctuation token">,</SPAN> DESTS<SPAN class="punctuation token">,</SPAN> "Name PopulID <SPAN class="comment token">#", "100 Meters", "PopulID", searchFC, "MATCH_TO_CLOSEST", "CLEAR", "NO_SNAP", "5 Meters", "INCLUDE", "")</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Solve_na<SPAN class="punctuation token">(</SPAN>OD_Cost_Matrix<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"SKIP"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"TERMINATE"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN>
subLayers <SPAN class="operator token">=</SPAN> dict<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">.</SPAN>datasetName<SPAN class="punctuation token">,</SPAN> lyr<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>outNALayer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
LinesSubLayer <SPAN class="operator token">=</SPAN> subLayers<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"ODLines"</SPAN><SPAN class="punctuation token">]</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SaveToLayerFile_management<SPAN class="punctuation token">(</SPAN>LinesSubLayer<SPAN class="punctuation token">,</SPAN> OD_MatrixLine_lyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN>OD_MatrixLine_lyr<SPAN class="punctuation token">,</SPAN> outMatrix<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>