I want to use the result of OD cost matrix for my further calculations. However, whne I try to extract the sublayer "lines" it returnes an error

AttributeError: 'Layer' object has no attribute 'listLayers'

However a line before the method saveACopy (file_name) worked on the same layer file.

import arcpy 
import os 
def execute(self, parameters, messages):
    # Check out the Network Analyst extension license

    # Set environment
    output_dir = r"D:\GISworkspace"
    arcpy.env.workspace = os.path.join(output_dir,'DataTest.gdb')  # Setting the database, where the output table will be created
    out_path = arcpy.env.workspace                                 # Variable so that it is easier to operate with the path


    #Set local variables
    layer_name = "ODcostMatrix"
    impedance = "Length"
    output_layer_file = os.path.join(output_dir, layer_name + ".lyrx")
    network = parameters[0].value
    nodes   = parameters[1].value

    result_object = arcpy.na.MakeODCostMatrixLayer(network, layer_name, impedance)

    #Get the layer object from the result object. The OD cost matrix layer can
    #now be referenced using the layer object.
    layer_object = result_object.getOutput(0)

    #Get the names of all the sublayers within the OD cost matrix layer.
    sublayer_names = arcpy.na.GetNAClassNames(layer_object)
    #Stores the layer names that we will use later
    origins_layer_name = sublayer_names["Origins"]
    destinations_layer_name = sublayer_names["Destinations"]

    #Load the BS locations (nodes) as both origins and destinations.
    arcpy.na.AddLocations(layer_object, origins_layer_name, nodes)
    arcpy.na.AddLocations(layer_object, destinations_layer_name,nodes)

    #Solve the OD cost matrix layer

    #Save the solved OD cost matrix layer as a layer file on disk

    #Get the Lines Sublayer (all the distances) TODO fix the Atrribute Error: no method listLayers

You are receiving this error for the reason @crmackey provided in a comment:

ListLayers is a function, not a class method. You have to pass in a MapDocument into ListLayers.

His comment is based on you using ArcPy with the ArcGIS 10.x architecture. On the other hand you are reading documentation from ArcGIS Pro and appear to be assuming that the ArcPy imported from Desktop and Pro are identical when they clearly are not (see Terminology for distinguishing ArcPy installed with ArcGIS 10.x for Desktop from that which comes with ArcGIS Pro?).

I have not worked with Network Analyst for quite a while, and perhaps never with arcpy.na, but I think the ODCostMatrixSolverProperties (arcpy.na) page may be a good place to start getting back on track:

Provides access to analysis properties from an origin-destination (OD) cost matrix network analysis layer. The GetSolverProperties function is used to obtain an ODCostMatrixSolverProperties object from an OD cost matrix network analysis layer.

  • Thank you! It gave me a good starting option for the search. Now it works. – elena_grigoreva Feb 11 '16 at 14:47

After being reasonably pointed out to the shortage of my knowledge, I have conducted some further research. Now, it works with the following solution:

lines_layer_name = sublayer_names["ODLines"]
lines_sublayer = arcpy.mapping.ListLayers(layer_object, lines_layer_name)[0]

More reading on this can be done at ArcGIS help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.