Extract by Mask for each feature in a feature class

1190
4
06-18-2010 04:10 PM
RichNauman
New Contributor III
Hello,

I am trying to create a python script to loop through each feature in a feature class and use each feature as the mask for an extract by mask.

Basically I have 12 polygons and want to calculate the mean value of a raster for each polygon - I could split the feature class into 12 feature classes each with one feature and loop through a list of the feature classes but that doesn't seem like the most elegant solution.

Any suggestions?

I tried a using a search cursor to loop through each row and use each row as the mask input value but get  an "Error in executing tool" error.

How do I covert the row object into an input for the tool?

Thanks,

Richard Nauman
Spatial Analysis Program Manager
National Center for Conservation Science and Policy
0 Kudos
4 Replies
KimOllivier
Occasional Contributor III
Create an array object to receive the geometry from the row.
Then you can use that temporary feature as a mask polygon.
0 Kudos
JeffLee
New Contributor II
You need a search cursor and as you loop through the feature class table create a feature layer with each feature, then use the feature layer to clip your raster.  Here is a little bit of code that should help.

Wrows = gp.SearchCursor(WatershedPath)
Wrow = Wrows.next()
Nfeatures = gp.GetCount_management(WatershedPath)
gp.addmessage(str(Nfeatures) + " Records to Process")
count = 1

while Wrow:
[INDENT]      WFValue = str(Wrow.GetValue(WLayerF))
      gp.addmessage("Processing watershed " + str(WFValue))
      gp.MakeFeatureLayer_management(WatershedPath, "lyr", "OBJECTID="+str(Wrow.OBJECTID))
      gp.ExtractbyMask_sa(IFI, "lyr", OutFolder + "/" + WFValue)
      gp.delete("lyr")
      Wrow = Wrows.next()[/INDENT]

Watch for the indent as I just can't figure out how to post python code like in the old forum.  How the hell does one post code in this contraption?
0 Kudos
StefanHaglund1
Esri Contributor
Hello,

I am trying to create a python script to loop through each feature in a feature class and use each feature as the mask for an extract by mask.

Basically I have 12 polygons and want to calculate the mean value of a raster for each polygon - I could split the feature class into 12 feature classes each with one feature and loop through a list of the feature classes but that doesn't seem like the most elegant solution.

Any suggestions?

I tried a using a search cursor to loop through each row and use each row as the mask input value but get  an "Error in executing tool" error.

How do I covert the row object into an input for the tool?

Thanks,

Richard Nauman
Spatial Analysis Program Manager
National Center for Conservation Science and Policy


Hi Richard,

If you want to calculate statistics of a raster you could use the Zonal Statistics tool in Spatial Analyst. It should give you what you want without needing to code.

Cheers,
Nooski
0 Kudos
RichNauman
New Contributor III
Thanks for the replies everyone.

Snoop pointed me in the direction I needed to go - The MakeFeatureLayer_management step with the where clause is what I didn't know how to do.

Richard Nauman
Spatial Analysis Program Manager
National Center for Conservation Science and Policy
0 Kudos