Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with cursors in Python GISDE Python Workshop Qiao Li.

Similar presentations


Presentation on theme: "Working with cursors in Python GISDE Python Workshop Qiao Li."— Presentation transcript:

1 Working with cursors in Python GISDE Python Workshop Qiao Li

2 Agenda 1. Accessing data fields 2. Writing vector attribute data 3. Working with rasters 4. Making a script tool

3 1. Accessing data fields Fields in the tableFields in the table Fields in the table store the geometry and attribute information for the features. There are two fields in the table that you cannot delete. SHAPE—contains the geometry information for the feature. FID—contains a unique number, or identifier for each record that is used by ArcGIS to keep track of features.

4 Discovering field names When you write a script, you’ll need to provide the names of the particular fields you want to read and write. You can get a Python list of field names using arcpy.ListFields().

5 Reading through records—The search cursor The arcpy module contains some objects called cursors that allow you to move through records in a table.

6 Cursors can be tricky to understand at first, so let’s look at those lines more closely. The loop condition “while row:” is a simple Boolean way of specifying whether the loop should continue. You can read a field value as a property of a row. For example, row.NAME gave you the value in the NAME field. If your table had a POPULATION field, you could use row.POPULATION to get the population. The names “rows” and “row” are just variable names that represent the SearchCursor and Row objects, respectively. We could name these anything.

7 Here’s another example where something more complex is done with the row values. This script finds the average population for states in the dataset.

8 You can make your scripts more versatile by using variables to represent field names. Rows.getValue()You could declare a variable, such as populationField to reference the population field name, whether it were POP2010, POP2011, or simply POPULATION. The Python interpreter isn’t going to recognize row.populationField, so you need to use Rows.getValue() instead and pass in the variable as a parameter.

9 Using a for loop with a cursor Although the above examples use a while loop in conjunction with the next() method to advance the cursor, it’s often easier to iterate through each record using a for loop.

10 Retrieving records using an attribute query The previous examples used the SearchCursor object to read through each record in a dataset. You can get more specific with the search cursor by instructing it to retrieve just the subset of records whose attributes comply with some criteria, for example “only records with a population greater than 1,000,000”, or “all records beginning with the letters P-Z”.

11 2. Writing vector attribute data In the same way that you use cursors to read vector attribute data, you use cursors to write data as well. Two types of cursors are supplied for writing data: Update cursor – This cursor edits values in existing records or deletes records. Insert cursor – This cursor inserts new records

12 Modifying field values When you create an UpdateCursor and advanced it to a row, you can then modify field values. There are two ways you can modify a value: Row.setValue() Row.getValue() Row.setValue(),Row.setValue() is similar to Row.getValue() that you use with search cursors, but it's important to remember that with Row.setValue(), you have to supply two arguments: the field to update, and the new value for that field.

13 Update cursor A little trick here~: You can remove the possibility of locks affecting your work by deleting your cursors where you are done using them. Use the built-in del function to do this. You can even delete multiple objects on the same line. Notice that our find and replace example also deletes the row just to be safe: # Delete the cursor to remove any data locks del row, rows

14 Inserting new records When adding a new record to a table, you must use the insert cursor. As with the update cursor, you can avoid data locking problems by deleting the insert cursor when you’ve finished using it. Insert cursors differ from search and update cursors in that you cannot provide a SQL expression when you create the insert cursor.

15 An example to insert a new state into the attribute table

16 3. Working with rasters So far in this lesson, your scripts have only read and edited vector datasets. Raster data is very different, and consists only of a series of cells, each with its own value. So how do you access and manipulate raster data using Python?

17 List rasters Python is helpful in dealing with large dataset! ListRasters – returns a Python list of the rasters in the workspace, limited by name and raster type.

18 The following example is a python function clipping a list of raster files to a specific extension

19 4. Making a script tool Scripts that you create can be executed with one of two basic methods: outside ArcGIS and within ArcGIS. Understanding script tool parameters Understanding script tool parameters Creating a custom toolbox Creating a custom toolbox Writing messages in script tools Writing messages in script tools Customizing script tool Customizing script tool

20 GetParameterAsText Make your inputs as parameters!

21 Add message

22 Creating the tool Demo


Download ppt "Working with cursors in Python GISDE Python Workshop Qiao Li."

Similar presentations


Ads by Google