Looping over a location allocation layer in model builder:

1830
9
Jump to solution
02-25-2019 12:51 AM
LorisH
by
New Contributor

I would like to update the number of facilities to choose in a p-median location allocation model using a for loop. It is possible to define the number (p) of facilities when generating the location allocation layer. In this case, in each iteration the demand and facilities have to be located in the network. I was able to build such a model. Is it possible to update the location allocation layer it self, so that it is not necessary to locate the demand and facilities into the network anymore? Do i have to use the tool „Update Analysis Layer Attribute Parameter“ and how the inputs values then look like?

Thank you!

0 Kudos
1 Solution

Accepted Solutions
MelindaMorang
Esri Regular Contributor

You can't update the Location-Allocation layer itself in Model Builder.  However, this can be done in a python script quite easily using the GetSolverProperties function and then setting the facilitiesToFind property.

If you prefer to use ModelBuilder, you can increase the efficiency of your model by pre-calculating the network locations of your facilities and demand points using the Calculate Locations tool.  This tool defines how the points locate on the network dataset.  Then, when you run Add Locations many times in your model, it doesn't have to calculate the locations again.  Rather, it just uses the network locations stored in fields in the input points, and it should be fast.

View solution in original post

9 Replies
MelindaMorang
Esri Regular Contributor

You can't update the Location-Allocation layer itself in Model Builder.  However, this can be done in a python script quite easily using the GetSolverProperties function and then setting the facilitiesToFind property.

If you prefer to use ModelBuilder, you can increase the efficiency of your model by pre-calculating the network locations of your facilities and demand points using the Calculate Locations tool.  This tool defines how the points locate on the network dataset.  Then, when you run Add Locations many times in your model, it doesn't have to calculate the locations again.  Rather, it just uses the network locations stored in fields in the input points, and it should be fast.

curtvprice
MVP Esteemed Contributor

Melinda, could you put a short python function calling GetSolverProperties inside a Calculate Value tool? 

0 Kudos
MelindaMorang
Esri Regular Contributor

No, I don't think that would work.  In this case, the user knows the value he wants but needs a way to inject that value into the Network Analyst layer's properties.  We just don't provide that capability in ModelBuilder.

0 Kudos
curtvprice
MVP Esteemed Contributor

Calculate Value gives you full access to Python, I don't see why this would not work, as I said I've used it with arcpy.mapping (also not directly supported by any GP tools) and it has worked great.

0 Kudos
AndreasLimacher
New Contributor

Hi Melinda,

Although this thread is quite old, I wanted to follow-up on it. I guess what Curt tried to say, is if you could provide a sample code. I've tried what you said with a script, imported that script into ModelBuilder and then connected the output to a Solve Tool. That did not work for me.

I first tested the script on its own. The process doesn't cause any error, but when accessing the properties of the Location-Allocation Layer through the top bar, nothing has changed.

Here's my code and below the Script Property Settings:

import arcpy, os

#Get Parameter values
LALayer = arcpy.GetParameter(0)
FacilityNum = arcpy.GetParameter(1)
Cutoff = arcpy.GetParameter(2)
Type = arcpy.GetParameterAsText(3)
TMode = arcpy.GetParameter(4)

#Update Location-Allocation Layer
p = arcpy.na.GetSolverProperties(LALayer)
p.applyTravelMode(TMode)
p.facilitiesToFind = FacilityNum
p.impedance = "Length"
p.impedanceCutoff = Cutoff
p.problemType = Type

#Set parameters
arcpy.SetParameter(5, LALayer)

0 Kudos
MelindaMorang
Esri Regular Contributor

I think the reason this doesn't work is that the solver properties get updated only within the context of this tool and do not manage to update the original layer in the tool output.  It is a longstanding issue.  I believe it will be fixed in the forthcoming Pro 3.0 release after some rearchitecture we did.

However, if you are capable of Python scripting, this is not the best way to solve the problem.  We no longer recommend using network analysis layers in Python scripts since we have introduced the arcpy.nax Python module, which provides easy-to-use Python classes for network analysis: https://pro.arcgis.com/en/pro-app/latest/arcpy/network-analyst/performing-network-analysis.htm

If you're capable of writing Python scripts, you're encouraged to just do your whole workflow using this new module. It should be quite easy to update the desired properties and solve iteratively.  You can wrap up your script in a geoprocessing script tool if you'd like.

0 Kudos
AndreasLimacher
New Contributor

First of all, thank you a lot Melinda for your support!

Alright I see, so I would need to write all processes that fall into that iteration, into the python script. Which I guess wouldn't be too difficult, but I could also just spend that time on doing it manually 😄

One more question though: I read that the arcpy.nax module is also used for API for ArcGIS Pro. It seems that for a later project, when I will no longer have access to ArcGIS Pro, but only to a Developers Essentials account, I would need to do some more network analysis on a pay-as-you-go basis. Will I be using the same module, or does that differ because I will be accessing web services?

0 Kudos
MelindaMorang
Esri Regular Contributor

All the advice in this thread assume that you have ArcGIS Pro installed and are using Pro's installed version of Python.  The arcpy module in general assumes this.

I'm not really the right person to ask about a Developers Essentials account, as I work exclusively on ArcGIS Pro.  But I believe if you at some point will no longer have ArcGIS Pro, you will need to use the ArcGIS API for Python, which is a completely separate thing from arcpy.  With this API, you can use network analysis services, but the syntax is all different from what we've been talking about here in this thread.

Please take a look at that documentation, and if you have further questions, I suggest you post them in a new question in a different area of the Esri Community so you can be sure the right people will see them and be able to answer them.

AndreasLimacher
New Contributor

Thank you Melinda, this has been very helpfull! 

0 Kudos