Lecture 10: Geoprocessing with Python (II) Dr. Taysir Hassan Abdel Hamid Associate Professor, Information Systems Dept., Faculty of Computers and Information.

Slides:



Advertisements
Similar presentations
Why python? Automate processes Batch programming Faster Open source Easy recognition of errors Good for data management What is python? Scripting programming.
Advertisements

ESRM 250 & CFR 520: Introduction to GIS © Phil Hurvitz, KEEP THIS TEXT BOX this slide includes some ESRI fonts. when you save this presentation,
NR 322: Editing Spatial Data Jim Graham Fall 2008 Chapter 6.
NR 322: Single Layer Analysis Jim Graham Fall 2008 Chapter 8 & 9.
Lecture 16: Data input 1: Digitizing and Geocoding By Austin Troy University of Vermont Using GIS-- Introduction to GIS.
Fundamentals of GIS Materials by Austin Troy © 2008 Lecture 18: Data Input: Geocoding and Digitizing By Austin Troy University of Vermont NR 143.
GIS Customization I. Binaural recording + reconstructing performances Holophonics - Virtual Barber Shop John Q. Walker: Re-creating great performances.
ModelBuilder In ArcGIS 9.x By Tim Weigel GEOG 407/607 April 3 rd, 2006.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
1 Computing for Todays Lecture 16 Yumei Huo Fall 2006.
arcpy.Describe and arcpy.List*
@ 2007 Austin Troy. Geoprocessing Introduction to GIS Geoprocessing is the processing of geographic information. Perform spatial analysis and modeling.
To find a suitable land to develop a dream vacation home GEO376 Final Python Project Helen Peng May 2010.
Advance Model Builder Features. Advance Features Using Lists (also Batching) Iteration Feedback Model Only Tools Inline Variable Substitution Preconditions.
Esri UC 2014 | Technical Workshop | Data Alignment and Management in ArcMap Lisa Stanners, Sean Jones.
NR 322: Organizing Data Jim Graham Fall GIS Data Flow Project, resample Processing Processed Data Original Data Final data And maps.
Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.
Working with cursors in Python GISDE Python Workshop Qiao Li.
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.
NR 422: Topology Jim Graham Fall 2010 See: odatabase-topology.pdf.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Overview Cursors arcpy.da module Geometrys Arrays SpatialReferences
Introduction to ArcPy. Topics What is ArcPy? Accessing geoprocessing tools using ArcPy Writing scripts using ArcPy.
Python: An Introduction
Functions Reading/writing files Catching exceptions
Major parts of ArcGIS ArcView -Basic mapping, editing and Analysis tools ArcEditor -all of ArcView plus Adds ability to deal with topological and network.
Using Describe. Topics How to use describe? Retrieving Descriptive Information about data.
School of Geography FACULTY OF ENVIRONMENT Introduction to ArcToolbox and Geoprocessing.
GIS Tutorial 1 Lecture 4 Geodatabases. Outline  Data types  Geodatabases  Data table joins  Spatial joins  Field calculator  Calculate geometry.
Exploring ArcToolbox Presented by: Isaac Johnson.
Copyright © 2006 by Maribeth H. Price 2-1 Chapter 2 Working with ArcMap.
If statements while loop for loop
Copyright © 2006 by Maribeth H. Price 8-1 Chapter 8 Geoprocessing.
Basic & Advanced Reporting in TIMSNT ** Part Two **
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
What have we learned?. What is a database? An organized collection of related data.
Query and Reasoning. Types of Queries Most GIS queries will select spatial features Query by Attribute (Select by Attribute) –Structured Query Language.
Geographic Data in GIS. Components of geographic data Three general components to geographic information Three general components to geographic information.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Juanita Cano City of Sacramento Spring 2014 Geography 375.
NSF DUE ; Wen M. Andrews J. Sargeant Reynolds Community College Richmond, Virginia.
Intro. To GIS Pre-Lab Spatial Analysis April 1 st, 2013.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Lecture 3: Spatial Data Management Dr. Taysir Hassan Abdel Hamid Associate Professor, Information Systems Dept., Faculty of Computers and Information.
Outline of Script Import Modules Setup Workspace Environment and Assign Data Path Variables Summary of Script Title and Author Info.
Lesson 17 Mail Merge. Overview Create a main document. Create a data source. Insert merge fields into a main document. Perform a mail merge. Use data.
Introduction to Geodatabases
Geocoding Chapter 16 GISV431 &GEN405 Dr W Britz. Georeferencing, Transformations and Geocoding Georeferencing is the aligning of geographic data to a.
Introduction to GIS Programming Final Project Submitted by Todd Lenkin Geography 375 Spring of 2011 American River College.
ASP.NET Programming with C# and SQL Server First Edition
Lecture 9: Geoprocessing with Python
GIS Institute Center for Geographic Analysis
Python – Beyond the Basics
PYTHON: AN INTRODUCTION
Lecture 08 Creating a Geodatabase
Lecture 22: Using ArcToolbox Tools in Python
Final Project: Read from a csv file and write to a database table
Lecture 9 Using Python for Geoprocessing
Lecture 10 Accessing tools and environment setting in Scripts
CIS16 Application Programming with Visual Basic
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Chapter Four UNIX File Processing.
This lecture Introduction to arcpy Debugging Using arcpy.
GIS Institute Center for Geographic Analysis
Vector Geoprocessing.
Python – Beyond the Basics
GIS Institute Center for Geographic Analysis
Presentation transcript:

Lecture 10: Geoprocessing with Python (II) Dr. Taysir Hassan Abdel Hamid Associate Professor, Information Systems Dept., Faculty of Computers and Information Assiut University April 18, 2016 IS311: GIS. Dr. Taysir Hassan A. Soliman

Outline Cursor functions Tool Messages IS311: GIS. Dr. Taysir Hassan A. Soliman

Cursor function A Cursor Returns a Cursor object against the specified feature class, shapefile, or table Cursor allows access of a collection of records. It allows: – Iterating over the set of rows in a table – Inserting new row in a table – Accessing a geometry Cursors read and write values while looping through a cursor, one record at a time. This can allow us to edit a table by adding or deleting records IS311: GIS. Dr. Taysir Hassan A. Soliman

Search Cursor The SearchCursor function establishes a read- only cursor on a feature class or table. The SearchCursor can be used to iterate through row objects and extract field values. The search can optionally be limited by a where clause or by field and optionally sorted. IS311: GIS. Dr. Taysir Hassan A. Soliman

SearchCursor IS311: GIS. Dr. Taysir Hassan A. Soliman

Example: List field contents for Counties.shp. Cursor sorted by State Name and Population. IS311: GIS. Dr. Taysir Hassan A. Soliman

Insert Cursor Inserts rows into a feature class, shapefile, or table. The InsertCursor returns an enumeration object that hands out row objects. Each call to insertRow on the cursor creates a new row in the table whose initial values are set to the values in the input row. IS311: GIS. Dr. Taysir Hassan A. Soliman

Insert Cursor IS311: GIS. Dr. Taysir Hassan A. Soliman Syntax: InsertCursor (dataset, {spatial_reference})

InsertCursor IS311: GIS. Dr. Taysir Hassan A. Soliman

Example : Insert Cursor Inserts 25 new rows into a table. import arcpy # Create insert cursor for table rows = arcpy.InsertCursor ("c:/base/data.gdb/roads_lut") # Create 25 new rows. Set the initial row ID and distance values for x in range(1, 26): row = rows.newRow() row.setValue("rowid", x) row.setValue("distance", 100) rows.insertRow(row) # Delete cursor and row objects to remove locks on the data del row del rows IS311: GIS. Dr. Taysir Hassan A. Soliman

UpdateCursor The UpdateCursor function creates a cursor that lets you update or delete rows on the specified feature class, shapefile, or table. The cursor places a lock on the data that will remain until either the script completes or the update cursor object is deleted IS311: GIS. Dr. Taysir Hassan A. Soliman

UpdateCursor Update cursors are able to be iterated with a for loop or in a while loop using the cursor's next method to return the next row. When using the next method on a cursor to retrieve all rows in a table containing N rows, the script must make N calls to next. A call to next after the last row in the result set has been retrieved returns None, which is a Python data type that acts here as a placeholder. IS311: GIS. Dr. Taysir Hassan A. Soliman

UpdateCursor IS311: GIS. Dr. Taysir Hassan A. Soliman

UpdateCursor IS311: GIS. Dr. Taysir Hassan A. Soliman

UpdateCursor #Update field values in feature class, based on another field's value. import arcpy # Create update cursor for feature class rows = arcpy.UpdateCursor("c:/data/base.gdb/roads") # Update the field used in buffer so the distance is based on the # road type. Road type is either 1, 2, 3 or 4. Distance is in meters. for row in rows: # Fields from the table can be dynamically accessed from the # row object. Here fields named BUFFER_DISTANCE and ROAD_TYPE # are used row.setValue("BUFFER_DISTANCE", row.getValue("ROAD_TYPE") * 100) rows.updateRow(row) # Delete cursor and row objects to remove locks on the data del row del rows IS311: GIS. Dr. Taysir Hassan A. Soliman

Tool messages When we execute a tool, there might be three types of messages: – Informative messages (severity =0) – Warning messages (severity =1 – Error messages (severity = 2) try: # start try block arcpy.Buffer (“C:/ws/roads.shp”, “C:/outws/roads10.shp”, 10) # print the tool messages except arcpy.ExecuteError: print arcpy.GetMessages (2) # any other error Except Exception as e: print e.message IS311: GIS. Dr. Taysir Hassan A. Soliman

Functions Functions perform useful tasks, such as: – Accessing geoprocessing tool messages ( GetMessages ) – Listing data for batch processing, e.g.: ListFeatureClasses, ListFields, plus nine other list functions – Retrieving a dataset’s properties (Describe) import arcpy # Set the workspace for ListFeatureClasses function arcpy.env.workspace = “c:/test” # For each feature class, create a scratch name and clip for fc in arcpy.ListFeatureClasses (): outName = arcpy.CreateScratchName (“clipped_” + fc, “”, “featureclass”, arcpy.env.workspace) arcpy.Clip_analysis(fc, “boundary”, outName) IS311: GIS. Dr. Taysir Hassan A. Soliman

Dealing with Functions/Methods Assigning a value to a property: # object.property = valuefor example: env.workspace = “C:/Temp” Return the value of a property: # object.propertyfor example: print “The workspace is “ + env.workspace Use a method: # object.method (arg1, arg2, …) e.g., put a buffer for a road: arcpy.Buffer_analysis (“c:/input/roads.tif’, “c:/output.gdb/buffer_output, 100) IS311: GIS. Dr. Taysir Hassan A. Soliman

The Describe function Using the Describe function, a dataset's properties can be determined and used to make decisions.Describe Takes some feature class, table, raster image (e.g., properties: type, number of bands, resolution), database, workspace, and describe it – e.g., we can find how many fields a table has, what is their type and name Returns an object with dynamic properties Allows script to determine properties of data, e.g.: – Data type (shapefile, coverage, network dataset, etc) – Shape type (point, polygon, line) – Spatial reference – Extent of features – List of fields IS311: GIS. Dr. Taysir Hassan A. Soliman

the following script uses Describe to evaluate the shape type (polyline, polygon, point, and so on) of input data and determine which geoprocessing tool is appropriate. IS311: GIS. Dr. Taysir Hassan A. Soliman

List functions Get a list of feature classes, tables, rasters, etc. Process data using a loop through the list # returns a list of feature classes, tables # for examples all the tables in a geodatabase, or fields in a table fcList = arcpy.ListFeatureClasses() # copy shapefiles to a file geodatabase one item at a time # loop through the list of shape files using copy management tool for fc in fcList: arcpy.Copy_management (fc, “d/base/output.gdb” + os.set + fc.rstrip(“.shp’)) IS311: GIS. Dr. Taysir Hassan A. Soliman

ListFeatureClasses Lists the feature classes in the workspace, limited by name, feature type, and optional feature dataset. The workspace environment must be set first before using several of the List functions, including ListDatasets,ListFeatureClasses, ListF iles, ListRasters, ListTables, and ListWorkspacesListDatasetsListFeatureClassesListF ilesListRastersListTablesListWorkspace IS311: GIS. Dr. Taysir Hassan A. Soliman