arcpy search/update cursor not working

5821
11
Jump to solution
04-28-2015 03:56 AM
MarkWisniewski
New Contributor III

Im running my script in ArcGIS 10.2 and the dialog window says completed, but the empty fields in the 'SoilsType' row of the 'mSoils' Feature Class aren't getting populated with the values from the 'SoilsType' field from the 'mLanduse' Feature Class.

cur = arcpy.da.SearchCursor(mLanduse, lField)
for row in cur:
 cur2 = arcpy.da.UpdateCursor(mSoils, sField)
 for row2 in cur2:
   if row2[0] == '' or row2[0] is None:
  row2[0] = lField
  cur2.updateRow(row2)
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Ernst,

It looks like you are trying to update the Soils feature class that have no value for the SoilType with values from a LandUse polygon.  Instead of using an updateCursor, I would recommend using the Spatial Join tool.  You can then join the output from this tool to the Soils feature class, and update the empty rows using the Field Calculator.

View solution in original post

11 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Ernst,

It looks like you are trying to update the Soils feature class that have no value for the SoilType with values from a LandUse polygon.  Instead of using an updateCursor, I would recommend using the Spatial Join tool.  You can then join the output from this tool to the Soils feature class, and update the empty rows using the Field Calculator.

MarkWisniewski
New Contributor III

Hi Jake,

You are correct with my workflow, except the SoilType Field has values in the Landuse Polygon. My query is trying to retrieve just the blank fields in the SoilType Field in the Soils Polygon. Is my workflow wrong?

0 Kudos
curtvprice
MVP Esteemed Contributor

Are you sure your soil types contain blanks or null? (The geodatabase supports null values, unlike shapefiles.)

This query would select both blanks and those that have null values:

query = "SoilType = '' or SoilType IS NULL"

But I think your workflow is wrong, including your use of arcpy.UpdateCursor, the construct you are using with row[0] etc. is what you would use with arcpy.da.UpdateCursor, not the old (10.0) arcpy.UpdateCursor method. Where is the variable LandUse being populated?

I would go with Jake's suggestion.

MarkWisniewski
New Contributor III

Curtis,

I am not seeing any NULL values, and when I do a Definition Query and select Get Unique values '' is the first option, so only blank fields are present.

I am not currently populating the LandUse variable, do I need another MakeFeatureLayer for this?

0 Kudos
curtvprice
MVP Esteemed Contributor

The issue I'm seeing here is you seem to be trying to transfer land use variables over to your blank soil polygons, but you're not doing any spatial overlay in your script, unless I'm missing something. That's what Jake was getting at by suggesting the Join By Location tool.

If you have an ArcGIS Advanced ("ArcInfo") license you could probably solve this problem best with the Identity tool followed by some field calculations.

MarkWisniewski
New Contributor III

Isn't this the spatial overlay? ;

  1. # Make a layer and select soils which fall within the Landuse polygon 
  2. arcpy.SelectLayerByLocation_management('Soils_lyr', 'within', Landuse)

I have ArcGIS Advanced, but I would like to do this in python. I have been referring to this website;

3.2.4 Retrieving records using a spatial query | GEOG 485: GIS Programming and Automation

0 Kudos
curtvprice
MVP Esteemed Contributor

That is just a spatial selection, you are trying to copy attribute data over. To do that you need to do a "real" spatial overlay to create a link between the soil and land use polygon features (ie a "join").

Identity (and related tools Intersect, Erase, etc) are tools you can run interactively or access from arcpy, see the examples in the help. (They require an ArcInfo license to run.)

MarkWisniewski
New Contributor III

But can't I use updateCursor to update existing fields in the table?;

3.3.1 Updating existing records | GEOG 485: GIS Programming and Automation

0 Kudos
curtvprice
MVP Esteemed Contributor

Yes, but where are you going to get the land use data value to update the soil polygon field? You need to somehow relate your blank soil polygons to the land use polygons that overlap it.