Sie sind auf Seite 1von 25

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

Print

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

Caption

and I'm an instructor with the Educational Services department in Charlotte, North Carolina. Joining me today is Bruce Harold who is product manager with the Desktop team in Redlands, California.

0:14 0:18 0:22 0:28 0:32 0:35

We want to welcome all of you today to this live training seminar. Today, we're going to focus on using Python in ArcGIS Desktop 10, and we're going to focus on some of the new functions that Python can add to 10, so we'll be pointing that out as we go through the presentation. We're going to look at some basic benefits and advantages of Python, then we're going to talk about the basic Python syntax. For those that may not have any experience with Python

0:41 0:44 0:51 0:56 1:00 1:03 1:09 1:14 1:18 1:21 1:28 1:35

and don't have really a programming background, we're going to discuss some of the basic syntax Im going to show you how you can use that then to automate the geoprocessing tools. For those that do have more experience, we're going to be talking more about the advanced functions So we'll be pointing out some of the advantages that you can gain from running Python scripts, some of the more advanced functions that you can perform. Each of these topics will be followed by a software demonstration, and then we'll turn it over to Bruce who will answer your questions. Remember, you can send questions in any time during the presentation. Python is an open source, cross-platform (meaning we can run it on Windows or UNIX or Linux) 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 2:03 2:10

That script can then go and copy data or convert data in a batch mode so take large numbers of files and process those during your times when the system isn't busy. What we'll probably be most interested in today in this seminar is the ability to take Python scripts

1 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

2:16

and automate the geoprocessing tools. So there are several hundred tools available to you in the ArcToolbox.

2:22

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

2:28

And you're going to find that just some basic understanding of scripts, basic understanding of the tools in a toolbox,

2:35 2:39

you'll be able to automate these things fairly easily. Besides automating just the tools in a toolbox, we also have the ability to work with some of the more advanced functions.

2:46 2:52 2:56 3:00

In other words, we couldfor instance, we could take a table and we can look through that table. We can read and write to the table. We can manipulate tabular data. You can also manipulate map documents and the layers within the map document. This is new at version 10 (the ability). We'll be talking more about this a little bit later. And we could also take geometry

3:06 3:10 3:16 3:20

points, lines, and polygonsand manipulate those. We'll see a couple of examples of how this works throughout the seminar.Where do we run Python code from? Well, there are a number of places you can type in the code or execute it from. One, if you're serious about writing Python scripts, you'll probably want to use some kind of a Python editor.

3:27 3:31 3:36

The one I have showing on the screen is called PythonWin. This is fairly common. But keep in mind, there are many editors available to you. Some are free. Some you can purchase. There's one called IDLE that's built into Python. If you want, you can use that one. I use this PythonWin fairly often.

3:44 3:50

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, and also to execute it. When you execute the code, you don't necessarily have to have ArcMap or ArcCatalog open.

3:57 4:02 4:08 4:14 4:19 4:24

They don't have to be running at the time in order to run the code from this editor. It can be run stand-alone on its own.You could also run the code from the new Python window. This is new at version 10, so in ArcMap, there is a window called the Python window. This actually replaces the older command line window that was there in version 9. In this window, you can type in individual commands. You can type in Python code within it. Some of you might recognizeit looks fairly similar to an interactive window that you have in many of the 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 the toolbox as a tool.

4:37

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

2 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

4:42 4:46 4:50

And finally, you can run Python code within the Field Calculator if you want. This is new at version 10 (this ability). At this point, I'm going to go to ArcMap, and I'm going to show you an example of running an actual script.

4:55 5:00 5:05

So to do this, I'm going to open up the Python window we just discussed, and I'm going to right-click and load an existing script into this Python window. So I have a script that will buffer some roads for me. And we're going talk more details about the code in just a little bit.

5:13 5:18 5:23

So we won't worry about the actual syntax at this point. We're just simply going to run the code. As this is running, it's going to run in the background. This is a new feature in version 10 that we can run any of the tools in a toolbox, or scripts in this case, can be run in the background and as they're running,

5:30 5:36 5:42 5:47

you can continue to do other work within ArcMap. For instance, you could zoom in, zoom out, or identify features on the map, open tables up. You see that the script is currently working. So what we're doing here with this is we're buffering roads at three different distances, and you can see it's running three different buffers. The script's automating this (one right after the other),

5:52 5:57 6:02 6:06 6:12 6:22 6:26 6:31 6:36

and you can see the results on the screen here. I'll open up the table of contents so you can see that it created three new layers that we see were automatically added to the table of contents. Any time you run code from the Python window, outputs automatically get put into your map for you. I'm going to turn these off. Let's talk about some basic Python syntax. This is more for the benefit of those of you that may not have a real Python experience. What we want to do is get you familiar with what the code looks like so when we see various examples (or you can go and read sample code that's built into the Desktop Help), you'll have some idea of what that code is doing for you. One thing you'll be seeing very often is variables.

6:43 6:46 6:50 6:54 6:57 7:02 7:07

A variable contains information. It stores it. For instance, we have a variable here called first that's equal to a string called Bruce. Another one called last that's equal to a text string called Harold, and you'll see that we have quotes around these text strings. This is what makes it a string, and you can use either single or double quotes like you see here. You can also take numeric data, numbers, and put them into a variable and store it. So the idea of a variable is we can use these variables all throughout the code over and over again,

3 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

7:12 7:17 7:22 7:27

but we only have to define them once. So you see the number, this integer called 50. Notice it doesn't have quotes around it because it's a number. Another kind of variable we might create is one that contains a list, and you see a couple examples of a list on the screen here. A list can contain any number of different objects inside of it.

7:34 7:39 7:44 7:51 7:54 7:58 8:02

It's just a container that stores objects. You see it has brackets around it. So all this will have these brackets, and each item in the list will be separated with a comma. So in this example we have a list of feature classesWater, Land, and Buildingsthat are text strings. We also can have a list that contains numbers. In this case, we have four numbers stored in the list, separated with commas again. Actually you can put any number of different kinds of objects into a list, but very commonly you're going to find strings and numbers in these.You can also manipulate variables.

8:08 8:12 8:17

For instance, we could take two variables and add them together, like you see here. We take the variable called first and the one called last, we add them together with a plus symbol, and it creates a new text string called BruceHarold. Or in the second example here, we have an age variable called age.

8:25 8:29 8:34 8:40

I'm going to add 25 to it. In this case, we're going to do some math with this. So we can manipulate variables all different types of ways. This is just two examples. Notice the pound signs, the green text you see on the screen. These are comments. So anytime you see this in a Python code, they're just comments. They don't actually get executed with the code.

8:44 8:49 8:55 9:00 9:07

They're just there to describe it. So it's very common to see these. Another type of data that we work with in Python is called a statement. One in particular that you'll see very often is called an import statement. What an import does is it's going to import a module into your code. Python is a modular language. It's made up of many different modules, and you can load in the various modules that you need to make your script work

9:14

the way you want. So you'll see there are all different kinds. We have the math modules and string modules

9:19 9:27 9:30 9:36

(for protected strings) and so on. The modules we're particularly interested in are the ArcPy modules. This gives us access to all the geoprocessing functions. So ESRI created the ArcPy modules and Python can then use that to work with the ArcGIS geoprocessing functions and the properties that are within that.

4 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

9:42 9:49 9:54 10:00 10:05 10:11

This is replacing the arcgisscripting module we had in previous versions. This is the newer way to work. Also within this module, I think a lot of you will find interesting, is we now have a mapping module as part of the ArcPy. The mapping module is going to give you access to map documents and layers within the map documents. This is something we've been wanting for quite awhile. We can now actually do this at version 10. We also have access to a 3D module. We have a Spatial Analyst module as well.So version 10 is very much expanding the use of these modules

10:17 10:21 10:28 10:31 10:34 10:39

from the Esri perspective. It's going to give you a lot more enhancements and ability and versatility when you write your code. Another statement we see fairly often in code is called a print statement. This is where you print something out to the end user so they can see it. You can give messages back to users, and I like to use them to debug code. Sometimes they're good at printing out the value that's stored inside of a variable that we just talked about.

10:46 10:49

We have another statement that's also very common to use in code. Probably most scripts I write would contain an if statement. If statements will just test to see if a condition is true or false.

10:57 11:03 11:08

So if the condition is true, then it will run all the code indented under the if statement. In Python, you have to indent the code in order to make it part of the if statement. So, quite simply, you can put any code you want between the if and the colon as long as it returned a true or a false,

11:14 11:19 11:24 11:30 11:36 11:40 11:43 11:50 11:53 11:57 12:00 12:03 12:10

that's all it really wants, and if, for instance, it returns a false, then it goes to the else statement that you see and it runs all the code indented under the else. So, again, Python needs you to indent the code for if statements and for loops and so on that we'll be looking at a little later in the session here. Speaking of a for loop, a for loop is another kind of statement that's very common. Most scripts will probably contain one of these. And quite simply, a for loop is just a looping construction that will loop through a list. So we can get a list of items in it like we just talked about. The for loop would then go in and visit that list one item at a time and do something with that. In this example, we've got a list of feature classes, and we're going to go through and print the name of each feature class out. We're going to look at the for loop in a little bit more detail in the next slide here. This slide is showing an example of everything we've been talking about so far.

5 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

12:14 12:16 12:24

We just put it all together and through a fairly simple script here. This script is simply going to take a Roads feature class and buffer it at three different distances. So we see the comments within the code. We also see the import where we're importing in the arcpy module.

12:33 12:38 12:43

We're also setting up an environment, a current workspace environment setting. We're defining that. And we're creating a variable that stores our Roads feature class. And then we see a list being created here, so the list contains three distances in it100 meters, 200, and 400.

12:50 12:54

Then we have the for loop that's going to loop through that list. So essentially what's going to happen here is the for loop is going to go to the list, get the first distance out of that,

13:02 13:07

insert it into the for loop, it's going to put it into a variable called dist which is then going to use that to create the output name for the buffer and then also to supply the buffer distance.

13:14 13:20

So this for loop is going to run, take the 100 meters and output a new feature class called Roads 100. The second time through the loop it gets the second item in the list, the 200 meters, creates a feature class for it...

13:26

and the third time it gets the 400-meter distance out of the list, buffers it, and then when it reaches the 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 back to ArcMap.

13:41 13:46 13:51

I want to show you an example of how we can use Python in a couple different ways to just to show you how it can be used within ArcMap. The first thing I'm going to do in this demonstration is I'm going to open up the Python window once again,

13:58 14:05

and I'm going to clear out the code that's already there, and I'm going to type in some code. 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 is I'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, Im going to type in 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 hit the tab key.

14:28 14:34

So I'm going to run the clipping tool that's in the toolbox. Because I'm running coding software, I am, instead of picking this out of a list, I'm going to actually type it in (it works better with this being in code).

14:40

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

6 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

14:56

And finally for the third parameter, I'm going to enter in the output feature class which is going to be called RoadsClip.

15:06 15:13

And then I'll end it with an ending parenthesis.I'm going to run this code and as it's runningagain, it's running in the backgroundit's going to clip the roads out and you see the tool working right now and when it's finished,

15:21 15:26 15:30

we should then have, in the table of contents, we'll have a RoadsClip feature class, which you see here, that was created and automatically added. Again, any time you run code from the Python window, it automatically puts the results into the map for you. I'll show you one other benefit of using the Python window.

15:40 15:45

If you want to rerun the code you just typed in, you can hit the up-arrow key. It brings the code back. You can make changes to it. You can run it again so you can keep backing up to various code that you had typed in.

15:51

So the Python window is very good at testing code, or typing in snippets of code, or seeing how it works.

15:57 16:06 16:10

Also, you can execute any tool in the toolbox through this window. I'm going to move on to the table that we see on the screen. This is a table of the Schools feature class, and in that we have a Type column and it tells us what kind of school it is

16:17 16:22

elementary, high school, or middle school. Over here we have the Name of the school. And what I'm going to work with this time is the Field Calculator. So I'm going to go to the Name column,

16:27

open up the Field Calculator and with version 10 we can now input Python code into the calculator here.

16:33

So what I want to do with this is I want to change the name of the school names to upper and lowercase.

16:41 16:43 16:48

Right now, all the school names are in uppercase. So I've got a script that's going to find all the elementary schools and just change their names, then it's going to parse out all the data, all the different words that are involved in the name of the school,

16:55 17:01

make it lowercase, and then make the first character and capitalize it. To do this, I'm going to load a script I've already written. It's called Capitalize Name. It loads the Python 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 an if statement in it.

17:14 17:18

It has a for loop in it as well. It's going to loop through all the names. It's going to make the changes and then write the name back into the table again.

7 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

17:22 17:27

So once I run this, click OK, you see it, it works. It took the names and you see all the elementary schools now have lowercase and uppercase characters

17:35

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

17:43 17:47 17:53 17:57 18:01

I'll show you one more example. This time I'm going to the Shape column. The Shape column actually stores the geometry, so we have all the points for the schools stored here. I'm going to open up the Field Calculator again, clear out this code, and I'm going to load in a different calculator script called ShiftX. What this script is going to do is access the x,y coordinates. It's going to take the x-coordinate and add 5,000 to it.

18:09 18:14

So we're essentially going to shift everything over in the x direction by 5,000 feet or meters, whatever the case isfairly simple script. You see it doesn't take a lot of code to actually do this. We're going to...

18:22 18:28 18:33 18:37 18:41

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 just like it's supposed to. I'm going to go back to this. If you're interested in Field Calculator scripts (which are fairly easy to write, again, just some basic knowledge of Python and you can actually do quite a bit with these), you can go to the Help button, you click on that, it's going to give you a lot of information about the Field Calculator

18:47 18:50 18:55 19:00 19:11 19:15 19:18

but probably what you'd be more interested in are the sample scripts. So there's actually sample code in this for the calculator that you can cut and paste, you can look at, and it gives you a lot of examples of how you can use this code within the Field Calculator. So this is a good place to start from if you're not familiar with calculator scripts. At this time, I'm going to turn it back over to Bruce. He'll answer some of your questions. Bruce: Thanks, Mike. We've got some good questions coming in from around the globe. Firstly, John from Indooroopilly in Australia asks, "Which versions of Python are supported in ArcGIS 10?"

19:24

Well, the one we'll support, John, is the one we ship which is Python 2.6.5.Meena from Albrightsville asks,

19:33 19:36

"Is the Python window free from schema locks?" Well, Meena, the Python window is subject to the same rules as the rest of the applications, ArcMap and ArcCatalog,

19:42

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

8 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

19:47

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

19:53 19:58

you'll need a shared lock. If you're doing something like doing background geoprocessing, and you're using a scripted tool to do this, and while the feature class or table is being operated on by the script tool,

20:05 20:09 20:13 20:17

if it's in your user interface, it will appear locked and you won't be able to edit it there. One thing to remember about locking, if you're using personal geodatabases, is that a whole of the personal geodatabase will be locked, whereas you might want to consider using a file geodatabase for any feature class where feature class locking

20:23 20:25 20:30 20:33 20:38

is implemented. Dennis from San Rafael asks, "Can Python refresh any of the frame screens? Can you zoom to a feature or coordinate in ArcMap using Python? Can the layer property changes via a Python script be used in a task in ArcGIS Server?" Well, Dennis, yes, Python can refresh the active view, either data or layout view, for any MXD that's currently open.

20:46 20:49

And, in code, you can read and write the extents of any frames, so you can achieve the zoom that you want to any coordinate in ArcMap using Python so ArcMap will refresh.

20:55

And your last question, "Can layer property changes be scripted in a task in ArcGIS Server?" Yes, they certainly can,

21:00 21:05 21:10

arcpy.mapping is easily used in geoprocessing tasks from ArcGIS Server. Mike from Orange County asks, "Can I create a Python script to assign symbology from an attribute table conditionally based on whether a particular value is available in the attribute table?"

21:17

Well, Mike, what you can do with Python with regards to assigning symbology is retrieve it from another layer.

21:24 21:28 21:33 21:36 21:41 21:45 21:49

So if somewhere else on your system you have layers with the symbology you're looking for then you can do your own logical testing and indeed assign the symbology that you're looking for from another one at run time. Sara from Ontario asks, "Can I use Python in ModelBuilder?" Yes, you can, Sara. At 10, there's a new model-only tool called Calculate Value which lets you use Python expressions that can include model variables, so ArcPy will know about the current model state.

9 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

21:52

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

21:58 22:02 22:07 22:12 22:18 22:25

or perhaps a value at another output variable based on the function of your own creation. Of course, wherever you can use Python interactively as an expression in ArcGIS, it can also be used in a process in ModelBuilder. Paul from Boston asks, "What are the major changes to scripting of geoprocessing in ArcGIS 10?" Well, Paul, the major change is that geoprocessing tools are implemented as ArcPy functions at 10. You no longer have to instantiate a GP object like you might be used to using at the arcgisscripting module.

22:34 22:38

When you come to porting scripts to ArcPy or creating new scripts in ArcPy, the biggest change that you might notice when you are using GP tool names is they're now case sensitive.

22:43 22:46 22:50 22:56 23:01 23:07 23:12 23:19 23:25 23:29 23:32 23:38 23:44 23:50 23:54

This is probably the biggest hurdle if you're a veteran scripter, you're going to have to catch yourself a few times with handling case. Another improvement at GP scripting at 10 is that modules like Spatial Analyst, 3D Analyst, Geostats Analyst and so forth may be brought into local scope by importing those submodules. You don't have to do a wordy path down to the function you're looking for. Sally from Atlanta asks, "Why is NumPy installed with ArcPy?" Well, Sally, some system script tools use NumPy for matrix calculations and array handling. So we do use it internally, but it's also very handy to have in its own right for indeed those reasons, working with arrays and doing matrix calculations. You might, especially if you're a REST or a geoanalyst type of person, want to correct your own functions in NumPy and send arrays over to those functions, get results to and back and use them downstream in ArcPy. Jim from Omaha asks, "How do I get autocomplete working for ArcPy in PythonWin or IDLE?" Well, all you need to do, Jim, is import arcpy into the interactive window in either of those two IDEs and you'll get autocomplete functionality in any of the open scripts in the session.

24:01 24:05 24:09 24:14 24:20 24:24

Last one we have time for right now is Tim from Georgia asks, "Where can I find the ArcPy geoprocessing object model diagram?" Well, Tim, we're not updating the OMD that you might be used to in 9.3 for the release of 10. What we'd like you to do is open up the module docs application in your Python program group. That will start the HTML server, and you go to your browser and you can search on ArcPy and investigate all the hierarchy of classes and functions and modules there just in your browser.

10 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

24:32 24:39 24:43 24:49 24:53

So I'd like to hand back to Mike now to carry on with the presentation. Mike: Thank you, Bruce. What we were just talking about in the last topic was the basic Python syntax, how you can take that and automate geoprocessing tools in a toolbox. And that's fairly easy to do. Like I said earlier, it doesn't take a lot of expertise to make that work. What we want to move on to now in the second part of this session is talk about the advanced functions and properties.

25:00 25:07 25:11 25:16

So besides the tools in a toolbox, there's an entire other category of geoprocessing functions and properties that you can access that don't normally show up in a toolbox. Scripts have access to this. ModelBuilder doesn't for instance, but you can do it with scripts. So what we want to start talking about now is some of these more advanced functions that are available to you.

25:21

You can find information about these in the Desktop Help. If you were to look up the ArcPy site package,

25:30

if you did a search for that in the help, you would end up with what you see basically on the screen here.

25:35 25:40 25:44 25:50 25:56

It would show you a listing of all the different categories of functions that's available to you, and then you can go to each of those categories and you'll see individual properties and functions within that that you can get help on.In the help though has sample code for you and it will give you all the syntax. So this is a great way to investigate what's available to you here. These are well worth the time it takes to learn because these will make your scripts much more versatile.

26:01 26:03 26:10

You can do a lot of very powerful processing with this. We'll take a closer look at these categories I was just talking about.You have a category for cursors. You've got one that works with fields, listing. We'll talk about some of these in the next few minutes coming up here.

26:16 26:22

And, again, only scripts have access to these functions and, like I said, they can really make your scripts much more versatile and you can do a lot of very powerful things with it.

26:27 26:32 26:36

Let's take a look at some of these.For instance, you have a describe function available to you. This will allow you to take some item and describe it, get information about it. For example, if you have a table, you can access the fields within that tableit can tell you how many fields it's got,

26:44 26:47 26:52

which fields you have, and what kind of fields that they are. We can get all kinds of information back about that table and what's inside the table. You can also do the same thing for raster images. What kind of raster is it? Is it compressed?

11 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

26:56 27:01 27:06 27:11 27:16 27:20 27:24 27:28 27:33

How many bands does it contain? What's the resolution of the raster image? So we can return information about geodatabases, workspaces. We can do the same thing for features classes. In fact, you see the example down below here. We're going to describe a feature class and then we're going to return the shape type. Is the feature class a point, line, or is it a polygon feature class? Not only that, we're going to access one of the fields within the feature class and print out its name and the field type that it contains. So describes are very good whenever you need to make a decision with your code. So, you may, for instance, if your feature class is a point feature class, you might run one set of code for that.

27:39 27:44 27:51 27:55 28:03 28:06 28:10 28:14

If it turns out to be a polygon feature class, you may have a different set of code for that one. So we can use the describe function to determine what it is you're working with within your script. Another very common function that's used quite often is called a list function. This is going to return to you a list of, for instance, with feature classes or a list of tables. For instance, I can go to a geodatabase, scan through it, and get a list of all the feature classes that are in that geodatabase, or all the tables that are within it, or all the raster images that are stored in it; or I could go to a folder, scan through the folder and return a list of everything that's inside of it. So we put these things into a list.

28:20

It's actually a standard Python list like we talked about earlier. You can manipulate that list. You can add 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 a time.

28:32 28:37

So you see the example that's on the screen here. We're going to get a list of feature classes. We're going to use a for loop to loop through that list. We're going to go toand this list happens to contain shapefiles,

28:43 28:46 28:53 28:56

so it's probably a list of all the shapefiles in a folder. The for loop goes through each shapefile and then in turn will convert it into a geodatabase, and that's what you're seeing happen with the Copy_management tool. So that's just simply copying it over to a geodatabase and the idea here is now we can do batch processing with this.

29:03 29:11

So we could write a script that's going to process hundreds of pieces of data, information like this, and then we can just let it run on its own. Another function that you'll find very helpful is called a cursor.

29:18

A cursor is simply a collection of records.

12 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

29:21 29:30

A cursor is going to allow you to access a table or a feature class and access all of its records. Once we have the cursor, then we can loop through that cursor one record at a time, and, not only that,

29:35 29:38 29:42

but we can also read and write to the table. So this is what gives us the ability to basically edit a table if you want to think of it like that. So we can write values to it, read values from it, we could even add new records to it or delete a record if we needed to.

29:49 29:53 30:00

So cursors are very versatile. We can also access geometry through it if you wanted to. The example we see on the screen here is creating a cursor for the roads feature class. In fact, we're not getting all the roads. We're actually going to get only the roads where the road type is not equal to 4.

30:06

So we can even constrain this down with query expressions to just put just the records you want inside of 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 you see in the example.

30:19 30:25

In this example, we're going to loop through each road and print out the road name and the CFCC code and print those out to the user. For those that may be already experienced with Python,

30:31

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

30:36

In prior versions, we had to use a while loop and the construction was a little bit different with the kind of code

30:43 30:46 30:52 30:55

that you've written. So now this is a little bit more streamlined than it used to be. We can now work with the for loop rather than a while loop. Geometry objectthis is something else you might be very interested in. Like I said earlier, we are able to manipulate geometry. There's about three or four ways we could actually do that.

31:01 31:05 31:08 31:12 31:17 31:23

We saw one method already when I did the demonstration in the Field Calculator. But, there's a couple other ways we can also deal with geometry. We can work through a cursor if we want to like we just talked about. Or, in this example that you see on the screen here, we can take geometry and manipulate it. So we may write a script that's going to delete it. We can even change the shape of geometry. We can move it to different locations if we want or if we would like, create a whole new point, line, or polygon

31:31

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

13 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

31:37 31:40 31:46 31:49 31:55 31:59 32:03 32:07 32:13 32:17 32:21

You see that where my cursor is here. This geometry object we put into a variable and then we're going to go to a tool called Copy Features. This is just a standard tool you'll find in the toolbox, but what we're doing different with this is we're adding this geometry object into the output. So rather than actually copy the features into another feature class, we're actually going to copy them into the geometry object. And essentially this is going to return to me a list of all the geometry, in this case, all the lines and all the streets that are in that will be put into this list. This is something that's fairly new at version 10, what we can do with this geometry object. So this is an alternate way of working with it. For those who have worked with geometry before, you might want to take a look at this example here. And by the way, all these examples we're looking at,

32:27 32:32

these all came from the help. So if you look in the help, the Desktop Help, you'll find these same examples there that you can work with.Once we have the list of geometry created,

32:39 32:43

we're going to use that for loop to loop through that list (like we've been talking about). In this example, we're just simply going to total up the total length of all the roads in the streets feature class

32:49 32:58 33:04 33:08 33:14 33:19 33:22 33:27

and then print that out.You could take a Python script and add it to the toolbox as a script tool. You'll find script tools very useful. Once you do this, the script takes on all the properties and the behavior of a normal tool. So if you know how to use tools in a toolbox, you'll automatically know how to use a script tool. There's nothing really special or different about it. In fact, that script tool can even execute executables or AMLs that you may put in there or batch files so we can not only execute a script, but we can actually call other code and run it from the toolbox as well. Like I said, it takes on all the properties of any other tool so that means that you're going to receive the same messages

33:33 33:37 33:40 33:45 33:50 33:56

back that any tool will give your user when you run it. You have access to all the environment settings that you may take advantage of. And what I think is probably the most important here is, anytime you run a script from the script tool, it will automatically add your output to your map. So, if you're running this from within ArcMap, it will be added to your table of contents for you. Other advantages of script tools: Once you have a script tool created, you can easily share it with other users.

14 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

34:01 34:08 34:12

You can e-mail it to another user. They, in turn, can add that to their toolbox and place it in there, double-click it and run it very easily. Users don't have to know anything about scripts. They don't even have to know this is a script actually. They can just double-click and run it like any other.

34:17 34:23 34:28 34:32 34:37

So you could share these. Script tools also have the ability to automatically create dialogs. You don't have to write any code necessarily for these dialogs. These are built into ArcGIS. They'll be created for you. I'm actually going to show you an example of this in just a few minutes. We'll see how this is actually accomplished. And new at version 10 is the ability to take any tool in the toolbox and put it into a toolbar or into a context menu.

34:46

So this is something that you no longer have to just run tools from the toolbox, you could just close the toolbox out,

34:53

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

34:59 35:03 35:09 35:15 35:22 35:25

So this is something people have been asking about for awhile. You can now do it in version 10. And, yes, you can take a script tool and add it to a model. So ModelBuilder can work with the scripts as well and, like I said, it's just another tool. So you can put basically any tool into ModelBuilder and execute it from that if you'd like. I'm going to go back to ArcMap and we're going to look at a demonstration. 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 can execute that.

35:33 35:37

So, what I'm going to do is I'm going to zoom into a particular area here on the map. This is the boundary between two cities. And you'll notice the boundary between the two cities has been badly digitized.

35:46 35:51 35:55 35:58 36:04 36:10 36:13

They're not really coincident. You'll see a lot of digitizing errors along the boundary. There is now a new toolbox added to version 10 that does editing for you. So there's an editor toolbox that's now been added. And I'm going to use one of the scripts to access a snapping tool that's in that toolbox. The snapping tool is going to, in turn, snap the vertices between these two boundaries together. So in essence it's going to do some editing for me automatically. So this was actually a nice new feature that's been added to 10. First, though, I need to make a script tool for this.

36:19 36:25

I'm going to go to ArcCatalog to do that. Notice we can now run ArcCatalog from ArcMap. I'm going to go into my Toolboxes, and I have to create a toolbox in order to add a script to it.

15 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

36:33

I'm going to create my own custom toolbox and in the toolbox, I'm going to now right-click it and add a script.

36:40 36:45 36:48 36:53 36:58 37:03

A script already existsactually, I think what I want to do right now is show you the script. This might make more sense if you actually see what the script looks like. So I'm going to go to the Python window here, I mean to PythonWin, the editor. This is the script I'm just about to add to ArcMap. So if you want to see what's in the script, you'll notice that we're importing the ArcPy module that we just talked about earlier. And what I really want to point out to you is this, these two lines of code. These are Python arguments.

37:10

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

37:15 37:20 37:28

It's going to expect that that data is going to be given to it. Since I'm running the snapping tool, the snap tool needs to know what layer am I going to snap to and then what the snapping distance is that I'm going to be working with. So I'm going to have these two arguments supply that information to the tool.

37:34 37:37

In other words, the user of this tool is going to be able to supply that so they could run this tool in any number of different feature classes using different snap distances if they would like.

37:45 37:49

So now that we know what it looks likethe other reason I'm using a script for this tool is this: Since this is an editing tool, I'm going to have it automatically make a backup of my data before it executes the tool.

37:56 38:03

So, in this case, we always run this tool from a script tool from a script like this, we'll always ensure that the backup will always get created first. That way we know that even if something goes wrong,

38:10 38:17 38:23

we can rely on the backup to replace it. I'm going to return to ArcMap. In ArcMap, remember, I'm adding my tool to this toolbox now. So I'm going through this wizard. 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 my tool Snap,

38:29 38:34 38:39 38:44 38:51 38:55 39:02

same thing for the label. I'll just give it the same name. We'll go to the next. Then it wants to know which tool, which script, do you want to add to this particular tool I'm creating and that's going to be that snapping script that I just looked at. I'm going to open that tool and add that snapping Python script to this. And the next dialogthis is the important onethis is where we supply those two arguments. So remember the script has two arguments in it. We need to create a dialog box for the user to fill out. So this form that you see in front of you is going to do that for you.

16 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

39:06 39:14

So I'm going to prompt the user to "Select a layer." This is going to be the snapping layer. Then I need to tell it what's the data type. In other words, what kind of data am I going to ask the user to supply?

39:20 39:23 39:27 39:32 39:38 39:45 39:51 39:57

And you see there are all kinds of data I can ask the user for here. The one that we're interested in is called a Feature Layer. The second argument that I need to prompt the user for is the snapping distance. So I will ask the user to "Enter a snap distance" and the data type that works the best for a snap distance like this would be called a linear unit. So I find a Linear Unit type. This is going to prompt the user for a number and a unit of measure, such as meters or centimeters or feet or inches, whatever the interest is. You'll see that there's a number of properties down here I can also set. I'm just going to default all those.

40:04

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

40:11

Again, it looks and behaves just like any other tool does. If I were to double-click this toolwe'll run the 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 a layer.

40:30 40:34

I'm going to pick out the Mineral Springs layer, then it wants the snap distance. I'm going type in 30, and I think I will make mine just 30 meters. It's kind of large for a snapping distance,

40:41 40:46

but you'll see that it should work for us. I'm now going to execute this by clicking OK. It's going to run the script, and you see after the script runs you see now that it cleaned up the borders.

40:56 40:59 41:04 41:09 41:13

It snapped all the vertices together for me. It did that automatically. So for those of you that have to do this kind of work or you were maybe manually digitizing this, you might actually have a script to be able to go out and clean up a lot of the boundaries and lines and points that aren't contiguous like you'd like them to be. So this is going to be a real time saver. Remember, you can do this at version 10 with that new snap tool.

41:18

There are other editing tools in that toolbox as well. There's one that will extend lines and so on for you.

41:24 41:34

So snapping's not the only one available.I'm now going to turn it back over to Bruce. Bruce:Thanks, Mike. Another Mike from Albany asks, "Will you be able to access and/or update metadata with ArcPy?"

41:40

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

17 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

41:47 41:54 41:58 42:01 42:05 42:10 42:15 42:20 42:27 42:31

and validate and so forth your metadata with ArcPy. Loan from Livermore asks, "Can you set Python to run specific code automatically once a month?" Yes. You use your operating system to do this Loan. In the Windows environment, you'd use Windows Scheduler and UNIX environments you'd use CRON and then the operating system will call Python and within that, of course, you can call ArcPy functions any time you want. Nenad from Wellington asks, "Hello, Will some existing Python scripts created at 9.3.1, that are running as scheduled tasks, need to be modified for ArcGIS10 (with regard to modules, object model, case sensitivity)?" The short answer is, Nenad, no. They will continue to work just as they do currently. You won't have to do any porting of your existing Python code.Vincent from Indooroopilly in Australia says,

42:37 42:42 42:49 42:57 43:00 43:05 43:10

"Can you use Python without ArcGIS running?" Yes, you can, Vincent. You don't need to have any ArcMap or ArcCatalog session open for a Python script to import ArcPy and execute any of its functions.Michael from Pontotoc in Tennessee asks, "After typing several commands into the embedded Python window, can the code be exported from the window to a Python script or do you have to copy and paste?" You can do either, Michael. You can with a context menu on a right-click you can export the history of the Python window to a PY or TXT file on disk or you can if you want select and copy/paste all of it independently.

43:19 43:26 43:29

Marisol from San Francisco asks, "Is the Python window available for ArcGIS 10 only or also for 9? And if we write Python code for 9, will we be able to read it in version 10?" The Python window is only in ArcGIS 10, Marisol. If you write Python code for ArcGIS 9, yes you can read it in version 10.

43:36

You can import it into the Python window or continue using it in existing scripts. Dinesh from Milton Keynes in the UK asks,

43:44 43:48 43:54

"Can Python code written in IDLE be imported into ArcGIS 10 Desktop?" It's very simple, Dinesh, just save your development work out of your window in IDLE to a PY file and you can import that into the Python window indirectively in desktop or use it straightaway in a script tool.

44:01

Mark from Corvallis asks, "Can you still use arcgisscripting?" Yes, Mark. ArcPy is built using arcgisscripting,

44:07

and arcgisscripting is still fully available. You can continue coding the way you used to with arcgisscripting

44:14 44:20

if you wish or you can port your workflows to ArcPy. Claudia from Sioux Falls asks, "Using Python in the Field Calculator, does it include the power function?" Yes it does, Claudia.

18 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

44:25

In fact, all of the math module is imported into the Field Calculator now and the power function is certainly there.

44:31 44:35 44:41 44:47

Tom from Columbia Falls asks, "Why is Esri moving away from VB? Will VB be in future releases or is everything going to Python?" With respect to VBA, Tom, we're moving away from VBA after ArcGIS 10 because Microsoft is removing support for it so we can't support it ourselves in absence of their support. For VB script, that's still supported in Windows applications,

44:54

it remains our way to do things like label expressions and indeed you can use it in the Field Calculator currently.

45:01 45:05 45:10

The last one we have time for this session, Claudia from Sioux Falls asks, "Does the Python window replace the command line window you find in previous versions of ArcGIS?" Yes, you're exactly right there, Claudia. The Python window does replace the command line window from 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 been discussing,

45:30 45:35

and what we want to focus on right now is the new mapping module that's been added to version 10. I think a lot of you are going to find this very interesting. The mapping module is what's going to give you access

45:41

to a map document and you can have quite a lot of access to it. So that means we can access data frames, layers.

45:49

We can have access to layouts actually, so you can do quite a bit with this new module that's been added.

45:56

There's a lot of documentation on this, a lot of sample scripts that you can look at that would show you examples

46:01

of how this is being used. Now I want you to understand that this is not going to allow you to create a map document,

46:09 46:14

but you can manipulate existing map documents. So you can open those documents up, retrieve all the contents that are within it, look at it.You can even query and find certain things within it,

46:19

and you even have the ability now to print a map. You can export a map out into various graphics formats.

46:26 46:32

You can even save it as a copy or you can save over an existing map document if you'd like to. So you can make changes to it, save it outjust very much like you would do if you were to manually open it

46:37

and edit it and work on your map document.

19 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

46:41 46:45 46:51 46:54 46:59 47:04

You can use scripts then to automate your workflows that involve map documents. This is something you could not do in prior versions. In one of the bullets here that you see, it's called update and repair data source. I think a lot of you will find this very interesting. This is going to allow you to go into a map document and repair any broken links or any sources that are broken in your layers. So you see the familiar red exclamation point, a mark that you have beside each of your layers that are broken. You're now going to be able to fix that through code.

47:10 47:14

You can do this in a batch mode if you'd like. So this is going to be something I think a lot of users will find very useful. Not only that, you could use it to create a report. You can open 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 some type of report out of it.

47:27 47:33 47:35 47:42

You can also use Python scripts to automate the new PDF map books that are in version 10 now. So if you want to automate that kind of a workflow, you can use that with the new map books that you're going to see at version 10. Looking at the map document, you're going to have access to data frames and layers within that map document.

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 47:58

The same thing with layers: you can add layers; you can remove layers.Not only that, we're also going to have access to the properties, so we can manipulate properties of a data framelike you see here.

48:05

For instance, you might want to change the rotation of a data frame or you want to change the extent of it.

48:11

We could apply a different coordinate system to it if we wanted to. If you look at the example code down below here,

48:18 48:25 48:29 48:32 48:36

it's exactly what it's doing. The code you're seeing is going to find a map document called Project.mxd. It's going to go into that map document and make a list of all the data frames within it. So here's the list again and the for loop that we've been talking about. I'm going to loop through that list of data frames, one data frame at a time. I'm going to set each one to a zero degree rotation, set the scale at 24,000, and then export it out as a TIFF image.

48:45 48:49 48:55

So I'm going to do this again in a batch mode for each data frame that's in that map document. You could do the same thing for layouts actually. Let's switch over to layers. We have access to most of the layer properties. So if you're familiar with layer properties,

20 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

48:59 49:05

you'll recognize what's in this list. For instance, we can change the transparency settings. We can turn a layer on and off. We can turn its labels on and off. We can change the source data again.

49:11 49:16 49:22 49:26

That's one you'll be particularly interested in. And if you look at the sample code below here, you'll see an example of opening up a layer file. Notice in this case we're actually opening a layer file out on your hard drive. The layers don't have to be in a map document. We can access layer files independently if you'd like. We're going to take the layer file. We're going to go into it.

49:32 49:38

We're going to set a name for it to call it highways instead of streets. We're going to turn its labels on and then we're going to save a copy of this out. So in essence we just took a layer,

49:45

made some changes to it, and saved it out as a different layer. So now we have one layer that has streets

49:50 49:56 50:00 50:05 50:09 50:17 50:22 50:25 50:32 50:41

without labels and another layer that has streets with labels. And we did this all through code. So it's just an example of how you can manipulate layers within a map document or within a layer file and work with it. I'm going to go back to ArcMap. I'm going to show you an example, a couple of examples actually, of how we can manipulate layers and work with these. I'll put up my table of contents. You're going to see we have a layer in here called Hydrants. These are fire hydrants, and what I'm going to do is I'm going to open up the Python window again, clear out the old code and what I'm going to do, I'm going to load a script in once more. This script is going to change a layer. So I'm going to open the script, and what I want to show you is a couple lines of code in here. I'm going to go up to the top of the script and notice here when we find...

50:46

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

50:53 50:59 51:06 51:11 51:15 51:21 51:26 51:33

This means that this script will use whatever the current map document is that I have running. So if I run this code from the Python window or as a script tool, like we just talked about, it will automatically find the current map document and go into that and do something with it. What I'm going to do with this map document is I'm going to find a layer called Hydrants. I'm going to take that layer once it's located, and I'm going to define a definition query for it. I'm going to change its name to Dresser Hydrants. I'm going to make it visible, turn it on, and we're going to turn the labels on for it. So let's execute thiswe're going to press the Enter key. It's already done; works very quickly. Im going to go to the table of contents and we'll see what the results are.

21 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

51:39 51:45

I'm going to refresh this. Youll notice the layers now have been changed to Dresser Hydrants, and if I turn it on and off here, you'll see that it also turned the labels on, so the labels are actually part of this now.

51:52 51:56 52:02 52:05

We turn it on. And if I were to go to the properties, the layers properties, you will now notice that the definition query has been defined just like I told it to do in the script. So the script actually manipulated all these properties for me. Again, you can do this with any layers that are within this map document. We can also rearrange layers,

52:12 52:17

turn layers on and off, you could remove a layer, add a layer to this map if you wanted to. There's actually quite a lot you can do with this. This is just a simple example of how you can manipulate this.

52:23 52:35 52:39 52:43

I'm going to show you one more example. In this one, I'm going to open up a different map document. In this map document I'm opening, it's going to have four layers that are broken. In other words, the sourceit cannot find the source information for those layers. This is a fairly typical problem. It happens any time you change the name of your data, the source data,

52:48 52:51

or you move it from one location to another one. A lot of you are probably well aware of when you do that your map documents can no longer find the source data.

53:02 53:11

Let me try this again. Open it up. Okay, here's what I'm talking about. You see in this new map document there are four layers with the all-too-familiar red exclamation mark next to them,

53:20 53:24 53:29 53:33 53:38 53:46 53:52

meaning that these are broken. You see that the layers don't show up on the map. What I'm going to do is I'm going to run a script from PythonWin that's going to fix this. So first I'm going to set my map document to a blank map. I'm going to do this because I'm actually going to save over that map document. I can't save it as long as it's open in ArcMap. I'm going to switch over to PythonWin. We'll close the old code out and this will be the script I'm looking at right here, it's called Fix Broken. Essentially, you see it's a very simple script. Manipulating map documents like this doesn't have to be anything

53:57 54:03

really difficult. Again, you can find this kind of code in the Desktop Help, there are samples of this. Notice what we're doing. We're going to import the ArcPy module. Were then going to find the broken map MXD,

54:11 54:14

so we're locating the map document. And this is what's interesting here: I'm going to go into that map document and make a list of all the broken layers,

22 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

54:19 54:24 54:28 54:33 54:36 54:41

all the layers that have broken data sources, will be put into my list and then I'm going to go and change the source name of those layers. So it's only going to work on the broken layer. It leaves the other layers alone. We're going to change the name of it and make them correct. Now what's really interesting with this is notice that I'm now going to save this map document. 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 it from PythonWin.

54:51 54:56

It works very quickly so it's already finished. We're going to go back to ArcMap. We will now open up that same map document again and this time we should see four layers that have been fixed.

55:04 55:11 55:15

So we should see the layers draw on the screen. Now you see the layers within the table of contents, see everything's fixed now. So, all is right with this map document. This is much better and much faster than we used to work with when you had to manually edit this and work 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 lot of time.

55:36 55:42 55:48 55:50 55:56

I'm going to turn this back over to Bruce. Bruce: Thanks, Mike. Eagle in Parnell asks, "Are you planning to allow Python to calculate labels or use Python for label expressions?" Yes, it is planned, although it won't be in the release of ArcGIS 10. You can look forward to using Python and label expressions in a future release. Georgeta from Boulder asks, "Can we create an MSD using Python or publish the MSD as a service in the same script?"

56:02

If other people are not aware, an MSD is a map service document which is optimized for rendering of the map

56:10

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

56:18 56:22

we don't currently support the writing of MSDs with arcpy.mapping. So, because we're expecting another authoring step between the authoring of the map and the publishing MSD,

56:27 56:32

we're not supporting writing MSDs and publishing them in the same script. Aaron from Denver asks, "Is there a Python script for snapping one layer to another, rather than within itself?"

56:38 56:45

Well, Aaron, yes the snap editing tool at ArcGIS 10 supports what's called a snapping environment. You can use any number of other feature classes as snapping targets

23 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

56:50 56:56 57:01

in different ways in addition to the feature class being edited.Georgeta from Boulder again asks, "Can you share a tool based on a Python script but with a compiled version of the script?" Georgeta, we're not supporting compiled versions of scripts for script tools because there's really not much point.

57:08 57:11 57:16 57:21

Compiled Python runs at the same speed as the ordinary PY files, but I think what you're getting at there is can you embed a script and somehow encrypt it. Well we password or let you password-protect scripts. There's one way you can share a script but not let it be fully exposed. Patty from Corvallis asks, "Can you run a cursor in ModelBuilder through a script?"

57:32 57:38

Well, I think a couple of options there for you, Patty, at ArcGIS 10 we have a model process 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 57:50

So that's one way of looking at that. Another way is, yes you can outright run a cursor in ModelBuilder by using the Calculate Value model tool in ModelBuilder which runs any Python code that you put in there.

57:58 58:05

Russell from Edna asks, "Can the script add output to a new map or dataset instead of the current?" Yes, Russell, you can write out any layer in memory and add it to any other map document you create on the fly

58:11 58:15

or have on disk, so you can add output to a new map or dataset instead of the current one. Claudia from Sioux Falls asks, "Is it possible to automatically add symbologies to several outputs from a Python script?"

58:23

Yes, Claudia, provided that you can retrieve the symbologies that you need from other existing layers or map documents

58:29 58:36 58:41 58:46 58:51

then you can automate the application of them to new outputs from scripts. Time for one more: Kamal from Toronto asks, "Can Python be used to create a list of files used and their locations with an MXD file?" Yes, Kamal, it's one of the primary use cases that we see for the ArcPy mapping module. You can certainly generate a report of the data sources used 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 59:05 59:11 59:15

Before we go, we'd like to point you to a few resources that you see on the slide here on the screen. If you want to know more about geoprocessing tools, there's an online Web training class you can take that will explain a lot of that to you. If you're more interested in learning Python and how you can work with some of the advanced functions that we talked about, we have an instructor-led training class,

59:22

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

24 trong 25

6/7/2012 11:09 AM

Using Python in ArcGIS Desktop 10

http://training.esri.com/Courses/PythonDesktop10_0/transcript189609_1.cfm

59:26 59:33 59:39 59:43

This will go into all the details about working with scripts. If you want other resources to learn Python, there are a number of books available to you online or in book stores that are fairly good that can give you information about all the basics of working with Python. There are also Web sites that are devoted to Python like python.org, for instance, is a good one that you can rely on.

59:51 59:55

We have other seminars like this one in the weeks and months ahead that you can also watch to learn more about ArcGIS 10 so there'll be more information forthcoming. I hope you found this seminar helpful.

60:03 60:08 60:15 60:21 60:26

I hope this has given you some ideas about how you can use Python and integrate it with ArcGIS and make your workflows easier and make your work go a little bit faster. Your comments help us to improve our seminars, so please take a moment to complete our survey. Just click the Give Us Feedback link to take the survey. We hope you enjoyed today's seminar. On behalf of Esri, I'd like to thank all of you for attending.

25 trong 25

6/7/2012 11:09 AM

Das könnte Ihnen auch gefallen