Lecture 10 Geoprocessing with Python II Dr Taysir

  • Slides: 29
Download presentation
Lecture 10: Geoprocessing with Python (II) Dr. Taysir Hassan Abdel Hamid Associate Professor, Information

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 IS 311: GIS. Dr. Taysir Hassan A. Soliman

Outline • Cursor functions • Tool Messages • IS 311: GIS. Dr. Taysir Hassan

Outline • Cursor functions • Tool Messages • IS 311: GIS. Dr. Taysir Hassan A. Soliman

Cursor function • A Cursor Returns a Cursor object against the specified feature class,

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 IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

Search Cursor • The Search. Cursor function establishes a readonly cursor on a feature

Search Cursor • The Search. Cursor function establishes a readonly cursor on a feature class or table. • The Search. Cursor 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. IS 311: GIS. Dr. Taysir Hassan A. Soliman

Search. Cursor IS 311: GIS. Dr. Taysir Hassan A. Soliman

Search. Cursor IS 311: GIS. Dr. Taysir Hassan A. Soliman

 • Example: List field contents for Counties. shp. Cursor sorted by State Name

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

Insert Cursor • Inserts rows into a feature class, shapefile, or table. The Insert.

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

Insert Cursor Syntax: Insert. Cursor (dataset, {spatial_reference}) IS 311: GIS. Dr. Taysir Hassan A.

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

Insert. Cursor IS 311: GIS. Dr. Taysir Hassan A. Soliman

Insert. Cursor IS 311: GIS. Dr. Taysir Hassan A. Soliman

Example : Insert Cursor Inserts 25 new rows into a table. import arcpy #

Example : Insert Cursor Inserts 25 new rows into a table. import arcpy # Create insert cursor for table rows = arcpy. Insert. Cursor ("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. new. Row() row. set. Value("rowid", x) row. set. Value("distance", 100) rows. insert. Row(row) # Delete cursor and row objects to remove locks on the data del rows IS 311: GIS. Dr. Taysir Hassan A. Soliman

Update. Cursor • The Update. Cursor function creates a cursor that lets you update

Update. Cursor • The Update. Cursor 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 IS 311: GIS. Dr. Taysir Hassan A. Soliman

Update. Cursor • Update cursors are able to be iterated with a for loop

Update. Cursor • 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. IS 311: GIS. Dr. Taysir Hassan A. Soliman

Update. Cursor IS 311: GIS. Dr. Taysir Hassan A. Soliman

Update. Cursor IS 311: GIS. Dr. Taysir Hassan A. Soliman

Update. Cursor IS 311: GIS. Dr. Taysir Hassan A. Soliman

Update. Cursor IS 311: GIS. Dr. Taysir Hassan A. Soliman

Update. Cursor #Update field values in feature class, based on another field's value. import

Update. Cursor #Update field values in feature class, based on another field's value. import arcpy # Create update cursor feature class rows = arcpy. Update. Cursor("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. set. Value("BUFFER_DISTANCE", row. get. Value("ROAD_TYPE") * 100) rows. update. Row(row) # Delete cursor and row objects to remove locks on the data del rows IS 311: GIS. Dr. Taysir Hassan A. Soliman

Tool messages • When we execute a tool, there might be three types of

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/roads 10. shp”, 10) # print the tool messages except arcpy. Execute. Error: print arcpy. Get. Messages (2) # any other error Exception as e: print e. message IS 311: GIS. Dr. Taysir Hassan A. Soliman

Functions • Functions perform useful tasks, such as: – Accessing geoprocessing tool messages (Get.

Functions • Functions perform useful tasks, such as: – Accessing geoprocessing tool messages (Get. Messages) – Listing data for batch processing, e. g. : • List. Feature. Classes, List. Fields, plus nine other list functions – Retrieving a dataset’s properties (Describe) import arcpy # Set the workspace for List. Feature. Classes function arcpy. env. workspace = “c: /test” # For each feature class, create a scratch name and clip for fc in arcpy. List. Feature. Classes (): out. Name = arcpy. Create. Scratch. Name (“clipped_” + fc, “”, “featureclass”, arcpy. env. workspace) arcpy. Clip_analysis(fc, “boundary”, out. Name) IS 311: GIS. Dr. Taysir Hassan A. Soliman

Dealing with Functions/Methods • Assigning a value to a property: # object. property =

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

The Describe function • Using the Describe function, a dataset's properties can be determined

The Describe function • Using the Describe function, a dataset's properties can be determined and used to make decisions. • 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 IS 311: GIS. Dr. Taysir Hassan A. Soliman

the following script uses Describe to evaluate the shape type (polyline, polygon, point, and

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. IS 311: GIS. Dr. Taysir Hassan A. Soliman

List functions • Get a list of feature classes, tables, rasters, etc. • Process

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 fc. List = arcpy. List. Feature. Classes() # 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 fc. List: arcpy. Copy_management (fc, “d/base/output. gdb” + os. set + fc. rstrip(“. shp’)) IS 311: GIS. Dr. Taysir Hassan A. Soliman

List. Feature. Classes • Lists the feature classes in the workspace, limited by name,

List. Feature. Classes • 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 List. Datasets, List. Feature. Classes, List. F iles, List. Rasters, List. Tables, and List. Workspaces IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman

IS 311: GIS. Dr. Taysir Hassan A. Soliman