Using Python in ArcGIS Desktop 10

25
Time Caption 0:01 Good afternoon. My name is Michael Rhoades, 0:04 and I'm an instructor with the Educational Services department in Charlotte, North Carolina. 0:09 Joining me today is Bruce Harold who is product manager with the Desktop team in Redlands, California. 0:14 We want to welcome all of you today to this live training seminar. 0:18 Today, we're going to focus on using Python in ArcGIS Desktop 10, 0:22 and we're going to focus on some of the new functions that Python can add to 10, 0:28 so we'll be pointing that out as we go through the presentation. 0:32 We're going to look at some basic benefits and advantages of Python, 0:35 then we're going to talk about the basic Python syntax. For those that may not have any experience with Python 0:41 and don't have really a programming background, 0:44 we're going to discuss some of the basic syntax 0:51 I’m going to show you how you can use that then to automate the geoprocessing tools. 0:56 For those that do have more experience, 1:00 we're going to be talking more about the advanced functions 1:03 So we'll be pointing out some of the advantages that you can gain from running Python scripts, 1:09 some of the more advanced functions that you can perform. 1:14 Each of these topics will be followed by a software demonstration, 1:18 and then we'll turn it over to Bruce who will answer your questions. 1:21 Remember, you can send questions in any time during the presentation. 1:28 Python is an open source, cross-platform (meaning we can run it on Windows or UNIX or Linux) 1:35 fairly easy to use scripting macro-type language, and it's object oriented, and it's freely available (it's open source). 1:45 You can download it from many sites.The idea behind Python is to automate your workflows fairly easily. 1:52 For an example, you might take a Python script that will execute in the middle of the night through a scheduler. 1:58 That script can then go and copy data or convert data in a batch mode 2:03 so take large numbers of files and process those during your times when the system isn't busy. 2:10 What we'll probably be most interested in today in this seminar is the ability to take Python scripts Print Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm 1 trong 25 6/7/2012 11:09 AM

Transcript of Using Python in ArcGIS Desktop 10

Page 1: Using Python in ArcGIS Desktop 10

Time Caption

0:01 Good afternoon. My name is Michael Rhoades,

0:04 and I'm an instructor with the Educational Services department in Charlotte, North Carolina.

0:09 Joining me today is Bruce Harold who is product manager with the Desktop team in Redlands,California.

0:14 We want to welcome all of you today to this live training seminar.

0:18 Today, we're going to focus on using Python in ArcGIS Desktop 10,

0:22 and we're going to focus on some of the new functions that Python can add to 10,

0:28 so we'll be pointing that out as we go through the presentation.

0:32 We're going to look at some basic benefits and advantages of Python,

0:35 then we're going to talk about the basic Python syntax. For those that may not have any experiencewith Python

0:41 and don't have really a programming background,

0:44 we're going to discuss some of the basic syntax

0:51 I’m going to show you how you can use that then to automate the geoprocessing tools.

0:56 For those that do have more experience,

1:00 we're going to be talking more about the advanced functions

1:03 So we'll be pointing out some of the advantages that you can gain from running Python scripts,

1:09 some of the more advanced functions that you can perform.

1:14 Each of these topics will be followed by a software demonstration,

1:18 and then we'll turn it over to Bruce who will answer your questions.

1:21 Remember, you can send questions in any time during the presentation.

1:28 Python is an open source, cross-platform (meaning we can run it on Windows or UNIX or Linux)

1:35 fairly easy to use scripting macro-type language, and it's object oriented, and it's freely available (it'sopen source).

1:45 You can download it from many sites.The idea behind Python is to automate your workflows fairlyeasily.

1:52 For an example, you might take a Python script that will execute in the middle of the night through ascheduler.

1:58 That script can then go and copy data or convert data in a batch mode

2:03 so take large numbers of files and process those during your times when the system isn't busy.

2:10 What we'll probably be most interested in today in this seminar is the ability to take Python scripts

Print

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

1 trong 25 6/7/2012 11:09 AM

Page 2: Using Python in ArcGIS Desktop 10

2:16 and automate the geoprocessing tools. So there are several hundred tools available to you in theArcToolbox.

2:22 We're going to be able to take those tools, put them into a script, and execute them in any order thatyou would like.

2:28 And you're going to find that just some basic understanding of scripts, basic understanding of the toolsin a toolbox,

2:35 you'll be able to automate these things fairly easily.

2:39 Besides automating just the tools in a toolbox, we also have the ability to work with some of the moreadvanced functions.

2:46 In other words, we could…for instance, we could take a table and we can look through that table.

2:52 We can read and write to the table. We can manipulate tabular data.

2:56 You can also manipulate map documents and the layers within the map document.

3:00 This is new at version 10 (the ability). We'll be talking more about this a little bit later. And we couldalso take geometry—

3:06 points, lines, and polygons—and manipulate those. We'll see a couple of examples of how this works

3:10 throughout the seminar.Where do we run Python code from?

3:16 Well, there are a number of places you can type in the code or execute it from.

3:20 One, if you're serious about writing Python scripts, you'll probably want to use some kind of a Pythoneditor.

3:27 The one I have showing on the screen is called PythonWin. This is fairly common.

3:31 But keep in mind, there are many editors available to you. Some are free. Some you can purchase.

3:36 There's one called IDLE that's built into Python. If you want, you can use that one. I use thisPythonWin fairly often.

3:44 The idea of the editor is it's going to give you the ability to type the code in, to enter it, to debug it,

3:50 and also to execute it. When you execute the code, you don't necessarily have to have ArcMap orArcCatalog open.

3:57 They don't have to be running at the time in order to run the code from this editor.

4:02 It can be run stand-alone on its own.You could also run the code from the new Python window.

4:08 This is new at version 10, so in ArcMap, there is a window called the Python window.

4:14 This actually replaces the older command line window that was there in version 9.

4:19 In this window, you can type in individual commands. You can type in Python code within it.

4:24 Some of you might recognize…it looks fairly similar to an interactive window that you have in many ofthe editors.

4:31 We'll be showing some of this a little bit later.You can also take a Python script and add it to thetoolbox as a tool.

4:37 This has a lot of advantages to it. We'll see an example of this later in the demonstration.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

2 trong 25 6/7/2012 11:09 AM

Page 3: Using Python in ArcGIS Desktop 10

4:42 And finally, you can run Python code within the Field Calculator if you want.

4:46 This is new at version 10 (this ability).

4:50 At this point, I'm going to go to ArcMap, and I'm going to show you an example of running an actualscript.

4:55 So to do this, I'm going to open up the Python window we just discussed,

5:00 and I'm going to right-click and load an existing script into this Python window.

5:05 So I have a script that will buffer some roads for me. And we're going talk more details about the codein just a little bit.

5:13 So we won't worry about the actual syntax at this point. We're just simply going to run the code.

5:18 As this is running, it's going to run in the background. This is a new feature in version 10 —

5:23 that we can run any of the tools in a toolbox, or scripts in this case, can be run in the background andas they're running,

5:30 you can continue to do other work within ArcMap. For instance, you could zoom in, zoom out,

5:36 or identify features on the map, open tables up. You see that the script is currently working.

5:42 So what we're doing here with this is we're buffering roads at three different distances,

5:47 and you can see it's running three different buffers. The script's automating this (one right after theother),

5:52 and you can see the results on the screen here.

5:57 I'll open up the table of contents so you can see that it created three new layers

6:02 that we see were automatically added to the table of contents.

6:06 Any time you run code from the Python window, outputs automatically get put into your map for you.

6:12 I'm going to turn these off. Let's talk about some basic Python syntax.

6:22 This is more for the benefit of those of you that may not have a real Python experience.

6:26 What we want to do is get you familiar with what the code looks like so when we see various examples

6:31 (or you can go and read sample code that's built into the Desktop Help),

6:36 you'll have some idea of what that code is doing for you. One thing you'll be seeing very often isvariables.

6:43 A variable contains information. It stores it. For instance,

6:46 we have a variable here called first that's equal to a string called Bruce.

6:50 Another one called last that's equal to a text string called Harold,

6:54 and you'll see that we have quotes around these text strings.

6:57 This is what makes it a string, and you can use either single or double quotes like you see here.

7:02 You can also take numeric data, numbers, and put them into a variable and store it.

7:07 So the idea of a variable is we can use these variables all throughout the code over and over again,

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

3 trong 25 6/7/2012 11:09 AM

Page 4: Using Python in ArcGIS Desktop 10

7:12 but we only have to define them once. So you see the number, this integer called 50.

7:17 Notice it doesn't have quotes around it because it's a number.

7:22 Another kind of variable we might create is one that contains a list,

7:27 and you see a couple examples of a list on the screen here. A list can contain any number of differentobjects inside of it.

7:34 It's just a container that stores objects. You see it has brackets around it.

7:39 So all this will have these brackets, and each item in the list will be separated with a comma.

7:44 So in this example we have a list of feature classes—Water, Land, and Buildings—that are text strings.

7:51 We also can have a list that contains numbers.

7:54 In this case, we have four numbers stored in the list, separated with commas again.

7:58 Actually you can put any number of different kinds of objects into a list,

8:02 but very commonly you're going to find strings and numbers in these.You can also manipulatevariables.

8:08 For instance, we could take two variables and add them together, like you see here.

8:12 We take the variable called first and the one called last, we add them together with a plus symbol,

8:17 and it creates a new text string called BruceHarold. Or in the second example here, we have an agevariable called age.

8:25 I'm going to add 25 to it. In this case, we're going to do some math with this.

8:29 So we can manipulate variables all different types of ways. This is just two examples.

8:34 Notice the pound signs, the green text you see on the screen. These are comments.

8:40 So anytime you see this in a Python code, they're just comments. They don't actually get executedwith the code.

8:44 They're just there to describe it. So it's very common to see these.

8:49 Another type of data that we work with in Python is called a statement.

8:55 One in particular that you'll see very often is called an import statement.

9:00 What an import does is it's going to import a module into your code. Python is a modular language.

9:07 It's made up of many different modules, and you can load in the various modules that you need tomake your script work

9:14 the way you want. So you'll see there are all different kinds. We have the math modules and stringmodules

9:19 (for protected strings) and so on. The modules we're particularly interested in are the ArcPy modules.

9:27 This gives us access to all the geoprocessing functions.

9:30 So ESRI created the ArcPy modules and Python can then use that to work with

9:36 the ArcGIS geoprocessing functions and the properties that are within that.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

4 trong 25 6/7/2012 11:09 AM

Page 5: Using Python in ArcGIS Desktop 10

9:42 This is replacing the arcgisscripting module we had in previous versions. This is the newer way to work.

9:49 Also within this module, I think a lot of you will find interesting, is we now have a mapping module

9:54 as part of the ArcPy. The mapping module is going to give you access to map documents

10:00 and layers within the map documents. This is something we've been wanting for quite awhile.

10:05 We can now actually do this at version 10. We also have access to a 3D module.

10:11 We have a Spatial Analyst module as well.So version 10 is very much expanding the use of thesemodules

10:17 from the Esri perspective.

10:21 It's going to give you a lot more enhancements and ability and versatility when you write your code.

10:28 Another statement we see fairly often in code is called a print statement.

10:31 This is where you print something out to the end user so they can see it.

10:34 You can give messages back to users, and I like to use them to debug code.

10:39 Sometimes they're good at printing out the value that's stored inside of a variable that we just talkedabout.

10:46 We have another statement that's also very common to use in code.

10:49 Probably most scripts I write would contain an if statement. If statements will just test to see if acondition is true or false.

10:57 So if the condition is true, then it will run all the code indented under the if statement.

11:03 In Python, you have to indent the code in order to make it part of the if statement.

11:08 So, quite simply, you can put any code you want between the if and the colon as long as it returned atrue or a false,

11:14 that's all it really wants, and if, for instance, it returns a false,

11:19 then it goes to the else statement that you see and it runs all the code indented under the else.

11:24 So, again, Python needs you to indent the code for if statements

11:30 and for loops and so on that we'll be looking at a little later in the session here.

11:36 Speaking of a for loop, a for loop is another kind of statement that's very common.

11:40 Most scripts will probably contain one of these.

11:43 And quite simply, a for loop is just a looping construction that will loop through a list.

11:50 So we can get a list of items in it like we just talked about.

11:53 The for loop would then go in and visit that list one item at a time and do something with that.

11:57 In this example, we've got a list of feature classes,

12:00 and we're going to go through and print the name of each feature class out.

12:03 We're going to look at the for loop in a little bit more detail in the next slide here.

12:10 This slide is showing an example of everything we've been talking about so far.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

5 trong 25 6/7/2012 11:09 AM

Page 6: Using Python in ArcGIS Desktop 10

12:14 We just put it all together and through a fairly simple script here.

12:16 This script is simply going to take a Roads feature class and buffer it at three different distances.

12:24 So we see the comments within the code. We also see the import where we're importing in the arcpymodule.

12:33 We're also setting up an environment, a current workspace environment setting. We're defining that.

12:38 And we're creating a variable that stores our Roads feature class.

12:43 And then we see a list being created here, so the list contains three distances in it—100 meters, 200,and 400.

12:50 Then we have the for loop that's going to loop through that list.

12:54 So essentially what's going to happen here is the for loop is going to go to the list, get the firstdistance out of that,

13:02 insert it into the for loop, it's going to put it into a variable called dist

13:07 which is then going to use that to create the output name for the buffer and then also to supply thebuffer distance.

13:14 So this for loop is going to run, take the 100 meters and output a new feature class called Roads 100.

13:20 The second time through the loop it gets the second item in the list, the 200 meters, creates a featureclass for it...

13:26 and the third time it gets the 400-meter distance out of the list, buffers it, and then when it reachesthe end of the list,

13:33 it breaks out of the for loop automatically, continues on with the remainder of your code. Let's go backto ArcMap.

13:41 I want to show you an example of how we can use Python in a couple different ways

13:46 to just to show you how it can be used within ArcMap.

13:51 The first thing I'm going to do in this demonstration is I'm going to open up the Python window onceagain,

13:58 and I'm going to clear out the code that's already there, and I'm going to type in some code.

14:05 So I'm not going to load a script this time. I'm actually going to type in a code. What I'm going to do isI'm going to buffer,

14:12 or actually I'm going to clip the roads out based on a city limit polygon.So to do this, I’m going to typein arcpy,

14:20 that's the module we just discussed, type in clip and you'll notice it code-completes it for me. I just hitthe tab key.

14:28 So I'm going to run the clipping tool that's in the toolbox. Because I'm running coding software, I am,

14:34 instead of picking this out of a list, I'm going to actually type it in (it works better with this being incode).

14:40 We're going to clip the Roads feature class using the Mineral_Springs city limit boundary.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

6 trong 25 6/7/2012 11:09 AM

Page 7: Using Python in ArcGIS Desktop 10

14:56 And finally for the third parameter, I'm going to enter in the output feature class which is going to becalled RoadsClip.

15:06 And then I'll end it with an ending parenthesis.I'm going to run this code and as it's running—again,

15:13 it's running in the background—it's going to clip the roads out and you see the tool working right nowand when it's finished,

15:21 we should then have, in the table of contents, we'll have a RoadsClip feature class, which you see here,

15:26 that was created and automatically added. Again, any time you run code from the Python window,

15:30 it automatically puts the results into the map for you. I'll show you one other benefit of using thePython window.

15:40 If you want to rerun the code you just typed in, you can hit the up-arrow key. It brings the code back.

15:45 You can make changes to it. You can run it again so you can keep backing up to various code that youhad typed in.

15:51 So the Python window is very good at testing code, or typing in snippets of code, or seeing how itworks.

15:57 Also, you can execute any tool in the toolbox through this window.

16:06 I'm going to move on to the table that we see on the screen.

16:10 This is a table of the Schools feature class, and in that we have a Type column and it tells us what kindof school it is—

16:17 elementary, high school, or middle school. Over here we have the Name of the school.

16:22 And what I'm going to work with this time is the Field Calculator. So I'm going to go to the Namecolumn,

16:27 open up the Field Calculator and with version 10 we can now input Python code into the calculatorhere.

16:33 So what I want to do with this is I want to change the name of the school names to upper andlowercase.

16:41 Right now, all the school names are in uppercase.

16:43 So I've got a script that's going to find all the elementary schools and just change their names,

16:48 then it's going to parse out all the data, all the different words that are involved in the name of theschool,

16:55 make it lowercase, and then make the first character and capitalize it.

17:01 To do this, I'm going to load a script I've already written. It's called Capitalize Name. It loads thePython code into this.

17:07 This is actually a Python function that we see, and basically the code is just doing what I said. It has anif statement in it.

17:14 It has a for loop in it as well. It's going to loop through all the names.

17:18 It's going to make the changes and then write the name back into the table again.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

7 trong 25 6/7/2012 11:09 AM

Page 8: Using Python in ArcGIS Desktop 10

17:22 So once I run this, click OK, you see it, it works.

17:27 It took the names and you see all the elementary schools now have lowercase and uppercasecharacters

17:35 in their names. So it only worked on the ones we told it to. So this is exactly what we were expectingto see.

17:43 I'll show you one more example. This time I'm going to the Shape column.

17:47 The Shape column actually stores the geometry, so we have all the points for the schools stored here.

17:53 I'm going to open up the Field Calculator again, clear out this code,

17:57 and I'm going to load in a different calculator script called ShiftX.

18:01 What this script is going to do is access the x,y coordinates. It's going to take the x-coordinate and add5,000 to it.

18:09 So we're essentially going to shift everything over in the x direction by 5,000 feet or meters,

18:14 whatever the case is—fairly simple script. You see it doesn't take a lot of code to actually do this. We'regoing to...

18:22 I'll move this over a little bit. We're going to run the code and you see that it's shifted all the schools

18:28 just like it's supposed to. I'm going to go back to this.

18:33 If you're interested in Field Calculator scripts (which are fairly easy to write, again,

18:37 just some basic knowledge of Python and you can actually do quite a bit with these),

18:41 you can go to the Help button, you click on that, it's going to give you a lot of information about theField Calculator

18:47 but probably what you'd be more interested in are the sample scripts.

18:50 So there's actually sample code in this for the calculator that you can cut and paste, you can look at,

18:55 and it gives you a lot of examples of how you can use this code within the Field Calculator.

19:00 So this is a good place to start from if you're not familiar with calculator scripts.

19:11 At this time, I'm going to turn it back over to Bruce. He'll answer some of your questions.

19:15 Bruce: Thanks, Mike. We've got some good questions coming in from around the globe.

19:18 Firstly, John from Indooroopilly in Australia asks, "Which versions of Python are supported in ArcGIS10?"

19:24 Well, the one we'll support, John, is the one we ship which is Python 2.6.5.Meena from Albrightsvilleasks,

19:33 "Is the Python window free from schema locks?"

19:36 Well, Meena, the Python window is subject to the same rules as the rest of the applications, ArcMapand ArcCatalog,

19:42 when it comes to schema locks. That means if you're wanting to make a schema change,

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

8 trong 25 6/7/2012 11:09 AM

Page 9: Using Python in ArcGIS Desktop 10

19:47 then you'll need an exclusive lock on the database. If you're wanting to access or read a feature classor table,

19:53 you'll need a shared lock. If you're doing something like doing background geoprocessing,

19:58 and you're using a scripted tool to do this, and while the feature class or table is being operated on bythe script tool,

20:05 if it's in your user interface, it will appear locked and you won't be able to edit it there.

20:09 One thing to remember about locking, if you're using personal geodatabases,

20:13 is that a whole of the personal geodatabase will be locked,

20:17 whereas you might want to consider using a file geodatabase for any feature class where feature classlocking

20:23 is implemented.

20:25 Dennis from San Rafael asks, "Can Python refresh any of the frame screens?

20:30 Can you zoom to a feature or coordinate in ArcMap using Python?

20:33 Can the layer property changes via a Python script be used in a task in ArcGIS Server?"

20:38 Well, Dennis, yes, Python can refresh the active view, either data or layout view, for any MXD that'scurrently open.

20:46 And, in code, you can read and write the extents of any frames,

20:49 so you can achieve the zoom that you want to any coordinate in ArcMap using Python so ArcMap willrefresh.

20:55 And your last question, "Can layer property changes be scripted in a task in ArcGIS Server?" Yes, theycertainly can,

21:00 arcpy.mapping is easily used in geoprocessing tasks from ArcGIS Server.

21:05 Mike from Orange County asks, "Can I create a Python script to assign symbology

21:10 from an attribute table conditionally based on whether a particular value is available in the attributetable?"

21:17 Well, Mike, what you can do with Python with regards to assigning symbology is retrieve it fromanother layer.

21:24 So if somewhere else on your system you have layers with the symbology you're looking for

21:28 then you can do your own logical testing and indeed assign the symbology that you're looking for

21:33 from another one at run time.

21:36 Sara from Ontario asks, "Can I use Python in ModelBuilder?" Yes, you can, Sara.

21:41 At 10, there's a new model-only tool called Calculate Value

21:45 which lets you use Python expressions that can include model variables,

21:49 so ArcPy will know about the current model state.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

9 trong 25 6/7/2012 11:09 AM

Page 10: Using Python in ArcGIS Desktop 10

21:52 An example we might use is setting a branch condition based on some calculation and outputting aBoolean true or false,

21:58 or perhaps a value at another output variable based on the function of your own creation.

22:02 Of course, wherever you can use Python interactively as an expression in ArcGIS,

22:07 it can also be used in a process in ModelBuilder.

22:12 Paul from Boston asks, "What are the major changes to scripting of geoprocessing in ArcGIS 10?"

22:18 Well, Paul, the major change is that geoprocessing tools are implemented as ArcPy functions at 10.

22:25 You no longer have to instantiate a GP object like you might be used to using at the arcgisscriptingmodule.

22:34 When you come to porting scripts to ArcPy or creating new scripts in ArcPy,

22:38 the biggest change that you might notice when you are using GP tool names is they're now casesensitive.

22:43 This is probably the biggest hurdle if you're a veteran scripter,

22:46 you're going to have to catch yourself a few times with handling case.

22:50 Another improvement at GP scripting at 10 is that modules like Spatial Analyst, 3D Analyst,

22:56 Geostats Analyst and so forth may be brought into local scope by importing those submodules.

23:01 You don't have to do a wordy path down to the function you're looking for.

23:07 Sally from Atlanta asks, "Why is NumPy installed with ArcPy?"

23:12 Well, Sally, some system script tools use NumPy for matrix calculations and array handling.

23:19 So we do use it internally, but it's also very handy to have in its own right for indeed those reasons,

23:25 working with arrays and doing matrix calculations. You might,

23:29 especially if you're a REST or a geoanalyst type of person,

23:32 want to correct your own functions in NumPy and send arrays over to those functions,

23:38 get results to and back and use them downstream in ArcPy.

23:44 Jim from Omaha asks, "How do I get autocomplete working for ArcPy in PythonWin or IDLE?"

23:50 Well, all you need to do, Jim, is import arcpy into the interactive window

23:54 in either of those two IDEs and you'll get autocomplete functionality in any of the open scripts in thesession.

24:01 Last one we have time for right now is Tim from Georgia asks,

24:05 "Where can I find the ArcPy geoprocessing object model diagram?"

24:09 Well, Tim, we're not updating the OMD that you might be used to in 9.3 for the release of 10.

24:14 What we'd like you to do is open up the module docs application in your Python program group.

24:20 That will start the HTML server, and you go to your browser and you can search on ArcPy

24:24 and investigate all the hierarchy of classes and functions and modules there just in your browser.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

10 trong 25 6/7/2012 11:09 AM

Page 11: Using Python in ArcGIS Desktop 10

24:32 So I'd like to hand back to Mike now to carry on with the presentation. Mike: Thank you, Bruce.

24:39 What we were just talking about in the last topic was the basic Python syntax,

24:43 how you can take that and automate geoprocessing tools in a toolbox. And that's fairly easy to do.

24:49 Like I said earlier, it doesn't take a lot of expertise to make that work.

24:53 What we want to move on to now in the second part of this session is talk about the advancedfunctions and properties.

25:00 So besides the tools in a toolbox, there's an entire other category of geoprocessing functions

25:07 and properties that you can access that don't normally show up in a toolbox.

25:11 Scripts have access to this. ModelBuilder doesn't for instance, but you can do it with scripts.

25:16 So what we want to start talking about now is some of these more advanced functions that areavailable to you.

25:21 You can find information about these in the Desktop Help. If you were to look up the ArcPy sitepackage,

25:30 if you did a search for that in the help, you would end up with what you see basically on the screenhere.

25:35 It would show you a listing of all the different categories of functions that's available to you,

25:40 and then you can go to each of those categories and you'll see individual properties

25:44 and functions within that that you can get help on.In the help though has sample code for you

25:50 and it will give you all the syntax. So this is a great way to investigate what's available to you here.

25:56 These are well worth the time it takes to learn because these will make your scripts much moreversatile.

26:01 You can do a lot of very powerful processing with this.

26:03 We'll take a closer look at these categories I was just talking about.You have a category for cursors.

26:10 You've got one that works with fields, listing. We'll talk about some of these in the next few minutescoming up here.

26:16 And, again, only scripts have access to these functions and, like I said,

26:22 they can really make your scripts much more versatile and you can do a lot of very powerful thingswith it.

26:27 Let's take a look at some of these.For instance, you have a describe function available to you.

26:32 This will allow you to take some item and describe it, get information about it.

26:36 For example, if you have a table, you can access the fields within that table—it can tell you how manyfields it's got,

26:44 which fields you have, and what kind of fields that they are.

26:47 We can get all kinds of information back about that table and what's inside the table.

26:52 You can also do the same thing for raster images. What kind of raster is it? Is it compressed?

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

11 trong 25 6/7/2012 11:09 AM

Page 12: Using Python in ArcGIS Desktop 10

26:56 How many bands does it contain? What's the resolution of the raster image?

27:01 So we can return information about geodatabases, workspaces.

27:06 We can do the same thing for features classes. In fact, you see the example down below here.

27:11 We're going to describe a feature class and then we're going to return the shape type.

27:16 Is the feature class a point, line, or is it a polygon feature class?

27:20 Not only that, we're going to access one of the fields within the feature class

27:24 and print out its name and the field type that it contains.

27:28 So describes are very good whenever you need to make a decision with your code.

27:33 So, you may, for instance, if your feature class is a point feature class, you might run one set of codefor that.

27:39 If it turns out to be a polygon feature class, you may have a different set of code for that one.

27:44 So we can use the describe function to determine what it is you're working with within your script.

27:51 Another very common function that's used quite often is called a list function.

27:55 This is going to return to you a list of, for instance, with feature classes or a list of tables.

28:03 For instance, I can go to a geodatabase, scan through it,

28:06 and get a list of all the feature classes that are in that geodatabase, or all the tables that are within it,

28:10 or all the raster images that are stored in it; or I could go to a folder,

28:14 scan through the folder and return a list of everything that's inside of it. So we put these things into alist.

28:20 It's actually a standard Python list like we talked about earlier. You can manipulate that list. You canadd to it,

28:25 take from it, and, more importantly, we can use a for loop then to loop through that list one item at atime.

28:32 So you see the example that's on the screen here. We're going to get a list of feature classes.

28:37 We're going to use a for loop to loop through that list. We're going to go to—and this list happens tocontain shapefiles,

28:43 so it's probably a list of all the shapefiles in a folder.

28:46 The for loop goes through each shapefile and then in turn will convert it into a geodatabase,

28:53 and that's what you're seeing happen with the Copy_management tool.

28:56 So that's just simply copying it over to a geodatabase and the idea here is now we can do batchprocessing with this.

29:03 So we could write a script that's going to process hundreds of pieces of data, information like this,

29:11 and then we can just let it run on its own. Another function that you'll find very helpful is called acursor.

29:18 A cursor is simply a collection of records.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

12 trong 25 6/7/2012 11:09 AM

Page 13: Using Python in ArcGIS Desktop 10

29:21 A cursor is going to allow you to access a table or a feature class and access all of its records.

29:30 Once we have the cursor, then we can loop through that cursor one record at a time, and, not onlythat,

29:35 but we can also read and write to the table.

29:38 So this is what gives us the ability to basically edit a table if you want to think of it like that.

29:42 So we can write values to it, read values from it, we could even add new records to it or delete arecord if we needed to.

29:49 So cursors are very versatile. We can also access geometry through it if you wanted to.

29:53 The example we see on the screen here is creating a cursor for the roads feature class.

30:00 In fact, we're not getting all the roads. We're actually going to get only the roads where the road typeis not equal to 4.

30:06 So we can even constrain this down with query expressions to just put just the records you want insideof that cursor.

30:13 Once they're in the cursor, we can use a for loop then to loop through it one record at a time, like yousee in the example.

30:19 In this example, we're going to loop through each road and print out the road name

30:25 and the CFCC code and print those out to the user. For those that may be already experienced withPython,

30:31 if you've used this before, in version 10, you're going to notice we can put the cursor into a for loopnow.

30:36 In prior versions, we had to use a while loop and the construction was a little bit different with the kindof code

30:43 that you've written. So now this is a little bit more streamlined than it used to be.

30:46 We can now work with the for loop rather than a while loop.

30:52 Geometry object—this is something else you might be very interested in.

30:55 Like I said earlier, we are able to manipulate geometry. There's about three or four ways we couldactually do that.

31:01 We saw one method already when I did the demonstration in the Field Calculator.

31:05 But, there's a couple other ways we can also deal with geometry.

31:08 We can work through a cursor if we want to like we just talked about.

31:12 Or, in this example that you see on the screen here, we can take geometry and manipulate it.

31:17 So we may write a script that's going to delete it. We can even change the shape of geometry.

31:23 We can move it to different locations if we want or if we would like, create a whole new point, line, orpolygon

31:31 and add it to a table if we'd like.In this example on the screen, we're going to create a geometryobject.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

13 trong 25 6/7/2012 11:09 AM

Page 14: Using Python in ArcGIS Desktop 10

31:37 You see that where my cursor is here.

31:40 This geometry object we put into a variable and then we're going to go to a tool called Copy Features.

31:46 This is just a standard tool you'll find in the toolbox,

31:49 but what we're doing different with this is we're adding this geometry object into the output.

31:55 So rather than actually copy the features into another feature class,

31:59 we're actually going to copy them into the geometry object.

32:03 And essentially this is going to return to me a list of all the geometry,

32:07 in this case, all the lines and all the streets that are in that will be put into this list.

32:13 This is something that's fairly new at version 10, what we can do with this geometry object.

32:17 So this is an alternate way of working with it. For those who have worked with geometry before,

32:21 you might want to take a look at this example here. And by the way, all these examples we're lookingat,

32:27 these all came from the help. So if you look in the help, the Desktop Help,

32:32 you'll find these same examples there that you can work with.Once we have the list of geometrycreated,

32:39 we're going to use that for loop to loop through that list (like we've been talking about).

32:43 In this example, we're just simply going to total up the total length of all the roads in the streetsfeature class

32:49 and then print that out.You could take a Python script and add it to the toolbox as a script tool.

32:58 You'll find script tools very useful. Once you do this, the script takes on all the properties

33:04 and the behavior of a normal tool. So if you know how to use tools in a toolbox,

33:08 you'll automatically know how to use a script tool. There's nothing really special or different about it.

33:14 In fact, that script tool can even execute executables or AMLs that you may put in there

33:19 or batch files so we can not only execute a script,

33:22 but we can actually call other code and run it from the toolbox as well.

33:27 Like I said, it takes on all the properties of any other tool so that means that you're going to receivethe same messages

33:33 back that any tool will give your user when you run it. You have access to all the environment settings

33:37 that you may take advantage of.

33:40 And what I think is probably the most important here is, anytime you run a script from the script tool,

33:45 it will automatically add your output to your map. So, if you're running this from within ArcMap,

33:50 it will be added to your table of contents for you.

33:56 Other advantages of script tools: Once you have a script tool created, you can easily share it with otherusers.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

14 trong 25 6/7/2012 11:09 AM

Page 15: Using Python in ArcGIS Desktop 10

34:01 You can e-mail it to another user. They, in turn, can add that to their toolbox and place it in there,

34:08 double-click it and run it very easily. Users don't have to know anything about scripts.

34:12 They don't even have to know this is a script actually. They can just double-click and run it like anyother.

34:17 So you could share these. Script tools also have the ability to automatically create dialogs.

34:23 You don't have to write any code necessarily for these dialogs. These are built into ArcGIS.

34:28 They'll be created for you. I'm actually going to show you an example of this in just a few minutes.

34:32 We'll see how this is actually accomplished.

34:37 And new at version 10 is the ability to take any tool in the toolbox and put it into a toolbar or into acontext menu.

34:46 So this is something that you no longer have to just run tools from the toolbox, you could just closethe toolbox out,

34:53 put that tool into a toolbar, and execute it from the toolbar like any other button that you find on atoolbar.

34:59 So this is something people have been asking about for awhile. You can now do it in version 10.

35:03 And, yes, you can take a script tool and add it to a model.

35:09 So ModelBuilder can work with the scripts as well and, like I said, it's just another tool.

35:15 So you can put basically any tool into ModelBuilder and execute it from that if you'd like.

35:22 I'm going to go back to ArcMap and we're going to look at a demonstration.

35:25 What I'm going to do is take a tool, and turn it into a script tool. I'm going to show you how we canexecute that.

35:33 So, what I'm going to do is I'm going to zoom into a particular area here on the map.

35:37 This is the boundary between two cities. And you'll notice the boundary between the two cities hasbeen badly digitized.

35:46 They're not really coincident. You'll see a lot of digitizing errors along the boundary.

35:51 There is now a new toolbox added to version 10 that does editing for you.

35:55 So there's an editor toolbox that's now been added.

35:58 And I'm going to use one of the scripts to access a snapping tool that's in that toolbox.

36:04 The snapping tool is going to, in turn, snap the vertices between these two boundaries together.

36:10 So in essence it's going to do some editing for me automatically.

36:13 So this was actually a nice new feature that's been added to 10. First, though, I need to make a scripttool for this.

36:19 I'm going to go to ArcCatalog to do that. Notice we can now run ArcCatalog from ArcMap.

36:25 I'm going to go into my Toolboxes, and I have to create a toolbox in order to add a script to it.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

15 trong 25 6/7/2012 11:09 AM

Page 16: Using Python in ArcGIS Desktop 10

36:33 I'm going to create my own custom toolbox and in the toolbox, I'm going to now right-click it and adda script.

36:40 A script already exists—actually, I think what I want to do right now is show you the script.

36:45 This might make more sense if you actually see what the script looks like.

36:48 So I'm going to go to the Python window here, I mean to PythonWin, the editor.

36:53 This is the script I'm just about to add to ArcMap. So if you want to see what's in the script,

36:58 you'll notice that we're importing the ArcPy module that we just talked about earlier.

37:03 And what I really want to point out to you is this, these two lines of code. These are Pythonarguments.

37:10 And what that means is whoever runs the script is going to have to supply these two arguments to thescript.

37:15 It's going to expect that that data is going to be given to it. Since I'm running the snapping tool,

37:20 the snap tool needs to know what layer am I going to snap to and then what the snapping distance is

37:28 that I'm going to be working with. So I'm going to have these two arguments supply that informationto the tool.

37:34 In other words, the user of this tool is going to be able to supply that

37:37 so they could run this tool in any number of different feature classes using different snap distances ifthey would like.

37:45 So now that we know what it looks like…the other reason I'm using a script for this tool is this:

37:49 Since this is an editing tool, I'm going to have it automatically make a backup of my data before itexecutes the tool.

37:56 So, in this case, we always run this tool from a script tool from a script like this,

38:03 we'll always ensure that the backup will always get created first. That way we know that even ifsomething goes wrong,

38:10 we can rely on the backup to replace it. I'm going to return to ArcMap. In ArcMap, remember,

38:17 I'm adding my tool to this toolbox now. So I'm going through this wizard.

38:23 First thing it wants to know when I add the tool is, what do you want to call it? So I'm going to call mytool Snap,

38:29 same thing for the label. I'll just give it the same name. We'll go to the next.

38:34 Then it wants to know which tool, which script, do you want to add to this particular tool I'm creating

38:39 and that's going to be that snapping script that I just looked at.

38:44 I'm going to open that tool and add that snapping Python script to this.

38:51 And the next dialog—this is the important one—this is where we supply those two arguments.

38:55 So remember the script has two arguments in it. We need to create a dialog box for the user to fill out.

39:02 So this form that you see in front of you is going to do that for you.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

16 trong 25 6/7/2012 11:09 AM

Page 17: Using Python in ArcGIS Desktop 10

39:06 So I'm going to prompt the user to "Select a layer." This is going to be the snapping layer.

39:14 Then I need to tell it what's the data type. In other words, what kind of data am I going to ask theuser to supply?

39:20 And you see there are all kinds of data I can ask the user for here.

39:23 The one that we're interested in is called a Feature Layer.

39:27 The second argument that I need to prompt the user for is the snapping distance.

39:32 So I will ask the user to "Enter a snap distance"

39:38 and the data type that works the best for a snap distance like this would be called a linear unit.

39:45 So I find a Linear Unit type. This is going to prompt the user for a number and a unit of measure,

39:51 such as meters or centimeters or feet or inches, whatever the interest is.

39:57 You'll see that there's a number of properties down here I can also set. I'm just going to default allthose.

40:04 I'm going to Finish. If I go back to ArcCatalog, you'll see it has now created a snapping tool, added itto my toolbox.

40:11 Again, it looks and behaves just like any other tool does. If I were to double-click this tool…we'll runthe tool.

40:25 When I run the tool, it's going to ask me for those two arguments. So it's going to ask me to select alayer.

40:30 I'm going to pick out the Mineral Springs layer, then it wants the snap distance.

40:34 I'm going type in 30, and I think I will make mine just 30 meters. It's kind of large for a snappingdistance,

40:41 but you'll see that it should work for us. I'm now going to execute this by clicking OK.

40:46 It's going to run the script, and you see after the script runs you see now that it cleaned up theborders.

40:56 It snapped all the vertices together for me. It did that automatically.

40:59 So for those of you that have to do this kind of work or you were maybe manually digitizing this,

41:04 you might actually have a script to be able to go out and clean up a lot of the boundaries

41:09 and lines and points that aren't contiguous like you'd like them to be.

41:13 So this is going to be a real time saver. Remember, you can do this at version 10 with that new snaptool.

41:18 There are other editing tools in that toolbox as well. There's one that will extend lines and so on foryou.

41:24 So snapping's not the only one available.I'm now going to turn it back over to Bruce.

41:34 Bruce:Thanks, Mike. Another Mike from Albany asks, "Will you be able to access and/or updatemetadata with ArcPy?"

41:40 Yes, you can, Mike. 10 has a rich set of metadata geoprocessing tools that let you import, export,

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

17 trong 25 6/7/2012 11:09 AM

Page 18: Using Python in ArcGIS Desktop 10

41:47 and validate and so forth your metadata with ArcPy. Loan from Livermore asks,

41:54 "Can you set Python to run specific code automatically once a month?" Yes.

41:58 You use your operating system to do this Loan. In the Windows environment,

42:01 you'd use Windows Scheduler and UNIX environments you'd use CRON

42:05 and then the operating system will call Python and within that, of course,

42:10 you can call ArcPy functions any time you want. Nenad from Wellington asks,

42:15 "Hello, Will some existing Python scripts created at 9.3.1, that are running as scheduled tasks,

42:20 need to be modified for ArcGIS10 (with regard to modules, object model, case sensitivity…)?"

42:27 The short answer is, Nenad, no. They will continue to work just as they do currently.

42:31 You won't have to do any porting of your existing Python code.Vincent from Indooroopilly in Australiasays,

42:37 "Can you use Python without ArcGIS running?" Yes, you can, Vincent.

42:42 You don't need to have any ArcMap or ArcCatalog session open for a Python script to import ArcPy

42:49 and execute any of its functions.Michael from Pontotoc in Tennessee asks,

42:57 "After typing several commands into the embedded Python window,

43:00 can the code be exported from the window to a Python script or do you have to copy and paste?"

43:05 You can do either, Michael. You can with a context menu on a right-click you can export the history

43:10 of the Python window to a PY or TXT file on disk or you can if you want select and copy/paste all of itindependently.

43:19 Marisol from San Francisco asks, "Is the Python window available for ArcGIS 10 only or also for 9?

43:26 And if we write Python code for 9, will we be able to read it in version 10?"

43:29 The Python window is only in ArcGIS 10, Marisol. If you write Python code for ArcGIS 9, yes you canread it in version 10.

43:36 You can import it into the Python window or continue using it in existing scripts. Dinesh from MiltonKeynes in the UK asks,

43:44 "Can Python code written in IDLE be imported into ArcGIS 10 Desktop?"

43:48 It's very simple, Dinesh, just save your development work out of your window in IDLE to a PY file

43:54 and you can import that into the Python window indirectively in desktop or use it straightaway in ascript tool.

44:01 Mark from Corvallis asks, "Can you still use arcgisscripting?" Yes, Mark. ArcPy is built usingarcgisscripting,

44:07 and arcgisscripting is still fully available. You can continue coding the way you used to witharcgisscripting

44:14 if you wish or you can port your workflows to ArcPy. Claudia from Sioux Falls asks,

44:20 "Using Python in the Field Calculator, does it include the power function?" Yes it does, Claudia.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

18 trong 25 6/7/2012 11:09 AM

Page 19: Using Python in ArcGIS Desktop 10

44:25 In fact, all of the math module is imported into the Field Calculator now and the power function iscertainly there.

44:31 Tom from Columbia Falls asks, "Why is Esri moving away from VB?

44:35 Will VB be in future releases or is everything going to Python?" With respect to VBA, Tom,

44:41 we're moving away from VBA after ArcGIS 10 because Microsoft is removing support for it

44:47 so we can't support it ourselves in absence of their support. For VB script, that's still supported inWindows applications,

44:54 it remains our way to do things like label expressions and indeed you can use it in the Field Calculatorcurrently.

45:01 The last one we have time for this session, Claudia from Sioux Falls asks,

45:05 "Does the Python window replace the command line window you find in previous versions of ArcGIS?"

45:10 Yes, you're exactly right there, Claudia. The Python window does replace the command line windowfrom ArcGIS 9.x,

45:17 and at 10 what will be there is the Python window alone. So with that I'll hand back to Mike. Mike:Thank you, Bruce.

45:24 I'm going to continue talking about some of the advanced functions and properties we've beendiscussing,

45:30 and what we want to focus on right now is the new mapping module that's been added to version 10.

45:35 I think a lot of you are going to find this very interesting. The mapping module is what's going to giveyou access

45:41 to a map document and you can have quite a lot of access to it. So that means we can access dataframes, layers.

45:49 We can have access to layouts actually, so you can do quite a bit with this new module that's beenadded.

45:56 There's a lot of documentation on this, a lot of sample scripts that you can look at that would show youexamples

46:01 of how this is being used. Now I want you to understand that this is not going to allow you to create amap document,

46:09 but you can manipulate existing map documents. So you can open those documents up,

46:14 retrieve all the contents that are within it, look at it.You can even query and find certain things withinit,

46:19 and you even have the ability now to print a map. You can export a map out into various graphicsformats.

46:26 You can even save it as a copy or you can save over an existing map document if you'd like to.

46:32 So you can make changes to it, save it out—just very much like you would do if you were to manuallyopen it

46:37 and edit it and work on your map document.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

19 trong 25 6/7/2012 11:09 AM

Page 20: Using Python in ArcGIS Desktop 10

46:41 You can use scripts then to automate your workflows that involve map documents.

46:45 This is something you could not do in prior versions. In one of the bullets here that you see,

46:51 it's called update and repair data source. I think a lot of you will find this very interesting.

46:54 This is going to allow you to go into a map document and repair any broken links or any sources

46:59 that are broken in your layers. So you see the familiar red exclamation point,

47:04 a mark that you have beside each of your layers that are broken. You're now going to be able to fixthat through code.

47:10 You can do this in a batch mode if you'd like. So this is going to be something

47:14 I think a lot of users will find very useful. Not only that, you could use it to create a report. You canopen up a document,

47:20 scan through it, look and see what's inside of the document, write it out to a text file, or make sometype of report out of it.

47:27 You can also use Python scripts to automate the new PDF map books that are in version 10 now.

47:33 So if you want to automate that kind of a workflow,

47:35 you can use that with the new map books that you're going to see at version 10.

47:42 Looking at the map document, you're going to have access to data frames and layers within that mapdocument.

47:48 So we're going to be able to change properties. We're going to be able to add new data frames,remove data frames.

47:54 The same thing with layers: you can add layers; you can remove layers.Not only that,

47:58 we're also going to have access to the properties, so we can manipulate properties of a dataframe……like you see here.

48:05 For instance, you might want to change the rotation of a data frame or you want to change the extentof it.

48:11 We could apply a different coordinate system to it if we wanted to. If you look at the example codedown below here,

48:18 it's exactly what it's doing. The code you're seeing is going to find a map document called Project.mxd.

48:25 It's going to go into that map document and make a list of all the data frames within it.

48:29 So here's the list again and the for loop that we've been talking about.

48:32 I'm going to loop through that list of data frames, one data frame at a time.

48:36 I'm going to set each one to a zero degree rotation, set the scale at 24,000, and then export it out as aTIFF image.

48:45 So I'm going to do this again in a batch mode for each data frame that's in that map document.

48:49 You could do the same thing for layouts actually. Let's switch over to layers.

48:55 We have access to most of the layer properties. So if you're familiar with layer properties,

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

20 trong 25 6/7/2012 11:09 AM

Page 21: Using Python in ArcGIS Desktop 10

48:59 you'll recognize what's in this list. For instance, we can change the transparency settings.

49:05 We can turn a layer on and off. We can turn its labels on and off. We can change the source dataagain.

49:11 That's one you'll be particularly interested in. And if you look at the sample code below here,

49:16 you'll see an example of opening up a layer file. Notice in this case we're actually opening a layer file

49:22 out on your hard drive. The layers don't have to be in a map document.

49:26 We can access layer files independently if you'd like. We're going to take the layer file. We're going togo into it.

49:32 We're going to set a name for it to call it highways instead of streets.

49:38 We're going to turn its labels on and then we're going to save a copy of this out. So in essence we justtook a layer,

49:45 made some changes to it, and saved it out as a different layer. So now we have one layer that hasstreets

49:50 without labels and another layer that has streets with labels. And we did this all through code.

49:56 So it's just an example of how you can manipulate layers within a map document

50:00 or within a layer file and work with it.

50:05 I'm going to go back to ArcMap. I'm going to show you an example, a couple of examples actually,

50:09 of how we can manipulate layers and work with these. I'll put up my table of contents.

50:17 You're going to see we have a layer in here called Hydrants. These are fire hydrants,

50:22 and what I'm going to do is I'm going to open up the Python window again,

50:25 clear out the old code and what I'm going to do, I'm going to load a script in once more.

50:32 This script is going to change a layer. So I'm going to open the script, and what I want to show you

50:41 is a couple lines of code in here. I'm going to go up to the top of the script and notice here when wefind...

50:46 this is the line of code that's going to find a map document. Notice how we're going to set the mapdocument to current.

50:53 This means that this script will use whatever the current map document is that I have running.

50:59 So if I run this code from the Python window or as a script tool, like we just talked about,

51:06 it will automatically find the current map document and go into that and do something with it.

51:11 What I'm going to do with this map document is I'm going to find a layer called Hydrants.

51:15 I'm going to take that layer once it's located, and I'm going to define a definition query for it.

51:21 I'm going to change its name to Dresser Hydrants. I'm going to make it visible, turn it on,

51:26 and we're going to turn the labels on for it. So let's execute this—we're going to press the Enter key.

51:33 It's already done; works very quickly. I’m going to go to the table of contents and we'll see what theresults are.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

21 trong 25 6/7/2012 11:09 AM

Page 22: Using Python in ArcGIS Desktop 10

51:39 I'm going to refresh this. You’ll notice the layers now have been changed to Dresser Hydrants,

51:45 and if I turn it on and off here, you'll see that it also turned the labels on, so the labels are actuallypart of this now.

51:52 We turn it on. And if I were to go to the properties, the layers properties,

51:56 you will now notice that the definition query has been defined just like I told it to do in the script.

52:02 So the script actually manipulated all these properties for me.

52:05 Again, you can do this with any layers that are within this map document. We can also rearrangelayers,

52:12 turn layers on and off, you could remove a layer, add a layer to this map if you wanted to.

52:17 There's actually quite a lot you can do with this. This is just a simple example of how you canmanipulate this.

52:23 I'm going to show you one more example. In this one, I'm going to open up a different map document.

52:35 In this map document I'm opening, it's going to have four layers that are broken.

52:39 In other words, the source…it cannot find the source information for those layers.

52:43 This is a fairly typical problem. It happens any time you change the name of your data, the sourcedata,

52:48 or you move it from one location to another one.

52:51 A lot of you are probably well aware of when you do that your map documents can no longer find thesource data.

53:02 Let me try this again. Open it up. Okay, here's what I'm talking about.

53:11 You see in this new map document there are four layers with the all-too-familiar red exclamation marknext to them,

53:20 meaning that these are broken. You see that the layers don't show up on the map.

53:24 What I'm going to do is I'm going to run a script from PythonWin that's going to fix this.

53:29 So first I'm going to set my map document to a blank map.

53:33 I'm going to do this because I'm actually going to save over that map document.

53:38 I can't save it as long as it's open in ArcMap. I'm going to switch over to PythonWin.

53:46 We'll close the old code out and this will be the script I'm looking at right here, it's called Fix Broken.

53:52 Essentially, you see it's a very simple script. Manipulating map documents like this doesn't have to beanything

53:57 really difficult. Again, you can find this kind of code in the Desktop Help, there are samples of this.

54:03 Notice what we're doing. We're going to import the ArcPy module. We’re then going to find the brokenmap MXD,

54:11 so we're locating the map document. And this is what's interesting here:

54:14 I'm going to go into that map document and make a list of all the broken layers,

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

22 trong 25 6/7/2012 11:09 AM

Page 23: Using Python in ArcGIS Desktop 10

54:19 all the layers that have broken data sources, will be put into my list

54:24 and then I'm going to go and change the source name of those layers.

54:28 So it's only going to work on the broken layer. It leaves the other layers alone.

54:33 We're going to change the name of it and make them correct.

54:36 Now what's really interesting with this is notice that I'm now going to save this map document.

54:41 So once I've made the changes to it, I turn around and save it. Let's run this script. I'm going to run itfrom PythonWin.

54:51 It works very quickly so it's already finished. We're going to go back to ArcMap.

54:56 We will now open up that same map document again and this time we should see four layers that havebeen fixed.

55:04 So we should see the layers draw on the screen. Now you see the layers within the table of contents,

55:11 see everything's fixed now. So, all is right with this map document.

55:15 This is much better and much faster than we used to work with when you had to manually edit this andwork with it.

55:23 This is going to be very useful for you; if you want to batch process large numbers of map documents,you can do that.

55:29 You can go in there and fix it anytime your data moves or changes. This should save a lot of users a lotof time.

55:36 I'm going to turn this back over to Bruce. Bruce: Thanks, Mike. Eagle in Parnell asks,

55:42 "Are you planning to allow Python to calculate labels or use Python for label expressions?"

55:48 Yes, it is planned, although it won't be in the release of ArcGIS 10.

55:50 You can look forward to using Python and label expressions in a future release.

55:56 Georgeta from Boulder asks, "Can we create an MSD using Python or publish the MSD as a service inthe same script?"

56:02 If other people are not aware, an MSD is a map service document which is optimized for rendering ofthe map

56:10 and using ArcGIS Server. Now, Georgeta, because we're expecting an analysis step during theauthoring of the MSD,

56:18 we don't currently support the writing of MSDs with arcpy.mapping.

56:22 So, because we're expecting another authoring step between the authoring of the map and thepublishing MSD,

56:27 we're not supporting writing MSDs and publishing them in the same script.

56:32 Aaron from Denver asks, "Is there a Python script for snapping one layer to another, rather than withinitself?"

56:38 Well, Aaron, yes the snap editing tool at ArcGIS 10 supports what's called a snapping environment.

56:45 You can use any number of other feature classes as snapping targets

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

23 trong 25 6/7/2012 11:09 AM

Page 24: Using Python in ArcGIS Desktop 10

56:50 in different ways in addition to the feature class being edited.Georgeta from Boulder again asks,

56:56 "Can you share a tool based on a Python script but with a compiled version of the script?"

57:01 Georgeta, we're not supporting compiled versions of scripts for script tools because there's really notmuch point.

57:08 Compiled Python runs at the same speed as the ordinary PY files,

57:11 but I think what you're getting at there is can you embed a script and somehow encrypt it.

57:16 Well we password or let you password-protect scripts. There's one way you can share a script

57:21 but not let it be fully exposed. Patty from Corvallis asks, "Can you run a cursor in ModelBuilder througha script?"

57:32 Well, I think a couple of options there for you, Patty, at ArcGIS 10 we have a model process

57:38 called an Iterator which will let you effectively run the cursor through a feature class or sets of values,or what have you.

57:45 So that's one way of looking at that. Another way is, yes you can outright run a cursor in ModelBuilder

57:50 by using the Calculate Value model tool in ModelBuilder which runs any Python code that you put inthere.

57:58 Russell from Edna asks, "Can the script add output to a new map or dataset instead of the current?"

58:05 Yes, Russell, you can write out any layer in memory and add it to any other map document you createon the fly

58:11 or have on disk, so you can add output to a new map or dataset instead of the current one.

58:15 Claudia from Sioux Falls asks, "Is it possible to automatically add symbologies to several outputs froma Python script?"

58:23 Yes, Claudia, provided that you can retrieve the symbologies that you need from other existing layersor map documents

58:29 then you can automate the application of them to new outputs from scripts.

58:36 Time for one more: Kamal from Toronto asks, "Can Python be used to create a list of files used

58:41 and their locations with an MXD file?" Yes, Kamal, it's one of the primary use cases that we see for

58:46 the ArcPy mapping module. You can certainly generate a report of the data sources used

58:51 and where they are from a script. So with that, I'll hand back to Mike to wrap up. Mike: Thank you,Bruce.

58:59 Before we go, we'd like to point you to a few resources that you see on the slide here on the screen.

59:05 If you want to know more about geoprocessing tools, there's an online Web training class you can take

59:11 that will explain a lot of that to you. If you're more interested in learning Python

59:15 and how you can work with some of the advanced functions that we talked about, we have aninstructor-led training class,

59:22 Introduction to Geoprocessing Script Using Python, that you can take.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

24 trong 25 6/7/2012 11:09 AM

Page 25: Using Python in ArcGIS Desktop 10

59:26 This will go into all the details about working with scripts. If you want other resources to learn Python,

59:33 there are a number of books available to you online or in book stores that are fairly good

59:39 that can give you information about all the basics of working with Python.

59:43 There are also Web sites that are devoted to Python like python.org, for instance, is a good one thatyou can rely on.

59:51 We have other seminars like this one in the weeks and months ahead that you can also watch

59:55 to learn more about ArcGIS 10 so there'll be more information forthcoming. I hope you found thisseminar helpful.

60:03 I hope this has given you some ideas about how you can use Python and integrate it with ArcGIS

60:08 and make your workflows easier and make your work go a little bit faster.

60:15 Your comments help us to improve our seminars, so please take a moment to complete our survey.

60:21 Just click the Give Us Feedback link to take the survey. We hope you enjoyed today's seminar.

60:26 On behalf of Esri, I'd like to thank all of you for attending.

Using Python in ArcGIS Desktop 10 http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

25 trong 25 6/7/2012 11:09 AM