I'm trying to create a OD Cost Matrix layer programmatically. The problem that I have, however, is that the layer doesn't get added to my Table of Contents.

If I manually run the Network Analyst->Analysis->Make OD Cost Matrix Layer tool a new layer is created in my Table of Contents; however, trying to accomplish the same from arcpy yields no layer.

Likewise, I'm trying to programmatically download, extract, and symbolize OSM (OpenStreetMaps) data. When utilize this OSM tool programmatically a new layer is not added to my Table of Contents.

Here is the relevant code from my toolbox.

# Load the OpenStreetMap toolbox.
arcpy.ImportToolbox(r"C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcToolbox\Toolboxes\OpenStreetMap Toolbox.tbx")

# Get the window extents.

# This is the current map, which should be an OSM base map.
curMapDoc = arcpy.mapping.MapDocument("CURRENT")

# Get the datafrom from the map (see the DataFrame object of arcpy).
# The DataFrame object has an "extent" object that has the XMin, XMax, YMin, and YMax.
dataFrame = arcpy.mapping.ListDataFrames(curMapDoc, "Layers")[0]
extent    = dataFrame.extent

messages.addMessage("XMin: {0}, XMax: {1}, YMin: {2}, YMax: {3}".format(extent.XMin, extent.XMax, extent.YMin, extent.YMax))

# Download the data from OSM.
# The "OSMLayer" layer does not get added!
arcpy.DownloadExtractSymbolizeOSMData2_osmtools(extent, True, "OSM_SAC_TEST", "OSMLayer")

# Convert the OSM data to a network dataset.
arcpy.OSMGPCreateNetworkDataset_osmtools("OSM_SAC_TEST", r"DriveGeneric.xml", r"ND")

# Create the OD Cost Matrix layer.
#network = "OSM_SAC_TEST_ND"
network = r"C:\Users\avejidah\Documents\ArcGIS\DB\ImportTest.gdb\OSM_SAC_TEST\OSM_SAC_TEST_ND"
# The "OD Cost Matrix" layer does not get added!
arcpy.na.MakeODCostMatrixLayer(network, "OD Cost Matrix", "DriveTime")

I don't get any error or anything, the tool runs and says "Completed." What am I missing here?


Never mind, I figured it out. I just needed to get the resulting layer and add it.

result = arcpy.na.MakeODCostMatrixLayer(network, "OD Cost Matrix", "DriveTime")
layerObj = result.getOutput(0)
arcpy.mapping.AddLayer(dataFrame, layerObj, "BOTTOM")
| improve this answer | |

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.