Make Service Area Analysis Layer (Network Analyst)

Summary

Makes a service area network analysis layer and sets its analysis properties. A service area analysis layer is useful in determining the area of accessibility within a given cutoff cost from a facility location. The layer can be created using a local network dataset or using a routing service hosted online or in a portal.

Usage

  • After creating the analysis layer with this tool, you can add network analysis objects to it using the Add Locations tool, solve the analysis using the Solve tool, and save the results on disk using the Save To Layer File tool.

  • When using this tool in geoprocessing models, if the model is run as a tool, the output network analysis layer must be made a model parameter; otherwise, the output layer is not added to the contents of the map.

  • In ArcGIS Pro, network analysis layer data is stored on disk in file geodatabase feature classes. When creating a network analysis layer in a project, the layer's data will be created in a new feature dataset in the Current Workspace environment. When creating a network analysis layer in a Python script, you must first explicitly set the workspace environment to a file geodatabase where you want the layer's data to be stored using arcpy.env.workspace = "<path to file gdb>". When the layer is created, a new feature dataset containing the appropriate sublayer feature classes will be added to this file geodatabase.

Parameters

LabelExplanationData Type
Network Data Source

The network dataset or service on which the network analysis will be performed. Use the portal URL for a service.

Network Dataset Layer;String
Layer Name
(Optional)

The name of the network analysis layer to create.

String
Travel Mode
(Optional)

The name of the travel mode to use in the analysis. The travel mode represents a collection of network settings, such as travel restrictions and U-turn policies, that determine how a pedestrian, car, truck, or other medium of transportation moves through the network. Travel modes are defined on your network data source.

An arcpy.na.TravelMode object and a string containing the valid JSON representation of a travel mode can also be used as input to the parameter.

String
Travel Direction
(Optional)

Specifies the direction of travel to or from the facilities.

  • Away from facilities —The service area represents traveling away from the facilities. This is the default.
  • Toward facilities —The service area represents traveling toward the facilities.
String
Cutoffs
(Optional)

The extent of the service area to be calculated in the units of the impedance attribute used by your selected travel mode. For example, when analyzing driving time, a cutoff value of 10 means that the resulting service area will represent the area reachable within a 10-minute drive time.

Multiple cutoffs can be set to create concentric service areas. For example, to find 2-, 3-, and 5-minute service areas for the same facility, specify 2, 3, and 5 as the values for this parameter.

This default cutoff value can be overridden on a per-facility basis by specifying individual break values in the facilities sublayer.

Double
Time of Day
(Optional)

The time to depart from or arrive at the facilities of the service area layer. The interpretation of this value as a departure or arrival time depends on whether travel is away from or toward the facilities.

  • It represents the departure time if Travel Direction is set to Away from facilities.
  • It represents the arrival time if Travel Direction is set to Toward facilities.

The Time of Day parameter is most useful for finding the roads that can be reached based on a travel mode that uses an impedance attribute that varies with the time of the day, such as one based on dynamic traffic conditions. Solving the same analysis using different Time of Day values allows you to see how a facility's reach changes over time. For example, the five-minute service area around a fire station may start out large in the early morning, diminish during the morning rush hour, grow in the late morning, and so on, throughout the day.

A date and time can be specified as 10/21/2015 10:30 AM.

Instead of using a particular date, a day of the week can be specified using the following dates:

  • Today—12/30/1899
  • Sunday—12/31/1899
  • Monday—1/1/1900
  • Tuesday—1/2/1900
  • Wednesday—1/3/1900
  • Thursday—1/4/1900
  • Friday—1/5/1900
  • Saturday—1/6/1900

Date
Time Zone
(Optional)

Specifies the time zone for the time of day parameter.

  • Local time at locations —The time of day parameter will use the time zone or zones in which the facilities are located. The start or end times of the service areas are staggered by time zone. This is the default.For example, setting time of day to 9:00 a.m. causes service areas to be generated for 9:00 a.m. eastern time for any facilities in the eastern time zone, 9:00 a.m. central time for facilities in the central time zone, 9:00 a.m. mountain time for facilities in the mountain time zone, and so on. If stores in a chain that span the U.S. open at 9:00 a.m. local time, choose this parameter value to find market territories at opening time for all stores in one solve. First, the stores in the eastern time zone open and a polygon is generated. An hour later, stores open in the central time zone, and so on. Nine o'clock is always in local time but staggered in real time.
  • UTC —The time of day parameter will use coordinated universal time (UTC). All facilities are reached or departed from simultaneously, regardless of the time zone or zones in which they are located.Setting time of day to 2:00 p.m. causes service areas to be generated for 9:00 a.m. eastern standard time for any facilities in the eastern time zone, 8:00 a.m. central standard time for facilities in the central time zone, 7:00 a.m. mountain standard time for facilities in the mountain time zone, and so on.The scenario above assumes standard time. During daylight saving time, the eastern, central, and mountain times will each be one hour ahead (that is, 10:00, 9:00, and 8:00 a.m., respectively).One of the cases in which the UTC option is useful is to visualize emergency response coverage for a jurisdiction that is split into two time zones. The emergency vehicles are loaded as facilities. Time of day is set to now in UTC. (You need to determine what the current time and date are in terms of UTC to correctly use this option.) Other properties are set and the analysis is solved. Even though a time-zone boundary divides the vehicles, the results show areas that can be reached given current traffic conditions. This same process can be used for other times as well, not just for now.
String
Output Type
(Optional)

Specifies the type of output to be generated. Service area output can be line features representing the roads reachable before the cutoffs are exceeded or the polygon features encompassing these lines (representing the reachable area).

  • Polygons —The service area output will contain polygons only. This is the default.
  • Lines —The service area output will contain lines only.
  • Polygons and lines —The service area output will contain both polygons and lines.
String
Polygon Detail
(Optional)

Specifies the level of detail of the output polygons.

  • Standard —Polygons with a standard level of detail will be created. This is the default.
  • Generalized —Generalized polygons will be created using the network's hierarchy attribute to produce results quickly. This option is not available if the network does not have a hierarchy attribute.
  • High —Polygons with a higher level of detail will be creates for applications in which precise results are important.
String
Geometry at Overlaps
(Optional)

Specifies the behavior of service-area output from multiple facilities in relation to one another.

  • Overlap —Individual polygons or sets of lines for each facility will be created. The polygons or lines can overlap each other. This is the default.
  • Dissolve —The polygons of multiple facilities that have the same cutoff value will be joined into a single polygon. This option does not apply to line output.
  • Split —An area will be assigned to the closest facility so polygons or lines do not overlap each other.
String
Geometry at Cutoffs
(Optional)

Specifies the behavior of service area output for a single facility when multiple cutoff values are specified. This parameter does not apply to line output.

  • Rings —Each polygon will include only the area between consecutive cutoff values. It will not include the area between the facility and any smaller cutoffs. For example, if you create 5- and 10-minute service areas, the 5-minute service area polygon will include the area reachable in 0 to 5 minutes, and the 10-minute service area polygon will include the area reachable in 5 to 10 minutes. This is the default.
  • Disks —Each polygon will include the area reachable from the facility up to the cutoff value, including the area reachable within smaller cutoff values. For example, if you create 5- and 10-minute service areas, the 10-minute service area polygon will include the area under the 5-minute service area polygon.
String
Polygon Trim Distance
(Optional)

The service area polygon trim distance. The polygon trim distance is the distance the service area polygon will extend from the road when no other reachable roads are nearby, similar to a line buffer size. This is useful when the network is sparse and you don't want the service area to cover large areas where there are no features.

This parameter includes a value and units for the distance. The default value is 100 meters.

Linear Unit
Exclude Sources From Polygon Generation
(Optional)

The network dataset edge sources that will be excluded when generating service area polygons. Polygons will not be generated around the excluded sources, even though they are traversed in the analysis.

Excluding a network source from service area polygons does not prevent those sources from being traversed. Excluding sources from service area polygons only influences the polygon shape of the service areas. To prevent traversal of a given network source, you must create an appropriate restriction when defining your network dataset.

This is useful if you have some network sources that you don't want to be included in the polygon generation because they create less accurate polygons or are inconsequential for the service area analysis. For example, while creating a walk-time service area in a multimodal network that includes streets and metro lines, you should choose to exclude the metro lines from polygon generation. Although the traveler can use the metro lines, they cannot stop partway along a metro line and enter a nearby building. Instead, they must travel the full length of the metro line, exit the metro system at a station, then use the streets to walk to the building. It would be inaccurate to generate a polygon feature around a metro line.

This parameter is not available when the output geometry types do not include polygons, there are less than two edge sources in the network, the network data source is an ArcGIS Online service, or the network data source is a service on a version of Portal for ArcGIS which does not support excluding sources.

String
Accumulate Attributes
(Optional)

A list of cost attributes to be accumulated during analysis. These accumulated attributes are for reference only; the solver only uses the cost attribute used by your designated travel mode when solving the analysis.

For each cost attribute that is accumulated, a Total_[Impedance] property is populated in the network analysis output features.

This parameter is not available if the analysis layer is not configured to output lines, the network data source is an ArcGIS Online service, or the network data source is a service on a version of Portal for ArcGIS that does not support accumulation.

String

Derived Output

LabelExplanationData Type
Network Analyst Layer

The newly created network analysis layer.

Network Analyst Layer

arcpy.na.MakeServiceAreaAnalysisLayer(network_data_source, {layer_name}, {travel_mode}, {travel_direction}, {cutoffs}, {time_of_day}, {time_zone}, {output_type}, {polygon_detail}, {geometry_at_overlaps}, {geometry_at_cutoffs}, {polygon_trim_distance}, {exclude_sources_from_polygon_generation}, {accumulate_attributes})
NameExplanationData Type
network_data_source

The network dataset or service on which the network analysis will be performed. Use the portal URL for a service.

Network Dataset Layer;String
layer_name
(Optional)

The name of the network analysis layer to create.

String
travel_mode
(Optional)

The name of the travel mode to use in the analysis. The travel mode represents a collection of network settings, such as travel restrictions and U-turn policies, that determine how a pedestrian, car, truck, or other medium of transportation moves through the network. Travel modes are defined on your network data source.

An arcpy.na.TravelMode object and a string containing the valid JSON representation of a travel mode can also be used as input to the parameter.

String
travel_direction
(Optional)

Specifies the direction of travel to or from the facilities.

  • FROM_FACILITIESThe service area represents traveling away from the facilities. This is the default.
  • TO_FACILITIESThe service area represents traveling toward the facilities.

Using this parameter can result in different service areas on a network with one-way restrictions and different impedances based on direction of travel. The service area for a pizza delivery store, for example, should be created away from the facility, whereas the service area of a hospital should be created toward the facility.

String
cutoffs
[cutoffs,...]
(Optional)

The extent of the service area to be calculated in the units of the impedance attribute used by your selected travel mode. For example, when analyzing driving time, a cutoff value of 10 means that the resulting service area will represent the area reachable within a 10-minute drive time.

Multiple cutoffs can be set to create concentric service areas. For example, to find 2-, 3-, and 5-minute service areas for the same facility, specify 2, 3, and 5 as the values for this parameter.

This default cutoff value can be overridden on a per-facility basis by specifying individual break values in the facilities sublayer.

Double
time_of_day
(Optional)

The time to depart from or arrive at the facilities of the service area layer. The interpretation of this value as a departure or arrival time depends on whether travel is away from or toward the facilities.

  • It represents the departure time if travel_direction='FROM_FACILITIES'.
  • It represents the arrival time if travel_direction='TO_FACILITIES'.

The time_of_day parameter is most useful for finding the roads that can be reached based on a travel mode that uses an impedance attribute that varies with the time of the day, such as one based on dynamic traffic conditions. Solving the same analysis using different time_of_day values allows you to see how a facility's reach changes over time. For example, the five-minute service area around a fire station may start out large in the early morning, diminish during the morning rush hour, grow in the late morning, and so on throughout the day.

A date and time can be specified as 10/21/2015 10:30 AM.

Instead of using a particular date, a day of the week can be specified using the following dates:

  • Today—12/30/1899
  • Sunday—12/31/1899
  • Monday—1/1/1900
  • Tuesday—1/2/1900
  • Wednesday—1/3/1900
  • Thursday—1/4/1900
  • Friday—1/5/1900
  • Saturday—1/6/1900

Date
time_zone
(Optional)

Specifies the time zone for the time of day parameter.

  • LOCAL_TIME_AT_LOCATIONSThe time of day parameter will use the time zone or zones in which the facilities are located. The start or end times of the service areas are staggered by time zone. This is the default.For example, setting time of day to 9:00 a.m. causes service areas to be generated for 9:00 a.m. eastern time for any facilities in the eastern time zone, 9:00 a.m. central time for facilities in the central time zone, 9:00 a.m. mountain time for facilities in the mountain time zone, and so on. If stores in a chain that span the U.S. open at 9:00 a.m. local time, choose this parameter value to find market territories at opening time for all stores in one solve. First, the stores in the eastern time zone open and a polygon is generated. An hour later, stores open in the central time zone, and so on. Nine o'clock is always in local time but staggered in real time.
  • UTCThe time of day parameter will use coordinated universal time (UTC). All facilities are reached or departed from simultaneously, regardless of the time zone or zones in which they are located.Setting time of day to 2:00 p.m. causes service areas to be generated for 9:00 a.m. eastern standard time for any facilities in the eastern time zone, 8:00 a.m. central standard time for facilities in the central time zone, 7:00 a.m. mountain standard time for facilities in the mountain time zone, and so on.
    Note:

    The scenario above assumes standard time. During daylight saving time, the eastern, central, and mountain times will each be one hour ahead (that is, 10:00, 9:00, and 8:00 a.m., respectively).

    One of the cases in which the UTC option is useful is to visualize emergency response coverage for a jurisdiction that is split into two time zones. The emergency vehicles are loaded as facilities. Time of day is set to now in UTC. (You need to determine what the current time and date are in terms of UTC to correctly use this option.) Other properties are set and the analysis is solved. Even though a time-zone boundary divides the vehicles, the results show areas that can be reached given current traffic conditions. This same process can be used for other times as well, not just for now.
String
output_type
(Optional)

Specifies the type of output to be generated. Service area output can be line features representing the roads reachable before the cutoffs are exceeded or the polygon features encompassing these lines (representing the reachable area).

  • POLYGONSThe service area output will contain polygons only. This is the default.
  • LINESThe service area output will contain lines only.
  • POLYGONS_AND_LINESThe service area output will contain both polygons and lines.

The Lines and Polygons and lines output types are not available if the network data source is a service on a version of Portal for ArcGIS that does not support line generation.

String
polygon_detail
(Optional)

Specifies the level of detail of the output polygons.

  • STANDARDPolygons with a standard level of detail will be created. This is the default.
  • GENERALIZEDGeneralized polygons will be created using the network's hierarchy attribute to produce results quickly. This option is not available if the network does not have a hierarchy attribute.
  • HIGHPolygons with a higher level of detail will be creates for applications in which precise results are important.

If your analysis includes an urban area with a grid-like street network, the difference between generalized and standard polygons will be minimal. However, for mountain and rural roads, the standard and detailed polygons may present significantly more accurate results than generalized polygons.

String
geometry_at_overlaps
(Optional)

Specifies the behavior of service-area output from multiple facilities in relation to one another.

  • OVERLAPIndividual polygons or sets of lines for each facility will be created. The polygons or lines can overlap each other. This is the default.
  • DISSOLVEThe polygons of multiple facilities that have the same cutoff value will be joined into a single polygon. This option does not apply to line output.
  • SPLITAn area will be assigned to the closest facility so polygons or lines do not overlap each other.
String
geometry_at_cutoffs
(Optional)

Specifies the behavior of service area output for a single facility when multiple cutoff values are specified. This parameter does not apply to line output.

  • RINGSEach polygon will include only the area between consecutive cutoff values. It will not include the area between the facility and any smaller cutoffs. For example, if you create 5- and 10-minute service areas, the 5-minute service area polygon will include the area reachable in 0 to 5 minutes, and the 10-minute service area polygon will include the area reachable in 5 to 10 minutes. This is the default.
  • DISKSEach polygon will include the area reachable from the facility up to the cutoff value, including the area reachable within smaller cutoff values. For example, if you create 5- and 10-minute service areas, the 10-minute service area polygon will include the area under the 5-minute service area polygon.
String
polygon_trim_distance
(Optional)

The service area polygon trim distance. The polygon trim distance is the distance the service area polygon will extend from the road when no other reachable roads are nearby, similar to a line buffer size. This is useful when the network is sparse and you don't want the service area to cover large areas where there are no features.

This parameter includes a value and units for the distance. The default value is 100 meters.

Linear Unit
exclude_sources_from_polygon_generation
[exclude_sources_from_polygon_generation,...]
(Optional)

The network dataset edge sources that will be excluded when generating service area polygons. Polygons will not be generated around the excluded sources, even though they are traversed in the analysis.

Excluding a network source from service area polygons does not prevent those sources from being traversed. Excluding sources from service area polygons only influences the polygon shape of the service areas. To prevent traversal of a given network source, you must create an appropriate restriction when defining your network dataset.

This is useful if you have some network sources that you don't want to be included in the polygon generation because they create less accurate polygons or are inconsequential for the service area analysis. For example, while creating a walk-time service area in a multimodal network that includes streets and metro lines, you should choose to exclude the metro lines from polygon generation. Although the traveler can use the metro lines, they cannot stop partway along a metro line and enter a nearby building. Instead, they must travel the full length of the metro line, exit the metro system at a station, then use the streets to walk to the building. It would be inaccurate to generate a polygon feature around a metro line.

This parameter is not available when the output geometry types do not include polygons, there are less than two edge sources in the network, the network data source is an ArcGIS Online service, or the network data source is a service on a version of Portal for ArcGIS which does not support excluding sources.

String
accumulate_attributes
[accumulate_attributes,...]
(Optional)

A list of cost attributes to be accumulated during analysis. These accumulated attributes are for reference only; the solver only uses the cost attribute used by your designated travel mode when solving the analysis.

For each cost attribute that is accumulated, a Total_[Impedance] property is populated in the network analysis output features.

This parameter is not available if the analysis layer is not configured to output lines, the network data source is an ArcGIS Online service, or the network data source is a service on a version of Portal for ArcGIS that does not support accumulation.

String

Derived Output

NameExplanationData Type
out_network_analysis_layer

The newly created network analysis layer.

Network Analyst Layer

Code sample

MakeServiceAreaAnalysisLayer example 1 (Python window)

Execute the tool using only the required parameters.

network = "C:/Data/SanFrancisco.gdb/Transportation/Streets_ND"
arcpy.na.MakeServiceAreaAnalysisLayer(network, "FireStationCoverage")
MakeServiceAreaAnalysisLayer example 2 (Python window)

Execute the tool using all parameters.

network = "C:/Data/Paris.gdb/Transportation/ParisMultimodal_ND"
arcpy.na.MakeServiceAreaAnalysisLayer(network, "WarehouseCoverage",
                                "Driving Time", "FROM_FACILITIES", [5, 10, 15],
                                "1/1/1900 9:00 AM", "UTC", "POLYGONS",
                                "STANDARD", "DISSOLVE", "RINGS", "100 meters",
                                ["Metro_Lines", "Transfer_Stations",
                                "Transfer_Street_Station"], ["Meters", 
                                "DriveTime"])
MakeServiceAreaAnalysisLayer example 3 (workflow)

The following stand-alone Python script demonstrates how the MakeServiceAreaAnalysisLayer tool can be used to generate a 1-, 2-, and 3-minute service area around fire stations.

# Name: MakeServiceAreaAnalysisLayer_Workflow.py
# Description: Generate 1-,2-,3- minute service areas around fire stations and
#              save the results to a layer file on disk. The service area
#              polygons can be used to visualize the areas that do not have
#              adequate coverage from the fire stations
# Requirements: Network Analyst Extension

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

try:
    #Check out Network Analyst license if available. Fail if the Network Analyst license is not available.
    if arcpy.CheckExtension("network") == "Available":
        arcpy.CheckOutExtension("network")
    else:
        raise arcpy.ExecuteError("Network Analyst Extension license is not available.")
    
    #Set environment settings
    output_dir = "C:/Data"
    #The NA layer's data will be saved to the workspace specified here
    env.workspace = os.path.join(output_dir, "Output.gdb")
    env.overwriteOutput = True

    #Set local variables
    input_gdb = "C:/Data/SanFrancisco.gdb"
    network = os.path.join(input_gdb, "Transportation", "Streets_ND")
    layer_name = "FireStationCoverage"
    travel_mode = "Driving Time"
    facilities = os.path.join(input_gdb, "Analysis", "FireStations")
    output_layer_file = os.path.join(output_dir, layer_name + ".lyrx")

    #Create a new service area layer. We wish to generate the service area
    #polygons as rings, so that we can easily visualize the coverage for any
    #given location. We also want overlapping polygons so we can determine the
    #number of fire stations that cover a given location. We will specify these
    #options while creating the new service area layer.
    result_object = arcpy.na.MakeServiceAreaAnalysisLayer(network, layer_name,
                                    travel_mode, "FROM_FACILITIES", [1, 2, 3],
                                    polygon_detail="HIGH",
                                    geometry_at_overlaps="OVERLAP",
                                    geometry_at_cutoffs="RINGS")

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

    #Get the names of all the sublayers within the service area layer.
    sublayer_names = arcpy.na.GetNAClassNames(layer_object)
    #Stores the layer names that we will use later
    facilities_layer_name = sublayer_names["Facilities"]

    #Load the fire stations as facilities using default field mappings and
    #default search tolerance
    arcpy.na.AddLocations(layer_object, facilities_layer_name, facilities, "",
                                                                            "")

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

    #Save the solved service area layer as a layer file on disk
    layer_object.saveACopy(output_layer_file)

    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 occurred on line %i" % tb.tb_lineno)
    print(str(e))
MakeServiceAreaAnalysisLayer example 4 (workflow)

This example shows how to create service areas around facilities for multiple times of the day as well as how to port fields from the input features to the output features and append output polygons to an existing feature class.

Legacy:

The GetNASublayer function can be used to retrieve the sublayers of a network analysis layer. It was introduced in ArcGIS Pro 2.7. In prior software versions, the best way to retrieve a sublayer object of a network analysis layer was to use the listLayers method of the network analysis Layer object using the sublayer name as a wildcard.

# Name: MakeServiceAreaAnalysisLayer_Workflow2.py
# Description: Generate 3-minute service areas around fire stations at several
#               times of day to compare coverage differences due to varying
#               traffic conditions. Save the results to a feature class on disk.
# Requirements: Network Analyst Extension

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

try:
    # Check out Network Analyst license if available. Fail if the Network Analyst license is not available.
    if arcpy.CheckExtension("network") == "Available":
        arcpy.CheckOutExtension("network")
    else:
        raise arcpy.ExecuteError("Network Analyst Extension license is not available.")
    
    # Set environment settings
    output_dir = "C:/Data"
    # The NA layer's data will be saved to the workspace specified here
    env.workspace = os.path.join(output_dir, "Output.gdb")
    env.overwriteOutput = True

    # Set local variables
    input_gdb = "C:/Data/SanFrancisco.gdb"
    network = os.path.join(input_gdb, "Transportation", "Streets_ND")
    layer_name = "FireStationCoverage"
    out_featureclass = os.path.join(output_dir, "Output.gdb",
                                                        "FireStationCoverage")
    travel_mode = "Driving Time"
    facilities = os.path.join(input_gdb, "Analysis", "FireStations")
    times_of_day = [datetime.datetime(2014, 9, 25, 7, 0, 0),
                    datetime.datetime(2014, 9, 25, 12, 30, 0),
                    datetime.datetime(2014, 9, 25, 17, 30, 0),
                    datetime.datetime(2014, 9, 25, 21, 0, 0)]

    # Create a new service area layer.
    result_object = arcpy.na.MakeServiceAreaAnalysisLayer(network, layer_name,
                                                travel_mode, "FROM_FACILITIES",
                                                [3], polygon_detail="HIGH",
                                                geometry_at_overlaps="OVERLAP")

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

    # Get the names of all the sublayers within the service area layer.
    sublayer_names = arcpy.na.GetNAClassNames(layer_object)
    # Stores the layer names that we will use later
    facilities_layer_name = sublayer_names["Facilities"]
    polygons_layer_name = sublayer_names["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(layer_object, facilities_layer_name,
                                                    "FireStationID", "TEXT")
    field_mappings = arcpy.na.NAClassFieldMappings(layer_object,
                                                    facilities_layer_name)
    field_mappings["FireStationID"].mappedFieldName = "FireStationID"

    # Load the fire stations as facilities.
    arcpy.na.AddLocations(layer_object, facilities_layer_name, facilities,
                            field_mappings, "")

    # Add fields to the output Polygons sublayer for later use.
    arcpy.na.AddFieldToAnalysisLayer(layer_object, polygons_layer_name,
                                        "FireStationID", "TEXT")
    arcpy.na.AddFieldToAnalysisLayer(layer_object, polygons_layer_name,
                                        "TimeOfDay", "TEXT")

    # Get sublayers to work with later
    facilities_sublayer = arcpy.na.GetNASublayer(layer_object, "Facilities")
    polygons_sublayer = arcpy.na.GetNASublayer(layer_object, "SAPolygons")

    # Get the Service Area Layer's solver properties. This can be used to
    # set individual properties later without re-creating the layer.
    solver_properties = arcpy.na.GetSolverProperties(layer_object)

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

        print("Calculating service area for time of day: ", t)

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

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

        # Transfer the FireStationID field from the input Facilities to the
        # output Polygons
        arcpy.management.AddJoin(polygons_sublayer, "FacilityID",
                                        facilities_sublayer, "ObjectID")
        # The joined fields are qualified by the feature class name of the joined
        # table, so determine the feature class names
        field_qualifier_pol = os.path.basename(polygons_sublayer.dataSource)
        target_field_name = "%s.FireStationID" % field_qualifier_pol
        field_qualifier_fac = os.path.basename(facilities_sublayer.dataSource)
        expression = "!%s.FireStationID!" % field_qualifier_fac
        arcpy.management.CalculateField(polygons_sublayer, target_field_name,
                                        expression, "PYTHON")
        arcpy.management.RemoveJoin(polygons_sublayer)

        # Populate the TimeOfDay field in the output feature class
        expression = '"' + str(t) + '"'
        arcpy.management.CalculateField(polygons_sublayer, "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(out_featureclass):
            arcpy.management.CopyFeatures(polygons_sublayer, out_featureclass)
        else:
            arcpy.management.Append(polygons_sublayer, out_featureclass)

    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 occurred on line %i" % tb.tb_lineno)
    print(str(e))

Environments

Licensing information

  • Basic: Yes
  • Standard: Yes
  • Advanced: Yes