I'd like to automate the following tasks completely in Python. Can somebody provide me with some introductory code ?
1) Define a street network in a GDB ( actually also create the street features in Python based on other data)
2) Take that street network as the network analysis base
3) Define a start point and end point of a route ( do these points have to be exactly ON the network line ?)
4) Run the analysis
5) Print out in the console how long it would take to walk that distance (at a certain speed)
A very simple example with 3 imaginary streets would be very helpful.
Any input welcome!
have you examined the code snippets in the help files for the process you wish to use? Have you done this manually? If so you can copy the Results window output and generate a shell of a script. Have you tried modelbuilder to generate a workflow ? That can be used to generate a script.
There are some examples of how to do this here:
For 1), what do you mean by "define a street network in a GDB"? If you have an existing network dataset, you can easily use it in python for your analysis. However, it is not possible to create a network dataset (meaning define its source features and set up all its properties) in python.
The rest of your steps can definitely be done in python. Check out the code samples on this page: ArcGIS Help (10.2, 10.2.1, and 10.2.2).
There are many code samples like that throughout the Network Analyst tool documentation which should help get you started.
Thanks for the inputs.
Well, I think I have to start at the very beginning with every step individually, but so many questions pop in my mind now. I have some experience in Python, but am just trying to get into arcPy.
By 'define a street network in a GDB' I mean:
- Create an empty GDB
- Create a polyline FC
- Fill in the street polylines with their attribution.
But that is just a subtask of my workflow.
So what I basically would like to do in one python session :
- define the input streets
- create the street network metadata ('network dataset')
- define e.g. a start and end point by coordinates
- run the simulation
- get the results (e.g. a polyline representing the best path and save it in an other GDB)
- close the session
Is that second step ('create network dataset') not possible to do in python ?
Correct, it's the 'create network dataset' step that is not possible in python.
Why do you need to re-create the network dataset every time? If you just want to solve a route between a different start and end point, you can re-use the network dataset, especially if the points are all within the same geographic area.
I need a different street network every time I run the simulation because it may potentially change every time (design iterations). I need a FULL simulation setup every time.
Can I use Python to set up a GP Tool Model to set the simulation up, instead of running everything purely in Python ?
Oh, I see. Yes, you can use python for every step except the network dataset creation and network dataset property editing. I don't know what you mean by "Can I use Python to set up a GP Tool Model to set the simulation up". It's also not possible to alter network dataset properties in Model Builder. You could possibly use ArcObjects to do that, and then you could call your ArcObjects executable code from within a python script.
If the only thing that's changing are the street features referenced by the network dataset, you might be able to automate the whole thing. Before starting your simulations, you could create your network dataset and get all the attributes and other properties defined. Then, during each run of your simulation, if you edit the street features, you can just run the Build tool on the network dataset so that it incorporates the new edits. If, on the other hand, you actually need to edit the network's attributes or connectivity properties or something, you will not be able to automate that with python.
Have you created a network dataset before? If not, I think it might be best if you run through the network dataset creation tutorial (ArcGIS Help (10.2, 10.2.1, and 10.2.2) ) first before you continue planning your simulation. This will give you some idea of what's involved and put some of my suggestions above into context.
By 'GP Tool Model' I meant using Model Builder.
Yes, I have run through two tutorials that show how to set up a network dataset and run two different simulations (best path, service area).
It's sad to hear this is not possible in Python. I have only heard of ArcObjects, but I am not a programmer, so I think I would fail on this..
But I am pretty sure I will not need to edit the network itself, just swap the whole dataset with a new one before each sim cycle. This could also be done using a 'prepared' scene. The only thing I want to avoid is to have to manually click things, e.g. opening a scene file and click something. I want to trigger 1 script and get the result.
If I could automate that, that could be good already...