Tuesday, May 31, 2011

Using the Insert Cursor

This is my 4th article discussing the cursor objects in the ArcPy module.  In previous posts, I have talked about both search and update cursors in depth, so if you want to learn more about those, please feel free to search my earlier postings.

An InsertCursor inserts rows into a feature class or table.  The InsertCursor like the other cursor types returns an enumerable object that returns a row object.  New row objects can be created using a newRow() on the enumeration object (the cursor object).  After a row is created using the newRow(), use the insertRow() to save the newly created row into the existing table or feature class.

To create an InsertCursor, you just need to pass in a data set.  There is an optional parameter of spatial reference, which would be the spatial reference of the input data set, and a Cursor object is returned.
Here is a complete description:

Example of InsertCursor and a table

import arcpy
table = r"c:\temp\table.shp"
iCursor = arcpy.InsertCursor(table)
row = iCursor.newRow()
row.setValue("foo","bar")
iCursor.insertRow(row)
del iCursor
del row

In the example about, a single new row is created, and the field "foo" is filled with the value "bar" then inserted in the existing table.

Example of InsertCursor and a feature class

import arcpy

fc = r"C:\TEMP\pts.shp"
# Use the Describe() to get the Shape Field name
#
desc = arcpy.Describe(fc)
# Create the cursor
iCursor = arcpy.InsertCursor(fc)
# Create a new row
row = iCursor.newRow()
# Create new location
pt = arcpy.Point(-75,35)
# set the input values
row.setValue(desc.shapeFieldName, pt)
# insert the row
iCursor.insertRow(row)
# delete the cursor references to release
# schema locks
del iCursor
del row

Friday, May 27, 2011

Using the Update Cursors

The UpdateCursor() is a cursor object that allows for the update or deletion of rows in a feature class or table.  The cursor will place a lock on the data that will persist until the object is deleted or when the script terminates.

To create an UpdateCursor object, you must provide a data set on creation. As shown in the brief example below

ds = r"c:\temp\feat.shp"
uCursor = arcpy.UpdateCursor(ds)
# Do something with object


Here is a complete list of the optional and required parameters for UpdateCursor:
When you are done updating a row, call the updateRow() to commit your action that was performed on the row.

ds = r"c:\temp\feat.shp"
updateField = "Foo"
updateValue = "bar"
uCursor = arcpy.UpdateCursor(ds)
for row in uCursor:
   row.setValue(updateField,updateValue)
   row.updateRow(row)
del row, uCursor

In the above example, a feature class called feat.shp is having the field 'Foo' updated with the value 'bar'. Notice that the del function is called after the actions are performed. This should be done at the end of all cursor operation in order to drop the schema lock that is held on the data. If you do not, no other operations will be allowed on your data, and most likely the script will fail.

The deleteRow() on the UpdateCursor allows for the deletion of the current row of a table or feature class and is pretty straight forward to use:

ds = r"c:\temp\feat.shp"
uCursor = arcpy.UpdateCursor(ds)
for row in uCursor:
   uCursor.deleteRow(row)
del row, uCursor

This script example deletes all the rows in the data set.

Enjoy

Thursday, May 26, 2011

Point Geometry Object

A point geometry object is a shape that has no length or area at a given scale according to the web help or it can be considered a 0 dimension figure.  The PointGeometry object allows programmers to perform spatial operations without creating a feature class.  The object can be used as both inputs and outputs of geoprocessing.

Here is details about the PointGeometry object:
Details about the methods can be found here.

Example:

import arcpy
# coordinate list
ptList = [[35,-75],[14,-74],[28,-75]]
# create an empty point
pt = arcpy.Point()
# create list to hold PointGeometry objects
ptGeoms = []
for p in ptList:
   pt.x = p[0]
   pt.y = p[1]
   ptGeoms.append(arcpy.PointGeometry(pt))
# create a feature class from pointgeometry objects
arcpy.CopyFeatures_management(ptGeoms, r"in_memory\feat")



Enjoy

Wednesday, May 25, 2011

Point Object

The point object is used frequently with cursors.  Point features return a single object instead of an array of point objects, but all other feature types return an Array of point objects or an array containing multiple arrays of point objects if the feature is a multiple part feature.

A Point is not a geometry class, but used to create a geometry. 
Example:

point = arcpy.Point(-35,74)
ptGeom = arcpy.PointGeometry(point)


The properties of the Point object are as follows:
There are various methods introduced at v10: clone, contains, crosses, etc...  They can be found here.

This should get you started with Point objects. 

Enjoy

Friday, May 20, 2011

Get the Geometry Field

In order to work with geometry in python you need to first get the geometry field. To do that, use the Describe():

import arcpy
fc = r"c:\temp\feature.shp"
desc = arcpy.Describe(fc)
geomField = desc.shapeFieldName


To read the geometry use a search cursor to access the row's geometry field

rows = arcpy.SearchCursor(fc)
for row in rows:
   feat = row.getValue(geomField)
   # do something
del rows, row


Use the getPart() function to get dig into the actual geometry object.

Enjoy

Thursday, May 19, 2011

Search Cursors - Using Expressions

In the last post, search cursor basics were discussed.  When creating search cursors, you can specify many properties, one way to limit the number of row object returned to the end user is the use SQL expressions.


import arcpy
sqlExpression = "\"STATE_NAME\" = 'California'"
fc = r"c:\temp\counties.shp"
rows = arcpy.SearchCursor("C:/Data/Counties.shp", sqlExpression)
# Do stuff with rows


Here only the California counties will be returned.

Enjoy

Wednesday, May 18, 2011

Search Cursors

I couldn't say it better, so from the ArcGIS Help:
"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."

Looking Object Model of SearchCursor() we see the following:

Let do a example:

import arcpy
fc = arcpy.GetParameter(0)
rows = arcpy.SearchCursor(fc)
for row in rows:
   print 'hello rows!'
del rows

This is a very simple example where the end user would provide a feature class as an input and the output would be displayed for each row in the rows object.

Not only is it import to understand cursors, but it's also important to understand what a cursor object will give you. It's a collection of Row objects. Row Objects have collection of methods that include getValue(), setValue(), inNull(), and setNull(). The names pretty much explain what each function does, but here is a graphic to explain it better:
If you use the web help like I often do to look up examples, how-to, etc.. for geoprocessing with python, you'll notice that many times the examples show a field value from a row being returned as such:

value = row.FieldName

Though there is nothing wrong with this, I do not prefer this method. I prefer calling the row method getValue().

value = row.getValue("FieldName")


Also remember, you cannot set values with a SearchCursor(), if you need to alter or add rows/features use the Update or Insert cursor objects.

Enjoy