Arcobjects icursor2/24/2023 ![]() Just as in a loop through a list, the iterator variable can be assigned a name of your choice (here, it's called "row"). A for loop is used to iterate through the SearchCursor (using the "cursor" name assigned to it).The "with" statement creates a SearchCursor object, and declares that it will be named "cursor" in any subsequent code.In the example above, the tuple contains just one field, "NAME". Supplying this tuple speeds up the work of the cursor because it does not have to deal with the potentially dozens of fields included in your dataset. Remember that a tuple is a Python data structure much like a list, except it is enclosed in parentheses and its contents cannot be modified. Creating a SearchCursor object requires specifying not just the desired table/feature class, but also the desired fields within it, as a tuple.After you create the cursor in your "with" statement, you'll initiate a for loop to run through all the rows in the table. The "with" statement requires that you indent all the code beneath it.This is a big issue with cursors, which can sometimes maintain locks on data if they are not exited properly. Although the explanation of "with" is somewhat technical, the key thing to understand is that it allows your cursor to exit the dataset gracefully, whether it crashes or completes its work successfully. The cursor is created using a "with" statement.Important points to note in this example: With arcpy.da.SearchCursor(featureClass,("NAME")) as cursor: # Prints the name of each city in a feature classįeatureClass = "C:\\Data\\USA\\USA.gdb\\Cities" Here's a very simple example of a search cursor that reads through a point dataset of cities and prints the name of each. Within the loop, do something with the values in the current row.Set up a loop that will iterate through the rows until there are no more available to read.This method takes several parameters in which you specify which dataset and, optionally, which specific rows you want to read. This is done through the method arcpy.da.SearchCursor(). The common workflow for reading data with a search cursor is as follows: An older, pre-10.1 class is available through the arcpy module and appears in the Help system as an "ArcPy Function.") ![]() (Be sure when searching the Help system that you choose the SearchCursor class found in the Data Access module. We'll discuss the editing cursors later, focusing our discussion now on the cursor used for reading tabular data, the search cursor.Īs with all geoprocessing classes, the SearchCursor class is documented in the Help system. The module contains different cursor classes for the different operations a developer might want to perform on table records - one for selecting, or reading, existing records one for making changes to existing records and one for adding new records. ![]() The arcpy data access moduleĪt version 10.1 of ArcMap, Esri released a new data access module, which offered faster performance along with more robust behavior when crashes or errors were encountered with the cursor. We will focus our discussion below on the current best practice in cursor usage, which you should be utilizing for scripts written in this course and in your workplace. If you find yourself working with code that was written for a pre-10.1 version of ArcMap, we suggest you check out this same page in our old ArcMap version of this course (see the content navigation links on the right side of the Drupal site). The arcpy module contains some objects called cursors that allow you to move through records in a table. There have been quite a few changes made to how cursors can be used over the different versions of ArcGIS, though the current best practice has been in place since version 10.1 of ArcGIS Desktop (aka ArcMap) and has carried over to ArcGIS Pro. PAv.Now that you know how to traverse the table horizontally, reading the fields that are available, let's examine how to read up and down through the table records. ' Loop through the cursor and count number of selected features. Set pFCursor = pFClass.Search(pSF, False) ![]() If pLayer.Name = "Layer1" Or pLayer.Name = "Layer2" Then '?MULITI CUR Set pLayer = pEnumLayer.Next Do Until pLayer Is Nothing Set pEnumLayer = pMap.Layers Dim pFLayer As IFeatureLayer PSF.SpatialRel = esriSpatialRelIntersects If pLayer.Name = "Layer1" Or pLayer.Name = "Layer2" The code works fine with one layer but when I add a second or third layer with an "OR" operator I only get the count of 0 because one layer may be visible within the extent but the others are not.Į.g. ![]() I would like to get a total count of multiple layer features (defined) within the ArcMap view extent. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |