0

I'm trying to create a series of Service Area polygons within a for loop. It performs OK creating the Service Area layer and loading Facilities, but for some features it begins running the arcpy.na.Solve function but never completes, just gets stuck running (no error, no crash). When done manually within Arc, the process is on the slow side but still works OK.

Strangely, on other features it works fine, there doesn't seem to be any difference between the features which do and don't work, and it isn't related to size (sometimes has been left running for hours with only 8 features, but on others works fine when there's 100+).

Is there a way to improve it?

#Import system modules
import arcpy
from os import path
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Network")


# When run from within Arc...
streets = arcpy.GetParameter(0)
rls = arcpy.GetParameterAsText(1)
selection_field = arcpy.GetParameterAsText(2)
outGDB = arcpy.GetParameterAsText(3)

#Set environment settings
arcpy.env.workspace = outGDB


# Create list of selection values
#   This is required to process in batches (or else Arc falls over)
selection_values = []
with arcpy.da.SearchCursor(rls,"{}".format(selection_field)) as cursor:
  for row in cursor:
    selection_values.append(row[0])


# Loop through each selection value
for selection_value in selection_values:

    outNALayerName = "ServiceAreas_small"
    impedanceAttribute = "minutes"

    #Create a new SERVICE AREA layer.
    outNALayer = arcpy.na.MakeServiceAreaLayer(streets, outNALayerName,
                                  impedanceAttribute, "TRAVEL_FROM", "10",
                                  "DETAILED_POLYS", "NO_MERGE", "RINGS",
                                  polygon_trim = "TRIM_POLYS", poly_trim_value = 100)

    # Get the layer object from the result object. The service layer can now bereferenced using the layer object.
    outNALayer = outNALayer.getOutput(0)

    selection_code = selection_value.split(" ")[1]
    outFC = "ServiceArea_smaller_{}".format(selection_code)

    # Select by relevant selection value
    arcpy.SelectLayerByAttribute_management(rls, "NEW_SELECTION", """{0} = '{1}'""".format(selection_field,selection_value))

    # Get the names of the SUBLAYERS within the service area layer.
    subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
    facilitiesLayerName = subLayerNames["Facilities"]
    polygonsLayerName = subLayerNames["SAPolygons"]

    # LOAD THE FACILITIES using default field mappings and default search tolerance
    arcpy.na.AddLocations(outNALayer, facilitiesLayerName, rls, "", "")

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


    # Get an object of the POLYGON layer
    PolygonsSubLayer = arcpy.mapping.ListLayers(outNALayer, polygonsLayerName)[0]

    # Create FEATURE CLASS
    arcpy.management.CopyFeatures(PolygonsSubLayer, outFC)


arcpy.SelectLayerByAttribute_management(rls, "CLEAR_SELECTION")

1 Answer 1

0

On a closer investigation it was just running very slowly and evident when performed within Arc too, was fixed by re-creating the Network Dataset layer and ticking on 'create service area index' as this wasn't done initially, was much faster after that...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.