Arcpy Closest Facility Layer unable to retrieve CFRoutes table

1753
6
Jump to solution
11-18-2013 03:10 AM
Highlighted
New Contributor II
I am using the following code to make a Closest Facility layer, which I then want to output the CFRoutes table to use in a SearchCursor:

    import arcpy, os, sys          from arcpy import env          arcpy.env.workspace = "C:\\Users\\Ant\\Documents\\ArcGIS\\ITN.gdb"     arcpy.RefreshCatalog("C:\\Users\\Ant\\Documents\\ArcGIS\\ITN.gdb")     env.overwriteOutput = True     inNetworkDataset = "C:\Users\Ant\Documents\ArcGIS\OSMaster.gdb\ITN_Network\ITN_NetworkDataset"     impedanceAttribute = "Drive"     searchTolerance = "5000 Meters"     accumulateAttributeName = ["Length", "Drive"]     inFacilities = "TL_chemistsV3"     inIncidents = "BLPUs_Points"     outLayerFile = "ProxChemistsCF.lyr"     outNALayer = arcpy.na.MakeClosestFacilityLayer(inNetworkDataset, "ProxChemistsCF", impedanceAttribute, "TRAVEL_TO", "", 1, accumulateAttributeName, "NO_UTURNS", "", "", "", "TRUE_LINES_WITHOUT_MEASURES")     outNALayer = outNALayer.getOutput(0)     subLayerNames = arcpy.na.GetNAClassNames(outNALayer)     facilitiesLayerName = subLayerNames["Facilities"]     incidentsLayerName = subLayerNames["Incidents"]     arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities, "", searchTolerance)     arcpy.na.AddLocations(outNALayer, incidentsLayerName, inIncidents, "", searchTolerance)     arcpy.na.Solve(outNALayer)     tableName = subLayerNames["CFRoutes"]     if arcpy.Exists("ProxChemists"):         arcpy.Delete_management("ProxChemists")     outTable = "ProxChemists"     arcpy.CopyRows_management(tableName, outTable)


but the last line fails with error message:

    ExecuteError: Failed to execute. Parameters are not valid.     ERROR 000732: Input Rows: Dataset Routes does not exist or is not supported     Failed to execute (CopyRows).


But the dataset I am inputting is CFRoutes, not Routes as per this line tableName = subLayerNames["CFRoutes"]

I tried skipping the CopyRows and jumping straight into the search cursor using this:

    with arcpy.da.UpdateCursor("BLPUs", ["FID", "ChemistsFID", "ChemistsDist", "ChemistsDrive"]) as cursor:         for row in cursor:             OID1 = str(int(row[0]))             searchrows = arcpy.da.SearchCursor(tableName, ["ObjectID", "FacilityID", "Total_Drive", "Total_Length"])             for searchrow in searchrows:                 searchrow1 = str(int(searchrow[0]))                 searchrow2 = str(int(searchrow[1]))                 searchrow3 = str(float(searchrow[2]))                 searchrow4 = str(float(searchrow[3]))                 if searchrow1 == OID1:                     row[1] = searchrow2                     row[2] = searchrow4                     row[3] = searchrow3                     cursor.updateRow(row)

But the error is 'RuntimeError: cannot open 'Routes''

So I guess calling CFRoutes tries to open the sub-table Routes intentionally but is failing, have I missed something out? Is the Closest Facility layer not actually being created?
Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Regular Contributor
Hi Anthony.  You're close, but you need to turn your CF sublayer into a layer object.  Then you can use it for input directly in other tools.

tableName = subLayerNames["CFRoutes"] routesSubLayer = arcpy.mapping.ListLayers(outNALayer, tableName)[0]  searchrows = arcpy.da.SearchCursor(routesSubLayer, ["ObjectID", "FacilityID", "Total_Drive", "Total_Length"]) ...

View solution in original post

Reply
0 Kudos
6 Replies
Highlighted
Esri Regular Contributor
Hi Anthony.  You're close, but you need to turn your CF sublayer into a layer object.  Then you can use it for input directly in other tools.

tableName = subLayerNames["CFRoutes"] routesSubLayer = arcpy.mapping.ListLayers(outNALayer, tableName)[0]  searchrows = arcpy.da.SearchCursor(routesSubLayer, ["ObjectID", "FacilityID", "Total_Drive", "Total_Length"]) ...

View solution in original post

Reply
0 Kudos
Highlighted
New Contributor II
hi and thanks - but now I get 'IndexError: list index out of range' - it's like it's not actually created, but I can see it when I print all subLayerNames. Any ideas??
Reply
0 Kudos
Highlighted
Esri Regular Contributor
Where in the code are you getting this error message?  Can you post your new code?
Reply
0 Kudos
Highlighted
New Contributor II
here it is, I tried adding df as the data frame and defining it as well, but didn't help:


import arcpy, os, sys

from arcpy import env
arcpy.env.workspace = "C:\\Users\\Ant\\Documents\\ArcGIS\\ITN.gdb"
arcpy.RefreshCatalog("C:\\Users\\Ant\\Documents\\ArcGIS\\ITN.gdb")
env.overwriteOutput = True
inNetworkDataset = "C:\Users\Ant\Documents\ArcGIS\OSMaster.gdb\ITN_Network\ITN_NetworkDataset"
impedanceAttribute = "Drive"
searchTolerance = "5000 Meters"
accumulateAttributeName = "Drive;Length"
inFacilities = "TL_chemistsV3"
inIncidents = "BLPUs_Points"
outLayerFile = "ProxChemistsCF.lyr"
outNALayer = arcpy.MakeClosestFacilityLayer_na(inNetworkDataset, "ProxChemistsCF", impedanceAttribute, "TRAVEL_TO", "", 1, accumulateAttributeName, "NO_UTURNS", "", "", "", "TRUE_LINES_WITHOUT_MEASURES")
outNALayer = outNALayer.getOutput(0)
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
facilitiesLayerName = subLayerNames["Facilities"]
incidentsLayerName = subLayerNames["Incidents"]
arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities, "", "")
arcpy.na.AddLocations(outNALayer, incidentsLayerName, inIncidents, "", "")
arcpy.Solve_na(outNALayer, "SKIP", "TERMINATE", "")
tableName = ["CFRoutes"]
routesSubLayer = arcpy.mapping.ListLayers(outNALayer, tableName)[0]
arcpy.AddField_management("BLPUs", "ChemistsFID", "LONG")
arcpy.AddField_management("BLPUs", "ChemistsDist", "DOUBLE")
arcpy.AddField_management("BLPUs", "ChemistsDrive", "DOUBLE")
arcpy.AddField_management("BLPUs", "ChemistsMiles", "DOUBLE")

with arcpy.da.UpdateCursor("BLPUs", ["FID", "ChemistsFID", "ChemistsDist", "ChemistsDrive"]) as cursor:
    for row in cursor:
        OID1 = str(int(row[0]))
        searchrows = arcpy.da.SearchCursor(routesSubLayer, ["ObjectID", "FacilityID", "Total_Drive", "Total_Length"])
        for searchrow in searchrows:
            searchrow1 = str(int(searchrow[0]))
            searchrow2 = str(int(searchrow[1]))
            searchrow3 = str(float(searchrow[2]))
            searchrow4 = str(float(searchrow[3]))
            if searchrow1 == OID1:
                row[1] = searchrow2
                row[2] = searchrow4
                row[3] = searchrow3
                cursor.updateRow(row)
SQL = ' [ChemistsDist] * 0.000621371192 '
arcpy.CalculateField_management("BLPUs", "ChemistsMiles1", SQL)
arcpy.DeleteField_management("BLPUs", "ChemistsDist1")
Reply
0 Kudos
Highlighted
Esri Regular Contributor
I think you just have a typo.

tableName = ["CFRoutes"]


should be:

tableName = subLayerNames["CFRoutes"]
Reply
0 Kudos
Highlighted
New Contributor II
ah yes, managed to drop that bit somehow, now working thanks very much!
Reply
0 Kudos