Problems saving ODLines layer to disk

852
2
06-11-2020 02:43 AM
CarstenSchuermann
Occasional Contributor

If have developed a python script that iterates through a list of 30 countries. For each country, I want to generate an OD matrix between origins and destinations. The script sucessfully sets up the MakeODCostMatrixLayer, adds the origins and destinations and solves the matrix. From the resulting matrix layers, I only want to save the ODLines layer to disk for further processing. My code is as follows (only those lines where the error occurs):

...

arcpy.na.Solve(outNALayer, "SKIP", "TERMINATE", "")  #until here the script successfully runs, and I can inspect the results in ArcCatalog

print ("... SAVING MATRIX TO LYR FILE ")
lyrsFile = arcpy.mp.LayerFile(outNALayer)
subLayers = dict((lyr.datasetName, lyr) for lyr in lyrsFile.listLayers())
LinesSubLayer = subLayers["ODLines"]
arcpy.SaveToLayerFile_management(LinesSubLayer, OD_MatrixLine_lyr, "", "CURRENT")
arcpy.CopyFeatures_management(OD_MatrixLine_lyr, outMatrix)

...

The script crashes after the print statement with the following error message:

<MappingLayerObject object at 0x000002A084AFEE88>

I am using ArcGIS Pro 2.5.1 and Python 3.6.9

Any ideas what happens? Maybe there is a simpler approach to save the ODLines layer to disk?

Any help is highly appreciated!

Carsten

0 Kudos
2 Replies
MelindaMorang
Esri Regular Contributor

This should be all you need.  You can retrieve the OD layer object from the Solve tool (or from the MakeODCostMatrixAnalysisLayer tool or AddLoations - all of them output the layer object).

You use listLayers with a search keyword to retrieve the output Lines sublayer, and then use that in copy features.

layer_object = arcpy.na.Solve(outNALayer, "SKIP", "TERMINATE", "").getOutput(0)
sublayer_names = arcpy.na.GetNAClassNames(layer_object)
lines_sublayer = layer_object.listLayers(sublayer_names["ODLines"])[0]
arcpy.CopyFeatures_management(lines_sublayer, outMatrix)‍‍‍

The second-to-last code sample here demonstrates this.

Please note that starting in ArcGIS Pro 2.4, we have provided a new python interface for the Network Analyst solvers.  The syntax is much simpler for setting up and solving an analysis and working with the results, and you don't need to work with layers.  Small solves are generally a lot faster as well.  Here is a presentation from DevSummit 2020 that describes how to use the new python classes and methods: https://youtu.be/TZ5aYTcDvME

CarstenSchuermann
Occasional Contributor

Thanks Melinda, for the correct code. Now it works fine. I will also have a look into the new NAX python interface. Simplifying syntax and codes is always appreciated!

0 Kudos