Select to view content in your preferred language

Help returning path object from Network Analysis Layer

374
0
06-29-2023 05:52 PM
LindsayRaabe_FPCWA
Regular Contributor II

Help - I'm trying to get the path to the "Routes" sub-layer of the Network Analysis layer created on Line 2, but can't figure out how to pass that information through to the correct variable (feature class identified at Line 52 and used at Lines 62 & 86). I suspect I'm not using the arcpy.Describe function correctly. What should I be using instead?

 

# Create route analysis layer and solve routes
    RAL = arcpy.na.MakeRouteAnalysisLayer(
        network_data_source = nd_path,
        layer_name = nd_layer_name,
        travel_mode = travelmode,
        sequence = "USE_CURRENT_ORDER",
        time_of_day = None,
        time_zone = "LOCAL_TIME_AT_LOCATIONS",
        line_shape = "ALONG_NETWORK",
        accumulate_attributes = None,
        generate_directions_on_solve = "DIRECTIONS",
        time_zone_for_time_fields = "LOCAL_TIME_AT_LOCATIONS",
        ignore_invalid_locations = "SKIP"
    )
    print ("Route analysis layer created")

    # Get the layer object from the Route Analysis Layer result object.
    # The route layer can now be referenced using the layer object.
    layer_object = RAL.getOutput(0)

    # Get the names of all the sublayers within the route layer.
    sublayer_names = arcpy.na.GetNAClassNames(layer_object)

    # Stores the layer names that we will use later
    routes_layer_name = sublayer_names["Routes"]

    # Instantiate a ServiceArea analysis object.
    service_area = arcpy.nax.ServiceArea(nd_path)
    print ("Service area set")

    # Get the desired travel mode for the analysis.
    nd_travel_modes = arcpy.nax.GetTravelModes(nd_path)
    travel_mode = nd_travel_modes[travelmode]
    print ("Travel mode acquired")

    # Set properties.
    service_area.timeUnits = arcpy.nax.TimeUnits.Minutes
    service_area.defaultImpedanceCutoffs = [5, 10, 15]
    service_area.travelMode = travel_mode
    service_area.outputType = arcpy.nax.ServiceAreaOutputType.Polygons
    service_area.geometryAtOverlap = arcpy.nax.ServiceAreaOverlapGeometry.Split
    print ("Network Analysis properties set. Adding locations...")

# Unrelated code removed...

    print ("Routes calculated")

    arcpy.CheckInExtension("Network")
    print ("Network Analyst license checked-in")

    ####################### Append data #######################
    datasets = arcpy.ListDatasets(feature_type='feature')
    datasets = [''] + datasets if datasets is not None else []

    for ds in datasets:
        for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
            path = os.path.join(arcpy.env.workspace, ds, fc)
            if (str(os.path.splitext(fc)[0])[:6]) == "Routes": ##### <<<<< This finds the Routes sublayer in the geodatabase\network dataset and assigns it to variable 'fc'
                print ("Routes found - start append process")

                # Variables - do not update
                fcpath = arcpy.Describe(fc) ###### <<<<<<<< This is where I am trying to get the path to variable 'fc'
                fc = str(fcpath)                           # Path to Feature Class
                fsItemId = outfsID                              # Feature Service Item ID to update
                layerIndex = sublayerID                         # Layer Index

                # Create UUID variable for GDB
                gdbId = str(uuid.uuid1())

                # Function to Zip FGD
                def zipDir(dirPath, zipPath):
                    '''Zip File Geodatabase'''
                    zipf = ZipFile(zipPath , mode='w')
                    gdb = os.path.basename(dirPath)
                    for root, _ , files in os.walk(dirPath):
                        for file in files:
                            if 'lock' not in file:
                               filePath = os.path.join(root, file)
                               zipf.write(filePath , os.path.join(gdb, file))
                    zipf.close()

                print("Creating temporary File Geodatabase")
                gdb = arcpy.CreateFileGDB_management(arcpy.env.scratchFolder, gdbId)[0]

                # Export feature class to temporary File Geodatabase
                fcName = os.path.basename(fc) ###### <<<<<<<< So that I can use the pathway here
                fcName = fcName.split('.')[-1]
                print(f"Exporting {fcName} to temp FGD")
                arcpy.conversion.ExportTable(fc, gdb + "\\" + fcName) # 

 

Current error from results below:

Creating temporary File Geodatabase
Exporting <geoprocessing describe data object object at 0x000001AE661C6070> to temp FGD

Script failed - email notification sent to gis@fpc.wa.gov.au

Exception: Failed to execute. Parameters are not valid.
ERROR 000732: Input Table: Dataset <geoprocessing describe data object object at 0x000001AE661C6070> does not exist or is not supported
ERROR 000354: The name contains invalid characters
Failed to execute (ExportTable).

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
0 Replies