Network Analyst Closest Facility Paris Script

238
7
04-15-2022 06:55 PM
IanBarr
New Contributor II

Full disclosure: I've successfully run the tutorial Paris code for the network analyst extension creating a closest to facility network once.

Upon retrying it and even starting fresh with new code and .gdb the code fails wit this error message...

IanBarr_0-1650073840129.png

I've had the same exact experience with my own personal .gdb. I was able to run the code successfully once and when I went back the  next day to run it again the code fails with the  same exact error message as above. Is there a bug with the Paris.gdb tutorial or something larger going on because I've recreated both the tutorial and my own dataset multiple times and cannot seem to get the script to run successfully anymore. Any and all ideas are appreciated as the final weeks of my semester approach.

0 Kudos
7 Replies
MelindaMorang
Esri Regular Contributor

Hello, Ian.

I suspect the issue here is that your script needs to set arcpy.env.workspace to a valid file geodatabase where the NA layer's analysis data will be placed.  You can read some explanation of why this is necessary in ArcGISPro here: https://pro.arcgis.com/en/pro-app/latest/arcpy/network-analyst/migratingfrom10xarcpynetworkanalyst.h...

In general, we now recommend that you do not use network analysis layers in Python scripts in ArcGIS Pro, as we now have available the arcpy.nax Python module, which makes Network Analyst workflows in Python easier.  Learn more here: https://pro.arcgis.com/en/pro-app/latest/arcpy/network-analyst/performing-network-analysis.htm

0 Kudos
IanBarr
New Contributor II

Hi thank you for your response. I have tried to input the new .nax package as referenced and i keep receiving an error message for not having driving time inputted though it is clearly in the code. 

0 Kudos
MelindaMorang
Esri Regular Contributor

I'm sorry you're still having trouble.  I got notifications for several posts in this thread, some of which seemed like they included your code. However, it looks like you deleted them.

I'm not really sure how to answer your remaining question, as it's a little vague.  Can you post your code again and the exact error text?

0 Kudos
IanBarr
New Contributor II
# Name: Solve_Workflow.py
# Description: Solve a closest facility analysis to find the closest warehouse
# from the store locations and save the results to a layer file on
# disk.
# 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/NationalGridCF.gdb"
network = os.path.join(input_gdb, "Transportation", "NationalGrid_ND")
layer_name = "ClosestFacility"
travel_mode = "Driving Time"
facilities = os.path.join(input_gdb, "Analysis", "Facilities")
incidents = os.path.join(input_gdb, "Analysis", "Incidents")
output_layer_file = os.path.join(output_dir, layer_name + ".lyrx")

# Create a new closest facility analysis layer.
result_object = arcpy.na.MakeClosestFacilityAnalysisLayer(network,
layer_name, travel_mode,
"FROM_FACILITIES",
number_of_facilities_to_find=1)

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

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

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

# Load the stores as Incidents. Map the Name property from the NOM field
# using field mappings
field_mappings = arcpy.nax.NAClassFieldMappings(layer_object,
incidents_layer_name)
field_mappings["Name"].mappedFieldName = "NOM"
arcpy.na.AddLocations(layer_object, incidents_layer_name, incidents,
field_mappings, "")

# Solve the closest facility layer
arcpy.na.Solve(layer_object)

# Save the solved closest facility 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))

IanBarr_0-1650550381934.png

 



0 Kudos
MelindaMorang
Esri Regular Contributor

For this one, the problem is that it cannot set the environment workspace to the designated file gdb.

 

# 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")

Does C:/Data/Output.gdb exist?  If not, this is your problem.  You must set the workspace to a geodatabase that exists. 

0 Kudos
IanBarr
New Contributor II
import arcpy
nd_path = "C:/Data/NationalGridCF.gdb/Transportation/NationalGrid_ND"
nd_layer_name = "National Grid Service Area"

# Create a network dataset layer. The layer will be referenced using its name.
arcpy.nax.MakeNetworkDatasetLayer(nd_path, nd_layer_name)

# Instantiate a ServiceArea analysis object.
service_area = arcpy.nax.ServiceArea(nd_layer_name)

# Get the desired travel mode for the analysis.
nd_travel_modes = arcpy.nax.GetTravelModes(nd_layer_name)
travel_mode = nd_travel_modes["Driving Time"]

# 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

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

# Solve the analysis.
result = service_area.solve()


# Export the results to a feature class. If the analysis failed print all the
# messages.
if result.solveSucceeded:
result.export(arcpy.nax.ServiceAreaOutputDataType.Polygons, output_polygons)
else:
arcpy.AddError("Analysis failed")
# Print all the warning messages.
for message in result.solverMessages(arcpy.nax.MessageSeverity.Warning):
arcpy.AddWarning(message[-1])
# Print all the error messages.
for message in result.solverMessages(arcpy.nax.MessageSeverity.Error):
arcpy.AddError(message[-1])


IanBarr_1-1650550460344.png

 

0 Kudos
MelindaMorang
Esri Regular Contributor

Okay, for this one, the error message tells you exactly what the problem is: "Input network dataset does not have at least one travel mode".  The arcpy.nax solver objects require that the network dataset is configured with travel modes.  Here's some documentation about travel modes and how to create them: https://pro.arcgis.com/en/pro-app/latest/help/analysis/networks/travel-modes.htm

0 Kudos