Skip To Content

GetSolverProperties

Summary

Returns a Network Analyst solver properties object based on the type of the network analysis layer specified as the argument. The solver properties object is used to update the analysis properties for the layer.

Discussion

GetSolverProperties acts as the main entry point when modifying the analysis properties of a network analysis layer. It returns a separate solver properties object based on the network analysis layer. There are five types of solver properties objects: RouteSolverProperties, ClosestFacilitySolverProperties, ServiceAreaSolverProperties, ODCostMatrixSolverProperties, and LocationAllocationSolverProperties. Each solver properties object provides read/write access to the analysis properties specific to a network analysis layer.

Syntax

GetSolverProperties (network_analyst_layer)
ParameterExplanationData Type
network_analyst_layer

A variable that references a Layer object obtained from a network analysis layer. It can be derived from existing layers in a map document or by specifying the catalog path to the network analysis layer file as an argument to the Layer class. The isNetworkAnalystLayer property on the Layer object can be used to identify whether a given Layer object is a network analysis layer.

Layer
Return Value
Data TypeExplanation
Object

A solver properties object corresponding to the type of the network analysis layer.

Code sample

GetSolverProperties example

This example shows how to use GetSolverProperties to access a network analysis layer's solver properties object and how to use that solver properties object to update analysis settings.

# Name: GetSolverProperties_01.py
# Description: Generate 3-minute service areas around fire stations at several
#               times of day to compare coverage differences due to varying
#               traffic conditions. Use GetSolverProperties to access the
#               Service Area layer's solver properties object, and update the
#               time of day property prior to each solve.
# Requirements: Network Analyst Extension

#Import system modules
import datetime
import arcpy
from arcpy import env

try:
    #Check out the Network Analyst extension license
    arcpy.CheckOutExtension("Network")

    #Set environment settings
    env.workspace = "C:/Data/SanFrancisco.gdb"
    env.overwriteOutput = True

    #Set local variables
    inNetworkDataset = "Transportation/Streets_ND"
    outNALayerName = "FireStationCoverage"
    outSAFC = "Analysis/outSAPolys"
    impedanceAttribute = "TravelTime"
    inFacilities = "Analysis/FireStations"
    timelist = [datetime.datetime(2013, 8, 23, 7, 0, 0),
                datetime.datetime(2013, 8, 23, 12, 30, 0),
                datetime.datetime(2013, 8, 23, 17, 30, 0),
                datetime.datetime(2013, 8, 23, 21, 0, 0)]

    #Create a new service area layer.
    outSAResultObject = arcpy.na.MakeServiceAreaLayer(inNetworkDataset, outNALayerName,
                                  impedanceAttribute, "TRAVEL_FROM", "3",
                                  "DETAILED_POLYS", "NO_MERGE",
                                  hierarchy = "NO_HIERARCHY")

    #Get the layer object from the result object. The service area layer can
    #now be referenced using the layer object.
    outNALayer = outSAResultObject.getOutput(0)

    #Get the names of all the sublayers within the service area layer.
    subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
    #Store the layer names that we will use later
    facilitiesLayerName = subLayerNames["Facilities"]
    polygonsLayerName = subLayerNames["SAPolygons"]

    #The input data has a field for FireStationID that we want to transfer to
    #our analysis layer. Add the field, and then use field mapping to transfer
    #the values.
    arcpy.na.AddFieldToAnalysisLayer(outNALayer, facilitiesLayerName,
                                                    "FireStationID", "TEXT")
    fieldMappings = arcpy.na.NAClassFieldMappings(outNALayer,
                                                    facilitiesLayerName)
    fieldMappings["FireStationID"].mappedFieldName = "FireStationID"

    #Load the fire stations as facilities.
    arcpy.na.AddLocations(outNALayer, facilitiesLayerName, inFacilities,
                            fieldMappings, "",
                            exclude_restricted_elements = "EXCLUDE")

    #Get sublayers we will want to work with later
    FacilitiesSubLayer = arcpy.mapping.ListLayers(outNALayer,
                                                    facilitiesLayerName)[0]
    PolygonsSubLayer = arcpy.mapping.ListLayers(outNALayer,
                                                    polygonsLayerName)[0]

    # Add fields to the output Polygons sublayer. We will fill the values later.
    arcpy.na.AddFieldToAnalysisLayer(outNALayer, polygonsLayerName,
                                        "FireStationID", "TEXT")
    arcpy.na.AddFieldToAnalysisLayer(outNALayer, polygonsLayerName,
                                        "TimeOfDay", "TEXT")

    #Get the Service Area Layer's solver properties. This will allow us to
    #set individual properties later without re-creating the layer.
    SA_SolverProperties = arcpy.na.GetSolverProperties(outNALayer)

    #Solve the Service Area for each time of day in our time list
    for t in timelist:

        print "Now solving for time of day: " + t

        #Use the solver properties to set the time of day for the solve
        SA_SolverProperties.timeOfDay = t

        #Solve the service area layer
        arcpy.na.Solve(outNALayer)

        #Transfer the FireStationID field from the input Facilities to the
        #output Polygons
        arcpy.management.AddJoin(PolygonsSubLayer, "FacilityID",
                                        FacilitiesSubLayer, "ObjectID")
        arcpy.management.CalculateField(PolygonsSubLayer, "FireStationID",
                                        "!Facilities.FireStationID!", "PYTHON")
        arcpy.management.RemoveJoin(PolygonsSubLayer)

        #Populate the TimeOfDay field
        expression = '"' + t + '"'
        arcpy.management.CalculateField(PolygonsSubLayer, "TimeOfDay",
                                            expression, "PYTHON")

        #Append the polygons to the output feature class. If this was the first
        #solve, create the feature class.
        if not arcpy.Exists(outSAFC):
            arcpy.management.CopyFeatures(PolygonsSubLayer, outSAFC)
        else:
            arcpy.management.Append(PolygonsSubLayer, outSAFC)

    print "Script completed successfully"

except Exception as e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "An error occured on line %i" % tb.tb_lineno
    print str(e)