Select to view content in your preferred language

For loop with Closest Facility

04-05-2017 11:51 AM
New Contributor

Hi I'm new to Python and trying to write a script to make my life easier.  I'm hoping to write a code that will allow me to import a shapefile of 10,000 points into "Incidents" and then another another shapefile of 600 points into "Facilities".  What I want to get is the distance from each of those 10,000 points to each of the 600 "Facilities" so basically run 600 different Closest Facility and print the Distance from each point into a table. Do I have to split that shapefile into 600 separate shapefiles or is it possible to loop through them without that?  Like I said I'm very new to Python so any input on the syntax would be much appreciated! Let me know what you guys are thinking.

python help‌ closest facility networkanalyst‌

0 Kudos
4 Replies
MVP Emeritus

I'm tagging Python‌  to get more exposure to your question.  You have this posted in the Network Analyst community, but I'm not sure if you are really trying to create a network or just a near table.

check out the help topics for Near—Help | ArcGIS Desktop and Generate Near Table—Help | ArcGIS Desktop 

Esri Esteemed Contributor

I'm pretty sure you don't need to create a featureclass for each facility. You can create a layerfile in memory looping over the facilities and use that featurelayer with the single facility in "" to set the facilities.

See: ClosestFacilitySolverProperties—Help | ArcGIS Desktop 

Esri Contributor

You should not need to create many different feature classes for your incidents. You could load all 10k points in as incidents, set the defaultTargetFacilityCount  of the ClosestFacilitySolver to <null> (which will have the analysis route from every incident to every facility) or to 600 (the number of your facilities), and run the analysis. 

ClosestFacilitySolverProperties—Help | ArcGIS Desktop 

Closest facility analysis—Help | ArcGIS Desktop  (Check the Facilities to Find parameter)

That kind of analysis would take a long time to run and may fail due to memory constraints. Instead, I would load all your facilities in, and use a loop to load in the first 1k or so (or more, it's up to you) of your incidents in, run the solve, then load the next chunk of the incidents in (so that they overwrite the previous set of incidents), run the solve again and repeat until all the incidents have been run through. Here's an overview of the process I'm thinking of:

1. Set the solver properties, particularly the defaultTargetFacilityCount to either null or the total number of facilities you have

2. Load all the facilities

3. Do whatever else you need for the solver (fieldmappings or whatever)

3. In a for loop:

      a. Use a cursor to grab the first thousand or so (or more, its up to you) records from the incidents shapefile

      b. Load those records as the Incidents

      c. Solve the analysis

      d. Have the loop go back, grab the next thousand or so records, repeat the loading and the solving

You can do the analysis in chunks like that because the route from each incident to each facility is independent of each other. So as long as you have all the facilities loaded in, you can have just a portion of the incidents loaded in at a time, which should help with the speed of the analysis.

New Contributor III

Do you need to see the routing from each facility to each incident, or just get the distances?  If you only need the distances, I'd suggest looking at the Origination-Destination Matrix instead of Closest Facility.  It will automatically get the distances (or times if the network is set that way) from each facility (origination) to each incident (destination), and is much faster than CF.  Actual routes are not rendered, only straight lines, but if all you want is a table, it might be a better option.

0 Kudos