Working with cursors in Python GISDE Python Workshop Qiao Li.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Access 2007 ® Use Databases How can Microsoft Access 2007 help you structure your database?
Why python? Automate processes Batch programming Faster Open source Easy recognition of errors Good for data management What is python? Scripting programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Automating Tasks With Macros
Automating Tasks With Macros. 2 Design a switchboard and dialog box for a graphical user interface Database developers interact directly with Access.
Python & ModelBuilder. Overview Python/ModelBuilder Concepts – The Geoprocessor – Checking some environment variables – Providing feedback from your model/script.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Advance Model Builder Features. Advance Features Using Lists (also Batching) Iteration Feedback Model Only Tools Inline Variable Substitution Preconditions.
MS Access: Database Concepts Instructor: Vicki Weidler.
8 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
Module 3: Table Selection
Python & ModelBuilder. Continuing Education Python and ModelBuilder Overview Python/ModelBuilder Concepts –The Geoprocessor –Checking some environment.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
1/29 Former leader of the “free” world … “I know what I believe. I will continue to articulate what I believe and what I believe.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
XP New Perspectives on Microsoft Access 2002 Tutorial 51 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith.
Overview Cursors arcpy.da module Geometrys Arrays SpatialReferences
Introduction to ArcPy. Topics What is ArcPy? Accessing geoprocessing tools using ArcPy Writing scripts using ArcPy.
Introduction to Microsoft Access Overview 1. Introduction What is Access? A relational database management system What is a Relational Database? Organized.
Microsoft ® Access ® 2010 Training Create Queries for a New Database If a yellow security bar appears at the top of the screen in PowerPoint, click Enable.
If statements while loop for loop
Basic & Advanced Reporting in TIMSNT ** Part Two **
Key Applications Module Lesson 21 — Access Essentials
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
Built-in Data Structures in Python An Introduction.
Access 2007 ® Use Databases How can Microsoft Access 2007 help you structure your database?
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Maintaining a Database Access Project 3. 2 What is Database Maintenance ?  Maintaining a database means modifying the data to keep it up-to-date. This.
Course ILT Forms and queries Unit objectives Create forms by using AutoForm and the Form Wizard, and add or modify form headers and footers Open and enter.
Introduction of Geoprocessing Lecture 9. Geoprocessing  Geoprocessing is any GIS operation used to manipulate data. A typical geoprocessing operation.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
implicit and an explicit cursor
Juanita Cano City of Sacramento Spring 2014 Geography 375.
NSF DUE ; Wen M. Andrews J. Sargeant Reynolds Community College Richmond, Virginia.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
Excel part 5 Working with Excel Tables, PivotTables, and PivotCharts.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Access Queries and Forms. Adding a New Field  To insert a field after you have saved your table, open Access, and open the table  It is easier to add.
Python: Building Geoprocessing Tools David Wynne, Ghislain Prince.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Using Structures With CFCs By Selene Bainum. June 27 th - 30 th 2007www.cfunited.com Why Am I here? Familiar with structures Familiar with ColdFusion.
Lecture 10: Geoprocessing with Python (II) Dr. Taysir Hassan Abdel Hamid Associate Professor, Information Systems Dept., Faculty of Computers and Information.
Technical Workshops | Esri International User Conference San Diego, California Branching and Iteration in ModelBuilder Shitij Mehta July 24, :15.
Introduction to Database Programming with Python Gary Stewart
Web Database Programming Using PHP
A Guide to SQL, Seventh Edition
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Web Database Programming Using PHP
Final Project: Read from a csv file and write to a database table
Error Handling Summary of the next few pages: Error Handling Cursors.
ISC440: Web Programming 2 Server-side Scripting PHP 3
Creating and Modifying Queries
File Handling Programming Guides.
CIS16 Application Programming with Visual Basic
Exploring Microsoft® Access® 2016 Series Editor Mary Anne Poatsy
Developing a Model-View-Controller Component for Joomla Part 3
CodePainter Revolution Trainer Course
PYTHON: BUILDING GEOPROCESSING TOOLS
Presentation transcript:

Working with cursors in Python GISDE Python Workshop Qiao Li

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

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.

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().

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

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.

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.

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.

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.

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”.

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

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.

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

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.

An example to insert a new state into the attribute table

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?

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.

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

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

GetParameterAsText Make your inputs as parameters!

Add message

Creating the tool Demo