ODCM_Table = arcpy.na.Solve(outNALayer) #Solves the Origin-Destination Cost Matrix and produces the ODCM_Table arcpy.TableToTable_conversion(ODCM_Table, workspace + "\Default.gdb", "ODCM") # Thought this should convert result to GDB Table
print ODCM_Table GPL0
Solved! Go to Solution.
# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script # The following inputs are layers or table views: "BG2Branches\Lines" arcpy.TableToTable_conversion("BG2Branches/Lines","C:/Users/jdk588/Documents/ArcGIS/Default.gdb","ODCM_Test","#","""Name "Name" true true true 128 Text 0 0 ,First,#,/ODLines,Name,-1,-1;OriginID "OriginID" true true true 4 Long 0 0 ,First,#,/ODLines,OriginID,-1,-1;DestinationID "DestinationID" true true true 4 Long 0 0 ,First,#,/ODLines,DestinationID,-1,-1;DestinationRank "DestinationRank" true true true 4 Long 0 0 ,First,#,/ODLines,DestinationRank,-1,-1;Total_Length "Total_Length" true true true 8 Double 0 0 ,First,#,/ODLines,Total_Length,-1,-1""","#")
arcpy.na.Solve(outNALayer) arcpy.SaveToLayerFile_management(outNALayer, workspace + "\\" + outLayerFile, "RELATIVE") mxd = arcpy.mapping.MapDocument(r" C:\Program Files (x86)\ArcGIS\Desktop10.1\MapTemplates\Traditional Layouts\LetterPortrait.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] addlyr = arcpy.mapping.Layer(r???C:\Temp\BG2Branches.lyr") arcpy.mapping.AddLayer(df, addlyr) for lyr in df: if lyr.name == "Lines": arcpy.TableToTable_conversion(lyr, addlyr, "ODCM_Table") arcpy.mapping.RemoveLayer(df, lyr) arcpy.Delete_management(workspace + ???\\??? + str(addlyr) + ???.lyr???) else: arcpy.AddError (???ODCM Lines Not Found.???)
import arcpy arcpy.CreateTable_management(temp_gdb, "temp_table", "D:\\temp.gdb\\template_table", "") ## use template table with column headings to create new empty table with da.InsertCursor(temp_table,['FIELD1', FIELD2']) as insert: ## if the column names match the table, should just insert right in. for row in ODCM_Table: insert.insertRow(row) del row del insert
ND = "your network dataset" OD_Lines = "output location for table Lines = r"OD_Matrix\Lines" OD_Matrix = "OD_Matrix" arcpy.MakeODCostMatrixLayer_na(ND,OD_Matrix,"Distance","","1") arcpy.AddLocations_na(OD_Matrix, "Origins", "your input") arcpy.AddLocations_na(OD_Matrix, "Destinations", "your input") arcpy.Solve_na(OD_Matrix, "SKIP","CONTINUE") arcpy.Select_analysis(Lines, OD_Lines)
If you are trying to get the lines object from an od cost matrix, you will need to use the "Select" tool. It would look something like this (sorry it's a dirty example):ND = "your network dataset" OD_Lines = "output location for table Lines = r"OD_Matrix\Lines" OD_Matrix = "OD_Matrix" arcpy.MakeODCostMatrixLayer_na(ND,OD_Matrix,"Distance","","1") arcpy.AddLocations_na(OD_Matrix, "Origins", "your input") arcpy.AddLocations_na(OD_Matrix, "Destinations", "your input") arcpy.Solve_na(OD_Matrix, "SKIP","CONTINUE") arcpy.Select_analysis(Lines, OD_Lines)
ND = "your network dataset" OD_Lines = "output location for table Lines = r"OD_Matrix\Lines" OD_Matrix = "OD_Matrix"
ND = "your network dataset" OD_Lines = "output location for table Lines = r"OD_Matrix\Lines" OD_Matrix = "OD_Matrix"
arcpy.env.workspace = r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb\CBSA_12345" arcpy.env.overwriteOutput = True GDB = r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb" cbsaFD = "CBSA_12345" bgCentroids = r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb\CBSA_12345\CBSA_12345_bgCentroids" proForma = r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb\CBSA_12345\CBSA_12345_proForma" AOIs = r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb\CBSA_12345\CBSA_12345_RN" inOrigins = bgCentroids inDestinations = proForma inNetworkDataset = r"C:\ArcGIS\Business Analyst\US\Data\StreetMapData\streets" outNALayerName = "BG2Branches" impedenceAttribute = "Length" outLayerFile = outNALayerName + ".lyr" outNALayer = arcpy.MakeODCostMatrixLayer_na(inNetworkDataset, outNALayerName, "Length", "", "", "", "", "", "USE_HIERARCHY", "", "NO_LINES") outNALayer = outNALayer.getOutput(0) subLayerNames = arcpy.na.GetNAClassNames(outNALayer) originsLayerName = subLayerNames["Origins"] originsFieldMap = arcpy.na.NAClassFieldMappings(outNALayer, originsLayerName) originsFieldMap["Name"].mappedFieldName = "BG_ID" arcpy.na.AddLocations(outNALayer, originsLayerName, inOrigins, originsFieldMap) destinationsLayerName = subLayerNames["Destinations"] destinationsFieldMap = arcpy.na.NAClassFieldMappings(outNALayer, destinationsLayerName) destinationsFieldMap["Name"].mappedFieldName = "DID" arcpy.na.AddLocations(outNALayer, destinationsLayerName, inDestinations, destinationsFieldMap) arcpy.na.Solve(outNALayer)
arcpy.TableToTable_conversion(r"BG2Branches\Lines", cbsaFD + "_ODCM")
arcpy.MakeFeatureLayer_management(outLayerName, cbsaFD + "ODCM", "", GDB) Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 5748, in MakeFeatureLayer raise e ExecuteError: ERROR 000732: Input Features: Dataset BG2Branches.lyr does not exist or is not supported
Branch2BG_ODCM_LYR = arcpy.Solve(outNALayer) Branch2BG_ODCM_LYR = Branch2BG_ODCM_LYR.getOutput(0) print Branch2BG_ODCM_LYR GPL0
arcpy.Describe(Branch2BG_ODCM_LYR) Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\__init__.py", line 1200, in Describe return gp.describe(value) File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 374, in describe self._gp.Describe(*gp_fixargs(args, True))) IOError: "GPL0" does not exist
I have a table being returned from the Network Analyst OD Cost Matrix...
I want to convert the result object to a GDB Table and I tried using this..
ODCM_Table = arcpy.na.Solve(outNALayer) #Solves the Origin-Destination Cost Matrix and produces the ODCM_Table arcpy.TableToTable_conversion(ODCM_Table, workspace + "\Default.gdb", "ODCM") # Thought this should convert result to GDB Table
But it throws an error:
Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\conversion.py", line 1904, in TableToTable raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Rows: Dataset GPL0 does not exist or is not supported Failed to execute (TableToTable).
I thought maybe the cause of the error was the formatting of the result object being held on the 'ODCM_Table' variable, so I printed it to see what it returns...
print ODCM_Table GPL0
No problem I can see there.
So how can I convert this result object to an actual table?
import arcpy import numpy as np oLoc = r"C:\Documents\ArcGIS\Default.gdb" tLoc = r"C:\Documents\ArcGIS" datasource = r"H:\Documents\ArcGIS\numpytab_out.dbf" #list out your fieldnames flds = ['staname', 'change'] #convert the dbf to a numpyarray obj numpytab = arcpy.da.TableToNumPyArray(datasource, flds) ##check to see if intermediate table numpytab exists and delete it if so arcpy.env.workspace = oLoc arcpy.env.overwriteOutput = True tabs2 = arcpy.ListTables() for tab2 in tabs2: if tab2=="numpytab_out": arcpy.Delete_management(r"H:\Documents\ArcGIS\Default.gdb\numpytab_out") arcpy.AddMessage("....Deleted numpytab_out") ##now covert the numpyarray to the gdb table in the default.gdb arcpy.da.NumPyArrayToTable(numpytab, r"H:\Documents\ArcGIS\Default.gdb\numpytab_out")
I'm not sure what your source table is (.dbf?),
Hey James, any insight is useful. The ODCM_Table should just be holding the result object from the OD Cost Matrix in Network Analyst. The OD Cost Matrix analysis layer 'outNALayer' is composed of six network analyst classes, one of which is 'Lines' which stores the path information, like Total Length from Origin to Destination.
So really the question here is how can I convert the an NA Class to a Table, or Feature Layer outside of an ArcMap Session. It doesn't seem possible.