1

I am using Python code to calculate the closest facilities. Here, first I create a random point dataset. Then I use that random point dataset as an incident dataset when I'm creating the closest facility dataset.

My code is a loop. There I am trying to create 100 random datasets and the closest facilities from each random dataset to the school dataset. The code takes a lot of time to run. How can I increase the speed of this?

# Import arcpy module
import arcpy
import os, time, sys, subprocess
from arcpy import env

# Local variables:
Default_gdb = "C:\\Users\\Madusha\\Documents\\ArcGIS\\Default.gdb"
ms_gdb = "D:\\Crime\\python\\ms.gdb"
constrain = r"D:\Crime\python\chicago_c.shp"
nds = os.path.join(ms_gdb, "Road", "Road_ND")
Chicago_schools = r"D:\Crime\python\School2.shp"
arcpy.env.overwriteOutput = "True"
arcpy.CheckOutExtension("Network")
Measurements_Units = "Meters"
NumFacilitiesToFind=1


inNetworkDataset = nds
inFacilities = Chicago_schools
outGeodatabase =ms_gdb
measurement_units = Measurements_Units

for n in range(1, 101):
    print 'Processing {0} of 100'.format(n)
    a=arcpy.CreateRandomPoints_management(ms_gdb, 'dataset_{}'.format(n) , constrain, constrain, "FID_fishne", "SUM_MEAN_c", "POINT", "0")
    inIncidents = a
    outRoutes = 'NNRoutes2012_{}'.format(n)
    outDirections = 'NNDirections2012_{}'.format(n)
    outClosestFacilities = 'ClosestFacilities2012_{}'.format(n)
    arcpy.na.FindClosestFacilities(inIncidents, inFacilities, measurement_units,
                                    inNetworkDataset, outGeodatabase, outRoutes,
                                    outDirections, outClosestFacilities,
                                    Number_of_Facilities_to_Find=NumFacilitiesToFind)
    print "Script completed successfully"
3
  • 4
    You could try converting your for loop into a function. Then use this function and create 100 simultaneous multiprocessing tasks using the multiprocessing python library.
    – Carlos
    Commented Jun 17, 2021 at 9:33
  • Simultaneous execution of high I/O tasks can often take longer than serial execution (two threads may time some time, but three might take longer). There isn't any magic to performance optimization, just hard work (using the data on the platform to identify costs and experience to explore alternatives)
    – Vince
    Commented Jun 17, 2021 at 11:38
  • 2
    Use in_memory where ever possible, instead of writing to disk. In your case, I think the only place you could use memory is with CreateRandomPoints. For JUST that tool, update ms_gdb to in_memory. It wont save a lot of time, but it'll help a bit.
    – KHibma
    Commented Jun 17, 2021 at 11:59

1 Answer 1

1

From the help https://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/create-random-points.htm

You can create 100 random points by specifying the parameter.

CreateRandomPoints(out_path, out_name, {constraining_feature_class}, {constraining_extent}, {number_of_points_or_field}, {minimum_allowed_distance}, {create_multipoint_output}, {multipoint_size})

Then use the output as the input for the network analysis without looping 100 times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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