Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

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

3 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

4

5 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

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

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

8 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

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

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

11 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

12 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

13 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

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

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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24

25

26

27

28

29


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

Similar presentations


Ads by Google