I am using ArcGIS Pro 2.9 with an advanced single use license and a locally saved network dataset.
I am attempting to write a standalone script that takes as its input an arbitrary set of origin-destination pairs and produces as its output the travel distance (e.g., driving distance in miles) and time (e.g., driving distance in minutes) for each pair. The script creates and subsequently solves a route analysis layer using the route solver method.
The core logic of the script is as follows:
(1) load the set of origin-destination pairs from an input csv file as a point feature class
arcpy.management.XYTableToPoint(
r"[input csv file]"
,'[output (origins|destinations) layer name]'
,'[input (origins|destinations) longitude field]', '[input (origins|destinations) latitude field])'
(2) create a new Route_ID field that concatenates the origin ID and destination ID for each pair with a pipe delimiter
arcpy.management.CalculateField(
'[(origins|destinations) layer name]'
,'Route_ID'
,field_type = 'TEXT'
,expression = '[concatenation expression]'
,expression_type = 'ARCADE')
(3) define a function that loads the origins and destinations for each chunk and then solves for the routes
odp_FieldMappings = route.fieldMappings(
arcpy.nax.RouteInputDataType.Stops
)
odp_FieldMappings['RouteName'].mappedFieldName = 'Route_ID'
def solve_chunk(chunk):
# Define chunk start and end
chunk_index = chunk - 1
odp_chunk_start = (chunk_index * ChunkSize) + 1
odp_chunk_end = odp_chunk_start + (ChunkSize - 1)
# Load chunk origins
arcpy.conversion.FeatureClassToFeatureClass(
'[origins layer name]'
,gdb
,'[origins chunk layer name]'
,f'OBJECTID >= {odp_chunk_start} \
and \
OBJECTID <= {odp_chunk_end}')
route.load(
arcpy.nax.RouteInputDataType.Stops
,'[origins chunk layer name]'
,odp_FieldMappings
,append=False)
# Load chunk destinations
arcpy.conversion.FeatureClassToFeatureClass(
'[destinations layer name]'
,gdb
,'[destinations chunk layer name]'
,f'OBJECTID >= {odp_chunk_start} \
and \
OBJECTID <= {odp_chunk_end}')
route.load(
arcpy.nax.RouteInputDataType.Stops
,'[destinations chunk layer name]'
,odp_FieldMappings
,append=True)
# Solve
return (chunk, route.solve())
(4) feed this function into a multiprocessing pool to generate the solve results
if __name__ == '__main__':
with mp.Pool() as pool:
results = pool.map_async(solve_chunk, chunks)
However, frustratingly, I just can't seem to get the multiprocessing to work. When I attempt to call the solve_chunk function within map_async(), I get a 000732 error when loading the chunk origins:
ERROR 000732: Input Features: Dataset [origins layer name] does not exist or is not supported
And sure enough, when I check my project's geodatabase, I don't see the expected feature classes.
I've tried multiple ways without success to fix this issue, including using different arcpy functions (e.g., arcpy.management.MakeFeatureLayer). But nothing seems to work. I've used ArcGIS for years, but running scripts in a standalone environment rather than via the Python window is new to me, as is multiprocessing.
Can anyone help?
P.S., here are my environment settings:
ArcGIS_ProjFolder = aprx.homeFolder
m = aprx.listMaps('Map')[0]
gdb = aprx.defaultGeodatabas
arcpy.env.workspace = gdb
arcpy.env.overwriteOutput = True