From ArcMap ModelBuilder to Python Script - a Brief Demo

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is dr. Daniel young University in the Department of Civil and environmental engineering this is a presentation on how to build a simple model in arcmap 10.1 and how to export that model into a Python script and then edit the Python script for use to begin I will create a new model and I'll do that by going to arc catalog looking in my toolboxes right clicking and adding a new a new toolset I will call that after myself and with within that toolset I can right click and add a new model so you may have built models in the past so this might be familiar to you we'll start by renaming this model I'm going to call this cities near rivers because it's good this will be a simple model that will find cities that happen to be near rivers in the United States cities near rivers model will be the name of the model and label notice I can use spaces in the label the description is this model finds cities within ten miles of rivers in the US and save it alright so far it's not a very exciting model but that's okay the next step is to bring some data into my model I will bring in a cities shape file for the United States and river shape file for the United States and now I will hook them together with some analysis so we'll start by making a 10-mile buffer around the rivers and I'll do that by linking the rivers shape file into a buffers tool from arctoolbox I will open the buffers tool and set the distance for the buffer to be ten miles the output of this is a polygon shape file with it which represents a 10-mile buffer around each River segment next I need to find the intersection between the cities and rivers polygon shapefile and I will do this with the intersect tool which I can connect here to both the cities shape file points and the river shape file of polygons now when this intersect runs the output should be all of the point locations of cities that are within 10 miles of a river there are actually other ways to do this there's an easier way using just a select tool but for the purposes of our Python demonstration this is probably a good simple model we've given it a name that save it one more thing we'll do is indicate that we would like the city shapefile to be a model parameter the rivers shapefile to be a model parameter and the output feature class to be a model parameter let's also give it a little bit better name I'll call it cities near rivers save our model once again and close it and let's see how it looks when we run it as a model builder model double clicking it here we see our description on the right hand side we see the input shape files here and the output shape file listed and it's going into our default geodatabase which is fine we can run the model hopefully see the results appear in the map just momentarily so these are all of the cities in the United States that are within ten miles of River if you want to see the original data we can bring in our city shapefile and you'll see that there are certainly more cities that are not near rivers okay so we know the model runs and that's always a good first step before moving your model into Python it's good just to make sure that you've got your logic right theoretically your model will be a lot more complicated than the one we just did you can test it in sections make sure it works properly what we'll do now is open the model and go to the model menu item and from here we can choose export to Python script at this point we have to supply a name cities near rivers py is a good name for a Python script and it is done what arcmap is done is taken that model and converted it into this Python file here which I will show you in a moment before we do will close the model and now we need to actually bring that Python script into arcmap to make it nice and clean and usable inside arcmap to do that I can go back to my catalog I can right click on my little toolset and click add a script so now I've brought I'm starting a new script this is a short wizard here that will need to go through a couple of steps I'll call this the city's near rivers script and go to a nice clean label cities near rivers script notice we can use spaces and label this is a script that finds cities within 10 miles of a river in the US now the most important part is on the next page we click Next and browse to the actual Python file that was made for us click Next again of course you could have written that Python file by hand or other ways but we have it nicely here from the model builder export now the next screen asks you what are the model parameters remember in model builder when we simply clicked on the model it gave the user a display of inputs and outputs we want the same thing to happen with our script so I'm going to create an input called cities and specify its type to be shape file you just do this by simply clicking in the boxes so I'll click here and I want an input called rivers and I will choose it also to be a shape file and finally notice that for both of these they default to direction input type required well the third one we will do is cities near rivers this would be an output I like working with shape files so we'll just choose shapefile again for the output type and on this one we need to go to direction and change it to be output it should also be required and click finish okay so what we've just done is set up the user interface for our for our script we can see that user interface by simply double-clicking on the script icon here there are the 2 input cities and rivers and the output I made cities near rivers and the description well we aren't going to run the script just yet because arcmap does a pretty good job of creating a default script we haven't even looked at it yet there going to be some problems in that script we need to go see what they are do a little bit of editing before we actually try running it so I'll close the user interface and let's get to the script itself so here it is I could just open it in a text editor rather what I'm going to do is right click on the script and choose the edit menu and this will bring something called the Python shell and its small editor that lets us actually edit the Python script so let's take a look at what was created by arc map automatically a little bit of comments at the top we have to import the arc PI module and then we do a few things inside here such as get a couple of the parameters here's the input parameter 1 and R and 2 these are identified by index 0 and 1 and 2 but they're actually parameters 1 2 and 3 the first two being our inputs of the input city and rivers and the output they're lumping the output of the these new rivers we are going to create a small local variable called the buffer and then we just run it here's the buffer analysis this is the geoprocessing tool right here but for analysis that we run another one the intersect analysis and get the result you would hope this would just work sometimes I'm sure these work but not usually for me I'm going to do a little bit of editing to this before we actually run it the first thing I'm going to do is set the environment workspace to be the folder where my data are this allows me to reference data without having to give the full path later on next thing I'm going to do is set an environment variable which is override output I'll set that to true which means every time you create a new data set go ahead and overwrite the output data don't crash or give me problems all right so now the script arguments I've pulled the first one into city shape that's great I can simplify the case where it can't find cities shape and just tell it look if somebody doesn't provide the input then just use cities SHP and you'll find it by the way in my workspace next I'm going to do the same with the rivers I'll just clean this up a little bit and say you know what its rivers that shape it already exists in that workspace if the user doesn't provide it of course we said that's required parameters so the user will always have to provide it then the output cities near rivers is input parameter index two which is number three in our list and give it a nice clean name cities new rivers rivers SHP I'd like that to be an output shapefile okay intermediate variable rivers underscore buffer needs to be changed a little bit we need to give it a nice name what it's trying to do right here is just use the same name as the input River shape file this it's this line here that would cause arcmap to crash if you use the default values so we'll give it a a rivers buffer SHP file name that will also be found which should be created in this workspace so that one change there is probably really the only one you would need to do to keep this from crashing out of the box so we've made that change next we need to step down to the buffer line and double check this make sure it's going to work just fine looks like it's good it's bringing in the river shape file and the the output will be rivers buffer - I don't mind that variable name for that and it's going to use a ten mile buffer with rounded end caps next we're going to do the intersection the intersection before we do this I'd like to set up a name for our clip features now follow me here this is a new variable I just created I'm bringing into it the rivers on the rivers buffer and the city's shape file so the city shapefile is defined here rivers buffer is here this won't work because here it's got doesn't have the number two so I'll delete the number two and the number two so now I've got rivers buffer rivers buffer and rivers buffer it's going to be the name basically what I'm doing setting up a string that has the name of the two files that we're going to be intersecting separated by a semicolon this is a string that arc Maps intersect function needs in order to work arcmap is trying to do that automatically for us by creating a long string here and I don't know just doesn't really work that well for me so I'm going to delete the default one they gave and instead we'll just put our clip features in that spot so now the input variable is clip features the output variable is city's new rivers and we're going to yeah intersect everything it should be good to go so the result of this should be a city's new river shapefile that we can then add to arcmap let's test it okay we'll test it by saving the file we just edited close the file go back to the catalog where I've been working and let's try running it here we'll run it by double-clicking the tool excuse me the script just sort of a script tool is really what it is we'll select our city's input file we'll select our rivers input file we'll double check the output I really don't want that to go to geo database rather just go ahead and set it right in my data USA folder and we'll give it a name cities near rivers stuck to SHP there's my output click OK and let it run well the script just completed and it created the city's new rivers shape file and it added it to the map what more could you ask for so that's the end of my quick demonstration on how to make a model export it to a script edit the script to be a little more functional than the default script and then run the script with parameters I hope you have enjoyed this tutorial good luck as you work on your projects
Info
Channel: Dan Ames
Views: 50,346
Rating: 4.9133859 out of 5
Keywords: ArcMap, ModelBuilder, Python
Id: lQFBuhtZD6M
Channel Id: undefined
Length: 14min 38sec (878 seconds)
Published: Wed Jan 09 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.