Problems with AddLocations in Python Scripting

1059
5
12-17-2018 12:15 AM
CarstenSchuermann
Occasional Contributor

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

try:
     arcpy.CheckOutExtension("Network")
     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)
0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

can you throw in a print statement and check the inputs to  arcpy.AddLocations_na to make sure they are correct

the code is within an incomplete try-except block, can you format it and show what it is supposed to do (or just remove the try-except block to get a message)

/blogs/dan_patterson/2016/08/14/script-formatting 

CarstenSchuermann
Occasional Contributor

Hi Dan,

of course my script ends with an except block. I didn´t include the except block in my post in order to keep it short. Anyway, the except block I use is the following:

except Exception as e:
    import traceback, sys
    tb = sys.exc_info()[2]
    print("An error occured on line %i" % tb.tb_lineno)
    print(str(e))

When running the script from its context menue with "Edit with Idle (ArcGIS Pro)" or with "Run with ArcGIS Pro", the following error messages is thrown by the except block:

"An error occured on line 19

Failure to execute. Invalid parameters.

ERROR 000192: Invalid value for sub layer. 

Error when trying to run AddLocations"

If I remove the try-except block, the script crashes with the following error messages:

####

Traceback (most recent call last):

  File "Z:\PP\PP_Run_woE.py", line 19, in <module>

    arcpy.AddLocations_na(OD_Cost_Matrix, "Origins", ORIG, "Name TARGET_FID #", "150 Meters", "TARGET_FID", searchFC, "MATCH_TO_CLOSEST", "CLEAR", "NO_SNAP", "5 Meters", "INCLUDE", "")

  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\na.py", line 285, in AddLocations

    raise e

  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\na.py", line 282, in AddLocations

    retval = convertArcObjectToPythonObject(gp.AddLocations_na(*gp_fixargs((in_network_analysis_layer, sub_layer, in_table, field_mappings, search_tolerance, sort_field, search_criteria, match_type, append, snap_to_position_along_network, snap_offset, exclude_restricted_elements, search_query), True)))

  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 496, in <lambda>

    return lambda *args: val(*gp_fixargs(args, True))

arcgisscripting.ExecuteError: Invalid parameters.

ERROR 000192: Invalid value for sub layer

Error when trying to run (AddLocations).

#####

Any idea?

0 Kudos
DeeleshMandloi
Esri Contributor

Hi Carsten,

   Your script is written to work against ArcMap and will require changes before it can be run against ArcGIS Pro.

For example, You are using arcpy.mapping.ListLayers which is no longer supported with arcpy that is included with ArcGIS Pro. The error you are currently getting is not because of the use of arcpy.mapping.ListLayers, But eventually when the line of code that contains arcpy.mapping is executed, you are going to run into errors.

The way you perform network analysis using Python (and arcpy) has changed between ArcMap and ArcGIS Pro. Some of the steps you can take to "migrate" your script are listed in Migrating arcpy.na to ArcGIS Pro—Network Analyst module | ArcGIS Desktop . 

In your case, you should start with using arcpy.na.MakeODCostMatrixAnalyisLayer_na and see if you get past the "invalid value for sub layer" name issue. As I mentioned before, you will also need to change your code to use arcpy.mp instead of arcpy.mapping.

While you can write a single script that can work against both ArcMap and ArcGIS Pro, your script will be a lot simpler if it was just meant to run against either ArcMap or ArcGIS Pro.

Hope this helps

Deelesh  

DanPatterson_Retired
MVP Emeritus

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.

Good point, but something failed along the way

0 Kudos
CarstenSchuermann
Occasional Contributor

Hi Deelesh,

thanks for your tips. Following your advice I used the arcpy.na.MakeODCostMatrixAnalyisLayer tool, but it turned out that this was not the cause of the problem, as the script still crashed with the same error message when it tries to run the AddLocation tool. But this tip put me on the right track.

Obviously, there is a difference how ArcMap and ArcGIS Pro handle the sub layer names of the AddLocation tool. As you see from my code example above, I used the standard names "Origins" and "Destinations" to indicate the sub layers in the AddLocation tool (second parameter of this tool). While this works for ArcMap, it obviously does not work  for ArcGIS Pro any longer, and then throws the error message. Then I modified my script as follows:

[...]

sublayer_names = arcpy.na.GetNAClassNames(outNALayer)
origins_layer_name = sublayer_names["Origins"]

arcpy.na.AddLocations(OD_Cost_Matrix,origins_layer_name,ORIG,"Name TARGET_FID #","150 Meters","TARGET_FID",searchFC,"MATCH_TO_CLOSEST","CLEAR","NO_SNAP","5 Meters","INCLUDE","")

[... similar modification for loading the destinations ...]

Now the script runs successfullym without throwing any error.

Do you know why the default sub layer names no longer work under ArcGIS Pro?

What I am wondering, though, is, why the AnalyzeToolForPro_Management tool did not identify this issue. I thought this tool was designed to find such tiny 'coding differences' between ArcMap and ArcGIS Pro.

0 Kudos